玩转MCP(2)-原理篇

1. 引言 这篇文章萌叔来谈谈MCP的协议的一些重要概念,以及它是如何和大模型进行交互的。 2. MCP架构 2.1 Host Host进程充当容器和协调器,它要负责启动MCP-Client,使用Client与对应的MCP-Server进行交互。 通常而言Host with Client就是第一个AI Agent,在其中必然会涉及与大模型的交互。 2.2 Client Client由Host创建,每个Client与特定的Server具有一对一的关系 2.3 Server 通过 MCP 原语暴露resource、tool和prompt, Server可以运行在本地或者是远程服务 3. MCP协议的内容 MCP协议的核心诉求是对外说明Server具有什么能力,Client该如何使用这种能力,每种能力其实都被抽象成了某个tool,类似于函数。 为了更好的使用tool,MCP引入了resource和prompt, 3.1 resource resource一般是可访问的静态或动态数据源,笔者的理解,resource是领域相关的知识库 3.2 prompt 用户或系统提供给模型的指令或上下文输入,用于引导模型行为。 大部分情况下,只提供tool即可,注意: 一个server通过会提供多个tool。 Client和Server采用JSON-RPC 2.0协议。 3.3 Client要获取tool信息通常会发起 ListTools 指令 Request { "method": "tools/list", "params": { "_meta": { "progressToken": 3 } }, "jsonrpc": "2.0", "id": 3 } Response { "jsonrpc": "2.0", "id": 3, "result": { "tools": [{ "name": "remember", "description": "Retrieve historical chat records between users and LLM.", "inputSchema": { "properties": { "keyword": { "description": "key word", "title": "Keyword", "type": "string" }, "start_date": { "anyOf": [{ "type": "string" }, { "type": "null" }], "default": null, "description": "Start date in 'YYYYMMDD' format.When empty, automatically uses the date 3 months before today", "examples": ["20250620"], "title": "Start Date" }, "end_date": { "anyOf": [{ "type": "string" }, { "type": "null" }], "default": null, "description": "End date in 'YYYYMMDD' format.When empty, automatically uses today's date", "examples": ["20250710"], "title": "End Date" }, "max_message_count": { "default": 200, "description": "The maximum number of messages that can be returned", "minimum": 1, "title": "Max Message Count", "type": "integer" } }, "required": ["keyword"], "type": "object" }, "outputSchema": { "properties": { "result": { "title": "Result", "type": "string" } }, "required": ["result"], "title": "_WrappedResult", "type": "object", "x-fastmcp-wrap-result": true } }] } } 3.4 Client使用某个tool时,会使用 callTool 指令 Request { "jsonrpc": "2.0", "method": "tools/call", "id": "tool-call-1754015372815", "params": { "name": "get_datetime", "arguments": { "format": "date_jp" }, "_meta": { "progressToken": 0 } } } Response { "jsonrpc": "2.0", "id": "tool-call-1754015372815", "result": { "content": [{ "type": "text", "text": "2025年08月01日" }], "isError": false } } 4.与大模型的交互 我们已经通过MCP协议获知了MCP Server所能提供的能力,现在需要把这些信息告诉大模型, 让大模型依据上下文以及MCP Server提供的能力,做出决策,发出指令。 ...

August 5, 2025 · 5 min

玩转MCP(1)-使用篇

1. 引言 MCP可以说是function calling的升级版,它使得大语言模型可以方便的整合和具有工具能力。 简单而言,LLM只是一个大脑,它只能思考。 但是当我们给它提供了MCP之后,它可以轻易的获得各种能力, 宛如给他安上了手和腿,它可以行走了,并对外部世界施加影响。 2. 一些MCP 聊天App 萌叔使用的是 daodao97/chatmcp chatmcp可以很方便的集成其它mcp工具,这里介绍几个MCP 2.1 mcp-searxng ihor-sokoliuk/mcp-searxng SearXNG 是一款免费的互联网元搜索引擎,它聚合了来自各种搜索引擎和服务的结果。 用户既不会被追踪,也不会被画像。传送门 使用mcp-searxng,使得大模型可以通过搜索引擎获取信息。 配置方式 { "mcpServers": { "searxng": { "command": "npx", "args": [ "-y", "mcp-searxng" ], "env": { "SEARXNG_URL": "YOUR_SEARXNG_INSTANCE_URL" } } } } Tools searxng_web_search 利用搜索引擎进行检索 web_url_read 从某个URL读取内容 2.2 mcp-datetime ZeparHyfar/mcp-datetime 很多大模型没有时间概念,这会导致他在处理问题时,出现时间错误,mcp-datetime可以 以各种格式获取当前日期和时间。 配置方式 { "mcpServers": { "mcp-datetime": { "command": "uvx", "args": ["mcp-datetime"] } } } Tools get_datetime 2.3 mcp-server-chart antvis/mcp-server-chart antvis/mcp-server-chart是蚂蚁集团出品的图表工具。 mcp-server-chart生成的图片已经使用CDN加速,与大模型交互只传递URL,可以极大的减少token开销。 配置方式 { "mcpServers": { "mcp-server-chart": { "command": "npx", "args": [ "-y", "@antv/mcp-server-chart" ] } } } Tools 可以画25种图表 ...

July 27, 2025 · 2 min