⚡ AI开发者必看:MCP协议如何让你的智能体能力暴涨10倍?

265 阅读37分钟

💡 核心提示:Model Context Protocol (MCP) 被誉为AI世界的"USB-C接口",正在重塑智能体开发范式!


📌 01 核心价值:从"万能插头"到"统一语言"

MCP的核心价值是标准化 。它就像为AI世界设计了一个"万能插头""USB-C接口" ,任何支持该协议的工具或数据源都可以被LLM"即插即用"**。

这彻底改变了以往为每个外部系统编写定制化集成代码的高成本、低效率模式。

更有意义的比喻:MCP实现了AI领域的"书同文,车同轨",为碎片化的工具和数据建立了一套统一的"交互语言"


📌 02 架构精髓:清晰的客户端-服务器模型

MCP采用经典的客户端-服务器(Client-Server)架构,这使得职责分离非常清晰:

🎯 MCP服务器(Server)

充当外部资源和工具的**"网关"**。它将特定的能力(如查询数据库、调用API、操作文件系统)封装成标准的:

  • "工具(Tools)"
  • "资源(Resources)"
  • "提示(Prompts)"

并暴露出来。

🎯 MCP客户端(Client)

通常是承载LLM的应用程序或智能体(Agent) 。它负责:

  1. 发现可用的MCP服务器
  2. 将LLM的意图转换为标准的MCP请求发送给服务器

架构优势:LLM(作为"大脑")无需关心工具的具体实现,只需通过客户端发出指令,实现了高度的解耦和可扩展性


📌 03 与传统函数调用的关键区别

文档清晰地对比了MCP与常见的**"工具函数调用(Tool Function Calling)"**:

对比维度传统函数调用MCP协议
开放性🔒 专有且封闭🔓 开放且可发现
比喻固定品牌的螺丝刀标准插座,任何电器都能接入
动态性静态配置动态发现和使用能力

🚀 关键飞跃:这是构建能够适应复杂、多变环境的智能体(Agent)的关键。


🛠️ 实践心得:MCP如何重塑我们的开发范式

结合文档中的实践指南和业界动态,MCP带来的不仅是技术规范,更是开发思维和工作流的革新


📌 01 降低集成复杂度,提升开发效率

文档提供的**"三步搭建MCP Agent"等实例表明,MCP能极大降低AI应用的开发门槛**。

开发者不再需要为每个数据源或API编写冗长的适配代码,只需专注于构建或利用现有的MCP服务器。

💭 开发范式升级:这就像从手工作坊升级到标准化流水线,让我们能更快地构建出功能强大的AI应用。

🏢 行业印证:腾讯云、百度等大厂纷纷推出集成MCP的平台服务,印证了这已成为降低开发成本的主流方向


📌 02 智能体(Agent)能力边界的巨大拓展

MCP使得AI智能体从**"对话大脑"真正进化为"行动手脚"**。通过MCP,智能体可以:

🌟 访问实时数据

直接查询数据库或调用API获取最新信息,克服LLM知识滞后的瓶颈

🌟 执行复杂操作

控制软件工具、甚至物联网设备,完成从数据分析到实际操作的闭环

🌟 orchestrate 复杂工作流

将多个MCP工具组合起来,自动完成多步骤任务,如:

查询数据 → 生成报告 → 发送邮件

🔥 垂直领域突破:这在实验室检测、钢铁产业等领域已展现出巨大潜力,实现了从**"单点智能""全域智能"**的跨越。


📌 03 技术选型的新思考:MCP vs. API

⚠️ 理性看待:虽然MCP优势明显,但文档也提醒我们需要理性看待

🤔 场景适用性

对于功能固定、简单的应用,传统的API或函数调用可能仍是更轻量的方案

🤔 生态成熟度

MCP生态仍在快速发展中,某些领域的官方或高质量MCP服务器可能还比较缺乏。

🤔 安全与性能

将内部能力暴露为MCP服务,需要:

  • 仔细设计认证、授权和错误处理机制
  • 考虑网络延迟等性能影响

🔮 总结与展望

总而言之,这篇文档系统地阐述了MCP作为下一代AI智能体基础协议的原理、价值和实践方法。

对于技术人员而言,掌握MCP意味着掌握了构建更强大、更灵活AI应用的关键钥匙

🎯 不仅仅是技术协议,更代表了一种通过标准化和模块化来构建复杂系统的设计哲学


🚀 行业布局

目前,行业巨头如:

  • OpenAI
  • 谷歌
  • 百度
  • 腾讯

均已积极布局MCP生态,这充分表明了其成为AI基础设施的潜力。


📋 行动建议

建议技术团队可以开始:

1️⃣ 学习与实验

利用ADK、FastMCP等工具上手实践,体验MCP的工作流程。

2️⃣ 评估现有系统

思考哪些内部工具或API可以封装成MCP服务器,以提升复用性和智能化水平

3️⃣ 保持关注

紧密跟踪MCP协议和开源生态的发展,适时将成熟方案引入生产环境


🌟 写在最后:MCP正推动AI开发进入一个**"即插即用"的新时代,理解和拥抱这一趋势,将帮助我们在构建真正智能的应用程序时占据先机**!


💬 互动话题

你觉得在你的工作场景中,哪些工具或系统最适合封装成MCP服务?欢迎在评论区分享你的想法!


Chapter 10: Model Context Protocol | 第10章:模型上下文协议

The Model Context Protocol (MCP) is an open protocol that standardizes how applications provide context and tools to LLMs. It enables seamless integration between LLM applications and various data sources, tools, and services through a universal client-server architecture. This protocol facilitates the creation of sophisticated AI agents that can interact with external systems efficiently.

