捣蛋汪星人免安装绿色版
350M · 2025-11-01
// 请求示例
{
"jsonrpc": "2.0",
"id": "req-123",
"method": "calculate",
"params": {"a": 5, "b": 3}
}
// 响应示例
{
"jsonrpc": "2.0",
"id": "req-123",
"result": 8
}
关键字段:method定义工具名,params传递参数
Streamable HTTP优势:
from fastmcp import FastMCP
mcp = FastMCP("Math Server")
@mcp.tool(name="calculate") # 装饰器注册工具
def math_tool(operation: str, a: float, b: float) -> float:
"""执行数学运算,支持add/subtract/multiply/divide"""
if operation == "add": return a + b
elif operation == "subtract": return a - b
elif operation == "multiply": return a * b
elif operation == "divide": return a / b # 注意除零校验
npx @modelcontextprotocol/inspector node math_server.py
验证点:
@app.route('/stream', methods=['GET'])
def sse_stream():
def event_stream():
while True:
# 监听工具调用请求
result = get_tool_result()
yield f"data: {json.dumps(result)}nn" # SSE格式要求
return Response(event_stream(), mimetype="text/event-stream")
const eventSource = new EventSource('http://*l*ocal*host:5000/stream');
// 监听计算结果
eventSource.addEventListener('calculate', (event) => {
const data = JSON.parse(event.data);
console.log("收到结果:", data.result);
});
// 错误处理
eventSource.onerror = (err) => {
console.error("SSE连接异常:", err);
};
避坑指南:需添加会话ID防止消息串扰
@mcp.tool()
def advanced_calc(expression: str) -> float:
"""执行复杂数学表达式,支持sin/cos/log等"""
try:
return eval(expression, {"__builtins__": None}, math.__dict__)
except Exception as e:
raise ValueError(f"表达式错误:{str(e)}")
async def solve_math_problem(question: str):
async with Client(mcp) as client:
# 1. 获取可用工具
tools = await client.list_tools()
# 2. 构造LLM提示词
prompt = f"问题:{question}。可用工具:{json.dumps(tools)}"
# 3. 解析LLM返回的工具调用指令
tool_call = await llm.generate(prompt)
# 4. 执行调用
result = await client.call_tool(tool_call.name, tool_call.params)
return result[0].text # 返回文本结果
统一端点管理:
mcp = FastMCP("AI Server", endpoint="/mcp") # 单端点支持POST/GET/SSE
上下文共享:
@mcp.tool()
def recommend_product(user_id: str):
ctx = get_context() # 获取会话上下文
history = ctx.get("purchase_history")
return db.query(f"SELECT * FROM products WHERE category IN {history}")
无缝集成OpenAPI:
from fastmcp.integrations import FastAPIPlugin
fastapi_app.include_router(FastAPIPlugin(mcp).router)
# 输入消毒
def sanitize_input(sql: str) -> str:
return re.sub(r"[;"]", "", sql) # 移除危险字符
# 权限分级
TOOL_PERMISSIONS = {"query_db": ["user"], "execute_payment": ["admin"]}
# 审计日志
@app.middleware("http")
async def log_requests(request: Request, call_next):
logger.info(f"{request.client} called {request.url.path}")
return await call_next(request)
源码与资源:
掌握MCP架构,你将成为大模型落地的“关键桥梁”。建议从本地工具调用起步,逐步挑战企业级集成场景!更多AI大模型应用开发学习视频内容和资料,尽在聚客AI学院。