/ 目錄 / 演練場 / mcp-go (SDK)
● 社群 mark3labs ⚡ 即開即用

mcp-go (SDK)

作者 mark3labs · mark3labs/mcp-go

用於建構 MCP 伺服器的 Go SDK——最少樣板程式碼、型別安全的工具定義,已被業界約半數的正式 Go MCP 使用。

mcp-go 是 Model Context Protocol 的事實標準 Go SDK。它處理傳輸層(stdio + SSE + streamable HTTP)、JSON-RPC 框架、工具/資源/提示的注冊,以及請求驗證,讓你專注於 MCP 伺服器邏輯。被 GitHub 的 mcp-server、dbhub、k8s-mcp 等眾多專案使用。

為什麼要用

核心特性

即時演示

實際使用效果

mcp-go-mark3labs.replay ▶ 就緒
0/0

安裝

選擇你的客戶端

~/Library/Application Support/Claude/claude_desktop_config.json  · Windows: %APPDATA%\Claude\claude_desktop_config.json
{
  "mcpServers": {
    "mcp-go-mark3labs": {
      "command": "go",
      "args": [
        "install",
        "github.com/mark3labs/mcp-go/cmd/example@latest"
      ],
      "_inferred": true
    }
  }
}

開啟 Claude Desktop → Settings → Developer → Edit Config。儲存後重啟應用。

~/.cursor/mcp.json · .cursor/mcp.json
{
  "mcpServers": {
    "mcp-go-mark3labs": {
      "command": "go",
      "args": [
        "install",
        "github.com/mark3labs/mcp-go/cmd/example@latest"
      ],
      "_inferred": true
    }
  }
}

Cursor 使用與 Claude Desktop 相同的 mcpServers 格式。專案級設定優先於全域。

VS Code → Cline → MCP Servers → Edit
{
  "mcpServers": {
    "mcp-go-mark3labs": {
      "command": "go",
      "args": [
        "install",
        "github.com/mark3labs/mcp-go/cmd/example@latest"
      ],
      "_inferred": true
    }
  }
}

點擊 Cline 側欄中的 MCP Servers 圖示,然後選 "Edit Configuration"。

~/.codeium/windsurf/mcp_config.json
{
  "mcpServers": {
    "mcp-go-mark3labs": {
      "command": "go",
      "args": [
        "install",
        "github.com/mark3labs/mcp-go/cmd/example@latest"
      ],
      "_inferred": true
    }
  }
}

格式與 Claude Desktop 相同。重啟 Windsurf 生效。

~/.continue/config.json
{
  "mcpServers": [
    {
      "name": "mcp-go-mark3labs",
      "command": "go",
      "args": [
        "install",
        "github.com/mark3labs/mcp-go/cmd/example@latest"
      ]
    }
  ]
}

Continue 使用伺服器物件陣列,而非映射。

~/.config/zed/settings.json
{
  "context_servers": {
    "mcp-go-mark3labs": {
      "command": {
        "path": "go",
        "args": [
          "install",
          "github.com/mark3labs/mcp-go/cmd/example@latest"
        ]
      }
    }
  }
}

加入 context_servers。Zed 儲存後熱重載。

claude mcp add mcp-go-mark3labs -- go install github.com/mark3labs/mcp-go/cmd/example@latest

一行命令搞定。用 claude mcp list 驗證,claude mcp remove 移除。

使用場景

實戰用法: mcp-go (SDK)

用不到 100 行程式碼為公司 API 建立內部 MCP 伺服器

👤 想要將內部工具暴露給 Claude 的公司 Go 開發者 ⏱ ~45 min intermediate

何時使用: 你有一個 Go 服務,想讓 Claude 呼叫它,無需撰寫 OpenAPI schema。

前置條件
  • Go 1.21+ — 透過 brew 或 asdf 安裝
步驟
  1. 建立伺服器框架
    Create a new Go project. Add mcp-go and define one tool: search_orders(customer_id) that calls our internal /v1/orders API.✓ 已複製
    → main.go 約 50 行,建置無誤
  2. 用 mcp-inspector 測試
    Run the server in stdio mode. Open mcp-inspector and verify the tool shows up.✓ 已複製
    → 工具可從 inspector 呼叫
  3. 在 Claude 中注冊
    Add the binary to claude_desktop_config.json. Test from Claude with a real customer ID.✓ 已複製
    → 來自 API 的即時回應

