我们的合规检查器现已更新,支持欧盟人工智能法案的合规检查! 了解更多。

MCP 生产级代理架构模式

2025/2/28

Tags: MCP, AI 代理, 架构模式, 生产环境, 系统设计, 可扩展性, 安全性

Jules Kovac

Jules Kovac

Business Analyst, Founder

MCP 生产级代理架构模式

 

MCP 生产现实

如果您在生产环境中部署 MCP 代理,第二天的痛苦可能会让您感觉正在失去控制:token 账单飙升,远程服务器出现故障,安全团队询问如何锁定这个系统。典型的痛点包括:

  • 您的 MCP 服务器在开发环境中工作正常,但在生产环境中静默死亡。
  • 您的 token 账单看起来像是房子的首付。
  • 您"简单"的代理设置变成了一个具有 14 种故障模式的分布式系统。

这就是模型上下文协议的真实故事:它不是某种抽象规范,而是代理与它们需要接触的现实世界工具和数据之间的管道。

 

 

六个生产级代理模式

本文将这些痛点映射到六个 MCP 架构模式,您可以实际使用这些模式来部署和扩展代理而不会失控。这六个模式是一个实用的现场指南,不是官方规范——每个模式都对应一个有充分文档记录的现实世界工程模式,并有生产实现支持。

 

 

1. 直连模式 — "今晚上线"

口号: 你,一个代理,一个 MCP 服务器,没有麻烦。

直连模式是"MCP 的单体架构"——您的主机应用程序直接通过 stdio 或 HTTP 与 MCP 服务器通信,没有额外的跳转。当您只想看到某些东西工作,而暂时不关心治理幻灯片时,这是完美的选择。

最适合:

  • 您正在构建 MVP 或黑客马拉松演示。
  • 单一团队,单一信任边界,一切都在"您的"基础设施中运行。
  • 您想要最低的延迟和最简单的调试。

避免使用:

  • 您正在跨团队或租户暴露工具。
  • 安全需要审计日志、访问策略,有人说"SOX"。

维度快照: 安全性 (⭐☆☆☆),可扩展性 (⭐☆☆☆),成本效率 (⭐⭐⭐☆),可调试性 (⭐⭐⭐⭐)

 

看看它在 FlowZap 中的样子:

Host { # Host Application
  n1: circle label:"User sends prompt"
  n2: rectangle label:"Agent builds JSON-RPC request"
  n3: rectangle label:"Send request via stdio"
  n4: rectangle label:"Receive JSON-RPC result"
  n5: rectangle label:"Agent responds to user"

  n1.handle(right) -> n2.handle(left)
  n2.handle(right) -> n3.handle(left)
  n3.handle(bottom) -> MCPServer.n6.handle(top) [label="JSON-RPC request"]
  n4.handle(right) -> n5.handle(left)
}

MCPServer { # MCP Server
  n6: rectangle label:"Parse incoming request"
  n7: rectangle label:"Execute tool or resource"
  n8: rectangle label:"Build JSON-RPC response"

  n6.handle(right) -> n7.handle(left)
  n7.handle(right) -> n8.handle(left)
  n8.handle(top) -> Host.n4.handle(bottom) [label="JSON-RPC response"]
}

 

 

2. 网关代理 — "让安全团队满意"

口号: 在您的工具前放一个保镖。

网关代理在您的代理和 MCP 服务器之间放置一个 API 网关,以处理身份验证、速率限制和审计。您的代理仍然认为它在正常调用工具;网关静默强制执行 OAuth 2.0、SAML、SSO、工具级速率限制和基于团队的配额执行,然后请求才会到达 MCP 服务器。这不是理论上的——MintMCP Gateway、Gravitee MCP Proxy、Kong 和 Azure APIM 等产品都实现了这个确切的模式。

这个模式的真实案例是鲜明的:没有网关级控制,单个陷入重试循环的代理可能在几小时内耗尽 API 预算。具有基于 token 的配额、突发允许和按工具细粒度控制的网关是标准预防措施。

