Bible Network Crypto DeFi Onchain RWA AI Agent Stablecoin Chain SAFU CryptoTax DeFAI AGI Claude Me Claude Skill Claude Design Claude Cowork
獨立知識媒體
與任何項目無關聯
探索AI智慧的思維邊界
claude-me.com
最新
Anthropic 親口說的八個原則:Claude 愈用愈笨,很可能是你的方式有問題  ·  Claude Code 現在可以自己「循環工作」了:Anthropic 公開四種 Loop 模式,讓 AI 真的幫你跑完整個流程  ·  Claude Cowork 真實評測:三個月後,哪些任務真的省了時間,哪些讓我後悔開了自動化  ·  七月開局的兩個大消息:Claude Sonnet 5 正式發布,Fable 5 出口管制解除全球重新開放  ·  RAG 是什麼:為什麼 Claude 讀不了你公司內網的資料,以及怎麼讓它讀到  ·  Claude Cowork 進階工作流:從「交代一件事」到「讓它跑完一整個流程」,三個真實可用的範本
mcp

開發者的 MCP 實作:從零開始建立你的第一個 MCP Server

30 秒速讀
建立一個 MCP Server 不需要你是資深工程師——30 行 Python 代碼就能讓 Claude 呼叫你的內部 API,用自然語言操作你的系統。

完整解析 +
01 · 為什麼發生?

自己建立 MCP Server 的情況,通常是你有內部工具或 API 沒有現成的 Server,或者你想把自己的服務暴露給 Claude。MCP Server 的核心結構是:宣告工具(告訴 Claude 能做什麼)、接收請求(Claude 呼叫你的工具時)、執行操作並回傳結果。使用 Anthropic 官方的 Python SDK,一個最小可用的 MCP Server 只需要約 30-50 行代碼。

02 · 運作原理是什麼?

MCP Server 的開發門檻比傳統 API 整合低,是因為 MCP 協議標準化了「AI 和工具之間的通訊格式」——你不需要為每個 AI 應用寫一套整合,只需要建立一個 MCP Server,所有支援 MCP 的 AI 應用都能使用。這個「寫一次,到處用」的特性,讓建立 MCP Server 的投資報酬率遠超過傳統 API 整合,也是為什麼 MCP 生態在 2025 年能快速擴張的根本原因。

03 · 如何應用

對開發者最直接的影響是:MCP 讓你能把 Claude 的能力整合進任何有 API 的系統,而且整合成本遠低於傳統方式。一個實際的場景:如果你的公司有內部知識庫、CRM 或工作管理系統,建立 MCP Server 之後,員工可以直接用自然語言查詢和操作這些系統,而不需要學習每個系統的操作介面。這個「用自然語言操作任何系統」的能力,是 AI 工具最能提升企業生產力的方式之一。

04 · 我該怎麼做?

開始建立你的第一個 MCP Server 的最快路徑:(1)先在 github.com/modelcontextprotocol/servers 找一個和你的需求最接近的現有 Server,用它的代碼作為起點;(2)安裝 pip install mcp httpx;(3)把 Server 的工具名稱和描述替換成你自己的;(4)把 call_tool 裡的邏輯替換成呼叫你的 API;(5)加入 Claude Desktop 設定,測試。從一個工具開始,確認基本流程跑通之後,再逐步增加更多工具。

完整內容 +

如果你已經理解了 MCP 的概念(如果還沒,先讀一下「MCP 是什麼」這篇),這篇文章帶你進入下一步:自己建立一個 MCP Server,把你的內部工具或自建系統接入 Claude。

什麼情況下值得自己建 MCP Server

官方和社群已經有超過 1,000 個現成的 MCP Server,覆蓋了大多數主流工具。你需要自己建 Server 的情況通常是:

  • 你有內部自建的工具或 API,沒有現成的 MCP Server
  • 現有的 MCP Server 不符合你的需求(例如你需要特定的認證方式、特定的資料格式)
  • 你想把你的服務暴露給 Claude,讓使用者能用自然語言操作你的系統

MCP Server 的基本架構

MCP Server 本質上是一個實作了 MCP 協議的程式,它做三件事:

  1. 宣告工具(Tools):告訴 Claude「我能做什麼」——例如「我能讀取資料庫」「我能發送 Email」
  2. 接收請求:Claude 呼叫你的工具時,Server 接收這個請求
  3. 執行操作並回傳結果:Server 實際執行操作(查資料庫、呼叫 API 等),把結果回傳給 Claude

用 Python 建立一個最小可用的 MCP Server

以下是一個能讓 Claude 查詢天氣的最小 MCP Server 範例(使用 Anthropic 官方的 Python SDK):

from mcp.server import Server
from mcp.server.stdio import stdio_server
from mcp import types
import httpx

app = Server("weather-server")

@app.list_tools()
async def list_tools() -> list[types.Tool]:
    return [
        types.Tool(
            name="get_weather",
            description="Get current weather for a city",
            inputSchema={
                "type": "object",
                "properties": {
                    "city": {
                        "type": "string",
                        "description": "The city name"
                    }
                },
                "required": ["city"]
            }
        )
    ]