結果: 內部 Go API 透過型別安全的 MCP 工具暴露給 Claude。

注意事項
  • 長時間執行的呼叫阻塞 stdio — 對超過 5 秒的呼叫使用 SSE 或 streamable HTTP 傳輸
搭配使用: mcp-python-sdk

將現有 REST API 移植到 MCP,同時不破壞 REST 客戶端

👤 採用 MCP 但不想強制遷移的後端團隊 ⏱ ~60 min intermediate

何時使用: 你想讓 REST 和 MCP 在同一個處理器上共存。

步驟
  1. 提取處理器邏輯
    Take the existing /api/v1/search handler. Extract the core function so both gin and mcp-go can call it.✓ 已複製
    → 處理器分離——HTTP 處理器委派給純函式
  2. 封裝為 MCP 工具
    Register the pure func as an mcp-go tool. Map URL params to tool inputs.✓ 已複製
    → 相同邏輯,兩個介面
  3. 單一二進位,兩種傳輸
    Build one binary that runs gin on :8080 and the MCP server over stdio when invoked with --mcp.✓ 已複製
    → 多模式二進位

結果: REST 和 MCP 從一個 Go 二進位共用核心提供服務。

透過 SSE 在公共網路上架設 MCP 伺服器

👤 發布公共 MCP 的開發者(如 git-mcp.io) ⏱ ~90 min advanced

何時使用: 你想讓使用者無需在本地安裝任何東西即可新增你的 MCP。

前置條件
  • 域名和 TLS — 搭配 Let's Encrypt 的 Caddy/nginx
步驟
  1. 切換到 SSE 傳輸
    Convert the stdio server to SSE. Add CORS for the relevant origins.✓ 已複製
    → 伺服器接受 /sse 連線
  2. 新增每用戶驗證
    Validate Bearer token on each connection; reject unknown.✓ 已複製
    → 錯誤 token 回傳 401,有效 token 正常
  3. 部署並測試
    Deploy to fly.io. Add the URL to Claude via mcp-remote.✓ 已複製
    → 從指向遠端 URL 的 Claude 可呼叫工具

結果: 帶有驗證的公網 MCP 伺服器,已準備好給使用者使用。

注意事項
  • SSE 在負載均衡器後面斷開長連線 — 在負載均衡器上設定超過 5 分鐘的閒置超時

組合

與其他 MCP 搭配,撬動十倍槓桿

mcp-go-mark3labs + mcp-python-sdk

同一個專案,兩個 SDK 用於不同介面

mcp-go-mark3labs + mcp-registry

將你建構的 MCP 發布到官方注冊表

工具

此 MCP 暴露的能力

工具輸入參數何時呼叫成本
AddTool name, description, handler 伺服器啟動時為每個工具呼叫 0
AddResource uri, name, handler 暴露可讀取的資源 0
AddPrompt name, description, handler 暴露可重用的提示 0
ServeStdio () 本地 stdio 模式(最常見) 0
ServeSSE addr, opts 網路/遠端模式 0

成本與限制

運行它的成本

API 配額
不適用——函式庫
每次呼叫 Token 數
不適用
費用
免費(MIT)
提示
固定到特定次要版本;API 在 2025 年已穩定,但仍有細微變動

安全

權限、密鑰、影響範圍

憑證儲存: 取決於你的工具處理器需要什麼
資料出站: 由你的處理器控制

故障排查

常見錯誤與修復

工具對 Claude 不可見

檢查能力是否已協商;工具必須在 ServeStdio 呼叫前注冊

驗證: 使用 mcp-inspector 確認工具清單
stdio 訊息格式錯誤

不要在你的處理器中輸出到 stdout——那是 JSON-RPC 通道。用 stderr 記錄日誌。

SSE 在閒置時斷線

新增定期心跳;設定代理超時

替代方案

mcp-go (SDK) 對比其他方案

替代方案何時用它替代權衡
mcp-python-sdk (official)你用 Python 建構不同語言;兩者都是一流支援
TypeScript SDK (official)Node/Bun 生態系統適合JS 優先;效能空間不如 Go

更多

資源

📖 閱讀 GitHub 上的官方 README

🐙 查看未解決的 issue

🔍 瀏覽全部 400+ MCP 伺服器和 Skills