Skip to content

Commit 1cb82d7

Browse files
authored
Merge pull request #38 from ayakut16/perf/init-once
2 parents d743d3b + 400724d commit 1cb82d7

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

cmd/mcptools/commands/utils.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,16 @@ var CreateClientFunc = func(args []string, opts ...client.Option) (*client.Clien
3434
return client.NewHTTP(server), nil
3535
}
3636
cmdParts := client.ParseCommandString(server)
37-
return client.NewStdio(cmdParts), nil
37+
c := client.NewStdio(cmdParts, opts...)
38+
return c, nil
3839
}
3940
}
4041

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

45-
c := client.NewStdio(args)
46-
47-
for _, opt := range opts {
48-
opt(c)
49-
}
46+
c := client.NewStdio(args, opts...)
5047

5148
return c, nil
5249
}

pkg/client/client.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,14 @@ func NewWithTransport(t transport.Transport) *Client {
4343

4444
// NewStdio creates a new MCP client that communicates with a command
4545
// via stdin/stdout using JSON-RPC.
46-
func NewStdio(command []string) *Client {
47-
return &Client{
46+
func NewStdio(command []string, opts ...Option) *Client {
47+
c := &Client{
4848
transport: transport.NewStdio(command),
4949
}
50+
for _, opt := range opts {
51+
opt(c)
52+
}
53+
return c
5054
}
5155

5256
// NewHTTP creates a MCP client that communicates with a server via HTTP using JSON-RPC.

pkg/transport/stdio.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ type Stdio struct {
2222

2323
// stdioProcess reflects the state of a running command.
2424
type stdioProcess struct {
25-
stdin io.WriteCloser
26-
stdout io.ReadCloser
27-
cmd *exec.Cmd
28-
stderrBuf *bytes.Buffer
25+
stdin io.WriteCloser
26+
stdout io.ReadCloser
27+
cmd *exec.Cmd
28+
stderrBuf *bytes.Buffer
29+
isInitializeSent bool
2930
}
3031

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

72-
if initErr := t.initialize(process.stdin, process.stdout); initErr != nil {
73-
if t.debug {
74-
fmt.Fprintf(os.Stderr, "DEBUG: Initialization failed: %v\n", initErr)
75-
if process.stderrBuf.Len() > 0 {
76-
fmt.Fprintf(os.Stderr, "DEBUG: stderr during init: %s\n", process.stderrBuf.String())
73+
if !process.isInitializeSent {
74+
if initErr := t.initialize(process.stdin, process.stdout); initErr != nil {
75+
if t.debug {
76+
fmt.Fprintf(os.Stderr, "DEBUG: Initialization failed: %v\n", initErr)
77+
if process.stderrBuf.Len() > 0 {
78+
fmt.Fprintf(os.Stderr, "DEBUG: stderr during init: %s\n", process.stderrBuf.String())
79+
}
7780
}
81+
return nil, initErr
7882
}
79-
return nil, initErr
83+
process.isInitializeSent = true
8084
}
8185

8286
if t.debug {

0 commit comments

Comments
 (0)