链载Ai

标题: 深入解析模型上下文协议(MCP) [打印本页]

作者: 链载Ai    时间: 昨天 12:42
标题: 深入解析模型上下文协议(MCP)

ingFang SC", "Microsoft YaHei", sans-serif;font-weight: bold;line-height: 1.5;padding-top: 10px;color: rgb(85, 85, 85);font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">概述

ingFang SC", "Microsoft YaHei", sans-serif;font-size: 18px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">随着 Cursor 等智能编程工具的崛起、Manus 产品的推出,以及 Claude Sonnet 等大语言模型在编程领域能力的显著提升,MCP 逐渐引起了技术社区的广泛关注与重视。本文将深入浅出地介绍 MCP 的核心概念,并结合具体场景探讨其实际应用价值。

ingFang SC", "Microsoft YaHei", sans-serif;font-weight: bold;line-height: 1.5;padding-top: 10px;color: rgb(85, 85, 85);font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">什么是 MCP?

ingFang SC", "Microsoft YaHei", sans-serif;font-size: 18px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">MCP(The Model Context Protocol,模型上下文协议)是一个开放协议,由 Anthropic 在 2024 年 11 月提出。MCP 能够实现 LLM 应用与外部数据源和工具之间的无缝集成。无论是在构建一个 AI 驱动的 IDE、增强聊天界面,还是创建自定义 AI 工作流,MCP 都提供了一种标准化的方式,将 LLM 与所需的上下文连接起来。可以把 MCP 理解为 Agent 世界的 转接头

2456f8e57cfcf4c02f49964bddeba6e1.png

ingFang SC", "Microsoft YaHei", sans-serif;font-weight: bold;line-height: 1.5;padding-top: 10px;color: rgb(85, 85, 85);font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">MCP 整体架构

ingFang SC", "Microsoft YaHei", sans-serif;font-size: 18px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">MCP 遵循 client-server 架构,对于一个 Agent 而言可以连接多个 MCP Server。整体架构包含 4 部分:

81b16e8ec94283df1df07d99ca0eefa9.png

ingFang SC", "Microsoft YaHei", sans-serif;font-weight: bold;line-height: 1.5;margin: 20px 0px 15px;font-size: 1.375em;padding-top: 10px;color: rgb(85, 85, 85);font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">MCP Host

ingFang SC", "Microsoft YaHei", sans-serif;font-size: 18px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">MCP Host 可以是任何需要访问外部数据的 LLM 应用/Agent。它们负责:

MCP Client

每个 MCP Client 负责:

MCP Server

MCP Server 是为 LLM 提供外部数据和上下文的基础构建单元。MCP Server 的关键组成包括:

工具由模型自主控制,而资源和提示模板则由用户进行控制。模型能够根据给定的上下文自动发现并调用所需工具。

MCP Protocols

所有的 MCP Protocol 必须遵循 JSON-RPC 2.0。目前协议定义的类型有:

关于协议的更多内容可以参考:https://spec.modelcontextprotocol.io/specification/2024-11-05/

Function Calling vs MCP

主要区别如下:

Function Calling 由特定的大模型服务提供商(如 OpenAI 的 GPT-4)引入的功能。它允许模型根据输入,生成特定格式的函数调用请求。应用程序接收到该请求后,执行相应的操作,并将结果返回给模型。这种机制使模型能够主动请求外部功能的执行,但并不强制要求使用特定的通信协议或格式。

MCP 是在 OpenAI 的 Function call 和 GPTs 之后提出的,可以说 Function call 为 MCP 提供了灵感和基础功能。MCP 是一种开放协议,旨在通过标准化的接口,实现大型语言模型与外部数据源及工具的无缝集成。它规定了上下文与请求的结构化传递方式,确保消息传递的标准化和一致性。MCP 的设计初衷是通过本地运行服务器来确保用户数据的安全性,避免将敏感信息直接发送给 LLM。

主要区别:






欢迎光临 链载Ai (https://www.lianzai.com/) Powered by Discuz! X3.5