Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite4e7ecc

Browse files
committed
chore(mcp): return StdioServer directly instead of return an io.Closer
1 parent86fdd92 commite4e7ecc

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
returncmd
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
returnerr
5253
}
5354
configPath=filepath.Join(configPath,"Claude")
54-
err=os.MkdirAll(configPath,0755)
55+
err=os.MkdirAll(configPath,0o755)
5556
iferr!=nil {
5657
returnerr
5758
}
@@ -85,7 +86,7 @@ func (r *RootCmd) mcpConfigureClaudeDesktop() *serpent.Command {
8586
iferr!=nil {
8687
returnerr
8788
}
88-
err=os.WriteFile(configPath,data,0600)
89+
err=os.WriteFile(configPath,data,0o600)
8990
iferr!=nil {
9091
returnerr
9192
}
@@ -95,7 +96,7 @@ func (r *RootCmd) mcpConfigureClaudeDesktop() *serpent.Command {
9596
returncmd
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
returncmd
107108
}
108109

109-
func (_*RootCmd)mcpConfigureCursor()*serpent.Command {
110+
func (*RootCmd)mcpConfigureCursor()*serpent.Command {
110111
varprojectbool
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
iferr!=nil {
136137
returnerr
137138
}
@@ -172,7 +173,7 @@ func (_ *RootCmd) mcpConfigureCursor() *serpent.Command {
172173
iferr!=nil {
173174
returnerr
174175
}
175-
err=os.WriteFile(mcpConfig,data,0600)
176+
err=os.WriteFile(mcpConfig,data,0o600)
176177
iferr!=nil {
177178
returnerr
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
iflen(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-
iferr:=closer.Close();err!=nil {
263+
done:=make(chanerror)
264+
gofunc() {
265+
deferclose(done)
266+
srvErr:=srv.Listen(ctx,invStdin,invStdout)
267+
done<-srvErr
268+
}()
269+
270+
iferr:=<-done;err!=nil {
265271
if!errors.Is(err,context.Canceled) {
266-
cliui.Errorf(inv.Stderr,"Failed tostop the MCP server: %s",err)
272+
cliui.Errorf(inv.Stderr,"Failed tostart the MCP server: %s",err)
267273
returnerr
268274
}
269275
}
276+
270277
returnnil
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
typemcpOptionsstruct {
20-
in io.Reader
21-
out io.Writer
2218
instructionsstring
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-
funcWithStdin(in io.Reader)Option {
46-
returnfunc(o*mcpOptions) {
47-
o.in=in
48-
}
49-
}
50-
51-
// WithStdout sets the output writer for the MCP server.
52-
funcWithStdout(out io.Writer)Option {
53-
returnfunc(o*mcpOptions) {
54-
o.out=out
55-
}
56-
}
57-
5840
// WithAllowedTools sets the allowed tools for the MCP server.
5941
funcWithAllowedTools(tools []string)Option {
6042
returnfunc(o*mcpOptions) {
6143
o.allowedTools=tools
6244
}
6345
}
6446

65-
// New creates a new MCP server with the given client and options.
66-
funcNew(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+
funcNewStdio(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:=rangeopts {
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(chanerror)
98-
gofunc() {
99-
deferclose(done)
100-
srvErr:=srv.Listen(ctx,options.in,options.out)
101-
done<-srvErr
102-
}()
103-
104-
returncloseFunc(func()error {
105-
return<-done
106-
})
77+
returnsrv
10778
}
10879

10980
typecloseFuncfunc()error

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp