From 4bf26bdf82f2db8214aa38fba95951d78ba24ea4 Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Mon, 2 Nov 2026 17:37:43 +0100 Subject: [PATCH] fix(mcp): recognise the current BTrace client's busy-port error The BTrace client now says "Port N is in use by another process ... choose a free agent port with -p " instead of "Port N unavailable". The MCP server matched only the old wording, so a busy port reached the user with advice about a CLI flag that means nothing over MCP, and the e2e test for it failed. Both wordings now give the same message, naming the 'port' argument. Verified: the new BTraceClientTest case fails without the change. ./gradlew test: 32 tests, 0 failed. e2eTest against the current BTrace 3.0.0-SNAPSHOT jar: 13 tests, 0 failed, aBusyPortIsReportedWithTheArgumentThatAvoidsIt included. validate-marketplace passed. Co-Authored-By: Claude Opus 5.5 --- .../main/java/io/btrace/mcp/BTraceClient.java | 19 +++++++++++++------ .../test/java/io/btrace/client/Client.java | 8 ++++++++ .../java/io/btrace/mcp/BTraceClientTest.java | 11 +++++++++++ 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/plugins/btrace-observability/mcp/src/main/java/io/btrace/mcp/BTraceClient.java b/plugins/btrace-observability/mcp/src/main/java/io/btrace/mcp/BTraceClient.java index cc9a754..c0dd3d2 100644 --- a/plugins/btrace-observability/mcp/src/main/java/io/btrace/mcp/BTraceClient.java +++ b/plugins/btrace-observability/mcp/src/main/java/io/btrace/mcp/BTraceClient.java @@ -26,6 +26,7 @@ import java.net.URL; import java.security.CodeSource; import java.security.ProtectionDomain; +import java.util.regex.Matcher; import java.util.regex.Pattern; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -40,7 +41,9 @@ public final class BTraceClient { private static final Logger log = LoggerFactory.getLogger(BTraceClient.class); private static final String JAR_PATH_PROPERTY = "btrace.jar.path"; - private static final Pattern PORT_UNAVAILABLE = Pattern.compile("Port \\d+ unavailable.*"); + // Older clients say "unavailable"; newer ones say "is in use" and advise the CLI's -p flag + private static final Pattern PORT_UNAVAILABLE = + Pattern.compile("Port (\\d+) (?:unavailable|is in use by another process).*"); @FunctionalInterface public interface CommandListener { @@ -236,12 +239,16 @@ private static Object call(Method method, Object target, Object... args) throws */ private static Exception unwrap(InvocationTargetException e) { Throwable cause = e.getCause(); - if (cause instanceof IOException - && cause.getMessage() != null - && PORT_UNAVAILABLE.matcher(cause.getMessage()).matches()) { + Matcher portUnavailable = + cause instanceof IOException && cause.getMessage() != null + ? PORT_UNAVAILABLE.matcher(cause.getMessage()) + : null; + if (portUnavailable != null && portUnavailable.matches()) { return new IOException( - cause.getMessage() - + " Another BTrace agent or process is using it; pass a different 'port' argument.", + "Port " + + portUnavailable.group(1) + + " unavailable. Another BTrace agent or process is using it; pass a different" + + " 'port' argument.", cause); } if (cause instanceof Exception && cause.getMessage() != null) { diff --git a/plugins/btrace-observability/mcp/src/test/java/io/btrace/client/Client.java b/plugins/btrace-observability/mcp/src/test/java/io/btrace/client/Client.java index 6f79a5f..e73ae3c 100644 --- a/plugins/btrace-observability/mcp/src/test/java/io/btrace/client/Client.java +++ b/plugins/btrace-observability/mcp/src/test/java/io/btrace/client/Client.java @@ -39,6 +39,14 @@ public void attach(String pid, String systemClassPath, String bootstrapClassPath if (pid.equals("port-busy")) { throw new java.io.IOException("Port " + port + " unavailable."); } + if (pid.equals("port-in-use")) { + // The wording of the current BTrace client + throw new java.io.IOException( + "Port " + + port + + " is in use by another process (often another BTrace agent); choose a free" + + " agent port with -p ."); + } if (pid.equals("no-message")) { throw new java.io.IOException(); } diff --git a/plugins/btrace-observability/mcp/src/test/java/io/btrace/mcp/BTraceClientTest.java b/plugins/btrace-observability/mcp/src/test/java/io/btrace/mcp/BTraceClientTest.java index c978869..fbcc6fc 100644 --- a/plugins/btrace-observability/mcp/src/test/java/io/btrace/mcp/BTraceClientTest.java +++ b/plugins/btrace-observability/mcp/src/test/java/io/btrace/mcp/BTraceClientTest.java @@ -82,6 +82,17 @@ void aBusyPortNamesThePortAndTheArgumentThatAvoidsIt() throws Exception { assertTrue(e.getMessage().contains("'port' argument"), e.getMessage()); } + @Test + void aBusyPortInTheCurrentClientWordingNamesTheArgumentNotTheCliFlag() throws Exception { + BTraceClient client = BTraceClient.create(2020); + + IOException e = assertThrows(IOException.class, () -> client.attach("port-in-use", null, ".")); + + assertTrue(e.getMessage().startsWith("Port 2020 unavailable. Another"), e.getMessage()); + assertTrue(e.getMessage().contains("'port' argument"), e.getMessage()); + assertFalse(e.getMessage().contains("-p"), e.getMessage()); + } + @Test void aFailureWithoutAMessageStillDescribesItself() throws Exception { BTraceClient client = BTraceClient.create(2020);