Configure GW-to-GW authentication and routing

Build and validate secure Gateway-to-Gateway MCP workflows between two companies, using the Caller Context, Target Authentication, and Policy elements, with practical scenarios for routing, credential injection, and troubleshooting.

By the end of this guide, you will have validated Caller Context, Target Authentication, and Policy enforcement across a live GW-to-GW MCP connection, with log evidence confirming behaviour at each hop.

This guide adds a Caller Context element with an API Key (secret store) method and a Policy element to Agent GW 1’s MCP surface, and a Caller Context element and a Target Authentication configuration to Agent GW 2’s MCP surface, then works through scenarios that exercise each combination end-to-end. For background on how two gateways route traffic to each other, see Gateways →.

Without validating these independently, a mismatched HTTP Header Name, a missing Policy element, or an incorrect secret between the two gateways is only visible in a live cross-organisation failure. Each scenario isolates one failure mode so problems are caught in a controlled environment before they reach production traffic.

Use this guide when:

  • You have connected two gateways and need to confirm authentication behaviour end-to-end.
  • You are building a cross-organisation MCP workflow and need a validation checklist before deployment.

To validate routing on its own, before adding authentication, run Scenario 2 first. It says which parts of the steps to complete before it.

What you are building

  • Company A hosts Agent GW 1 with MCP surface A.
  • Company B hosts Agent GW 2 with MCP surface B.
  • Gateways are connected through a DIDComm gateway connection.
  • Agent GW 2 routes requests to Agent GW 1.
  • Agent GW 1 forwards to an MCP backend service.

This pattern supports automated AI-driven workflows across company boundaries while keeping authentication and policy controls explicit.

Architecture overview

