跳到主要內容

MCP Server 開發入門:讓 AI 連接你的工具 | Building MCP Servers: Connect AI to Your Tools

By Kit 小克 | AI Tool Observer | 2026-03-27

🇹🇼 MCP Server 開發入門:讓 AI 連接你的工具

MCP(Model Context Protocol)是 Anthropic 推出的開放協議,讓 AI 模型能直接連接外部工具和數據源。如果你想讓 Claude 或其他支援 MCP 的 AI 讀取你的資料庫、操作你的 API,開發一個 MCP Server 是最標準的做法。

MCP 是什麼?

簡單來說,MCP 就是 AI 的「USB 介面」。就像 USB 讓任何裝置都能連接電腦,MCP 讓任何工具都能連接 AI。它定義了一套標準的通訊協議,包含:

  • Tools:AI 可以呼叫的函式(例如查詢資料庫、發送訊息)
  • Resources:AI 可以讀取的資料(例如檔案、文件)
  • Prompts:預定義的提示模板

開發一個簡單的 MCP Server

用 Python SDK 開發最簡單。首先安裝:

pip install mcp

一個最基本的 MCP Server 結構:

from mcp.server import Server
from mcp.types import Tool, TextContent

server = Server("my-tool")

@server.list_tools()
async def list_tools():
    return [Tool(
        name="get_weather",
        description="Get weather info",
        inputSchema={
            "type": "object",
            "properties": {
                "city": {"type": "string"}
            },
            "required": ["city"]
        }
    )]

@server.call_tool()
async def call_tool(name, arguments):
    if name == "get_weather":
        city = arguments["city"]
        return [TextContent(type="text", text=f"Weather in {city}: Sunny, 25C")]

實用的 MCP Server 範例

  • 資料庫查詢:讓 AI 直接查詢 PostgreSQL 或 MySQL
  • 檔案系統:讓 AI 讀寫本地檔案(官方已提供)
  • GitHub 整合:讓 AI 操作 PR、Issue、Code Review
  • Slack/Discord:讓 AI 讀取和發送訊息
  • Google 日曆:讓 AI 管理你的行程

在 Claude Desktop 中使用

在 Claude Desktop 的設定檔(claude_desktop_config.json)中加入你的 MCP Server:

{
  "mcpServers": {
    "my-tool": {
      "command": "python",
      "args": ["/path/to/your/server.py"]
    }
  }
}

重啟 Claude Desktop 後,AI 就能使用你定義的工具了。

開發注意事項

  • 安全性:MCP Server 有完整的系統存取權限,務必做好權限控制
  • 錯誤處理:AI 可能傳入非預期的參數,做好防禦性程式設計
  • 效能:避免長時間阻塞的操作,設定合理的 timeout
  • 文件:工具描述寫清楚,AI 靠這個理解何時該用你的工具

好不好用,試了才知道。從一個簡單的工具開始,體驗 AI 直接操作你的系統的威力。


🇺🇸 Building MCP Servers: Connect AI to Your Tools

MCP (Model Context Protocol) is an open protocol by Anthropic that lets AI models directly connect to external tools and data sources. If you want Claude or other MCP-compatible AI to read your database or interact with your APIs, building an MCP Server is the standard approach.

What is MCP?

Simply put, MCP is like "USB for AI." Just as USB lets any device connect to a computer, MCP lets any tool connect to AI. It defines a standard communication protocol with:

  • Tools: Functions AI can call (e.g., query databases, send messages)
  • Resources: Data AI can read (e.g., files, documents)
  • Prompts: Predefined prompt templates

Building a Simple MCP Server

The Python SDK makes development straightforward. First, install it:

pip install mcp

A basic MCP Server structure:

from mcp.server import Server
from mcp.types import Tool, TextContent

server = Server("my-tool")

@server.list_tools()
async def list_tools():
    return [Tool(
        name="get_weather",
        description="Get weather information",
        inputSchema={
            "type": "object",
            "properties": {
                "city": {"type": "string"}
            },
            "required": ["city"]
        }
    )]

@server.call_tool()
async def call_tool(name, arguments):
    if name == "get_weather":
        city = arguments["city"]
        return [TextContent(type="text", text=f"Weather in {city}: Sunny, 25C")]

Practical MCP Server Examples

  • Database Queries: Let AI query PostgreSQL or MySQL directly
  • File System: Let AI read/write local files (official server available)
  • GitHub Integration: Let AI manage PRs, Issues, and Code Reviews
  • Slack/Discord: Let AI read and send messages
  • Google Calendar: Let AI manage your schedule

Using with Claude Desktop

Add your MCP Server to Claude Desktop's config file (claude_desktop_config.json):

{
  "mcpServers": {
    "my-tool": {
      "command": "python",
      "args": ["/path/to/your/server.py"]
    }
  }
}

After restarting Claude Desktop, the AI can use your defined tools.

Development Tips

  • Security: MCP Servers have full system access — implement proper permission controls
  • Error Handling: AI may send unexpected parameters — practice defensive programming
  • Performance: Avoid long-blocking operations and set reasonable timeouts
  • Documentation: Write clear tool descriptions — AI uses these to understand when to use your tools

You won't know until you try it. Start with a simple tool and experience the power of AI directly operating your systems.

Sources / 資料來源


AI 工具觀察站 — 每日精選 AI Agent 與工具趨勢
AI Tool Observer — Daily curated AI Agent & tool trends

留言

這個網誌中的熱門文章

MCP 突破 9700 萬次下載:AI Agent 的「USB-C」為何成為 2026 年最重要的標準? | MCP Hits 97 Million Downloads: Why Model Context Protocol Became the Most Important AI Standard of 2026

歡迎來到 AI 工具觀察站 | Welcome to AI Tool Observer

ARC-AGI-3 發布:頂尖 AI 全部得分不到 1% | ARC-AGI-3: Every Top AI Model Scored Under 1%