用 AWS IAM 替代 OAuth 的 MCP 客户端
展示如何用 AWS IAM 认证构建 MCP 客户端,避免 OAuth 复杂性,是实用的 AI 应用集成方案
展示如何用 AWS IAM 认证构建 MCP 客户端,避免 OAuth 复杂性,是实用的 AI 应用集成方案
当 Anthropic 发布 Model Context Protocol(MCP)时,我立刻开始尝试在 AWS 上部署它的各种方案:
首先,我尝试将 MCP server 作为 AWS Lambda 函数运行。就简单程度和成本而言,这是一个很棒的解决方案,但也意味着我必须手动管理不同调用之间的 session state。
接下来,我将它们以容器形式部署到 Amazon ECS。这立刻替我解决了 session state 管理问题,但即使 server 处于空闲状态,我也必须为持续运行的 server 付费。
后来,Amazon Bedrock AgentCore 发布了。事实证明,它正是我一直在寻找的解决方案:一个 serverless runtime,能够自动处理 session state,并且只对 MCP request 的实际处理时间收费——兼得两种方案的优点。同时,作为 AWS 原生服务,它支持 IAM,可以完美融入我现有的技术栈。
但当我尝试真正将 Agent 连接到 server 时,却碰了壁:
无论使用哪个 Agent framework,它们都要求 OAuth。这意味着,仅仅为了将 Agent 连接到它的工具,我就得单独搭建一套 Cognito 技术栈。但我更希望像使用 AWS 上的其他所有服务一样,直接使用 IAM credentials。
这就是我构建 MCP client for IAM 的原因。它是一个可以直接替换现有实现的组件,适用于任何使用官方 MCP SDK streamablehttp_client 的 Python framework。
不需要配置 Cognito,不需要管理 token,也不需要编写自定义签名代码。这个 client 会使用 Agent 现有的 AWS credentials,自动为 MCP request 签名。
在本文中,我将介绍如何将它与 LangChain、Strands Agents、LlamaIndex 和 Microsoft Agent Framework 等主流 AI Agent framework 配合使用。
MCP client for IAM 是开源项目,也是官方 MCP Proxy for AWS 的一部分。你可以在 GitHub 和 PyPI 上找到它。
如果你已经在 AWS 上部署了启用 IAM auth 的 MCP server,只需在 client 中安装这个 package:
pip install mcp-proxy-for-aws
就是这么简单。现在,你可以通过下面的 import,替换 MCP 默认的 streamablehttp_client:
from mcp_proxy_for_aws.client import aws_iam_streamablehttp_client
如果你还没有在 AWS 上部署 MCP server,可以查看这篇快速教程:用两条命令将本地 MCP Server 部署到 AWS。
不同的 framework 管理 MCP connection 的方式各不相同。这个 client 支持两种集成模式,以适配以下不同方案:
适用于:接受返回 MCP client 的 factory function 的 framework,例如 Strands Agents SDK 和 Microsoft Agent Framework。将 aws_iam_streamablehttp_client 作为 factory 传给 framework,之后由 framework 在内部管理 connection lifecycle。
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 session 的 framework,例如 LangChain 和 LlamaIndex。aws_iam_streamablehttp_client 提供经过身份验证的 transport stream,再使用这些 stream 创建 MCP ClientSession。
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, ...)
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 repository 中查看适用于不同 framework、可以完整运行的示例。
简单的本地开发:直接使用 AWS CLI credentials。不需要配置 Cognito,不需要管理 token,也不需要单独搭建 auth infrastructure。
标准的 AWS 模式:通过 IAM role assumption 实现 cross-account access,其工作方式与其他所有 AWS 服务完全一致。
基于 IAM 的 access control:使用 IAM policy 控制哪些 Agent 可以连接到哪些 MCP server。不需要修改 server code。
全面兼容 AWS:MCP client for IAM 可以与 AgentCore Gateway、AgentCore Runtime,以及任何通过 HTTPS 暴露 MCP 并使用 IAM authentication 的其他 AWS 服务配合使用。
来看看这种在 AWS 上部署自己的 MCP server 的简单方式。
查看 GitHub 上的完整示例,获取适配多种 framework 的可运行代码。
PyPI 上的 package——安装说明和 changelog。
GitHub 上的 source——参与贡献或报告 issue。
你打算构建什么样的 MCP server?欢迎在评论区分享你的 use case!
如果需要采取进一步措施,你可以考虑屏蔽此人和/或举报滥用行为。