m

mcp-gopls 工具(模型上下文协议 Go 语言服务器)

@hloiseaufcms/mcp-gopls
0 Stars 34 次浏览 hloiseaufcms 更新于 2026-08-23

一种模型上下文协议(MCP)服务器,允许像 Claude 这样的 AI 助手与 Go 的语言服务器协议(LSP)交互,并从高级的 Go 代码分析功能中受益。

MCP 服务配置

复制以下 JSON 到 OPClaw 或其他 MCP 客户端的配置文件中即可使用

{
  "mcpServers": {
    "mcp-gopls": {
      "command": "mcp-gopls"
    }
  }
}

服务介绍

MCP LSP Go

一个模型上下文协议 (MCP) 服务器,允许像 Claude 这样的 AI 助手与 Go 的语言服务器协议 (LSP) 交互,并从中受益于高级的 Go 代码分析功能。

概述

此 MCP 服务器帮助 AI 助手:

  • 使用 LSP 分析 Go 代码
  • 导航到定义并查找引用
  • 检查代码诊断
  • 获取符号的悬停信息
  • 获取补全建议

架构

该项目使用 mark3labs/mcp-go 库来实现模型上下文协议。MCP 集成使得 AI 助手与 Go 工具之间的通信变得无缝。

服务器通过语言服务器协议 (LSP) 与 Go 的官方语言服务器 gopls 进行通信。

特性

  • LSP 集成:连接到 Go 的语言服务器协议以进行代码分析
  • 代码导航:在代码中查找定义和引用
  • 代码质量:获取诊断和错误
  • 高级信息:悬停信息和补全建议

项目结构

.
├── cmd
│   └── mcp-gopls        # Application entry point
├── pkg
│   ├── lsp             # LSP client to communicate with gopls
│   │   ├── client      # LSP client implementation
│   │   └── protocol    # LSP protocol types and features
│   ├── server          # MCP server
│   └── tools           # MCP tools exposing LSP features

安装

go install github.com/hloiseaufcms/mcp-gopls/cmd/mcp-gopls@latest

添加到光标

{
  "mcpServers": {
    "mcp-gopls": {
      "command": "mcp-gopls"
    }
  }
} 

MCP 工具

MCP 服务器提供了以下工具:

工具 描述
go_to_definition 导航到符号的定义
find_references 查找符号的所有引用
check_diagnostics 获取文件的诊断信息
get_hover_info 获取关于符号的详细信息
get_completion 获取指定位置的补全建议
analyze_coverage 分析 Go 代码的测试覆盖率

使用示例

使用支持 MCP 的 AI 助手配合该服务器:

# Ask the AI to get information about the code
Can you find the definition of the `ServeStdio` function in this project?

# Ask for diagnostics
Are there any errors in my main.go file?

# Ask for information about a symbol
What does the Context.WithTimeout function do in Go?

开发

git clone https://github.com/hloiseaufcms/mcp-gopls.git
cd mcp-gopls
go mod tidy
go build -o mcp-gopls cmd/mcp-gopls/main.go
./mcp-gopls

前提条件

  • Go 1.24 或更高版本
  • 安装 gopls (go install golang.org/x/tools/gopls@latest)

许可证

Apache License 2.0