@@ -15,7 +15,8 @@ import (
15
15
"github.com/gptscript-ai/gptscript/pkg/mvl"
16
16
"github.com/gptscript-ai/gptscript/pkg/types"
17
17
"github.com/gptscript-ai/gptscript/pkg/version"
18
- "github.com/mark3labs/mcp-go/client"
18
+ mcpclient "github.com/mark3labs/mcp-go/client"
19
+ "github.com/mark3labs/mcp-go/client/transport"
19
20
"github.com/mark3labs/mcp-go/mcp"
20
21
)
21
22
@@ -36,7 +37,7 @@ type Local struct {
36
37
type Session struct {
37
38
ID string
38
39
InitResult * mcp.InitializeResult
39
- Client client .MCPClient
40
+ Client mcpclient .MCPClient
40
41
Config ServerConfig
41
42
}
42
43
@@ -117,7 +118,7 @@ func (l *Local) LoadTools(ctx context.Context, server ServerConfig, toolName str
117
118
// Reset so we don't start a new MCP server, no reason to if one is already running and the allowed tools change.
118
119
server .AllowedTools = nil
119
120
120
- session , err := l .loadSession (server )
121
+ session , err := l .loadSession (server , true )
121
122
if err != nil {
122
123
return nil , err
123
124
}
@@ -279,7 +280,7 @@ func (l *Local) sessionToTools(ctx context.Context, session *Session, toolName s
279
280
return toolDefs , nil
280
281
}
281
282
282
- func (l * Local ) loadSession (server ServerConfig ) (* Session , error ) {
283
+ func (l * Local ) loadSession (server ServerConfig , tryHTTPStreaming bool ) (* Session , error ) {
283
284
id := hash .Digest (server )
284
285
l .lock .Lock ()
285
286
existing , ok := l .sessions [id ]
@@ -294,11 +295,11 @@ func (l *Local) loadSession(server ServerConfig) (*Session, error) {
294
295
}
295
296
296
297
var (
297
- c * client .Client
298
+ c * mcpclient .Client
298
299
err error
299
300
)
300
301
if server .Command != "" {
301
- c , err = client .NewStdioMCPClient (server .Command , server .Env , server .Args ... )
302
+ c , err = mcpclient .NewStdioMCPClient (server .Command , server .Env , server .Args ... )
302
303
if err != nil {
303
304
return nil , fmt .Errorf ("failed to create MCP stdio client: %w" , err )
304
305
}
@@ -314,7 +315,11 @@ func (l *Local) loadSession(server ServerConfig) (*Session, error) {
314
315
headers [k ] = v
315
316
}
316
317
317
- c , err = client .NewSSEMCPClient (url , client .WithHeaders (headers ))
318
+ if tryHTTPStreaming {
319
+ c , err = mcpclient .NewStreamableHttpClient (url , transport .WithHTTPHeaders (headers ))
320
+ } else {
321
+ c , err = mcpclient .NewSSEMCPClient (url , mcpclient .WithHeaders (headers ))
322
+ }
318
323
if err != nil {
319
324
return nil , fmt .Errorf ("failed to create MCP HTTP client: %w" , err )
320
325
}
@@ -333,6 +338,13 @@ func (l *Local) loadSession(server ServerConfig) (*Session, error) {
333
338
334
339
initResult , err := c .Initialize (ctx , initRequest )
335
340
if err != nil {
341
+ if server .Command == "" && tryHTTPStreaming {
342
+ // The MCP spec indicates that trying to initialize the client for HTTP streaming and checking for an error
343
+ // is the recommended way to determine if the server supports HTTP streaming, falling back to SEE.
344
+ // Ideally, we can check for a 400-level error, but our client implementation doesn't expose that information.
345
+ // Retrying on any error is harmless.
346
+ return l .loadSession (server , false )
347
+ }
336
348
return nil , fmt .Errorf ("failed to initialize MCP client: %w" , err )
337
349
}
338
350
0 commit comments