public class MCPServer

  1. Object
  2. MCPServer

A Model Context Protocol server. Speaks JSON-RPC 2.0 over a pluggable MCPTransport, dispatching the MCP methods initialize, tools/list, tools/call, resources/list, resources/read, ping and the notifications/* family.

Tools are Tool instances. The built in tools from McpUiTools expose the accessibility semantics tree so any application is drivable without code; applications add domain tools with addTool(Tool).

The message dispatch is thread free and reentrant: handleMessage(String) takes one request line and returns one response line (or null for a notification), which makes it directly unit testable. start(MCPTransport) wraps that in a reader thread over the transport.

Fields

public static final int PARSE_ERROR = -32700Standard JSON-RPC and MCP error codes.
public static final int INVALID_REQUEST = -32600
public static final int METHOD_NOT_FOUND = -32601
public static final int INVALID_PARAMS = -32602
public static final int INTERNAL_ERROR = -32603
public static final int RESOURCE_NOT_FOUND = -32002
public static final String DEFAULT_PROTOCOL_VERSION = "2024-11-05"MCP protocol revision advertised when the client does not request one, or requests one this server does not implement.

Constructors

public MCPServer()

Methods

public void setVerbosity(MCPVerbosity verbosity)Sets how much of the MCP conversation is echoed to the Codename One log for debugging.
public MCPVerbosity getVerbosity()
public synchronized void addTool(Tool tool)Registers a developer defined tool, replacing any existing tool with the same name.
public synchronized void removeTool(String name)Removes a previously registered tool by name.
public void setServerInfo(String name, String version)Sets the server identity reported to the host during initialize.
public void setScreenshotEnabled(boolean screenshotEnabled)Enables or disables the built in screenshot resource.
public synchronized boolean isRunning()
public synchronized void start(MCPTransport transport)Starts serving over the given transport on a dedicated reader thread.
public synchronized void stop()Stops serving and closes the transport.
public String handleMessage(String line)Handles one inbound JSON-RPC message and returns the response line, or null when the message is a notification that warrants no reply.

Inherited methods

Field details

PARSE_ERROR

public static final int PARSE_ERROR = -32700
Standard JSON-RPC and MCP error codes.

INVALID_REQUEST

public static final int INVALID_REQUEST = -32600

METHOD_NOT_FOUND

public static final int METHOD_NOT_FOUND = -32601

INVALID_PARAMS

public static final int INVALID_PARAMS = -32602

INTERNAL_ERROR

public static final int INTERNAL_ERROR = -32603

RESOURCE_NOT_FOUND

public static final int RESOURCE_NOT_FOUND = -32002

DEFAULT_PROTOCOL_VERSION

public static final String DEFAULT_PROTOCOL_VERSION = "2024-11-05"
MCP protocol revision advertised when the client does not request one, or requests one this server does not implement.

Constructor details

MCPServer

public MCPServer()

Method details

setVerbosity

public void setVerbosity(MCPVerbosity verbosity)
Sets how much of the MCP conversation is echoed to the Codename One log for debugging. Defaults to MCPVerbosity.OFF.

getVerbosity

public MCPVerbosity getVerbosity()

addTool

public synchronized void addTool(Tool tool)
Registers a developer defined tool, replacing any existing tool with the same name. This is how an application publishes domain specific data and actions.

removeTool

public synchronized void removeTool(String name)
Removes a previously registered tool by name.

setServerInfo

public void setServerInfo(String name, String version)
Sets the server identity reported to the host during initialize.

setScreenshotEnabled

public void setScreenshotEnabled(boolean screenshotEnabled)
Enables or disables the built in screenshot resource. Enabled by default.

isRunning

public synchronized boolean isRunning()

start

public synchronized void start(MCPTransport transport)
Starts serving over the given transport on a dedicated reader thread.

stop

public synchronized void stop()
Stops serving and closes the transport.

handleMessage

public String handleMessage(String line)
Handles one inbound JSON-RPC message and returns the response line, or null when the message is a notification that warrants no reply. Never throws; every failure is turned into a JSON-RPC error response.