@@ -106,13 +106,15 @@ func (m *MCPManager) ConnectToServerConfigs(ctx context.Context, serverConfigs [
106
106
}
107
107
108
108
results := make (chan result , len (serverConfigs ))
109
- ctxWithTimeout , cancel := context .WithTimeout (ctx , timeout )
110
- defer cancel ()
109
+ // Create a timeout context only for the connection establishment goroutines
110
+ connectionCtx , connectionCancel := context .WithTimeout (context .Background (), timeout )
111
+ defer connectionCancel ()
111
112
112
113
for _ , config := range serverConfigs {
113
114
go func (cfg ServerConfig ) {
114
115
slog .InfoContext (ctx , "Connecting to MCP server" , "server" , cfg .Name , "type" , cfg .Type , "url" , cfg .URL , "command" , cfg .Command )
115
- tools , originalToolNames , err := m .connectToServerWithNames (ctxWithTimeout , cfg )
116
+ // Pass both the long-running context (ctx) and the connection timeout context
117
+ tools , originalToolNames , err := m .connectToServerWithNames (ctx , connectionCtx , cfg )
116
118
results <- result {
117
119
tools : tools ,
118
120
err : err ,
@@ -143,7 +145,7 @@ NextServer:
143
145
connections = append (connections , connection )
144
146
slog .InfoContext (ctx , "Successfully connected to MCP server" , "server" , res .serverName , "tools" , len (res .tools ), "tool_names" , res .originalTools )
145
147
}
146
- case <- ctxWithTimeout .Done ():
148
+ case <- connectionCtx .Done ():
147
149
errors = append (errors , fmt .Errorf ("timeout connecting to MCP servers" ))
148
150
break NextServer
149
151
}
@@ -153,8 +155,8 @@ NextServer:
153
155
}
154
156
155
157
// connectToServerWithNames connects to a single MCP server and returns tools with original names
156
- func (m * MCPManager ) connectToServerWithNames (ctx context.Context , config ServerConfig ) ([]* llm.Tool , []string , error ) {
157
- tools , err := m .connectToServer (ctx , config )
158
+ func (m * MCPManager ) connectToServerWithNames (longRunningCtx context. Context , connectionCtx context.Context , config ServerConfig ) ([]* llm.Tool , []string , error ) {
159
+ tools , err := m .connectToServer (longRunningCtx , connectionCtx , config )
158
160
if err != nil {
159
161
return nil , nil , err
160
162
}
@@ -175,7 +177,9 @@ func (m *MCPManager) connectToServerWithNames(ctx context.Context, config Server
175
177
}
176
178
177
179
// connectToServer connects to a single MCP server
178
- func (m * MCPManager ) connectToServer (ctx context.Context , config ServerConfig ) ([]* llm.Tool , error ) {
180
+ // longRunningCtx: context for the ongoing MCP client lifecycle (SSE streams)
181
+ // connectionCtx: context with timeout for connection establishment only
182
+ func (m * MCPManager ) connectToServer (longRunningCtx context.Context , connectionCtx context.Context , config ServerConfig ) ([]* llm.Tool , error ) {
179
183
var mcpClient * client.Client
180
184
var err error
181
185
@@ -220,12 +224,12 @@ func (m *MCPManager) connectToServer(ctx context.Context, config ServerConfig) (
220
224
return nil , fmt .Errorf ("failed to create MCP client: %w" , err )
221
225
}
222
226
223
- // Start the client first
224
- if err := mcpClient .Start (ctx ); err != nil {
227
+ // Start the client with the long-running context for SSE streams
228
+ if err := mcpClient .Start (longRunningCtx ); err != nil {
225
229
return nil , fmt .Errorf ("failed to start MCP client: %w" , err )
226
230
}
227
231
228
- // Initialize the client
232
+ // Initialize the client with connection timeout context
229
233
initReq := mcp.InitializeRequest {
230
234
Params : mcp.InitializeParams {
231
235
ProtocolVersion : mcp .LATEST_PROTOCOL_VERSION ,
@@ -236,13 +240,13 @@ func (m *MCPManager) connectToServer(ctx context.Context, config ServerConfig) (
236
240
},
237
241
},
238
242
}
239
- if _ , err := mcpClient .Initialize (ctx , initReq ); err != nil {
243
+ if _ , err := mcpClient .Initialize (connectionCtx , initReq ); err != nil {
240
244
return nil , fmt .Errorf ("failed to initialize MCP client: %w" , err )
241
245
}
242
246
243
- // Get available tools
247
+ // Get available tools with connection timeout context
244
248
toolsReq := mcp.ListToolsRequest {}
245
- toolsResp , err := mcpClient .ListTools (ctx , toolsReq )
249
+ toolsResp , err := mcpClient .ListTools (connectionCtx , toolsReq )
246
250
if err != nil {
247
251
return nil , fmt .Errorf ("failed to list tools: %w" , err )
248
252
}
0 commit comments