【go/gopls/mcp】官方gopls内置mcp server使用
文章目录
- 说在前面
- gopls安装
- gopls mcp说明
- The gopls MCP server
- Detecting a Go workspace
- Go programming workflows
- Read workflow
- Editing workflow
- 运行gopls mcp
- 在编辑器中使用
说在前面
- 操作系统:win11
- gopls版本:golang.org/x/tools/gopls v0.20.0
- go版本:go version go1.24.4 windows/amd64
gopls安装
- 官方文档
这里 - 安装最新版本(老版本没有该功能)
go install golang.org/x/tools/gopls@latest
gopls mcp说明
- 安装好之后,使用以下命令即可运行mcp服务
start the gopls MCP server in headless modeUsage:gopls [flags] mcp [mcp-flags]Starts the gopls MCP server in headless mode, without needing an LSP client. Starts the server over stdio or sse with http, depending on whether the listen flag is provided.Examples:$ gopls mcp -listen=localhost:3000$ gopls mcp //start over stdio-instructionsif set, print gopls' MCP instructions and exit-listen=stringthe address on which to run the mcp server-logfile=stringfilename to log to; if unset, logs to stderr-rpc.traceprint MCP rpc traces; cannot be used with -listen
- 查看mcp指令,即
gopls mcp -instructions
The gopls MCP server
These instructions describe how to efficiently work in the Go programming language using the gopls MCP server. You can load this file directly into a session where the gopls MCP server is connected.
本文档描述了如何使用 gopls MCP 服务器高效地进行 Go 编程语言开发。您可以在连接了 gopls MCP 服务器的会话中直接加载此文件。Detecting a Go workspace
At the start of every session, you MUST use the
go_workspace
tool to learn about the Go workspace. The rest of these instructions apply whenever that tool indicates that the user is in a Go workspace.
在每个会话开始时,您必须使用 go_workspace 工具来了解 Go 工作区。当该工具指示用户处于 Go 工作区时,以下说明将适用。Go programming workflows
These guidelines MUST be followed whenever working in a Go workspace. There are two workflows described below: the ‘Read Workflow’ must be followed when the user asks a question about a Go workspace. The ‘Edit Workflow’ must be followed when the user edits a Go workspace.
You may re-do parts of each workflow as necessary to recover from errors. However, you must not skip any steps.
在 Go 工作区中工作时,必须遵循这些准则。下面描述了两个工作流程:当用户询问有关 Go 工作区的问题时,必须遵循"读取工作流程"。当用户编辑 Go 工作区时,必须遵循"编辑工作流程"。
您可以根据需要重做每个工作流程的部分内容以从错误中恢复。但是,您不得跳过任何步骤。Read workflow
The goal of the read workflow is to understand the codebase.
读取工作流程的目标是理解代码库。-
Understand the workspace layout: Start by using
go_workspace
to understand the overall structure of the workspace, such as whether it’s a module, a workspace, or a GOPATH project.
理解工作区布局:首先使用 go_workspace 来理解工作区的整体结构,例如它是模块、工作区还是 GOPATH 项目。 -
Find relevant symbols: If you’re looking for a specific type, function, or variable, use
go_search
. This is a fuzzy search that will help you locate symbols even if you don’t know the exact name or location.
EXAMPLE: search for the ‘Server’ type:go_search({"query":"server"})
查找相关符号:如果您要查找特定的类型、函数或变量,请使用 go_search。这是一个模糊搜索,即使您不知道确切的名称或位置,也能帮助您定位符号。
示例:搜索 ‘Server’ 类型:go_search({“query”:“server”}) -
Understand a file and its intra-package dependencies: When you have a file path and want to understand its contents and how it connects to other files in the same package, use
go_file_context
. This tool will show you a summary of the declarations from other files in the same package that are used by the current file.go_file_context
MUST be used immediately after reading any Go file for the first time, and MAY be re-used if dependencies have changed.
EXAMPLE: to understandserver.go
’s dependencies on other files in its package:go_file_context({"file":"/path/to/server.go"})
理解文件及其包内依赖关系:当您有一个文件路径并想要理解其内容以及它如何连接到同一包中的其他文件时,使用 go_file_context。此工具将显示当前文件使用的同一包中其他文件的声明摘要。go_file_context 必须在首次读取任何 Go 文件后立即使用,如果依赖关系发生变化,可以重新使用。
示例:理解 server.go 对其包中其他文件的依赖关系:go_file_context({“file”:“/path/to/server.go”}) -
Understand a package’s public API: When you need to understand what a package provides to external code (i.e., its public API), use
go_package_api
. This is especially useful for understanding third-party dependencies or other packages in the same monorepo.
EXAMPLE: to see the API of thestorage
package:go_package_api({"packagePaths":["example.com/internal/storage"]})
理解包的公共 API:当您需要理解包向外部代码提供的内容(即其公共 API)时,使用 go_package_api。这对于理解第三方依赖项或同一代码仓库中的其他包特别有用。
示例:查看 storage 包的 API:go_package_api({“packagePaths”:[“example.com/internal/storage”]})
Editing workflow
The editing workflow is iterative. You should cycle through these steps until the task is complete.
编辑工作流程是迭代的。您应该循环执行这些步骤直到任务完成。- Read first: Before making any edits, follow the Read Workflow to understand the user’s request and the relevant code.
先读取:在进行任何编辑之前,遵循读取工作流程来理解用户的请求和相关代码。 - Find references: Before modifying the definition of any symbol, use the
go_symbol_references
tool to find all references to that identifier. This is critical for understanding the impact of your change. Read the files containing references to evaluate if any further edits are required.
EXAMPLE:go_symbol_references({"file":"/path/to/server.go","symbol":"Server.Run"})
查找引用:在修改任何符号的定义之前,使用 go_symbol_references 工具查找对该标识符的所有引用。这对于理解您的更改的影响至关重要。读取包含引用的文件,以评估是否需要进一步的编辑。
示例:go_symbol_references({“file”:“/path/to/server.go”,“symbol”:“Server.Run”}) - Make edits: Make the required edits, including edits to references you identified in the previous step. Don’t proceed to the next step until all planned edits are complete.
进行编辑:进行所需的编辑,包括对您在上一步中识别的引用进行编辑。在所有计划的编辑完成之前,不要进入下一步。 - Check for errors: After every code modification, you MUST call the
go_diagnostics
tool. Pass the paths of the files you have edited. This tool will report any build or analysis errors.
EXAMPLE:go_diagnostics({"files":["/path/to/server.go"]})
检查错误:在每次代码修改后,您必须调用 go_diagnostics 工具。传递您已编辑的文件路径。此工具将报告任何构建或分析错误。
示例:go_diagnostics({“files”:[“/path/to/server.go”]}) - Fix errors: If
go_diagnostics
reports any errors, fix them. The tool may provide suggested quick fixes in the form of diffs. You should review these diffs and apply them if they are correct. Once you’ve applied a fix, re-rungo_diagnostics
to confirm that the issue is resolved. It is OK to ignore ‘hint’ or ‘info’ diagnostics if they are not relevant to the current task. Note that Go diagnostic messages may contain a summary of the source code, which may not match its exact text.
修复错误:如果 go_diagnostics 报告任何错误,请修复它们。该工具可能以差异的形式提供建议的快速修复。您应该审查这些差异,如果它们是正确的,则应用它们。应用修复后,重新运行 go_diagnostics 以确认问题已解决。如果"提示"或"信息"诊断与当前任务无关,可以忽略它们。请注意,Go 诊断消息可能包含源代码摘要,这可能与其确切文本不匹配。 - Run tests: Once
go_diagnostics
reports no errors (and ONLY once there are no errors), run the tests for the packages you have changed. You can do this withgo test [packagePath...]
. Don’t rungo test ./...
unless the user explicitly requests it, as doing so may slow down the iteration loop.
运行测试:一旦 go_diagnostics 报告没有错误(并且仅在没有错误时),为您更改的包运行测试。您可以使用 go test [packagePath…] 来执行此操作。除非用户明确要求,否则不要运行 go test ./…,因为这样做可能会减慢迭代循环。
-
运行gopls mcp
- 有两种运行方式,一种使用stdio作为输入输出,另一种使用http作为输入输出,这里我们使用http方式
通过以上命令执行之后,gopls会以运行目录作为go workspace,也就是说,gopls会分析运行目录的代码gopls mcp -listen=localhost:8091
在编辑器中使用
-
以trae为例,在mcp页签点击添加并选择手动添加:
点击原始配置:
将gopls mcp监听的地址写入:
保存后即可看到可用的工具
其他编辑器类似 -
然后就可以正常使用了
-
但是感觉不是特别好用,比如go_search直接用的模糊搜索,会得到一堆结果,go_symbol_references也一样