Merge pull request #60 from dj-oyu/feature/mcp-call-timeout

feat: add per-server timeout for MCP tool calls
This commit is contained in:
dj-oyu 2026-03-20 06:38:40 +09:00 committed by GitHub
commit 8ffbc05e62
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 18 additions and 0 deletions

View file

@ -846,6 +846,8 @@ type MCPServerConfig struct {
URL string `json:"url,omitempty"`
// Headers are HTTP headers to send with requests (sse/http only)
Headers map[string]string `json:"headers,omitempty"`
// Timeout is the maximum duration in seconds for tool calls to this server (default: 60)
Timeout int `json:"timeout,omitempty"`
}
// MCPConfig defines configuration for all MCP servers

View file

@ -12,6 +12,7 @@ import (
"strings"
"sync"
"sync/atomic"
"time"
"github.com/modelcontextprotocol/go-sdk/mcp"
@ -103,6 +104,7 @@ type ServerConnection struct {
Client *mcp.Client
Session *mcp.ClientSession
Tools []*mcp.Tool
Timeout time.Duration
}
// Manager manages multiple MCP server connections
@ -401,6 +403,12 @@ func (m *Manager) ConnectServer(
})
}
// Resolve timeout (default 60s)
timeout := 60 * time.Second
if cfg.Timeout > 0 {
timeout = time.Duration(cfg.Timeout) * time.Second
}
// Store connection
m.mu.Lock()
m.servers[name] = &ServerConnection{
@ -408,6 +416,7 @@ func (m *Manager) ConnectServer(
Client: client,
Session: session,
Tools: tools,
Timeout: timeout,
}
m.mu.Unlock()
@ -463,6 +472,13 @@ func (m *Manager) CallTool(
}
defer m.wg.Done()
// Apply per-server timeout
if conn.Timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, conn.Timeout)
defer cancel()
}
params := &mcp.CallToolParams{
Name: toolName,
Arguments: arguments,