|
@@ -1,52 +1,46 @@
|
|
|
-# from __future__ import annotations
|
|
|
|
|
-#
|
|
|
|
|
-# from pydantic import ValidationError
|
|
|
|
|
-# from quart import Blueprint, jsonify
|
|
|
|
|
-#
|
|
|
|
|
-# from app.api.v1.utils import ApiDependencies, LongArticlesMcpRequest
|
|
|
|
|
-# from app.api.v1.utils import parse_json, validation_error_response
|
|
|
|
|
-# # from app.domains.mcp import deal, UnknownMcpTaskName
|
|
|
|
|
-#
|
|
|
|
|
-#
|
|
|
|
|
-# def create_mcp_bp(deps: ApiDependencies) -> Blueprint:
|
|
|
|
|
-# bp = Blueprint("mcp", __name__)
|
|
|
|
|
-#
|
|
|
|
|
-# @bp.route("/long_articles_mcp/<string:task_name>", methods=["POST"])
|
|
|
|
|
-# async def long_articles_mcp(task_name: str):
|
|
|
|
|
-# """MCP 交互接口
|
|
|
|
|
-#
|
|
|
|
|
-# - POST /api/long_articles_mcp/<task_name>
|
|
|
|
|
-# - 通过 task_name 分发到 MCP 域内对应的方法
|
|
|
|
|
-# - 请求体为 JSON,可选字段如 page、page_size、sort_by、sort_order 等
|
|
|
|
|
-# """
|
|
|
|
|
-# try:
|
|
|
|
|
-# req, _ = await parse_json(LongArticlesMcpRequest)
|
|
|
|
|
-# except ValidationError as e:
|
|
|
|
|
-# payload, status = validation_error_response(e)
|
|
|
|
|
-# return jsonify(payload), status
|
|
|
|
|
-#
|
|
|
|
|
-# # 使用校验后的模型转 dict,保证类型正确,且含 extra 透传字段
|
|
|
|
|
-# params = req.model_dump(exclude_none=True)
|
|
|
|
|
-#
|
|
|
|
|
-# try:
|
|
|
|
|
-# result = await deal(
|
|
|
|
|
-# task_name=task_name,
|
|
|
|
|
-# pool=deps.db,
|
|
|
|
|
-# log_service=deps.log,
|
|
|
|
|
-# params=params,
|
|
|
|
|
-# )
|
|
|
|
|
-# except UnknownMcpTaskName:
|
|
|
|
|
-# return (
|
|
|
|
|
-# jsonify(
|
|
|
|
|
-# {
|
|
|
|
|
-# "code": 404,
|
|
|
|
|
-# "message": f"unknown task_name: {task_name}",
|
|
|
|
|
-# "data": None,
|
|
|
|
|
-# }
|
|
|
|
|
-# ),
|
|
|
|
|
-# 404,
|
|
|
|
|
-# )
|
|
|
|
|
-#
|
|
|
|
|
-# return jsonify({"code": 0, "message": "success", "data": result})
|
|
|
|
|
-#
|
|
|
|
|
-# return bp
|
|
|
|
|
|
|
+from __future__ import annotations
|
|
|
|
|
+
|
|
|
|
|
+from pydantic import ValidationError
|
|
|
|
|
+from quart import Blueprint, jsonify
|
|
|
|
|
+
|
|
|
|
|
+from app.api.v1.utils import ApiDependencies, LongArticlesMcpRequest
|
|
|
|
|
+from app.api.v1.utils import parse_json, validation_error_response
|
|
|
|
|
+from app.domains.mcp import LongArticlesMcp
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def create_mcp_bp(deps: ApiDependencies) -> Blueprint:
|
|
|
|
|
+ bp = Blueprint("mcp", __name__)
|
|
|
|
|
+
|
|
|
|
|
+ @bp.route("/long_articles_mcp/<string:task_name>", methods=["POST"])
|
|
|
|
|
+ async def long_articles_mcp(task_name: str):
|
|
|
|
|
+ """MCP 交互接口
|
|
|
|
|
+
|
|
|
|
|
+ POST /api/long_articles_mcp/<task_name>
|
|
|
|
|
+ 请求体为 JSON,可选字段: page, page_size, sort_by, sort_order
|
|
|
|
|
+ """
|
|
|
|
|
+ # 1. 请求体校验
|
|
|
|
|
+ try:
|
|
|
|
|
+ req, _ = await parse_json(LongArticlesMcpRequest)
|
|
|
|
|
+ except ValidationError as e:
|
|
|
|
|
+ payload, status = validation_error_response(e)
|
|
|
|
|
+ return jsonify(payload), status
|
|
|
|
|
+
|
|
|
|
|
+ params = req.model_dump(exclude_none=True)
|
|
|
|
|
+
|
|
|
|
|
+ # 2. 构造 MCP 入口并分发
|
|
|
|
|
+ mcp = LongArticlesMcp(
|
|
|
|
|
+ params=params,
|
|
|
|
|
+ pool=deps.db,
|
|
|
|
|
+ log_service=deps.log,
|
|
|
|
|
+ config=deps.config,
|
|
|
|
|
+ )
|
|
|
|
|
+ result = await mcp.deal(task_name=task_name, params=params)
|
|
|
|
|
+
|
|
|
|
|
+ # 3. deal() 已统一封装 {code, message, data},直接透传
|
|
|
|
|
+ http_status = 200 if result.get("code") == 0 else result.get("code", 500)
|
|
|
|
|
+ return jsonify(result), http_status
|
|
|
|
|
+
|
|
|
|
|
+ return bp
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+__all__ = ["create_mcp_bp"]
|