Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
6 changes: 4 additions & 2 deletions CodenameOne/src/com/codename1/mcp/MCP.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions docs/developer-guide/MCP-Headless-API.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading