intermcp 0.2.0

Ultra-fast, safe Model Context Protocol (MCP) engine and multiplexing hub in pure Rust, built for Interlayer Blockchain and open for all
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
pub mod auto_config;
pub mod cache;
pub mod config;
pub mod discovery;
pub mod error;
pub mod guardrails;
pub mod http_server;
pub mod hub;
pub mod manifest;
pub mod policy;
pub mod prompt;
pub mod protocol;
pub mod reaper;
pub mod receipts;
pub mod record;
pub mod resource;
pub mod sandbox;
pub mod server;
pub mod smac;
pub mod taint;
pub mod tool;
pub mod tools;
pub mod vault_lock;
pub mod wasm;

pub use auto_config::{
    configure_all_ides, configure_all_ides as auto_configure_all_ides, configure_mcp_json,
    configure_zed_json, SetupResult,
};
pub use cache::ToolCache;
pub use config::PolicyConfig;
pub use discovery::create_tool_discovery_tool;
pub use error::FastMcpError;
pub use guardrails::GuardrailPolicy;
pub use http_server::{run_http_server, HttpServerConfig};
pub use hub::{
    load_hub_tools, load_hub_tools_with_firewall, HubConfig, PinnedToolContract,
    SupplyChainFirewall, UpstreamServerConfig,
};
pub use manifest::{load_manifest_tools, DeclarativeTool, ManifestConfig, ManifestTool};
pub use policy::{
    DeclarativePolicy, FilesystemPolicy, LimitsPolicy, PolicyEngine, PolicyMode, PolicyViolation,
    ShellPolicy, ShellPolicyDecision,
};
pub use prompt::{Prompt, SimplePrompt};
pub use protocol::{CallToolResult, ContentItem, JsonRpcRequest, JsonRpcResponse, ToolDefinition};
pub use receipts::{
    canonicalize_json, hash_canonical_json, verify_receipt_chain_file, ExecutionReceipt,
    ReceiptBook, ReceiptStatus, SignedReceiptRecord, VerificationSummary,
};
pub use record::{FrameDirection, ReplaySummary, SessionFrame, SessionRecorder, SessionReplayer};
pub use resource::{Resource, SimpleResource};
pub use sandbox::SandboxPolicy;
pub use server::{mask_secrets, redact_for_log, Server};
pub use smac::{verify_smac_log, SmacEntry, SmacLogger};
pub use taint::{SensitivityLabel, SinkCapability, TaintTracker, TaintViolation};
pub use tool::{SimpleTool, Tool};
pub use vault_lock::{PendingActionSummary, TimeLockedVault};
pub use wasm::{
    WasmInspector, WasmModuleMetadata, WasmModuleValidator, WasmSandboxConfig, WasmTool,
};

pub type Result<T> = std::result::Result<T, FastMcpError>;

/// Instantiate standard InterMCP server with universal tools, resources, and prompts
pub fn create_default_server() -> Server {
    let mut server = Server::new("intermcp", env!("CARGO_PKG_VERSION"));
    server.add_tools(tools::universal_toolset(None));
    server.add_resource(resource::create_system_resource());
    server.add_prompt(prompt::create_code_review_prompt());
    server
}

/// Instantiate an InterMCP server with SafeFS sandboxing and optional caching
pub fn create_sandboxed_server(
    sandbox: SandboxPolicy,
    cache_ttl: Option<std::time::Duration>,
) -> Server {
    let mut server = Server::new("intermcp-sandboxed", env!("CARGO_PKG_VERSION"));
    if let Some(ttl) = cache_ttl {
        server = server.with_cache(ttl);
    }
    server.add_tools(tools::universal_toolset(Some(sandbox)));
    server.add_resource(resource::create_system_resource());
    server.add_prompt(prompt::create_code_review_prompt());
    server
}

/// Instantiate an InterMCP server with dynamic semantic tool discovery enabled (saves 85% prompt tokens)
pub fn create_smart_discovery_server() -> Server {
    let mut server = create_default_server();
    let defs = server.list_tool_definitions();
    let discovery_tool = discovery::create_tool_discovery_tool(defs);
    server.add_tool(discovery_tool);
    server
}