模型上下文协议(MCP) 是一个开放协议,它标准化了应用程序如何向LLM提供上下文工具。它通过通用客户端-服务器架构实现LLM应用程序与各种数据源工具服务之间的无缝集成。该协议促进了能够与外部系统高效交互的复杂AI智能体的创建。

MCP Pattern Overview | MCP模式概述

Imagine a universal adapter that allows any LLM to plug into any external system, database, or tool without a custom integration for each one. That's essentially what the Model Context Protocol (MCP) is. It's an open standard designed to standardize how LLMs like Gemini, OpenAI's GPT models, Mixtral, and Claude communicate with external applications, data sources, and tools. Think of it as a universal connection mechanism that simplifies how LLMs obtain context, execute actions, and interact with various systems.

想象一下,有一个通用适配器允许任何LLM插入任何外部系统数据库工具,而无需为每个系统进行自定义集成。这本质上就是模型上下文协议(MCP) 。它是一个开放标准,旨在标准化GeminiOpenAI的GPT模型MixtralClaudeLLM外部应用程序数据源工具通信的方式。可以将其视为简化LLM获取上下文、执行操作和与各种系统交互的通用连接机制

MCP operates on a client-server architecture. It defines how different elements—data (referred to as resources), interactive templates (which are essentially prompts), and actionable functions (known as tools)—are exposed by an MCP server. These are then consumed by an MCP client, which could be an LLM host application or an AI agent itself. This standardized approach dramatically reduces the complexity of integrating LLMs into diverse operational environments.

MCP在客户端-服务器架构上运行。它定义了不同元素——数据(称为资源)、交互式模板(本质上是提示)和可操作函数(称为工具)——如何由MCP服务器公开。然后这些被MCP客户端消费,该客户端可以是LLM宿主应用程序AI智能体本身。这种标准化方法极大地降低了将LLM集成到不同操作环境中的复杂性。

However, MCP is a contract for an "agentic interface," and its effectiveness depends heavily on the design of the underlying APIs it exposes. There is a risk that developers simply wrap pre-existing, legacy APIs without modification, which can be suboptimal for an agent. For example, if a ticketing system's API only allows retrieving full ticket details one by one, an agent asked to summarize high-priority tickets will be slow and inaccurate at high volumes. To be truly effective, the underlying API should be improved with deterministic features like filtering and sorting to help the non-deterministic agent work efficiently. This highlights that agents do not magically replace deterministic workflows; they often require stronger deterministic support to succeed.

然而,MCP是一个"智能体接口"的契约,其有效性在很大程度上取决于其公开的基础API的设计。存在一种风险,即开发者简单地包装预先存在的遗留API而不进行修改,这可能对智能体来说是次优的。例如,如果票务系统的API仅允许逐个检索完整的票务详细信息,那么被要求总结高优先级票务的智能体在高容量时将缓慢且不准确。为了真正有效,基础API应该通过过滤排序确定性功能进行改进,以帮助非确定性智能体高效工作。这突出了智能体不会神奇地替代确定性工作流;它们通常需要更强的确定性支持才能成功。

Furthermore, MCP can wrap an API whose input or output is still not inherently understandable by the agent. An API is only useful if its data format is agent-friendly, a guarantee that MCP itself does not enforce. For instance, creating an MCP server for a document store that returns files as PDFs is mostly useless if the consuming agent cannot parse PDF content. The better approach would be to first create an API that returns a textual version of the document, such as Markdown, which the agent can actually read and process. This demonstrates that developers must consider not just the connection, but the nature of the data being exchanged to ensure true compatibility.

此外,MCP可以包装一个其输入或输出仍然不能被智能体固有理解的API。只有当API数据格式智能体友好时,它才有用,而MCP本身并不保证这一点。例如,如果消费智能体无法解析PDF内容,则为返回PDF格式文件的文档存储创建MCP服务器基本上是无用的。更好的方法是首先创建一个返回文档文本版本API,例如Markdown智能体实际上可以读取和处理该版本。这表明开发者必须考虑的不仅仅是连接,还要考虑被交换数据的性质,以确保真正的兼容性

MCP vs. Tool Function Calling | MCP与工具函数调用

The Model Context Protocol (MCP) and tool function calling are distinct mechanisms that enable LLMs to interact with external capabilities (including tools) and execute actions. While both serve to extend LLM capabilities beyond text generation, they differ in their approach and level of abstraction.

模型上下文协议(MCP)工具函数调用是不同的机制,它们使LLM能够与外部功能(包括工具)交互并执行操作。虽然两者都用于将LLM能力扩展到文本生成之外,但它们在方法抽象级别上有所不同。

Tool function calling can be thought of as a direct request from an LLM to a specific, pre-defined tool or function. Note that in this context we use the words "tool" and "function" interchangeably. This interaction is characterized by a one-to-one communication model, where the LLM formats a request based on its understanding of a user's intent requiring external action. The application code then executes this request and returns the result to the LLM. This process is often proprietary and varies across different LLM providers.

工具函数调用可以被视为LLM特定预定义工具函数直接请求。请注意,在这种情况下,我们互换使用"工具"和"函数"这两个词。这种交互的特征在于一对一通信模型,其中LLM根据其对外部操作所需用户意图的理解来格式化请求。然后应用程序代码执行此请求并将结果返回给LLM。此过程通常是专有的,并且在不同的LLM提供商之间有所不同。

