| 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. To avoid |
| 44 | forcing the client into specific workflows, these instructions are not |
| 45 | automatically published during initialization. |
| 46 | |
| 47 | You can load them as additional context in your AI-assisted session by using |
| 48 | the `-instructions` flag to print them: |
| 49 | |
| 50 | ``` |
| 51 | gopls mcp -instructions > /path/to/contextFile.md |
| 52 | ``` |
| 53 | |
| 54 | ## Coding assistant setup |
| 55 | |
| 56 | To use the gopls MCP server with an LLM-based coding assistant, |
| 57 | you need to configure the assistant's MCP client to talk to `gopls`. |
| 58 | Below are guides for popular assistants. |
| 59 | |
| 60 | First ensure that gopls v0.20 or higher is installed and available in |
| 61 | your `$PATH`. Use this command to install the latest version: |
| 62 | |
| 63 | ```bash |
| 64 | $ go install golang.org/x/tools/gopls@latest |
| 65 | ``` |
| 66 | |
| 67 | ### Gemini CLI |
| 68 | |
| 69 | For Gemini CLI, add the following configuration to |
| 70 | `$HOME/.gemini/extensions/go/gemini-extension.json`: |
| 71 | |
| 72 | ```json |
| 73 | { |
| 74 | "name": "go", |
| 75 | "version": "0.0.1", |
| 76 | "mcpServers": { |
| 77 | "go": { |
| 78 | "command": "gopls", |
| 79 | "args": ["mcp"] |
| 80 | } |
| 81 | }, |
| 82 | "contextFileName": "GEMINI.md" |
| 83 | } |
| 84 | ``` |
| 85 | |
| 86 | Then run this command to emit the model instructions: |
| 87 | |
| 88 | ```bash |
| 89 | $ gopls mcp -instructions > ~/.gemini/extensions/go/GEMINI.md |
| 90 | ``` |
| 91 | |
| 92 | To verify the connection, use the `/mcp list` command in your Gemini |
| 93 | session to view available tools. Alternatively, run `gemini mcp list` |
| 94 | from your shell to inspect active MCP servers and connection status. |
| 95 | |
| 96 | ### Claude Code |
| 97 | |
| 98 | For Claude Code, run the following command to add `gopls` as an MCP server: |
| 99 | |
| 100 | ```bash |
| 101 | $ claude mcp add gopls -- gopls mcp |
| 102 | ``` |
| 103 | |
| 104 | To verify the connection and explore available tools: |
| 105 | |
| 106 | * Type `/mcp` inside your Claude session to view the list of tools offering. |
| 107 | * Run `claude mcp list` to inspect currently active MCP servers. |
| 108 | * Run `claude mcp get gopls` to inspect the configuration details of the server. |
| 109 | |
| 110 | ## Security considerations |
| 111 | |
| 112 | The gopls MCP server is a wrapper around the functionality ordinarily exposed |
| 113 | by gopls through the Language Server Protocol (LSP). As such, gopls' tools |
| 114 | may perform any of the operations gopls normally performs, including: |
| 115 | |
| 116 | - reading files from the file system, and returning their contents in tool |
| 117 | results (such as when providing context); |
| 118 | - executing the `go` command to load package information, which may result in |
| 119 | calls to https://proxy.golang.org to download Go modules, and writes to go |
| 120 | caches; |
| 121 | - writing to gopls' cache or persistent configuration files; and |
| 122 | - uploading weekly telemetry data **if you have opted in** to [Go telemetry](https://go.dev/doc/telemetry). |
| 123 | |
| 124 | The gopls MCP server does not perform any operations not already performed by |
| 125 | gopls in an ordinary IDE session. Like most LSP servers, gopls does not |
| 126 | generally write directly to your source tree, though it may instruct the client |
| 127 | to apply edits. Nor does it make arbitrary requests over the network, though it |
| 128 | may make narrowly scoped requests to certain services such as the Go module |
| 129 | mirror or the Go vulnerability database, which can't readily be exploited as a |
| 130 | vehicle for exfiltration by a confused agent. Nevertheless, these capabilities |
| 131 | may require additional consideration when used as part of an AI-enabled system. |