|
2 | 2 | from workers import DurableObject |
3 | 3 | from logger import logger |
4 | 4 | import sys |
| 5 | +import httpx_patch # noqa: F401 |
5 | 6 | sys.path.insert(0, "/session/metadata/vendor") |
6 | 7 | sys.path.insert(0, "/session/metadata") |
7 | 8 |
|
8 | 9 |
|
9 | | -def setup_server(): |
| 10 | +def setup_server(env): |
| 11 | + from fastapi import FastAPI, Request |
| 12 | + from pydantic import BaseModel |
| 13 | + from fastapi_mcp import FastApiMCP |
10 | 14 | from exceptions import HTTPException, http_exception |
11 | | - from mcp.server.fastmcp import FastMCP |
12 | | - mcp = FastMCP("Demo") |
13 | | - |
14 | | - @mcp.tool() |
15 | | - def add(a: int, b: int) -> int: |
16 | | - """Add two numbers""" |
17 | | - return a + b |
18 | | - |
19 | | - @mcp.resource("greeting://{name}") |
20 | | - def get_greeting(name: str) -> str: |
21 | | - """Get a personalized greeting""" |
22 | | - return f"Hello, {name}!" |
23 | | - |
24 | | - @mcp.tool() |
25 | | - def calculate_bmi(weight_kg: float, height_m: float) -> float: |
26 | | - """Calculate BMI given weight in kg and height in meters""" |
27 | | - return weight_kg / (height_m**2) |
28 | | - |
29 | | - @mcp.prompt() |
30 | | - def echo_prompt(message: str) -> str: |
31 | | - """Create an echo prompt""" |
32 | | - return f"Please process this message: {message}" |
33 | | - |
34 | | - app = mcp.sse_app() |
| 15 | + |
| 16 | + app = FastAPI() |
35 | 17 | app.add_exception_handler(HTTPException, http_exception) |
| 18 | + |
| 19 | + mcp = FastApiMCP(app) |
| 20 | + |
| 21 | + # Mount the MCP server directly to your FastAPI app |
| 22 | + mcp.mount() |
| 23 | + # Auto-generated operation_id (something like "read_user_users__user_id__get") |
| 24 | + @app.get("/") |
| 25 | + async def root(): |
| 26 | + return {"message": "Hello, World!"} |
| 27 | + |
| 28 | + @app.get("/env") |
| 29 | + async def root(): |
| 30 | + return {"message": "Here is an example of getting an environment variable: " + env.MESSAGE} |
| 31 | + |
| 32 | + class Item(BaseModel): |
| 33 | + name: str |
| 34 | + description: str | None = None |
| 35 | + price: float |
| 36 | + tax: float | None = None |
| 37 | + |
| 38 | + @app.post("/items/") |
| 39 | + async def create_item(item: Item): |
| 40 | + return item |
| 41 | + |
| 42 | + @app.put("/items/{item_id}") |
| 43 | + async def create_item(item_id: int, item: Item, q: str | None = None): |
| 44 | + result = {"item_id": item_id, **item.dict()} |
| 45 | + if q: |
| 46 | + result.update({"q": q}) |
| 47 | + return result |
| 48 | + |
| 49 | + @app.get("/items/{item_id}") |
| 50 | + async def read_item(item_id: int): |
| 51 | + return {"item_id": item_id} |
| 52 | + |
| 53 | + mcp.setup_server() |
36 | 54 | return mcp, app |
37 | 55 |
|
38 | 56 |
|
39 | 57 | class FastMCPServer(DurableObject): |
40 | 58 | def __init__(self, ctx, env): |
41 | 59 | self.ctx = ctx |
42 | 60 | self.env = env |
43 | | - self.mcp, self.app = setup_server() |
| 61 | + self.mcp, self.app = setup_server(self.env) |
44 | 62 |
|
45 | 63 | async def call(self, request): |
46 | 64 | import asgi |
|
0 commit comments