这是一个使用mcp-go、Cobra 和 Viper 构建 Go MCP 服务器的起始仓库。
/vscode/mcp.json 运行服务器服务器通过包含的 mcp.json 文件配置为开箱即用。
./script/go-run
此脚本确保您始终从正确的目录运行最新代码,就像在github/github-mcp-server#51中所做的那样。
go build -o mcp-server ./cmd/mcp && ./mcp-server stdio
MCP_GREETING:用于 hello 工具和提示的问候语(默认值:Hello)MCP_SECRET:演示用途的秘密值(默认值:Hello)name 参数并返回问候语。pkg/example/resources/example.md 作为 MCP 资源。您可以从您的 MCP 客户端(如安装了 MCP 扩展的 VS Code)或编程方式调用 hello_world 工具。这里是一个使用工具并带有参数 name: "Sam" 的示例:
请求:
{
"method": "call_tool",
"params": {
"tool": "hello_world",
"arguments": {
"name": "Sam"
}
},
"id": 1,
"jsonrpc": "2.0"
}
响应:
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"type": "text",
"text": "Hello, Sam!"
}
}
请求:
{
"method": "call_tool",
"params": {
"tool": "choose_color",
"arguments": {
"color": "green"
}
},
"id": 2,
"jsonrpc": "2.0"
}
响应:
{
"jsonrpc": "2.0",
"id": 2,
"result": {
"type": "text",
"text": "You chose the color: green"
}
}
请求:
{
"method": "get_prompt",
"params": {
"prompt": "hello_prompt",
"arguments": {
"name": "Sam"
}
},
"id": 3,
"jsonrpc": "2.0"
}
响应:
{
"jsonrpc": "2.0",
"id": 3,
"result": {
"title": "A friendly greeting",
"messages": [
{
"role": "assistant",
"content": {
"type": "text",
"text": "Hello, Sam! How can I help you today?"
}
}
]
}
}
请求:
{
"method": "read_resource",
"params": {
"resource": "docs://example"
},
"id": 4,
"jsonrpc": "2.0"
}
响应:
{
"jsonrpc": "2.0",
"id": 4,
"result": [
{
"uri": "docs://example",
"mime_type": "text/markdown",
"text": "# Example Markdown Resource\n\nThis is an example markdown file served as an MCP resource.\n\n- You can edit this file to change the resource content.\n- It is embedded in the Go binary using Go's embed package.\n"
}
]
}
如果您希望 LLM 代理(如 Copilot 或其他兼容 MCP 的助手)调用您的工具和资源,可以使用如下提示:
您是一个具有以下工具访问权限的助手:
hello_world:使用配置的问候语按名字问候一个人。choose_color:让您从红色、绿色或蓝色中选择一种颜色。docs://example:包含示例内容的 Markdown 资源。当用户要求问候时,请使用他们的名字调用
hello_world工具。如果他们要求选择颜色,请使用他们想要的颜色调用choose_color工具。如果他们要求文档或帮助,请返回docs://example资源的内容。
这个提示会鼓励 LLM 按照预期使用您的工具和资源。
.vscode/mcp.json 预配置了环境变量输入以实现快速开发。command 字段中使用脚本的完整路径以获得最佳效果。提供了基本的 devcontainer,适用于启用了 Go 工具的 GitHub Codespaces。
更多详情,请参阅mcp-go README。
注意:只有某些客户端支持提示。这里是一个使用直接 JSON-RPC 通过 stdio 的示例:
echo '{"jsonrpc":"2.0","id":3,"params":{"name": "hello_prompt"},"method":"prompts/get"}' | go run cmd/mcp/main.go stdio
输出:
{
"jsonrpc": "2.0",
"id": 3,
"result": {
"description": "A friendly greeting",
"messages": [
{
"role": "assistant",
"content": {
"type": "text",
"text": "Hello, friend! How can I help you today?"
}
}
]
}
}