Skip to content

Commit 0e88bb7

Browse files
committed
feat(sse): add SSEParserConfig, raise default per-line limit to 1 MB
- Introduce SSEParserConfig record with builder to make SSE parser limits (maxLineLength, maxBufferLines, maxBufferChars) configurable - Raise default per-line limit from 64 KB to 1 MB so large JSON-RPC responses are no longer silently dropped - Thread SSEParserConfig through all HTTP client implementations (JDK, Android, OkHttp) via new constructors and provider methods - Prevent corrupt/skipped event blocks from advancing lastEventId, which could cause event loss on reconnect - Add AbstractA2AHttpClientSSETest for shared cross-client SSE tests - Document SSEParserConfig usage and security considerations Fixes a2aproject#1123 Signed-off-by: Emmanuel Hugonnet <ehugonne@redhat.com>
1 parent 4557dae commit 0e88bb7

18 files changed

Lines changed: 689 additions & 28 deletions

‎docs/content/dev/client.md‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,35 @@ Client client = Client
185185
.build();
186186
```
187187

188+
### SSE Parser Configuration
189+
190+
The client uses a Server-Sent Events (SSE) parser for streaming responses. You can tune its limits via `SSEParserConfig` to handle agents that return large payloads (e.g., large artifacts or tool results):
191+
192+
| Parameter | Description | Default |
193+
|------------------|----------------------------------------------------------|-----------|
194+
| `maxLineLength` | Max characters per raw SSE line (`0` = disabled) | 1 MB |
195+
| `maxBufferLines` | Max `data:` lines per event block | 1 000 |
196+
| `maxBufferChars` | Max total characters across all `data:` values per event | 1 MB |
197+
198+
The defaults are suitable for most deployments. To override them, build a custom `SSEParserConfig` and pass it to the HTTP client.
199+
200+
> **Security note:** setting `maxLineLength` to `0` disables the per-line length check, relying on `maxBufferChars` alone to bound memory. Only disable it when you trust the remote agent or have other safeguards (e.g. a reverse proxy with its own line-length limit).
201+
202+
```java
203+
SSEParserConfig sseConfig = SSEParserConfig.builder()
204+
.maxLineLength(4 * 1024 * 1024) // 4 MB per line
205+
.maxBufferChars(4 * 1024 * 1024) // 4 MB per event
206+
.build();
207+
208+
// Pass to JdkA2AHttpClient, then to your transport config
209+
JdkA2AHttpClient httpClient = new JdkA2AHttpClient(sseConfig);
210+
211+
Client client = Client
212+
.builder(agentCard)
213+
.withTransport(JSONRPCTransport.class, new JSONRPCTransportConfig(httpClient))
214+
.build();
215+
```
216+
188217
## Observability (Optional)
189218

190219
Add distributed tracing and W3C Trace Context propagation to client calls with the [OpenTelemetry extras modules](extra/opentelemetry#client).

‎extras/http-client-android/src/main/java/org/a2aproject/sdk/client/http/android/AndroidA2AHttpClient.java‎

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.a2aproject.sdk.client.http.android;
22

3+
import static org.a2aproject.sdk.util.Assert.checkNotNullParam;
4+
35
import static java.net.HttpURLConnection.HTTP_FORBIDDEN;
46
import static java.net.HttpURLConnection.HTTP_MULT_CHOICE;
57
import static java.net.HttpURLConnection.HTTP_OK;
@@ -28,6 +30,7 @@
2830
import org.a2aproject.sdk.client.http.A2AHttpHeaders;
2931
import org.a2aproject.sdk.client.http.A2AHttpResponse;
3032
import org.a2aproject.sdk.client.http.ServerSentEvent;
33+
import org.a2aproject.sdk.client.http.SSEParserConfig;
3134
import org.a2aproject.sdk.client.http.ServerSentEventParser;
3235
import org.a2aproject.sdk.common.A2AErrorMessages;
3336
import org.a2aproject.sdk.spec.A2AClientHTTPError;
@@ -48,24 +51,44 @@ public class AndroidA2AHttpClient implements A2AHttpClient {
4851
return t;
4952
});
5053

54+
private final SSEParserConfig sseParserConfig;
55+
56+
public AndroidA2AHttpClient() {
57+
this(SSEParserConfig.DEFAULT);
58+
}
59+
60+
/**
61+
* Creates a new Android HTTP client with custom SSE parser limits.
62+
*
63+
* @param sseParserConfig the SSE parser configuration to use for streaming responses
64+
*/
65+
public AndroidA2AHttpClient(SSEParserConfig sseParserConfig) {
66+
this.sseParserConfig = checkNotNullParam("sseParserConfig", sseParserConfig);
67+
}
68+
5169
@Override
5270
public GetBuilder createGet() {
53-
return new AndroidGetBuilder();
71+
return new AndroidGetBuilder(sseParserConfig);
5472
}
5573

5674
@Override
5775
public PostBuilder createPost() {
58-
return new AndroidPostBuilder();
76+
return new AndroidPostBuilder(sseParserConfig);
5977
}
6078

6179
@Override
6280
public DeleteBuilder createDelete() {
63-
return new AndroidDeleteBuilder();
81+
return new AndroidDeleteBuilder(sseParserConfig);
6482
}
6583

6684
private abstract static class AndroidBuilder<T extends Builder<T>> implements Builder<T> {
6785
protected String url = "";
6886
protected Map<String, String> headers = new HashMap<>();
87+
protected final SSEParserConfig sseParserConfig;
88+
89+
AndroidBuilder(SSEParserConfig sseParserConfig) {
90+
this.sseParserConfig = sseParserConfig;
91+
}
6992

7093
@Override
7194
public T url(String url) {
@@ -203,7 +226,7 @@ protected void processSSEResponse(
203226
BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
204227
String line;
205228
if (isSse) {
206-
ServerSentEventParser sseParser = new ServerSentEventParser(messageConsumer, errorConsumer);
229+
ServerSentEventParser sseParser = new ServerSentEventParser(messageConsumer, errorConsumer, sseParserConfig);
207230
while ((line = reader.readLine()) != null) {
208231
sseParser.processLine(line);
209232
}
@@ -244,6 +267,10 @@ protected CompletableFuture<Void> executeAsyncSSE(
244267
}
245268

246269
private static class AndroidGetBuilder extends AndroidBuilder<GetBuilder> implements GetBuilder {
270+
AndroidGetBuilder(SSEParserConfig sseParserConfig) {
271+
super(sseParserConfig);
272+
}
273+
247274
@Override
248275
public A2AHttpResponse get() throws IOException {
249276
HttpURLConnection connection = createConnection("GET", false);
@@ -271,6 +298,10 @@ private static class AndroidPostBuilder extends AndroidBuilder<PostBuilder>
271298
private String body = "";
272299
private boolean followRedirects = false;
273300

301+
AndroidPostBuilder(SSEParserConfig sseParserConfig) {
302+
super(sseParserConfig);
303+
}
304+
274305
@Override
275306
public PostBuilder body(String body) {
276307
this.body = body;
@@ -325,6 +356,10 @@ public CompletableFuture<Void> postAsyncSSE(
325356

326357
private static class AndroidDeleteBuilder extends AndroidBuilder<DeleteBuilder>
327358
implements DeleteBuilder {
359+
AndroidDeleteBuilder(SSEParserConfig sseParserConfig) {
360+
super(sseParserConfig);
361+
}
362+
328363
@Override
329364
public A2AHttpResponse delete() throws IOException {
330365
HttpURLConnection connection = createConnection("DELETE", false);

‎extras/http-client-android/src/main/java/org/a2aproject/sdk/client/http/android/AndroidA2AHttpClientProvider.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.a2aproject.sdk.client.http.A2AHttpClient;
44
import org.a2aproject.sdk.client.http.A2AHttpClientProvider;
5+
import org.a2aproject.sdk.client.http.SSEParserConfig;
56

67
/**
78
* Service provider for {@link AndroidA2AHttpClient}.
@@ -24,6 +25,20 @@ public A2AHttpClient create() {
2425
return new AndroidA2AHttpClient();
2526
}
2627

28+
/**
29+
* {@inheritDoc}
30+
*
31+
* @throws IllegalStateException if the Android runtime is not available
32+
*/
33+
@Override
34+
public A2AHttpClient create(SSEParserConfig sseParserConfig) {
35+
if (!ANDROID_AVAILABLE) {
36+
throw new IllegalStateException(
37+
"Android classes are not available. This provider is only supported on Android.");
38+
}
39+
return new AndroidA2AHttpClient(sseParserConfig);
40+
}
41+
2742
@Override
2843
public int priority() {
2944
return ANDROID_AVAILABLE ? 110 : -1; // Higher priority than Vert.x on Android

‎extras/http-client-android/src/test/java/org/a2aproject/sdk/client/http/android/AndroidA2AHttpClientSSETest.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@
22

33
import org.a2aproject.sdk.client.http.A2AHttpClient;
44
import org.a2aproject.sdk.client.http.AbstractA2AHttpClientSSETest;
5+
import org.a2aproject.sdk.client.http.SSEParserConfig;
56

67
public class AndroidA2AHttpClientSSETest extends AbstractA2AHttpClientSSETest {
78

89
@Override
910
protected A2AHttpClient createClient() {
1011
return new AndroidA2AHttpClient();
1112
}
13+
14+
@Override
15+
protected A2AHttpClient createClient(SSEParserConfig sseParserConfig) {
16+
return new AndroidA2AHttpClient(sseParserConfig);
17+
}
1218
}

‎extras/http-client-android/src/test/java/org/a2aproject/sdk/client/http/android/OkHttpA2AHttpClient.java‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.a2aproject.sdk.client.http.A2AHttpHeaders;
2323
import org.a2aproject.sdk.client.http.A2AHttpResponse;
2424
import org.a2aproject.sdk.client.http.ServerSentEvent;
25+
import org.a2aproject.sdk.client.http.SSEParserConfig;
2526
import org.a2aproject.sdk.client.http.ServerSentEventParser;
2627
import org.a2aproject.sdk.common.A2AErrorMessages;
2728
import org.a2aproject.sdk.spec.A2AClientHTTPError;
@@ -48,24 +49,39 @@ class OkHttpA2AHttpClient implements A2AHttpClient {
4849
return t;
4950
});
5051

52+
private final SSEParserConfig sseParserConfig;
53+
54+
OkHttpA2AHttpClient() {
55+
this(SSEParserConfig.DEFAULT);
56+
}
57+
58+
OkHttpA2AHttpClient(SSEParserConfig sseParserConfig) {
59+
this.sseParserConfig = sseParserConfig;
60+
}
61+
5162
@Override
5263
public GetBuilder createGet() {
53-
return new OkHttpGetBuilder();
64+
return new OkHttpGetBuilder(sseParserConfig);
5465
}
5566

5667
@Override
5768
public PostBuilder createPost() {
58-
return new OkHttpPostBuilder();
69+
return new OkHttpPostBuilder(sseParserConfig);
5970
}
6071

6172
@Override
6273
public DeleteBuilder createDelete() {
63-
return new OkHttpDeleteBuilder();
74+
return new OkHttpDeleteBuilder(sseParserConfig);
6475
}
6576

6677
private abstract static class OkHttpBuilder<T extends Builder<T>> implements Builder<T> {
6778
protected String url = "";
6879
protected final Map<String, String> headers = new HashMap<>();
80+
protected final SSEParserConfig sseParserConfig;
81+
82+
OkHttpBuilder(SSEParserConfig sseParserConfig) {
83+
this.sseParserConfig = sseParserConfig;
84+
}
6985

7086
@Override
7187
public T url(String url) {
@@ -189,7 +205,7 @@ private void parseResponseBody(
189205
new InputStreamReader(body.byteStream(), StandardCharsets.UTF_8))) {
190206
String line;
191207
if (isSse) {
192-
ServerSentEventParser sseParser = new ServerSentEventParser(messageConsumer, errorConsumer);
208+
ServerSentEventParser sseParser = new ServerSentEventParser(messageConsumer, errorConsumer, sseParserConfig);
193209
while ((line = reader.readLine()) != null) {
194210
sseParser.processLine(line);
195211
}
@@ -214,6 +230,10 @@ private void parseResponseBody(
214230
}
215231

216232
private static class OkHttpGetBuilder extends OkHttpBuilder<GetBuilder> implements GetBuilder {
233+
OkHttpGetBuilder(SSEParserConfig sseParserConfig) {
234+
super(sseParserConfig);
235+
}
236+
217237
@Override
218238
public A2AHttpResponse get() throws IOException {
219239
OkHttpClient client = buildClient(false);
@@ -248,6 +268,10 @@ private static class OkHttpPostBuilder extends OkHttpBuilder<PostBuilder> implem
248268
private String body = "";
249269
private boolean followRedirects = false;
250270

271+
OkHttpPostBuilder(SSEParserConfig sseParserConfig) {
272+
super(sseParserConfig);
273+
}
274+
251275
@Override
252276
public PostBuilder body(String body) {
253277
this.body = body;
@@ -295,6 +319,10 @@ public CompletableFuture<Void> postAsyncSSE(
295319
}
296320

297321
private static class OkHttpDeleteBuilder extends OkHttpBuilder<DeleteBuilder> implements DeleteBuilder {
322+
OkHttpDeleteBuilder(SSEParserConfig sseParserConfig) {
323+
super(sseParserConfig);
324+
}
325+
298326
@Override
299327
public A2AHttpResponse delete() throws IOException {
300328
OkHttpClient client = buildClient(false);

‎extras/http-client-android/src/test/java/org/a2aproject/sdk/client/http/android/OkHttpA2AHttpClientSSETest.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@
22

33
import org.a2aproject.sdk.client.http.A2AHttpClient;
44
import org.a2aproject.sdk.client.http.AbstractA2AHttpClientSSETest;
5+
import org.a2aproject.sdk.client.http.SSEParserConfig;
56

67
public class OkHttpA2AHttpClientSSETest extends AbstractA2AHttpClientSSETest {
78

89
@Override
910
protected A2AHttpClient createClient() {
1011
return new OkHttpA2AHttpClient();
1112
}
13+
14+
@Override
15+
protected A2AHttpClient createClient(SSEParserConfig sseParserConfig) {
16+
return new OkHttpA2AHttpClient(sseParserConfig);
17+
}
1218
}

‎http-client/src/main/java/org/a2aproject/sdk/client/http/A2AHttpClientFactory.java‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,35 @@ public static A2AHttpClient create() {
8585
.orElseThrow(() -> new IllegalStateException("No A2AHttpClientProvider could be instantiated"));
8686
}
8787

88+
/**
89+
* Creates a new A2AHttpClient instance with the given {@link SSEParserConfig} using the
90+
* highest available priority provider.
91+
*
92+
* <p>
93+
* Providers that support SSE parser configuration will use it; others fall back to their
94+
* default configuration. Providers are tried in descending priority order.
95+
*
96+
* @param sseParserConfig the SSE parser configuration to apply
97+
* @return a new A2AHttpClient instance
98+
* @throws IllegalStateException if no provider found or all providers failed to instantiate
99+
*/
100+
public static A2AHttpClient create(SSEParserConfig sseParserConfig) {
101+
if (sseParserConfig == null) {
102+
return create();
103+
}
104+
return PROVIDERS.stream()
105+
.flatMap(p -> {
106+
try {
107+
return Stream.of(p.create(sseParserConfig));
108+
} catch (Exception e) {
109+
LOGGER.log(Level.WARNING, e, () -> "Provider " + p.name() + " skipped");
110+
return Stream.empty();
111+
}
112+
})
113+
.findFirst()
114+
.orElseThrow(() -> new IllegalStateException("No A2AHttpClientProvider could be instantiated"));
115+
}
116+
88117
/**
89118
* Creates a new A2AHttpClient instance using a specific provider by name.
90119
*

‎http-client/src/main/java/org/a2aproject/sdk/client/http/A2AHttpClientProvider.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ public interface A2AHttpClientProvider {
2323
*/
2424
A2AHttpClient create();
2525

26+
/**
27+
* Creates a new instance of an A2AHttpClient with the given {@link SSEParserConfig}.
28+
*
29+
* <p>Providers that support SSE parser configuration should override this method.
30+
* The default implementation ignores {@code sseParserConfig} and delegates to {@link #create()}.
31+
*
32+
* @param sseParserConfig the SSE parser configuration to apply
33+
* @return a new A2AHttpClient instance
34+
*/
35+
default A2AHttpClient create(SSEParserConfig sseParserConfig) {
36+
return create();
37+
}
38+
2639
/**
2740
* Returns the priority of this provider. Higher priority providers are
2841
* tried first; the first one whose {@link #create()} succeeds is used.

0 commit comments

Comments
 (0)