How we built an MCP bridge to give our AgentCore-hosted AI agent access to local MCP tools
https://aws.amazon.com/blogs/machine-learning/how-we-built-an-mcp-bridge-to-give-our-agentcore-hosted-ai-agent-access-to-local-mcp-tools/📌 【AWS 技術分享】如何透過 MCP Bridge,讓雲端 AI Agent 操控你電腦裡的本地工具
TL;DR:透過 WebSocket 與 Native Messaging 建立橋樑,讓部署在雲端的 AI Agent 能直接存取本地端的 MCP 工具。
🎣 當雲端 Agent 遇上本地端檔案:如何跨越雲端與桌機的鴻溝?
想像一下,你的 AI Agent 運行在雲端(如 Amazon Bedrock AgentCore),但你的關鍵數據——例如 Excel 試算表——卻靜靜地躺在你的筆電硬碟裡。這中間存在著巨大的鴻溝:雲端 Agent 無法直接讀取你的本地檔案,而傳統的 MCP(Model Context Protocol)標準雖然解決了模型與工具的連接問題,但它預設的通訊機制(如 stdio 或 HTTP)往往難以直接跨越「雲端服務」與「本地處理程序」之間的界限。
🤔 為什麼我們需要一個「橋樑」?
Anthropic 在 2024 年 11 月推出的 MCP 標準,旨在統一 AI 模型連接外部數據與工具的方式。目前的 MCP 架構主要依賴兩種傳輸機制:
- stdio:用於同一臺機器上的本地處理程序通訊。
- streamable HTTP:用於遠端伺服器與客戶端之間的通訊。
然而,當「MCP Client 在雲端」而「MCP Server 在本地」時,現有的機制就失效了。這對於需要處理本地 Excel 檔案進行財務分析的專業人士來說,是一個嚴重的痛點。為了讓雲端 Agent 能像 Claude Code 或 Amazon Quick 那樣調用本地工具,我們需要一種機制來封裝並傳輸這些訊息。
🧩 架構解析:透過 WebSocket 與 Native Messaging 實現通訊
為了實現這個目標,我們開發了一套基於「橋樑(Bridge)」的概念,將訊息在不同層級間進行封裝與解封裝。
🚀 訊息傳輸的四個關鍵步驟
- 建立安全連線:瀏覽器擴充功能透過一個預簽章(Presigned)的 WebSocket URL 與 AgentCore 運行時建立連線。為了安全性,該 URL 使用 AWS SigV4 簽章,且有效期僅 5 分鐘,由本地端的 SDK 負責更新,確保憑證不會離開使用者的電腦。
- 發送請求:當 Agent 需要呼叫工具時,它會發送一個包裝在 JSON 封套(Envelope)中的 MCP JSON-RPC 請求,經由 WebSocket 回傳至瀏覽器擴充功能。
- 本地轉譯:擴充功能透過 Native Messaging(一種瀏覽器與本地程式通訊的機制)將訊息轉交給本地的「MCP Bridge」。Bridge 會拆解封套,提取原始的 JSON-RPC 內容。
- 執行工具:Bridge 將內容透過 stdio 傳送給本地的 MCP Server。當 Server 回傳結果後,Bridge 會重新包裝訊息,依原路徑傳回雲端 Agent。
💡 MCP Bridge 的內部設計:雙迴圈與非同步處理
為了確保效能與穩定性,MCP Bridge 內部採用了雙迴圈(Two-loop)設計,並透過 FastMCP proxy 進行管理:
- 主迴圈:負責讀取來自瀏覽器的訊息、解析 JSON 並將其放入輸入佇列(Input Queue)。
- 背景迴圈:負責從輸出佇列(Output Queue)提取結果,封裝後寫回 stdout 給瀏覽器。
這種設計將「瀏覽器的請求頻率」與「MCP Server 的處理速度」解耦合(Decouple),避免了因為某個工具執行過慢而導致整個通訊通道阻塞的問題。此外,透過 asyncio.Future 機制,系統可以同時處理多個正在進行中的工具呼叫(In-flight tool calls),而不會產生歧義。
📊 開發實作細節
- 工具發現(Tool Discovery):Agent 在每次發送訊息時會呼叫
tools/list。一旦 MCP Server 新增了工具,Agent 無需更改程式碼即可自動獲得新能力。 - 通訊限制:為了保護瀏覽器,單筆訊息的最大容量限制為 1 MB。
- 配置簡化:新增一個 MCP Server 僅需修改
mcp.json檔案中的一行配置,其餘複雜的通訊邏輯皆由 Bridge 處理。
🎯 實務啟示
對於需要處理高度敏感數據(如金融報表、本地開發環境)的企業來說,這種「雲端思考、本地執行」的模式提供了極大的靈活性。開發者可以利用雲端強大的運算能力與模型,同時保有對本地數據的控制權與安全性,無需將所有私密檔案都上傳至雲端。
🔗 來源
- 標題:How we built an MCP bridge to give our AgentCore-hosted AI agent access to local MCP tools
- 作者/機構:Rohan Lekhwani @ AWS ML
- 連結:https://aws.amazon.com/blogs/machine-learning/how-we-built-an-mcp-bridge-to-give-our-agentcore-hosted-ai-agent-access-to-local-mcp-tools/
#AI #MCP #AWS #AmazonBedrock #AgentCore #MachineLearning #SoftwareArchitecture #CloudComputing #LLM #DeveloperTools
原始資料 AWS ML · 收集於 2026-08-06
摘要原文
Our agent runs in the cloud, but our users’ spreadsheets live on their laptops. How do you bridge that gap? The Model Context Protocol (MCP) is an open source standard introduced by Anthropic in November 2024 to standardize how AI models connect to external data and tools. MCP follows a client-server architecture where an MCP host, an AI application like Amazon Quick or Claude Code, establishes connections to one or more MCP servers. The MCP protocol supports two transport mechanisms: stdio (standard I/O for communication between local processes on the same machine) and streamable HTTP transport (HTTP-based communication between remote servers and clients). A missing piece is when the MCP server exists locally and the MCP client is remote. This pattern matters for financial managers and analysts who primarily work with Excel and local files. They can use centrally deployed AI agents to act on those files while also drawing context from their browser. This is the same pattern that powers products like Claude Cowork , a cloud agent calling local tools through MCP, but fully self-hosted on AWS with your own model and custom tool servers. Internally, we built a production-grade AI assistant for finance that has seen over 41,000 conversations within a year since launch. In this post, we recreate what we built internally in a simplified form. Our agent, deployed on Amazon Bedrock AgentCore , uses MCP servers that run on a user’s local machine. We bridge the gap between the remote MCP client and the local MCP server by tunneling MCP messages over WebSocket and native messaging. We discuss additional production hardening measures in the What’s Next section. The complete source code is available on GitHub . The MCP Bridge Demo extension summarizing a local Excel workbook. The cloud-hosted agent reads the file directly from the user’s machine through the MCP bridge and streams a structured summary back to the side panel The AgentCore runtime, a capability of Amazon Bedrock AgentCore, anchors an architecture with four components: The following diagram shows the end-to-end message flow. The user sends a message through the extension, which connects to the AgentCore runtime over a presigned WebSocket. When the Strands agent needs to call a tool, it sends an MCP JSON-RPC request wrapped in a JSON envelope back through the WebSocket to the extension. The extension relays the message as-is to the bridge through native messaging. The bridge unwraps the envelope, extracts the JSON-RPC content, and forwards it to the MCP server over stdio. The response travels the reverse path. The bridge wraps the unmodified MCP server response back into an envelope and relays it through the extension to the AgentCore runtime, where the agent consumes the tool result and continues generation. High-level architecture diagram showing all components. The browser extension and MCP Bridge act as relays that wrap and unwrap JSON messages from the AgentCore runtime and JSON-RPC messages from the MCP server The following table shows a single tool call as it travels from the agent to the MCP server, with each hop stripping one layer of wrapping: WebSocket connection: The browser extension connects to the AgentCore runtime over a presigned WebSocket URL. On startup, the side panel sends a presign request through the background script to the native bridge, which uses the user’s local AWS credentials and the bedrock-agentcore software development kit (SDK) to generate a SigV4-signed wss:// URL scoped to the deployed runtime ARN (valid for 5 minutes). The side panel opens a WebSocket to that URL. No credentials ever leave the user’s machine or enter the browser. If the connection drops because of URL expiry or network interruption, the side panel automatically requests a fresh presigned URL after 2 seconds and reconnects, making the expiry window invisible to the user during normal use. MCP initialization: Before discovering tools, the agent performs the standard MCP initialization handshake . It sends an initialize request with the protocol version, waits for the server’s capabilities response, and then sends a notifications/initialized notification. Only after this handshake completes does the server accept tools/list and tools/call requests. Tool discovery: On each user message, the agent calls tools/list and receives an array of tool schemas. It wraps each schema into a Strands AgentTool whose stream() method sends a tools/call request through the bridge. Tools added to the MCP server are automatically available on the next request with no agent code changes. Request-response correlation: Each outbound JSON-RPC request from the agent is assigned a unique ID and registered against an asyncio.Future keyed by (session_id, jsonrpc_id) . When the response arrives back over the WebSocket, it is matched to the waiting Future and resolved. This allows multiple tool calls to be in flight concurrently without ambiguity. We need the extension to talk to a long-running local process without network permissions or per-message user prompts. Native messaging provides exactly this. Both Chrome and Firefox support native messaging for their extensions. The browser looks for a manifest file at a well-known location on the user’s machine that specifies which binary to launch. The native messaging manifest file for Chrome on macOS is as follows: On extension startup, the background script calls chrome.runtime.connectNative("com.example.mcp_bridge") to launch the native app locally. The run_bridge.sh script referenced in the manifest activates the Python environment and starts the bridge: The native messaging host process stays alive for the lifetime of the connection. Each message is serialized as JSON, UTF-8 encoded, and preceded with a 32-bit message length in little-endian byte order. The maximum size of a single message from the native messaging host is 1 MB (to protect the browser from misbehaving native applications). The maximum size of a message sent to the native messaging host is 64 MiB. The MCP Bridge acts as a protocol translator between two worlds: Chrome’s native messaging protocol on one side and the MCP standard (JSON-RPC 2.0 over stdio) on the other. On the inbound path, it strips the 4-byte length header from stdin, parses the JSON body, and unwraps the envelope to extract the raw JSON-RPC message. On the outbound path, it does the reverse: wraps the JSON-RPC response in an envelope and writes it back with the length header. The JSON-RPC content itself passes through untouched. Internally, the bridge runs two concurrent loops connected through a FastMCP proxy. The main loop reads messages from the browser, unwraps them, and places the JSON-RPC content onto an input queue. The FastMCP proxy, started once and kept alive for the bridge’s lifetime, picks messages off this queue, forwards them to the MCP server subprocess over its stdin, and places responses from the server’s stdout onto an output queue. A second background loop reads from the output queue, wraps each response back into an envelope, and writes it to stdout for the browser to receive. This two-loop design decouples the browser’s request timing from the MCP server’s processing speed so the bridge does not block waiting for a slow tool to finish before accepting the next request. The MCP server itself is a child process spawned by the bridge on startup, configured through a mcp.json file. It stays running for the bridge’s lifetime with no per-request process overhead. Adding a new MCP server is a one-line config change. The bridge handles the plumbing. Internal architecture of the MCP Bridge. The MCP Bridge translates messages from the browser extension into the MCP protocol for the MCP server. I/O queues work with a FastMCP proxy server to forward messages to the locally running MCP server and relay messages back to the browser extension The following prerequisites are needed to deploy and test the MCP bridge solution.
由 tencent/hy3:free 自動生成