Class MCPServer
java.lang.Object
com.codename1.mcp.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.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringMCP protocol revision advertised when the client does not request one, or requests one this server does not implement.static final intstatic final intstatic final intstatic final intstatic final intStandard JSON-RPC and MCP error codes.static final int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidRegisters a developer defined tool, replacing any existing tool with the same name.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.booleanvoidremoveTool(String name) Removes a previously registered tool by name.voidsetScreenshotEnabled(boolean screenshotEnabled) Enables or disables the built in screenshot resource.voidsetServerInfo(String name, String version) Sets the server identity reported to the host duringinitialize.voidsetVerbosity(MCPVerbosity verbosity) Sets how much of the MCP conversation is echoed to the Codename One log for debugging.voidstart(MCPTransport transport) Starts serving over the given transport on a dedicated reader thread.voidstop()Stops serving and closes the transport.
-
Field Details
-
PARSE_ERROR
public static final int PARSE_ERRORStandard JSON-RPC and MCP error codes.- See Also:
-
INVALID_REQUEST
public static final int INVALID_REQUEST- See Also:
-
METHOD_NOT_FOUND
public static final int METHOD_NOT_FOUND- See Also:
-
INVALID_PARAMS
public static final int INVALID_PARAMS- See Also:
-
INTERNAL_ERROR
public static final int INTERNAL_ERROR- See Also:
-
RESOURCE_NOT_FOUND
public static final int RESOURCE_NOT_FOUND- See Also:
-
DEFAULT_PROTOCOL_VERSION
MCP protocol revision advertised when the client does not request one, or requests one this server does not implement.- See Also:
-
-
Constructor Details
-
MCPServer
public MCPServer()
-
-
Method Details
-
setVerbosity
Sets how much of the MCP conversation is echoed to the Codename One log for debugging. Defaults toMCPVerbosity.OFF. -
getVerbosity
-
addTool
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
Removes a previously registered tool by name. -
setServerInfo
-
setScreenshotEnabled
public void setScreenshotEnabled(boolean screenshotEnabled) Enables or disables the built in screenshot resource. Enabled by default. -
isRunning
public boolean isRunning() -
start
Starts serving over the given transport on a dedicated reader thread. -
stop
public void stop()Stops serving and closes the transport. -
handleMessage
-