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:
parent
94c03505c1
commit
cc67d8fa76
2 changed files with 36 additions and 12 deletions
|
|
@ -170,8 +170,7 @@ public class HttpClientSseClientTransport implements McpClientTransport {
|
||||||
@Deprecated(forRemoval = true)
|
@Deprecated(forRemoval = true)
|
||||||
public HttpClientSseClientTransport(HttpClient.Builder clientBuilder, HttpRequest.Builder requestBuilder,
|
public HttpClientSseClientTransport(HttpClient.Builder clientBuilder, HttpRequest.Builder requestBuilder,
|
||||||
String baseUri, String sseEndpoint, ObjectMapper objectMapper) {
|
String baseUri, String sseEndpoint, ObjectMapper objectMapper) {
|
||||||
this(clientBuilder.connectTimeout(Duration.ofSeconds(10)).build(), requestBuilder, baseUri, sseEndpoint,
|
this(clientBuilder.build(), requestBuilder, baseUri, sseEndpoint, objectMapper);
|
||||||
objectMapper);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -241,9 +240,7 @@ public class HttpClientSseClientTransport implements McpClientTransport {
|
||||||
|
|
||||||
private String sseEndpoint = DEFAULT_SSE_ENDPOINT;
|
private String sseEndpoint = DEFAULT_SSE_ENDPOINT;
|
||||||
|
|
||||||
private HttpClient.Builder clientBuilder = HttpClient.newBuilder()
|
private HttpClient.Builder clientBuilder = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1);
|
||||||
.version(HttpClient.Version.HTTP_1_1)
|
|
||||||
.connectTimeout(Duration.ofSeconds(10));
|
|
||||||
|
|
||||||
private ObjectMapper objectMapper = new ObjectMapper();
|
private ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
|
||||||
|
|
@ -252,6 +249,8 @@ public class HttpClientSseClientTransport implements McpClientTransport {
|
||||||
|
|
||||||
private AsyncHttpRequestCustomizer httpRequestCustomizer = AsyncHttpRequestCustomizer.NOOP;
|
private AsyncHttpRequestCustomizer httpRequestCustomizer = AsyncHttpRequestCustomizer.NOOP;
|
||||||
|
|
||||||
|
private Duration connectTimeout = Duration.ofSeconds(10);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new builder instance.
|
* Creates a new builder instance.
|
||||||
*/
|
*/
|
||||||
|
|
@ -383,13 +382,25 @@ public class HttpClientSseClientTransport implements McpClientTransport {
|
||||||
return this;
|
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.
|
* Builds a new {@link HttpClientSseClientTransport} instance.
|
||||||
* @return a new transport instance
|
* @return a new transport instance
|
||||||
*/
|
*/
|
||||||
public HttpClientSseClientTransport build() {
|
public HttpClientSseClientTransport build() {
|
||||||
return new HttpClientSseClientTransport(clientBuilder.build(), requestBuilder, baseUri, sseEndpoint,
|
HttpClient httpClient = this.clientBuilder.connectTimeout(this.connectTimeout).build();
|
||||||
objectMapper, httpRequestCustomizer);
|
return new HttpClientSseClientTransport(httpClient, requestBuilder, baseUri, sseEndpoint, objectMapper,
|
||||||
|
httpRequestCustomizer);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -588,9 +588,7 @@ public class HttpClientStreamableHttpTransport implements McpClientTransport {
|
||||||
|
|
||||||
private ObjectMapper objectMapper;
|
private ObjectMapper objectMapper;
|
||||||
|
|
||||||
private HttpClient.Builder clientBuilder = HttpClient.newBuilder()
|
private HttpClient.Builder clientBuilder = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1);
|
||||||
.version(HttpClient.Version.HTTP_1_1)
|
|
||||||
.connectTimeout(Duration.ofSeconds(10));
|
|
||||||
|
|
||||||
private String endpoint = DEFAULT_ENDPOINT;
|
private String endpoint = DEFAULT_ENDPOINT;
|
||||||
|
|
||||||
|
|
@ -602,6 +600,8 @@ public class HttpClientStreamableHttpTransport implements McpClientTransport {
|
||||||
|
|
||||||
private AsyncHttpRequestCustomizer httpRequestCustomizer = AsyncHttpRequestCustomizer.NOOP;
|
private AsyncHttpRequestCustomizer httpRequestCustomizer = AsyncHttpRequestCustomizer.NOOP;
|
||||||
|
|
||||||
|
private Duration connectTimeout = Duration.ofSeconds(10);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new builder with the specified base URI.
|
* Creates a new builder with the specified base URI.
|
||||||
* @param baseUri the base URI of the MCP server
|
* @param baseUri the base URI of the MCP server
|
||||||
|
|
@ -738,6 +738,17 @@ public class HttpClientStreamableHttpTransport implements McpClientTransport {
|
||||||
return this;
|
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
|
* Construct a fresh instance of {@link HttpClientStreamableHttpTransport} using
|
||||||
* the current builder configuration.
|
* the current builder configuration.
|
||||||
|
|
@ -746,8 +757,10 @@ public class HttpClientStreamableHttpTransport implements McpClientTransport {
|
||||||
public HttpClientStreamableHttpTransport build() {
|
public HttpClientStreamableHttpTransport build() {
|
||||||
ObjectMapper objectMapper = this.objectMapper != null ? this.objectMapper : new ObjectMapper();
|
ObjectMapper objectMapper = this.objectMapper != null ? this.objectMapper : new ObjectMapper();
|
||||||
|
|
||||||
return new HttpClientStreamableHttpTransport(objectMapper, clientBuilder.build(), requestBuilder, baseUri,
|
HttpClient httpClient = this.clientBuilder.connectTimeout(this.connectTimeout).build();
|
||||||
endpoint, resumableStreams, openConnectionOnStartup, httpRequestCustomizer);
|
|
||||||
|
return new HttpClientStreamableHttpTransport(objectMapper, httpClient, requestBuilder, baseUri, endpoint,
|
||||||
|
resumableStreams, openConnectionOnStartup, httpRequestCustomizer);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue