Skip to content

Commit 0afccc9

Browse files
committed
feat: mcp
1 parent 676edeb commit 0afccc9

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

backend/apps/datasource/api/datasource.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
path = "/opt/sqlbot/data/excel"
1919

2020

21-
@router.get("/list")
21+
@router.get("/list", operation_id="get_datasource_list")
2222
async def datasource_list(session: SessionDep):
2323
return get_datasource_list(session=session)
2424

backend/common/utils/whitelist.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
"*.woff2",
2121
"*.ttf",
2222
"*.eot",
23-
"*.otf"
23+
"*.otf",
24+
"/mcp",
25+
"/mcp/message"
2426
]
2527

2628
class WhitelistChecker:

backend/main.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,20 @@
1414
from common.core.response_middleware import ResponseMiddleware, exception_handler
1515
from alembic.config import Config
1616
from alembic import command
17+
from fastapi_mcp import FastApiMCP
18+
1719

1820
def run_migrations():
1921
alembic_cfg = Config("alembic.ini")
2022
command.upgrade(alembic_cfg, "head")
23+
24+
2125
@asynccontextmanager
2226
async def lifespan(app: FastAPI):
2327
run_migrations()
2428
yield
2529

30+
2631
def custom_generate_unique_id(route: APIRoute) -> str:
2732
tag = route.tags[0] if route.tags and len(route.tags) > 0 else ""
2833
return f"{tag}-{route.name}"
@@ -38,6 +43,17 @@ def custom_generate_unique_id(route: APIRoute) -> str:
3843
lifespan=lifespan
3944
)
4045

46+
mcp_app = FastAPI()
47+
48+
mcp = FastApiMCP(
49+
app,
50+
name="SQLBot MCP Server",
51+
description="SQLBot MCP Server",
52+
include_operations=["get_datasource_list"]
53+
)
54+
55+
mcp.mount(mcp_app)
56+
4157
# Set all CORS enabled origins
4258
if settings.all_cors_origins:
4359
app.add_middleware(
@@ -66,9 +82,13 @@ def custom_generate_unique_id(route: APIRoute) -> str:
6682
@app.get("/", include_in_schema=False)
6783
async def read_index():
6884
return FileResponse(path=os.path.join(frontend_dist, "index.html"))
69-
85+
86+
7087
app.mount("/", StaticFiles(directory=frontend_dist), name="static")
7188

89+
mcp.setup_server()
90+
7291
if __name__ == "__main__":
7392
import uvicorn
74-
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)
93+
94+
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)

backend/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ dependencies = [
3434
"psycopg2-binary (>=2.9.10,<3.0.0)",
3535
"oracledb (>=3.1.1,<4.0.0)",
3636
"pyyaml (>=6.0.2,<7.0.0)",
37+
"fastapi-mcp (>=0.3.4,<0.4.0)",
3738
]
3839
[[tool.uv.index]]
3940
url = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple"

0 commit comments

Comments
 (0)