1use std::collections::HashMap;
65use std::future::Future;
66#[cfg(feature = "acquisition-runner")]
67use std::sync::Arc;
68use std::time::Duration;
69
70#[cfg(feature = "charon")]
71use serde::de::DeserializeOwned;
72use serde_json::{Value, json};
73use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
74#[cfg(feature = "acquisition-runner")]
75use tokio::sync::OnceCell;
76use tracing::{debug, info, warn};
77
78#[cfg(feature = "acquisition-runner")]
79use stygian_browser::{
80 AcquisitionMode, AcquisitionRequest, AcquisitionRunner, BrowserConfig, BrowserPool,
81};
82#[cfg(feature = "acquisition-runner")]
83use stygian_charon::AcquisitionModeHint;
84#[cfg(feature = "charon")]
85use stygian_charon::{
86 AcquisitionPolicy, InvestigationBundle, InvestigationReport, RequirementsProfile,
87 RuntimePolicy, TargetClass, TransactionView, build_runtime_policy, classify_transaction,
88 infer_requirements_with_target_class, investigate_har, map_runtime_policy,
89};
90
91use crate::{
92 adapters::{
93 graphql::{GraphQlConfig, GraphQlService},
94 http::{HttpAdapter, HttpConfig},
95 rest_api::RestApiAdapter,
96 rss_feed::RssFeedAdapter,
97 sitemap::SitemapAdapter,
98 },
99 application::pipeline_parser::{NodeDecl, PipelineParser, ServiceDecl},
100 ports::{ScrapingService, ServiceInput},
101};
102
103fn error_response(id: &Value, code: i64, message: &str) -> Value {
106 json!({
107 "jsonrpc": "2.0",
108 "id": id,
109 "error": { "code": code, "message": message }
110 })
111}
112
113fn ok_response(id: &Value, result: Value) -> Value {
114 let mut map = serde_json::Map::new();
115 map.insert("jsonrpc".to_owned(), json!("2.0"));
116 map.insert("id".to_owned(), id.clone());
117 let result = match result {
121 Value::Object(mut obj) => {
122 obj.insert("resultType".to_owned(), json!("complete"));
123 Value::Object(obj)
124 }
125 other => {
126 let mut obj = serde_json::Map::new();
127 obj.insert("resultType".to_owned(), json!("complete"));
128 obj.insert("value".to_owned(), other);
129 Value::Object(obj)
130 }
131 };
132 map.insert("result".to_owned(), result);
133 Value::Object(map)
134}
135
136#[cfg(feature = "charon")]
137fn json_content_response(id: &Value, payload: &Value) -> Value {
138 ok_response(
139 id,
140 json!({
141 "content": [{
142 "type": "text",
143 "text": serde_json::to_string(payload).unwrap_or_default()
144 }]
145 }),
146 )
147}
148
149#[cfg(feature = "charon")]
150fn decode_required_arg<T: DeserializeOwned>(args: &Value, key: &str) -> Result<T, String> {
151 let raw = args
152 .get(key)
153 .cloned()
154 .ok_or_else(|| format!("Missing required parameter: {key}"))?;
155 serde_json::from_value(raw).map_err(|e| format!("Invalid parameter '{key}': {e}"))
156}
157
158#[cfg(feature = "charon")]
159fn parse_target_class_json(value: Option<&Value>) -> Result<TargetClass, String> {
160 let Some(value) = value else {
161 return Ok(TargetClass::Unknown);
162 };
163 let Some(raw) = value.as_str() else {
164 return Err("target_class must be a string".to_string());
165 };
166
167 match raw.trim().to_ascii_lowercase().as_str() {
168 "api" => Ok(TargetClass::Api),
169 "content-site" | "content_site" | "contentsite" | "content" => Ok(TargetClass::ContentSite),
170 "high-security" | "high_security" | "highsecurity" => Ok(TargetClass::HighSecurity),
171 "unknown" => Ok(TargetClass::Unknown),
172 _ => Err(format!("Unknown target_class: {raw}")),
173 }
174}
175
176#[allow(dead_code)] fn extract_meta<'a>(req: &'a Value, key: &str) -> Option<&'a Value> {
187 let meta = req.get("params")?.get("_meta")?.as_object()?;
188 meta.get(&format!("io.modelcontextprotocol/{key}"))
189}
190
191#[allow(dead_code)] fn extract_client_protocol_version(req: &Value) -> Option<String> {
200 extract_meta(req, "protocolVersion")
201 .and_then(Value::as_str)
202 .map(str::to_owned)
203}
204
205#[cfg(test)]
213fn is_supported_protocol_version(client: &str, supported: &[&str]) -> Result<(), String> {
214 if supported.contains(&client) {
215 Ok(())
216 } else {
217 Err(format!("Unsupported protocol version: {client}"))
218 }
219}
220
221pub struct McpGraphServer;
239
240impl McpGraphServer {
241 #[must_use]
243 pub const fn new() -> Self {
244 Self
245 }
246
247 pub async fn run() -> Result<(), Box<dyn std::error::Error>> {
254 info!("stygian-graph MCP server starting");
255
256 let stdin = tokio::io::stdin();
257 let mut reader = BufReader::new(stdin);
258 let mut stdout = tokio::io::stdout();
259 let mut line = String::new();
260
261 loop {
262 line.clear();
263 let bytes = reader.read_line(&mut line).await?;
264 if bytes == 0 {
265 break; }
267
268 let trimmed = line.trim();
269 if trimmed.is_empty() {
270 continue;
271 }
272
273 debug!(request = trimmed, "received");
274
275 let response = match serde_json::from_str::<Value>(trimmed) {
276 Ok(req) => {
277 let is_well_formed_notification = req.is_object()
278 && req.get("jsonrpc").and_then(Value::as_str) == Some("2.0")
279 && req.get("id").is_none()
280 && req.get("method").and_then(Value::as_str).is_some();
281 let response = Self::handle(&req).await;
282 if is_well_formed_notification {
283 continue;
284 }
285 response
286 }
287 Err(e) => json!({
288 "jsonrpc": "2.0",
289 "id": null,
290 "error": { "code": -32700, "message": format!("Parse error: {e}") }
291 }),
292 };
293
294 let mut out = serde_json::to_string(&response)?;
295 out.push('\n');
296 stdout.write_all(out.as_bytes()).await?;
297 stdout.flush().await?;
298 }
299
300 info!("stygian-graph MCP server stopped");
301 Ok(())
302 }
303
304 pub async fn handle_request(req: &Value) -> Value {
325 Self::handle(req).await
326 }
327
328 async fn handle(req: &Value) -> Value {
329 let null = Value::Null;
330 let id = req.get("id").unwrap_or(&null);
331 let method = req.get("method").and_then(Value::as_str).unwrap_or("");
332
333 match method {
334 "server/discover" => Self::handle_discover(id),
338 "tools/list" => Self::handle_tools_list(id),
339 "tools/call" => Self::handle_tools_call(id, req).await,
340 _ => error_response(id, -32601, &format!("Method not found: {method}")),
341 }
342 }
343
344 fn handle_discover(id: &Value) -> Value {
348 ok_response(
349 id,
350 json!({
351 "protocolVersion": "2026-07-28",
352 "supportedProtocolVersions": ["2026-07-28"],
353 "capabilities": {
354 "tools": { "listChanged": false },
355 "resources": { "listChanged": false }
356 },
357 "serverInfo": {
358 "name": "stygian-graph",
359 "version": env!("CARGO_PKG_VERSION")
360 },
361 "extensions": []
362 }),
363 )
364 }
365
366 fn scraping_tool_defs() -> Vec<Value> {
367 vec![
368 json!({
369 "name": "scrape",
370 "description": "Fetch a URL with anti-bot UA rotation and retry logic. Returns raw HTML/JSON content and response metadata.",
371 "inputSchema": {
372 "type": "object",
373 "properties": {
374 "url": { "type": "string", "description": "Target URL" },
375 "timeout_secs": { "type": "integer", "description": "Request timeout in seconds (default: 30)" },
376 "proxy_url": { "type": "string", "description": "HTTP/SOCKS5 proxy URL (e.g. socks5://user:pass@host:1080). Only pass this when the user has explicitly requested proxy use. Do NOT populate this field by default." },
377 "rotate_ua": { "type": "boolean", "description": "Rotate User-Agent on each request (default: true)" }
378 },
379 "required": ["url"]
380 }
381 }),
382 json!({
383 "name": "scrape_rest",
384 "description": "Call a REST/JSON API. Supports bearer/API-key auth, arbitrary HTTP methods, query parameters, request bodies, pagination, and response path extraction.",
385 "inputSchema": {
386 "type": "object",
387 "properties": {
388 "url": { "type": "string", "description": "API endpoint URL" },
389 "method": { "type": "string", "description": "HTTP method (GET, POST, PUT, PATCH, DELETE — default: GET)" },
390 "auth": {
391 "type": "object",
392 "description": "Authentication config",
393 "properties": {
394 "type": { "type": "string", "description": "bearer | api_key | basic | header" },
395 "token": { "type": "string", "description": "Token or credential value" },
396 "header":{ "type": "string", "description": "Custom header name (for type=header)" }
397 }
398 },
399 "query": { "type": "object", "description": "URL query parameters as key-value pairs" },
400 "body": { "type": "object", "description": "Request body (JSON)" },
401 "headers": { "type": "object", "description": "Custom request headers" },
402 "pagination": {
403 "type": "object",
404 "description": "Pagination config",
405 "properties": {
406 "strategy": { "type": "string", "description": "link_header | offset | cursor" },
407 "max_pages": { "type": "integer", "description": "Maximum pages to fetch (default: 1)" }
408 }
409 },
410 "data_path": { "type": "string", "description": "Dot-separated JSON path to extract (e.g. data.items)" }
411 },
412 "required": ["url"]
413 }
414 }),
415 json!({
416 "name": "scrape_graphql",
417 "description": "Execute a GraphQL query against any spec-compliant endpoint. Supports bearer/API-key auth, variables, and dot-path data extraction.",
418 "inputSchema": {
419 "type": "object",
420 "properties": {
421 "url": { "type": "string", "description": "GraphQL endpoint URL" },
422 "query": { "type": "string", "description": "GraphQL query or mutation string" },
423 "variables": { "type": "object", "description": "Query variables (JSON object)" },
424 "auth": {
425 "type": "object",
426 "description": "Auth config",
427 "properties": {
428 "kind": { "type": "string", "description": "bearer | api_key | header | none" },
429 "token": { "type": "string", "description": "Auth token or key" },
430 "header_name": { "type": "string", "description": "Custom header name (default: X-Api-Key)" }
431 }
432 },
433 "data_path": { "type": "string", "description": "Dot-separated path to extract from response (e.g. data.countries)" },
434 "timeout_secs": { "type": "integer", "description": "Request timeout in seconds (default: 30)" }
435 },
436 "required": ["url", "query"]
437 }
438 }),
439 json!({
440 "name": "scrape_sitemap",
441 "description": "Parse a sitemap.xml or sitemap index and return all discovered URLs with their priorities and change frequencies.",
442 "inputSchema": {
443 "type": "object",
444 "properties": {
445 "url": { "type": "string", "description": "Sitemap URL (sitemap.xml or sitemap index)" },
446 "max_depth": { "type": "integer", "description": "Maximum sitemap index recursion depth (default: 5)" }
447 },
448 "required": ["url"]
449 }
450 }),
451 json!({
452 "name": "scrape_rss",
453 "description": "Parse an RSS or Atom feed and return all entries as structured JSON.",
454 "inputSchema": {
455 "type": "object",
456 "properties": {
457 "url": { "type": "string", "description": "RSS/Atom feed URL" }
458 },
459 "required": ["url"]
460 }
461 }),
462 ]
463 }
464
465 fn graph_tool_defs() -> Vec<Value> {
466 let mut tools = vec![
467 json!({
468 "name": "pipeline_validate",
469 "description": "Parse and validate a TOML pipeline definition without executing it. Returns the node list, service declarations, and computed execution order.",
470 "inputSchema": {
471 "type": "object",
472 "properties": {
473 "toml": { "type": "string", "description": "TOML pipeline definition string" }
474 },
475 "required": ["toml"]
476 }
477 }),
478 json!({
479 "name": "pipeline_run",
480 "description": "Parse, validate, and execute a TOML pipeline DAG. HTTP, REST, GraphQL, sitemap, and RSS nodes are executed. AI nodes and browser nodes without opt-in acquisition config are recorded in the skipped list.",
481 "inputSchema": {
482 "type": "object",
483 "properties": {
484 "toml": { "type": "string", "description": "TOML pipeline definition string" },
485 "timeout_secs": { "type": "integer", "description": "Per-node timeout in seconds (default: 30)" }
486 },
487 "required": ["toml"]
488 }
489 }),
490 json!({
491 "name": "inspect",
492 "description": "Get a complete snapshot of a pipeline's graph structure including nodes, edges, execution waves, critical path, and connectivity metrics.",
493 "inputSchema": {
494 "type": "object",
495 "properties": {
496 "toml": { "type": "string", "description": "TOML pipeline definition string" }
497 },
498 "required": ["toml"]
499 }
500 }),
501 json!({
502 "name": "node_info",
503 "description": "Get detailed information about a specific node in the pipeline graph, including its service type, depth, predecessors, and successors.",
504 "inputSchema": {
505 "type": "object",
506 "properties": {
507 "toml": { "type": "string", "description": "TOML pipeline definition string" },
508 "node_id": { "type": "string", "description": "Node ID to inspect" }
509 },
510 "required": ["toml", "node_id"]
511 }
512 }),
513 json!({
514 "name": "impact",
515 "description": "Analyze what would be affected by changing a node. Returns all upstream dependencies and downstream dependents.",
516 "inputSchema": {
517 "type": "object",
518 "properties": {
519 "toml": { "type": "string", "description": "TOML pipeline definition string" },
520 "node_id": { "type": "string", "description": "Node ID to analyze impact for" }
521 },
522 "required": ["toml", "node_id"]
523 }
524 }),
525 json!({
526 "name": "query_nodes",
527 "description": "Query nodes in the pipeline graph by various criteria: service type, root/leaf status, depth range, or ID pattern.",
528 "inputSchema": {
529 "type": "object",
530 "properties": {
531 "toml": { "type": "string", "description": "TOML pipeline definition string" },
532 "service": { "type": "string", "description": "Filter by service type (http, ai, browser, etc.)" },
533 "id_pattern": { "type": "string", "description": "Filter by node ID substring match" },
534 "is_root": { "type": "boolean", "description": "Only return root nodes (no predecessors)" },
535 "is_leaf": { "type": "boolean", "description": "Only return leaf nodes (no successors)" },
536 "min_depth": { "type": "integer", "description": "Minimum depth from root nodes" },
537 "max_depth": { "type": "integer", "description": "Maximum depth from root nodes" }
538 },
539 "required": ["toml"]
540 }
541 }),
542 ];
543
544 #[cfg(feature = "charon")]
545 tools.extend(Self::charon_tool_defs());
546
547 tools
548 }
549
550 #[cfg(feature = "charon")]
551 fn charon_tool_defs() -> Vec<Value> {
552 vec![
553 json!({
554 "name": "charon_classify_transaction",
555 "description": "Classify a single HTTP transaction for likely anti-bot provider signals.",
556 "inputSchema": {
557 "type": "object",
558 "properties": {
559 "url": { "type": "string", "description": "Request URL" },
560 "status": { "type": "integer", "description": "HTTP status code" },
561 "response_headers": { "type": "object", "description": "Response headers as a string map" },
562 "response_body_snippet": { "type": "string", "description": "Optional response body snippet" },
563 "response_body_excerpt": { "type": "string", "description": "Alias for response_body_snippet" }
564 },
565 "required": ["url", "status"]
566 }
567 }),
568 json!({
569 "name": "charon_investigate_har",
570 "description": "Build a Charon investigation report from a HAR payload.",
571 "inputSchema": {
572 "type": "object",
573 "properties": {
574 "har": { "type": "string", "description": "HAR JSON payload" },
575 "target_class": { "type": "string", "description": "Optional target class: api | content-site | high-security | unknown" }
576 },
577 "required": ["har"]
578 }
579 }),
580 json!({
581 "name": "charon_infer_requirements",
582 "description": "Infer Charon operational requirements from an investigation report.",
583 "inputSchema": {
584 "type": "object",
585 "properties": {
586 "report": { "type": "object", "description": "InvestigationReport JSON object" },
587 "target_class": { "type": "string", "description": "Optional target class override: api | content-site | high-security | unknown" }
588 },
589 "required": ["report"]
590 }
591 }),
592 json!({
593 "name": "charon_build_runtime_policy",
594 "description": "Build a runtime policy from a Charon investigation report and inferred requirements profile.",
595 "inputSchema": {
596 "type": "object",
597 "properties": {
598 "report": { "type": "object", "description": "InvestigationReport JSON object" },
599 "requirements": { "type": "object", "description": "RequirementsProfile JSON object" }
600 },
601 "required": ["report", "requirements"]
602 }
603 }),
604 json!({
605 "name": "charon_map_runtime_policy",
606 "description": "Map a Charon runtime policy into acquisition hints for downstream runners.",
607 "inputSchema": {
608 "type": "object",
609 "properties": {
610 "policy": { "type": "object", "description": "RuntimePolicy JSON object" }
611 },
612 "required": ["policy"]
613 }
614 }),
615 json!({
616 "name": "charon_analyze_and_plan",
617 "description": "Run end-to-end Charon HAR analysis, requirement inference, runtime policy planning, and acquisition mapping in one call.",
618 "inputSchema": {
619 "type": "object",
620 "properties": {
621 "har": { "type": "string", "description": "HAR JSON payload" },
622 "target_class": { "type": "string", "description": "Optional target class: api | content-site | high-security | unknown" }
623 },
624 "required": ["har"]
625 }
626 }),
627 ]
628 }
629
630 fn handle_tools_list(id: &Value) -> Value {
631 let mut tools = Self::scraping_tool_defs();
632 tools.extend(Self::graph_tool_defs());
633 ok_response(id, json!({ "tools": tools }))
634 }
635
636 async fn handle_tools_call(id: &Value, req: &Value) -> Value {
637 let null = Value::Null;
638 let params = req.get("params").unwrap_or(&null);
639 let name = params.get("name").and_then(Value::as_str).unwrap_or("");
640 let args = params.get("arguments").cloned().unwrap_or(Value::Null);
641
642 match name {
643 "scrape" => Self::tool_scrape(id, &args).await,
644 "scrape_rest" => Self::tool_scrape_rest(id, &args).await,
645 "scrape_graphql" => Self::tool_scrape_graphql(id, &args).await,
646 "scrape_sitemap" => Self::tool_scrape_sitemap(id, &args).await,
647 "scrape_rss" => Self::tool_scrape_rss(id, &args).await,
648 "pipeline_validate" => Self::tool_pipeline_validate(id, &args),
649 "pipeline_run" => Self::tool_pipeline_run(id, &args).await,
650 "inspect" => Self::tool_graph_inspect(id, &args),
651 "node_info" => Self::tool_graph_node_info(id, &args),
652 "impact" => Self::tool_graph_impact(id, &args),
653 "query_nodes" => Self::tool_graph_query(id, &args),
654 #[cfg(feature = "charon")]
655 "charon_classify_transaction" => Self::tool_charon_classify_transaction(id, &args),
656 #[cfg(feature = "charon")]
657 "charon_investigate_har" => Self::tool_charon_investigate_har(id, &args),
658 #[cfg(feature = "charon")]
659 "charon_infer_requirements" => Self::tool_charon_infer_requirements(id, &args),
660 #[cfg(feature = "charon")]
661 "charon_build_runtime_policy" => Self::tool_charon_build_runtime_policy(id, &args),
662 #[cfg(feature = "charon")]
663 "charon_map_runtime_policy" => Self::tool_charon_map_runtime_policy(id, &args),
664 #[cfg(feature = "charon")]
665 "charon_analyze_and_plan" => Self::tool_charon_analyze_and_plan(id, &args),
666 _ => error_response(id, -32602, &format!("Unknown tool: {name}")),
667 }
668 }
669
670 #[cfg(feature = "charon")]
671 fn tool_charon_classify_transaction(id: &Value, args: &Value) -> Value {
672 let Some(url) = args.get("url").and_then(Value::as_str) else {
673 return error_response(id, -32602, "Missing required parameter: url");
674 };
675 let Some(status_u64) = args.get("status").and_then(Value::as_u64) else {
676 return error_response(id, -32602, "Missing required parameter: status");
677 };
678 let Ok(status) = u16::try_from(status_u64) else {
679 return error_response(id, -32602, "status must fit in a 16-bit unsigned integer");
680 };
681
682 let response_headers = match args.get("response_headers") {
683 Some(value) if !value.is_null() => {
684 match serde_json::from_value::<std::collections::BTreeMap<String, String>>(
685 value.clone(),
686 ) {
687 Ok(headers) => headers,
688 Err(e) => {
689 return error_response(
690 id,
691 -32602,
692 &format!("Invalid parameter 'response_headers': {e}"),
693 );
694 }
695 }
696 }
697 _ => std::collections::BTreeMap::new(),
698 };
699 let response_body_snippet = args
700 .get("response_body_snippet")
701 .or_else(|| args.get("response_body_excerpt"))
702 .and_then(Value::as_str)
703 .map(str::to_string);
704
705 let tx = TransactionView {
706 url: url.to_string(),
707 status,
708 response_headers,
709 response_body_snippet,
710 };
711 let detection = classify_transaction(&tx);
712 json_content_response(id, &json!({ "detection": detection }))
713 }
714
715 #[cfg(feature = "charon")]
716 fn tool_charon_investigate_har(id: &Value, args: &Value) -> Value {
717 let Some(har) = args.get("har").and_then(Value::as_str) else {
718 return error_response(id, -32602, "Missing required parameter: har");
719 };
720 let target_class = match parse_target_class_json(args.get("target_class")) {
721 Ok(target_class) => target_class,
722 Err(e) => return error_response(id, -32602, &e),
723 };
724
725 match investigate_har(har) {
726 Ok(mut report) => {
727 report.target_class = Some(target_class);
728 json_content_response(id, &json!({ "report": report }))
729 }
730 Err(e) => error_response(id, -32603, &format!("HAR investigation failed: {e}")),
731 }
732 }
733
734 #[cfg(feature = "charon")]
735 fn tool_charon_infer_requirements(id: &Value, args: &Value) -> Value {
736 let mut report: InvestigationReport = match decode_required_arg(args, "report") {
737 Ok(report) => report,
738 Err(e) => return error_response(id, -32602, &e),
739 };
740 let target_class = match parse_target_class_json(args.get("target_class")) {
741 Ok(TargetClass::Unknown) => report.target_class.unwrap_or(TargetClass::Unknown),
742 Ok(target_class) => target_class,
743 Err(e) => return error_response(id, -32602, &e),
744 };
745
746 report.target_class = Some(target_class);
747 let requirements = infer_requirements_with_target_class(&report, target_class);
748 json_content_response(id, &json!({ "requirements": requirements }))
749 }
750
751 #[cfg(feature = "charon")]
752 fn tool_charon_build_runtime_policy(id: &Value, args: &Value) -> Value {
753 let report: InvestigationReport = match decode_required_arg(args, "report") {
754 Ok(report) => report,
755 Err(e) => return error_response(id, -32602, &e),
756 };
757 let requirements: RequirementsProfile = match decode_required_arg(args, "requirements") {
758 Ok(requirements) => requirements,
759 Err(e) => return error_response(id, -32602, &e),
760 };
761
762 let policy = build_runtime_policy(&report, &requirements);
763 json_content_response(id, &json!({ "policy": policy }))
764 }
765
766 #[cfg(feature = "charon")]
767 fn tool_charon_map_runtime_policy(id: &Value, args: &Value) -> Value {
768 let policy: RuntimePolicy = match decode_required_arg(args, "policy") {
769 Ok(policy) => policy,
770 Err(e) => return error_response(id, -32602, &e),
771 };
772
773 let acquisition: AcquisitionPolicy = map_runtime_policy(&policy);
774 json_content_response(id, &json!({ "acquisition": acquisition }))
775 }
776
777 #[cfg(feature = "charon")]
778 fn tool_charon_analyze_and_plan(id: &Value, args: &Value) -> Value {
779 let Some(har) = args.get("har").and_then(Value::as_str) else {
780 return error_response(id, -32602, "Missing required parameter: har");
781 };
782 let target_class = match parse_target_class_json(args.get("target_class")) {
783 Ok(target_class) => target_class,
784 Err(e) => return error_response(id, -32602, &e),
785 };
786
787 match investigate_har(har) {
788 Ok(mut report) => {
789 report.target_class = Some(target_class);
790 let requirements = infer_requirements_with_target_class(&report, target_class);
791 let policy = build_runtime_policy(&report, &requirements);
792 let acquisition = map_runtime_policy(&policy);
793 let bundle = InvestigationBundle {
794 report,
795 requirements,
796 policy,
797 };
798 json_content_response(id, &json!({ "bundle": bundle, "acquisition": acquisition }))
799 }
800 Err(e) => error_response(id, -32603, &format!("HAR investigation failed: {e}")),
801 }
802 }
803
804 async fn tool_scrape(id: &Value, args: &Value) -> Value {
807 let Some(url) = args.get("url").and_then(Value::as_str) else {
808 return error_response(id, -32602, "Missing required parameter: url");
809 };
810
811 let timeout_secs = args
812 .get("timeout_secs")
813 .and_then(Value::as_u64)
814 .unwrap_or(30);
815 let proxy_url = args
816 .get("proxy_url")
817 .and_then(Value::as_str)
818 .map(str::to_string);
819 let rotate_ua = args
820 .get("rotate_ua")
821 .and_then(Value::as_bool)
822 .unwrap_or(true);
823
824 let config = HttpConfig {
825 timeout: std::time::Duration::from_secs(timeout_secs),
826 proxy_url,
827 rotate_user_agent: rotate_ua,
828 ..HttpConfig::default()
829 };
830 let adapter = HttpAdapter::with_config(config);
831 let input = ServiceInput {
832 url: url.to_string(),
833 params: json!({}),
834 };
835
836 match adapter.execute(input).await {
837 Ok(output) => ok_response(
838 id,
839 json!({
840 "content": [{
841 "type": "text",
842 "text": serde_json::to_string(&json!({
843 "data": output.data,
844 "metadata": output.metadata
845 })).unwrap_or_default()
846 }]
847 }),
848 ),
849 Err(e) => error_response(id, -32603, &format!("Scrape failed: {e}")),
850 }
851 }
852
853 async fn tool_scrape_rest(id: &Value, args: &Value) -> Value {
856 let Some(url) = args.get("url").and_then(Value::as_str) else {
857 return error_response(id, -32602, "Missing required parameter: url");
858 };
859
860 let mut map = serde_json::Map::new();
863 if let Some(method) = args.get("method").and_then(Value::as_str) {
864 map.insert("method".to_owned(), json!(method));
865 }
866 if let Some(auth) = args.get("auth").filter(|v| !v.is_null()) {
867 map.insert("auth".to_owned(), auth.clone());
868 }
869 if let Some(query) = args.get("query").filter(|v| !v.is_null()) {
870 map.insert("query".to_owned(), query.clone());
871 }
872 if let Some(body) = args.get("body").filter(|v| !v.is_null()) {
873 map.insert("body".to_owned(), body.clone());
874 }
875 if let Some(headers) = args.get("headers").filter(|v| !v.is_null()) {
876 map.insert("headers".to_owned(), headers.clone());
877 }
878 if let Some(pagination) = args.get("pagination").filter(|v| !v.is_null()) {
879 map.insert("pagination".to_owned(), pagination.clone());
880 }
881 if let Some(dp) = args.get("data_path").and_then(Value::as_str) {
882 map.insert("response".to_owned(), json!({ "data_path": dp }));
883 }
884 let params = Value::Object(map);
885
886 let adapter = RestApiAdapter::new();
887 let input = ServiceInput {
888 url: url.to_string(),
889 params,
890 };
891
892 match adapter.execute(input).await {
893 Ok(output) => ok_response(
894 id,
895 json!({
896 "content": [{
897 "type": "text",
898 "text": serde_json::to_string(&json!({
899 "data": output.data,
900 "metadata": output.metadata
901 })).unwrap_or_default()
902 }]
903 }),
904 ),
905 Err(e) => error_response(id, -32603, &format!("REST scrape failed: {e}")),
906 }
907 }
908
909 async fn tool_scrape_graphql(id: &Value, args: &Value) -> Value {
912 let Some(url) = args.get("url").and_then(Value::as_str) else {
913 return error_response(id, -32602, "Missing required parameter: url");
914 };
915 let Some(query) = args.get("query").and_then(Value::as_str) else {
916 return error_response(id, -32602, "Missing required parameter: query");
917 };
918
919 let timeout_secs = args
920 .get("timeout_secs")
921 .and_then(Value::as_u64)
922 .unwrap_or(30);
923
924 let config = GraphQlConfig {
925 timeout_secs,
926 ..GraphQlConfig::default()
927 };
928 let service = GraphQlService::new(config, None);
929
930 let mut gql_map = serde_json::Map::new();
931 gql_map.insert("query".to_owned(), json!(query));
932 if let Some(variables) = args.get("variables").filter(|v| !v.is_null()) {
933 gql_map.insert("variables".to_owned(), variables.clone());
934 }
935 if let Some(auth) = args.get("auth").filter(|v| !v.is_null()) {
936 gql_map.insert("auth".to_owned(), auth.clone());
937 }
938 if let Some(dp) = args.get("data_path").and_then(Value::as_str) {
939 gql_map.insert("data_path".to_owned(), json!(dp));
940 }
941 let params = Value::Object(gql_map);
942
943 let input = ServiceInput {
944 url: url.to_string(),
945 params,
946 };
947
948 match service.execute(input).await {
949 Ok(output) => ok_response(
950 id,
951 json!({
952 "content": [{
953 "type": "text",
954 "text": serde_json::to_string(&json!({
955 "data": output.data,
956 "metadata": output.metadata
957 })).unwrap_or_default()
958 }]
959 }),
960 ),
961 Err(e) => error_response(id, -32603, &format!("GraphQL scrape failed: {e}")),
962 }
963 }
964
965 async fn tool_scrape_sitemap(id: &Value, args: &Value) -> Value {
968 let Some(url) = args.get("url").and_then(Value::as_str) else {
969 return error_response(id, -32602, "Missing required parameter: url");
970 };
971
972 let max_depth = args
973 .get("max_depth")
974 .and_then(Value::as_u64)
975 .map_or(5, |v| usize::try_from(v).unwrap_or(5));
976 let client = reqwest::Client::new();
977 let adapter = SitemapAdapter::new(client, max_depth);
978 let input = ServiceInput {
979 url: url.to_string(),
980 params: json!({}),
981 };
982
983 match adapter.execute(input).await {
984 Ok(output) => ok_response(
985 id,
986 json!({
987 "content": [{
988 "type": "text",
989 "text": serde_json::to_string(&json!({
990 "data": output.data,
991 "metadata": output.metadata
992 })).unwrap_or_default()
993 }]
994 }),
995 ),
996 Err(e) => error_response(id, -32603, &format!("Sitemap scrape failed: {e}")),
997 }
998 }
999
1000 async fn tool_scrape_rss(id: &Value, args: &Value) -> Value {
1003 let Some(url) = args.get("url").and_then(Value::as_str) else {
1004 return error_response(id, -32602, "Missing required parameter: url");
1005 };
1006
1007 let client = reqwest::Client::new();
1008 let adapter = RssFeedAdapter::new(client);
1009 let input = ServiceInput {
1010 url: url.to_string(),
1011 params: json!({}),
1012 };
1013
1014 match adapter.execute(input).await {
1015 Ok(output) => ok_response(
1016 id,
1017 json!({
1018 "content": [{
1019 "type": "text",
1020 "text": serde_json::to_string(&json!({
1021 "data": output.data,
1022 "metadata": output.metadata
1023 })).unwrap_or_default()
1024 }]
1025 }),
1026 ),
1027 Err(e) => error_response(id, -32603, &format!("RSS scrape failed: {e}")),
1028 }
1029 }
1030
1031 fn tool_pipeline_validate(id: &Value, args: &Value) -> Value {
1034 let Some(toml) = args.get("toml").and_then(Value::as_str) else {
1035 return error_response(id, -32602, "Missing required parameter: toml");
1036 };
1037
1038 let def = match PipelineParser::from_str(toml) {
1039 Ok(d) => d,
1040 Err(e) => return error_response(id, -32603, &format!("Parse error: {e}")),
1041 };
1042
1043 if let Err(e) = def.validate() {
1044 return ok_response(
1045 id,
1046 json!({
1047 "content": [{
1048 "type": "text",
1049 "text": serde_json::to_string(&json!({
1050 "valid": false,
1051 "error": e.to_string(),
1052 "nodes": def.nodes.len(),
1053 "services": def.services.len()
1054 })).unwrap_or_default()
1055 }]
1056 }),
1057 );
1058 }
1059
1060 let order = match def.topological_order() {
1061 Ok(o) => o,
1062 Err(e) => return error_response(id, -32603, &format!("Topology error: {e}")),
1063 };
1064
1065 let node_info: Vec<Value> = def
1066 .nodes
1067 .iter()
1068 .map(|n| {
1069 json!({
1070 "name": n.name,
1071 "service": n.service,
1072 "url": n.url,
1073 "depends_on": n.depends_on
1074 })
1075 })
1076 .collect();
1077
1078 let svc_info: Vec<Value> = def
1079 .services
1080 .iter()
1081 .map(|s| {
1082 json!({
1083 "name": s.name,
1084 "kind": s.kind,
1085 "model": s.model
1086 })
1087 })
1088 .collect();
1089
1090 ok_response(
1091 id,
1092 json!({
1093 "content": [{
1094 "type": "text",
1095 "text": serde_json::to_string(&json!({
1096 "valid": true,
1097 "node_count": def.nodes.len(),
1098 "service_count": def.services.len(),
1099 "execution_order": order,
1100 "nodes": node_info,
1101 "services": svc_info
1102 })).unwrap_or_default()
1103 }]
1104 }),
1105 )
1106 }
1107
1108 async fn tool_pipeline_run(id: &Value, args: &Value) -> Value {
1111 let Some(toml) = args.get("toml").and_then(Value::as_str) else {
1112 return error_response(id, -32602, "Missing required parameter: toml");
1113 };
1114
1115 let timeout_secs = args
1116 .get("timeout_secs")
1117 .and_then(Value::as_u64)
1118 .unwrap_or(30);
1119
1120 let def = match PipelineParser::from_str(toml) {
1121 Ok(d) => d,
1122 Err(e) => return error_response(id, -32603, &format!("Parse error: {e}")),
1123 };
1124
1125 if let Err(e) = def.validate() {
1126 return error_response(id, -32603, &format!("Validation error: {e}"));
1127 }
1128
1129 let order = match def.topological_order() {
1130 Ok(o) => o,
1131 Err(e) => return error_response(id, -32603, &format!("Topology error: {e}")),
1132 };
1133
1134 let svc_kinds: HashMap<String, ServiceDecl> = def
1135 .services
1136 .iter()
1137 .map(|s| (s.name.clone(), s.clone()))
1138 .collect();
1139
1140 let mut outputs: HashMap<String, Value> = HashMap::new();
1141 let mut skipped: Vec<String> = Vec::new();
1142 let mut errors: HashMap<String, String> = HashMap::new();
1143
1144 for node_name in &order {
1145 let Some(node) = def.nodes.iter().find(|n| n.name == *node_name) else {
1146 continue;
1147 };
1148
1149 let kind = svc_kinds
1150 .get(&node.service)
1151 .map_or(node.service.as_str(), |s| s.kind.as_str());
1152
1153 let Some(url) = node.url.as_deref() else {
1155 skipped.push(node_name.clone());
1156 continue;
1157 };
1158
1159 match execute_pipeline_node(kind, url, node_name, node, timeout_secs).await {
1160 Some(Ok(out)) => {
1161 outputs.insert(node_name.clone(), out);
1162 }
1163 Some(Err(e)) => {
1164 errors.insert(node_name.clone(), e);
1165 }
1166 None => {
1167 skipped.push(node_name.clone());
1168 }
1169 }
1170 }
1171
1172 ok_response(
1173 id,
1174 json!({
1175 "content": [{
1176 "type": "text",
1177 "text": serde_json::to_string(&json!({
1178 "execution_order": order,
1179 "outputs": outputs,
1180 "skipped": skipped,
1181 "errors": errors
1182 })).unwrap_or_default()
1183 }]
1184 }),
1185 )
1186 }
1187
1188 fn tool_graph_inspect(id: &Value, args: &Value) -> Value {
1191 let Some(toml) = args.get("toml").and_then(Value::as_str) else {
1192 return error_response(id, -32602, "Missing required parameter: toml");
1193 };
1194
1195 let def = match PipelineParser::from_str(toml) {
1196 Ok(d) => d,
1197 Err(e) => return error_response(id, -32603, &format!("Parse error: {e}")),
1198 };
1199
1200 if let Err(e) = def.validate() {
1201 return error_response(id, -32603, &format!("Validation error: {e}"));
1202 }
1203
1204 let mut pipeline = crate::domain::graph::Pipeline::new("pipeline");
1206 for node in &def.nodes {
1207 pipeline.add_node(crate::domain::graph::Node::with_metadata(
1208 &node.name,
1209 &node.service,
1210 serde_json::json!({
1211 "url": node.url,
1212 "params": toml_to_json(&toml::Value::Table(
1213 node.params.iter()
1214 .map(|(k, v)| (k.clone(), v.clone()))
1215 .collect()
1216 ))
1217 }),
1218 serde_json::Value::Null,
1219 ));
1220 for dep in &node.depends_on {
1221 pipeline.add_edge(crate::domain::graph::Edge::new(dep, &node.name));
1222 }
1223 }
1224
1225 let executor = match crate::domain::graph::DagExecutor::from_pipeline(&pipeline) {
1226 Ok(e) => e,
1227 Err(e) => return error_response(id, -32603, &format!("Graph build error: {e}")),
1228 };
1229
1230 let snapshot = executor.snapshot();
1231
1232 ok_response(
1233 id,
1234 json!({
1235 "content": [{
1236 "type": "text",
1237 "text": serde_json::to_string(&snapshot).unwrap_or_default()
1238 }]
1239 }),
1240 )
1241 }
1242
1243 fn tool_graph_node_info(id: &Value, args: &Value) -> Value {
1244 let Some(toml) = args.get("toml").and_then(Value::as_str) else {
1245 return error_response(id, -32602, "Missing required parameter: toml");
1246 };
1247 let Some(node_id) = args.get("node_id").and_then(Value::as_str) else {
1248 return error_response(id, -32602, "Missing required parameter: node_id");
1249 };
1250
1251 let def = match PipelineParser::from_str(toml) {
1252 Ok(d) => d,
1253 Err(e) => return error_response(id, -32603, &format!("Parse error: {e}")),
1254 };
1255
1256 if let Err(e) = def.validate() {
1257 return error_response(id, -32603, &format!("Validation error: {e}"));
1258 }
1259
1260 let mut pipeline = crate::domain::graph::Pipeline::new("pipeline");
1261 for node in &def.nodes {
1262 pipeline.add_node(crate::domain::graph::Node::with_metadata(
1263 &node.name,
1264 &node.service,
1265 serde_json::json!({
1266 "url": node.url,
1267 "params": toml_to_json(&toml::Value::Table(
1268 node.params.iter()
1269 .map(|(k, v)| (k.clone(), v.clone()))
1270 .collect()
1271 ))
1272 }),
1273 serde_json::Value::Null,
1274 ));
1275 for dep in &node.depends_on {
1276 pipeline.add_edge(crate::domain::graph::Edge::new(dep, &node.name));
1277 }
1278 }
1279
1280 let executor = match crate::domain::graph::DagExecutor::from_pipeline(&pipeline) {
1281 Ok(e) => e,
1282 Err(e) => return error_response(id, -32603, &format!("Graph build error: {e}")),
1283 };
1284
1285 executor.node_info(node_id).map_or_else(
1286 || error_response(id, -32602, &format!("Node not found: {node_id}")),
1287 |info| {
1288 ok_response(
1289 id,
1290 json!({
1291 "content": [{
1292 "type": "text",
1293 "text": serde_json::to_string(&info).unwrap_or_default()
1294 }]
1295 }),
1296 )
1297 },
1298 )
1299 }
1300
1301 fn tool_graph_impact(id: &Value, args: &Value) -> Value {
1302 let Some(toml) = args.get("toml").and_then(Value::as_str) else {
1303 return error_response(id, -32602, "Missing required parameter: toml");
1304 };
1305 let Some(node_id) = args.get("node_id").and_then(Value::as_str) else {
1306 return error_response(id, -32602, "Missing required parameter: node_id");
1307 };
1308
1309 let def = match PipelineParser::from_str(toml) {
1310 Ok(d) => d,
1311 Err(e) => return error_response(id, -32603, &format!("Parse error: {e}")),
1312 };
1313
1314 if let Err(e) = def.validate() {
1315 return error_response(id, -32603, &format!("Validation error: {e}"));
1316 }
1317
1318 let mut pipeline = crate::domain::graph::Pipeline::new("pipeline");
1319 for node in &def.nodes {
1320 pipeline.add_node(crate::domain::graph::Node::with_metadata(
1321 &node.name,
1322 &node.service,
1323 serde_json::json!({
1324 "url": node.url,
1325 "params": toml_to_json(&toml::Value::Table(
1326 node.params.iter()
1327 .map(|(k, v)| (k.clone(), v.clone()))
1328 .collect()
1329 ))
1330 }),
1331 serde_json::Value::Null,
1332 ));
1333 for dep in &node.depends_on {
1334 pipeline.add_edge(crate::domain::graph::Edge::new(dep, &node.name));
1335 }
1336 }
1337
1338 let executor = match crate::domain::graph::DagExecutor::from_pipeline(&pipeline) {
1339 Ok(e) => e,
1340 Err(e) => return error_response(id, -32603, &format!("Graph build error: {e}")),
1341 };
1342
1343 let impact = executor.impact_analysis(node_id);
1344
1345 ok_response(
1346 id,
1347 json!({
1348 "content": [{
1349 "type": "text",
1350 "text": serde_json::to_string(&impact).unwrap_or_default()
1351 }]
1352 }),
1353 )
1354 }
1355
1356 fn tool_graph_query(id: &Value, args: &Value) -> Value {
1357 let Some(toml) = args.get("toml").and_then(Value::as_str) else {
1358 return error_response(id, -32602, "Missing required parameter: toml");
1359 };
1360
1361 let def = match PipelineParser::from_str(toml) {
1362 Ok(d) => d,
1363 Err(e) => return error_response(id, -32603, &format!("Parse error: {e}")),
1364 };
1365
1366 if let Err(e) = def.validate() {
1367 return error_response(id, -32603, &format!("Validation error: {e}"));
1368 }
1369
1370 let mut pipeline = crate::domain::graph::Pipeline::new("pipeline");
1371 for node in &def.nodes {
1372 pipeline.add_node(crate::domain::graph::Node::with_metadata(
1373 &node.name,
1374 &node.service,
1375 serde_json::json!({
1376 "url": node.url,
1377 "params": toml_to_json(&toml::Value::Table(
1378 node.params.iter()
1379 .map(|(k, v)| (k.clone(), v.clone()))
1380 .collect()
1381 ))
1382 }),
1383 serde_json::Value::Null,
1384 ));
1385 for dep in &node.depends_on {
1386 pipeline.add_edge(crate::domain::graph::Edge::new(dep, &node.name));
1387 }
1388 }
1389
1390 let executor = match crate::domain::graph::DagExecutor::from_pipeline(&pipeline) {
1391 Ok(e) => e,
1392 Err(e) => return error_response(id, -32603, &format!("Graph build error: {e}")),
1393 };
1394
1395 let query = crate::domain::introspection::NodeQuery {
1397 service: args
1398 .get("service")
1399 .and_then(Value::as_str)
1400 .map(String::from),
1401 id: None,
1402 id_pattern: args
1403 .get("id_pattern")
1404 .and_then(Value::as_str)
1405 .map(String::from),
1406 is_root: args.get("is_root").and_then(Value::as_bool),
1407 is_leaf: args.get("is_leaf").and_then(Value::as_bool),
1408 min_depth: args
1409 .get("min_depth")
1410 .and_then(Value::as_u64)
1411 .map(|v| usize::try_from(v).unwrap_or(0)),
1412 max_depth: args
1413 .get("max_depth")
1414 .and_then(Value::as_u64)
1415 .map(|v| usize::try_from(v).unwrap_or(0)),
1416 };
1417
1418 let results = executor.query_nodes(&query);
1419
1420 ok_response(
1421 id,
1422 json!({
1423 "content": [{
1424 "type": "text",
1425 "text": serde_json::to_string(&results).unwrap_or_default()
1426 }]
1427 }),
1428 )
1429 }
1430}
1431
1432impl Default for McpGraphServer {
1433 fn default() -> Self {
1434 Self::new()
1435 }
1436}
1437
1438fn build_graphql_node_request(
1444 node: &NodeDecl,
1445 url: &str,
1446 timeout_secs: u64,
1447) -> (GraphQlService, ServiceInput) {
1448 let query = node
1449 .params
1450 .get("query")
1451 .and_then(|v| v.as_str())
1452 .unwrap_or("")
1453 .to_string();
1454 let config = GraphQlConfig {
1455 timeout_secs,
1456 ..GraphQlConfig::default()
1457 };
1458 let service = GraphQlService::new(config, None);
1459 let mut gql_map = serde_json::Map::new();
1460 gql_map.insert("query".to_owned(), json!(query));
1461 if let Some(variables) = node.params.get("variables") {
1462 gql_map.insert("variables".to_owned(), toml_to_json(variables));
1463 }
1464 if let Some(auth) = node.params.get("auth") {
1465 gql_map.insert("auth".to_owned(), toml_to_json(auth));
1466 }
1467 if let Some(dp) = node.params.get("data_path").and_then(|v| v.as_str()) {
1468 gql_map.insert("data_path".to_owned(), json!(dp));
1469 }
1470 (
1471 service,
1472 ServiceInput {
1473 url: url.to_string(),
1474 params: Value::Object(gql_map),
1475 },
1476 )
1477}
1478
1479#[derive(Debug, Clone, PartialEq)]
1480struct AcquisitionNodeConfig {
1481 mode: String,
1482 wait_for_selector: Option<String>,
1483 extraction_js: Option<String>,
1484 total_timeout: Option<Duration>,
1485 #[cfg(feature = "acquisition-runner")]
1486 target_class: Option<TargetClass>,
1487}
1488
1489#[cfg(feature = "acquisition-runner")]
1490fn parse_optional_target_class(value: &toml::Value) -> Result<TargetClass, String> {
1491 let raw = value
1492 .as_str()
1493 .ok_or_else(|| "acquisition.target_class must be a string".to_string())?;
1494 match raw {
1495 "api" => Ok(TargetClass::Api),
1496 "content-site" | "content_site" | "contentsite" | "content" => Ok(TargetClass::ContentSite),
1497 "high-security" | "high_security" | "highsecurity" | "high" => {
1498 Ok(TargetClass::HighSecurity)
1499 }
1500 "unknown" => Ok(TargetClass::Unknown),
1501 _ => Err(
1502 "acquisition.target_class must be one of: api, content-site, high-security, unknown"
1503 .to_string(),
1504 ),
1505 }
1506}
1507
1508#[cfg(feature = "acquisition-runner")]
1509fn parse_optional_positive_secs(value: &toml::Value) -> Result<Duration, String> {
1510 const MAX_ACQUISITION_TIMEOUT_SECS: u64 = 86_400;
1511 const MAX_ACQUISITION_TIMEOUT_SECS_F64: f64 = 86_400.0;
1512
1513 if let Some(seconds) = value.as_float() {
1514 if seconds.is_finite() && seconds > 0.0 && seconds <= MAX_ACQUISITION_TIMEOUT_SECS_F64 {
1515 return Ok(Duration::from_secs_f64(seconds));
1516 }
1517 return Err(format!(
1518 "acquisition.total_timeout_secs must be a positive finite number <= {MAX_ACQUISITION_TIMEOUT_SECS}"
1519 ));
1520 }
1521
1522 if let Some(seconds) = value.as_integer() {
1523 if seconds > 0 && seconds <= i64::try_from(MAX_ACQUISITION_TIMEOUT_SECS).unwrap_or(i64::MAX)
1524 {
1525 return Ok(Duration::from_secs(u64::try_from(seconds).map_err(
1526 |_| "acquisition.total_timeout_secs must fit into an unsigned integer".to_string(),
1527 )?));
1528 }
1529 return Err(format!(
1530 "acquisition.total_timeout_secs must be an integer in 1..={MAX_ACQUISITION_TIMEOUT_SECS}"
1531 ));
1532 }
1533
1534 Err("acquisition.total_timeout_secs must be a number".to_string())
1535}
1536
1537#[cfg(feature = "acquisition-runner")]
1538fn acquisition_config_from_node(node: &NodeDecl) -> Result<Option<AcquisitionNodeConfig>, String> {
1539 let Some(raw) = node.params.get("acquisition") else {
1540 return Ok(None);
1541 };
1542
1543 let table = raw
1544 .as_table()
1545 .ok_or_else(|| "acquisition must be a TOML table".to_string())?;
1546
1547 let enabled = table
1548 .get("enabled")
1549 .and_then(toml::Value::as_bool)
1550 .unwrap_or(true);
1551
1552 if !enabled {
1553 return Ok(None);
1554 }
1555
1556 let mode = table
1557 .get("mode")
1558 .and_then(toml::Value::as_str)
1559 .unwrap_or("resilient")
1560 .to_string();
1561
1562 let wait_for_selector = table
1563 .get("wait_for_selector")
1564 .or_else(|| table.get("selector_wait"))
1565 .and_then(toml::Value::as_str)
1566 .map(ToString::to_string);
1567
1568 let extraction_js = table
1569 .get("extraction_js")
1570 .and_then(toml::Value::as_str)
1571 .map(ToString::to_string);
1572
1573 let total_timeout = table
1574 .get("total_timeout_secs")
1575 .map(parse_optional_positive_secs)
1576 .transpose()?;
1577
1578 let target_class = table
1579 .get("target_class")
1580 .map(parse_optional_target_class)
1581 .transpose()?;
1582
1583 Ok(Some(AcquisitionNodeConfig {
1584 mode,
1585 wait_for_selector,
1586 extraction_js,
1587 total_timeout,
1588 target_class,
1589 }))
1590}
1591
1592#[cfg(feature = "acquisition-runner")]
1593fn parse_acquisition_mode(raw: &str) -> Result<AcquisitionMode, String> {
1594 match raw {
1595 "fast" => Ok(AcquisitionMode::Fast),
1596 "resilient" => Ok(AcquisitionMode::Resilient),
1597 "hostile" => Ok(AcquisitionMode::Hostile),
1598 "investigate" => Ok(AcquisitionMode::Investigate),
1599 other => Err(format!(
1600 "Invalid acquisition mode '{other}'. Use one of: fast, resilient, hostile, investigate"
1601 )),
1602 }
1603}
1604
1605#[cfg(feature = "acquisition-runner")]
1606const fn mode_rank(mode: AcquisitionMode) -> u8 {
1607 match mode {
1608 AcquisitionMode::Fast => 0,
1609 AcquisitionMode::Resilient => 1,
1610 AcquisitionMode::Hostile => 2,
1611 AcquisitionMode::Investigate => 3,
1612 }
1613}
1614
1615#[cfg(all(feature = "acquisition-runner", feature = "charon"))]
1616const fn mode_from_hint(hint: AcquisitionModeHint) -> AcquisitionMode {
1617 match hint {
1618 AcquisitionModeHint::Fast => AcquisitionMode::Fast,
1619 AcquisitionModeHint::Resilient => AcquisitionMode::Resilient,
1620 AcquisitionModeHint::Hostile => AcquisitionMode::Hostile,
1621 AcquisitionModeHint::Investigate => AcquisitionMode::Investigate,
1622 }
1623}
1624
1625#[cfg(feature = "acquisition-runner")]
1626fn build_status_only_har(url: &str, status: u16, body_excerpt: Option<&str>) -> String {
1627 let text = body_excerpt.unwrap_or_default();
1628 json!({
1629 "log": {
1630 "version": "1.2",
1631 "creator": {"name": "stygian-graph-acquisition-bridge", "version": "1.0"},
1632 "pages": [{
1633 "id": "page_1",
1634 "title": url,
1635 "startedDateTime": "2026-01-01T00:00:00.000Z",
1636 "pageTimings": {"onLoad": 0}
1637 }],
1638 "entries": [{
1639 "pageref": "page_1",
1640 "startedDateTime": "2026-01-01T00:00:00.000Z",
1641 "time": 0,
1642 "request": {
1643 "method": "GET",
1644 "url": url,
1645 "httpVersion": "HTTP/2",
1646 "headers": [],
1647 "queryString": [],
1648 "cookies": [],
1649 "headersSize": -1,
1650 "bodySize": 0
1651 },
1652 "response": {
1653 "status": status,
1654 "statusText": "bridge",
1655 "httpVersion": "HTTP/2",
1656 "headers": [],
1657 "cookies": [],
1658 "content": {"size": text.len(), "mimeType": "text/html", "text": text},
1659 "redirectURL": "",
1660 "headersSize": -1,
1661 "bodySize": 0
1662 },
1663 "cache": {},
1664 "timings": {
1665 "blocked": 0,
1666 "dns": 0,
1667 "connect": 0,
1668 "send": 0,
1669 "wait": 0,
1670 "receive": 0,
1671 "ssl": 0
1672 }
1673 }]
1674 }
1675 })
1676 .to_string()
1677}
1678
1679#[cfg(feature = "acquisition-runner")]
1680fn suggest_mode_from_slo(
1681 url: &str,
1682 status_code: Option<u16>,
1683 html_excerpt: Option<&str>,
1684 target_class: TargetClass,
1685) -> Option<AcquisitionMode> {
1686 let status = status_code.unwrap_or(200);
1687 let har = build_status_only_har(url, status, html_excerpt);
1688 let report = investigate_har(&har).ok()?;
1689 let requirements = infer_requirements_with_target_class(&report, target_class);
1690 let policy = build_runtime_policy(&report, &requirements);
1691 let mapped = map_runtime_policy(&policy);
1692 Some(mode_from_hint(mapped.mode))
1693}
1694
1695#[cfg(feature = "acquisition-runner")]
1696static ACQUISITION_BRIDGE_POOL: OnceCell<Arc<BrowserPool>> = OnceCell::const_new();
1697
1698#[cfg(feature = "acquisition-runner")]
1699async fn acquisition_bridge_pool() -> Result<Arc<BrowserPool>, String> {
1700 let pool = ACQUISITION_BRIDGE_POOL
1701 .get_or_try_init(|| async {
1702 BrowserPool::new(BrowserConfig::default())
1703 .await
1704 .map_err(|e| format!("acquisition bridge browser pool init failed: {e}"))
1705 })
1706 .await?;
1707 Ok(Arc::clone(pool))
1708}
1709
1710#[cfg(feature = "acquisition-runner")]
1711async fn run_acquisition_bridge(url: &str, cfg: &AcquisitionNodeConfig) -> Result<Value, String> {
1712 let configured_mode = parse_acquisition_mode(&cfg.mode)?;
1713 let pool = acquisition_bridge_pool().await?;
1714
1715 let runner = AcquisitionRunner::new(pool);
1716 let total_timeout = cfg
1717 .total_timeout
1718 .unwrap_or_else(|| AcquisitionRequest::default().total_timeout);
1719
1720 let mut result = runner
1721 .run(AcquisitionRequest {
1722 url: url.to_string(),
1723 mode: configured_mode,
1724 wait_for_selector: cfg.wait_for_selector.clone(),
1725 extraction_js: cfg.extraction_js.clone(),
1726 total_timeout,
1727 ..AcquisitionRequest::default()
1728 })
1729 .await;
1730
1731 let mut effective_mode = configured_mode;
1732 let mut slo_recommended_mode: Option<AcquisitionMode> = None;
1733 let mut slo_bridge_applied = false;
1734
1735 if let Some(target_class) = cfg.target_class
1736 && let Some(recommended_mode) = suggest_mode_from_slo(
1737 result.final_url.as_deref().unwrap_or(url),
1738 result.status_code,
1739 result.html_excerpt.as_deref(),
1740 target_class,
1741 )
1742 {
1743 slo_recommended_mode = Some(recommended_mode);
1744 if mode_rank(recommended_mode) > mode_rank(configured_mode) {
1745 let retried = runner
1746 .run(AcquisitionRequest {
1747 url: url.to_string(),
1748 mode: recommended_mode,
1749 wait_for_selector: cfg.wait_for_selector.clone(),
1750 extraction_js: cfg.extraction_js.clone(),
1751 total_timeout,
1752 ..AcquisitionRequest::default()
1753 })
1754 .await;
1755 if retried.success || !result.success {
1756 result = retried;
1757 effective_mode = recommended_mode;
1758 slo_bridge_applied = true;
1759 }
1760 }
1761 }
1762
1763 let strategy_used = serde_json::to_value(result.strategy_used).unwrap_or(Value::Null);
1764 let attempted = serde_json::to_value(&result.attempted).unwrap_or(Value::Array(Vec::new()));
1765 let failures = serde_json::to_value(&result.failures).unwrap_or(Value::Array(Vec::new()));
1766
1767 Ok(json!({
1768 "data": {
1769 "success": result.success,
1770 "strategy_used": strategy_used,
1771 "final_url": result.final_url,
1772 "status_code": result.status_code,
1773 "extracted": result.extracted,
1774 "html_excerpt": result.html_excerpt,
1775 },
1776 "metadata": {
1777 "acquisition_runner": true,
1778 "diagnostics": {
1779 "attempted": attempted,
1780 "timed_out": result.timed_out,
1781 "failure_count": result.failures.len(),
1782 "failures": failures,
1783 "configured_mode": format!("{configured_mode:?}"),
1784 "effective_mode": format!("{effective_mode:?}"),
1785 "slo_target_class": cfg.target_class.map(|tc| format!("{tc:?}")),
1786 "slo_recommended_mode": slo_recommended_mode.map(|mode| format!("{mode:?}")),
1787 "slo_bridge_applied": slo_bridge_applied,
1788 }
1789 }
1790 }))
1791}
1792
1793#[cfg(not(feature = "acquisition-runner"))]
1794#[allow(clippy::unused_async)]
1795async fn run_acquisition_bridge(_url: &str, _cfg: &AcquisitionNodeConfig) -> Result<Value, String> {
1796 Err(
1797 "acquisition bridge requested but stygian-graph was built without feature 'acquisition-runner'"
1798 .to_string(),
1799 )
1800}
1801
1802async fn execute_pipeline_node(
1807 kind: &str,
1808 url: &str,
1809 node_name: &str,
1810 node: &NodeDecl,
1811 timeout_secs: u64,
1812) -> Option<Result<Value, String>> {
1813 execute_pipeline_node_with(
1814 kind,
1815 url,
1816 node_name,
1817 node,
1818 timeout_secs,
1819 |bridge_url, cfg| async move { run_acquisition_bridge(&bridge_url, &cfg).await },
1820 )
1821 .await
1822}
1823
1824async fn execute_pipeline_node_with<F, Fut>(
1825 kind: &str,
1826 url: &str,
1827 node_name: &str,
1828 node: &NodeDecl,
1829 timeout_secs: u64,
1830 run_acquisition: F,
1831) -> Option<Result<Value, String>>
1832where
1833 F: Fn(String, AcquisitionNodeConfig) -> Fut + Send + Sync,
1834 Fut: Future<Output = Result<Value, String>> + Send,
1835{
1836 match kind {
1837 "http" => {
1838 let config = HttpConfig {
1839 timeout: Duration::from_secs(timeout_secs),
1840 ..HttpConfig::default()
1841 };
1842 let adapter = HttpAdapter::with_config(config);
1843 let input = ServiceInput {
1844 url: url.to_string(),
1845 params: json!({}),
1846 };
1847 Some(
1848 adapter
1849 .execute(input)
1850 .await
1851 .map(|out| json!({ "data": out.data, "metadata": out.metadata }))
1852 .map_err(|e| e.to_string()),
1853 )
1854 }
1855 "rest" => {
1856 let params = build_rest_params_from_node(node);
1857 let adapter = RestApiAdapter::new();
1858 let input = ServiceInput {
1859 url: url.to_string(),
1860 params,
1861 };
1862 Some(
1863 adapter
1864 .execute(input)
1865 .await
1866 .map(|out| json!({ "data": out.data, "metadata": out.metadata }))
1867 .map_err(|e| e.to_string()),
1868 )
1869 }
1870 "graphql" => {
1871 let (service, input) = build_graphql_node_request(node, url, timeout_secs);
1872 Some(
1873 service
1874 .execute(input)
1875 .await
1876 .map(|out| json!({ "data": out.data, "metadata": out.metadata }))
1877 .map_err(|e| e.to_string()),
1878 )
1879 }
1880 "sitemap" => {
1881 let max_depth = node
1882 .params
1883 .get("max_depth")
1884 .and_then(toml::Value::as_integer)
1885 .map_or(5, |v| usize::try_from(v).unwrap_or(5));
1886 let client = reqwest::Client::new();
1887 let adapter = SitemapAdapter::new(client, max_depth);
1888 let input = ServiceInput {
1889 url: url.to_string(),
1890 params: json!({}),
1891 };
1892 Some(
1893 adapter
1894 .execute(input)
1895 .await
1896 .map(|out| json!({ "data": out.data, "metadata": out.metadata }))
1897 .map_err(|e| e.to_string()),
1898 )
1899 }
1900 "rss" => {
1901 let client = reqwest::Client::new();
1902 let adapter = RssFeedAdapter::new(client);
1903 let input = ServiceInput {
1904 url: url.to_string(),
1905 params: json!({}),
1906 };
1907 Some(
1908 adapter
1909 .execute(input)
1910 .await
1911 .map(|out| json!({ "data": out.data, "metadata": out.metadata }))
1912 .map_err(|e| e.to_string()),
1913 )
1914 }
1915 "browser" => execute_browser_pipeline_node(node, node_name, url, &run_acquisition).await,
1916 other => {
1917 warn!(
1918 kind = other,
1919 node = node_name,
1920 "skipping unsupported service kind in pipeline_run"
1921 );
1922 None
1923 }
1924 }
1925}
1926
1927#[cfg(feature = "acquisition-runner")]
1928async fn execute_browser_pipeline_node<F, Fut>(
1929 node: &NodeDecl,
1930 node_name: &str,
1931 url: &str,
1932 run_acquisition: &F,
1933) -> Option<Result<Value, String>>
1934where
1935 F: Fn(String, AcquisitionNodeConfig) -> Fut + Send + Sync,
1936 Fut: Future<Output = Result<Value, String>> + Send,
1937{
1938 let cfg = match acquisition_config_from_node(node) {
1939 Ok(Some(cfg)) => cfg,
1940 Ok(None) => return None,
1941 Err(err) => {
1942 return Some(Err(format!(
1943 "Invalid acquisition config for node '{node_name}': {err}"
1944 )));
1945 }
1946 };
1947
1948 Some(run_acquisition(url.to_string(), cfg).await)
1949}
1950
1951#[cfg(not(feature = "acquisition-runner"))]
1952#[allow(clippy::unused_async)]
1953async fn execute_browser_pipeline_node<F, Fut>(
1954 _node: &NodeDecl,
1955 _node_name: &str,
1956 _url: &str,
1957 _run_acquisition: &F,
1958) -> Option<Result<Value, String>>
1959where
1960 F: Fn(String, AcquisitionNodeConfig) -> Fut + Send + Sync,
1961 Fut: Future<Output = Result<Value, String>> + Send,
1962{
1963 None
1964}
1965
1966fn toml_to_json(v: &toml::Value) -> Value {
1968 match v {
1969 toml::Value::String(s) => Value::String(s.clone()),
1970 toml::Value::Integer(i) => Value::Number((*i).into()),
1971 toml::Value::Float(f) => {
1972 serde_json::Number::from_f64(*f).map_or(Value::Null, Value::Number)
1973 }
1974 toml::Value::Boolean(b) => Value::Bool(*b),
1975 toml::Value::Array(arr) => Value::Array(arr.iter().map(toml_to_json).collect()),
1976 toml::Value::Table(tbl) => Value::Object(
1977 tbl.iter()
1978 .map(|(k, v)| (k.clone(), toml_to_json(v)))
1979 .collect(),
1980 ),
1981 toml::Value::Datetime(dt) => Value::String(dt.to_string()),
1982 }
1983}
1984
1985fn build_rest_params_from_node(node: &NodeDecl) -> Value {
1990 let mut map = serde_json::Map::new();
1991
1992 if let Some(method) = node.params.get("method").and_then(|v| v.as_str()) {
1993 map.insert("method".to_owned(), json!(method));
1994 }
1995 if let Some(auth) = node.params.get("auth") {
1996 map.insert("auth".to_owned(), toml_to_json(auth));
1997 }
1998 if let Some(headers) = node.params.get("headers") {
1999 map.insert("headers".to_owned(), toml_to_json(headers));
2000 }
2001 if let Some(query) = node.params.get("query") {
2002 map.insert("query".to_owned(), toml_to_json(query));
2003 }
2004 if let Some(body) = node.params.get("body") {
2005 map.insert("body".to_owned(), toml_to_json(body));
2006 }
2007 if let Some(pagination) = node.params.get("pagination") {
2008 map.insert("pagination".to_owned(), toml_to_json(pagination));
2009 }
2010 if let Some(dp) = node.params.get("data_path").and_then(|v| v.as_str()) {
2011 map.insert("response".to_owned(), json!({ "data_path": dp }));
2012 }
2013
2014 Value::Object(map)
2015}
2016
2017#[cfg(test)]
2020#[allow(clippy::unwrap_used)]
2021mod tests {
2022 use super::*;
2023
2024 #[test]
2025 fn server_builds() {
2026 let _ = McpGraphServer::new();
2027 }
2028
2029 #[test]
2030 fn discover_response_advertises_protocol_version() {
2031 let id = json!(1);
2032 let resp = McpGraphServer::handle_discover(&id);
2033 assert_eq!(
2034 resp.pointer("/result/protocolVersion")
2035 .and_then(Value::as_str),
2036 Some("2026-07-28")
2037 );
2038 assert_eq!(
2039 resp.pointer("/result/supportedProtocolVersions")
2040 .and_then(Value::as_array)
2041 .and_then(|v| v.first())
2042 .and_then(Value::as_str),
2043 Some("2026-07-28")
2044 );
2045 assert_eq!(
2046 resp.pointer("/result/serverInfo/name")
2047 .and_then(Value::as_str),
2048 Some("stygian-graph")
2049 );
2050 assert_eq!(
2051 resp.pointer("/result/resultType").and_then(Value::as_str),
2052 Some("complete")
2053 );
2054 assert!(
2056 resp.pointer("/result/extensions")
2057 .and_then(Value::as_array)
2058 .is_some()
2059 );
2060 }
2061
2062 #[test]
2063 fn initialize_method_is_no_longer_recognized() {
2064 let resp = tokio_test::block_on(McpGraphServer::handle_request(&json!({
2068 "jsonrpc": "2.0",
2069 "id": 1,
2070 "method": "initialize",
2071 "params": {}
2072 })));
2073 assert_eq!(
2074 resp.pointer("/error/code").and_then(Value::as_i64),
2075 Some(-32601)
2076 );
2077 }
2078
2079 #[test]
2080 fn ping_method_is_no_longer_recognized() {
2081 let resp = tokio_test::block_on(McpGraphServer::handle_request(&json!({
2084 "jsonrpc": "2.0",
2085 "id": 7,
2086 "method": "ping"
2087 })));
2088 assert_eq!(
2089 resp.pointer("/error/code").and_then(Value::as_i64),
2090 Some(-32601)
2091 );
2092 }
2093
2094 #[test]
2095 fn ok_response_threads_result_type_complete() {
2096 let id = json!(42);
2102 let obj = McpGraphServer::handle_tools_list(&id);
2103 assert_eq!(
2104 obj.pointer("/result/resultType").and_then(Value::as_str),
2105 Some("complete")
2106 );
2107 assert!(obj.pointer("/result/tools").is_some());
2108
2109 let scalar = ok_response(&id, json!("plain-string"));
2110 assert_eq!(
2111 scalar.pointer("/result/resultType").and_then(Value::as_str),
2112 Some("complete")
2113 );
2114 assert_eq!(
2115 scalar.pointer("/result/value").and_then(Value::as_str),
2116 Some("plain-string")
2117 );
2118 }
2119
2120 #[test]
2121 fn extract_meta_reads_namespaced_keys() {
2122 let req = json!({
2126 "jsonrpc": "2.0",
2127 "id": 1,
2128 "method": "tools/list",
2129 "params": {
2130 "_meta": {
2131 "io.modelcontextprotocol/protocolVersion": "2026-07-28",
2132 "io.modelcontextprotocol/clientInfo": {
2133 "name": "test-client",
2134 "version": "0.0.1"
2135 },
2136 "io.modelcontextprotocol/clientCapabilities": {
2137 "tools": {}
2138 },
2139 "unrelated": "ignored"
2140 }
2141 }
2142 });
2143 assert_eq!(
2144 extract_client_protocol_version(&req).as_deref(),
2145 Some("2026-07-28")
2146 );
2147 assert_eq!(
2148 extract_meta(&req, "clientInfo")
2149 .and_then(|v| v.get("name"))
2150 .and_then(Value::as_str),
2151 Some("test-client")
2152 );
2153 assert!(extract_meta(&req, "clientCapabilities").is_some());
2154 assert!(extract_meta(&req, "not-a-key").is_none());
2155
2156 let bare = json!({"jsonrpc": "2.0", "id": 1, "method": "tools/list"});
2158 assert!(extract_client_protocol_version(&bare).is_none());
2159 assert!(extract_meta(&bare, "protocolVersion").is_none());
2160 }
2161
2162 #[test]
2163 fn is_supported_protocol_version_accepts_listed_and_rejects_others() {
2164 assert!(is_supported_protocol_version("2026-07-28", &["2026-07-28"]).is_ok());
2172 assert!(is_supported_protocol_version("2026-07-28", &["2025-11-25"]).is_err());
2173 assert!(is_supported_protocol_version("2025-11-25", &["2026-07-28", "2025-11-25"]).is_ok());
2174 }
2175
2176 #[test]
2177 fn tools_list_contains_all_tools() {
2178 let id = json!(1);
2179 let resp = McpGraphServer::handle_tools_list(&id);
2180 let tools = resp
2181 .pointer("/result/tools")
2182 .and_then(Value::as_array)
2183 .unwrap();
2184 let names: Vec<&str> = tools
2185 .iter()
2186 .map(|t| t.get("name").and_then(Value::as_str).unwrap())
2187 .collect();
2188 assert!(names.contains(&"scrape"));
2189 assert!(names.contains(&"scrape_rest"));
2190 assert!(names.contains(&"scrape_graphql"));
2191 assert!(names.contains(&"scrape_sitemap"));
2192 assert!(names.contains(&"scrape_rss"));
2193 assert!(names.contains(&"pipeline_validate"));
2194 assert!(names.contains(&"pipeline_run"));
2195
2196 #[cfg(feature = "charon")]
2197 {
2198 assert!(names.contains(&"charon_classify_transaction"));
2199 assert!(names.contains(&"charon_investigate_har"));
2200 assert!(names.contains(&"charon_infer_requirements"));
2201 assert!(names.contains(&"charon_build_runtime_policy"));
2202 assert!(names.contains(&"charon_map_runtime_policy"));
2203 assert!(names.contains(&"charon_analyze_and_plan"));
2204 }
2205 }
2206
2207 #[cfg(feature = "charon")]
2208 #[test]
2209 fn charon_classify_transaction_returns_detection() {
2210 let id = json!(99);
2211 let args = json!({
2212 "url": "https://example.com/challenge",
2213 "status": 403,
2214 "response_headers": { "x-datadome": "1" },
2215 "response_body_snippet": "captcha-delivery.com"
2216 });
2217
2218 let resp = McpGraphServer::tool_charon_classify_transaction(&id, &args);
2219 let text = resp
2220 .pointer("/result/content/0/text")
2221 .and_then(Value::as_str)
2222 .unwrap_or_default();
2223 let payload: Value = serde_json::from_str(text).unwrap_or(Value::Null);
2224
2225 assert_eq!(
2226 payload
2227 .pointer("/detection/provider")
2228 .and_then(Value::as_str),
2229 Some("DataDome")
2230 );
2231 }
2232
2233 #[cfg(feature = "charon")]
2234 #[test]
2235 fn charon_analyze_and_plan_returns_policy_and_acquisition() {
2236 let id = json!(100);
2237 let args = json!({
2238 "har": json!({
2239 "log": {
2240 "version": "1.2",
2241 "creator": {"name": "test", "version": "1.0"},
2242 "pages": [{
2243 "id": "page_1",
2244 "title": "https://example.com/challenge",
2245 "startedDateTime": "2026-01-01T00:00:00.000Z",
2246 "pageTimings": {"onLoad": 0}
2247 }],
2248 "entries": [{
2249 "pageref": "page_1",
2250 "startedDateTime": "2026-01-01T00:00:00.000Z",
2251 "time": 0,
2252 "request": {
2253 "method": "GET",
2254 "url": "https://example.com/challenge",
2255 "httpVersion": "HTTP/2",
2256 "headers": [],
2257 "queryString": [],
2258 "cookies": [],
2259 "headersSize": -1,
2260 "bodySize": 0
2261 },
2262 "response": {
2263 "status": 403,
2264 "statusText": "Forbidden",
2265 "httpVersion": "HTTP/2",
2266 "headers": [],
2267 "cookies": [],
2268 "content": {
2269 "size": 0,
2270 "mimeType": "text/html",
2271 "text": "captcha-delivery.com"
2272 },
2273 "redirectURL": "",
2274 "headersSize": -1,
2275 "bodySize": 0
2276 },
2277 "cache": {},
2278 "timings": {
2279 "blocked": 0,
2280 "dns": 0,
2281 "connect": 0,
2282 "send": 0,
2283 "wait": 0,
2284 "receive": 0,
2285 "ssl": 0
2286 }
2287 }]
2288 }
2289 }).to_string(),
2290 "target_class": "api"
2291 });
2292
2293 let resp = McpGraphServer::tool_charon_analyze_and_plan(&id, &args);
2294 let text = resp
2295 .pointer("/result/content/0/text")
2296 .and_then(Value::as_str)
2297 .unwrap_or_default();
2298 let payload: Value = serde_json::from_str(text).unwrap_or(Value::Null);
2299
2300 assert!(payload.get("bundle").is_some());
2301 assert!(payload.pointer("/bundle/policy").is_some());
2302 assert!(payload.pointer("/acquisition/mode").is_some());
2303 }
2304
2305 #[test]
2306 fn pipeline_validate_rejects_bad_toml() {
2307 let id = json!(1);
2308 let args = json!({ "toml": "this is not valid toml [[[[" });
2309 let resp = McpGraphServer::tool_pipeline_validate(&id, &args);
2310 assert!(
2311 resp.get("error").is_some_and(Value::is_object)
2312 || resp
2313 .pointer("/result/content/0/text")
2314 .and_then(Value::as_str)
2315 .unwrap_or("")
2316 .contains("false")
2317 );
2318 }
2319
2320 #[test]
2321 fn pipeline_validate_accepts_valid_pipeline() {
2322 let id = json!(1);
2323 let toml = r#"
2324[[nodes]]
2325name = "fetch"
2326service = "http"
2327url = "https://example.com"
2328
2329[[nodes]]
2330name = "process"
2331service = "http"
2332url = "https://example.com/api"
2333depends_on = ["fetch"]
2334"#;
2335 let args = json!({ "toml": toml });
2336 let resp = McpGraphServer::tool_pipeline_validate(&id, &args);
2337 let text = resp
2338 .pointer("/result/content/0/text")
2339 .and_then(Value::as_str)
2340 .unwrap();
2341 let parsed: Value = serde_json::from_str(text).unwrap();
2342 assert_eq!(parsed.get("valid"), Some(&json!(true)));
2343 assert_eq!(parsed.get("node_count"), Some(&json!(2)));
2344 }
2345
2346 #[test]
2347 fn pipeline_validate_missing_toml_returns_error() {
2348 let id = json!(1);
2350 let args = json!({});
2351 let resp = McpGraphServer::tool_pipeline_validate(&id, &args);
2354 assert!(resp.get("error").is_some_and(Value::is_object));
2355 }
2356
2357 #[tokio::test]
2358 async fn pipeline_browser_node_without_acquisition_is_skipped() {
2359 let node = NodeDecl {
2360 name: "render".to_string(),
2361 service: "browser".to_string(),
2362 depends_on: Vec::new(),
2363 url: Some("https://example.com".to_string()),
2364 params: HashMap::new(),
2365 };
2366
2367 let result = execute_pipeline_node_with(
2368 "browser",
2369 "https://example.com",
2370 "render",
2371 &node,
2372 30,
2373 |_url, _cfg| async { Ok(json!({"data": "should-not-run"})) },
2374 )
2375 .await;
2376
2377 assert!(result.is_none());
2378 }
2379
2380 #[cfg(feature = "acquisition-runner")]
2381 #[tokio::test]
2382 async fn pipeline_browser_node_with_acquisition_uses_bridge_path() {
2383 let mut acquisition = toml::map::Map::new();
2384 acquisition.insert("mode".to_string(), toml::Value::String("fast".to_string()));
2385 acquisition.insert(
2386 "wait_for_selector".to_string(),
2387 toml::Value::String("main".to_string()),
2388 );
2389
2390 let mut params = HashMap::new();
2391 params.insert("acquisition".to_string(), toml::Value::Table(acquisition));
2392
2393 let node = NodeDecl {
2394 name: "render".to_string(),
2395 service: "browser".to_string(),
2396 depends_on: Vec::new(),
2397 url: Some("https://example.com".to_string()),
2398 params,
2399 };
2400
2401 let result = execute_pipeline_node_with(
2402 "browser",
2403 "https://example.com",
2404 "render",
2405 &node,
2406 30,
2407 |url, cfg| async move {
2408 Ok(json!({
2409 "data": {
2410 "url": url,
2411 "mode": cfg.mode,
2412 "wait_for_selector": cfg.wait_for_selector,
2413 },
2414 "metadata": {"bridge": "mock"}
2415 }))
2416 },
2417 )
2418 .await;
2419
2420 let payload = match result {
2421 Some(Ok(payload)) => payload,
2422 other => {
2423 assert!(
2424 matches!(other, Some(Ok(_))),
2425 "browser acquisition should return Some(Ok(_))"
2426 );
2427 return;
2428 }
2429 };
2430
2431 assert_eq!(
2432 payload.pointer("/data/url").and_then(Value::as_str),
2433 Some("https://example.com")
2434 );
2435 assert_eq!(
2436 payload.pointer("/data/mode").and_then(Value::as_str),
2437 Some("fast")
2438 );
2439 assert_eq!(
2440 payload
2441 .pointer("/data/wait_for_selector")
2442 .and_then(Value::as_str),
2443 Some("main")
2444 );
2445 }
2446
2447 #[cfg(feature = "acquisition-runner")]
2448 #[test]
2449 fn acquisition_config_parses_target_class() {
2450 let mut acquisition = toml::map::Map::new();
2451 acquisition.insert(
2452 "mode".to_string(),
2453 toml::Value::String("resilient".to_string()),
2454 );
2455 acquisition.insert(
2456 "target_class".to_string(),
2457 toml::Value::String("content-site".to_string()),
2458 );
2459
2460 let mut params = HashMap::new();
2461 params.insert("acquisition".to_string(), toml::Value::Table(acquisition));
2462
2463 let node = NodeDecl {
2464 name: "render".to_string(),
2465 service: "browser".to_string(),
2466 depends_on: Vec::new(),
2467 url: Some("https://example.com".to_string()),
2468 params,
2469 };
2470
2471 let parsed = acquisition_config_from_node(&node);
2472 assert!(parsed.is_ok(), "target_class should parse");
2473 let Ok(Some(cfg)) = parsed else {
2474 return;
2475 };
2476 assert_eq!(cfg.target_class, Some(TargetClass::ContentSite));
2477 }
2478
2479 #[cfg(feature = "acquisition-runner")]
2480 #[test]
2481 fn slo_bridge_can_recommend_stronger_mode_for_blocked_status() {
2482 let recommended = suggest_mode_from_slo(
2483 "https://example.com/challenge",
2484 Some(403),
2485 Some("captcha-delivery.com"),
2486 TargetClass::Api,
2487 );
2488
2489 assert!(recommended.is_some(), "SLO bridge should return a mode");
2490 let Some(mode) = recommended else {
2491 return;
2492 };
2493 assert!(
2494 mode_rank(mode) >= mode_rank(AcquisitionMode::Resilient),
2495 "blocked scenarios should not downshift below resilient"
2496 );
2497 }
2498
2499 #[cfg(not(feature = "acquisition-runner"))]
2500 #[tokio::test]
2501 async fn pipeline_browser_node_with_acquisition_is_skipped_without_feature() {
2502 let mut acquisition = toml::map::Map::new();
2503 acquisition.insert("mode".to_string(), toml::Value::String("fast".to_string()));
2504
2505 let mut params = HashMap::new();
2506 params.insert("acquisition".to_string(), toml::Value::Table(acquisition));
2507
2508 let node = NodeDecl {
2509 name: "render".to_string(),
2510 service: "browser".to_string(),
2511 depends_on: Vec::new(),
2512 url: Some("https://example.com".to_string()),
2513 params,
2514 };
2515
2516 let result = execute_pipeline_node_with(
2517 "browser",
2518 "https://example.com",
2519 "render",
2520 &node,
2521 30,
2522 |_url, _cfg| async { Ok(json!({"data": "should-not-run"})) },
2523 )
2524 .await;
2525
2526 assert!(result.is_none());
2527 }
2528
2529 #[cfg(feature = "acquisition-runner")]
2530 #[tokio::test]
2531 async fn pipeline_browser_node_invalid_acquisition_timeout_returns_error() {
2532 let mut acquisition = toml::map::Map::new();
2533 acquisition.insert("mode".to_string(), toml::Value::String("fast".to_string()));
2534 acquisition.insert("total_timeout_secs".to_string(), toml::Value::Integer(0));
2535
2536 let mut params = HashMap::new();
2537 params.insert("acquisition".to_string(), toml::Value::Table(acquisition));
2538
2539 let node = NodeDecl {
2540 name: "render".to_string(),
2541 service: "browser".to_string(),
2542 depends_on: Vec::new(),
2543 url: Some("https://example.com".to_string()),
2544 params,
2545 };
2546
2547 let result = execute_pipeline_node_with(
2548 "browser",
2549 "https://example.com",
2550 "render",
2551 &node,
2552 30,
2553 |_url, _cfg| async { Ok(json!({"data": "unexpected"})) },
2554 )
2555 .await;
2556
2557 let err = match result {
2558 Some(Err(err)) => err,
2559 other => {
2560 assert!(
2561 matches!(other, Some(Err(_))),
2562 "invalid config should return Some(Err(_))"
2563 );
2564 return;
2565 }
2566 };
2567
2568 assert!(
2569 err.contains("total_timeout_secs") || err.contains("Invalid acquisition config"),
2570 "unexpected error: {err}"
2571 );
2572 }
2573}