In contrast, the Model Context Protocol (MCP) operates as a standardized interface for LLMs to discover, communicate with, and utilize external capabilities. It functions as an open protocol that facilitates interaction with a wide range of tools and systems, aiming to establish an ecosystem where any compliant tool can be accessed by any compliant LLM. This fosters interoperability, composability and reusability across different systems and implementations. By adopting a federated model, we significantly improve interoperability and unlock the value of existing assets. This strategy allows us to bring disparate and legacy services into a modern ecosystem simply by wrapping them in an MCP-compliant interface. These services continue to operate independently, but can now be composed into new applications and workflows, with their collaboration orchestrated by LLMs. This fosters agility and reusability without requiring costly rewrites of foundational systems.

相比之下,模型上下文协议(MCP) 作为LLM发现、通信和利用外部功能标准化接口运作。它作为一个开放协议,促进与各种工具和系统的交互,旨在建立一个生态系统,其中任何兼容的工具都可以被任何兼容的LLM访问。这促进了不同系统和实现之间的互操作性可组合性可重用性。通过采用联合模型,我们显著改善了互操作性并释放了现有资产的价值。此策略允许我们通过将它们包装在符合MCP的接口中,将不同的遗留服务带入现代生态系统。这些服务继续独立运行,但现在可以被组合到新的应用程序和工作流中,它们的协作由LLM编排。这促进了敏捷性可重用性,而无需对基础系统进行昂贵的重写

Here's a breakdown of the fundamental distinctions between MCP and tool function calling:

以下是MCP与工具函数调用之间基本区别的细分:

FeatureTool Function CallingModel Context Protocol (MCP)
StandardizationProprietary and vendor-specific. The format and implementation differ across LLM providers.An open, standardized protocol, promoting interoperability between different LLMs and tools.
标准化专有且特定于供应商。格式和实现在不同的LLM提供商之间有所不同。一个开放的、标准化的协议,促进不同LLM和工具之间的互操作性。
ScopeA direct mechanism for an LLM to request the execution of a specific, predefined function.A broader framework for how LLMs and external tools discover and communicate with each other.
范围LLM请求执行特定预定义函数的直接机制。LLM与外部工具如何发现和相互通信的更广泛框架。
ArchitectureA one-to-one interaction between the LLM and the application's tool-handling logic.A client-server architecture where LLM-powered applications (clients) can connect to and utilize various MCP servers (tools).
架构LLM与应用程序的工具处理逻辑之间的一对一交互。客户端-服务器架构,其中LLM驱动的应用程序(客户端)可以连接和利用各种MCP服务器(工具)。
DiscoveryThe LLM is explicitly told which tools are available within the context of a specific conversation.Enables dynamic discovery of available tools. An MCP client can query a server to see what capabilities it offers.
发现在特定对话的上下文中明确告知LLM哪些工具可用。支持动态发现可用工具。MCP客户端可以查询服务器以查看其提供的功能。
ReusabilityTool integrations are often tightly coupled with the specific application and LLM being used.Promotes the development of reusable, standalone "MCP servers" that can be accessed by any compliant application.
可重用性工具集成通常与正在使用的特定应用程序和LLM紧密耦合。促进可重用、独立的"MCP服务器"的开发,任何兼容的应用程序都可以访问。

Think of tool function calling as giving an AI a specific set of custom-built tools, like a particular wrench and screwdriver. This is efficient for a workshop with a fixed set of tasks. MCP (Model Context Protocol) , on the other hand, is like creating a universal, standardized power outlet system. It doesn't provide the tools itself, but it allows any compliant tool from any manufacturer to plug in and work, enabling a dynamic and ever-expanding workshop.

工具函数调用视为为AI提供一组特定的自定义工具,就像特定的扳手螺丝刀。这对于具有固定任务集工作坊来说是高效的。另一方面,MCP(模型上下文协议) 就像创建一个通用标准化的电源插座系统。它本身不提供工具,但它允许来自任何制造商的任何兼容工具插入并工作,从而实现动态不断扩展的工作坊

In short, function calling provides direct access to a few specific functions, while MCP is the standardized communication framework that lets LLMs discover and use a vast range of external resources. For simple applications, specific tools are enough; for complex, interconnected AI systems that need to adapt, a universal standard like MCP is essential.

简而言之,函数调用提供对少数特定函数直接访问,而MCP是让LLM发现和使用大量外部资源标准化通信框架。对于简单应用程序特定工具就足够了;对于需要适应复杂互连的AI系统,像MCP这样的通用标准是必不可少的。

Additional considerations for MCP | MCP的其他考虑因素

MCP is an open protocol that standardizes how applications provide context, tools, and prompts to LLMs. It uses a universal client-server architecture, where each server exposes a specific capability through a standardized interface.

MCP是一个开放协议,它标准化应用程序如何向LLM提供上下文工具提示。它使用通用的客户端-服务器架构,其中每个服务器通过标准化接口公开特定功能

While MCP presents a powerful framework, a thorough evaluation requires considering several crucial aspects that influence its suitability for a given use case. Let's see some aspects in more details:

