Skip to content

React Website to AgentCore Gateway

The connection generator can connect a React website to the agents fronted by an AgentCore Gateway generated with protocol: http.

The browser talks only to the Gateway — requests to <gatewayUrl>/<targetName>/invocations are proxied to the agent runtime behind it. Because the website never needs to reach the runtimes directly, the agents can be deployed inside a VPC, with the Gateway as the single governed entry point providing authentication and observability.

Before using this generator, ensure you have:

  1. A React website project
  2. An agentcore-gateway project generated with protocol: http
  3. At least one AG-UI or HTTP agent attached to the Gateway via the Gateway to agent connection generator

The generator creates a website client for each AG-UI or HTTP agent attached to the Gateway (A2A targets are skipped — they speak agent-to-agent JSON-RPC, not a browser protocol). It generates the same clients as connecting the website to each agent directly (AG-UI/CopilotKit for AG-UI agents, an OpenAPI client for Python HTTP agents), routed through the Gateway rather than at the runtime.

  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 the website project as the source and the Gateway project as the target.

    ParameterTypeDefaultDescription
    sourceProject Requiredstring-The source project
    targetProject Requiredstring-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 booleantrueWhether 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.

    For each fronted AG-UI agent, the same files as the React to AG-UI agent connection are generated (an AguiProvider, a useAgui<Agent> hook and a themed CopilotKit component module), with the hook reading the Gateway’s URL from runtime configuration:

    • Directorypackages/<website>/src
      • Directorycomponents
        • AguiProvider.tsx CopilotKit provider registering each agent
        • Directorycopilot/ Themed chat components
      • Directoryhooks
        • useAgui<Agent>.tsx Connects to the agent via the Gateway’s /<targetName>/invocations route

    For each fronted Python HTTP agent, the same files as the React to Python agent connection are generated (a type-safe OpenAPI client, hooks, and a provider), with the client’s base URL routed through the Gateway.

    The Gateway’s URL is registered in the connection.gateways.<GatewayClassName> namespace of Runtime Configuration, so it is published to the website’s runtime-config.json.

    For an IAM Gateway, grant the website’s authenticated users permission to invoke it (a Cognito Gateway needs no grant — the browser authenticates with its JWT bearer token instead):

    packages/infra/src/stacks/application-stack.ts
    const identity = new UserIdentity(this, 'UserIdentity');
    new Website(this, 'Website');
    const myGateway = new MyGateway(this, 'MyGateway');
    myGateway.addAgent(myAgent);
    myGateway.grantInvokeAccess(identity.identityPool.authenticatedRole);

    The website’s dev target gains a dependency on the Gateway project’s dev target, which starts the local gateway plus every attached agent. The website’s runtime configuration is overridden in local-dev mode to point the Gateway’s URL at the local gateway, so the browser exercises the same /<targetName>/invocations routes locally:

    Terminal window
    pnpm nx dev <website-name>

    The generator connects the website to the agents attached to the Gateway at the time it runs. After attaching another agent to the Gateway, re-run the connection generator to generate its website client — existing clients are left untouched.