Class MCPServer

java.lang.Object
com.codename1.mcp.MCPServer

public class MCPServer extends Object

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 Details

    • PARSE_ERROR

      public static final int PARSE_ERROR
      Standard 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

      public static final String 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

      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 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 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 boolean isRunning()
    • start

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

      public 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.