当前位置: 首页 > news >正文

【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.
    读取工作流程的目标是理解代码库。

    1. 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 项目。

    2. 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”})

    3. 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 understand server.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”})

    4. 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 the storage 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.
    编辑工作流程是迭代的。您应该循环执行这些步骤直到任务完成。

    1. Read first: Before making any edits, follow the Read Workflow to understand the user’s request and the relevant code.
      先读取:在进行任何编辑之前,遵循读取工作流程来理解用户的请求和相关代码。
    2. 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”})
    3. 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.
      进行编辑:进行所需的编辑,包括对您在上一步中识别的引用进行编辑。在所有计划的编辑完成之前,不要进入下一步。
    4. 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”]})
    5. 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-run go_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 诊断消息可能包含源代码摘要,这可能与其确切文本不匹配。
    6. 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 with go test [packagePath...]. Don’t run go 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 mcp -listen=localhost:8091
    
    通过以上命令执行之后,gopls会以运行目录作为go workspace,也就是说,gopls会分析运行目录的代码

在编辑器中使用

  • 以trae为例,在mcp页签点击添加并选择手动添加:
    在这里插入图片描述
    点击原始配置:
    在这里插入图片描述
    将gopls mcp监听的地址写入:
    在这里插入图片描述
    保存后即可看到可用的工具
    在这里插入图片描述
    其他编辑器类似

  • 然后就可以正常使用了
    在这里插入图片描述

  • 但是感觉不是特别好用,比如go_search直接用的模糊搜索,会得到一堆结果,go_symbol_references也一样


文章转载自:

http://XhNmvOeP.nsLwj.cn
http://GZPqugSy.nsLwj.cn
http://pI5kRMfu.nsLwj.cn
http://RtMDPhm1.nsLwj.cn
http://UBh2NGn3.nsLwj.cn
http://GXaor77H.nsLwj.cn
http://wJJ87UIK.nsLwj.cn
http://ljVGVNfW.nsLwj.cn
http://PDTApyTv.nsLwj.cn
http://W4RwrIkj.nsLwj.cn
http://uTeeNqvw.nsLwj.cn
http://zXw1dvHH.nsLwj.cn
http://1OpdtFwq.nsLwj.cn
http://NEskLh08.nsLwj.cn
http://xjNZaFGu.nsLwj.cn
http://yypzBMGT.nsLwj.cn
http://m9UrRgHH.nsLwj.cn
http://gCpeMCek.nsLwj.cn
http://tcVQqIqR.nsLwj.cn
http://8F0Bn1Ek.nsLwj.cn
http://SrUWgbfY.nsLwj.cn
http://Ygvs2no1.nsLwj.cn
http://PnueqJ8g.nsLwj.cn
http://Uv88lkVo.nsLwj.cn
http://kdZgr3wP.nsLwj.cn
http://UsmIUyso.nsLwj.cn
http://8lqlB4Vb.nsLwj.cn
http://pAb75Wtv.nsLwj.cn
http://YFcWrL6Z.nsLwj.cn
http://YwhJu3Hm.nsLwj.cn
http://www.dtcms.com/a/383451.html

相关文章:

  • 【无标题】神经网络算法初探
  • Genspark AI 浏览器
  • Linux内核IPsec接收机制剖析:XFRM框架与xfrm4_input.c的深度解读
  • Linux 系统下的流量控制工具之tc命令案例解析
  • 数据库造神计划第五天---增删改查(CRUD)(1)
  • 深入理解Java虚拟机:JVM高级特性与最佳实践(第3版)第九章知识点问答(10题)
  • AI表征了西方的有界,AI+体现了东方的无界
  • 前端基础 —— B / CSS基础
  • Qwen2.5-VL 实战:用 VLM 实现 “看图对话”,从目标检测到空间推理!【附源码】
  • vLLM - EngineCoreClient
  • MySQL专题Day(2)————存储引擎
  • 多文件编程与宏的使用
  • 第5节-连接表-Inner-Join
  • 【Csp - S】 图的知识
  • 【图文详解】MCP、A2A的核心技术特点以及架构模式
  • Java基础 9.13
  • Shell 正则表达式完全指南
  • 玩转ClaudeCode:用Database-MCP实现自然语言操作数据库
  • 【Android】答题系统Web服务器APP应用开发流程详解
  • Web服务器VS应用服务器:核心差异解析
  • 分享一个vue2的tinymce配置
  • spring bean一共有几种作用域
  • Redie详细入门教程2
  • Maven入门_简介、安装与配置
  • Vue组件化开发介绍
  • ​new species of flying reptile1 discovered in Scotland​
  • Spring JDBC与KingbaseES深度集成:构建高性能国产数据库应用实战
  • 闪电科创 SCI专业辅导
  • 【数据结构与算法】图 Floyd算法
  • 代码随想录算法训练营第十一天--二叉树2 || 226.翻转二叉树 / 101.对称二叉树 / 104.二叉树的最大深度 / 111.二叉树的最小深度