Skip to content

Commit 388830c

Browse files
committed
Merge branch '3.5.x'
2 parents 1fecce3 + c46c726 commit 388830c

File tree

3 files changed

+39
-24
lines changed

3 files changed

+39
-24
lines changed

framework/fel/java/plugins/tool-mcp-client/src/main/java/modelengine/fel/tool/mcp/client/support/DefaultMcpClient.java

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
package modelengine.fel.tool.mcp.client.support;
88

9+
import static modelengine.fitframework.inspection.Validation.notBlank;
10+
import static modelengine.fitframework.inspection.Validation.notNull;
911
import static modelengine.fitframework.util.ObjectUtils.cast;
1012

1113
import modelengine.fel.tool.mcp.client.McpClient;
@@ -57,14 +59,14 @@
5759
*/
5860
public class DefaultMcpClient implements McpClient {
5961
private static final Logger log = Logger.get(DefaultMcpClient.class);
60-
private static final long DELAY_MILLIS = 30_000L;
6162

6263
private final ObjectSerializer jsonSerializer;
6364
private final HttpClassicClient client;
6465
private final String baseUri;
6566
private final String sseEndpoint;
6667
private final String name;
6768
private final AtomicLong id = new AtomicLong(0);
69+
private final long pingInterval;
6870

6971
private volatile String messageEndpoint;
7072
private volatile String sessionId;
@@ -88,14 +90,16 @@ public class DefaultMcpClient implements McpClient {
8890
* @param client The HTTP client used for communication with the MCP server.
8991
* @param baseUri The base URI of the MCP server.
9092
* @param sseEndpoint The endpoint for the Server-Sent Events (SSE) connection.
93+
* @param pingInterval The interval for sending ping messages to the MCP server. Unit: milliseconds.
9194
*/
9295
public DefaultMcpClient(ObjectSerializer jsonSerializer, HttpClassicClient client, String baseUri,
93-
String sseEndpoint) {
94-
this.jsonSerializer = jsonSerializer;
95-
this.client = client;
96-
this.baseUri = baseUri;
97-
this.sseEndpoint = sseEndpoint;
96+
String sseEndpoint, long pingInterval) {
97+
this.jsonSerializer = notNull(jsonSerializer, "The json serializer cannot be null.");
98+
this.client = notNull(client, "The http client cannot be null.");
99+
this.baseUri = notBlank(baseUri, "The MCP server base URI cannot be blank.");
100+
this.sseEndpoint = notBlank(sseEndpoint, "The MCP server SSE endpoint cannot be blank.");
98101
this.name = UuidUtils.randomUuidString();
102+
this.pingInterval = pingInterval > 0 ? pingInterval : 15_000;
99103
}
100104

101105
@Override
@@ -126,20 +130,6 @@ public void initialize() {
126130
(subscription, textEvent) -> this.consumeTextEvent(textEvent),
127131
subscription -> log.info("SSE channel is completed."),
128132
(subscription, cause) -> log.error("SSE channel is failed.", cause));
129-
this.pingScheduler = ThreadPoolScheduler.custom()
130-
.threadPoolName("mcp-client-ping-" + this.name)
131-
.awaitTermination(3, TimeUnit.SECONDS)
132-
.isImmediateShutdown(true)
133-
.corePoolSize(1)
134-
.maximumPoolSize(1)
135-
.keepAliveTime(60, TimeUnit.SECONDS)
136-
.workQueueCapacity(Integer.MAX_VALUE)
137-
.isDaemonThread(true)
138-
.build();
139-
this.pingScheduler.schedule(Task.builder()
140-
.runnable(this::pingServer)
141-
.policy(ExecutePolicy.fixedDelay(DELAY_MILLIS))
142-
.build(), DELAY_MILLIS);
143133
if (!this.waitInitialized()) {
144134
throw new IllegalStateException("Failed to initialize.");
145135
}
@@ -236,6 +226,20 @@ private void initializedMcpServer(JsonRpc.Response<Long> response) {
236226
} catch (IOException e) {
237227
throw new IllegalStateException(e);
238228
}
229+
this.pingScheduler = ThreadPoolScheduler.custom()
230+
.threadPoolName("mcp-client-ping-" + this.name)
231+
.awaitTermination(3, TimeUnit.SECONDS)
232+
.isImmediateShutdown(true)
233+
.corePoolSize(1)
234+
.maximumPoolSize(1)
235+
.keepAliveTime(60, TimeUnit.SECONDS)
236+
.workQueueCapacity(Integer.MAX_VALUE)
237+
.isDaemonThread(true)
238+
.build();
239+
this.pingScheduler.schedule(Task.builder()
240+
.runnable(this::pingServer)
241+
.policy(ExecutePolicy.fixedDelay(this.pingInterval))
242+
.build(), this.pingInterval);
239243
}
240244

241245
private void recordServerSchema(JsonRpc.Response<Long> response) {

framework/fel/java/plugins/tool-mcp-client/src/main/java/modelengine/fel/tool/mcp/client/support/DefaultMcpClientFactory.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66

77
package modelengine.fel.tool.mcp.client.support;
88

9+
import static modelengine.fitframework.inspection.Validation.notNull;
10+
911
import modelengine.fel.tool.mcp.client.McpClient;
1012
import modelengine.fel.tool.mcp.client.McpClientFactory;
1113
import modelengine.fit.http.client.HttpClassicClient;
1214
import modelengine.fit.http.client.HttpClassicClientFactory;
1315
import modelengine.fitframework.annotation.Component;
1416
import modelengine.fitframework.annotation.Fit;
17+
import modelengine.fitframework.annotation.Value;
1518
import modelengine.fitframework.serialization.ObjectSerializer;
1619

1720
/**
@@ -26,25 +29,29 @@
2629
public class DefaultMcpClientFactory implements McpClientFactory {
2730
private final HttpClassicClient client;
2831
private final ObjectSerializer jsonSerializer;
32+
private final long pingInterval;
2933

3034
/**
3135
* Constructs a new instance of the DefaultMcpClientFactory.
3236
*
3337
* @param clientFactory The factory used to create the HTTP client.
3438
* @param jsonSerializer The JSON serializer used for serialization and deserialization.
39+
* @param pingInterval The interval between ping requests. Units: milliseconds.
3540
*/
3641
public DefaultMcpClientFactory(HttpClassicClientFactory clientFactory,
37-
@Fit(alias = "json") ObjectSerializer jsonSerializer) {
42+
@Fit(alias = "json") ObjectSerializer jsonSerializer,
43+
@Value("${mcp.client.ping-interval}") long pingInterval) {
3844
this.client = clientFactory.create(HttpClassicClientFactory.Config.builder()
3945
.connectTimeout(30_000)
4046
.socketTimeout(60_000)
4147
.connectionRequestTimeout(60_000)
4248
.build());
43-
this.jsonSerializer = jsonSerializer;
49+
this.jsonSerializer = notNull(jsonSerializer, "The json serializer cannot be null.");
50+
this.pingInterval = pingInterval;
4451
}
4552

4653
@Override
4754
public McpClient create(String baseUri, String sseEndpoint) {
48-
return new DefaultMcpClient(this.jsonSerializer, this.client, baseUri, sseEndpoint);
55+
return new DefaultMcpClient(this.jsonSerializer, this.client, baseUri, sseEndpoint, this.pingInterval);
4956
}
5057
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
fit:
22
beans:
33
packages:
4-
- 'modelengine.fel.tool.mcp.client.support'
4+
- 'modelengine.fel.tool.mcp.client.support'
5+
6+
mcp:
7+
client:
8+
ping-interval: 15000

0 commit comments

Comments
 (0)