Skip to content

Commit cc67d8f

Browse files
Zizo-Vigongwn1
andauthored
fix: http streamable client connect timeout (#511)
* add http streamable client connect timeout * add http sse client connect timeout Co-authored-by: gongwn1 <[email protected]>
1 parent 94c0350 commit cc67d8f

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

mcp/src/main/java/io/modelcontextprotocol/client/transport/HttpClientSseClientTransport.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,7 @@ public HttpClientSseClientTransport(HttpClient.Builder clientBuilder, String bas
170170
@Deprecated(forRemoval = true)
171171
public HttpClientSseClientTransport(HttpClient.Builder clientBuilder, HttpRequest.Builder requestBuilder,
172172
String baseUri, String sseEndpoint, ObjectMapper objectMapper) {
173-
this(clientBuilder.connectTimeout(Duration.ofSeconds(10)).build(), requestBuilder, baseUri, sseEndpoint,
174-
objectMapper);
173+
this(clientBuilder.build(), requestBuilder, baseUri, sseEndpoint, objectMapper);
175174
}
176175

177176
/**
@@ -241,9 +240,7 @@ public static class Builder {
241240

242241
private String sseEndpoint = DEFAULT_SSE_ENDPOINT;
243242

244-
private HttpClient.Builder clientBuilder = HttpClient.newBuilder()
245-
.version(HttpClient.Version.HTTP_1_1)
246-
.connectTimeout(Duration.ofSeconds(10));
243+
private HttpClient.Builder clientBuilder = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1);
247244

248245
private ObjectMapper objectMapper = new ObjectMapper();
249246

@@ -252,6 +249,8 @@ public static class Builder {
252249

253250
private AsyncHttpRequestCustomizer httpRequestCustomizer = AsyncHttpRequestCustomizer.NOOP;
254251

252+
private Duration connectTimeout = Duration.ofSeconds(10);
253+
255254
/**
256255
* Creates a new builder instance.
257256
*/
@@ -383,13 +382,25 @@ public Builder asyncHttpRequestCustomizer(AsyncHttpRequestCustomizer asyncHttpRe
383382
return this;
384383
}
385384

385+
/**
386+
* Sets the connection timeout for the HTTP client.
387+
* @param connectTimeout the connection timeout duration
388+
* @return this builder
389+
*/
390+
public Builder connectTimeout(Duration connectTimeout) {
391+
Assert.notNull(connectTimeout, "connectTimeout must not be null");
392+
this.connectTimeout = connectTimeout;
393+
return this;
394+
}
395+
386396
/**
387397
* Builds a new {@link HttpClientSseClientTransport} instance.
388398
* @return a new transport instance
389399
*/
390400
public HttpClientSseClientTransport build() {
391-
return new HttpClientSseClientTransport(clientBuilder.build(), requestBuilder, baseUri, sseEndpoint,
392-
objectMapper, httpRequestCustomizer);
401+
HttpClient httpClient = this.clientBuilder.connectTimeout(this.connectTimeout).build();
402+
return new HttpClientSseClientTransport(httpClient, requestBuilder, baseUri, sseEndpoint, objectMapper,
403+
httpRequestCustomizer);
393404
}
394405

395406
}

mcp/src/main/java/io/modelcontextprotocol/client/transport/HttpClientStreamableHttpTransport.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -588,9 +588,7 @@ public static class Builder {
588588

589589
private ObjectMapper objectMapper;
590590

591-
private HttpClient.Builder clientBuilder = HttpClient.newBuilder()
592-
.version(HttpClient.Version.HTTP_1_1)
593-
.connectTimeout(Duration.ofSeconds(10));
591+
private HttpClient.Builder clientBuilder = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1);
594592

595593
private String endpoint = DEFAULT_ENDPOINT;
596594

@@ -602,6 +600,8 @@ public static class Builder {
602600

603601
private AsyncHttpRequestCustomizer httpRequestCustomizer = AsyncHttpRequestCustomizer.NOOP;
604602

603+
private Duration connectTimeout = Duration.ofSeconds(10);
604+
605605
/**
606606
* Creates a new builder with the specified base URI.
607607
* @param baseUri the base URI of the MCP server
@@ -738,6 +738,17 @@ public Builder asyncHttpRequestCustomizer(AsyncHttpRequestCustomizer asyncHttpRe
738738
return this;
739739
}
740740

741+
/**
742+
* Sets the connection timeout for the HTTP client.
743+
* @param connectTimeout the connection timeout duration
744+
* @return this builder
745+
*/
746+
public Builder connectTimeout(Duration connectTimeout) {
747+
Assert.notNull(connectTimeout, "connectTimeout must not be null");
748+
this.connectTimeout = connectTimeout;
749+
return this;
750+
}
751+
741752
/**
742753
* Construct a fresh instance of {@link HttpClientStreamableHttpTransport} using
743754
* the current builder configuration.
@@ -746,8 +757,10 @@ public Builder asyncHttpRequestCustomizer(AsyncHttpRequestCustomizer asyncHttpRe
746757
public HttpClientStreamableHttpTransport build() {
747758
ObjectMapper objectMapper = this.objectMapper != null ? this.objectMapper : new ObjectMapper();
748759

749-
return new HttpClientStreamableHttpTransport(objectMapper, clientBuilder.build(), requestBuilder, baseUri,
750-
endpoint, resumableStreams, openConnectionOnStartup, httpRequestCustomizer);
760+
HttpClient httpClient = this.clientBuilder.connectTimeout(this.connectTimeout).build();
761+
762+
return new HttpClientStreamableHttpTransport(objectMapper, httpClient, requestBuilder, baseUri, endpoint,
763+
resumableStreams, openConnectionOnStartup, httpRequestCustomizer);
751764
}
752765

753766
}

0 commit comments

Comments
 (0)