TypeScript Strands Agent 到 MCP
connection 生成器可以将您的 TypeScript Strands Agent 连接到 MCP 服务器(TypeScript 或 Python)。
该生成器会设置所有必要的连接,使您的智能体能够发现并调用 MCP 服务器的工具,无论是部署到 AWS(通过 Bedrock AgentCore)还是在本地运行时。
在使用此生成器之前,请确保您具有:
- 一个包含 Strands Agent 组件的 TypeScript 项目
- 一个包含 MCP 服务器组件的项目(
ts#mcp-server或py#mcp-server) - 两个组件都使用
computeType: BedrockAgentCoreRuntime创建
- 安装 Nx Console VSCode Plugin 如果您尚未安装
- 在VSCode中打开Nx控制台
- 点击
Generate (UI)在"Common Nx Commands"部分 - 搜索
@aws/nx-plugin - connection - 填写必需参数
- 点击
Generate
pnpm nx g @aws/nx-plugin:connectionyarn nx g @aws/nx-plugin:connectionnpx nx g @aws/nx-plugin:connectionbunx nx g @aws/nx-plugin:connection选择您的智能体项目作为源,MCP 服务器项目作为目标。如果您的项目包含多个组件,请指定 sourceComponent 和 targetComponent 选项以消除歧义。
| 参数 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| sourceProject 必需 | string | - | The source project |
| targetProject 必需 | string | - | The target project to connect to |
| sourceComponent | string | - | The source component to connect from (component name, path relative to source project root, or generator id). Use '.' to explicitly select the project as the source. |
| targetComponent | string | - | The target component to connect to (component name, path relative to target project root, or generator id). Use '.' to explicitly select the project as the target. |
生成器会创建一个共享的 agent-connection 包并修改您的智能体代码:
文件夹packages/common/agent-connection
文件夹src
文件夹app
- <mcp-server-name>-client.ts 连接的 MCP 服务器的高级客户端
文件夹core
- agentcore-mcp-client.ts 带有 SigV4/JWT 身份验证的低级 AgentCore MCP 客户端
- index.ts 导出所有客户端
- project.json
- tsconfig.json
此外,它还会:
- 转换您的智能体的
agent.ts以导入和使用 MCP 服务器的工具 - 更新智能体的
serve-local目标以依赖于 MCP 服务器的 serve 目标 - 安装所需的依赖项
使用已连接的 MCP 服务器
Section titled “使用已连接的 MCP 服务器”生成器会转换您的智能体的 agent.ts 以使用 MCP 服务器的工具:
import { Agent, tool } from '@strands-agents/sdk';import { MyMcpServerClient } from ':my-scope/agent-connection';
export const getAgent = async (sessionId: string) => { const myMcpServerClient = await MyMcpServerClient.create(sessionId); return new Agent({ systemPrompt: '...', tools: [myMcpServerClient], });};sessionId 参数从调用者传递过来,确保 Bedrock AgentCore Observability 的一致性。
运行连接生成器后,您需要授予智能体调用 MCP 服务器的权限:
const mcpServer = new MyMcpServer(this, 'MyMcpServer');const myAgent = new MyAgent(this, 'MyAgent');
// Grant the agent access to bedrock modelsmyAgent.agentCoreRuntime.addToRolePolicy( new PolicyStatement({ actions: ['bedrock:InvokeModel', 'bedrock:InvokeModelWithResponseStream'], resources: ['arn:aws:bedrock:*:*:foundation-model/*', 'arn:aws:bedrock:*:*:inference-profile/*'], }),);
// Grant the agent permissions to invoke the MCP servermcpServer.agentCoreRuntime.grantInvoke(myAgent.agentCoreRuntime);MCP 服务器的 AgentCore 运行时 ARN 会由生成的 CDK 构造自动注册到 Runtime Configuration 的 connection 命名空间中,以便智能体可以在运行时发现它。
运行连接生成器后,您需要在 Terraform 配置中授予智能体调用 MCP 服务器的权限:
module "inventory_mcp_server" { source = "../../common/terraform/src/app/mcp-servers/inventory-mcp"}
module "story_agent" { source = "../../common/terraform/src/app/agents/story-agent"}
# Grant the agent permissions to invoke the MCP serverresource "aws_iam_policy" "agent_invoke_mcp" { name = "AgentInvokeMcpPolicy" policy = jsonencode({ Version = "2012-10-17" Statement = [{ Effect = "Allow" Action = "bedrock-agentcore:InvokeAgent" Resource = module.inventory_mcp_server.agent_core_runtime_arn }] })}
resource "aws_iam_role_policy_attachment" "agent_invoke_mcp" { role = module.story_agent.agent_core_runtime_role_arn policy_arn = aws_iam_policy.agent_invoke_mcp.arn}MCP 服务器的 AgentCore 运行时 ARN 会由生成的 Terraform 模块自动注册到 Runtime Configuration 的 connection 命名空间中,以便智能体可以在运行时发现它。
生成器会配置智能体的 serve-local 目标以:
- 自动启动已连接的 MCP 服务器
- 设置
SERVE_LOCAL=true,使生成的客户端使用直接 HTTP 传输而不是 AgentCore
使用以下命令在本地运行智能体:
pnpm nx <agent-name>-serve-local <project-name>yarn nx <agent-name>-serve-local <project-name>npx nx <agent-name>-serve-local <project-name>bunx nx <agent-name>-serve-local <project-name>这将同时启动智能体和所有已连接的 MCP 服务器,智能体通过 HTTP 在其分配的本地端口上直接连接到 MCP 服务器。