Implementation:A2aproject A2a java Client Auth Interception
| Knowledge Sources | |
|---|---|
| Domains | Client, Security, Authentication |
| Last Updated | 2026-10-30 00:00 GMT |
Overview
Concrete client call interceptor SPI and authentication interceptor that automatically adds auth credentials (Bearer, Basic, API key) to requests based on agent security schemes.
Description
The ClientCallInterceptor is the abstract base class for intercepting all client transport calls. The AuthInterceptor extends it to automatically inject authentication headers based on the agent's declared SecurityRequirements. It supports:
- Bearer tokens: `Authorization: Bearer {token}` for HTTP/OAuth2 schemes
- Basic auth: `Authorization: Basic {base64}` for HTTP basic scheme
- API keys: Injection into a safe-list of headers (`x-api-key`, `api-key`, `x-auth-token`, etc.) — restricted to prevent credential leakage through malicious header names
Credentials are retrieved from a CredentialService (per-call, keyed by session). The interceptor reads the agent card's security requirements and schemes, matching them to available credentials.
Usage
- Secured agents: Add to transport config when the agent requires authentication
- Custom auth: Extend ClientCallInterceptor for custom auth protocols (mTLS, signing, etc.)
- Credential management: Implement CredentialService to supply credentials per session/context
Code Reference
Source Location
- Repository: a2a-java
- Files:
- `client/transport/spi/src/main/java/org/a2aproject/sdk/client/transport/spi/interceptors/ClientCallInterceptor.java` (L1-27)
- `client/transport/spi/src/main/java/org/a2aproject/sdk/client/transport/spi/interceptors/auth/AuthInterceptor.java` (L1-119)
Signature
// Base interceptor SPI
public abstract class ClientCallInterceptor {
public abstract PayloadAndHeaders intercept(String methodName,
@Nullable Object payload, Map<String, String> headers,
@Nullable AgentCard agentCard,
@Nullable ClientCallContext clientCallContext);
}
// Authentication interceptor
public class AuthInterceptor extends ClientCallInterceptor {
public static final String AUTHORIZATION = "Authorization";
public AuthInterceptor(final CredentialService credentialService);
@Override
public PayloadAndHeaders intercept(String methodName,
@Nullable Object payload, Map<String, String> headers,
@Nullable AgentCard agentCard,
@Nullable ClientCallContext clientCallContext);
}
Import
import org.a2aproject.sdk.client.transport.spi.interceptors.ClientCallInterceptor;
import org.a2aproject.sdk.client.transport.spi.interceptors.auth.AuthInterceptor;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| methodName | String | Yes | A2A method being called |
| payload | Object | No | Request payload |
| headers | Map<String, String> | Yes | Existing headers |
| agentCard | AgentCard | No | Agent card with security schemes |
| credentialService | CredentialService | Yes (Auth) | Supplies credentials |
Outputs
| Name | Type | Description |
|---|---|---|
| returns | PayloadAndHeaders | Payload with updated headers |
Usage Examples
Adding Auth to Transport Config
import org.a2aproject.sdk.client.transport.spi.interceptors.auth.AuthInterceptor;
import org.a2aproject.sdk.client.transport.spi.interceptors.auth.InMemoryContextCredentialService;
InMemoryContextCredentialService credentials = new InMemoryContextCredentialService();
credentials.setCredential(sessionKey, "my-bearer-token");
AuthInterceptor authInterceptor = new AuthInterceptor(credentials);
JSONRPCTransportConfig config = new JSONRPCTransportConfigBuilder()
.addInterceptor(authInterceptor)
.build();
Client client = Client.builder(agentCard)
.withTransport(JSONRPCTransport.class, config)
.build();
Custom Interceptor
import org.a2aproject.sdk.client.transport.spi.interceptors.ClientCallInterceptor;
public class TracingInterceptor extends ClientCallInterceptor {
@Override
public PayloadAndHeaders intercept(String methodName,
@Nullable Object payload, Map<String, String> headers,
@Nullable AgentCard agentCard,
@Nullable ClientCallContext ctx) {
headers.put("X-Trace-Id", generateTraceId());
return new PayloadAndHeaders(payload, headers);
}
}