def build_cg_rag_mcp(service: Any) -> FastMCP:
mcp = FastMCP("CG_RAG", json_response=True, streamable_http_path="/")
@mcp.tool()
async def cg_health() -> dict[str, Any]:
"""返回 CG_RAG 服务健康状态、active scope、可用 profile、rerank 和 LLM 配置状态。"""
return await _call_service(service.health)
@mcp.tool()
async def cg_list_profiles() -> dict[str, Any]:
"""列出 CG_RAG 可用检索范围 profile,例如 usual、usual_plus_law 和 full。"""
return await _call_service(lambda: {"profiles": service.profiles()})
@mcp.tool()
async def cg_retrieve_rerank(
query: TextInput = None, scope: TextInput = "usual", topk: IntInput = 78
) -> dict[str, Any]:
"""执行 CG_RAG 召回和重排,只返回候选条文,不调用 LLM 生成。"""
return await _call_service(
lambda: service.retrieve_rerank(CgRetrieveRerankRequest(query=query, scope=scope, topk=topk))
)
@mcp.tool()
async def cg_constrained_generate(
query: TextInput = None,
retrieval_docs: DocsInput = None,
max_items: IntInput = 10,
include_debug: BoolInput = False,
) -> dict[str, Any]:
"""基于 CG_RAG 调用方传入的候选条文执行受约束生成,返回候选索引和格式化条文。"""
return await _call_service(
lambda: service.constrained_generate(
CgConstrainedGenerateRequest(
query=query,
retrieval_docs=retrieval_docs,
max_items=max_items,
include_debug=include_debug,
)
)
)
@mcp.tool()
async def cg_rag(
query: TextInput = None,
scope: TextInput = "usual",
topk: IntInput = 78,
max_items: IntInput = 10,
include_debug: BoolInput = False,
) -> dict[str, Any]:
"""执行完整 CG_RAG 流程:召回、重排、受约束生成。该流程不包含 domain gate。"""
return await _call_service(
lambda: service.rag(
CgRagRequest(
query=query,
scope=scope,
topk=topk,
max_items=max_items,
include_debug=include_debug,
)
),
operation_ok_from_data=_cg_rag_operation_ok,
)
return mcp