最适合:

  • 需要跨所有工具的一致身份验证(OAuth/JWT/API 密钥)。
  • 合规性(SOC2、GDPR)或事件响应需要请求日志。
  • 多个团队或客户共享相同的 MCP 资产。

避免使用:

  • 超延迟敏感且每毫秒都很重要——网关增加了一个网络跳转。
  • 流量不足以证明增加的复杂性。

维度快照: 安全性 (⭐⭐⭐⭐),可扩展性 (⭐⭐⭐⭐),成本效率 (⭐⭐☆☆),可调试性 (⭐⭐⭐☆)

 

FlowZap 代码

Host { # Host Application
  n1: circle label:"User sends prompt"
  n2: rectangle label:"Agent builds tool call"
  n3: rectangle label:"Send request to gateway"
  n4: rectangle label:"Receive gateway response"
  n5: rectangle label:"Agent responds to user"

  n1.handle(right) -> n2.handle(left)
  n2.handle(right) -> n3.handle(left)
  n3.handle(bottom) -> Gateway.n6.handle(top) [label="Tool request"]
  n4.handle(right) -> n5.handle(left)
}

Gateway { # MCP Gateway
  n6: rectangle label:"Receive and log request"
  n7: diamond label:"Authorized?"
  n8: rectangle label:"Forward to MCP server"
  n9: rectangle label:"Receive MCP response"
  n10: rectangle label:"Log response and return"

  n6.handle(right) -> n7.handle(left)
  n7.handle(right) -> n8.handle(left) [label="Yes"]
  n7.handle(top) -> Host.n4.handle(left) [label="401 Unauthorized"]
  n8.handle(bottom) -> MCPServer.n11.handle(top) [label="Forwarded request"]
  n9.handle(right) -> n10.handle(left)
  n10.handle(top) -> Host.n4.handle(bottom) [label="Authorized response"]
}

MCPServer { # MCP Server
  n11: rectangle label:"Execute tool"
  n12: rectangle label:"Return result"

  n11.handle(right) -> n12.handle(left)
  n12.handle(top) -> Gateway.n9.handle(bottom) [label="Tool result"]
}

 

 

3. 工具路由器 — "别再给 LLM 一本电话簿"

口号: 50 个工具,1 个代理,合理的 token 使用。

工具路由器模式在您的工具前放置一个路由大脑,以便 LLM 只"看到"它实际需要的子集。这是一个有记录的、严重的问题:加载到上下文中的完整工具模式定义可以在用户发送第一条消息之前消耗 40% 的可用 token。Writer.com 通过构建一个语义"搜索元工具"来解决这个问题,该工具使用向量嵌入和余弦相似度将用户意图动态匹配到正确的工具。Speakeasy 使用动态工具集实现了输入 token 减少 96%,总 token 消耗减少 90%。

语义 MCP 路由器方法提供两条发现路径:一个精选的"前 20"默认工具集预加载到上下文中,以及一条用于专业工具的深层语义搜索路径。这种双轨模型保持快速路径快速,长尾可访问,而不会膨胀每个提示。

最适合:

  • 超过五个工具,提示疯狂膨胀。
  • 不同用例需要不同的工具切片(计费 vs. 分析 vs. 运维)。
  • 需要在不降低代理能力的情况下减少上下文大小。

避免使用:

  • 一个只有几个工具的小型应用程序。
  • 团队还不能支持路由逻辑、指标和回退。

维度快照: 安全性 (⭐⭐⭐☆),可扩展性 (⭐⭐⭐⭐),成本效率 (⭐⭐⭐⭐),可调试性 (⭐⭐☆☆)

 

FlowZap 代码

Host { # Host Application
  n1: circle label:"User sends prompt"
  n2: rectangle label:"Agent extracts intent"
  n3: rectangle label:"Send intent to router"
  n4: rectangle label:"Receive routed result"
  n5: rectangle label:"Agent responds to user"

  n1.handle(right) -> n2.handle(left)
  n2.handle(right) -> n3.handle(left)
  n3.handle(bottom) -> Router.n6.handle(top) [label="Intent + tool request"]
  n4.handle(right) -> n5.handle(left)
}

