Skip to content

Commit bb320ab

Browse files
committed
refactor(server): Convert McpHttpServer to a record and update start method signature
1 parent 5255ad3 commit bb320ab

File tree

3 files changed

+21
-26
lines changed

3 files changed

+21
-26
lines changed

src/main/java/com/github/codeboyzhou/mcp/declarative/server/McpHttpServer.java

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,15 @@
88
import org.slf4j.Logger;
99
import org.slf4j.LoggerFactory;
1010

11-
public class McpHttpServer {
11+
public record McpHttpServer(int port) {
1212

1313
private static final Logger logger = LoggerFactory.getLogger(McpHttpServer.class);
1414

1515
private static final String DEFAULT_SERVLET_CONTEXT_PATH = "/";
1616

1717
private static final String DEFAULT_SERVLET_PATH = "/*";
1818

19-
private final HttpServletSseServerTransportProvider transportProvider;
20-
21-
private final int port;
22-
23-
public McpHttpServer(HttpServletSseServerTransportProvider transportProvider, int port) {
24-
this.transportProvider = transportProvider;
25-
this.port = port;
26-
}
27-
28-
public void start() {
19+
public void start(HttpServletSseServerTransportProvider transportProvider) {
2920
ServletContextHandler handler = new ServletContextHandler(ServletContextHandler.SESSIONS);
3021
handler.setContextPath(DEFAULT_SERVLET_CONTEXT_PATH);
3122

@@ -62,16 +53,16 @@ public void start() {
6253
}
6354

6455
private void addShutdownHook(Server httpserver) {
65-
Runtime.getRuntime()
66-
.addShutdownHook(
67-
new Thread(
68-
() -> {
69-
try {
70-
logger.info("Shutting down HTTP server and MCP server");
71-
httpserver.stop();
72-
} catch (Exception e) {
73-
logger.error("Error stopping HTTP server and MCP server", e);
74-
}
75-
}));
56+
Runnable runnable =
57+
() -> {
58+
try {
59+
logger.info("Shutting down HTTP server and MCP server");
60+
httpserver.stop();
61+
} catch (Exception e) {
62+
logger.error("Error stopping HTTP server and MCP server", e);
63+
}
64+
};
65+
Thread shutdownHook = new Thread(runnable);
66+
Runtime.getRuntime().addShutdownHook(shutdownHook);
7667
}
7768
}

src/main/java/com/github/codeboyzhou/mcp/declarative/server/factory/ConfigurableMcpHttpSseServerFactory.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import io.modelcontextprotocol.server.McpServer;
99
import io.modelcontextprotocol.server.transport.HttpServletSseServerTransportProvider;
1010
import java.time.Duration;
11+
import java.util.concurrent.ExecutorService;
1112
import java.util.concurrent.Executors;
1213

1314
public class ConfigurableMcpHttpSseServerFactory
@@ -40,9 +41,10 @@ public McpAsyncServer create() {
4041
.instructions(configuration.instructions())
4142
.requestTimeout(Duration.ofMillis(configuration.requestTimeout()))
4243
.build();
43-
McpHttpServer httpServer = new McpHttpServer(transportProvider, configuration.sse().port());
44+
McpHttpServer httpServer = new McpHttpServer(configuration.sse().port());
4445
NamedThreadFactory threadFactory = new NamedThreadFactory(McpHttpServer.class.getSimpleName());
45-
Executors.newSingleThreadExecutor(threadFactory).execute(httpServer::start);
46+
ExecutorService executor = Executors.newSingleThreadExecutor(threadFactory);
47+
executor.execute(() -> httpServer.start(transportProvider));
4648
return server;
4749
}
4850
}

src/main/java/com/github/codeboyzhou/mcp/declarative/server/factory/McpHttpSseServerFactory.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.modelcontextprotocol.server.McpAsyncServer;
77
import io.modelcontextprotocol.server.McpServer;
88
import io.modelcontextprotocol.server.transport.HttpServletSseServerTransportProvider;
9+
import java.util.concurrent.ExecutorService;
910
import java.util.concurrent.Executors;
1011

1112
public class McpHttpSseServerFactory
@@ -33,9 +34,10 @@ public McpAsyncServer create(McpSseServerInfo serverInfo) {
3334
.instructions(serverInfo.instructions())
3435
.requestTimeout(serverInfo.requestTimeout())
3536
.build();
36-
McpHttpServer httpServer = new McpHttpServer(transportProvider, serverInfo.port());
37+
McpHttpServer httpServer = new McpHttpServer(serverInfo.port());
3738
NamedThreadFactory threadFactory = new NamedThreadFactory(McpHttpServer.class.getSimpleName());
38-
Executors.newSingleThreadExecutor(threadFactory).execute(httpServer::start);
39+
ExecutorService executor = Executors.newSingleThreadExecutor(threadFactory);
40+
executor.execute(() -> httpServer.start(transportProvider));
3941
return server;
4042
}
4143
}

0 commit comments

Comments
 (0)