Skip to content

Commit 3569955

Browse files
committed
feat: mcp tools
1 parent 60e9b7a commit 3569955

File tree

3 files changed

+15
-21
lines changed

3 files changed

+15
-21
lines changed

backend/apps/chat/api/chat.py

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ async def delete(session: SessionDep, chart_id: int):
5757
)
5858

5959

60+
@router.post("/mcp_start", operation_id="mcp_start")
61+
async def mcp_start(session: SessionDep, token: str, create_chat_obj: CreateChat):
62+
user = await get_current_user(session, token)
63+
return await start_chat(session, user, create_chat_obj)
64+
65+
66+
@router.post("/mcp_question", operation_id="mcp_question")
67+
async def mcp_question(session: SessionDep, token: str, request_question: ChatQuestion):
68+
user = await get_current_user(session, token)
69+
return await stream_sql(session, user, request_question)
70+
71+
6072
@router.post("/start")
6173
async def start_chat(session: SessionDep, current_user: CurrentUser, create_chat_obj: CreateChat):
6274
try:
@@ -68,25 +80,6 @@ async def start_chat(session: SessionDep, current_user: CurrentUser, create_chat
6880
)
6981

7082

71-
@router.post("/mcp_question", operation_id="mcp_question")
72-
async def mcp_question(session: SessionDep, token: str, request_question: ChatQuestion):
73-
user = await get_current_user(session, token)
74-
# return await stream_sql(session, user, request_question)
75-
return {"content": """步骤1: 确定需要查询的字段。
76-
我们需要统计上海的订单总数,因此需要从"城市"字段中筛选出值为"上海"的记录,并使用COUNT函数计算这些记录的数量。
77-
78-
步骤2: 确定筛选条件。
79-
问题要求统计上海的订单总数,所以我们需要在SQL语句中添加WHERE "城市" = '上海'来筛选出符合条件的记录。
80-
81-
步骤3: 避免关键字冲突。
82-
因为这个Excel/CSV数据库是 PostgreSQL 类型,所以在schema、表名、字段名和别名外层加双引号。
83-
84-
最终答案:
85-
```json
86-
{"success":true,"sql":"SELECT COUNT(*) AS \"TotalOrders\" FROM \"public\".\"Sheet1_c27345b66e\" WHERE \"城市\" = '上海';"}
87-
```"""}
88-
89-
9083
@router.post("/question", operation_id="question")
9184
async def stream_sql(session: SessionDep, current_user: CurrentUser, request_question: ChatQuestion):
9285
"""Stream SQL analysis results

backend/common/utils/whitelist.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"/mcp/message",
2626
"/datasource/list",
2727
"/system/aimodel/list",
28-
"/chat/mcp_question"
28+
"/chat/mcp_question",
29+
"/chat/mcp_start"
2930
]
3031

3132
class WhitelistChecker:

backend/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def custom_generate_unique_id(route: APIRoute) -> str:
5353
description="SQLBot MCP Server",
5454
describe_all_responses=True,
5555
describe_full_response_schema=True,
56-
include_operations=["get_datasource_list", "get_model_list", "mcp_question"]
56+
include_operations=["get_datasource_list", "get_model_list", "mcp_question", "mcp_start"]
5757
)
5858

5959
mcp.mount(mcp_app)

0 commit comments

Comments
 (0)