
FastMCP - FastAPI MCP Framework & Runner
io.github.Manas-makerv0.1.0更新於 Oct 3, 2026
FastAPI-native Model Context Protocol framework and stdio runner.
概覽
一套 Python 框架,把帶標籤的 FastAPI 路由與自訂函式轉成 MCP 工具,並提供連接桌面 AI 用戶端的本機 stdio 執行器。
- 功能
- FastMCP 是 FastAPI 原生的框架,用來把現有 FastAPI 應用程式暴露成 MCP 伺服器。帶有 tags=["mcp"] 標籤的路由會被反射成 MCP 工具,Pydantic 模型、路徑/查詢/請求體參數與 docstring 會轉換成輸入 schema 與描述;未加標籤的路由則維持私有。它也支援以 @mcp.tool 裝飾器定義自訂工具、透過 search_tools 中繼工具進行漸進式工具探索、回傳 isError 結果的錯誤攔截、自訂序列化器,以及位於 /mcp/docs 的內嵌瀏覽器檢查器。stdio 命令列執行器可透過標準輸入輸出,把 Claude Desktop、Cursor 等桌面用戶端連到該應用程式。
- 適用情境
- 當你已有 FastAPI 後端,想讓 AI 助理直接呼叫其介面而不必另外寫一個 MCP 伺服器時使用。也適合需要暴露大量工具但不想佔滿模型上下文,或想透過本機 stdio 橋接桌面 AI 用戶端的團隊。
- 執行需求
- 需要 Python 並安裝 mcp-fastapi 套件(pip 或 uv)。以本機 stdio 程序執行,或掛載到由 uvicorn 提供的 ASGI 應用程式中。選用環境變數 FASTMCP_LOG_LEVEL 控制日誌等級。伺服器本身未宣告帳號或 API 金鑰;認證取決於底層 FastAPI 應用程式的實作。
安裝
在 SourceWeft 中
- 開啟 儀表板中的 FastMCP - FastAPI MCP Framework & Runner,將其新增到工作區。
- 為需要使用其工具的對話啟用該服務。
Desktop only,透過 STDIO。 STDIO 服務會啟動本機處理程序,因此需要 SourceWeft 桌面主機。
其他 MCP 客戶端
參照 儲存庫 中的啟動說明。
README
fast-mcp
FastAPI-native Model Context Protocol (MCP) framework with automatic route reflection, ASGI scope bridging, dynamic progressive tool discovery, resilient error recovery, and interactive in-chat MCP Apps (SEP-1865).
[PyPI] [Tests] [Documentation] [Python] [FastAPI] [Glama]
Highlights
- ⚡ Hybrid Dual-Citizen ASGI Mount: Mounts directly onto any existing
FastAPIinstance in-process over standard ASGI—zero external proxying, zero hanging subprocesses. - 🔌 Local stdio CLI Runner & Desktop AI Bridge: Run
fast-mcp stdio main:apporpython -m fast_mcp stdio main:appto connect desktop AI clients (Claude Desktop, Cursor) over standard I/O pipes with zero network setup. - 🔍 Route Reflection: Opt-in tags (
tags=["mcp"]) automatically convert FastAPI endpoints, Pydantic models, docstrings, path/query/body parameters into MCP tools. - 🛠️ Custom AI Tools (
@mcp.tool): Define AI-tailored composite tools alongside reflected routes with automatic schema and docstring extraction. - 🔐 ASGI Scope Bridging: Client authorization headers (
Authorization: Bearer <token>, cookies, API keys) captured during the MCP handshake are bridged into an in-memory ASGIRequest, natively resolving FastAPI'sDepends()andSecurity()providers without code changes. - 🧠 Dynamic Progressive Tool Discovery: Protect agent context windows via progressive discovery (
dynamic_discovery=True), thesearch_tools(query: str)meta-tool, and zero-dependencyKeywordTagRouter. - 🛡️ Resilient Error Recovery & Minified JSON: Traps route
HTTPExceptionand Pydantic validation errors into informativeCallToolResult(isError=True)responses so LLMs can self-correct without protocol failures. Output defaults to compact, token-conscious minified JSON with custom@mcp.serializerformatting hooks. - 🖥️ Dual UI & In-Chat MCP Apps (SEP-1865): Embedded browser inspector at
/mcp/docsplus native support for in-chat interactive iframes in desktop AI clients (Claude Desktop, Cursor, VS Code) via_meta.ui.resourceUri, the built-ininspect()tool, and the@mcp.app()decorator.
Installation
Or using uv:
Quickstart
Run with standard ASGI servers:
Core Capabilities
1. Route Reflection
Routes tagged with tags=["mcp"] (configurable via route_tag) are automatically inspected upon mcp.mount():
- Endpoint docstrings (Google, Sphinx, NumPy format) become tool descriptions and parameter docs.
- Pydantic request models, query parameters, and path variables become MCP input schemas.
- Untagged endpoints remain standard HTTP routes and are never leaked to LLMs.
2. Custom AI Tools (@mcp.tool)
Register AI-specialized tools that don't need dedicated REST endpoints:
3. ASGI Scope Bridging & Native Auth
Incoming headers (Authorization: Bearer ..., cookies, API keys) from the MCP client's SSE handshake or message posts are captured into an active request context:
If authorization fails or headers are omitted, fast-mcp unwraps the resulting HTTPException(401) into CallToolResult(is_error=True) so the agent receives an actionable authentication error rather than crashing the transport.
4. Dynamic Progressive Tool Discovery
Prevent LLM context window bloat on large FastAPI applications with hundreds of endpoints:
- When active,
tools/listexposes only baseline tools plus thesearch_tools(query: str)meta-tool. - Calling
search_tools(query="invoice")executes the pluggableToolRouter(defaults to zero-dependencyKeywordTagRouterwith tokenized name/tag/description ranking) and returns matching tool definitions with full JSON schemas.
5. Resilient Error Interception & Custom Serializers
- Exception Traps:
HTTPException(400, 404, 422) and Pydantic validation errors return clean, concise messages withisError=True. - Minified Output: Responses serialize to compact minified JSON (
{"id":1,"name":"widget"}) saving prompt tokens. - Custom Serializers: Format return types into tailored markdown or summaries:
6. Dual UI: Browser Inspector & In-Chat MCP Apps (SEP-1865)
Embedded Browser Inspector
Open http://localhost:8000/mcp/docs in any browser to inspect registered tools, view schemas, and execute test invocations interactively without external Node.js CLIs. (Disable with FastMCP(app, enable_ui=False)).
In-Chat MCP Apps (SEP-1865)
Render rich interactive HTML/JS widgets directly in modern desktop AI clients (Claude Desktop, Cursor, VS Code):
7. Local stdio CLI Runner & Desktop AI Bridge
Connect desktop AI clients (Claude Desktop, Cursor) directly to your FastAPI backend or FastMCP instance over standard input/output (stdio) pipes with zero network setup, port conflicts, or external proxying.
Command-Line Usage
Claude Desktop Configuration (claude_desktop_config.json)
Configure Claude Desktop to launch your FastMCP server directly:
Or using uv to manage the virtual environment automatically:
Programmatic Stdio Runner
You can also run stdio mode programmatically from Python:
Testing & Verification
fast-mcp exercises external behavior across the ASGI Protocol Seam using httpx.AsyncClient with ASGITransport:
Specification & Architectural Documents
- Interactive Documentation Website
- AI Agent Index (llms.txt)
- Specification: fast-mcp Core Framework (V1)
- GLOSSARY.md
- ADR 0001: Architecture Foundation and Hybrid Scope
- ADR 0002: Dual-UI, ASGI Scope Bridging, and Resilient Error Handling
License
MIT
來源:README.md,提交 2fb6e40
工具
0版本歷史
1- v0.1.0最新Oct 3, 2026