Router { # Tool Router
  n6: rectangle label:"Receive intent"
  n7: rectangle label:"Semantic match via embeddings"
  n8: diamond label:"Which MCP server?"
  n9: rectangle label:"Forward to Server A"
  n10: rectangle label:"Forward to Server B"
  n11: rectangle label:"Normalize and return result"

  n6.handle(right) -> n7.handle(left)
  n7.handle(right) -> n8.handle(left)
  n8.handle(bottom) -> n9.handle(top) [label="Route A"]
  n8.handle(right) -> n10.handle(left) [label="Route B"]
  n9.handle(bottom) -> ServerA.n12.handle(top) [label="Call Server A"]
  n10.handle(bottom) -> ServerB.n14.handle(top) [label="Call Server B"]
  n11.handle(top) -> Host.n4.handle(bottom) [label="Final result"]
}

ServerA { # MCP Server A
  n12: rectangle label:"Execute tool A"
  n13: rectangle label:"Return A result"

  n12.handle(right) -> n13.handle(left)
  n13.handle(top) -> Router.n11.handle(bottom) [label="Result A"]
}

ServerB { # MCP Server B
  n14: rectangle label:"Execute tool B"
  n15: rectangle label:"Return B result"

  n14.handle(right) -> n15.handle(left)
  n15.handle(top) -> Router.n11.handle(left) [label="Result B"]
}

 

 

4. 代理网格 — "特工小队,一个大脑"

口号: 许多专家,共享上下文,受控的混乱。

代理网格是当您停止假装一个代理可以做所有事情时发生的事情。多个代理通过 MCP 支持的共享上下文代理进行通信,实现协调的工具访问和状态同步。微软的 Azure 实现使用 Cosmos DB(带有内存回退)的持久会话状态,支持动态模式交换和可追踪的多代理交互。

这里的关键架构选择是编排与编排。在编排设置中,管理器代理协调所有交互,维护任务分类账,并可以基于中间发现动态重新规划。在编排中,代理通过 MCP 的结构化 JSON-RPC 交换进行点对点通信,任何代理都可以向任何其他代理请求帮助。两种方法都依赖共享内存,因此所有代理访问相同的状态存储以保持一致上下文。

风险是真实的:代理可以在彼此之间无限期地来回 ping-pong 任务,没有适当的终止条件和可观察性。

最适合:

  • 存在不同角色:规划者、编码者、审查者、操作员。
  • 需要共享状态(任务、资源、工作流)而不是隔离孤岛。
  • 工作负载自然分解为可并行化的子任务。

避免使用:

  • 一个装备精良的单一代理就足够了。
  • 可观察性和追踪尚未到位(调试将很痛苦)。

维度快照: 安全性 (⭐⭐⭐☆),可扩展性 (⭐⭐⭐⭐),成本效率 (⭐⭐☆☆),可调试性 (⭐⭐☆☆)

 

FlowZap 代码

Orchestrator { # Orchestrator Agent
  n1: circle label:"Complex task received"
  n2: rectangle label:"Decompose into subtasks"
  n3: rectangle label:"Assign subtask to Agent B"
  n4: rectangle label:"Receive subtask result"
  n5: rectangle label:"Request shared context"
  n6: rectangle label:"Compile final response"

  n1.handle(right) -> n2.handle(left)
  n2.handle(right) -> n3.handle(left)
  n3.handle(bottom) -> Worker.n7.handle(top) [label="Subtask assignment"]
  n4.handle(right) -> n5.handle(left)
  n5.handle(bottom) -> Broker.n11.handle(top) [label="Context request"]
  n6.handle(left) -> n2.handle(bottom) [label="Next iteration"]
}

Worker { # Worker Agent
  n7: rectangle label:"Receive subtask"
  n8: rectangle label:"Fetch shared context"
  n9: rectangle label:"Call MCP tool"
  n10: rectangle label:"Return result to orchestrator"

  n7.handle(right) -> n8.handle(left)
  n8.handle(bottom) -> Broker.n11.handle(left) [label="Context request"]
  n8.handle(right) -> n9.handle(left)
  n9.handle(bottom) -> MCPServer.n13.handle(top) [label="MCP tool call"]
  n10.handle(top) -> Orchestrator.n4.handle(bottom) [label="Subtask result"]
}

