使用AWS代理核心部署MCP服务器
Guida每次部署都是非服务器MCP(模型上下文协议)su AWS基岩代理核心运行时。
先决条件
- Python 3.10或更高级
- 具有本地配置凭据的 AWS 帐户
- Node.js和npm(根据测试控制MCP检查器)
步骤 1: 安装
安装必要的依赖关系:
pip install -r requirements.txt步骤 2: 本地测试 MCP 服务器
启动服务器
python my_mcp_server.py服务器将可在 http://localhost:8000/mcp.
与本地客户端测试
在新终端中,执行:
python my_mcp_client.pyMCP检验员测试
npx @modelcontextprotocol/inspector- 打开浏览器
http://localhost:6274 - 请输入服务器地址 :
http://localhost:8000/mcp - 测试界面中可用的工具
步骤3:部署su AWS
1. 设置 Cognito 进行身份验证
创建文件 setup_cognito.sh 设置环境变量:
export REGION=us-west-2
export USERNAME=your_username
export PASSWORD=your_password
source setup_cognito.sh脚本将返回:
- 发现URL: 需要配置
- 客户端ID: 需要配置
- 持有者令牌需要调用部署的服务器
2. 配置部署
agentcore configure -e my_mcp_server.py --protocol MCP指导过程持续时间:
- 执行角色提供具有适当权限的 IAM 角色
- ECR按 Enter 自动创建
- 相关文件会自动检测到
- OAuth:数字
yes并提供 Discovery URL 和 Client ID
3.部署su AWS
agentcore launch命令 :
- 建立 Docker 容器
- 将图像上传到 Amazon ECR
- Crea-un亚马逊基岩代理核心运行时
- 在 AWS 上部署服务器
您将收到类似于:
arn:aws:bedrock-agentcore:us-west-2:accountId:runtime/my_mcp_server-xyz123步骤4:测试del服务器部署
设置环境变量
export AGENT_ARN="arn:aws:bedrock-agentcore:us-west-2:accountId:runtime/my_mcp_server-xyz123"
export BEARER_TOKEN="your_bearer_token"创建和测试远程客户端
创建文件 my_mcp_client_remote.py:
import asyncio
import os
import sys
from mcp import ClientSession
from mcp.client.streamable_http import streamablehttp_client
async def main():
agent_arn = os.getenv('AGENT_ARN')
bearer_token = os.getenv('BEARER_TOKEN')
if not agent_arn or not bearer_token:
print("Error: AGENT_ARN or BEARER_TOKEN not set")
sys.exit(1)
encoded_arn = agent_arn.replace(':', '%3A').replace('/', '%2F')
mcp_url = f"https://bedrock-agentcore.us-west-2.amazonaws.com/runtimes/{encoded_arn}/invocations?qualifier=DEFAULT"
headers = {
"authorization": f"Bearer {bearer_token}",
"Content-Type": "application/json"
}
async with streamablehttp_client(mcp_url, headers, timeout=120, terminate_on_close=False) as (
read_stream,
write_stream,
_,
):
async with ClientSession(read_stream, write_stream) as session:
await session.initialize()
tool_result = await session.list_tools()
print(tool_result)
asyncio.run(main())尝试 :
python my_mcp_client_remote.py测试控制MCP检查员(远程)
npx @modelcontextprotocol/inspector在 Web 界面中:
- 选择“Streamable HTTP”作为传输
- 输入端点URL(使用ARN URL编码)
- 在身份验证中添加 Bearer 令牌
- 点击“连接”
可用工具
MCP 服务器包括三个示例工具:
add_numbers: 将两个数字相加multiply_numbers乘以两个数字greet_user以姓名问候用户
参考资料
本指南基于 AWS 官方文档: 在AgentCore运行时部署MCP服务器
