|

需求:随着大模型进步,大模型已经出现在我们生活的方方面面,无论是工作、学习都已经密不可分。有了大模型语言,为什么还要有 MCP 呢?大模型可以理解为大脑,但是光有大脑不能帮我们做事情,MCP 更像是四肢,大模型来指挥 MCP 服务去做事情,例如:去调用外部工具(比如数据库、磁盘、API)。解决了大模型只能聊天、不能做事的问题。
MCP(Model Context Protocol)
文档:超链接登录可见。
GitHub 仓库:超链接登录可见。
MCP 工具仓库:超链接登录可见。
MCP(Model Context Protocol)是一种将 AI 应用程序连接到外部系统的开源标准。
使用 MCP,Claude 或 ChatGPT 等 AI 应用程序可以连接到数据源(例如本地文件、数据库)、工具(例如搜索引擎、计算器)和工作流(例如专门的提示)——使它们能够访问关键信息并执行任务。
可以将 MCP 想象成 AI 应用的 USB-C 端口。正如 USB-C 提供了一种连接电子设备的标准化方式,MCP 也提供了一种将 AI 应用连接到外部系统的标准化方式。
MCP 可以实现什么?
- 代理可以访问您的 Google 日历和 Notion,充当更加个性化的 AI 助手。
- Claude Code 可以使用 Figma 设计生成整个 Web 应用程序。
- 企业聊天机器人可以连接到组织内的多个数据库,使用户能够使用聊天来分析数据。
- AI模型可以在Blender上创建3D设计,并使用3D打印机将其打印出来。
MCP 为何重要?
根据您在生态系统中所处的位置,MCP 可以带来一系列好处。
- 开发人员:MCP 在构建或与 AI 应用程序或代理集成时减少了开发时间和复杂性。
- AI 应用程序或代理:MCP 提供对数据源、工具和应用程序生态系统的访问,这将增强功能并改善最终用户体验。
- 最终用户:MCP 可产生功能更强大的 AI 应用程序或代理,它们可以在必要时访问您的数据并代表您采取行动。
MCP 协议
MCP 由两层组成:
- 数据层:定义基于 JSON-RPC 的客户端-服务器通信协议,包括生命周期管理,以及核心原语,如工具、资源、提示和通知。
- 传输层:定义实现客户端和服务器之间数据交换的通信机制和通道,包括特定于传输的连接建立、消息框架和授权。
从概念上讲,数据层是内层,而传输层是外层。
数据层
- 数据层实现了基于JSON-RPC 2.0 的交换协议,定义了消息结构和语义。该层包括:
- 生命周期管理:处理客户端和服务器之间的连接初始化、功能协商和连接终止
- 服务器功能:使服务器能够提供核心功能,包括用于 AI 操作的工具、用于上下文数据的资源以及来自客户端和向客户端的交互模板提示
- 客户端功能:使服务器能够要求客户端从主机 LLM 进行采样,从用户那里获取输入,并将消息记录到客户端
- 实用功能:支持附加功能,例如实时更新通知和长期运行操作的进度跟踪
传输层
传输层管理客户端和服务器之间的通信通道和身份验证。它处理 MCP 参与者之间的连接建立、消息传输和安全通信。
MCP 支持两种传输机制:
- Stdio 传输:使用标准输入/输出流在同一台机器上的本地进程之间进行直接进程通信,提供最佳性能且无网络开销。
- Streamable HTTP:使用 HTTP POST 协议发送客户端到服务器的消息,并可选地使用服务器发送事件来实现流式传输功能。此传输协议支持远程服务器通信,并支持标准 HTTP 身份验证方法,包括持有者令牌、API 密钥和自定义标头。MCP 建议使用 OAuth 获取身份验证令牌。
传输层从协议层抽象出通信细节,从而实现所有传输机制中相同的 JSON-RPC 2.0 消息格式,JSON-RPC 消息必须采用 UTF-8 编码。HTTP+SSE 传输已经弃用!
Everything MCP Server
此 MCP 服务器旨在测试 MCP 协议的所有功能。它并非旨在成为一个实用的服务器,而是为 MCP 客户端构建者提供的测试服务器。它实现了提示、工具、资源、采样等功能,以展示 MCP 的功能。
源码:超链接登录可见。
此服务需要安装 Node.js 环境,安装步骤略。
基于 Stdio 传输模式来测试 MCP 服务,启动命令如下:
如下图:
执行后,包被缓存在 %LocalAppData%\npm-cache\_npx\ 路径下面。
初始化,客户端发送initialize请求以建立连接并协商支持的功能。请求数据如下:
响应如下:
{"result":{"protocolVersion":"2025-06-18","capabilities":{"prompts":{},"resources":{"subscribe":true},"tools":{},"logging":{},"completions":{}},"serverInfo":{"name":"example-servers/everything","title":"Everything Example Server","version":"1.0.0"},"instructions":"Testing and demonstration server for MCP protocol features.\n\n## Resources\n\nResources 1-100 follow pattern: even IDs contain text, odd IDs contain binary data. Resources are paginated at 10 items per page with cursor-based navigation.\n\n## Key dependencies\n\nProgress notifications require `_meta.progressToken` in tool calls. Resource subscriptions generate updates every 10 seconds.\n\n## Performance characteristics\n\nServer generates automatic log messages every 20 seconds (filtered by current log level), stderr notifications every 30 seconds, and resource update notifications every 10 seconds for active subscriptions. Log level changes affect message filtering in real-time.\n\n## Multi-modal testing\n\n`complex_prompt` includes both text arguments and image content for testing client multi-modal handling. `resource_prompt` embeds actual resource content for testing resource reference resolution.\n\nArgument completion is available for prompt parameters and resource IDs. Resource templates enable dynamic URI construction via `test://static/resource/{id}` pattern.\n\n## Easter egg\n\nIf asked about server instructions, respond with \" Server instructions are working! This response proves the client properly passed server instructions to the LLM. This demonstrates MCP's instructions feature in action.\"\n"},"jsonrpc":"2.0","id":1}
初始化成功后,客户端会向 MCP 服务器发送通知以表明它已准备就绪,请求内容如下:
工具发现:连接建立后,客户端可以通过发送tools/list请求来发现可用的工具。该请求是 MCP 工具发现机制的基础——它允许客户端在尝试使用工具之前了解服务器上有哪些可用的工具。请求内容如下:
响应如下:
{"result":{"tools":[{"name":"echo","description":"Echoes back the input","inputSchema":{"type":"object","properties":{"message":{"type":"string","description":"Message to echo"}},"required":["message"],"additionalProperties":false,"$schema":"http://json-schema.org/draft-07/schema#"}},{"name":"add","description":"Adds two numbers","inputSchema":{"type":"object","properties":{"a":{"type":"number","description":"First number"},"b":{"type":"number","description":"Second number"}},"required":["a","b"],"additionalProperties":false,"$schema":"http://json-schema.org/draft-07/schema#"}},{"name":"longRunningOperation","description":"Demonstrates a long running operation with progress updates","inputSchema":{"type":"object","properties":{"duration":{"type":"number","default":10,"description":"Duration of the operation in seconds"},"steps":{"type":"number","default":5,"description":"Number of steps in the operation"}},"additionalProperties":false,"$schema":"http://json-schema.org/draft-07/schema#"}},{"name":"printEnv","description":"Prints all environment variables, helpful for debugging MCP server configuration","inputSchema":{"type":"object","properties":{},"additionalProperties":false,"$schema":"http://json-schema.org/draft-07/schema#"}},{"name":"sampleLLM","description":"Samples from an LLM using MCP's sampling feature","inputSchema":{"type":"object","properties":{"prompt":{"type":"string","description":"The prompt to send to the LLM"},"maxTokens":{"type":"number","default":100,"description":"Maximum number of tokens to generate"}},"required":["prompt"],"additionalProperties":false,"$schema":"http://json-schema.org/draft-07/schema#"}},{"name":"getTinyImage","description":"Returns the MCP_TINY_IMAGE","inputSchema":{"type":"object","properties":{},"additionalProperties":false,"$schema":"http://json-schema.org/draft-07/schema#"}},{"name":"annotatedMessage","description":"Demonstrates how annotations can be used to provide metadata about content","inputSchema":{"type":"object","properties":{"messageType":{"type":"string","enum":["error","success","debug"],"description":"Type of message to demonstrate different annotation patterns"},"includeImage":{"type":"boolean","default":false,"description":"Whether to include an example image"}},"required":["messageType"],"additionalProperties":false,"$schema":"http://json-schema.org/draft-07/schema#"}},{"name":"getResourceReference","description":"Returns a resource reference that can be used by MCP clients","inputSchema":{"type":"object","properties":{"resourceId":{"type":"number","minimum":1,"maximum":100,"description":"ID of the resource to reference (1-100)"}},"required":["resourceId"],"additionalProperties":false,"$schema":"http://json-schema.org/draft-07/schema#"}},{"name":"getResourceLinks","description":"Returns multiple resource links that reference different types of resources","inputSchema":{"type":"object","properties":{"count":{"type":"number","minimum":1,"maximum":10,"default":3,"description":"Number of resource links to return (1-10)"}},"additionalProperties":false,"$schema":"http://json-schema.org/draft-07/schema#"}},{"name":"structuredContent","description":"Returns structured content along with an output schema for client data validation","inputSchema":{"type":"object","properties":{"location":{"type":"string","minLength":1,"description":"City name or zip code"}},"required":["location"],"additionalProperties":false,"$schema":"http://json-schema.org/draft-07/schema#"},"outputSchema":{"type":"object","properties":{"temperature":{"type":"number","description":"Temperature in celsius"},"conditions":{"type":"string","description":"Weather conditions description"},"humidity":{"type":"number","description":"Humidity percentage"}},"required":["temperature","conditions","humidity"],"additionalProperties":false,"$schema":"http://json-schema.org/draft-07/schema#"}},{"name":"startElicitation","description":"Demonstrates the Elicitation feature by asking the user to provide information about their favorite color, number, and pets.","inputSchema":{"type":"object","properties":{},"additionalProperties":false,"$schema":"http://json-schema.org/draft-07/schema#"}}]},"jsonrpc":"2.0","id":2}
工具执行:客户端现在可以使用该方法执行工具tools/call。这演示了 MCP 原语的实际应用:发现可用工具后,客户端可以使用适当的参数调用它们。以调用 echo 函数为例,请求内容如下:
响应如下:
{"result":{"content":[{"type":"text","text":"Echo: hi, my name is itsvse"}]},"jsonrpc":"2.0","id":3} 如下图:
(完) |
上一篇:VMware中的Ubuntu虚拟机中实现与windows宿主机的复制粘贴互通下一篇:VMware虚拟机经常性卡死,打开运行一段时间后卡死
|