Broker { # Context Broker
  n11: rectangle label:"Resolve context request"
  n12: rectangle label:"Return shared state"

  n11.handle(right) -> n12.handle(left)
  n12.handle(top) -> Orchestrator.n6.handle(bottom) [label="Context to orchestrator"]
  n12.handle(left) -> Worker.n9.handle(top) [label="Context to worker"]
}

MCPServer { # MCP Server
  n13: rectangle label:"Execute tool"
  n14: rectangle label:"Return tool output"

  n13.handle(right) -> n14.handle(left)
  n14.handle(top) -> Worker.n10.handle(bottom) [label="Tool output"]
}

 

 

5. 熔断器 — "不再有僵尸调用"

口号: 如果工具正在死亡,停止锤击它。

熔断器使用健康感知门包装 MCP 调用,使用三种状态:关闭(正常操作,请求通过),打开(检测到故障,请求快速失败),和半开(测试服务是否已恢复)。这是应用于 MCP 工具调用的经典分布式系统卫生。

没有熔断器,故障级联是可预测的:工具 A 失败 → 重试堆积 → 资源耗尽 → 其他工具变慢 → 系统超载 → 一切都失败。有了熔断器:工具 A 失败 → 电路打开 → 快速失败 → 其他工具不受影响 → 系统稳定 → 准备好时恢复。

这种模式有真实的 MCP 实现。IBM 的 mcp-context-forge 有一个完整的熔断器功能请求,具有半开状态恢复、故障阈值和快速故障保护。MCP Go SDK 包括一个生产就绪的错误恢复示例,实现熔断器以及指数退避重试和隔板隔离。Octopus.com 记录了使用 pybreaker 库进行 MCP 工具调用的完整 Langchain + Python 实现。

最适合:

  • 依赖不可靠的第三方 API 或遗留数据库。
  • 观察到代理冻结,因为单个 MCP 服务器变得无响应。
  • 更喜欢优雅降级而不是全有或全无的行为。

避免使用:

  • 一切都是本地、快速和坚如磐石的(例如,stdio 到本地进程)。
  • 对"快速失败"时做什么没有计划(回退工具、用户消息等)。

维度快照: 安全性 (⭐⭐⭐☆),可扩展性 (⭐⭐⭐⭐),成本效率 (⭐⭐⭐☆),可调试性 (⭐⭐⭐⭐)

 

FlowZap 代码

Host { # Host Application
  n1: circle label:"User sends prompt"
  n2: rectangle label:"Agent prepares MCP call"
  n3: rectangle label:"Pass call to circuit breaker"
  n4: rectangle label:"Receive result or error"
  n5: rectangle label:"Agent responds to user"

  n1.handle(right) -> n2.handle(left)
  n2.handle(right) -> n3.handle(left)
  n3.handle(bottom) -> CB.n6.handle(top) [label="MCP tool call"]
  n4.handle(right) -> n5.handle(left)
}

CB { # Circuit Breaker
  n6: rectangle label:"Check circuit state"
  n7: diamond label:"Circuit open?"
  n8: rectangle label:"Forward to MCP server"
  n9: rectangle label:"Fast-fail with error"
  n10: diamond label:"Call succeeded?"
  n11: rectangle label:"Record success"
  n12: rectangle label:"Record failure and check threshold"

  n6.handle(right) -> n7.handle(left)
  n7.handle(right) -> n8.handle(left) [label="Closed"]
  n7.handle(bottom) -> n9.handle(top) [label="Open"]
  n8.handle(bottom) -> MCPServer.n13.handle(top) [label="Forward request"]
  n9.handle(top) -> Host.n4.handle(bottom) [label="CircuitOpenError"]
  n10.handle(right) -> n11.handle(left) [label="Yes"]
  n10.handle(bottom) -> n12.handle(top) [label="No"]
  n11.handle(top) -> Host.n4.handle(left) [label="Return result"]
  n12.handle(top) -> Host.n4.handle(right) [label="Return error"]
}

