Skip to content

Commit 4dd843d

Browse files
authored
Merge pull request #2141 from dgageot/fix-gopls
Revert "Add timeouts to remote MCP initialization and tool calls"
2 parents 10cec57 + 2efe377 commit 4dd843d

2 files changed

Lines changed: 5 additions & 48 deletions

File tree

pkg/tools/mcp/mcp.go

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -113,23 +113,6 @@ func NewRemoteToolset(name, urlString, transport string, headers map[string]stri
113113
// because there is no live connection to monitor.
114114
var errServerUnavailable = errors.New("MCP server unavailable")
115115

116-
const (
117-
// mcpInitTimeout is the maximum time allowed for the MCP initialization
118-
// handshake (connect + initialize). If a remote server accepts the TCP
119-
// connection but never responds, this prevents the agent from hanging
120-
// indefinitely.
121-
mcpInitTimeout = 2 * time.Minute
122-
123-
// mcpCallToolTimeout is the maximum time allowed for a single tool call.
124-
// Tool calls may be long-running (e.g. code execution), so this is
125-
// deliberately generous.
126-
mcpCallToolTimeout = 10 * time.Minute
127-
128-
// mcpListTimeout is the maximum time allowed for listing tools, prompts
129-
// or fetching a single prompt from the MCP server.
130-
mcpListTimeout = 1 * time.Minute
131-
)
132-
133116
// Describe returns a short, user-visible description of this toolset instance.
134117
// It never includes secrets.
135118
func (ts *Toolset) Describe() string {
@@ -193,11 +176,6 @@ func (ts *Toolset) doStart(ctx context.Context) error {
193176
// This is critical for OAuth flows where the toolset connection needs to remain alive after the initial HTTP request completes.
194177
ctx = context.WithoutCancel(ctx)
195178

196-
// Apply an initialization timeout so we don't hang forever if the
197-
// remote server accepts the connection but never responds.
198-
initCtx, cancel := context.WithTimeout(ctx, mcpInitTimeout)
199-
defer cancel()
200-
201179
slog.Debug("Starting MCP toolset", "server", ts.logID)
202180

203181
// Register notification handlers to invalidate caches when the server
@@ -241,7 +219,7 @@ func (ts *Toolset) doStart(ctx context.Context) error {
241219
const maxRetries = 3
242220
for attempt := 0; ; attempt++ {
243221
var err error
244-
result, err = ts.mcpClient.Initialize(initCtx, initRequest)
222+
result, err = ts.mcpClient.Initialize(ctx, initRequest)
245223
if err == nil {
246224
break
247225
}
@@ -269,8 +247,8 @@ func (ts *Toolset) doStart(ctx context.Context) error {
269247
slog.Debug("MCP initialize failed to send initialized notification; retrying", "id", ts.logID, "attempt", attempt+1, "backoff_ms", backoff.Milliseconds())
270248
select {
271249
case <-time.After(backoff):
272-
case <-initCtx.Done():
273-
return fmt.Errorf("failed to initialize MCP client: %w", initCtx.Err())
250+
case <-ctx.Done():
251+
return fmt.Errorf("failed to initialize MCP client: %w", ctx.Err())
274252
}
275253
}
276254

pkg/tools/mcp/session_client.go

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,7 @@ func (c *sessionClient) Close(context.Context) error {
9494

9595
func (c *sessionClient) ListTools(ctx context.Context, request *gomcp.ListToolsParams) iter.Seq2[*gomcp.Tool, error] {
9696
if s := c.getSession(); s != nil {
97-
ctx, cancel := context.WithTimeout(ctx, mcpListTimeout)
98-
// Wrap the iterator so the cancel fires after iteration completes.
99-
return func(yield func(*gomcp.Tool, error) bool) {
100-
defer cancel()
101-
for t, err := range s.Tools(ctx, request) {
102-
if !yield(t, err) {
103-
return
104-
}
105-
}
106-
}
97+
return s.Tools(ctx, request)
10798
}
10899
return func(yield func(*gomcp.Tool, error) bool) {
109100
yield(nil, errors.New("session not initialized"))
@@ -112,24 +103,14 @@ func (c *sessionClient) ListTools(ctx context.Context, request *gomcp.ListToolsP
112103

113104
func (c *sessionClient) CallTool(ctx context.Context, request *gomcp.CallToolParams) (*gomcp.CallToolResult, error) {
114105
if s := c.getSession(); s != nil {
115-
ctx, cancel := context.WithTimeout(ctx, mcpCallToolTimeout)
116-
defer cancel()
117106
return s.CallTool(ctx, request)
118107
}
119108
return nil, errors.New("session not initialized")
120109
}
121110

122111
func (c *sessionClient) ListPrompts(ctx context.Context, request *gomcp.ListPromptsParams) iter.Seq2[*gomcp.Prompt, error] {
123112
if s := c.getSession(); s != nil {
124-
ctx, cancel := context.WithTimeout(ctx, mcpListTimeout)
125-
return func(yield func(*gomcp.Prompt, error) bool) {
126-
defer cancel()
127-
for p, err := range s.Prompts(ctx, request) {
128-
if !yield(p, err) {
129-
return
130-
}
131-
}
132-
}
113+
return s.Prompts(ctx, request)
133114
}
134115
return func(yield func(*gomcp.Prompt, error) bool) {
135116
yield(nil, errors.New("session not initialized"))
@@ -138,8 +119,6 @@ func (c *sessionClient) ListPrompts(ctx context.Context, request *gomcp.ListProm
138119

139120
func (c *sessionClient) GetPrompt(ctx context.Context, request *gomcp.GetPromptParams) (*gomcp.GetPromptResult, error) {
140121
if s := c.getSession(); s != nil {
141-
ctx, cancel := context.WithTimeout(ctx, mcpListTimeout)
142-
defer cancel()
143122
return s.GetPrompt(ctx, request)
144123
}
145124
return nil, errors.New("session not initialized")

0 commit comments

Comments
 (0)