返回市场
麦普戈启动器

麦普戈启动器

作者:SamMorrowDrums6 星标更新:2025-06-17

项目介绍

mcp-go-starter

这是一个使用mcp-go、Cobra 和 Viper 构建 Go MCP 服务器的起始仓库。

快速开始

  1. /vscode/mcp.json 运行服务器

服务器通过包含的 mcp.json 文件配置为开箱即用。

  1. 使用脚本运行服务器(开发模式):
./script/go-run

此脚本确保您始终从正确的目录运行最新代码,就像在github/github-mcp-server#51中所做的那样。

  1. 或者手动构建并运行服务器:
go build -o mcp-server ./cmd/mcp && ./mcp-server stdio

环境变量

  • MCP_GREETING:用于 hello 工具和提示的问候语(默认值:Hello
  • MCP_SECRET:演示用途的秘密值(默认值:Hello

功能

  • Hello World 工具:接受一个 name 参数并返回问候语。
  • Markdown 资源:提供 pkg/example/resources/example.md 作为 MCP 资源。
  • 提示示例:简单的提示,按名字问候用户。

示例:调用 hello_world 工具

您可以从您的 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!"
  }
}

示例:调用 choose_color 工具(枚举)

请求:

{
  "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?"
        }
      }
    ]
  }
}

示例:获取 Markdown 资源

请求:

{
  "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 代理提示

如果您希望 LLM 代理(如 Copilot 或其他兼容 MCP 的助手)调用您的工具和资源,可以使用如下提示:

您是一个具有以下工具访问权限的助手:

  • hello_world:使用配置的问候语按名字问候一个人。
  • choose_color:让您从红色、绿色或蓝色中选择一种颜色。
  • docs://example:包含示例内容的 Markdown 资源。

当用户要求问候时,请使用他们的名字调用 hello_world 工具。如果他们要求选择颜色,请使用他们想要的颜色调用 choose_color 工具。如果他们要求文档或帮助,请返回 docs://example 资源的内容。

这个提示会鼓励 LLM 按照预期使用您的工具和资源。

VS Code 集成

  • .vscode/mcp.json 预配置了环境变量输入以实现快速开发。
  • 确保在 command 字段中使用脚本的完整路径以获得最佳效果。

Codespaces/Devcontainer

提供了基本的 devcontainer,适用于启用了 Go 工具的 GitHub Codespaces。


更多详情,请参阅mcp-go README

其他示例

直接通过 stdio 访问提示的原始 JSON-RPC

注意:只有某些客户端支持提示。这里是一个使用直接 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?"
        }
      }
    ]
  }
}