Overview
Concrete gRPC client transport implementation for A2A communication using protobuf-encoded binary protocol over gRPC stubs.
Description
The GrpcTransport class implements the ClientTransport SPI using gRPC blocking and async stubs from A2AServiceGrpc. It converts A2A spec records to and from protobuf messages via ProtoUtils mappers, attaches gRPC metadata for authorization, extensions, and protocol version headers, and uses StreamObserver for streaming operations. gRPC status codes are mapped to A2A exceptions via GrpcErrorMapper. The transport supports multitenancy by resolving the tenant from the request or falling back to the agent's default tenant.
Usage
- Binary protocol efficiency: Use when gRPC is preferred over JSON-RPC/REST for lower latency and smaller payloads
- Streaming task updates: Uses server-streaming gRPC for `sendMessageStreaming` and `subscribeToTask`
- Multitenant agents: Pass an `agentTenant` to the constructor for tenant-aware routing
Code Reference
Source Location
- Repository: a2a-java
- File: `client/transport/grpc/src/main/java/org/a2aproject/sdk/client/transport/grpc/GrpcTransport.java`
- Lines: L60-471
Signature
public class GrpcTransport implements ClientTransport {
public GrpcTransport(Channel channel, AgentCard agentCard);
public GrpcTransport(Channel channel, AgentCard agentCard,
@Nullable String agentTenant,
@Nullable List<ClientCallInterceptor> interceptors);
EventKind sendMessage(MessageSendParams request, @Nullable ClientCallContext context)
throws A2AClientException;
void sendMessageStreaming(MessageSendParams request,
Consumer<StreamingEventKind> eventConsumer,
Consumer<Throwable> errorConsumer,
@Nullable ClientCallContext context) throws A2AClientException;
Task getTask(TaskQueryParams request, @Nullable ClientCallContext context)
throws A2AClientException;
Task cancelTask(CancelTaskParams request, @Nullable ClientCallContext context)
throws A2AClientException;
ListTasksResult listTasks(ListTasksParams request, @Nullable ClientCallContext context)
throws A2AClientException;
AgentCard getExtendedAgentCard(GetExtendedAgentCardParams params,
@Nullable ClientCallContext context) throws A2AClientException;
void subscribeToTask(TaskIdParams request, Consumer<StreamingEventKind> eventConsumer,
Consumer<Throwable> errorConsumer,
@Nullable ClientCallContext context) throws A2AClientException;
void close();
}
Import
import org.a2aproject.sdk.client.transport.grpc.GrpcTransport;
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| channel |
io.grpc.Channel |
Yes |
gRPC channel to the agent server
|
| agentCard |
AgentCard |
Yes |
Resolved agent card with capabilities
|
| agentTenant |
String |
No |
Default tenant for multitenant agents
|
| interceptors |
List<ClientCallInterceptor> |
No |
Call interceptors for auth/cross-cutting
|
| request |
MessageSendParams |
Yes (sendMessage) |
Message parameters to send
|
| request |
TaskQueryParams |
Yes (getTask) |
Task ID to retrieve
|
| request |
TaskIdParams |
Yes (subscribeToTask) |
Task ID to subscribe to
|
Outputs
| Name |
Type |
Description
|
| sendMessage returns |
EventKind (Message or Task) |
Agent response
|
| Streaming events |
StreamingEventKind |
Delivered to eventConsumer asynchronously
|
| getTask/cancelTask returns |
Task |
Task state
|
| Exceptions |
A2AClientException |
Communication or protocol errors
|
Usage Examples
Basic gRPC Message Send
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import org.a2aproject.sdk.client.transport.grpc.GrpcTransport;
import org.a2aproject.sdk.spec.AgentCard;
// Create gRPC channel
ManagedChannel channel = ManagedChannelBuilder
.forAddress("localhost", 9999)
.usePlaintext()
.build();
// Create transport with agent card
GrpcTransport transport = new GrpcTransport(channel, agentCard);
// Send a message (blocking)
EventKind result = transport.sendMessage(
A2A.toMessageSendParams("Tell me a joke"), null);
if (result instanceof Message msg) {
System.out.println("Response: " + msg.parts());
}
Streaming with gRPC
// Stream events as they arrive
transport.sendMessageStreaming(
A2A.toMessageSendParams("Summarize this article"),
event -> System.out.println("Event: " + event),
error -> System.err.println("Error: " + error),
null // no custom context
);
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.