Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions client/base/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>a2a-java-sdk-client-transport-jsonrpc</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
import org.a2aproject.sdk.client.transport.spi.ClientTransportConfigBuilder;
import org.a2aproject.sdk.client.transport.spi.ClientTransportProvider;
import org.a2aproject.sdk.client.transport.spi.ClientTransportWrapper;
import org.a2aproject.sdk.client.transport.spi.interceptors.ClientRoutingInterceptor;
import org.a2aproject.sdk.spec.A2AClientException;
import org.a2aproject.sdk.spec.AgentCard;
import org.a2aproject.sdk.spec.AgentInterface;
import org.a2aproject.sdk.spec.TransportProtocol;
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -325,6 +325,11 @@ private ClientTransport buildClientTransport() throws A2AClientException {
throw new A2AClientException("Missing required TransportConfig for " + agentInterface.protocolBinding());
}

if (!clientTransportProvider.supportsTenantRouting()
&& clientTransportConfig.getInterceptors().stream().anyMatch(ClientRoutingInterceptor.class::isInstance)) {
throw new A2AClientException("The selected transport does not support tenant routing");
}

return wrap(clientTransportProvider.create(clientTransportConfig, agentCard, agentInterface), clientTransportConfig);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@
import org.a2aproject.sdk.client.transport.jsonrpc.JSONRPCTransport;
import org.a2aproject.sdk.client.transport.jsonrpc.JSONRPCTransportConfig;
import org.a2aproject.sdk.client.transport.jsonrpc.JSONRPCTransportConfigBuilder;
import org.a2aproject.sdk.client.transport.spi.ClientTransportConfig;
import org.a2aproject.sdk.client.transport.spi.ClientTransportProvider;
import org.a2aproject.sdk.client.transport.spi.interceptors.ClientCallContext;
import org.a2aproject.sdk.client.transport.spi.interceptors.ClientRoutingInterceptor;
import org.a2aproject.sdk.spec.A2AClientException;
import org.a2aproject.sdk.spec.AgentCapabilities;
import org.a2aproject.sdk.spec.AgentCard;
import org.a2aproject.sdk.spec.AgentInterface;
import org.a2aproject.sdk.spec.AgentSkill;
import org.a2aproject.sdk.spec.TransportProtocol;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -78,6 +83,77 @@ public void shouldNotFindConfigurationTransport() throws A2AClientException {
Assertions.assertTrue(exception.getMessage() != null && exception.getMessage().startsWith("Missing required TransportConfig for"));
}

@Test
void rejectsRoutingInterceptorBeforeCreatingUnsupportedTransport() throws Exception {
UnsupportedRoutingProvider.created = false;
AgentCard unsupportedCard = buildCard(List.of(new AgentInterface("test-unsupported-routing", "http://localhost:9999")));
UnsupportedRoutingConfig config = new UnsupportedRoutingConfig();
config.setInterceptors(List.of(new ClientRoutingInterceptor() {
@Override
public @Nullable String routeTenant(@Nullable String destination, String method, @Nullable String tenant,
@Nullable AgentCard agent, @Nullable ClientCallContext context) {
throw new AssertionError("Routing must not run on an unsupported transport");
}
}));

A2AClientException exception = Assertions.assertThrows(A2AClientException.class,
() -> Client.builder(unsupportedCard).withTransport(UnsupportedRoutingTransport.class, config).build());
Assertions.assertEquals("The selected transport does not support tenant routing", exception.getMessage());
Assertions.assertFalse(UnsupportedRoutingProvider.created);

config.setInterceptors(List.of());
try (Client client = Client.builder(unsupportedCard).withTransport(UnsupportedRoutingTransport.class, config).build()) {
Assertions.assertNotNull(client);
Assertions.assertTrue(UnsupportedRoutingProvider.created);
}
}

@Test
void acceptsRoutingInterceptorOnSupportedTransport() throws Exception {
ClientRoutingInterceptor router = new ClientRoutingInterceptor() {
@Override
public @Nullable String routeTenant(@Nullable String destination, String method, @Nullable String tenant,
@Nullable AgentCard agent, @Nullable ClientCallContext context) {
return tenant;
}
};
try (Client client = Client.builder(card)
.withTransport(JSONRPCTransport.class, new JSONRPCTransportConfigBuilder().addInterceptor(router))
.build()) {
Assertions.assertNotNull(client);
}
}

public static final class UnsupportedRoutingTransport extends JSONRPCTransport {
public UnsupportedRoutingTransport() {
super("http://localhost:9999");
}
}

public static final class UnsupportedRoutingConfig extends ClientTransportConfig<UnsupportedRoutingTransport> {
}

public static final class UnsupportedRoutingProvider
implements ClientTransportProvider<UnsupportedRoutingTransport, UnsupportedRoutingConfig> {
private static boolean created;

@Override
public UnsupportedRoutingTransport create(UnsupportedRoutingConfig config, AgentCard agent, AgentInterface endpoint) {
created = true;
return new UnsupportedRoutingTransport();
}

@Override
public String getTransportProtocol() {
return "test-unsupported-routing";
}

@Override
public Class<UnsupportedRoutingTransport> getTransportProtocolClass() {
return UnsupportedRoutingTransport.class;
}
}

@Test
public void shouldCreateJSONRPCClient() throws A2AClientException {
Client client = Client
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.a2aproject.sdk.client.ClientBuilderTest$UnsupportedRoutingProvider
5 changes: 5 additions & 0 deletions client/transport/grpc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@
<artifactId>mockserver-netty</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Loading
Loading