自己建立 MCP Server 的情況,通常是你有內部工具或 API 沒有現成的 Server,或者你想把自己的服務暴露給 Claude。MCP Server 的核心結構是:宣告工具(告訴 Claude 能做什麼)、接收請求(Claude 呼叫你的工具時)、執行操作並回傳結果。使用 Anthropic 官方的 Python SDK,一個最小可用的 MCP Server 只需要約 30-50 行代碼。
MCP Server 的開發門檻比傳統 API 整合低,是因為 MCP 協議標準化了「AI 和工具之間的通訊格式」——你不需要為每個 AI 應用寫一套整合,只需要建立一個 MCP Server,所有支援 MCP 的 AI 應用都能使用。這個「寫一次,到處用」的特性,讓建立 MCP Server 的投資報酬率遠超過傳統 API 整合,也是為什麼 MCP 生態在 2025 年能快速擴張的根本原因。
對開發者最直接的影響是:MCP 讓你能把 Claude 的能力整合進任何有 API 的系統,而且整合成本遠低於傳統方式。一個實際的場景:如果你的公司有內部知識庫、CRM 或工作管理系統,建立 MCP Server 之後,員工可以直接用自然語言查詢和操作這些系統,而不需要學習每個系統的操作介面。這個「用自然語言操作任何系統」的能力,是 AI 工具最能提升企業生產力的方式之一。
開始建立你的第一個 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。
官方和社群已經有超過 1,000 個現成的 MCP Server,覆蓋了大多數主流工具。你需要自己建 Server 的情況通常是:
MCP Server 本質上是一個實作了 MCP 協議的程式,它做三件事:
以下是一個能讓 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"¤t=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 的設定檔(claude_desktop_config.json)裡加入:
{
"mcpServers": {
"weather": {
"command": "python",
"args": ["/path/to/your/weather_server.py"]
}
}
}
重啟 Claude Desktop,然後試著問:「台北現在幾度?」Claude 就會呼叫你的 weather server 來回答。
上面的範例示範了基本結構。在實際工作中,最常見的 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 層加入確認機制或操作日誌,讓系統的行為可追溯。