Skip to content

Commit e4e7ecc

Browse files
committed
chore(mcp): return StdioServer directly instead of return an io.Closer
1 parent 86fdd92 commit e4e7ecc

File tree

2 files changed

+24
-46
lines changed

2 files changed

+24
-46
lines changed

cli/exp_mcp.go

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"errors"
7+
"log"
78
"os"
89
"path/filepath"
910

@@ -41,7 +42,7 @@ func (r *RootCmd) mcpConfigure() *serpent.Command {
4142
return cmd
4243
}
4344

44-
func (r *RootCmd) mcpConfigureClaudeDesktop() *serpent.Command {
45+
func (*RootCmd) mcpConfigureClaudeDesktop() *serpent.Command {
4546
cmd := &serpent.Command{
4647
Use: "claude-desktop",
4748
Short: "Configure the Claude Desktop server.",
@@ -51,7 +52,7 @@ func (r *RootCmd) mcpConfigureClaudeDesktop() *serpent.Command {
5152
return err
5253
}
5354
configPath = filepath.Join(configPath, "Claude")
54-
err = os.MkdirAll(configPath, 0755)
55+
err = os.MkdirAll(configPath, 0o755)
5556
if err != nil {
5657
return err
5758
}
@@ -85,7 +86,7 @@ func (r *RootCmd) mcpConfigureClaudeDesktop() *serpent.Command {
8586
if err != nil {
8687
return err
8788
}
88-
err = os.WriteFile(configPath, data, 0600)
89+
err = os.WriteFile(configPath, data, 0o600)
8990
if err != nil {
9091
return err
9192
}
@@ -95,7 +96,7 @@ func (r *RootCmd) mcpConfigureClaudeDesktop() *serpent.Command {
9596
return cmd
9697
}
9798

98-
func (_ *RootCmd) mcpConfigureClaudeCode() *serpent.Command {
99+
func (*RootCmd) mcpConfigureClaudeCode() *serpent.Command {
99100
cmd := &serpent.Command{
100101
Use: "claude-code",
101102
Short: "Configure the Claude Code server.",
@@ -106,7 +107,7 @@ func (_ *RootCmd) mcpConfigureClaudeCode() *serpent.Command {
106107
return cmd
107108
}
108109

109-
func (_ *RootCmd) mcpConfigureCursor() *serpent.Command {
110+
func (*RootCmd) mcpConfigureCursor() *serpent.Command {
110111
var project bool
111112
cmd := &serpent.Command{
112113
Use: "cursor",
@@ -131,7 +132,7 @@ func (_ *RootCmd) mcpConfigureCursor() *serpent.Command {
131132
}
132133
}
133134
cursorDir := filepath.Join(dir, ".cursor")
134-
err = os.MkdirAll(cursorDir, 0755)
135+
err = os.MkdirAll(cursorDir, 0o755)
135136
if err != nil {
136137
return err
137138
}
@@ -172,7 +173,7 @@ func (_ *RootCmd) mcpConfigureCursor() *serpent.Command {
172173
if err != nil {
173174
return err
174175
}
175-
err = os.WriteFile(mcpConfig, data, 0600)
176+
err = os.WriteFile(mcpConfig, data, 0o600)
176177
if err != nil {
177178
return err
178179
}
@@ -249,23 +250,29 @@ func mcpServerHandler(inv *serpent.Invocation, client *codersdk.Client, instruct
249250
options := []codermcp.Option{
250251
codermcp.WithInstructions(instructions),
251252
codermcp.WithLogger(&logger),
252-
codermcp.WithStdin(invStdin),
253-
codermcp.WithStdout(invStdout),
254253
}
255254

256255
// Add allowed tools option if specified
257256
if len(allowedTools) > 0 {
258257
options = append(options, codermcp.WithAllowedTools(allowedTools))
259258
}
260259

261-
closer := codermcp.New(ctx, client, options...)
260+
srv := codermcp.NewStdio(client, options...)
261+
srv.SetErrorLogger(log.New(invStderr, "", log.LstdFlags))
262262

263-
<-ctx.Done()
264-
if err := closer.Close(); err != nil {
263+
done := make(chan error)
264+
go func() {
265+
defer close(done)
266+
srvErr := srv.Listen(ctx, invStdin, invStdout)
267+
done <- srvErr
268+
}()
269+
270+
if err := <-done; err != nil {
265271
if !errors.Is(err, context.Canceled) {
266-
cliui.Errorf(inv.Stderr, "Failed to stop the MCP server: %s", err)
272+
cliui.Errorf(inv.Stderr, "Failed to start the MCP server: %s", err)
267273
return err
268274
}
269275
}
276+
270277
return nil
271278
}

mcp/mcp.go

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package codermcp
22

33
import (
4-
"context"
54
"io"
6-
"log"
75
"os"
86

97
"github.com/mark3labs/mcp-go/server"
@@ -17,8 +15,6 @@ import (
1715
)
1816

1917
type mcpOptions struct {
20-
in io.Reader
21-
out io.Writer
2218
instructions string
2319
logger *slog.Logger
2420
allowedTools []string
@@ -41,34 +37,19 @@ func WithLogger(logger *slog.Logger) Option {
4137
}
4238
}
4339

44-
// WithStdin sets the input reader for the MCP server.
45-
func WithStdin(in io.Reader) Option {
46-
return func(o *mcpOptions) {
47-
o.in = in
48-
}
49-
}
50-
51-
// WithStdout sets the output writer for the MCP server.
52-
func WithStdout(out io.Writer) Option {
53-
return func(o *mcpOptions) {
54-
o.out = out
55-
}
56-
}
57-
5840
// WithAllowedTools sets the allowed tools for the MCP server.
5941
func WithAllowedTools(tools []string) Option {
6042
return func(o *mcpOptions) {
6143
o.allowedTools = tools
6244
}
6345
}
6446

65-
// New creates a new MCP server with the given client and options.
66-
func New(ctx context.Context, client *codersdk.Client, opts ...Option) io.Closer {
47+
// NewStdio creates a new MCP stdio server with the given client and options.
48+
// It is the responsibility of the caller to start and stop the server.
49+
func NewStdio(client *codersdk.Client, opts ...Option) *server.StdioServer {
6750
options := &mcpOptions{
68-
in: os.Stdin,
6951
instructions: ``,
7052
logger: ptr.Ref(slog.Make(sloghuman.Sink(os.Stdout))),
71-
out: os.Stdout,
7253
}
7354
for _, opt := range opts {
7455
opt(options)
@@ -93,17 +74,7 @@ func New(ctx context.Context, client *codersdk.Client, opts ...Option) io.Closer
9374
})
9475

9576
srv := server.NewStdioServer(mcpSrv)
96-
srv.SetErrorLogger(log.New(options.out, "", log.LstdFlags))
97-
done := make(chan error)
98-
go func() {
99-
defer close(done)
100-
srvErr := srv.Listen(ctx, options.in, options.out)
101-
done <- srvErr
102-
}()
103-
104-
return closeFunc(func() error {
105-
return <-done
106-
})
77+
return srv
10778
}
10879

10980
type closeFunc func() error

0 commit comments

Comments
 (0)