虽然MCP提供了一个强大的框架,但全面评估需要考虑影响其是否适合特定用例的几个关键方面。让我们更详细地了解一些方面

  • Tool vs. Resource vs. Prompt: It's important to understand the specific roles of these components. A resource is static data (e.g., a PDF file, a database record). A tool is an executable function that performs an action (e.g., sending an email, querying an API). A prompt is a template that guides the LLM in how to interact with a resource or tool, ensuring the interaction is structured and effective.
  • 工具与资源与提示:理解这些组件特定角色很重要。资源静态数据(例如,PDF文件数据库记录)。工具执行操作可执行函数(例如,发送电子邮件查询API)。提示指导LLM如何与资源工具交互的模板,确保交互结构化有效的。
  • Discoverability: A key advantage of MCP is that an MCP client can dynamically query a server to learn what tools and resources it offers. This "just-in-time" discovery mechanism is powerful for agents that need to adapt to new capabilities without being redeployed.
  • 可发现性MCP的一个关键优势MCP客户端可以动态查询服务器以了解其提供的工具资源。这种**"即时"发现机制对于需要适应功能而无需重新部署智能体来说非常强大**。
  • Security: Exposing tools and data via any protocol requires robust security measures. An MCP implementation must include authentication and authorization to control which clients can access which servers and what specific actions they are permitted to perform.
  • 安全性:通过任何协议公开工具和数据都需要强大的安全措施MCP实现必须包括身份验证授权,以控制哪些客户端可以访问哪些服务器以及它们被允许执行哪些特定操作
  • Implementation: While MCP is an open standard, its implementation can be complex. However, providers are beginning to simplify this process. For example, some model providers like Anthropic or FastMCP offer SDKs that abstract away much of the boilerplate code, making it easier for developers to create and connect MCP clients and servers.
  • 实现:虽然MCP是一个开放标准,但其实现可能很复杂。然而,提供商开始简化这一过程。例如,一些模型提供商AnthropicFastMCP提供SDK,这些SDK抽象了许多样板代码,使开发者更容易创建连接****MCP客户端服务器
  • Error Handling: A comprehensive error-handling strategy is critical. The protocol must define how errors (e.g., tool execution failure, unavailable server, invalid request) are communicated back to the LLM so it can understand the failure and potentially try an alternative approach.
  • 错误处理全面的错误处理策略至关重要的协议必须定义如何将错误(例如,工具执行失败服务器不可用无效请求传达回****LLM,以便它能够理解失败并可能尝试替代方法
  • Local vs. Remote Server: MCP servers can be deployed locally on the same machine as the agent or remotely on a different server. A local server might be chosen for speed and security with sensitive data, while a remote server architecture allows for shared, scalable access to common tools across an organization.
  • 本地与远程服务器MCP服务器可以部署在本地在与智能体相同的机器上,也可以远程部署在不同的服务器上。本地服务器可能因其处理敏感数据时的速度安全性而被选择,而远程服务器架构允许在整个组织共享可扩展地访问通用工具
  • On-demand vs. Batch: MCP can support both on-demand, interactive sessions and larger-scale batch processing. The choice depends on the application, from a real-time conversational agent needing immediate tool access to a data analysis pipeline that processes records in batches.
  • 按需与批量MCP可以支持按需交互会话大规模批量处理选择取决于应用程序,从需要即时工具访问实时对话智能体批量处理记录数据分析管道
  • Transportation Mechanism: The protocol also defines the underlying transport layers for communication. For local interactions, it uses JSON-RPC over STDIO (standard input/output) for efficient inter-process communication. For remote connections, it leverages web-friendly protocols like Streamable HTTP and Server-Sent Events (SSE) to enable persistent and efficient client-server communication.
  • 传输机制协议还定义了通信底层传输层。对于本地交互,它使用基于STDIO(标准输入/输出)的JSON-RPC进行高效的进程间通信。对于远程连接,它利用Streamable HTTP服务器发送事件(SSE)网络友好协议,实现持久高效的客户端-服务器通信

The Model Context Protocol uses a client-server model to standardize information flow. Understanding component interaction is key to MCP's advanced agentic behavior:

模型上下文协议使用客户端-服务器模型标准化信息流理解组件交互MCP高级智能体行为关键

  1. Large Language Model (LLM) : The core intelligence. It processes user requests, formulates plans, and decides when it needs to access external information or perform an action.
  2. 大语言模型(LLM)核心智能。它处理用户请求制定计划,并决定何时需要访问外部信息执行操作
  3. MCP Client: This is an application or wrapper around the LLM. It acts as the intermediary, translating the LLM's intent into a formal request that conforms to the MCP standard. It is responsible for discovering, connecting to, and communicating with MCP Servers.
  4. MCP客户端:这是LLM周围的应用程序包装器。它充当中介,将LLM的意图转换为符合MCP标准的正式请求。它负责发现、连接和与MCP服务器通信。
  5. MCP Server: This is the gateway to the external world. It exposes a set of tools, resources, and prompts to any authorized MCP Client. Each server is typically responsible for a specific domain, such as a connection to a company's internal database, an email service, or a public API.
  6. MCP服务器:这是通往外部世界网关。它向任何授权的MCP客户端公开一组工具资源提示。每个服务器通常负责特定,例如与公司内部数据库电子邮件服务公共API连接
  7. ​​Optional Third-Party (3P) Service: This represents the actual external tool, application, or data source that the MCP Server manages and exposes. It is the ultimate endpoint that performs the requested action, such as querying a proprietary database, interacting with a SaaS platform, or calling a public weather API.
  8. 可选的第三方(3P)服务:这代表MCP服务器管理和公开实际外部工具应用程序数据源。它是执行请求操作最终端点,例如查询专有数据库与SaaS平台交互调用公共天气API

The interaction flows as follows:

交互流程如下:

  1. Discovery: The MCP Client, on behalf of the LLM, queries an MCP Server to ask what capabilities it offers. The server responds with a manifest listing its available tools (e.g., send_email), resources (e.g., customer_database), and prompts.
  2. 发现MCP客户端代表LLM查询MCP服务器以了解其提供的功能服务器清单形式响应,列出其可用工具(例如,send_email)、资源(例如,customer_database)和提示
  3. Request Formulation: The LLM determines that it needs to use one of the discovered tools. For instance, it decides to send an email. It formulates a request, specifying the tool to use (send_email) and the necessary parameters (recipient, subject, body).
  4. 请求制定LLM确定需要使用其中一个发现的工具。例如,它决定发送电子邮件。它制定请求指定要使用的工具send_email)和必要的参数收件人主题正文)。
  5. Client Communication: The MCP Client takes the LLM's formulated request and sends it as a standardized call to the appropriate MCP Server.
  6. 客户端通信MCP客户端获取LLM制定的请求,并将其作为标准化调用发送到相应的MCP服务器
  7. Server Execution: The MCP Server receives the request. It authenticates the client, validates the request, and then executes the specified action by interfacing with the underlying software (e.g., calling the send() function of an email API).
  8. 服务器执行MCP服务器接收请求。它对客户端进行身份验证验证请求,然后通过与支持软件交互执行指定的操作(例如,调用电子邮件APIsend()函数)。
  9. Response and Context Update: After execution, the MCP Server sends a standardized response back to the MCP Client. This response indicates whether the action was successful and includes any relevant output (e.g., a confirmation ID for the sent email). The client then passes this result back to the LLM, updating its context and enabling it to proceed with the next step of its task.
  10. 响应和上下文更新执行后,MCP服务器MCP客户端发送标准化响应。此响应指示操作是否成功,并包括任何相关输出(例如,发送电子邮件确认ID)。客户端然后将此结果传递回LLM更新其上下文使其能够继续执行任务的下一步

