From bc52493640ce98e9ed34717731ab43b754e5b82c Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Wed, 13 Aug 2025 16:06:40 -0300 Subject: [PATCH] feat(mcp): configurable MCP timeout closes #604 closes https://github.com/charmbracelet/crush/discussions/754 Signed-off-by: Carlos Alexandro Becker --- internal/config/config.go | 1 + internal/llm/agent/mcp-tools.go | 4 +++- schema.json | 10 ++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/internal/config/config.go b/internal/config/config.go index 18c97872b9717d01db8f124f475d92265f80a2eb..f4ba8848033a9e99b8d487e5a494565c89bda5a0 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -110,6 +110,7 @@ type MCPConfig struct { Type MCPType `json:"type" jsonschema:"required,description=Type of MCP connection,enum=stdio,enum=sse,enum=http,default=stdio"` URL string `json:"url,omitempty" jsonschema:"description=URL for HTTP or SSE MCP servers,format=uri,example=http://localhost:3000/mcp"` Disabled bool `json:"disabled,omitempty" jsonschema:"description=Whether this MCP server is disabled,default=false"` + Timeout int `json:"timeout,omitempty" jsonschema:"description=Timeout in seconds for MCP server connections,default=15,example=30,example=60,example=120"` // TODO: maybe make it possible to get the value from the env Headers map[string]string `json:"headers,omitempty" jsonschema:"description=HTTP headers for HTTP/SSE MCP servers"` diff --git a/internal/llm/agent/mcp-tools.go b/internal/llm/agent/mcp-tools.go index e5cc769bdd2e1855557eb1c4ea326df68ccc5cf0..beb35085a5206ba55d81d927c3fb84d7120bdff1 100644 --- a/internal/llm/agent/mcp-tools.go +++ b/internal/llm/agent/mcp-tools.go @@ -1,6 +1,7 @@ package agent import ( + "cmp" "context" "encoding/json" "fmt" @@ -274,7 +275,8 @@ func doGetMCPTools(ctx context.Context, permissions permission.Service, cfg *con } }() - ctx, cancel := context.WithTimeout(ctx, 10*time.Second) + timeout := time.Duration(cmp.Or(m.Timeout, 15)) * time.Second + ctx, cancel := context.WithTimeout(ctx, timeout) defer cancel() c, err := createMcpClient(m) if err != nil { diff --git a/schema.json b/schema.json index 6fbc23e81cbf81ff88497ca432af8720d1830e1f..4c2066505f01270fa94eaa5ad70dfbe4f8442728 100644 --- a/schema.json +++ b/schema.json @@ -125,6 +125,16 @@ "description": "Whether this MCP server is disabled", "default": false }, + "timeout": { + "type": "integer", + "description": "Timeout in seconds for MCP server connections", + "default": 15, + "examples": [ + 30, + 60, + 120 + ] + }, "headers": { "additionalProperties": { "type": "string"