Skip to content

TypeScript Strands Agent から MCP への接続

connection ジェネレーターを使用すると、TypeScript Strands Agent を MCP サーバー(TypeScript または Python)に接続できます。

このジェネレーターは、エージェントが MCP サーバーのツールを検出して呼び出すために必要なすべての配線を設定します。これは、AWS へのデプロイ時(Bedrock AgentCore 経由)とローカル実行時の両方で機能します。

このジェネレーターを使用する前に、以下を確認してください:

  1. Strands Agent コンポーネントを持つ TypeScript プロジェクト
  2. MCP サーバーコンポーネント(ts#mcp-server または py#mcp-server)を持つプロジェクト
  3. 両方のコンポーネントが computeType: BedrockAgentCoreRuntime で作成されていること
  1. インストール Nx Console VSCode Plugin まだインストールしていない場合
  2. VSCodeでNxコンソールを開く
  3. クリック Generate (UI) "Common Nx Commands"セクションで
  4. 検索 @aws/nx-plugin - connection
  5. 必須パラメータを入力
    • クリック Generate

    エージェントプロジェクトをソースとして、MCP サーバープロジェクトをターゲットとして選択します。プロジェクトに複数のコンポーネントが含まれている場合は、sourceComponenttargetComponent オプションを指定して明確にします。

    パラメータ デフォルト 説明
    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 パッケージを作成し、エージェントコードを変更します:

    • 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 ターゲットに依存
    • 必要な依存関係をインストール

    ジェネレーターはエージェントの agent.ts を変換して MCP サーバーのツールを使用します:

    packages/example/src/my-agent/agent.ts
    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 の一貫性を保証します。

    connection ジェネレーターを実行した後、エージェントに MCP サーバーを呼び出す権限を付与する必要があります:

    packages/infra/src/stacks/application-stack.ts
    const mcpServer = new MyMcpServer(this, 'MyMcpServer');
    const myAgent = new MyAgent(this, 'MyAgent');
    // Grant the agent access to bedrock models
    myAgent.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 server
    mcpServer.agentCoreRuntime.grantInvoke(myAgent.agentCoreRuntime);

    MCP サーバーの AgentCore ランタイム ARN は、生成された CDK コンストラクトによって Runtime Configurationconnection 名前空間に自動的に登録されるため、エージェントは実行時にそれを検出できます。

    ジェネレーターはエージェントの serve-local ターゲットを次のように設定します:

    1. 接続された MCP サーバーを自動的に起動
    2. SERVE_LOCAL=true を設定して、生成されたクライアントが AgentCore の代わりに直接 HTTP トランスポートを使用するようにする

    エージェントをローカルで実行するには:

    Terminal window
    pnpm nx <agent-name>-serve-local <project-name>

    これにより、エージェントとすべての接続された MCP サーバーの両方が起動し、エージェントは割り当てられたローカルポートで HTTP 経由で MCP サーバーに直接接続します。