Practical Applications & Use Cases | 实际应用和用例

MCP显著扩展AI/LLM的能力,使它们更加多功能强大。以下是九个关键用例

MCP显著扩展AI/LLM的能力,使它们更加多功能强大。以下是九个关键用例

  • Database Integration: MCP allows LLMs and agents to seamlessly access and interact with structured data in databases. For instance, using the MCP Toolbox for Databases, an agent can query Google BigQuery datasets to retrieve real-time information, generate reports, or update records, all driven by natural language commands.
  • 数据库集成MCP允许LLM和智能体无缝访问和与数据库中的结构化数据交互。例如,使用数据库MCP工具箱智能体可以查询Google BigQuery数据集检索实时信息生成报告更新记录,所有这些都由自然语言命令驱动。
  • Generative Media Orchestration: MCP enables agents to integrate with advanced generative media services. Through MCP Tools for Genmedia Services, an agent can orchestrate workflows involving Google's Imagen for image generation, Google's Veo for video creation, Google's Chirp 3 HD for realistic voices, or Google's Lyria for music composition, allowing for dynamic content creation within AI applications.
  • 生成媒体编排MCP使智能体能够与先进的生成媒体服务集成。通过生成媒体服务的MCP工具智能体可以编排工作流,涉及Google Imagen进行图像生成Google Veo进行视频创建Google Chirp 3 HD进行逼真语音Google Lyria进行音乐合成,实现AI应用程序中的动态内容创建
  • External API Interaction: MCP provides a standardized way for LLMs to call and receive responses from any external API. This means an agent can fetch live weather data, pull stock prices, send emails, or interact with CRM systems, extending its capabilities far beyond its core language model.
  • 外部API交互:MCP为LLM调用和接收来自任何外部API的响应提供了标准化方式。这意味着智能体可以获取实时天气数据、提取股票价格、发送电子邮件或与CRM系统交互,将其能力远远扩展到核心语言模型之外。
  • Reasoning-Based Information Extraction: Leveraging an LLM's strong reasoning skills, MCP facilitates effective, query-dependent information extraction that surpasses conventional search and retrieval systems. Instead of a traditional search tool returning an entire document, an agent can analyze the text and extract the precise clause, figure, or statement that directly answers a user's complex question.
  • 基于推理的信息提取:利用LLM强大的推理能力,MCP促进有效的、依赖查询的信息提取,超越传统的搜索和检索系统。智能体可以分析文本并提取直接回答用户复杂问题的精确条款、数字或陈述,而不是让传统搜索工具返回整个文档。
  • Custom Tool Development: Developers can build custom tools and expose them via an MCP server (e.g., using FastMCP). This allows specialized internal functions or proprietary systems to be made available to LLMs and other agents in a standardized, easily consumable format, without needing to modify the LLM directly.
  • 自定义工具开发:开发者可以构建自定义工具并通过MCP服务器公开它们(例如,使用FastMCP)。这使得专门的内部功能或专有系统能够以标准化、易于消费的格式提供给LLM和其他智能体,而无需直接修改LLM。
  • Standardized LLM-to-Application Communication: MCP ensures a consistent communication layer between LLMs and the applications they interact with. This reduces integration overhead, promotes interoperability between different LLM providers and host applications, and simplifies the development of complex agentic systems.
  • 标准化LLM到应用程序的通信:MCP确保LLM与它们交互的应用程序之间的一致通信层。这减少了集成开销,促进了不同LLM提供商和宿主应用程序之间的互操作性,并简化了复杂智能体系统的开发。
  • Complex Workflow Orchestration: By combining various MCP-exposed tools and data sources, agents can orchestrate highly complex, multi-step workflows. An agent could, for example, retrieve customer data from a database, generate a personalized marketing image, draft a tailored email, and then send it, all by interacting with different MCP services.
  • 复杂工作流编排:通过组合各种MCP公开的工具和数据源,智能体可以编排高度复杂的多步骤工作流。例如,智能体可以从数据库检索客户数据、生成个性化营销图像、起草定制电子邮件,然后发送它,所有这些都通过与不同的MCP服务交互完成。
  • IoT Device Control: MCP can facilitate LLM interaction with Internet of Things (IoT) devices. An agent could use MCP to send commands to smart home appliances, industrial sensors, or robotics, enabling natural language control and automation of physical systems.
  • 物联网设备控制:MCP可以促进LLM与物联网(IoT)设备的交互。智能体可以使用MCP向智能家居设备、工业传感器或机器人发送命令,实现物理系统的自然语言控制和自动化。
  • Financial Services Automation: In financial services, MCP could enable LLMs to interact with various financial data sources, trading platforms, or compliance systems. An agent might analyze market data, execute trades, generate personalized financial advice, or automate regulatory reporting, all while maintaining secure and standardized communication.
  • 金融服务自动化:在金融服务中,MCP可以使LLM与各种金融数据源、交易平台或合规系统进行交互。智能体可以分析市场数据、执行交易、生成个性化金融建议或自动化监管报告,同时保持安全和标准化的通信。

