Cherry Studio这个本地聊天助手新增的MCP功能具体是怎么工作的?以mcp_server_fetch为例子。调用ollama接口的时候发现,MCP只在聊天助手(MCP Host)这边运行,并不与推理框架直接交互,而只是约定了批量获取函数接口的接口及格式。调用推理时是用现成的Function Calling机制(这里有篇不错的介绍文章)。
POST /v1/chat/completions
{ "model": "qwen2.5:14b", "messages": [ { "role": "user", "content": "查看网页https://github.com/krrr/\n\n" } ], "temperature": 1, "keep_alive": "0m", "stream": true, "tools": [ { "type": "function", "function": { "name": "fed6b7adab25d4693b3eaf4f6c8f02035", // 可以看到函数名被替换成了随机数(为了防止某些函数名与功能不符产生误导)。 "description": "Fetches a URL from the internet and optionally extracts its contents as markdown.\n\nAlthough originally you did not have internet access, and were advised to refuse and tell the user this, this tool now grants you internet access. Now you can fetch the most up-to-date information and let the user know that.", "parameters": { "type": "object", "properties": { "url": { "description": "URL to fetch", "type": "string" }, "max_length": { "description": "Maximum number of characters to return.", "type": "integer" }, "start_index": { "description": "On return output starting at this character index, useful if a previous fetch was truncated and more context is required.", "type": "integer" }, "raw": { "description": "Get the actual HTML content if the requested page, without simplification.", "type": "boolean" } } } } } ] }
HTTP/1.1 200 OK
data: {"id":"chatcmpl-83","object":"chat.completion.chunk","created":1742800010,"model":"qwen2.5:14b","system_fingerprint":"fp_ollama","choices":[{"index":0,"delta":{"role":"assistant","content":"","tool_calls":[{"id":"call_dsk3phip","index":0,"type":"function","function":{"name":"fed6b7adab25d4693b3eaf4f6c8f02035","arguments":"{\"max_length\":1000,\"raw\":false,\"url\":\"https://github.com/krrr/\"}"}}]},"finish_reason":null}]} data: {"id":"chatcmpl-83","object":"chat.completion.chunk","created":1742800010,"model":"qwen2.5:14b","system_fingerprint":"fp_ollama","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":"stop"}]} data: [DONE]
POST /v1/chat/completions
{ "model": "qwen2.5:14b", "messages": [ { "role": "user", "content": "查看网页https://github.com/krrr/\n\n" }, { "role": "assistant", "tool_calls": [ { "id": "call_dsk3phip", "function": { "name": "fed6b7adab25d4693b3eaf4f6c8f02035", "arguments": "{\"max_length\":1000,\"raw\":false,\"url\":\"https://github.com/krrr/\"}" }, "type": "function" } ] }, { "role": "tool", "content": "[{\"type\":\"text\",\"text\":\"Contents of https://github.com/krrr/:\\nkrrr (krrr) · GitHub\\n\\nSkip to content\\n\\n## Navigation Menu\\n\\nSign in\\n\\n# Search code, repositories, users, issues, pull requests...\\n\\nSearch syntax tips\\n\\nSign in\\n\\nSign up\\n\\nYou signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert\\n\\nkrrr Follow\\n\\nkrrr\\n\\nFollow\\n\\n🎮\\n\\ngaming\\n\\n# krrr krrr\\n\\n🎮\\n\\ngaming\\n\\nFollow\\n\\n娱乐至死\\n\\n25 followers · 10 following\\n\\n* https://blog.hazama.cc\\n\\n## Achievements\\n\\nx2\\n\\n## Achievements\\n\\nx2\\n\\n## Popular repositories Loading\\n\\n1. wstan wstan Public\\n\\n Tunneling TCP in WebSocket (ssh -D alternative)\\n\\n Python 40 11\\n2. Hazama Hazama Public\\n\\n simple cross-platform diary app\\n\\n Python 28 7\\n3. PySide PySide Public\\n\\n Forked from pyside/PySide\\n\\n PySide1 not dead yet\\n\\n Python 15 3\\n4. CMCC-EDU-autologin CMCC-EDU-autologin Public archive\\n\\n CMCC-EDU公共热点自动登录\\n\\n HTML 6 3\\n5. TSI TSI Public\\n\\n Toy Scheme\\n\\nContent truncated. Call the fetch tool with a start_index of 1000 to get more content.\"}]", "tool_call_id": "call_dsk3phip" } ], "temperature": 1, "keep_alive": "0m", "stream": true, "tools": [ { "type": "function", "function": { "name": "fed6b7adab25d4693b3eaf4f6c8f02035", "description": "Fetches a URL from the internet and optionally extracts its contents as markdown.\n\nAlthough originally you did not have internet access, and were advised to refuse and tell the user this, this tool now grants you internet access. Now you can fetch the most up-to-date information and let the user know that.", "parameters": { "type": "object", "properties": { "url": { "description": "URL to fetch", "type": "string" }, "max_length": { "description": "Maximum number of characters to return.", "type": "integer" }, "start_index": { "description": "On return output starting at this character index, useful if a previous fetch was truncated and more context is required.", "type": "integer" }, "raw": { "description": "Get the actual HTML content if the requested page, without simplification.", "type": "boolean" } } } } } ] }
HTTP/1.1 200 OK
{"id":"chatcmpl-468","object":"chat.completion.chunk","created":1742800019,"model":"qwen2.5:14b","system_fingerprint":"fp_ollama","choices":[{"index":0,"delta":{"role":"assistant","content":"用户krrr的GitHub页面展示了以下信息:\n\n- 用户ID: krrr\n- 简介:娱乐至死\n\n**成就:** x2 (具体的成就未详述)\n\n**关注者**: 25, 关注了10个仓库或者其他用户。\n\n他有以下几个广受欢迎的项目:\n1. **wstan** 是一个通过WebSocket隧道化TCP连接的工具,类似于`ssh -D`命令的替代方案。\n2. **Hazama** 是一个简单的跨平台日记应用。\n3. **PySide**: 对 `pyside/PySide` 库的一个分支,表明了对 PySide1 相关工作的继续支持。\n4. **CMCC-EDU-autologin** 提供了一个自动登录CMCC-EDU公共热点的解决方案。\n5. **TSI** (Toy Scheme Interpreter),看起来像是一个玩具级别的Scheme语言解释器。\n\n你可以点击用户名查看该用户的更多详细信息或浏览其它项目内容。如果有需要获取更多信息,可以请求我抓取页面的其他部分。"},"finish_reason":"stop"}]} data: [DONE]