iT邦幫忙

2025 iThome 鐵人賽

DAY 26
0

  在昨天我們了解了 MCP(Model Context Protocol, 模型上下文協定)如何成為 AI 世界的「USB-C 標準」,讓模型能夠以安全、標準化的方式存取外部資料與工具。了解了協定運作原理後,我們需要一個好用的框架來快速實作,今天要介紹的就是 FastMCP,FastMCP 可以讓你三分鐘就能打造屬於自己的 LLM 外掛伺服器!

為什麼要用 FastMCP?

  在沒有 FastMCP 之前,如果想讓 LLM 存取外部資料(例如資料庫、雲端 API、Notion、或 IoT 裝置),會需要自行撰寫 HTTP API、設計金鑰驗證、撰寫序列化與回傳格式、這樣的開發過程繁瑣又容易出錯,FastMCP 讓一切變的簡單許多,他直接封裝了 MCP 協定層與通訊細節,讓開發者專注於寫出「功能」本身就好,是目前最輕量、最符合 MCP 標準的框架之一。

環境安裝與準備

pip install fastmcp

建立一個最簡單的 MCP Server

from fastmcp import FastMCP

# 建立 MCP 伺服器
mcp = FastMCP("PromptMCP")

# 工具:簡單文字摘要
@mcp.tool()
def summarize_text(text: str) -> str:
    """將輸入文字壓縮成摘要"""
    return text[:50] + "..." if len(text) > 50 else text

if __name__ == "__main__":
    mcp.run()

執行看看

python server.py

輸出

看到下面這個就代表 MCP Server 被成功建立囉!

╭────────────────────────────────────────────────────────────────────────────╮
│                                                                            │
│        _ __ ___  _____           __  __  _____________    ____    ____     │
│       _ __ ___ .'____/___ ______/ /_/  |/  / ____/ __ \  |___ \  / __ \    │
│      _ __ ___ / /_  / __ `/ ___/ __/ /|_/ / /   / /_/ /  ___/ / / / / /    │
│     _ __ ___ / __/ / /_/ (__  ) /_/ /  / / /___/ ____/  /  __/_/ /_/ /     │
│    _ __ ___ /_/    \____/____/\__/_/  /_/\____/_/      /_____(*)____/      │
│                                                                            │
│                                                                            │
│                                FastMCP  2.0                                │
│                                                                            │
│                                                                            │
│                 🖥️  Server name:     PromptMCP                              │
│                 📦 Transport:       STDIO                                  │
│                                                                            │
│                 🏎️  FastMCP version: 2.12.4                                 │
│                 🤝 MCP SDK version: 1.17.0                                 │
│                                                                            │
│                 📚 Docs:            https://gofastmcp.com                  │
│                 🚀 Deploy:          https://fastmcp.cloud                  │
│                                                                            │
╰────────────────────────────────────────────────────────────────────────────╯


[10/10/25 21:54:06] INFO     Starting MCP server 'PromptMCP' with transport 'stdio'

在 Claude Desktop 中測試

  1. 開啟 Claude Desktop
  2. 點擊左下角的大頭,點選「Settings」->「Developer」->「Edit Config」
  3. 編輯 Config 如下,"command"可輸入"python"或是指定的虛擬環境路徑;"args"輸入剛剛建立的 MCPServer 檔案路徑
{
    "mcpServers": {
        "fastmcp-demo": {
        "command": "C:\\Users\\ruiyang\\venv\\Scripts\\python.exe",
        "args": [
            "C:\\Users\\ruiyang\\server.py"
        ]
        }
    }
}
  1. 編輯完成後,重新開啟 Claude 就會自動開啟和偵測 MCP server。

  2. 測試:Tool summarize_text
    在 Claude 對話視窗輸入:

呼叫 PromptMCP 的 summarize_text 工具,輸入文字:
"FastMCP 是一個讓大型語言模型能安全訪問外部工具和資料的開源框架,可以用來快速實作 MCP 功能。"

Claude 回覆:

"FastMCP 是一個讓大型語言模型能安全訪問外部工具和資料的開源框架,可以用來快速實作 MCP 功..."

上一篇
[Day25] 模型上下文協定(MCP)
下一篇
[Day27] 利用 n8n 來打造你的本地 AI Agent
系列文
從上下文工程到 Agent:30 天生成式 AI 與 LLM 學習紀錄29
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言