In short, the Model Context Protocol (MCP) enables agents to access real-time information from databases, APIs, and web resources. It also allows agents to perform actions like sending emails, updating records, controlling devices, and executing complex tasks by integrating and processing data from various sources. Additionally, MCP supports media generation tools for AI applications.

简而言之,模型上下文协议(MCP)使智能体能够访问来自数据库、API和网络资源的实时信息。它还允许智能体执行发送电子邮件、更新记录、控制设备和执行复杂任务等操作,通过集成和处理来自各种来源的数据。此外,MCP还支持AI应用程序的媒体生成工具。

Hands-On Code Example with ADK | 使用ADK的实际代码示例

This section outlines how to connect to a local MCP server that provides file system operations, enabling an ADK agent to interact with the local file system.

本节概述如何连接到提供文件系统操作的本地MCP服务器,使ADK智能体能够与本地文件系统交互。

Agent Setup with MCPToolset | 使用MCPToolset设置智能体

To configure an agent for file system interaction, an agent.py file must be created (e.g., at ./adk_agent_samples/mcp_agent/agent.py). The MCPToolset is instantiated within the tools list of the LlmAgent object. It is crucial to replace "/path/to/your/folder" in the args list with the absolute path to a directory on the local system that the MCP server can access. This directory will be the root for the file system operations performed by the agent.

要配置用于文件系统交互的智能体,必须创建一个agent.py文件(例如,在./adk_agent_samples/mcp_agent/agent.py)。MCPToolsetLlmAgent对象的tools列表中实例化。关键是将args列表中的"/path/to/your/folder"替换为MCP服务器可以访问的本地系统上的目录的绝对路径。该目录将作为智能体执行的文件系统操作的根目录。

import os
from google.adk.agents import LlmAgent
from google.adk.tools.mcp_tool.mcp_toolset import MCPToolset, StdioServerParameters

# Create a reliable absolute path to a folder named 'mcp_managed_files'
# within the same directory as this agent script.
# This ensures the agent works out-of-the-box for demonstration.
# For production, you would point this to a more persistent and secure location.
TARGET_FOLDER_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "mcp_managed_files")

# Ensure the target directory exists before the agent needs it.
os.makedirs(TARGET_FOLDER_PATH, exist_ok=True)

root_agent = LlmAgent(
    model='gemini-2.0-flash',
    name='filesystem_assistant_agent',
    instruction=(
        'Help the user manage their files. You can list files, read files, and write files. '
        f'You are operating in the following directory: {TARGET_FOLDER_PATH}'
    ),
    tools=[
        MCPToolset(
            connection_params=StdioServerParameters(
                command='npx',
                args=[
                    "-y",  # Argument for npx to auto-confirm install
                    "@modelcontextprotocol/server-filesystem",
                    # This MUST be an absolute path to a folder.
                    TARGET_FOLDER_PATH,
                ],
            ),
            # Optional: You can filter which tools from the MCP server are exposed.
            # For example, to only allow reading:
            # tool_filter=['list_directory', 'read_file']
        )
    ],
)

npx (Node Package Execute), bundled with npm (Node Package Manager) versions 5.2.0 and later, is a utility that enables direct execution of Node.js packages from the npm registry. This eliminates the need for global installation. In essence, npx serves as an npm package runner, and it is commonly used to run many community MCP servers, which are distributed as Node.js packages.

npx(节点包执行器),与npm(节点包管理器)5.2.0及更高版本捆绑,是一个支持从npm注册表直接执行Node.js包的实用程序。这消除了全局安装的需要。本质上,npx充当npm包运行器,通常用于运行许多作为Node.js包分发的社区MCP服务器。

Creating an __init__.py file is necessary to ensure the agent.py file is recognized as part of a discoverable Python package for the Agent Development Kit (ADK). This file should reside in the same directory as agent.py.

创建__init__.py文件是确保agent.py文件被识别为Agent开发工具包(ADK)可发现Python包的一部分所必需的。此文件应与agent.py位于同一目录中。

__init__.py
# This file makes the directory a Python package for ADK

Certainly, other supported commands are available for use. For example, connecting to python3 can be achieved as follows:

当然,其他支持的命令也可供使用。例如,连接到python3可以通过以下方式实现:

connection_params = StdioConnectionParams(
    server_params={
        "command""python3",
        "args": ["./agent/mcp_server.py"],
        "env": {
            "SERVICE_ACCOUNT_PATH": SERVICE_ACCOUNT_PATH,
            "DRIVE_FOLDER_ID": DRIVE_FOLDER_ID
        }
    }
)

UVX, in the context of Python, refers to a command-line tool that utilizes uv to execute commands in a temporary, isolated Python environment. Essentially, it allows you to run Python tools and packages without needing to install them globally or within your project's environment. You can run it via the MCP server.

