Fast API Auto MCP — native MCP server support via mcp_url#1
Open
swe-brain[bot] wants to merge 1 commit intomasterfrom
Open
Fast API Auto MCP — native MCP server support via mcp_url#1swe-brain[bot] wants to merge 1 commit intomasterfrom
swe-brain[bot] wants to merge 1 commit intomasterfrom
Conversation
Adds first-class MCP (Model Context Protocol) support to FastAPI. When mcp_url is set, an MCP server is automatically mounted that exposes all schema-included routes as callable MCP tools. - fastapi/mcp/generator.py: converts APIRoute objects to MCP Tool definitions - fastapi/mcp/server.py: stateless ASGI MCPApp using StreamableHTTP transport - fastapi/applications.py: mcp_url parameter added to FastAPI.__init__ + setup() - pyproject.toml: optional [mcp] dependency group added (mcp>=1.9) - tests/test_mcp.py: 7 integration tests covering all plan requirements
Author
|
Gentle reminder: this PR has been open for 6 days awaiting review. @sullivanj91 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds first-class MCP (Model Context Protocol) support to FastAPI. Enable with a single parameter:
app = FastAPI(mcp_url="/mcp"). FastAPI automatically generates an MCP server at that path exposing all schema-included routes as callable tools, executed in-process with zero network overhead.Trying It on an Existing FastAPI App
1. Install FastAPI from this fork + the MCP dependency
2. Add
mcp_urlto your app3. Run your app normally
4. Connect Claude Desktop
Add this to your
claude_desktop_config.json(usually at~/Library/Application Support/Claude/claude_desktop_config.json):{ "mcpServers": { "my-api": { "command": "npx", "args": ["mcp-remote", "http://localhost:8000/mcp/"] } } }Restart Claude Desktop. Your API routes will appear as tools Claude can call directly.
5. Quick smoke-test (no Claude Desktop needed)
Implementation Plan
Files Created
fastapi/mcp/__init__.py— public exports (MCPApp)fastapi/mcp/generator.py— convertsAPIRouteobjects to MCPTooldefinitions:include_in_schema=Trueroute.unique_id(e.g.read_item_items__item_id__get)summaryordescriptionor"METHOD /path"bodyfieldapp.openapi()schema — no re-computationfastapi/mcp/server.py— statelessMCPAppASGI app:StreamableHTTPServerTransportwithis_json_response_enabled=Trueanyiotask group (stateless mode)httpx.AsyncClient(transport=ASGITransport(app=fastapi_app))— in-process, zero network overhead, passes through all FastAPI middlewareFiles Modified
fastapi/applications.py— addedmcp_url: str | None = Noneparameter with fullannotated_docdocstring; stores asself.mcp_url; mountsMCPAppinsetup()when set; raises helpfulImportErrorifmcppackage not installedpyproject.toml— added[project.optional-dependencies] mcp = ["mcp>=1.9"]Tests
tests/test_mcp.py— 7 tests × 2 backends (asyncio + trio) = 14 total, all passing:test_mcp_disabled_by_default—FastAPI()withoutmcp_url→ 404 at/mcptest_mcp_custom_url—FastAPI(mcp_url="/api/mcp")mounts at custom pathtest_mcp_tools_list— tools/list returns all schema-included routestest_include_in_schema_false_excluded—include_in_schema=Falseroutes excludedtest_mcp_tool_call_get— GET endpoint callable via MCP tools/calltest_mcp_tool_call_post_with_body— POST with Pydantic body callable via MCPtest_mcp_path_params— path parameters correctly substitutedTest Plan
pytest tests/test_mcp.py -v)mcpnot installedImplemented by swe-brain · Planning issue: https://github.com/ArcInstitute/swe-brain/issues/7