Skip to content

Commit 95aad07

Browse files
committed
gopls/internal/mcp: document MCP for v0.20.0; some CLI improvements
Add documentation to our features and release notes for the MCP server added in v0.20.0. Additionally, make the following CLI improvements: - s/mcp-listen/mcp.listen, to be consistent with the rpc.trace flag. - Add an -instructions flag to print the instructions. Users installing gopls from the module proxy will not have access to its source. Fixes golang/go#73580 Change-Id: Id2f7d9b2076126e84db1510d7bfcb80702e4d6b4 Reviewed-on: https://go-review.googlesource.com/c/tools/+/687435 Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent d90a343 commit 95aad07

File tree

10 files changed

+112
-14
lines changed

10 files changed

+112
-14
lines changed

gopls/doc/features/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ when making significant changes to existing features or when adding new ones.
6464
- [go.mod and go.work files](modfiles.md): Go module and workspace manifests
6565
- [Go *.s assembly files](assembly.md): Go assembly files
6666
- [Command-line interface](../command-line.md): CLI for debugging and scripting (unstable)
67+
- [Model Context Protocol](mcp.md): use some features in AI-assisted environments
6768

6869
You can find this page from within your editor by executing the
6970
`gopls.doc.features` [code action](transformation.md#code-actions),

gopls/doc/features/mcp.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
title: "Gopls: Model Context Protocol support"
3+
---
4+
5+
Gopls includes an experimental built-in server for the [Model Context
6+
Protocol](https://modelcontextprotocol.io/introduction) (MCP), allowing it to
7+
expose a subset of its functionality to AI assistants in the form of MCP tools.
8+
9+
## Running the MCP server
10+
11+
There are two modes for running this server: 'attached' and 'detached'. In
12+
attached mode, the MCP server operates in the context of an active gopls LSP
13+
session, and so is able to share memory with your LSP session and observe the
14+
current unsaved buffer state. In detached mode, gopls interacts with a headless
15+
LSP session, and therefore only sees saved files on disk.
16+
17+
### Attached mode
18+
19+
To use the 'attached' mode, run gopls with the `-mcp.listen` flag. For
20+
example:
21+
22+
```
23+
gopls serve -mcp.listen=localhost:8092`
24+
```
25+
26+
This exposes an HTTP based MCP server using the server-sent event transport
27+
(SSE), available at `http://localhost:8092/sessions/1` (assuming you have only
28+
one [session](../daemon.md) on your gopls instance).
29+
30+
### Detached mode
31+
32+
To use the 'detached' mode, run the `mcp` subcommand:
33+
34+
```
35+
gopls mcp
36+
```
37+
38+
This runs a standalone gopls instance that speaks MCP over stdin/stdout.
39+
40+
## Instructions to the model
41+
42+
This gopls MCP server includes model instructions for its usage, describing
43+
workflows for interacting with Go code using its available tools. These
44+
instructions are automatically published during the MCP server initialization,
45+
but you may want to also load them as additional context in your AI-assisted
46+
session, to emphasize their importance. The `-instructions` flag causes them to
47+
be printed, so that you can do, for example:
48+
49+
```
50+
gopls mcp -instructions` > /path/to/contextFile.md
51+
```
52+
53+
## Security considerations
54+
55+
The gopls MCP server is a wrapper around the functionality ordinarily exposed
56+
by gopls through the Language Server Protocol (LSP). As such, gopls' tools
57+
may perform any of the operations gopls normally performs, including:
58+
59+
- reading files from the file system, and returning their contents in tool
60+
results (such as when providing context);
61+
- executing the `go` command to load package information, which may result in
62+
calls to https://proxy.golang.org to download Go modules, and writes to go
63+
caches;
64+
- writing to gopls' cache or persistant configuration files; and
65+
- uploading weekly telemetry data **if you have opted in** to [Go telemetry](https://go.dev/doc/telemetry).
66+
67+
The gopls MCP server does not perform any operations not already performed by
68+
gopls in an ordinary IDE session. Like most LSP servers, gopls does not
69+
generally write directly to your source tree, though it may instruct the client
70+
to apply edits. Nor does it make arbitrary requests over the network, though it
71+
may make narrowly scoped requests to certain services such as the Go module
72+
mirror or the Go vulnerability database, which can't readily be exploited as a
73+
vehicle for exfiltration by a confused agent. Nevertheless, these capabilities
74+
may require additional consideration when used as part of an AI-enabled system.

gopls/doc/release/v0.20.0.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
title: "Gopls release v0.20.0 (forthcoming)"
33
---
44

5-
Gopls' documentation is now available on the Go project's website at
6-
https://go.dev/gopls. (Note: this link will not be accurate until the
5+
This release contains a new experimental Model Context Protocol server for
6+
gopls, which may be used to integrate a subset of gopls' features in
7+
AI-assisted environments.
8+
9+
Additionally, gopls' documentation is now available on the Go project's website
10+
at https://go.dev/gopls. (Note: this link will not be accurate until the
711
completion of the release. Dueing the pre-release period, please use
812
https://tip.golang.org/gopls, which reflects the latest commit.)
913

@@ -12,9 +16,7 @@ web search index.
1216

1317
## Configuration Changes
1418

15-
## Navigation features
16-
17-
### Web-based features
19+
## Web-based features
1820

1921
### "Split package" tool
2022

@@ -46,6 +48,20 @@ https://go.dev/issue#xxxxx
4648

4749
## Editing features
4850

51+
### Model Context Protocol server
52+
53+
Gopls now includes an experimental built-in server for the Model Context
54+
Protocol (MCP), allowing it to expose a subset of its functionality to
55+
AI assistants in the form of MCP tools.
56+
57+
See the [documentation](../features/mcp.md) for more information.
58+
59+
**Caveats:** This is a brand new mode of operation for gopls, and so we're
60+
still experimenting with the best set of tools and instructions to provide.
61+
Please let us know how well it works for you. Also, please be aware that using
62+
gopls in an AI powered environment may carry additional security
63+
considerations, as discussed in the documentation above.
64+
4965
## Analysis features
5066

5167
### `ignoredError` inlay hint

gopls/internal/cmd/mcp.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ import (
2323
type headlessMCP struct {
2424
app *Application
2525

26-
Address string `flag:"listen" help:"the address on which to run the mcp server"`
27-
Logfile string `flag:"logfile" help:"filename to log to; if unset, logs to stderr"`
28-
RPCTrace bool `flag:"rpc.trace" help:"print MCP rpc traces; cannot be used with -listen"`
26+
Address string `flag:"listen" help:"the address on which to run the mcp server"`
27+
Logfile string `flag:"logfile" help:"filename to log to; if unset, logs to stderr"`
28+
RPCTrace bool `flag:"rpc.trace" help:"print MCP rpc traces; cannot be used with -listen"`
29+
Instructions bool `flag:"instructions" help:"if set, print gopls' MCP instructions and exit"`
2930
}
3031

3132
func (m *headlessMCP) Name() string { return "mcp" }
@@ -46,6 +47,10 @@ Examples:
4647
}
4748

4849
func (m *headlessMCP) Run(ctx context.Context, args ...string) error {
50+
if m.Instructions {
51+
fmt.Println(mcp.Instructions)
52+
return nil
53+
}
4954
if m.Address != "" && m.RPCTrace {
5055
// There's currently no way to plumb logging instrumentation into the SSE
5156
// transport that is created on connections to the HTTP handler, so we must

gopls/internal/cmd/serve.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type Serve struct {
4141
RemoteLogfile string `flag:"remote.logfile" help:"when used with -remote=auto, the -logfile value used to start the daemon"`
4242

4343
// MCP Server related configurations.
44-
MCPAddress string `flag:"mcp-listen" help:"experimental: address on which to listen for model context protocol connections. If port is localhost:0, pick a random port in localhost instead."`
44+
MCPAddress string `flag:"mcp.listen" help:"experimental: address on which to listen for model context protocol connections. If port is localhost:0, pick a random port in localhost instead."`
4545

4646
app *Application
4747
}

gopls/internal/cmd/usage/mcp.hlp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Starts the server over stdio or sse with http, depending on whether the listen f
99
Examples:
1010
$ gopls mcp -listen=localhost:3000
1111
$ gopls mcp //start over stdio
12+
-instructions
13+
if set, print gopls' MCP instructions and exit
1214
-listen=string
1315
the address on which to run the mcp server
1416
-logfile=string

gopls/internal/cmd/usage/serve.hlp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ server-flags:
1616
when used with -listen, shut down the server when there are no connected clients for this duration
1717
-logfile=string
1818
filename to log to. if value is "auto", then logging to a default output file is enabled
19-
-mcp-listen=string
19+
-mcp.listen=string
2020
experimental: address on which to listen for model context protocol connections. If port is localhost:0, pick a random port in localhost instead.
2121
-mode=string
2222
no effect

gopls/internal/cmd/usage/usage-v.hlp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ flags:
6060
when used with -listen, shut down the server when there are no connected clients for this duration
6161
-logfile=string
6262
filename to log to. if value is "auto", then logging to a default output file is enabled
63-
-mcp-listen=string
63+
-mcp.listen=string
6464
experimental: address on which to listen for model context protocol connections. If port is localhost:0, pick a random port in localhost instead.
6565
-mode=string
6666
no effect

gopls/internal/cmd/usage/usage.hlp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ flags:
5757
when used with -listen, shut down the server when there are no connected clients for this duration
5858
-logfile=string
5959
filename to log to. if value is "auto", then logging to a default output file is enabled
60-
-mcp-listen=string
60+
-mcp.listen=string
6161
experimental: address on which to listen for model context protocol connections. If port is localhost:0, pick a random port in localhost instead.
6262
-mode=string
6363
no effect

gopls/internal/mcp/mcp.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
)
2626

2727
//go:embed instructions.md
28-
var instructions string
28+
var Instructions string
2929

3030
// A handler implements various MCP tools for an LSP session.
3131
type handler struct {
@@ -156,7 +156,7 @@ func newServer(session *cache.Session, lspServer protocol.Server) *mcp.Server {
156156
lspServer: lspServer,
157157
}
158158
mcpServer := mcp.NewServer("gopls", "v0.1.0", &mcp.ServerOptions{
159-
Instructions: instructions,
159+
Instructions: Instructions,
160160
})
161161

162162
defaultTools := []*mcp.ServerTool{

0 commit comments

Comments
 (0)