在Python环境中,UVX指的是利用uv在临时、隔离的Python环境中执行命令的命令行工具。本质上,它允许您运行Python工具和包,而无需在全局或项目环境中安装它们。您可以通过MCP服务器运行它。

connection_params = StdioConnectionParams(
    server_params={
        "command""uvx",
        "args": ["mcp-google-sheets@latest"],
        "env": {
            "SERVICE_ACCOUNT_PATH": SERVICE_ACCOUNT_PATH,
            "DRIVE_FOLDER_ID": DRIVE_FOLDER_ID
        }
    }
)

Once the MCP Server is created, the next step is to connect to it.

创建MCP服务器后,下一步是连接到它。

Connecting the MCP Server with ADK Web | 使用ADK Web连接MCP服务器

To begin, execute 'adk web'. Navigate to the parent directory of mcp_agent (e.g., adk_agent_samples) in your terminal and run:

首先,执行'adk web'。在终端中导航到mcp_agent的父目录(例如,adk_agent_samples)并运行:

adk web

Once the ADK Web UI has loaded in your browser, select the filesystem_assistant_agent from the agent menu. Next, experiment with prompts such as:

ADK Web UI在浏览器中加载后,从智能体菜单中选择filesystem_assistant_agent。接下来,尝试使用以下提示:

  • "Show me the contents of this folder." | "显示此文件夹的内容。"
  • "Read the sample.txt file." (This assumes sample.txt is located at TARGET_FOLDER_PATH.) | "读取sample.txt文件。"(这假设sample.txt位于TARGET_FOLDER_PATH。)
  • "What's in another_file.md?" | "another_file.md中有什么?"

Creating an MCP Server with FastMCP | 使用FastMCP创建MCP服务器

FastMCP is a high-level Python framework designed to streamline the development of MCP servers. It provides an abstraction layer that simplifies protocol complexities, allowing developers to focus on core logic.

FastMCP是一个高级Python框架,旨在简化MCP服务器的开发。它提供了一个抽象层,简化了协议复杂性,使开发者能够专注于核心逻辑。

The library enables rapid definition of tools, resources, and prompts using simple Python decorators. A significant advantage is its automatic schema generation, which intelligently interprets Python function signatures, type hints, and documentation strings to construct necessary AI model interface specifications. This automation minimizes manual configuration and reduces human error.

该库支持使用简单的Python装饰器快速定义工具、资源和提示。一个重要优势是其自动模式生成,智能地解释Python函数签名、类型提示和文档字符串以构建必要的AI模型接口规范。这种自动化最小化了手动配置并减少了人为错误。

Beyond basic tool creation, FastMCP facilitates advanced architectural patterns like server composition and proxying. This enables modular development of complex, multi-component systems and seamless integration of existing services into an AI-accessible framework. Additionally, FastMCP includes optimizations for efficient, distributed, and scalable AI-driven applications.

除了基本工具创建之外,FastMCP还支持服务器组合和代理等高级架构模式。这使得复杂多组件系统的模块化开发成为可能,并无缝集成现有服务到AI可访问框架中。此外,FastMCP还包括针对高效、分布式和可扩展AI驱动应用程序的优化。

Server setup with FastMCP

To illustrate, consider a basic "greet" tool provided by the server. ADK agents and other MCP clients can interact with this tool using HTTP once it is active.

# fastmcp_server.py
# This script demonstrates how to create a simple MCP server using FastMCP.
# It exposes a single tool that generates a greeting.

# 1. Make sure you have FastMCP installed:
# pip install fastmcp

from fastmcp import FastMCP, Client

# Initialize the FastMCP server.
mcp_server = FastMCP()

# Define a simple tool function.
# The `@mcp_server.tool` decorator registers this Python function as an MCP tool.
# The docstring becomes the tool's description for the LLM.
@mcp_server.tool
def greet(name: str) -> str:
    """
    Generates a personalized greeting.
    
    Args:
        name: The name of the person to greet.
    
    Returns:
        A greeting string.
    """
    return f"Hello, {name}! Nice to meet you."

# Or if you want to run it from the script:
if __name__ == "__main__":
    mcp_server.run(
        transport="http",
        host="127.0.0.1",
        port=8000
    )

This Python script defines a single function called greet, which takes a person's name and returns a personalized greeting. 这个Python脚本定义了一个名为greet的函数,它接受一个人的名字并返回个性化的问候。

The @tool() decorator above this function automatically registers it as a tool that an AI or another program can use. 函数上方的@tool()装饰器自动将其注册为AI或其他程序可以使用的工具。

The function's documentation string and type hints are used by FastMCP to tell the Agent how the tool works, what inputs it needs, and what it will return. 函数的文档字符串和类型提示被FastMCP用来告诉智能体该工具如何工作、需要什么输入以及将返回什么。

When the script is executed, it starts the FastMCP server, which listens for requests on localhost:8000. 当脚本被执行时,它启动FastMCP服务器,该服务器监听localhost:8000上的请求。

This makes the greet function available as a network service. 这使得greet函数作为网络服务可用。

An agent could then be configured to connect to this server and use the greet tool to generate greetings as part of a larger task. 然后可以配置一个智能体连接到该服务器,并使用greet工具生成问候语,作为更大任务的一部分。

The server runs continuously until it is manually stopped. 服务器持续运行,直到被手动停止。

Consuming the FastMCP Server with an ADK Agent

An ADK agent can be set up as an MCP client to use a running FastMCP server. This requires configuring HttpServerParameters with the FastMCP server's network address, which is usually http://localhost:8000.