flowchart LR
  C[Client or Agent in Company B] --> B[Agent GW 2\nMCP Surface B]
  B -->|Gateway connection routing| A[Agent GW 1\nMCP Surface A]
  A -->|Target endpoint| M[MCP backend service]

  subgraph Auth checks
    S1[Caller Context + Policy on Surface A]
    T1[Target Authentication on Surface B's Managed Agent]
  end

  B --> T1
  A --> S1

How authentication is enforced across the two hops

Three elements apply at different points in the request path:

  • Caller Context: a canvas element dropped on the Access Point → Managed Agent edge. It extracts and validates an inbound caller’s credentials (JWT, API key, or DID Auth) before any policy or payment gate runs. mTLS is visible in the Authentication Method dropdown and the Add method menu but not currently selectable. On a closed Access Point, the default for surfaces created from version 1.0.4, a missing or invalid credential gets 401 before policy runs, and a surface without Caller Context answers every request with 503, including requests that arrive over a gateway connection. See Access Point readiness.
  • Policy: a canvas element on the same edge that reads the Caller Context result (input.source_auth) and decides whether to allow or deny the request. On a public or legacy Access Point, a missing or invalid credential is recorded as input.source_auth.method == "failed" and the request reaches the Managed Agent unless a Policy element denies it explicitly. See OPA policies reference for the full input.source_auth schema.
  • Target Authentication: a section on the Managed Agent node. It injects credentials into outbound traffic before the gateway forwards to a target endpoint, whether that target is a direct URL or another gateway reached via a gateway connection.

This guide sends a fixed API key from a gateway secret. A caller’s delegated credentials from Credential Delegation are handled separately: they cross a gateway connection only when the route’s Send delegated credentials over Fabric switch is on, and it is off by default. See Managed Agent and Transit Points.

In GW-to-GW MCP flows, use this mental model:

  1. Agent GW 2 receives a request on MCP surface B. Surface B’s own Caller Context authenticates the caller.
  2. Agent GW 2 removes the header its Caller Context read, and any Authorization header, from the request. If surface B’s Managed Agent node has Target Authentication configured, Agent GW 2 adds the configured header, then forwards the request over the gateway connection.
  3. Agent GW 1 receives the forwarded request on MCP surface A. Surface A’s Caller Context validates the credential it reads. A closed surface A refuses a missing or invalid credential with 401. The Policy element then decides whether to allow or deny an authenticated caller.
  4. If the request is allowed, Agent GW 1 forwards it to the MCP backend using its own Managed Agent node’s endpoint configuration.
  5. MCP backend logs show the request and any injected header values expected by your test.

Because Agent GW 2 removes the header its own Caller Context read, the two surfaces read different headers in this guide: X-Client-Key on Surface B and X-API-Key on Surface A.

Prerequisites

  • You are logged in to the Agent Gateway dashboard for both Agent GW 1 and Agent GW 2, with permission to create and edit surfaces.
  • Agent GW 1 and Agent GW 2 are already connected: open Connections → Gateways on either gateway and confirm the connection status badge reads ACTIVE.
  • Agent GW 1 exposes Surface A to Agent GW 2: on Agent GW 1, the Publishing tab of Agent GW 2’s gateway record has Selected surfaces with Surface A checked, or All surfaces. A newly paired gateway reaches no surface until one is exposed. See Connect two gateways.
  • Both gateways have MCP surfaces created:
    • Surface A on Agent GW 1.
    • Surface B on Agent GW 2.
  • MCP backend service reachable from Agent GW 1’s Managed Agent target endpoint.
  • You can view:
    • Gateway logs/monitoring.
    • MCP backend logs.
  • Gateway secrets that hold the test API key values, each with Secret Type ApiKey so that the Caller Context element lists it. Follow Secrets to create them:
    • On Agent GW 1, a secret holding Surface A’s key (for example, keyA). You select it in Step 1.
    • On Agent GW 2, a secret holding Surface B’s key (for example, keyB). You select it in Step 2.
    • If you are testing Scenario 7 or 8 (Target Authentication), a secret on Agent GW 2 holding keyA too.

Steps

Configure Agent GW 1’s MCP surface A

  1. Open Surfaces on Agent GW 1’s dashboard and select MCP surface A.

  2. Select the Managed Agent node and set Target Endpoint URL to your MCP backend address.

  3. In the left palette, drag a Caller Context element onto the Access Point → Managed Agent edge.

  4. Select the Caller Context node. On the method row, select Configure ↗ to open the full editor.

  5. In Authentication Method, select API Key (secret store), then set:

    • HTTP Header Name: the header callers must include in every request, for example X-API-Key.
    • Secret ID: the secret holding keyA that you created in Prerequisites.
  6. Close the editor.

  7. Add a Policy element from the Security & Policy palette category and drop it on the same edge as Caller Context. Set Policy Definition to a policy with this rule.

    package surface.policy
    
    default allow = false
    
    allow if {
      input.source_auth.method == "api_key"
    }

    A closed Access Point refuses a missing or invalid key before this policy runs. The policy also denies such a caller if Surface A’s Access Point is public or legacy, where a failed key would otherwise reach the MCP backend.

  8. Select the disk-icon Save button (tooltip: Save changes).

Configure Agent GW 2’s MCP surface B

  1. Open Surfaces on Agent GW 2’s dashboard and select MCP surface B.
  2. Select the Managed Agent node.
  3. Set Endpoint Type to via Gateway Connection.
  4. Select Agent GW 1 from the Gateway dropdown, then select Surface A from the Agent Surface on Gateway dropdown.
  5. Drag a Caller Context element onto the Access Point → Managed Agent edge. Select it, and on the method row, select Configure ↗. In Authentication Method, select API Key (secret store), set HTTP Header Name to X-Client-Key, and select the secret holding keyB in Secret ID. Close the editor. Surface B needs its own caller authentication to accept callers, and a header different from Surface A’s, because Agent GW 2 removes the header this element reads before forwarding.
  6. For scenarios that exercise target-side authentication (Scenarios 7 and 8): under Target Authentication, set Authentication Type to API Key, then select Configure….
  7. In the full editor, set:
    • Credential Source: Static secret.
    • Secret: the secret holding keyA on Agent GW 2 (see Prerequisites).
    • Header Name: must match the HTTP Header Name configured on Surface A’s Caller Context element, for example X-API-Key.
  8. Close the editor.
  9. Select the disk-icon Save button (tooltip: Save changes).
Target Authentication editor with Authentication Type set to API Key, a selected Secret, and the Header Name field set to X-API-Key

Scenario playbooks

Each scenario below is written as a runbook: setup, action, expected outcome, and validation checks.

Scenario 1: Target Authentication and secret injection

Given: Agent GW 1’s MCP surface A has Target Authentication configured (Authentication Type: API Key) on its own Managed Agent node, pointing at the MCP backend.

When: A request with a valid X-API-Key is sent through Agent GW 1’s MCP surface A.

Then:

  • The gateway injects the configured header before the backend call.
  • MCP backend logs include the expected header value.

Validate:

  1. Send an MCP request through Agent GW 1’s MCP surface A.
  2. Confirm the request succeeds.
  3. Check the backend log for the configured header name and value pattern.

Scenario 2: Gateway-to-Gateway communication

Given: Agent GW 2 is connected to Agent GW 1, and MCP surface B routes to MCP surface A. Neither surface has a Caller Context, Policy, or Target Authentication yet, so run this scenario before you add them in Steps 1 and 2:

  1. Set Surface A’s Target Endpoint URL (Step 1, item 2) and Surface B’s gateway route (Step 2, items 3 and 4).
  2. A closed surface without caller authentication answers every request with 503, so switch on Public access on both Access Points and save.
  3. After this scenario, switch Public access off on both Access Points, then complete Steps 1 and 2 and save.

When: Agent GW 2 sends a request to Agent GW 1.

Then:

  • The request is processed successfully end to end.
  • MCP backend logs are visible.

Validate:

  1. Send a request to Agent GW 2’s MCP surface B.
  2. Confirm the response is a success.
  3. Confirm Agent GW 1’s logs show the routed inbound request.
  4. Confirm the MCP backend request log exists.

Scenario 3: Missing source authentication

Given: Agent GW 1’s MCP surface A has a Caller Context element (API Key (secret store)) and a Policy element that denies when authentication fails.

When: A request is sent without the API key header.

Then:

  • Surface A refuses the caller before the Policy element runs.
  • The caller receives 401 with “Caller credential missing or invalid”.

Validate:

  1. Remove the X-API-Key header from the test request.
  2. Send the request through Agent GW 1 directly, or through Agent GW 2 routing to Surface A with a valid X-Client-Key.
  3. Confirm the response is 401.
  4. Confirm Agent GW 1’s logs show the failed source authentication for Surface A.

If Surface A’s Access Point is public or legacy, the request reaches policy instead, recorded as input.source_auth.method == "failed", and the Policy element denies it with 403. See Troubleshooting.

Scenario 4: Valid source authentication

Given: Agent GW 1’s MCP surface A has a Caller Context element (API Key (secret store)) and a Policy element, as in Scenario 3.

When: A request is sent with a valid API key.

Then:

  • Caller Context records input.source_auth.method == "api_key"; the Policy element allows the request.
  • The request is processed successfully.

Validate:

  1. Send a request with the correct header, for example X-API-Key: keyA (or your configured header name).
  2. Confirm a success response.
  3. Confirm Agent GW 1’s logs show the allowed Policy decision.

Scenario 5: Cross-gateway request with valid API key

Given:

  • Agent GW 1’s MCP surface A requires keyA in X-API-Key (Caller Context and Policy, as in Scenario 3).
  • Agent GW 2’s MCP surface B requires keyB in X-Client-Key.

When: A client sends a request to Agent GW 2’s MCP surface B with both keys, and Agent GW 2 forwards it to Agent GW 1.

Then:

  • The request succeeds end to end.

Validate:

  1. Send a request to MCP surface B with X-Client-Key: keyB and X-API-Key: keyA. The client can set X-API-Key itself, or Target Authentication can inject it (see Scenario 7).
  2. Confirm the response is a success.
  3. Confirm Agent GW 1’s Policy decision is an allow.

Scenario 6: Cross-gateway request without API key

Given:

  • Agent GW 1’s MCP surface A requires keyA (Caller Context and Policy, as in Scenario 3).
  • Agent GW 2’s MCP surface B requires keyB, and has no Target Authentication.

When: A client sends a request to MCP surface B with keyB but without keyA.

Then:

  • Surface A on Agent GW 1 refuses the request with 401 before policy runs.

Validate:

  1. Send a request to MCP surface B with X-Client-Key: keyB and no X-API-Key.
  2. Confirm the response is 401.
  3. Confirm Agent GW 1’s logs show the failed source authentication for Surface A.

Scenario 7: Target Authentication configuration

Given:

  • Agent GW 1’s MCP surface A requires API key keyA (Caller Context and Policy, as in Scenario 3).
  • Agent GW 2’s MCP surface B Managed Agent node has Target Authentication configured: Authentication Type API Key, Header Name matching Surface A’s HTTP Header Name, Secret holding keyA.

When: A client sends a request to MCP surface B with X-Client-Key: keyB, without setting X-API-Key itself.

Then:

  • Agent GW 2 injects the header automatically before forwarding.
  • The request succeeds without the client manually adding an API key on every call.

Validate:

  1. Confirm Target Authentication is configured on Surface B’s Managed Agent node with the matching secret.
  2. Send a request to MCP surface B with X-Client-Key: keyB and no X-API-Key header.
  3. Confirm a success response.
  4. Confirm Agent GW 1’s Policy decision is an allow.

Scenario 8: Incorrect authentication configuration

Given:

  • Agent GW 1’s MCP surface A requires API key keyA (Caller Context and Policy, as in Scenario 3).
  • Agent GW 2’s MCP surface B Target Authentication is misconfigured (wrong Header Name, wrong Secret, or an Authentication Type Surface A does not expect).

When: A request is sent.

Then:

  • The response is 401, because Surface A refuses the missing or wrong credential before policy runs.

Validate:

  1. Misconfigure one field intentionally on Surface B’s Target Authentication (wrong Header Name, wrong Secret, or wrong Authentication Type).
  2. Send the request with X-Client-Key: keyB.
  3. Confirm the response is 401.
  4. Confirm Agent GW 1’s logs show the failed source authentication for Surface A.

If Surface A’s Access Point is public or legacy, the mismatch reaches policy instead, and only the Policy element turns it into a 403. Without that element, an incorrect key reaches the backend with no error.

Confirm

Test 1: cross-gateway request without a valid key returns 401

From Scenario 3 or Scenario 6, Surface A should have refused the request with 401 when the X-API-Key header was absent. Confirm Agent GW 1’s logs show the failed source authentication for that request.

Test 2: cross-gateway request with a valid key succeeds end-to-end

From Scenario 4 or Scenario 5, the request with the correct API key header should have succeeded. Confirm that Agent GW 2, Agent GW 1, and the MCP backend all show the request reaching the backend.

Test 3: Target Authentication injection is visible in backend logs

From Scenario 1 or Scenario 7, the MCP backend logs should include the expected header and its value. If the header is absent, Target Authentication is not configured on the correct hop’s Managed Agent node.

Logging and observability

Before running the scenarios, confirm a successful request produces one log trail on Agent GW 2 (outbound) and Agent GW 1 (inbound/forward), and that the MCP backend logs include request receipt. Configuration changes made in the surface builder apply immediately through hot reload after you save, so you can move straight from a save action to a test request.

Use logs from three places for every test:

  1. Agent GW 2 logs: request accepted, routing decision to the gateway connection, outbound attempt, and any Target Authentication header injection.
  2. Agent GW 1 logs: Caller Context validation result, Policy decision, inbound handling, forward decision.
  3. MCP backend logs: request arrival and expected injected header behaviour.

Best practices:

  • Keep a consistent correlation key in request payload/metadata for traceability.
  • Validate both status code and log trail before marking a test as passed.
  • Store auth-related logs in your runbook evidence for operational audits.

Troubleshooting

SymptomLikely causeFix
Request from B to A fails with 401 unexpectedlyHTTP Header Name or Secret ID on Surface A’s Caller Context does not match what Surface B sendsConfirm the HTTP Header Name and Secret ID configured on Surface A’s Caller Context match the Header Name and Secret configured in Surface B’s Target Authentication, then resend.
Request from B to A fails with 401 although the client sends keyASurface B’s Caller Context reads the same header as Surface A’s, so Agent GW 2 removes it before forwardingSet a different HTTP Header Name on Surface B’s Caller Context, such as X-Client-Key, or inject keyA with Surface B’s Target Authentication.
503 with “This Access Point is not available.”The surface that answered has no caller authentication, so it is not ready for external accessAdd a Caller Context element to that surface and save. Over a gateway connection, the 503 can come from Surface A.
Request succeeds with 200 even though the API key is missing or wrongSurface A’s Access Point is public or legacy, and no Policy element denies input.source_auth.method == "failed"Switch off Public access on Surface A, or select Close Access Point in its legacy notice, and save. Alternatively, add the Policy element from Configure Agent GW 1’s MCP surface A.
Request fails after enabling Target AuthenticationSecret mismatched, or the wrong Header Name/Authentication Type is set on Surface B’s Managed Agent nodeRe-check the Secret and Header Name in Surface B’s Target Authentication editor.
No logs in backendRouting not pointing to the intended target endpointRe-check Surface A’s Managed Agent Target Endpoint URL, and Surface B’s Gateway / Agent Surface on Gateway selection.
Surface A is missing from the Agent Surface on Gateway dropdown on Agent GW 2Agent GW 1 doesn’t expose Surface A to Agent GW 2On Agent GW 1, open Agent GW 2’s gateway record, select Selected surfaces with Surface A checked (or All surfaces) on the Publishing tab, and select Save Exposure.
Intermittent pass/fail in testsMixed test inputs and stale browser/session stateUse explicit test payloads and rerun with clean request inputs.

Implementation notes from field experience

  • Start with one strict, Policy-enforced boundary first (Caller Context and Policy on Agent GW 1), then layer Target Authentication injection on Agent GW 2.
  • Use one canonical API key per scenario set during initial testing to reduce false negatives.
  • Keep the HTTP Header Name explicit and documented across teams. Most cross-company failures come from a header name or secret mismatch, not routing.
  • On a public or legacy Access Point, a Caller Context element without a Policy element is the most common false negative: the scenario looks like it passed because nothing crashed, but the key was never actually enforced.
  • Validate with logs at every hop, not just response status.

Next steps

  • Connect two gateways: create and approve the gateway connection tunnel first.
  • OPA policies reference: the full input.source_auth schema and the Policy element’s fields.
  • Secrets: store the credentials used by Caller Context and Target Authentication.
  • Observability: understand how to monitor runtime request and auth behaviour.