@app.call_tool()
async def call_tool(
    name: str, arguments: dict
) -> list[types.TextContent]:
    if name == "get_weather":
        city = arguments["city"]
        # 呼叫天氣 API(示範用 Open-Meteo,免費不需要 API Key)
        async with httpx.AsyncClient() as client:
            # 先取得城市座標
            geo_response = await client.get(
                f"https://geocoding-api.open-meteo.com/v1/search?name={city}&count=1"
            )
            geo_data = geo_response.json()
            if not geo_data.get("results"):
                return [types.TextContent(
                    type="text",
                    text=f"找不到城市:{city}"
                )]
            
            lat = geo_data["results"][0]["latitude"]
            lon = geo_data["results"][0]["longitude"]
            
            # 取得天氣
            weather_response = await client.get(
                f"https://api.open-meteo.com/v1/forecast?"
                f"latitude={lat}&longitude={lon}"
                f"&current=temperature_2m,weathercode"
            )
            weather_data = weather_response.json()
            temp = weather_data["current"]["temperature_2m"]
            
            return [types.TextContent(
                type="text",
                text=f"{city} 目前氣溫:{temp}°C"
            )]
    
    return [types.TextContent(type="text", text="Unknown tool")]

if __name__ == "__main__":
    import asyncio
    asyncio.run(stdio_server(app))

安裝依賴:

pip install mcp httpx

把你的 Server 加入 Claude Desktop

建好 Server 之後,在 Claude Desktop 的設定檔(claude_desktop_config.json)裡加入:

{
  "mcpServers": {
    "weather": {
      "command": "python",
      "args": ["/path/to/your/weather_server.py"]
    }
  }
}

重啟 Claude Desktop,然後試著問:「台北現在幾度?」Claude 就會呼叫你的 weather server 來回答。

實際應用:連接你的內部 API

上面的範例示範了基本結構。在實際工作中,最常見的 MCP Server 使用場景是連接內部 API:

# 範例:連接你的內部 CRM API
@app.list_tools()
async def list_tools() -> list[types.Tool]:
    return [
        types.Tool(
            name="get_customer_info",
            description="Get customer information from our CRM by customer ID or email",
            inputSchema={
                "type": "object",
                "properties": {
                    "query": {
                        "type": "string",
                        "description": "Customer ID or email address"
                    }
                },
                "required": ["query"]
            }
        ),
        types.Tool(
            name="update_customer_note",
            description="Add a note to a customer record",
            inputSchema={
                "type": "object",
                "properties": {
                    "customer_id": {"type": "string"},
                    "note": {"type": "string"}
                },
                "required": ["customer_id", "note"]
            }
        )
    ]

有了這樣的 Server,業務人員就能直接在 Claude 對話裡說「查一下客戶 ABC 公司的資料,然後在他們的記錄裡加一個備注說我們預計下週跟進」——Claude 呼叫你的 CRM Server 完成整個流程。

幾個重要的設計原則

最小權限原則:你的 MCP Server 只暴露真正需要的操作。如果某個工具只需要讀取,不要給它寫入的能力。

清楚的工具描述description 欄位非常重要——Claude 根據這個描述決定什麼時候呼叫這個工具。描述越清楚,Claude 呼叫的準確率越高。

錯誤處理:MCP Server 應該對所有可能的錯誤(網路失敗、認證問題、資料不存在)都有明確的錯誤回應,而不是讓 Claude 收到一個空結果然後猜測發生了什麼。

考慮操作的可逆性:對於會改變狀態的操作(刪除、更新、發送),考慮在 Server 層加入確認機制或操作日誌,讓系統的行為可追溯。

圖解
MCP Server 架構:你的代碼如何成為 Claude 能呼叫的工具架構圖呈現 MCP Server 的三層結構:Claude(呼叫端)→ MCP Protocol(通訊標準)→ 你的 Server(工具宣告 + 執行邏輯)→ 外部 API / 資料庫(實際操作),說明每一層的職責和信息流向。MCP Server Architecture — How Your Code Becomes a Claude ToolClaudeMakes tool callsGets resultsMCP ProtocolStandard formatJSON-RPC messagesYour MCP Serverlist_tools()Declares what Claude can callname + description + schemacall_tool()Receives Claude's callExecutes logic, returns resultYour business logicCall internal APIsQuery databasesRun scriptsExternalYour APIDatabaseResultsJSON / TextBack to ClaudeKey insight: Your Server is the bridge between Claude's natural language and your systemsClaude says "get customer info" → MCP calls your tool → your code queries CRM → result back to Claude → Claude responds naturallyClaude Me · claude-me.com
歡迎截圖分享,轉載請註明來源
提問
請至少輸入 10 個字
相關文章
MCP 和直接 Claude API 有什麼不同?什麼時候用哪個
mcp · 06/17
自己寫一個 MCP Server:讓 Claude 安全連上你的內部工具(含權限與除錯)
mcp · 06/15
Notion MCP 實戰:設定完成後 Claude 能讀寫你的 Notion,用自然語言管理知識庫
mcp · 06/29
MCP 實戰:設定 Google Drive MCP 之後,Claude 可以直接讀你的文件(不需要複製貼上)
mcp · 06/26
相關新聞
更多相關主題