A tool_filter parameter can be included to restrict the agent's tool usage to specific tools offered by the server, such as 'greet'. When prompted with a request like "Greet John Doe," the agent's embedded LLM identifies the 'greet' tool available via MCP, invokes it with the argument "John Doe," and returns the server's response. This process demonstrates the integration of user-defined tools exposed through MCP with an ADK agent.

To establish this configuration, an agent file (e.g., agent.py located in ./adk_agent_samples/fastmcp_client_agent/) is required. This file will instantiate an ADK agent and use HttpServerParameters to establish a connection with the operational FastMCP server.

# ./adk_agent_samples/fastmcp_client_agent/agent.py

import os
from google.adk.agents import LlmAgent
from google.adk.tools.mcp_tool.mcp_toolset import MCPToolset, HttpServerParameters

# Define the FastMCP server's address.
# Make sure your fastmcp_server.py (defined previously) is running on this port.
FASTMCP_SERVER_URL = "http://localhost:8000"

root_agent = LlmAgent(
    model='gemini-2.0-flash',  # Or your preferred model
    name='fastmcp_greeter_agent',
    instruction='You are a friendly assistant that can greet people by their name. Use the "greet" tool.',
    tools=[
        MCPToolset(
            connection_params=HttpServerParameters(
                url=FASTMCP_SERVER_URL,
            ),
            # Optional: Filter which tools from the MCP server are exposed
            # For this example, we're expecting only 'greet'
            tool_filter=['greet']
        )
    ],
)

The script defines an Agent named fastmcp_greeter_agent that uses a Gemini language model. It's given a specific instruction to act as a friendly assistant whose purpose is to greet people. Crucially, the code equips this agent with a tool to perform its task. It configures an MCPToolset to connect to a separate server running on localhost:8000, which is expected to be the FastMCP server from the previous example. The agent is specifically granted access to the greet tool hosted on that server. In essence, this code sets up the client side of the system, creating an intelligent agent that understands its goal is to greet people and knows exactly which external tool to use to accomplish it.

Creating an init.py file within the fastmcp_client_agent directory is necessary. This ensures the agent is recognized as a discoverable Python package for the ADK.

To begin, open a new terminal and run python fastmcp_server.py to start the FastMCP server. Next, go to the parent directory of fastmcp_client_agent (for example, adk_agent_samples) in your terminal and execute adk web. Once the ADK Web UI loads in your browser, select the fastmcp_greeter_agent from the agent menu. You can then test it by entering a prompt like "Greet John Doe." The agent will use the greet tool on your FastMCP server to create a response.

At a Glance | 概览

What | 是什么

MCP is a standardized protocol that allows LLMs to interact with external tools and data sources through a unified interface.

MCP是一个标准化协议,允许LLM通过统一接口与外部工具和数据源交互。

Why | 为什么

Traditional LLMs are limited to their training data. MCP enables them to access real-time information, perform actions, and integrate with various services, making them more capable and contextually aware.

传统LLM受限于其训练数据。MCP使它们能够访问实时信息、执行操作并与各种服务集成,使其更有能力和具备上下文感知。

Rule of Thumb | 经验法则

Use MCP when you need to connect your AI agent to external systems, databases, APIs, or tools that are not part of the LLM's training data. It's particularly valuable for applications requiring real-time data access or system integration.

当您需要将AI智能体连接到外部系统、数据库、API或不是LLM训练数据部分的工具时,请使用MCP。对于需要实时数据访问或系统集成的应用程序,它特别有价值。

ScreenShot_2025-11-09_202643_697.png

Key Takeaways | 关键要点

  • Standardized Integration: MCP provides a universal protocol for connecting LLMs to external tools and services, eliminating the need for custom integrations for each new service.
  • 标准化集成:MCP提供了连接LLM与外部工具和服务的通用协议,消除了为每个新服务进行自定义集成的需要。
  • Enhanced Capabilities: By enabling access to real-time data and external tools, MCP transforms LLMs from static knowledge repositories into dynamic, action-oriented agents.
  • 增强的能力:通过实现对实时数据和外部工具的访问,MCP将LLM从静态知识库转变为动态的、以行动为导向的智能体。
  • Security and Control: MCP maintains security by acting as a controlled gateway between LLMs and external systems, with proper authentication and authorization mechanisms.
  • 安全性和控制:MCP通过充当LLM与外部系统之间的受控网关来维护安全性,具有适当的身份验证和授权机制。
  • Developer-Friendly: Frameworks like FastMCP simplify MCP server development, making it accessible for developers to create custom integrations.
  • 开发者友好:像FastMCP这样的框架简化了MCP服务器的开发,使开发者能够轻松创建自定义集成。
  • Future-Proof Architecture: As AI systems become more integrated with business processes, MCP provides a scalable foundation for building sophisticated agentic applications.
  • 未来就绪的架构:随着AI系统与业务流程的集成度越来越高,MCP为构建复杂的智能体应用程序提供了可扩展的基础。

Conclusion | 结论

The Model Context Protocol represents a significant advancement in AI system integration. By providing a standardized, secure, and efficient way for LLMs to interact with external tools and data sources, MCP enables the creation of more capable and contextually aware AI applications. Whether you're building a simple file system agent or a complex multi-service orchestration system, MCP provides the foundation for creating truly integrated AI solutions.

模型上下文协议代表了AI系统集成的重大进步。通过为LLM与外部工具和数据源的交互提供标准化、安全且高效的方式,MCP使创建更有能力和具备上下文感知的AI应用程序成为可能。无论您是在构建简单的文件系统智能体还是复杂的多服务编排系统,MCP都为创建真正集成的AI解决方案提供了基础。