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で作成されていること
ジェネレーターの実行
Section titled “ジェネレーターの実行”- インストール 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. |
ジェネレーターの出力
Section titled “ジェネレーターの出力”ジェネレーターは共有の agent-connection パッケージを作成し、エージェントコードを変更します:
Directorypackages/common/agent-connection
Directorysrc
Directoryapp
- <mcp-server-name>-client.ts 接続された MCP サーバー用の高レベルクライアント
Directorycore
- 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 の一貫性を保証します。
インフラストラクチャ
Section titled “インフラストラクチャ”connection ジェネレーターを実行した後、エージェントに 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 名前空間に自動的に登録されるため、エージェントは実行時にそれを検出できます。
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 名前空間に自動的に登録されるため、エージェントは実行時にそれを検出できます。
ローカル開発
Section titled “ローカル開発”ジェネレーターはエージェントの serve-local ターゲットを次のように設定します:
- 接続された MCP サーバーを自動的に起動
SERVE_LOCAL=trueを設定して、生成されたクライアントが AgentCore の代わりに直接 HTTP トランスポートを使用するようにする
エージェントをローカルで実行するには:
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 サーバーに直接接続します。