fix: add optional timeout to McpClient::send_request() (#852)

We now impose a 10s timeout on the initial `tools/list` request to an
MCP server. We do not apply a timeout for other types of requests yet,
but we should start enforcing those, as well.
This commit is contained in:
Michael Bolin
2025-05-07 12:56:38 -07:00
committed by GitHub
parent 0360b4d0d7
commit 9da6ebef3f
6 changed files with 85 additions and 31 deletions

View File

@@ -5,6 +5,7 @@ use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::Mutex;
use std::time::Duration;
use anyhow::Context;
use async_channel::Receiver;
@@ -396,9 +397,10 @@ impl Session {
server: &str,
tool: &str,
arguments: Option<serde_json::Value>,
timeout: Option<Duration>,
) -> anyhow::Result<mcp_types::CallToolResult> {
self.mcp_connection_manager
.call_tool(server, tool, arguments)
.call_tool(server, tool, arguments, timeout)
.await
}
@@ -1194,7 +1196,12 @@ async fn handle_function_call(
_ => {
match try_parse_fully_qualified_tool_name(&name) {
Some((server, tool_name)) => {
handle_mcp_tool_call(sess, &sub_id, call_id, server, tool_name, arguments).await
// TODO(mbolin): Determine appropriate timeout for tool call.
let timeout = None;
handle_mcp_tool_call(
sess, &sub_id, call_id, server, tool_name, arguments, timeout,
)
.await
}
None => {
// Unknown function: reply with structured failure so the model can adapt.