Skip to main content

multi_server_compose/
multi_server_compose.rs

1//! Demonstrates multi-server MCP composition auditing (`compose_catalogs`),
2//! pre-indexed sub-millisecond `Bm25Index` search, per-term `explain_query`,
3//! and side-by-side `compare_rankers`.
4use mcp_vol::prelude::*;
5
6fn main() {
7    let github = Catalog::from_mcp_json(
8        r#"{
9          "tools": [
10            {
11              "name": "create_issue",
12              "description": "Creates a new GitHub issue in a repository and returns its issue URL.",
13              "inputSchema": {
14                "type": "object",
15                "properties": {
16                  "owner": { "type": "string", "description": "Repository owner." },
17                  "repo": { "type": "string", "description": "Repository name." },
18                  "title": { "type": "string", "description": "Issue title." }
19                },
20                "required": ["owner", "repo", "title"]
21              }
22            },
23            {
24              "name": "search_repositories",
25              "description": "Searches GitHub repositories by keyword and returns a paginated list.",
26              "inputSchema": {
27                "type": "object",
28                "properties": {
29                  "query": { "type": "string", "description": "GitHub search query." },
30                  "perPage": { "type": "integer", "description": "Results per page." }
31                },
32                "required": ["query"]
33              }
34            }
35          ]
36        }"#,
37    )
38    .expect("valid github catalog");
39
40    let gitlab = Catalog::from_mcp_json(
41        r#"{
42          "tools": [
43            {
44              "name": "create_issue",
45              "description": "Creates a new GitLab issue in a project and returns its issue IID.",
46              "inputSchema": {
47                "type": "object",
48                "properties": {
49                  "project_id": { "type": "string", "description": "Project ID or URL-encoded path." },
50                  "title": { "type": "string", "description": "Issue title." }
51                },
52                "required": ["project_id", "title"]
53              }
54            },
55            {
56              "name": "list_merge_requests",
57              "description": "Lists open GitLab merge requests for a project with pagination.",
58              "inputSchema": {
59                "type": "object",
60                "properties": {
61                  "project_id": { "type": "string", "description": "Project ID." },
62                  "limit": { "type": "integer", "description": "Max rows returned." }
63                },
64                "required": ["project_id"]
65              }
66            }
67          ]
68        }"#,
69    )
70    .expect("valid gitlab catalog");
71
72    // 1) Multi-server composition & namespace simulation
73    let composed = compose_catalogs(
74        &[
75            ("github".into(), github.clone()),
76            ("gitlab".into(), gitlab.clone()),
77        ],
78        &AuditConfig::default(),
79    );
80    println!("{}", composed.to_text());
81
82    // 2) Pre-indexed Bm25Index on the namespaced catalog + explainability
83    let mut combined = github.with_namespace_prefix("github").tools().to_vec();
84    combined.extend(
85        gitlab
86            .with_namespace_prefix("gitlab")
87            .tools()
88            .iter()
89            .cloned(),
90    );
91    let namespaced = Catalog::new(combined);
92
93    let index = Bm25Index::build(&namespaced);
94    let hits = index.search("open gitlab merge requests", 2);
95    println!(
96        "Top Bm25Index hit: {} ({:.3})",
97        hits[0].tool_name, hits[0].score
98    );
99
100    let explanation = explain_query(
101        &namespaced,
102        "open gitlab merge requests",
103        Bm25Ranker::default(),
104        2,
105    );
106    println!("{}", explanation.to_text());
107
108    // 3) Side-by-side ranker comparison on an auto-generated adversarial suite
109    let suite = EvalSuite::generate_adversarial(&namespaced);
110    let tfidf = TfIdfRanker::default();
111    let hybrid = HybridRanker::default();
112    let cmp = compare_rankers(
113        &suite,
114        &namespaced,
115        &[("bm25", &index), ("tfidf", &tfidf), ("hybrid", &hybrid)],
116        3,
117    );
118    println!("{}", cmp.to_text());
119}