Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions cmd/mcptools/commands/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,16 @@ var CreateClientFunc = func(args []string, opts ...client.Option) (*client.Clien
return client.NewHTTP(server), nil
}
cmdParts := client.ParseCommandString(server)
return client.NewStdio(cmdParts), nil
c := client.NewStdio(cmdParts, opts...)
return c, nil
}
}

if len(args) == 1 && isHTTP(args[0]) {
return client.NewHTTP(args[0]), nil
}

c := client.NewStdio(args)

for _, opt := range opts {
opt(c)
}
c := client.NewStdio(args, opts...)

return c, nil
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@ func NewWithTransport(t transport.Transport) *Client {

// NewStdio creates a new MCP client that communicates with a command
// via stdin/stdout using JSON-RPC.
func NewStdio(command []string) *Client {
return &Client{
func NewStdio(command []string, opts ...Option) *Client {
c := &Client{
transport: transport.NewStdio(command),
}
for _, opt := range opts {
opt(c)
}
return c
}

// NewHTTP creates a MCP client that communicates with a server via HTTP using JSON-RPC.
Expand Down
24 changes: 14 additions & 10 deletions pkg/transport/stdio.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ type Stdio struct {

// stdioProcess reflects the state of a running command.
type stdioProcess struct {
stdin io.WriteCloser
stdout io.ReadCloser
cmd *exec.Cmd
stderrBuf *bytes.Buffer
stdin io.WriteCloser
stdout io.ReadCloser
cmd *exec.Cmd
stderrBuf *bytes.Buffer
isInitializeSent bool
}

// NewStdio creates a new Stdio transport that will execute the given command.
Expand Down Expand Up @@ -69,14 +70,17 @@ func (t *Stdio) Execute(method string, params any) (map[string]any, error) {
fmt.Fprintf(os.Stderr, "DEBUG: Starting initialization\n")
}

if initErr := t.initialize(process.stdin, process.stdout); initErr != nil {
if t.debug {
fmt.Fprintf(os.Stderr, "DEBUG: Initialization failed: %v\n", initErr)
if process.stderrBuf.Len() > 0 {
fmt.Fprintf(os.Stderr, "DEBUG: stderr during init: %s\n", process.stderrBuf.String())
if !process.isInitializeSent {
if initErr := t.initialize(process.stdin, process.stdout); initErr != nil {
if t.debug {
fmt.Fprintf(os.Stderr, "DEBUG: Initialization failed: %v\n", initErr)
if process.stderrBuf.Len() > 0 {
fmt.Fprintf(os.Stderr, "DEBUG: stderr during init: %s\n", process.stderrBuf.String())
}
}
return nil, initErr
}
return nil, initErr
process.isInitializeSent = true
}

if t.debug {
Expand Down