首页›文档/手册›MCP 接入›工具与调用

工具与调用

MCP 接入 阅读 9

initialize / tools/list / tools/call,以及资源与提示词的调用示例。

一次典型会话

MCP 客户端连上后一般按这个顺序走:initialize 握手 → 列能力(tools/list 等)→ 按需调用(tools/call 等)。

握手

curl https://api.chenra.com/api/relay/mcp \
  -H "Authorization: Bearer sk-你的密钥" \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18"}}'

返回协议版本、能力与自身信息:

{"jsonrpc":"2.0","id":1,"result":{"protocolVersion":"2025-06-18","capabilities":{"tools":{},"resources":{},"prompts":{}},"serverInfo":{"name":"z-hub-mcp","version":"1.0.0"}}}

列出工具

{"jsonrpc":"2.0","id":2,"method":"tools/list"}

每个工具给出 name / description / inputSchema;模型据此决定调哪个、带什么参数。 以「MCP 服务」页里列出的服务为准(平台新增能力时这里会多出对应工具)。

调用工具:读微信账号基础信息

工具 wechat_basic_info 传账号ID 即可:

{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"wechat_basic_info","arguments":{"wechat_id":29129}}}

成功返回结果内容(text 里是可读的多行文本):

{"jsonrpc":"2.0","id":3,"result":{"content":[{"type":"text","text":"微信账号基础信息\n微信账号ID:29129\n昵称:小予\nwxid:wxid_xxx\n微信号:xxx\n头像:https://...\n性别:男\n在线:否\n最近登录:2026-06-30 15:07:01\n视频号:已开通\nAI接管:已开启"}],"isError":false}}

只放行已授权给当前密钥的账号。调了没授权的账号,工具不报协议错误,而是在结果里带 isError: true 说明原因:

{"jsonrpc":"2.0","id":4,"result":{"content":[{"type":"text","text":"微信账号 29129 未授权给当前密钥,请先在客户端「MCP 配置」里授权!"}],"isError":true}}

未知工具才走协议错误:

{"jsonrpc":"2.0","id":5,"error":{"code":-32602,"message":"未知工具:nope"}}

资源与提示词

资源是只读上下文(按 URI 取),提示词是可复用模板(按名字取)。当前还没有服务产出两类条目,有产出时用:

{"jsonrpc":"2.0","id":6,"method":"resources/list"}
{"jsonrpc":"2.0","id":7,"method":"resources/read","params":{"uri":"wechat://..."}}
{"jsonrpc":"2.0","id":8,"method":"prompts/list"}
{"jsonrpc":"2.0","id":9,"method":"prompts/get","params":{"name":"..."}}

resources/read 返回 contents,prompts/get 返回 messages(可直接作为对话开头的消息)。

分享这篇文档