/// Instantiate an InterMCP server with an optional plugin (e.g. "gravity" for Omni-VM)
pub fn create_server_with_plugin(plugin: &str) -> Server {
    let mut server = create_default_server();
    server.add_tools(tools::plugin_toolset(plugin));
    server
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;
    use std::time::Duration;

    #[tokio::test]
    async fn test_mcp_initialize() {
        let server = create_default_server();
        let init_req = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {
                "protocolVersion": "2024-11-05",
                "clientInfo": {
                    "name": "claude-desktop",
                    "version": "0.1.0"
                }
            }
        });

        let resp_str = server.handle_raw_message(&init_req.to_string()).await;
        assert!(resp_str.is_some());
        let resp: JsonRpcResponse = serde_json::from_str(&resp_str.unwrap()).unwrap();
        assert_eq!(resp.id, json!(1));
        assert!(resp.error.is_none());
        assert!(resp.result.is_some());
    }

    #[tokio::test]
    async fn test_mcp_smart_discovery_tool() {
        let server = create_smart_discovery_server();

        let call_req = json!({
            "jsonrpc": "2.0",
            "id": 15,
            "method": "tools/call",
            "params": {
                "name": "intermcp_search_tools",
                "arguments": { "query": "git" }
            }
        })
        .to_string();

        let resp_str = server.handle_raw_message(&call_req).await.unwrap();
        let resp: JsonRpcResponse = serde_json::from_str(&resp_str).unwrap();
        let tool_res: CallToolResult = serde_json::from_value(resp.result.unwrap()).unwrap();
        assert!(!tool_res.is_error);
        if let ContentItem::Text { text } = &tool_res.content[0] {
            assert!(text.contains("git_status"));
            assert!(text.contains("git_diff"));
        } else {
            panic!("Expected text output");
        }
    }

    #[tokio::test]
    async fn test_mcp_loop_breaker_guardrail() {
        let server = create_default_server().with_guardrails(100, 3); // loop threshold: 3 calls

        let call_req = json!({
            "jsonrpc": "2.0",
            "id": 50,
            "method": "tools/call",
            "params": {
                "name": "system_info",
                "arguments": {}
            }
        })
        .to_string();

        // Call 1, 2, 3 should succeed
        let _ = server.handle_raw_message(&call_req).await;
        let _ = server.handle_raw_message(&call_req).await;
        let _ = server.handle_raw_message(&call_req).await;

        // Call 4: Loop Breaker must trigger!
        let resp4_str = server.handle_raw_message(&call_req).await.unwrap();
        let resp4: JsonRpcResponse = serde_json::from_str(&resp4_str).unwrap();
        let tool_res: CallToolResult = serde_json::from_value(resp4.result.unwrap()).unwrap();

        assert!(tool_res.is_error);
        if let ContentItem::Text { text } = &tool_res.content[0] {
            assert!(text.contains("InterMCP Loop Breaker: Infinite loop detected"));
        } else {
            panic!("Expected loop breaker error text");
        }
    }

    #[tokio::test]
    async fn test_mcp_tool_caching() {
        let server = create_default_server().with_cache(Duration::from_secs(60));

        let call_req = json!({
            "jsonrpc": "2.0",
            "id": 10,
            "method": "tools/call",
            "params": {
                "name": "system_info",
                "arguments": {}
            }
        })
        .to_string();

        let resp1 = server.handle_raw_message(&call_req).await;
        assert!(resp1.is_some());

        let resp2 = server.handle_raw_message(&call_req).await;
        assert!(resp2.is_some());

        let (hits, misses, count) = server.cache_stats().unwrap();
        assert_eq!(misses, 1);
        assert_eq!(hits, 1);
        assert_eq!(count, 1);
    }

    #[tokio::test]
    async fn test_mcp_safefs_sandbox_violation() {
        let sandbox = SandboxPolicy::new(vec![std::path::PathBuf::from("./src")]);
        let server = create_sandboxed_server(sandbox, None);

        let call_req = json!({
            "jsonrpc": "2.0",
            "id": 20,
            "method": "tools/call",
            "params": {
                "name": "fs_read_file",
                "arguments": { "path": "../../Cargo.toml" }
            }
        })
        .to_string();

        let resp_str = server.handle_raw_message(&call_req).await.unwrap();
        let resp: JsonRpcResponse = serde_json::from_str(&resp_str).unwrap();
        let tool_res: CallToolResult = serde_json::from_value(resp.result.unwrap()).unwrap();

        assert!(tool_res.is_error);
        if let ContentItem::Text { text } = &tool_res.content[0] {
            assert!(text.contains("SafeFS Security Violation"));
        } else {
            panic!("Expected SafeFS violation text");
        }
    }

    #[tokio::test]
    async fn test_mcp_resources() {
        let server = create_default_server();

        let list_req = json!({
            "jsonrpc": "2.0",
            "id": "res-1",
            "method": "resources/list",
            "params": {}
        });
        let resp_str = server
            .handle_raw_message(&list_req.to_string())
            .await
            .unwrap();
        let resp: JsonRpcResponse = serde_json::from_str(&resp_str).unwrap();
        let list_res: protocol::ListResourcesResult =
            serde_json::from_value(resp.result.unwrap()).unwrap();
        assert!(list_res
            .resources
            .iter()
            .any(|r| r.uri == "system://diagnostics"));
    }

    #[tokio::test]
    async fn test_safefs_nested_uncreated_path() {
        let sandbox = SandboxPolicy::new(vec![std::path::PathBuf::from("./src")]);

        // Legitimate nested uncreated file inside ./src must be allowed
        let valid_path = std::path::Path::new("./src/uncreated_sub/nested/file.rs");
        assert!(sandbox.validate_path(valid_path).is_ok());

        // Path traversal using ../ outside allowed root must be blocked
        let traversal_path = std::path::Path::new("./src/uncreated_sub/../../Cargo.toml");
        assert!(sandbox.validate_path(traversal_path).is_err());
    }

    #[tokio::test]
    async fn test_cache_safety_on_mutations() {
        // Filesystem and git tools must NEVER be cached to prevent serving stale data to agents
        let server = create_default_server().with_cache(Duration::from_secs(60));

        let call_req = json!({
            "jsonrpc": "2.0",
            "id": 99,
            "method": "tools/call",
            "params": {
                "name": "git_status",
                "arguments": {}
            }
        })
        .to_string();

        let _ = server.handle_raw_message(&call_req).await;
        let _ = server.handle_raw_message(&call_req).await;

        let (hits, _misses, _count) = server.cache_stats().unwrap();
        // git_status is not cacheable, so hits must remain 0!
        assert_eq!(hits, 0);
    }

    #[tokio::test]
    async fn test_safefs_secret_shield() {
        let sandbox = SandboxPolicy::new(vec![std::path::PathBuf::from(".")]);
        // .env file inside allowed root must still be blocked by Secret Shield
        let env_path = std::path::Path::new("./.env");
        let res = sandbox.validate_path(env_path);
        assert!(res.is_err());
        let err_msg = res.unwrap_err().to_string();
        assert!(err_msg.contains("Secret Shield"));

        // id_rsa private key must be blocked
        let rsa_path = std::path::Path::new("./id_rsa");
        assert!(sandbox.validate_path(rsa_path).is_err());
    }

    #[tokio::test]
    async fn test_safeshell_destructive_command_blocked() {
        let server = create_default_server();
        let call_req = json!({
            "jsonrpc": "2.0",
            "id": 101,
            "method": "tools/call",
            "params": {
                "name": "system_run_command",
                "arguments": { "command": "rm -rf /" }
            }
        })
        .to_string();

        let resp_str = server.handle_raw_message(&call_req).await.unwrap();
        let resp: JsonRpcResponse = serde_json::from_str(&resp_str).unwrap();
        let tool_res: CallToolResult = serde_json::from_value(resp.result.unwrap()).unwrap();
        assert!(tool_res.is_error);
        if let ContentItem::Text { text } = &tool_res.content[0] {
            assert!(text.contains("Safe-Shell Violation"));
        } else {
            panic!("Expected Safe-Shell violation text");
        }
    }

    #[tokio::test]
    async fn test_semantic_discovery_synonyms() {
        let server = create_smart_discovery_server();

        // Query with semantic intent synonym "save code" should discover fs_write_file
        let call_req = json!({
            "jsonrpc": "2.0",
            "id": 102,
            "method": "tools/call",
            "params": {
                "name": "intermcp_search_tools",
                "arguments": { "query": "save code" }
            }
        })
        .to_string();

        let resp_str = server.handle_raw_message(&call_req).await.unwrap();
        let resp: JsonRpcResponse = serde_json::from_str(&resp_str).unwrap();
        let tool_res: CallToolResult = serde_json::from_value(resp.result.unwrap()).unwrap();
        assert!(!tool_res.is_error);
        if let ContentItem::Text { text } = &tool_res.content[0] {
            assert!(text.contains("fs_write_file"));
        } else {
            panic!("Expected text output with fs_write_file");
        }
    }

    #[tokio::test]
    async fn test_token_budget_sentinel() {
        // Create server with strict 10-token budget (~40 chars)
        let server = create_default_server().with_token_budget(10);

        let call_req = json!({
            "jsonrpc": "2.0",
            "id": 103,
            "method": "tools/call",
            "params": {
                "name": "system_info",
                "arguments": {}
            }
        })
        .to_string();

        let resp_str = server.handle_raw_message(&call_req).await.unwrap();
        let resp: JsonRpcResponse = serde_json::from_str(&resp_str).unwrap();
        let tool_res: CallToolResult = serde_json::from_value(resp.result.unwrap()).unwrap();
        // Because system_info output is > 100 chars, it must trigger the budget sentinel
        assert!(tool_res.is_error);
        if let ContentItem::Text { text } = &tool_res.content[0] {
            assert!(text.contains("InterMCP Budget Sentinel"));
        } else {
            panic!("Expected budget sentinel error");
        }
    }

    #[tokio::test]
    async fn test_secret_vault_redaction() {
        std::env::set_var("TEST_INTERMCP_SECRET_TOKEN", "super_secret_api_key_12345");
        let server = create_default_server();

        // If a command echos the secret token, it must be intercepted and masked
        let call_req = json!({
            "jsonrpc": "2.0",
            "id": 104,
            "method": "tools/call",
            "params": {
                "name": "system_run_command",
                "arguments": { "command": "echo super_secret_api_key_12345" }
            }
        })
        .to_string();

        let resp_str = server.handle_raw_message(&call_req).await.unwrap();
        let resp: JsonRpcResponse = serde_json::from_str(&resp_str).unwrap();
        let tool_res: CallToolResult = serde_json::from_value(resp.result.unwrap()).unwrap();
        if let ContentItem::Text { text } = &tool_res.content[0] {
            assert!(!text.contains("super_secret_api_key_12345"));
            assert!(text.contains("[REDACTED_BY_INTERMCP]"));
        } else {
            panic!("Expected redacted text output");
        }
    }

    #[test]
    fn test_cache_capacity_eviction() {
        let cache = ToolCache::new(Duration::from_secs(300));
        // Insert 1005 items; cache capacity must stay bounded at 1000 items
        for i in 0..1005 {
            let key = format!("val_{}", i);
            cache.set("tool", &json!({ "i": key }), json!({ "res": i }), None);
        }
        let (_, _, count) = cache.stats();
        assert!(count <= 1000);
    }

    #[tokio::test]
    async fn test_guardrail_consecutive_loop_reset() {
        // Threshold: 3 consecutive calls
        let server = create_default_server().with_guardrails(100, 3);

        let call_a = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "tools/call",
            "params": { "name": "system_info", "arguments": {} }
        })
        .to_string();

        let call_b = json!({
            "jsonrpc": "2.0",
            "id": 2,
            "method": "tools/call",
            "params": { "name": "git_status", "arguments": {} }
        })
        .to_string();

        // Alternating calls A, B, A, B should NOT trigger loop breaker
        for _ in 0..5 {
            let _ = server.handle_raw_message(&call_a).await;
            let _ = server.handle_raw_message(&call_b).await;
        }

        // 4 consecutive calls to A MUST trigger loop breaker on call 4
        let _ = server.handle_raw_message(&call_a).await;
        let _ = server.handle_raw_message(&call_a).await;
        let _ = server.handle_raw_message(&call_a).await;
        let resp4_str = server.handle_raw_message(&call_a).await.unwrap();
        let resp4: JsonRpcResponse = serde_json::from_str(&resp4_str).unwrap();
        let tool_res: CallToolResult = serde_json::from_value(resp4.result.unwrap()).unwrap();
        assert!(tool_res.is_error);
        if let ContentItem::Text { text } = &tool_res.content[0] {
            assert!(text.contains("InterMCP Loop Breaker"));
        }
    }

    #[test]
    fn test_hub_config_with_env() {
        let json_data = json!({
            "servers": [
                {
                    "name": "github",
                    "command": "npx",
                    "args": ["-y", "@modelcontextprotocol/server-github"],
                    "env": {
                        "GITHUB_PERSONAL_ACCESS_TOKEN": "ghp_12345"
                    }
                }
            ]
        });

        let hub_cfg: HubConfig = serde_json::from_value(json_data).unwrap();
        assert_eq!(
            hub_cfg.servers[0]
                .env
                .get("GITHUB_PERSONAL_ACCESS_TOKEN")
                .unwrap(),
            "ghp_12345"
        );
    }
}