|
| 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. |
0 commit comments