Skip to content

Commit a798c0e

Browse files
committed
gopls/internal/cmd: headless mcp server command
Adds a gopls subcommand that runs the Go MCP server in "headless" mode - independently from a real LSP client. Uses the fake client implementation used by other gopls subcommands to initialize a new gopls session. `gopls mcp` - start an stdio MCP server `gopls mcp -listen=localhost:3000" - start an SSE MCP server running over http on the given address Change-Id: I64ea79196a8d1d01df02757e693354beb8e52197 Reviewed-on: https://go-review.googlesource.com/c/tools/+/675935 Reviewed-by: Hongxiang Jiang <[email protected]> Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 3f93fec commit a798c0e

28 files changed

+321
-27
lines changed

gopls/internal/cmd/call_hierarchy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (c *callHierarchy) Run(ctx context.Context, args ...string) error {
3939
return tool.CommandLineErrorf("call_hierarchy expects 1 argument (position)")
4040
}
4141

42-
conn, err := c.app.connect(ctx)
42+
conn, _, err := c.app.connect(ctx)
4343
if err != nil {
4444
return err
4545
}

gopls/internal/cmd/check.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (c *check) Run(ctx context.Context, args ...string) error {
6666
opts.RelatedInformationSupported = true
6767
}
6868

69-
conn, err := c.app.connect(ctx)
69+
conn, _, err := c.app.connect(ctx)
7070
if err != nil {
7171
return err
7272
}

gopls/internal/cmd/cmd.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ func (app *Application) featureCommands() []tool.Application {
287287
&fix{app: app}, // (non-functional)
288288
&foldingRanges{app: app},
289289
&format{app: app},
290+
&headlessMCP{app: app},
290291
&highlight{app: app},
291292
&implementation{app: app},
292293
&imports{app: app},
@@ -305,20 +306,24 @@ func (app *Application) featureCommands() []tool.Application {
305306
}
306307
}
307308

308-
// connect creates and initializes a new in-process gopls session.
309-
func (app *Application) connect(ctx context.Context) (*connection, error) {
309+
// connect creates and initializes a new in-process gopls LSP session.
310+
func (app *Application) connect(ctx context.Context) (*connection, *cache.Session, error) {
310311
client := newClient(app)
311-
var svr protocol.Server
312+
var (
313+
svr protocol.Server
314+
sess *cache.Session
315+
)
312316
if app.Remote == "" {
313317
// local
314318
options := settings.DefaultOptions(app.options)
315-
svr = server.New(cache.NewSession(ctx, cache.New(nil)), client, options)
319+
sess = cache.NewSession(ctx, cache.New(nil))
320+
svr = server.New(sess, client, options)
316321
ctx = protocol.WithClient(ctx, client)
317322
} else {
318323
// remote
319324
netConn, err := lsprpc.ConnectToRemote(ctx, app.Remote)
320325
if err != nil {
321-
return nil, err
326+
return nil, nil, err
322327
}
323328
stream := jsonrpc2.NewHeaderStream(netConn)
324329
jsonConn := jsonrpc2.NewConn(stream)
@@ -329,7 +334,7 @@ func (app *Application) connect(ctx context.Context) (*connection, error) {
329334
protocol.ClientHandler(client, jsonrpc2.MethodNotFound)))
330335
}
331336
conn := newConnection(svr, client)
332-
return conn, conn.initialize(ctx, app.options)
337+
return conn, sess, conn.initialize(ctx, app.options)
333338
}
334339

335340
func (c *connection) initialize(ctx context.Context, options func(*settings.Options)) error {

gopls/internal/cmd/codeaction.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (cmd *codeaction) Run(ctx context.Context, args ...string) error {
108108
return tool.CommandLineErrorf("codeaction expects at least 1 argument")
109109
}
110110
cmd.app.editFlags = &cmd.EditFlags
111-
conn, err := cmd.app.connect(ctx)
111+
conn, _, err := cmd.app.connect(ctx)
112112
if err != nil {
113113
return err
114114
}

gopls/internal/cmd/codelens.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func (r *codelens) Run(ctx context.Context, args ...string) error {
8383
opts.Codelenses[settings.CodeLensTest] = true
8484
}
8585

86-
conn, err := r.app.connect(ctx)
86+
conn, _, err := r.app.connect(ctx)
8787
if err != nil {
8888
return err
8989
}

gopls/internal/cmd/definition.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func (d *definition) Run(ctx context.Context, args ...string) error {
7373
o.PreferredContentFormat = protocol.Markdown
7474
}
7575
}
76-
conn, err := d.app.connect(ctx)
76+
conn, _, err := d.app.connect(ctx)
7777
if err != nil {
7878
return err
7979
}

gopls/internal/cmd/execute.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (e *execute) Run(ctx context.Context, args ...string) error {
7171

7272
e.app.editFlags = &e.EditFlags // in case command performs an edit
7373

74-
conn, err := e.app.connect(ctx)
74+
conn, _, err := e.app.connect(ctx)
7575
if err != nil {
7676
return err
7777
}

gopls/internal/cmd/folding_range.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (r *foldingRanges) Run(ctx context.Context, args ...string) error {
3636
return tool.CommandLineErrorf("folding_ranges expects 1 argument (file)")
3737
}
3838

39-
conn, err := r.app.connect(ctx)
39+
conn, _, err := r.app.connect(ctx)
4040
if err != nil {
4141
return err
4242
}

gopls/internal/cmd/format.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (c *format) Run(ctx context.Context, args ...string) error {
4242
return nil
4343
}
4444
c.app.editFlags = &c.EditFlags
45-
conn, err := c.app.connect(ctx)
45+
conn, _, err := c.app.connect(ctx)
4646
if err != nil {
4747
return err
4848
}

gopls/internal/cmd/highlight.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (r *highlight) Run(ctx context.Context, args ...string) error {
3838
return tool.CommandLineErrorf("highlight expects 1 argument (position)")
3939
}
4040

41-
conn, err := r.app.connect(ctx)
41+
conn, _, err := r.app.connect(ctx)
4242
if err != nil {
4343
return err
4444
}

0 commit comments

Comments
 (0)