Skip to content

Commit d2f81b6

Browse files
authored
fix: prevent double-starting stdio transport in client (#564)
* fix: prevent double-starting stdio transport in client Avoid starting stdio transport twice by checking transport type before calling Start(). The stdio transport from NewStdioMCPClientWithOptions is already started and doesn't need to be started again. * docs: update NewStdioMCPClient comment for clarity
1 parent 40ce109 commit d2f81b6

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

client/client.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,16 @@ func (c *Client) Start(ctx context.Context) error {
7777
if c.transport == nil {
7878
return fmt.Errorf("transport is nil")
7979
}
80-
err := c.transport.Start(ctx)
81-
if err != nil {
82-
return err
80+
81+
if _, ok := c.transport.(*transport.Stdio); !ok {
82+
// the stdio transport from NewStdioMCPClientWithOptions
83+
// is already started, dont start again.
84+
//
85+
// Start the transport for other transport types
86+
err := c.transport.Start(ctx)
87+
if err != nil {
88+
return err
89+
}
8390
}
8491

8592
c.transport.SetNotificationHandler(func(notification mcp.JSONRPCNotification) {

client/stdio.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
// It launches the specified command with given arguments and sets up stdin/stdout pipes for communication.
1313
// Returns an error if the subprocess cannot be started or the pipes cannot be created.
1414
//
15-
// NOTICE: NewStdioMCPClient will start the connection automatically. Don't call the Start method manually.
15+
// NOTICE: NewStdioMCPClient will start the connection automatically.
1616
// This is for backward compatibility.
1717
func NewStdioMCPClient(
1818
command string,
@@ -28,7 +28,6 @@ func NewStdioMCPClient(
2828
// such as setting a custom command function.
2929
//
3030
// NOTICE: NewStdioMCPClientWithOptions automatically starts the underlying transport.
31-
// Don't call the Start method manually.
3231
// This is for backward compatibility.
3332
func NewStdioMCPClientWithOptions(
3433
command string,

0 commit comments

Comments
 (0)