-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
30 lines (22 loc) · 949 Bytes
/
main.py
File metadata and controls
30 lines (22 loc) · 949 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from fastapi import FastAPI, HTTPException
from fastapi_mcp import FastApiMCP
app = FastAPI(title="Weather MCP Service")
# 定义天气查询接口
@app.get("/weather/{city}")
async def get_weather(city: str):
if city not in ["Beijing", "Shanghai", "Guangzhou"]:
raise HTTPException(status_code=404, detail="City not supported")
return {"city": city, "temperature": 25, "condition": "Sunny"}
# This endpoint will not be registered as a tool, since it was added after the MCP instance was created
@app.get("/new/endpoint/", operation_id="new_endpoint", response_model=dict[str, str])
async def new_endpoint():
return {"message": "Hello, world!"}
# 先创建 MCP 实例
mcp = FastApiMCP(app)
# 在定义路由之前先挂载 MCP
mcp.mount()
# But if you re-run the setup, the new endpoints will now be exposed.
mcp.setup_server()
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)