MCPServer { # MCP Server
  n13: rectangle label:"Attempt tool execution"
  n14: rectangle label:"Return result or error"

  n13.handle(right) -> n14.handle(left)
  n14.handle(top) -> CB.n10.handle(bottom) [label="Execution outcome"]
}

 

 

6. 上下文代理 — "将您的 LLM 账单减半"

口号: 缓存无聊的东西,为智能的东西付费。

上下文代理是代理和 MCP 服务器之间的缓存和压缩层,在冗余上下文请求到达网络之前拦截它们。这将上下文视为具有 TTL、失效钩子和命中率监控的实际托管资源——而不是魔法般的无限 token 流。

这种模式的有力证据是强有力的。Token Optimizer MCP 服务器将 Brotli 压缩与基于 SQLite 的持久缓存相结合,实现了高达 95%+ 的 token 减少。GitHub 上的 mcp-context-proxy 项目充当透明的 MCP 代理,在使用外部 LLM 将大型工具响应传递给资源受限的本地模型之前对其进行压缩。有效策略包括提示级缓存(重用完整的提示-响应对)、部分上下文缓存(重用静态系统提示)和语义缓存(通过嵌入匹配近重复请求)。

缓存失效是困难的部分。基于时间的过期适用于缓慢变化的数据,基于事件的失效处理数据更新,混合方法平衡新鲜度和效率。根据实际应用需求定义陈旧容忍度。

最适合:

  • 代理不断询问相同的文档、模式或代码库切片。
  • 检索在相对静态的数据上运行(知识库、规范)。
  • 账单 screaming "上下文膨胀" 多于"模型大小"。

避免使用:

  • 数据是实时的,陈旧是危险的(交易、关键操作)。
  • 没有明确的失效和新鲜度策略。

维度快照: 安全性 (⭐⭐⭐☆),可扩展性 (⭐⭐⭐⭐),成本效率 (⭐⭐⭐⭐),可调试性 (⭐⭐⭐☆)

 

FlowZap 代码

Host { # Host Application
  n1: circle label:"User sends prompt"
  n2: rectangle label:"Agent requests context"
  n3: rectangle label:"Receive context"
  n4: rectangle label:"Agent responds to user"

  n1.handle(right) -> n2.handle(left)
  n2.handle(bottom) -> Proxy.n5.handle(top) [label="Context request"]
  n3.handle(right) -> n4.handle(left)
}

Proxy { # Context Proxy
  n5: rectangle label:"Receive context request"
  n6: rectangle label:"Check cache with TTL"
  n7: diamond label:"Cache hit?"
  n8: rectangle label:"Return cached context"
  n9: rectangle label:"Fetch fresh from MCP server"
  n10: rectangle label:"Compress and cache response"

  n5.handle(right) -> n6.handle(left)
  n6.handle(right) -> n7.handle(left)
  n7.handle(right) -> n8.handle(left) [label="Hit"]
  n7.handle(bottom) -> n9.handle(top) [label="Miss"]
  n8.handle(top) -> Host.n3.handle(bottom) [label="Cached context"]
  n9.handle(bottom) -> MCPServer.n11.handle(top) [label="Fetch request"]
  n10.handle(top) -> Host.n3.handle(left) [label="Fresh context"]
}

MCPServer { # MCP Server
  n11: rectangle label:"Fetch full context"
  n12: rectangle label:"Return fresh data"

  n11.handle(right) -> n12.handle(left)
  n12.handle(top) -> Proxy.n10.handle(bottom) [label="Fresh data"]
}

 

 

如何实际使用这些模式

如果您在想"我应该选择哪一个?",请使用这个阶梯:

  1. 从直连模式开始,让某些东西工作。
  2. 一旦有了真实用户和安全需求,添加网关代理。
  3. 当您超过 5 个工具并出现 token 痛苦时,引入工具路由器。
  4. 一旦任何远程东西可能失败(它会),就加入熔断器。
  5. 财务第一次向您发送关于 LLM 成本的消息时,就引入上下文代理。
  6. 只有当单个代理确实无法跟上时,才考虑代理网格。

 

 

灵感来源

直接使用 MCP
返回所有博客文章