From 12a58e3daa8a51ee5bb76e759bcf3cde210212ff Mon Sep 17 00:00:00 2001 From: chenweiqing Date: Tue, 2 Sep 2025 17:38:32 +0800 Subject: [PATCH] =?UTF-8?q?1.feat(feat-core):=20HTTP=E5=AE=A2=E6=88=B7?= =?UTF-8?q?=E7=AB=AF=E5=A2=9E=E5=8A=A0=E5=88=9D=E5=A7=8B=E5=8C=96=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E5=87=BD=E6=95=B0=202.feat(feat-ai):=20MCP=E5=AE=A2?= =?UTF-8?q?=E6=88=B7=E7=AB=AF=E5=A2=9E=E5=8A=A0=E5=88=9D=E5=A7=8B=E5=8C=96?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../smartboot/feat/ai/mcp/client/McpOptions.java | 13 ++++++++++++- .../smartboot/feat/ai/mcp/client/SseTransport.java | 4 ++-- .../feat/ai/mcp/client/StreamableTransport.java | 4 ++-- .../tech/smartboot/feat/core/client/HttpClient.java | 10 ++++++++++ 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/feat-ai/src/main/java/tech/smartboot/feat/ai/mcp/client/McpOptions.java b/feat-ai/src/main/java/tech/smartboot/feat/ai/mcp/client/McpOptions.java index 45944b1b..fc922311 100644 --- a/feat-ai/src/main/java/tech/smartboot/feat/ai/mcp/client/McpOptions.java +++ b/feat-ai/src/main/java/tech/smartboot/feat/ai/mcp/client/McpOptions.java @@ -14,6 +14,7 @@ import com.alibaba.fastjson2.JSONObject; import tech.smartboot.feat.Feat; import tech.smartboot.feat.ai.mcp.model.Implementation; import tech.smartboot.feat.ai.mcp.model.Roots; +import tech.smartboot.feat.core.client.HttpRest; import java.util.ArrayList; import java.util.List; @@ -27,6 +28,7 @@ public class McpOptions { private String baseUrl; private String mcpEndpoint = "/mcp"; private String sseEndpoint = "/sse"; + private Consumer onInitRest; private final Implementation implementation = Implementation.of("feat-mcp-client", "Feat MCP", Feat.VERSION); private boolean roots; private boolean sampling; @@ -61,10 +63,19 @@ public class McpOptions { return sseEndpoint; } - public void setSseEndpoint(String sseEndpoint) { + public McpOptions setSseEndpoint(String sseEndpoint) { this.sseEndpoint = sseEndpoint; + return this; } + public Consumer getOnInitRest() { + return this.onInitRest; + } + + public McpOptions setOnInitRest(Consumer onInitRest) { + this.onInitRest = onInitRest; + return this; + } public Implementation getImplementation() { return implementation; diff --git a/feat-ai/src/main/java/tech/smartboot/feat/ai/mcp/client/SseTransport.java b/feat-ai/src/main/java/tech/smartboot/feat/ai/mcp/client/SseTransport.java index aeaf9d3b..35cbeea7 100644 --- a/feat-ai/src/main/java/tech/smartboot/feat/ai/mcp/client/SseTransport.java +++ b/feat-ai/src/main/java/tech/smartboot/feat/ai/mcp/client/SseTransport.java @@ -44,8 +44,8 @@ final class SseTransport extends Transport { public SseTransport(McpOptions options) { super(options); - httpClient = new HttpClient(options.getBaseUrl()); - sseClient = new HttpClient(options.getBaseUrl()); + httpClient = new HttpClient(options.getBaseUrl()).setOnInitRest(options.getOnInitRest()); + sseClient = new HttpClient(options.getBaseUrl()).setOnInitRest(options.getOnInitRest()); sseClient.post(options.getSseEndpoint()).header(header -> header.set(HeaderName.ACCEPT, HeaderValue.ContentType.EVENT_STREAM).set(HeaderName.CACHE_CONTROL.getName(), HeaderValue.NO_CACHE).set(HeaderName.CONNECTION.getName(), HeaderValue.Connection.KEEPALIVE)).onResponseBody(new ServerSentEventStream() { @Override public void onEvent(HttpResponse httpResponse, Map event) { diff --git a/feat-ai/src/main/java/tech/smartboot/feat/ai/mcp/client/StreamableTransport.java b/feat-ai/src/main/java/tech/smartboot/feat/ai/mcp/client/StreamableTransport.java index 0c62a0fc..f31284e3 100644 --- a/feat-ai/src/main/java/tech/smartboot/feat/ai/mcp/client/StreamableTransport.java +++ b/feat-ai/src/main/java/tech/smartboot/feat/ai/mcp/client/StreamableTransport.java @@ -42,8 +42,8 @@ final class StreamableTransport extends Transport { public StreamableTransport(McpOptions options) { super(options); - httpClient = new HttpClient(options.getBaseUrl()); - sseClient = new HttpClient(options.getBaseUrl()); + httpClient = new HttpClient(options.getBaseUrl()).setOnInitRest(options.getOnInitRest()); + sseClient = new HttpClient(options.getBaseUrl()).setOnInitRest(options.getOnInitRest()); } @Override diff --git a/feat-core/src/main/java/tech/smartboot/feat/core/client/HttpClient.java b/feat-core/src/main/java/tech/smartboot/feat/core/client/HttpClient.java index 1bc78453..673c64d7 100644 --- a/feat-core/src/main/java/tech/smartboot/feat/core/client/HttpClient.java +++ b/feat-core/src/main/java/tech/smartboot/feat/core/client/HttpClient.java @@ -28,6 +28,7 @@ import java.util.Base64; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.function.Consumer; /** * @author 三刀 zhengjunweimail@163.com @@ -61,6 +62,12 @@ public final class HttpClient { private final HttpMessageProcessor processor = new HttpMessageProcessor(); private final String uri; + private Consumer onInitRest; + + public HttpClient setOnInitRest(Consumer onInitRest) { + this.onInitRest = onInitRest; + return this; + } public HttpClient(String url) { int schemaIndex = url.indexOf("://"); if (schemaIndex == -1) { @@ -185,6 +192,9 @@ public final class HttpClient { releaseConnection(client); return null; }); + if(onInitRest != null) { + onInitRest.accept(httpRestImpl); + } } -- Gitee