MCP Go启动器
  ](https://go.dev/)  
Go中使用官方Go-sdk的功能完整的模型上下文协议(MCP)服务器模板。这个初学者用干净、地道的Go代码演示了MCP的所有主要功能。
📚 文档
✨ 特性
| 类别 | 功能 | 描述 |
|---|---|---|
| 工具 | hello | 带注释的基本工具 |
get_weather | 返回结构化数据的工具 | |
ask_llm | 调用LLM采样的工具 | |
long_task | 具有5秒进度更新的工具 | |
load_bonus_tool | 动态加载新工具 | |
| 资源 | info://about | 静态信息资源 |
file://example.md | 基于文件的标记资源 | |
| 模板 | greeting://{name} | 个性化问候 |
data://items/{id} | 按ID查找数据 | |
| 提示 | greet | 各种风格的问候 |
code_review | 重点领域代码审查 |
🚀 快速开始
先决条件
安装
# Clone the repository
git clone https://github.com/SamMorrowDrums/mcp-go-starter.git
cd mcp-go-starter
# Download dependencies
go mod download运行服务器
stdio传输 (地方发展):
go run ./cmd/stdio
# Or: make run-stdioHTTP传输 (用于远程/web部署):
go run ./cmd/http
# Or: make run-http
# Server runs on http://localhost:3000构建二进制文件
make build
# Creates bin/stdio and bin/http🔧 VS代码集成
该项目包括用于无缝开发的VS代码配置:
- 在VS Code中打开项目
- MCP配置位于
.vscode/mcp.json - 构建于
Ctrl+Shift+B(或Cmd+Shift+B在Mac上) - 使用VS Code的MCP工具测试服务器
使用DevContainers
- 安装 开发容器扩展
- 打开命令面板:“开发容器:在容器中重新打开”
- 一切都是预先配置好的,随时可以使用!
📁 项目结构
.
├── cmd/
│ ├── stdio/
│ │ └── main.go # stdio transport entrypoint
│ └── http/
│ └── main.go # HTTP transport entrypoint
├── internal/
│ └── server/
│ ├── server.go # Server orchestration
│ ├── tools.go # Tool definitions (hello, get_weather, etc.)
│ ├── resources.go # Resource and template definitions
│ └── prompts.go # Prompt definitions
├── .vscode/
│ ├── mcp.json # MCP server configuration
│ ├── tasks.json # Build/run tasks
│ └── extensions.json
├── .devcontainer/
│ └── devcontainer.json
├── .air.toml # Live reload configuration
├── .golangci.yml # Linter configuration
├── go.mod
├── Makefile
└── README.md🛠️ 发展
# Development with live reload (recommended)
make dev
# Requires air: go install github.com/air-verse/air@latest
# Run without live reload
make run-stdio
# Run tests
make test
# Format code
make fmt
# Lint code
make lint
# Install all dev tools
make install-tools
# Clean build artifacts
make clean实时重新加载
安装 空气 对于自动重建:
go install github.com/air-verse/air@latest
make dev更改任何 .go 文件将自动重建并重新启动服务器。
🔍 MCP检查员
这 MCP检查员 是测试和调试MCP服务器的重要开发工具。
运行检查器
npx @modelcontextprotocol/inspector -- go run ./cmd/stdio/main.go检查员提供什么
- 工具选项卡:列出并调用所有已注册的带有参数的工具
- 资源选项卡:浏览和阅读资源和模板
- 提示选项卡:查看和测试提示模板
- 日志选项卡:请参阅客户端和服务器之间的JSON-RPC消息
- 模式验证:验证工具输入/输出模式
调试提示
- 在连接IDE/客户端之前启动检查器
- 使用“日志”选项卡查看确切的请求/响应有效载荷
- 测试工具注释(ToolAnnotations)正确显示
- 验证是否显示进度通知
long_task - 检查取样是否有效
ask_llm
📖 功能示例
带注释的工具
mcp.AddTool(server, &mcp.Tool{
Name: "hello",
Title: "Say Hello",
Description: "A friendly greeting tool",
Annotations: &mcp.ToolAnnotations{
ReadOnlyHint: ptr(true),
},
}, helloHandler)
func helloHandler(ctx context.Context, req *mcp.CallToolRequest, input helloInput) (*mcp.CallToolResult, any, error) {
return &mcp.CallToolResult{
Content: []mcp.Content{
&mcp.TextContent{Text: fmt.Sprintf("Hello, %s!", input.Name)},
},
}, nil, nil
}资源模板
server.AddResourceTemplate(&mcp.ResourceTemplate{
Name: "Personalized Greeting",
URITemplate: "greeting://{name}",
MIMEType: "text/plain",
}, greetingHandler)带有进度更新的工具
func longTaskHandler(ctx context.Context, req *mcp.CallToolRequest, input longTaskInput) (*mcp.CallToolResult, any, error) {
progressToken := req.Params.GetProgressToken()
for i := 0; i < 5; i++ {
req.Session.NotifyProgress(ctx, &mcp.ProgressNotificationParams{
ProgressToken: progressToken,
Progress: float64(i) / 5.0,
Total: 1.0,
})
time.Sleep(time.Second)
}
return &mcp.CallToolResult{
Content: []mcp.Content{&mcp.TextContent{Text: "Done!"}},
}, nil, nil
}带取样的工具
result, err := req.Session.CreateMessage(ctx, &mcp.CreateMessageParams{
Messages: []*mcp.SamplingMessage{
{Role: "user", Content: &mcp.TextContent{Text: prompt}},
},
MaxTokens: 100,
})🔐 环境变量
复制 .env.example 到 .env 并配置:
cp .env.example .env| 变量 | 描述 | 默认值 |
|---|---|---|
PORT | HTTP服务器端口 | 3000 |
🤝 贡献
欢迎投稿!请确保您的更改与其他语言初学者保持功能对等。
📄 许可证
MIT许可证-请参阅 许可证 了解详情。
