MCP Server + AgentCore Gateway 实战:把飞书封装为 AI Agent 可调用的工具

MCP Server + AgentCore Gateway 实战:把飞书封装为 AI Agent 可调用的工具

概述

亚马逊云科技中国官博 5/11 发布了一篇详细的集成指南,演示了如何将飞书(Lark)能力封装为 MCP Server,通过 AgentCore Gateway 实现统一鉴权与路由,部署到 AgentCore Runtime,并接入 Quick Suite AI Agent。

本文基于官方指南,梳理关键步骤和技术细节。

架构设计

Amazon Quick Suite (MCP Client / AI Agent)
    │
    ├── 1. client_credentials → Cognito → JWT Token
    │
    ▼
Amazon Bedrock AgentCore Gateway
    │
    ├── 2. 验证 JWT Token
    ├── 3. IAM SigV4 签名转发
    │
    ▼
Amazon Bedrock AgentCore Runtime (容器化)
    │
    └── MCP Server (飞书工具封装)
         ├── send_message
         ├── get_calendar
         └── search_docs

核心设计:入站用 OAuth2 JWT 认证,出站用 IAM SigV4。数据全程在用户 AWS 账户内。

六步实施

Step 1:Cognito 认证基础设施

import boto3

cognito = boto3.client('cognito-idp', region_name='us-east-1')

# 依次创建:User Pool → Domain → Resource Server → App Client
# 使用 client_credentials 模式(机器对机器认证)
pool = cognito.create_user_pool(PoolName='mcp-pool', ...)
cognito.create_user_pool_domain(UserPoolId=pool_id, Domain=domain_name)
cognito.create_resource_server(
    UserPoolId=pool_id, Identifier='MyGateway',
    Scopes=[{'ScopeName': 'invoke', 'ScopeDescription': 'Invoke tools'}]
)
client = cognito.create_user_pool_client(
    UserPoolId=pool_id, AllowedOAuthFlows=['client_credentials'], ...
)

Step 2:创建 Gateway

agentcore = boto3.client('bedrock-agentcore')
gateway = agentcore.create_gateway(
    name='mcp-gateway',
    roleArn='arn:aws:iam::ACCOUNT:role/gateway-role',
    protocolType='MCP',
    authorizerConfig={'type': 'COGNITO', ...}
)

Gateway Role 需要 trust policy 允许 bedrock-agentcore.amazonaws.com assume。

Step 3:MCP Server 容器化

from mcp.server import Server

server = Server("lark-tools")

@server.tool("send_message")
async def send_message(chat_id: str, content: str):
    # 调飞书 Open API
    ...

@server.tool("get_calendar")
async def get_calendar(date: str):
    ...

打 Docker 镜像推 ECR。

Step 4:部署到 Runtime

agentcore.create_runtime(
    name='lark-mcp',
    containerConfig={
        'imageUri': 'ECR_URI/lark-mcp:latest',
        'port': 8080,
        'healthCheckPath': '/health'
    }
)

Runtime 提供自动扩缩容和健康检查。

Step 5:注册 Target

agentcore.create_gateway_target(
    gatewayId=gateway_id,
    name='lark-tools',
    targetType='AGENTCORE_RUNTIME',
    runtimeId=runtime_id
)

Step 6:端到端验证

TOKEN=$(curl -s -X POST "${TOKEN_URL}" \
  -u "${CLIENT_ID}:${SECRET}" \
  -d "grant_type=client_credentials&scope=MyGateway/invoke" | jq -r '.access_token')

curl -X POST "${GATEWAY_URL}/mcp" \
  -H "Authorization: Bearer ${TOKEN}" \
  -d '{"method":"tools/list"}'

可复制性

这套模式可以封装任何内部系统为 MCP Server:

  • Jira / Confluence → 项目管理工具
  • Jenkins / GitHub Actions → CI/CD 工具
  • PostgreSQL / MySQL → 数据查询工具
  • 自定义 API → 任意封装

安全分层

  • Cognito JWT:客户端认证
  • IAM SigV4:服务间请求签名
  • VPC 隔离:容器网络安全
  • IAM Role:细粒度权限控制

适用场景

  1. 企业内部工具 AI 化——让 AI Agent 操作内部系统
  2. 多工具统一管理——Gateway 统一入口和认证
  3. 无服务器 MCP 部署——Runtime 自动扩缩容

官方原文:https://aws.amazon.com/cn/blogs/china/tool-mcp-server-amazon-bedrock-agentcore-quick/
AgentCore 文档:https://docs.aws.amazon.com/bedrock/latest/userguide/agentcore.html

posted @ 2026-05-14 00:32  亚马逊云开发者  阅读(17)  评论(0)    收藏  举报