fix: http streamable client connect timeout (#511)

* add http streamable client connect timeout
* add http sse client connect timeout

Co-authored-by: gongwn1 <gongwn1@lenovo.com>
This commit is contained in:
Zizo 2025-08-25 22:14:28 +08:00 • committed by GitHub
commit cc67d8fa76
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 36 additions and 12 deletions

View file

@ -170,8 +170,7 @@ public class HttpClientSseClientTransport implements McpClientTransport {
@Deprecated(forRemoval = true)
public HttpClientSseClientTransport(HttpClient.Builder clientBuilder, HttpRequest.Builder requestBuilder,
String baseUri, String sseEndpoint, ObjectMapper objectMapper) {
this(clientBuilder.connectTimeout(Duration.ofSeconds(10)).build(), requestBuilder, baseUri, sseEndpoint,
objectMapper);
this(clientBuilder.build(), requestBuilder, baseUri, sseEndpoint, objectMapper);
}
/**
@ -241,9 +240,7 @@ public class HttpClientSseClientTransport implements McpClientTransport {
private String sseEndpoint = DEFAULT_SSE_ENDPOINT;
private HttpClient.Builder clientBuilder = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.connectTimeout(Duration.ofSeconds(10));
private HttpClient.Builder clientBuilder = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1);
private ObjectMapper objectMapper = new ObjectMapper();
@ -252,6 +249,8 @@ public class HttpClientSseClientTransport implements McpClientTransport {
private AsyncHttpRequestCustomizer httpRequestCustomizer = AsyncHttpRequestCustomizer.NOOP;
private Duration connectTimeout = Duration.ofSeconds(10);
/**
* Creates a new builder instance.
*/
@ -383,13 +382,25 @@ public class HttpClientSseClientTransport implements McpClientTransport {
return this;
}
/**
* Sets the connection timeout for the HTTP client.
* @param connectTimeout the connection timeout duration
* @return this builder
*/
public Builder connectTimeout(Duration connectTimeout) {
Assert.notNull(connectTimeout, "connectTimeout must not be null");
this.connectTimeout = connectTimeout;
return this;
}
/**
* Builds a new {@link HttpClientSseClientTransport} instance.
* @return a new transport instance
*/
public HttpClientSseClientTransport build() {
return new HttpClientSseClientTransport(clientBuilder.build(), requestBuilder, baseUri, sseEndpoint,
objectMapper, httpRequestCustomizer);
HttpClient httpClient = this.clientBuilder.connectTimeout(this.connectTimeout).build();
return new HttpClientSseClientTransport(httpClient, requestBuilder, baseUri, sseEndpoint, objectMapper,
httpRequestCustomizer);
}
}

View file

@ -588,9 +588,7 @@ public class HttpClientStreamableHttpTransport implements McpClientTransport {
private ObjectMapper objectMapper;
private HttpClient.Builder clientBuilder = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.connectTimeout(Duration.ofSeconds(10));
private HttpClient.Builder clientBuilder = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1);
private String endpoint = DEFAULT_ENDPOINT;
@ -602,6 +600,8 @@ public class HttpClientStreamableHttpTransport implements McpClientTransport {
private AsyncHttpRequestCustomizer httpRequestCustomizer = AsyncHttpRequestCustomizer.NOOP;
private Duration connectTimeout = Duration.ofSeconds(10);
/**
* Creates a new builder with the specified base URI.
* @param baseUri the base URI of the MCP server
@ -738,6 +738,17 @@ public class HttpClientStreamableHttpTransport implements McpClientTransport {
return this;
}
/**
* Sets the connection timeout for the HTTP client.
* @param connectTimeout the connection timeout duration
* @return this builder
*/
public Builder connectTimeout(Duration connectTimeout) {
Assert.notNull(connectTimeout, "connectTimeout must not be null");
this.connectTimeout = connectTimeout;
return this;
}
/**
* Construct a fresh instance of {@link HttpClientStreamableHttpTransport} using
* the current builder configuration.
@ -746,8 +757,10 @@ public class HttpClientStreamableHttpTransport implements McpClientTransport {
public HttpClientStreamableHttpTransport build() {
ObjectMapper objectMapper = this.objectMapper != null ? this.objectMapper : new ObjectMapper();
return new HttpClientStreamableHttpTransport(objectMapper, clientBuilder.build(), requestBuilder, baseUri,
endpoint, resumableStreams, openConnectionOnStartup, httpRequestCustomizer);
HttpClient httpClient = this.clientBuilder.connectTimeout(this.connectTimeout).build();
return new HttpClientStreamableHttpTransport(objectMapper, httpClient, requestBuilder, baseUri, endpoint,
resumableStreams, openConnectionOnStartup, httpRequestCustomizer);
}
}