From dbb05e4c95c52653c8d9f3c85e5f8329f21c90c3 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 5 Dec 2026 16:37:48 +0000 Subject: [PATCH] mcp.devicePort build hint serves MCP from a development build Driving an app on a device or emulator needed MCP.startSocketServer in the app's own code. With `codename1.arg.mcp.devicePort=N` the builders' shared start invocation (iOS, Android, macOS) starts the socket server on N before the app's first start(), once, and only where MCP.isSocketServerAllowedOnThisBuild() holds, so a release build never serves it. The hint is declared on @OnDeviceDebug as mcpDevicePort and documented in the MCP chapter. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SZMcbmyfhDCp3DQ3U1YvWh --- .../annotations/buildhints/OnDeviceDebug.java | 10 ++++ CodenameOne/src/com/codename1/mcp/MCP.java | 6 +- .../developer-guide/MCP-Headless-API.asciidoc | 2 + .../java/com/codename1/builders/Executor.java | 34 +++++++++++ .../builders/ExecutorMcpDevicePortTest.java | 59 +++++++++++++++++++ 5 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 maven/build-engine/src/test/java/com/codename1/builders/ExecutorMcpDevicePortTest.java diff --git a/CodenameOne/src/com/codename1/annotations/buildhints/OnDeviceDebug.java b/CodenameOne/src/com/codename1/annotations/buildhints/OnDeviceDebug.java index f8095ca271d..7eebcde0cc8 100644 --- a/CodenameOne/src/com/codename1/annotations/buildhints/OnDeviceDebug.java +++ b/CodenameOne/src/com/codename1/annotations/buildhints/OnDeviceDebug.java @@ -79,4 +79,14 @@ /// effect unless `ios.onDeviceDebug=true`. @Hint(name = "ios.onDeviceDebug.waitForAttach", platform = "ios") Toggle iosWaitForAttach() default Toggle.DEFAULT; + + /// Loopback port a development build serves Codename One's MCP tools on, so an + /// agent can read the screen and drive the app on a device or emulator without + /// a call to `MCP.startSocketServer` in the app's code. The generated app starts + /// the server just before its first `start()`. A release build never serves it + /// (see `MCP.setAllowOnReleaseBuilds`), so the hint can stay set when you ship. + /// Reach the port from the desktop with `adb forward tcp:N tcp:N` on Android; the + /// iOS simulator shares the Mac's loopback interface. + @Hint(name = "mcp.devicePort", platform = "general", valuePattern = "[1-9][0-9]{0,4}") + int mcpDevicePort() default 0; } diff --git a/CodenameOne/src/com/codename1/mcp/MCP.java b/CodenameOne/src/com/codename1/mcp/MCP.java index 7a03a1bcd02..17e056ad7d2 100644 --- a/CodenameOne/src/com/codename1/mcp/MCP.java +++ b/CodenameOne/src/com/codename1/mcp/MCP.java @@ -26,8 +26,10 @@ import com.codename1.ui.Display; /// Public entry point for the Codename One MCP headless API. Invoking a starter here is -/// the enablement; there is no build hint or property to toggle. A single server -/// instance exists per process. +/// the enablement. The one exception is the `mcp.devicePort` build hint +/// ([com.codename1.annotations.buildhints.OnDeviceDebug#mcpDevicePort()]), which makes a +/// development build call [#startSocketServer(int)] itself. A single server instance +/// exists per process. /// /// The stdio transport is supported by the Codename One JavaSE port, which powers the /// simulator and the desktop tooling. The socket transport also serves a build running on diff --git a/docs/developer-guide/MCP-Headless-API.asciidoc b/docs/developer-guide/MCP-Headless-API.asciidoc index b4244025c6c..cf22f303117 100644 --- a/docs/developer-guide/MCP-Headless-API.asciidoc +++ b/docs/developer-guide/MCP-Headless-API.asciidoc @@ -61,6 +61,8 @@ It also drives a development build on the iOS simulator or an Android device, an The socket transport binds the loopback interface, and it binds the loopback interface of whatever machine the application is running on. In the simulator that's your own, so an agent connects to `127.0.0.1` and there's nothing else to arrange. On a phone the port belongs to the phone, and reaching it needs a forward. +To serve the tools from a device build without calling a starter in your code, set the `mcp.devicePort` build hint, for example `codename1.arg.mcp.devicePort=8765` in `codenameone_settings.properties`. The generated application then starts the socket server on that port just before its first `start()`, and only on a development build, so the hint can stay set in a release. + The native Linux port supports the portable socket transport through an IPv4 listener bound to `127.0.0.1`. Stopping the listener wakes a waiting listener, and closing a connection wakes its blocked input/output operations. This capability doesn't enable wildcard-bound server sockets or change the release-build guard. Native Linux builds still require the explicit, controlled-development opt-in described below. For a Linux application running in a container, run the MCP client in that same container or network namespace. Publishing a container port doesn't make a listener bound to its internal loopback address reachable from the host. A local headless test can use Xvfb; when its launcher runs in Docker, use `--init` so the launcher's readiness signals are delivered normally. Native window-manager behavior still needs a desktop session. diff --git a/maven/build-engine/src/main/java/com/codename1/builders/Executor.java b/maven/build-engine/src/main/java/com/codename1/builders/Executor.java index 5559c022746..5265c9082e8 100644 --- a/maven/build-engine/src/main/java/com/codename1/builders/Executor.java +++ b/maven/build-engine/src/main/java/com/codename1/builders/Executor.java @@ -270,6 +270,40 @@ protected String createStartInvocation(BuildRequest request, String mainObject) } protected String createStartInvocation(BuildRequest request, String mainObject, boolean includeVserv) { + return mcpDevicePortInvocation(request) + createAppStartInvocation(request, mainObject, includeVserv); + } + + /// The statements a build carrying `mcp.devicePort` runs before the app's start(): + /// start the MCP socket server on that port, once, and only where + /// [com.codename1.mcp.MCP#isSocketServerAllowedOnThisBuild()] says the build may + /// serve it, so a release build never does. Empty when the hint is unset. + static String mcpDevicePortInvocation(BuildRequest request) { + String value = request.getArg("mcp.devicePort", null); + if (value == null || value.trim().length() == 0) { + return ""; + } + int port; + try { + port = Integer.parseInt(value.trim()); + } catch (NumberFormatException ex) { + throw new IllegalArgumentException("The mcp.devicePort build hint must be a port number, not \"" + + value + "\"", ex); + } + if (port < 1 || port > 65535) { + throw new IllegalArgumentException("The mcp.devicePort build hint must be a port between 1 and 65535, not " + + port); + } + return " if (!com.codename1.mcp.MCP.isRunning()" + + " && com.codename1.mcp.MCP.isSocketServerAllowedOnThisBuild()) {\n" + + " try {\n" + + " com.codename1.mcp.MCP.startSocketServer(" + port + ");\n" + + " } catch (RuntimeException cn1McpError) {\n" + + " com.codename1.io.Log.e(cn1McpError);\n" + + " }\n" + + " }\n"; + } + + private String createAppStartInvocation(BuildRequest request, String mainObject, boolean includeVserv) { String zone = request.getArg("vserv.zone", null); if (includeVserv && zone != null && zone.length() > 0) { String transition = request.getArg("vserv.transition", "300000"); diff --git a/maven/build-engine/src/test/java/com/codename1/builders/ExecutorMcpDevicePortTest.java b/maven/build-engine/src/test/java/com/codename1/builders/ExecutorMcpDevicePortTest.java new file mode 100644 index 00000000000..5f8a97d77d3 --- /dev/null +++ b/maven/build-engine/src/test/java/com/codename1/builders/ExecutorMcpDevicePortTest.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2026, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ +package com.codename1.builders; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class ExecutorMcpDevicePortTest { + @Test + public void noHintAddsNothing() { + assertEquals("", Executor.mcpDevicePortInvocation(new BuildRequest())); + } + + @Test + public void hintStartsTheServerOnceAndOnlyWhereTheBuildMayServeIt() { + BuildRequest request = new BuildRequest(); + request.putArgument("mcp.devicePort", "8765"); + String code = Executor.mcpDevicePortInvocation(request); + assertTrue(code, code.contains("com.codename1.mcp.MCP.startSocketServer(8765);")); + assertTrue(code, code.contains("!com.codename1.mcp.MCP.isRunning()")); + assertTrue(code, code.contains("com.codename1.mcp.MCP.isSocketServerAllowedOnThisBuild()")); + } + + @Test(expected = IllegalArgumentException.class) + public void aPortOutOfRangeFailsTheBuild() { + BuildRequest request = new BuildRequest(); + request.putArgument("mcp.devicePort", "70000"); + Executor.mcpDevicePortInvocation(request); + } + + @Test(expected = IllegalArgumentException.class) + public void aValueThatIsNotANumberFailsTheBuild() { + BuildRequest request = new BuildRequest(); + request.putArgument("mcp.devicePort", "auto"); + Executor.mcpDevicePortInvocation(request); + } +}