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
+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())