包含 CodeAgent 前后端源码、SKILLS、MCP、demo 代码与工作区文档。 排除依赖缓存、运行数据、备份与二进制附件,规则见 .gitignore。 Co-Authored-By: Claude Code <[email protected]>
43 lines
1.2 KiB
Python
43 lines
1.2 KiB
Python
"""
|
|
MCP 协议 API
|
|
============
|
|
|
|
【功能说明】MCP 工具注册/查询/调用。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from typing import Any
|
|
|
|
from fastapi import APIRouter
|
|
|
|
from app.schemas.project import ApiResponse
|
|
from app.services.mcp_service import registry
|
|
|
|
logger = logging.getLogger("agent.mcp.api")
|
|
|
|
router = APIRouter(prefix="/api/mcp", tags=["MCP 协议"])
|
|
|
|
|
|
@router.get("/tools", response_model=ApiResponse[list[dict]], summary="MCP 工具列表")
|
|
def list_tools() -> ApiResponse[list[dict]]:
|
|
"""列出所有已注册的 MCP 工具。"""
|
|
tools = registry.list_tools()
|
|
return ApiResponse(code=0, message=f"共 {len(tools)} 个工具", data=tools)
|
|
|
|
|
|
@router.post("/call", response_model=ApiResponse[dict], summary="调用 MCP 工具")
|
|
def call_tool(payload: dict[str, Any]) -> ApiResponse[dict]:
|
|
"""调用指定 MCP 工具。"""
|
|
name = payload.get("name", "")
|
|
arguments = payload.get("arguments", {})
|
|
if not name:
|
|
return ApiResponse(code=422, message="name 必填", data=None)
|
|
result = registry.call_tool(name, arguments)
|
|
return ApiResponse(
|
|
code=0 if result.get("success") else 1,
|
|
message=result.get("message", "ok"),
|
|
data=result,
|
|
)
|