Skip to content

Commit fc93554

Browse files
committed
Shut down local MCP server when the client disconnects
1 parent b5959a9 commit fc93554

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

mcp/mcp-cli-api/src/main/java/software/amazon/smithy/java/mcp/cli/SmithyMcpCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public final Integer call() throws Exception {
5454
protected String registryToUse(Config config) {
5555
return config.getDefaultRegistry();
5656
}
57-
57+
5858
protected final CommandLine.Model.CommandSpec commandSpec() {
5959
return commandSpec;
6060
}

mcp/mcp-cli/src/main/java/software/amazon/smithy/java/mcp/cli/commands/StartServer.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.io.IOException;
99
import java.util.ArrayList;
1010
import java.util.List;
11+
import java.util.concurrent.CountDownLatch;
1112
import java.util.stream.Collectors;
1213
import picocli.CommandLine.Command;
1314
import picocli.CommandLine.Option;
@@ -52,6 +53,7 @@ public final class StartServer extends SmithyMcpCommand {
5253
boolean registryServer;
5354

5455
private volatile McpServer mcpServer;
56+
private final CountDownLatch shutdownLatch = new CountDownLatch(1);
5557

5658
/**
5759
* Executes the start-server command.
@@ -115,10 +117,16 @@ public void execute(ExecutionContext context) throws IOException {
115117
this.mcpServer =
116118
(McpServer) McpServer.builder().stdio().addServices(services).name("smithy-mcp-server").build();
117119
mcpServer.start();
120+
121+
boolean shutdown = false;
118122
try {
119-
Thread.currentThread().join();
120-
} catch (InterruptedException e) {
123+
mcpServer.awaitCompletion();
124+
shutdown = true;
121125
mcpServer.shutdown().join();
126+
} catch (Exception e) {
127+
if (!shutdown) {
128+
mcpServer.shutdown().join();
129+
}
122130
}
123131
}
124132

mcp/mcp-server/src/main/java/software/amazon/smithy/java/mcp/server/McpServer.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.Scanner;
1818
import java.util.concurrent.CompletableFuture;
1919
import java.util.concurrent.ConcurrentHashMap;
20+
import java.util.concurrent.CountDownLatch;
2021
import software.amazon.smithy.java.core.schema.Schema;
2122
import software.amazon.smithy.java.core.schema.SerializableShape;
2223
import software.amazon.smithy.java.core.schema.SerializableStruct;
@@ -61,6 +62,7 @@ public final class McpServer implements Server {
6162
private final OutputStream os;
6263
private final String name;
6364
private final List<McpServerProxy> proxies;
65+
private final CountDownLatch done = new CountDownLatch(1);
6466

6567
McpServer(McpServerBuilder builder) {
6668
this.tools = createTools(builder.serviceList);
@@ -73,6 +75,8 @@ public final class McpServer implements Server {
7375
this.listen();
7476
} catch (Exception e) {
7577
LOG.error("Error handling request", e);
78+
} finally {
79+
done.countDown();
7680
}
7781
});
7882
listener.setName("stdio-dispatcher");
@@ -350,6 +354,10 @@ public CompletableFuture<Void> shutdown() {
350354
}
351355
}
352356

357+
public void awaitCompletion() throws InterruptedException {
358+
done.await();
359+
}
360+
353361
private record Tool(ToolInfo toolInfo, Operation operation, McpServerProxy proxy) {
354362

355363
Tool(ToolInfo toolInfo, Operation operation) {

0 commit comments

Comments
 (0)