By default, FlashMCP will map FastAPI routes to MCP components according to the following rules:
FastAPI Route Type
FastAPI Example
MCP Component
Notes
GET without path params
@app.get("/stats")
Resource
Simple resources for fetching data
GET with path params
@app.get("/users/{id}")
Resource Template
Path parameters become template parameters
POST, PUT, DELETE, etc.
@app.post("/users")
Tool
Operations that modify data
For more details on route mapping or custom mapping rules, see the OpenAPI integration documentation; FlashMCP uses the same mapping rules for both FastAPI and OpenAPI integrations.
import asynciofrom fastapi import FastAPI, HTTPExceptionfrom pydantic import BaseModelfrom FlashMCP import FlashMCP, Client# Define your Pydantic modelclass Item(BaseModel): name: str price: float# Create your FastAPI appapp = FastAPI()items = {} # In-memory database@app.get("/items")def list_items(): """List all items""" return list(items.values())@app.get("/items/{item_id}")def get_item(item_id: int): """Get item by ID""" if item_id not in items: raise HTTPException(404, "Item not found") return items[item_id]@app.post("/items")def create_item(item: Item): """Create a new item""" item_id = len(items) + 1 items[item_id] = {"id": item_id, **item.model_dump()} return items[item_id]# Test your MCP server with a clientasync def check_mcp(mcp: FlashMCP): # List the components that were created tools = await mcp.get_tools() resources = await mcp.get_resources() templates = await mcp.get_resource_templates() print( f"{len(tools)} Tool(s): {', '.join([t.name for t in tools.values()])}" ) print( f"{len(resources)} Resource(s): {', '.join([r.name for r in resources.values()])}" ) print( f"{len(templates)} Resource Template(s): {', '.join([t.name for t in templates.values()])}" ) return mcpif __name__ == "__main__": # Create MCP server from FastAPI app mcp = FlashMCP.from_fastapi(app=app) asyncio.run(check_mcp(mcp)) # In a real scenario, you would run the server: mcp.run()