更新时间:2026-09-24 GMT+08:00
调用网关工具
在请求体中指定tools/call作为请求方法,调用网关提供的特定工具。调用时需指定工具名称和入参,工具名称可通过tools/list接口查询获取,入参需严格按照工具定义的参数结构填写。
请求体示例:
{
"jsonrpc": "2.0",
"id": "call-tool-request",
"method": "tools/call",
"params": {
"name": "<TOOL_NAME>",
"arguments": {
"param1": "value1",
"param2": "value2"
}
}
} 其中name为待调用的工具名称,通过tools/list接口查询获取;arguments为工具调用入参,需严格按照工具定义的参数结构填写。
响应体示例:
{
"jsonrpc": "2.0",
"id": "call-tool-request",
"result": {
"content": [
{
"type": "text",
"text": "工具调用结果"
}
]
}
} - API Key认证的tools/call调用示例
import requests import json import re def call_tool(gateway_url, access_token, tool_name, arguments): headers = { "Content-Type": "application/json", "Accept": "application/json, text/event-stream", "mcp-session-id": "<MCP_SESSION_ID>", # MCP会话ID,相同的sessionid会使用同一网关资源,调用时请配置此参数,例如:4ada96ff-1e16-4e5b-8fb4-b3f0f207a4f3 "Mcp-Protocol-Version": "<MCP_PROTOCOL_VERSION>", # MCP协议版本 "Authorization": f"Bearer {access_token}" } payload = {"jsonrpc": "2.0","id": "call-tool-request","method": "tools/call","params": {"name": tool_name,"arguments": arguments}} response = requests.post(gateway_url, headers=headers, json=payload) # 若需要忽略ssl校验,可补充参数verify=False raw = response.content.decode('utf-8', errors='replace') ct = response.headers.get('content-type', '') if 'text/event-stream' in ct: for line in raw.strip().split('\n'): if line.startswith('data:'): try: return json.loads(line[5:].strip()) except json.JSONDecodeError: continue m = re.search(r'\{.*\}', raw, re.DOTALL) if m: return json.loads(m.group()) try: return json.loads(raw) except json.JSONDecodeError: print(f"[Fail]: {raw[:200]}") return None gateway_url = "<GATEWAY_URL>" # 网关接口地址(业务API调用入口) access_token = "<API_KEY>" # 认证凭证:APIKey认证直接填写密钥;无认证则留空/删除 tool_name = "<TOOL_NAME>" # 待调用的工具名称,通过 tools/list 接口查询获取 arguments = {"param1": "value1", "param2": "value2"} # 工具调用入参,严格按照工具定义的参数结构填写 result = call_tool(gateway_url, access_token, tool_name, arguments) if result: print(json.dumps(result, indent=2, ensure_ascii=False)) - IAM认证的tools/call调用示例
import requests import json import re def call_tool(gateway_url, access_token, tool_name, arguments): headers = { "Content-Type": "application/json", "Accept": "application/json, text/event-stream", "mcp-session-id": "<MCP_SESSION_ID>", # MCP会话ID,相同的sessionid会使用同一网关资源,调用时请配置此参数,例如:4ada96ff-1e16-4e5b-8fb4-b3f0f207a4f3 "Mcp-Protocol-Version": "<MCP_PROTOCOL_VERSION>", # MCP协议版本 "X-Sdk-Date": "<X_SDK_DATE>", # 网关鉴权时间戳 "X-Sdk-Content-Sha256": "UNSIGNED-PAYLOAD", "Authorization": access_token } payload = {"jsonrpc": "2.0", "id": "call-tool-request", "method": "tools/call", "params": {"name": tool_name, "arguments": arguments}} response = requests.post(gateway_url, headers=headers, json=payload) # 若需要忽略ssl校验,可补充参数verify=False raw = response.content.decode('utf-8', errors='replace') ct = response.headers.get('content-type', '') if 'text/event-stream' in ct: for line in raw.strip().split('\n'): if line.startswith('data:'): try: return json.loads(line[5:].strip()) except json.JSONDecodeError: continue m = re.search(r'\{.*\}', raw, re.DOTALL) if m: return json.loads(m.group()) try: return json.loads(raw) except json.JSONDecodeError: print(f"[Fail]: {raw[:200]}") return None gateway_url = "<GATEWAY_URL>" # 网关接口地址(业务API调用入口) access_token = "<AUTHORIZATION>" # 鉴权Authorization tool_name = "<TOOL_NAME>" # 待调用的工具名称,通过 tools/list 接口查询获取 arguments = {"param1": "value1", "param2": "value2"} # 工具调用入参,严格按照工具定义的参数结构填写 result = call_tool(gateway_url, access_token, tool_name, arguments) if result: print(json.dumps(result, indent=2, ensure_ascii=False))
父主题: 调测与调用MCP Target