Class McpRequestHandler

java.lang.Object
com.tnsai.mcp.server.McpRequestHandler

public class McpRequestHandler extends Object
Pure-logic handler for MCP JSON-RPC requests.

This class contains all routing and request handling logic extracted from McpServer. It is transport-agnostic: it takes a JSON-RPC message string and returns a McpRequestHandler.HandlerResult without performing any I/O. Both McpServer (stdio) and HttpMcpServer (HTTP) delegate to this class.

Supports the following MCP protocols:

  • Tools (list, call)
  • Resources (list, read, subscribe, templates)
  • Prompts (list, get)
  • Sampling (createMessage)
  • Completion (complete)
  • Logging (setLevel)
  • Roots (list via client capabilities)
  • Constructor Details

    • McpRequestHandler

      public McpRequestHandler(String serverName, String serverVersion)
      Creates a new McpRequestHandler.
      Parameters:
      serverName - the server name reported during initialization
      serverVersion - the server version reported during initialization
  • Method Details

    • processMessage

      public McpRequestHandler.HandlerResult processMessage(String message)
      Processes a single JSON-RPC message and returns the result.
      Parameters:
      message - the JSON-RPC message string
      Returns:
      the result of processing
    • buildNotification

      public String buildNotification(String method, com.fasterxml.jackson.databind.JsonNode params)
      Builds a JSON-RPC notification string.
      Parameters:
      method - the notification method
      params - optional params (may be null)
      Returns:
      the JSON-RPC notification string
    • buildLogNotification

      public String buildLogNotification(LogMessage logMessage)
      Builds a log notification message for sending to the client.
      Parameters:
      logMessage - the log message
      Returns:
      the JSON-RPC notification string, or null if below current log level
    • addToolErrorListener

      public void addToolErrorListener(ToolErrorListener listener)
      Adds a listener for tool execution error events.
    • removeToolErrorListener

      public void removeToolErrorListener(ToolErrorListener listener)
      Removes a tool error listener.
    • getToolErrorHistory

      public List<ToolErrorEvent> getToolErrorHistory()
      Returns the history of tool error events (most recent last).
    • clearToolErrorHistory

      public void clearToolErrorHistory()
      Clears the tool error history.
    • addToolProvider

      public void addToolProvider(ToolProvider provider)
      Adds a tool provider.
      Parameters:
      provider - the tool provider to add
    • removeToolProvider

      public void removeToolProvider(ToolProvider provider)
      Removes a tool provider.
      Parameters:
      provider - the tool provider to remove
    • addResourceProvider

      public void addResourceProvider(ResourceProvider provider)
      Adds a resource provider.
      Parameters:
      provider - the resource provider to add
    • removeResourceProvider

      public void removeResourceProvider(ResourceProvider provider)
      Removes a resource provider.
      Parameters:
      provider - the resource provider to remove
    • addPromptProvider

      public void addPromptProvider(PromptProvider provider)
      Adds a prompt provider.
      Parameters:
      provider - the prompt provider to add
    • removePromptProvider

      public void removePromptProvider(PromptProvider provider)
      Removes a prompt provider.
      Parameters:
      provider - the prompt provider to remove
    • setResourceUpdateNotifier

      public void setResourceUpdateNotifier(Consumer<String> notifier)
      Sets a callback for resource update notifications.
      Parameters:
      notifier - the callback that receives the updated resource URI
    • setSamplingHandler

      public void setSamplingHandler(Function<SamplingRequest,SamplingResponse> handler)
      Sets the handler for sampling/createMessage requests. The function receives a SamplingRequest and must return a SamplingResponse.
      Parameters:
      handler - the sampling handler function
    • getLogLevel

      public LogLevel getLogLevel()
      Returns the current log level.
      Returns:
      the current minimum log level
    • setLogNotifier

      public void setLogNotifier(Consumer<LogMessage> notifier)
      Sets a callback for log message notifications.
      Parameters:
      notifier - the callback that receives log messages
    • emitLogMessage

      public void emitLogMessage(LogMessage logMessage)
      Emits a log message. If a log notifier is set and the message meets the minimum log level, the notifier is called.
      Parameters:
      logMessage - the log message to emit
    • setClientRoots

      public void setClientRoots(List<Root> roots)
      Sets the roots provided by the client.
      Parameters:
      roots - the list of client roots
    • getClientRoots

      public List<Root> getClientRoots()
      Returns the roots provided by the client.
      Returns:
      immutable list of client roots
    • setOnRootsChanged

      public void setOnRootsChanged(Consumer<List<Root>> listener)
      Sets a callback for when client roots change.
      Parameters:
      listener - the callback
    • isInitialized

      public boolean isInitialized()
      Returns whether this handler has been initialized by a client.
      Returns:
      true if initialized
    • getServerName

      public String getServerName()
      Returns the server name.
      Returns:
      the server name
    • getServerVersion

      public String getServerVersion()
      Returns the server version.
      Returns:
      the server version