Skip to content

Python Agent to MCP

The connection generator can connect your Python Agent to an MCP server (either TypeScript or Python).

The generator sets up all the necessary wiring so your agent can discover and invoke the MCP server’s tools, both when deployed to AWS (via Bedrock AgentCore) and when running locally.

Before using this generator, ensure you have:

  1. A Python project with a Python Agent component (Strands or LangChain)
  2. A project with an MCP server component (either ts#mcp-server or py#mcp-server)
  3. Both components created with infra: agentcore
  1. Install the Nx Console VSCode Plugin if you haven't already
  2. Open the Nx Console in VSCode
  3. Click Generate (UI) in the "Common Nx Commands" section
  4. Search for @aws/nx-plugin - connection
  5. Fill in the required parameters
    • Click Generate

    Select your agent project as the source and your MCP server project as the target. If your projects contain multiple components, specify the sourceComponent and targetComponent options to disambiguate.

    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.
    preferInstallDependencies boolean true Whether to prefer installing dependencies after the generator runs. Set to false to defer installing when batching multiple generators (an install still runs if needed so subsequent generators can compute the Nx project graph); install once at the end.

    The generator creates a shared agent_connection Python project at packages/common/agent_connection/ (if it doesn’t already exist). Per-connection client modules are generated into this shared project:

    • Directorypackages/common/agent_connection
      • Directory<scope>_agent_connection
        • __init__.py Re-exports per-connection clients
        • Directorycore
          • agentcore_endpoints.py Framework-agnostic ARN/URL resolution
          • agentcore_mcp_transport.py Framework-agnostic MCP transport
          • agentcore_mcp_client_<framework>.py MCP client wrapping the transport for your agent’s framework
          • Directoryauth/ Framework-agnostic SigV4 / session-forwarding httpx.Auth
        • Directoryapp
          • <mcp_server_name>_client_<framework>.py Per-connection client for each MCP server

    The client suffix matches your agent’s framework (_strands or _langchain).

    Additionally, the generator:

    • Transforms your agent’s agent.py to import and use the MCP server’s tools via a class-based client
    • Adds the agent_connection project as a workspace dependency of your agent project
    • Updates the agent’s dev target to depend on the MCP server’s serve target

    The generator transforms your agent’s agent.py to use the MCP server’s tools:

    packages/my-project/my_module/agent/agent.py
    from contextlib import contextmanager
    from strands import Agent
    from my_scope_agent_connection import MyMcpServerClientStrands
    @contextmanager
    def get_agent():
    my_mcp_server = MyMcpServerClientStrands.create()
    with (
    my_mcp_server,
    ):
    yield Agent(
    system_prompt="...",
    tools=[*my_mcp_server.list_tools_sync()],
    )

    The Strands client is a context manager, entered in a with block around the agent.

    The AgentCore session ID is propagated to the MCP server automatically via the X-Amzn-Bedrock-AgentCore-Runtime-Session-Id header for both frameworks, ensuring consistency for Bedrock AgentCore Observability.

    After running the connection generator, you need to grant the agent permission to invoke the MCP server:

    packages/infra/src/stacks/application-stack.ts
    const mcpServer = new MyMcpServer(this, 'MyMcpServer');
    const myAgent = new MyAgent(this, 'MyAgent');
    // Grant the agent permissions to invoke the MCP server
    mcpServer.grantInvokeAccess(myAgent);

    The MCP server’s AgentCore runtime ARN is automatically registered in the agentcore namespace of Runtime Configuration by the generated CDK construct, so the agent can discover it at runtime.

    The generator configures the agent’s dev target to:

    1. Start the connected MCP server(s) automatically
    2. Set LOCAL_DEV=true so the generated client uses direct HTTP transport instead of AgentCore

    Run the agent locally with:

    Terminal window
    pnpm nx <agent-name>-dev <project-name>

    This will start both the agent and all connected MCP servers, with the agent connecting to the MCP servers directly via HTTP on their assigned local ports.