Transform any FastAPI application into a live Hyperia MCP server with a single call—unlocking LLM‑ready tools and resources while preserving your existing Python business logic.
Prerequisite Install FastAPI & an ASGI runtime: pip install fastapi uvicorn[standard].
1 Hello‑World Conversion
Copy from fastapi import FastAPI
from hyperia import Hyperia
api = FastAPI ()
@ api . get ( " /ping " )
def ping ():
return { " pong " : True }
mcp = Hyperia . from_fastapi ( app = api , name = " PingAPI " )
if __name__ == " __main__ " :
mcp . run ( transport = " streamable-http " , port = 9100 ) Now any Hyperia client (or Claude Desktop) can read_resource("resource://ping").
2 How the Converter Works
Inspects the FastAPI app’s router (app.openapi() behind the scenes).
Applies route‑mapping rules (see §3).
Builds wrappers that:
Extract path/query/body args from the MCP call.
Invoke the FastAPI dependency graph (including Depends, auth, middle‑ ware).
Serialise the Starlette Response → MCP contents.
Registers the new callable with the Hyperia server.
Because we bypass the network, calls execute in‑process with the same Python interpreter—zero HTTP overhead.
3 Default Route Mapping
FastAPI Decorator
Example
Hyperia Component
Name / URI result
any non‑GET (post/put/patch/delete)
createUser (from op id or path)
Customise with route_maps (same structure as OpenAPI integration):
all_routes_as_tools=True forces everything to be a Tool—nice for agent back‑ends.
4 Timeout & Concurrency Control
timeout – per‑request ceiling (seconds) for tool/resource execution.
max_concurrency – semaphore protecting the underlying FastAPI app when hit via MCP (does not affect external HTTP calls to the same FastAPI app).
5 Advanced Examples
5.1 CRUD Service with Pydantic Models
Generated components:
Tool new (calls POST /items)
Resource items (GET /items)
Template items://{idx} (GET /items/{idx})
5.2 Streaming Responses
FastAPI StreamingResponse → Hyperia chunked TextContent. Supports SSE or incremental JSON.
5.3 Auth Propagation
If your FastAPI endpoints use Depends(get_current_user), they still work—Hyperia just runs the dependency call chain.
For agent scenarios supply a dummy user via DependencyOverrides before wrapping:
6 Why Use FastAPI→Hyperia Instead of OpenAPI→Hyperia?
Criteria
FastAPI wrapper
OpenAPI wrapper
Re‑use FastAPI dependencies & state
You only have OpenAPI description
You can even combine : expose some endpoints through HTTP (OpenAPI) and mount a FastAPI‑wrapped MCP for internal operations.
7 Limitations & Caveats
FastAPI middlewares that modify the raw ASGI send/receive flow (e.g. WebSocket) are ignored for MCP calls.
Background tasks run inside the MCP event‑loop—monitor memory.
Proprietary Response classes (FileResponse, RedirectResponse) map to simple TextContent with their .body string or URL.
Multipart form file uploads require the client to supply base64 blobs; binary passthrough coming in v2.6.
8 Troubleshooting
RuntimeError: FastAPI not installed
422 errors when tool called
Validation failed on pydantic model
check arg names & types; they must match FastAPI signature
Endpoint returned Response(status_code=204)
Upgrade to v2.5 (maps 204 → empty list)
Last updated 9 months ago