當 Anthropic 發布模型上下文協定 (MCP)時,我立即開始在 AWS 上嘗試各種部署選項:
首先,我嘗試將 MCP 伺服器作為 AWS Lambda 函數執行。這在簡單性和成本方面都是一個很棒的解決方案,但也意味著我必須在每次呼叫之間手動管理會話狀態。
接下來,我將它們作為容器部署在 Amazon ECS 上,這立即解決了我的會話狀態管理問題,但我必須為執行中的伺服器付費,即使它處於空閒狀態。
後來, Amazon Bedrock AgentCore發布了,它正是我一直在尋找的解決方案:一個無伺服器執行時環境,能夠自動處理會話狀態,並且只對 MCP 請求的實際處理時間收費——可謂兩全其美。此外,作為原生 AWS 服務,它也支援 IAM,這與我現有的技術堆疊完美契合。
但是當我嘗試將代理連接到伺服器時,卻遇到了阻礙:
無論我使用哪個代理框架,它們都要求 OAuth。這意味著我需要搭建一個單獨的 Cognito 堆疊,僅僅為了將代理連接到它的工具。但我希望像在 AWS 上管理其他所有事務一樣,使用 IAM 憑證。
這就是我為 IAM 建立 MCP 用戶端的原因,它是一個即插即用的替代品,可以使用官方 MCP SDK 的streamablehttp_client與任何 Python 框架一起使用。
無需 Cognito 設定、無需令牌管理、無需自訂簽名程式碼。此用戶端使用您代理現有的 AWS 憑證自動對 MCP 請求進行簽署。
在這篇文章中,我將向您展示如何將其與流行的 AI 代理框架(如 LangChain、Strands Agents、LlamaIndex 和 Microsoft 的 Agent Framework)一起使用。
適用於 IAM 的 MCP 用戶端是開源的,並且是AWS 官方 MCP 代理的一部分。您可以在GitHub和PyPI上找到它。
如果您已經在 AWS 上擁有一個啟用了 IAM 驗證的 MCP 伺服器,那麼您只需要在用戶端安裝該軟體包:
pip install mcp-proxy-for-aws
就這樣。現在你可以用以下導入語句取代 MCP 的預設streamablehttp_client :
from mcp_proxy_for_aws.client import aws_iam_streamablehttp_client
如果您還沒有在 AWS 上部署 MCP 伺服器,請繼續關注。我很快就會發布 AgentCore Runtime 和 AgentCore Gateway 的完整端到端範例。
不同的框架對 MCP 連線的管理方式不同。客戶端支援兩種整合模式,以配合以下方法:

適用場景:接受傳回 MCP 用戶端的工廠函數的框架,例如 Strands Agents SDK 和 Microsoft 的 Agent aws_iam_streamablehttp_client作為工廠函數傳遞給框架,框架隨後會在內部處理連接生命週期。
範例 - 鏈狀代理:
from mcp_proxy_for_aws.client import aws_iam_streamablehttp_client
mcp_client_factory = lambda: aws_iam_streamablehttp_client(
endpoint=mcp_url, # The URL of the MCP server
aws_region=region, # The region of the MCP server
aws_service=service # The underlying AWS service, e.g. "bedrock-agentcore"
)
with MCPClient(mcp_client_factory) as mcp_client:
mcp_tools = mcp_client.list_tools_sync()
agent = Agent(tools=mcp_tools, ...)
例如—微軟的代理框架:
from mcp_proxy_for_aws.client import aws_iam_streamablehttp_client
mcp_client_factory = lambda: aws_iam_streamablehttp_client(
endpoint=mcp_url, # The URL of the MCP server
aws_region=region, # The region of the MCP server
aws_service=service # The underlying AWS service, e.g. "bedrock-agentcore"
)
mcp_tools = MCPStreamableHTTPTool(name="MCP Tools", url=mcp_url)
mcp_tools.get_mcp_client = mcp_client_factory
async with mcp_tools:
agent = ChatAgent(tools=[mcp_tools], ...)

適用場景:需要直接存取 MCP 會話的框架, aws_iam_streamablehttp_client LangChain 和 LlamaIndex。 aws_iam_streamablehttp_client 提供經過驗證的傳輸流,然後使用這些傳輸流建立 MCP ClientSession 。
範例 - LangChain:
from mcp_proxy_for_aws.client import aws_iam_streamablehttp_client
mcp_client = aws_iam_streamablehttp_client(
endpoint=mcp_url, # The URL of the MCP server
aws_region=region, # The region of the MCP server
aws_service=service # The underlying AWS service, e.g. "bedrock-agentcore"
)
async with mcp_client as (read, write, session_id_callback):
async with ClientSession(read, write) as session:
mcp_tools = await load_mcp_tools(session)
agent = create_langchain_agent(tools=mcp_tools, ...)
範例 - LlamaIndex:
from mcp_proxy_for_aws.client import aws_iam_streamablehttp_client
mcp_client = aws_iam_streamablehttp_client(
endpoint=mcp_url, # The URL of the MCP server
aws_region=region, # The region of the MCP server
aws_service=service # The underlying AWS service, e.g. "bedrock-agentcore"
)
async with mcp_client as (read, write, session_id_callback):
async with ClientSession(read, write) as session:
mcp_tools = await McpToolSpec(client=session).to_tool_list_async()
agent = ReActAgent(tools=mcp_tools, ...)
在GitHub 儲存庫中探索不同框架的完整工作範例。
簡單的本地開發:
直接使用您的 AWS CLI 憑證。無需 Cognito 設置,無需令牌管理,也無需單獨的身份驗證基礎設施。
標準 AWS 模式:
透過 IAM 角色承擔進行跨帳戶存取的方式與對其他所有 AWS 服務的方式相同。
基於身分和存取管理的存取控制:
使用 IAM 策略控制哪些代理程式可以連接到哪些 MCP 伺服器。無需更改伺服器程式碼。
AWS 的通用相容性:
MCP IAM 用戶端可與 AgentCore Gateway、AgentCore Runtime 以及任何其他透過 HTTPS 公開 MCP 並支援 IAM 驗證的 AWS 服務搭配使用。
請造訪GitHub 查看完整的範例,其中包含使用多種框架的可執行程式碼。
更多資源:
即將推出:
完整教學:如何在 AgentCore Runtime 上建置和部署 MCP 伺服器
完整教學:如何使用 AgentCore Gateway 將現有 API 轉換為 MCP 伺服器
您計劃建置哪些 MCP 伺服器?歡迎在評論區分享您的使用案例!
原文出處:https://dev.to/aws/no-oauth-required-an-mcp-client-for-aws-iam-k1o