TypeScript Strands Agent đến MCP
Trình tạo connection có thể kết nối TypeScript Strands Agent của bạn với máy chủ MCP (có thể là TypeScript hoặc Python).
Trình tạo thiết lập tất cả các kết nối cần thiết để agent của bạn có thể khám phá và gọi các công cụ của máy chủ MCP, cả khi được triển khai lên AWS (thông qua Bedrock AgentCore) và khi chạy cục bộ.
Yêu cầu trước
Phần tiêu đề “Yêu cầu trước”Trước khi sử dụng trình tạo này, hãy đảm bảo bạn có:
- Một dự án TypeScript với thành phần Strands Agent
- Một dự án với thành phần máy chủ MCP (có thể là
ts#mcp-serverhoặcpy#mcp-server) - Cả hai thành phần được tạo với
computeType: BedrockAgentCoreRuntime
Cách sử dụng
Phần tiêu đề “Cách sử dụng”Chạy trình tạo
Phần tiêu đề “Chạy trình tạo”- Install the Nx Console VSCode Plugin if you haven't already
- Open the Nx Console in VSCode
- Click
Generate (UI)in the "Common Nx Commands" section - Search for
@aws/nx-plugin - connection - Fill in the required parameters
- Click
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:connectionYou can also perform a dry-run to see what files would be changed
pnpm nx g @aws/nx-plugin:connection --dry-runyarn nx g @aws/nx-plugin:connection --dry-runnpx nx g @aws/nx-plugin:connection --dry-runbunx nx g @aws/nx-plugin:connection --dry-runChọn dự án agent của bạn làm nguồn và dự án máy chủ MCP của bạn làm đích. Nếu các dự án của bạn chứa nhiều thành phần, hãy chỉ định các tùy chọn sourceComponent và targetComponent để phân biệt.
Tùy chọn
Phần tiêu đề “Tùy chọn”| Parameter | Type | Default | Description |
|---|---|---|---|
| sourceProject Required | string | - | The source project |
| targetProject Required | 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. |
Kết quả đầu ra của trình tạo
Phần tiêu đề “Kết quả đầu ra của trình tạo”Trình tạo tạo một gói agent-connection được chia sẻ và sửa đổi mã agent của bạn:
Thư mụcpackages/common/agent-connection
Thư mụcsrc
Thư mụcapp
- <mcp-server-name>-client.ts Client cấp cao cho máy chủ MCP được kết nối
Thư mụccore
- agentcore-mcp-client.ts Client MCP AgentCore cấp thấp với xác thực SigV4/JWT
- index.ts Xuất tất cả các client
- project.json
- tsconfig.json
Ngoài ra, nó:
- Chuyển đổi
agent.tscủa agent để import và sử dụng các công cụ của máy chủ MCP - Cập nhật target
serve-localcủa agent để phụ thuộc vào target serve của máy chủ MCP - Cài đặt các phụ thuộc cần thiết
Sử dụng máy chủ MCP đã kết nối
Phần tiêu đề “Sử dụng máy chủ MCP đã kết nối”Trình tạo chuyển đổi agent.ts của agent để sử dụng các công cụ của máy chủ 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], });};Tham số sessionId được truyền từ người gọi, đảm bảo tính nhất quán cho Bedrock AgentCore Observability.
Cơ sở hạ tầng
Phần tiêu đề “Cơ sở hạ tầng”Sau khi chạy trình tạo kết nối, bạn cần cấp quyền cho agent để gọi máy chủ 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);ARN runtime AgentCore của máy chủ MCP được tự động đăng ký trong namespace connection của Runtime Configuration bởi CDK construct được tạo, để agent có thể khám phá nó khi chạy.
Sau khi chạy trình tạo kết nối, bạn cần cấp quyền cho agent để gọi máy chủ MCP trong cấu hình Terraform của bạn:
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}ARN runtime AgentCore của máy chủ MCP được tự động đăng ký trong namespace connection của Runtime Configuration bởi module Terraform được tạo, để agent có thể khám phá nó khi chạy.
Phát triển cục bộ
Phần tiêu đề “Phát triển cục bộ”Trình tạo cấu hình target serve-local của agent để:
- Tự động khởi động (các) máy chủ MCP đã kết nối
- Đặt
SERVE_LOCAL=trueđể client được tạo sử dụng truyền tải HTTP trực tiếp thay vì AgentCore
Chạy agent cục bộ với:
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>Điều này sẽ khởi động cả agent và tất cả các máy chủ MCP đã kết nối, với agent kết nối trực tiếp đến các máy chủ MCP qua HTTP trên các cổng cục bộ được chỉ định của chúng.