mcp: execute stdio tool calls

This commit is contained in:
2026-06-20 18:07:21 +09:00
parent 92432ad750
commit 9a2454037f
4 changed files with 689 additions and 12 deletions
+92 -1
View File
@@ -10,7 +10,10 @@ fn main() {
match mode.as_str() {
"success" => success(),
"tools" => tools_list(),
"tools-call-forbidden" => tools_list(),
"tools-call-normal" => tools_call_normal(),
"tools-call-is-error" => tools_call_is_error(),
"tools-call-protocol-error" => tools_call_protocol_error(),
"tools-call-forbidden" => tools_call_forbidden(),
"fail-init" => fail_init(),
"sampling" => sampling_request(),
"shutdown-hang" => shutdown_hang(),
@@ -96,6 +99,94 @@ fn tools_list() {
}
}
fn tools_call_normal() {
tools_call(|request| {
assert_eq!(request["params"]["name"], "search-files");
assert_eq!(request["params"]["arguments"]["query"], "needle");
json!({
"jsonrpc": "2.0",
"id": request["id"],
"result": {
"content": [{"type": "text", "text": "found needle"}],
"structuredContent": {"matches": ["needle.rs"]},
"_meta": {"server": "mock"}
}
})
});
}
fn tools_call_is_error() {
tools_call(|request| {
assert_eq!(request["params"]["name"], "search-files");
json!({
"jsonrpc": "2.0",
"id": request["id"],
"result": {
"isError": true,
"content": [{"type": "text", "text": "tool-level failure"}]
}
})
});
}
fn tools_call_protocol_error() {
tools_call(|request| {
json!({
"jsonrpc": "2.0",
"id": request["id"],
"error": {"code": -32010, "message": "server refused tools/call"}
})
});
}
fn tools_call_forbidden() {
let init = read_json();
assert_eq!(init["method"], "initialize");
write_json(json!({
"jsonrpc": "2.0",
"id": init["id"],
"result": initialize_result(),
}));
let initialized = read_json();
assert_eq!(initialized["method"], "notifications/initialized");
loop {
let request = read_json();
assert_ne!(
request["method"], "tools/call",
"permission denial path must not send MCP tools/call"
);
if request["method"] == "shutdown" {
write_json(json!({"jsonrpc":"2.0", "id": request["id"], "result": {}}));
let notification = read_json();
assert_eq!(notification["method"], "exit");
break;
}
}
}
fn tools_call(response: impl FnOnce(&Value) -> Value) {
let init = read_json();
assert_eq!(init["method"], "initialize");
write_json(json!({
"jsonrpc": "2.0",
"id": init["id"],
"result": initialize_result(),
}));
let initialized = read_json();
assert_eq!(initialized["method"], "notifications/initialized");
let call = read_json();
assert_eq!(call["method"], "tools/call");
write_json(response(&call));
let shutdown = read_json();
assert_eq!(shutdown["method"], "shutdown");
write_json(json!({"jsonrpc":"2.0", "id": shutdown["id"], "result": {}}));
let notification = read_json();
assert_eq!(notification["method"], "exit");
}
fn fail_init() {
let secret = env::var("MCP_TEST_SECRET").unwrap_or_default();
for idx in 0..5 {
+70 -1
View File
@@ -1,7 +1,8 @@
use std::time::Duration;
use mcp::stdio::{
McpErrorKind, McpPhase, McpStdioClient, McpStdioLimits, McpStdioServerSpec, McpToolListLimits,
CallToolRequest, McpErrorKind, McpPhase, McpStdioClient, McpStdioLimits, McpStdioServerSpec,
McpToolListLimits,
};
fn mock_server(mode: &str) -> McpStdioServerSpec {
@@ -161,6 +162,74 @@ async fn initialize_failure_reports_server_phase_and_redacted_bounded_stderr() {
);
}
#[tokio::test]
async fn call_tool_returns_normal_result() {
let mut client = McpStdioClient::connect(mock_server("tools-call-normal"), tight_limits())
.await
.expect("connect");
let result = client
.call_tool(CallToolRequest::new(
"search-files",
serde_json::json!({"query": "needle"}),
))
.await
.expect("call tool");
assert!(!result.is_error);
assert_eq!(result.content.len(), 1);
assert_eq!(result.content[0].kind, "text");
assert_eq!(result.content[0].fields["text"], "found needle");
assert_eq!(
result.structured_content.as_ref().unwrap()["matches"][0],
"needle.rs"
);
assert_eq!(result.meta.as_ref().unwrap()["server"], "mock");
client.shutdown().await.expect("shutdown");
}
#[tokio::test]
async fn call_tool_preserves_mcp_is_error_result() {
let mut client = McpStdioClient::connect(mock_server("tools-call-is-error"), tight_limits())
.await
.expect("connect");
let result = client
.call_tool(CallToolRequest::new(
"search-files",
serde_json::json!({"query": "needle"}),
))
.await
.expect("call tool");
assert!(result.is_error);
assert_eq!(result.content[0].fields["text"], "tool-level failure");
client.shutdown().await.expect("shutdown");
}
#[tokio::test]
async fn call_tool_reports_json_rpc_protocol_error_distinctly() {
let mut client =
McpStdioClient::connect(mock_server("tools-call-protocol-error"), tight_limits())
.await
.expect("connect");
let err = client
.call_tool(CallToolRequest::new(
"search-files",
serde_json::json!({"query": "needle"}),
))
.await
.expect_err("protocol error");
assert!(matches!(err.kind, McpErrorKind::JsonRpcError { .. }));
client.shutdown().await.expect("shutdown");
}
#[tokio::test]
async fn permission_denial_style_shutdown_sends_no_tools_call() {
let mut client = McpStdioClient::connect(mock_server("tools-call-forbidden"), tight_limits())
.await
.expect("connect");
// This mirrors Worker pre-tool-call denial: the ordinary Tool execution body
// is never entered, so the MCP server sees lifecycle shutdown but no call.
client.shutdown().await.expect("shutdown");
}
#[tokio::test]
async fn shutdown_terminates_or_kills_uncooperative_server() {
let mut client = McpStdioClient::connect(mock_server("shutdown-hang"), tight_limits())