# 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 Summary

  Fields
  Modifier and Type
  Field
  Description
  `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.
  `static final int`
  `INTERNAL_ERROR`
  `static final int`
  `INVALID_PARAMS`
  `static final int`
  `INVALID_REQUEST`
  `static final int`
  `METHOD_NOT_FOUND`
  `static final int`
  `PARSE_ERROR`
  Standard JSON-RPC and MCP error codes.
  `static final int`
  `RESOURCE_NOT_FOUND`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `MCPServer()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `addTool(Tool tool)`
  Registers a developer defined tool, replacing any existing tool with the same name.
  `MCPVerbosity`
  `getVerbosity()`
  `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.
  `boolean`
  `isRunning()`
  `void`
  `removeTool(String name)`
  Removes a previously registered tool by name.
  `void`
  `setScreenshotEnabled(boolean screenshotEnabled)`
  Enables or disables the built in screenshot resource.
  `void`
  `setServerInfo(String name, String version)`
  Sets the server identity reported to the host during `initialize`.
  `void`
  `setVerbosity(MCPVerbosity verbosity)`
  Sets how much of the MCP conversation is echoed to the Codename One log for debugging.
  `void`
  `start(MCPTransport transport)`
  Starts serving over the given transport on a dedicated reader thread.
  `void`
  `stop()`
  Stops serving and closes the transport.

  ### Methods inherited from class Object

  `clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### PARSE_ERROR

    public static final int PARSE_ERROR
    Standard JSON-RPC and MCP error codes.
    See Also:
      - Constant Field Values

  - ### INVALID_REQUEST

    public static final int INVALID_REQUEST
    See Also:
      - Constant Field Values

  - ### METHOD_NOT_FOUND

    public static final int METHOD_NOT_FOUND
    See Also:
      - Constant Field Values

  - ### INVALID_PARAMS

    public static final int INVALID_PARAMS
    See Also:
      - Constant Field Values

  - ### INTERNAL_ERROR

    public static final int INTERNAL_ERROR
    See Also:
      - Constant Field Values

  - ### RESOURCE_NOT_FOUND

    public static final int RESOURCE_NOT_FOUND
    See Also:
      - Constant Field Values

  - ### 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:
      - Constant Field Values

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