[智能体-52]:MCP代码示例 一套完整、可运行、带超详细注释的MCP 代码示例包含MCP Server服务端MCP Client客户端基于JSON-RPC 2.0实现最核心功能tools/list、tools/call全部中文注释完全贴合你说的MCP 结构化 Web 服务 大模型可识别接口一、MCP Server 代码Python功能提供一个查询天气的 MCP 标准工具python运行# -*- coding: utf-8 -*- MCP Server 示例 作用对外提供标准化的 MCP 工具Tools 遵循JSON-RPC 2.0 MCP 应用层协议 import json from flask import Flask, request, jsonify app Flask(__name__) # 1. MCP 服务端能力定义核心 # MCP 标准必须暴露支持哪些工具大模型/Agent 会先读取这个列表 MCP_SERVER_CAPABILITIES { tools: [ { name: get_weather, # 工具名标准英文 description: 查询城市天气, # 描述大模型能看懂这个工具干嘛 inputSchema: { # 入参结构大模型自动生成参数 type: object, properties: { city: { type: string, description: 要查询的城市名称 } }, required: [city] # 必填参数 } } ] } # 2. 工具实现业务逻辑 def get_weather(city: str) - dict: 实际的工具功能查询天气 return { city: city, temperature: 25℃, weather: 晴天, wind: 微风 } # 3. MCP 标准路由JSON-RPC 2.0 app.route(/mcp, methods[POST]) def mcp_server(): MCP 统一入口 所有 MCP Client 都请求这里 遵循 JSON-RPC 2.0 规范 # 1. 获取客户端发来的 JSON-RPC 请求 req_data request.get_json() # 2. 解析 JSON-RPC 基础字段必须jsonrpc, id, method jsonrpc req_data.get(jsonrpc) req_id req_data.get(id) method req_data.get(method) params req_data.get(params, {}) # ------------------------------ # MCP 标准方法 1获取工具列表 # ------------------------------ if method tools/list: return jsonify({ jsonrpc: 2.0, id: req_id, result: MCP_SERVER_CAPABILITIES }) # ------------------------------ # MCP 标准方法 2调用工具核心 # ------------------------------ elif method tools/call: tool_name params.get(name) arguments params.get(arguments, {}) # 调用我们实现的工具 if tool_name get_weather: result get_weather(**arguments) return jsonify({ jsonrpc: 2.0, id: req_id, result: { content: [ {type: text, text: json.dumps(result, ensure_asciiFalse)} ] } }) # ------------------------------ # 方法不存在JSON-RPC 标准错误 # ------------------------------ return jsonify({ jsonrpc: 2.0, id: req_id, error: { code: -32601, message: fMethod {method} not found } }) if __name__ __main__: print(✅ MCP Server 启动成功http://127.0.0.1:5000/mcp) app.run(port5000, debugTrue)二、MCP Client 代码Python功能调用 MCP Server模拟 AI Agent 调用外部工具python运行# -*- coding: utf-8 -*- MCP Client 示例 作用模拟 AI Agent / 大模型 调用 MCP 服务 遵循 JSON-RPC 2.0 MCP 协议 import json import requests # MCP Server 地址 MCP_SERVER_URL http://127.0.0.1:5000/mcp def rpc_request(method, paramsNone, req_id1): 封装 JSON-RPC 2.0 请求 :param method: MCP 方法名tools/list, tools/call :param params: 参数 :param req_id: 请求ID用于匹配请求响应 return { jsonrpc: 2.0, # JSON-RPC 版本固定 id: req_id, # 唯一ID method: method, # MCP 标准方法 params: params or {} } # 1. 第一步获取 MCP 工具列表 print( * 50) print( 步骤1Client 获取 Server 工具列表) resp requests.post(MCP_SERVER_URL, jsonrpc_request(tools/list)) tools_data resp.json() print(工具列表, json.dumps(tools_data, ensure_asciiFalse, indent2)) # 2. 第二步调用 MCP 工具查询天气 print(\n * 50) print( 步骤2Client 调用 MCP 工具get_weather) call_params { name: get_weather, arguments: {city: 北京} } resp requests.post(MCP_SERVER_URL, jsonrpc_request(tools/call, call_params)) result resp.json() print(调用结果, json.dumps(result, ensure_asciiFalse, indent2))三、运行结果一看就懂 MCP 机制1. 获取工具列表Agent / 模型先读这个json{ jsonrpc: 2.0, id: 1, result: { tools: [ { name: get_weather, description: 查询城市天气, inputSchema: { type: object, properties: { city: { type: string, description: 要查询的城市名称 } }, required: [city] } } ] } }2. 调用工具返回json{ jsonrpc: 2.0, id: 1, result: { content: [ { type: text, text: {\city\:\北京\,\temperature\:\25℃\,\weather\:\晴天\,\wind\:\微风\} } ] } }四、这段代码告诉你 MCP 的本质MCP Server 标准化 Web 服务接口 JSON-RPC 2.0内容 大模型能看懂的结构化工具工具名描述入参格式调用 统一格式 tools/call完全符合你说的MCP 是一个结构化、标准化、可被大模型识别的 Web 服务接口。