Package com.tnsai.mcp.server
Class McpRequestHandler
java.lang.Object
com.tnsai.mcp.server.McpRequestHandler
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)
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceResult of processing a JSON-RPC message. -
Constructor Summary
ConstructorsConstructorDescriptionMcpRequestHandler(String serverName, String serverVersion) Creates a new McpRequestHandler. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddPromptProvider(PromptProvider provider) Adds a prompt provider.voidaddResourceProvider(ResourceProvider provider) Adds a resource provider.voidaddToolErrorListener(ToolErrorListener listener) Adds a listener for tool execution error events.voidaddToolProvider(ToolProvider provider) Adds a tool provider.buildLogNotification(LogMessage logMessage) Builds a log notification message for sending to the client.buildNotification(String method, com.fasterxml.jackson.databind.JsonNode params) Builds a JSON-RPC notification string.voidClears the tool error history.voidemitLogMessage(LogMessage logMessage) Emits a log message.Returns the roots provided by the client.Returns the current log level.Returns the server name.Returns the server version.Returns the history of tool error events (most recent last).booleanReturns whether this handler has been initialized by a client.processMessage(String message) Processes a single JSON-RPC message and returns the result.voidremovePromptProvider(PromptProvider provider) Removes a prompt provider.voidremoveResourceProvider(ResourceProvider provider) Removes a resource provider.voidremoveToolErrorListener(ToolErrorListener listener) Removes a tool error listener.voidremoveToolProvider(ToolProvider provider) Removes a tool provider.voidsetClientRoots(List<Root> roots) Sets the roots provided by the client.voidsetLogNotifier(Consumer<LogMessage> notifier) Sets a callback for log message notifications.voidsetOnRootsChanged(Consumer<List<Root>> listener) Sets a callback for when client roots change.voidsetResourceUpdateNotifier(Consumer<String> notifier) Sets a callback for resource update notifications.voidSets the handler for sampling/createMessage requests.
-
Constructor Details
-
McpRequestHandler
Creates a new McpRequestHandler.- Parameters:
serverName- the server name reported during initializationserverVersion- the server version reported during initialization
-
-
Method Details
-
processMessage
Processes a single JSON-RPC message and returns the result.- Parameters:
message- the JSON-RPC message string- Returns:
- the result of processing
-
buildNotification
Builds a JSON-RPC notification string.- Parameters:
method- the notification methodparams- optional params (may be null)- Returns:
- the JSON-RPC notification string
-
buildLogNotification
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
Adds a listener for tool execution error events. -
removeToolErrorListener
Removes a tool error listener. -
getToolErrorHistory
Returns the history of tool error events (most recent last). -
clearToolErrorHistory
public void clearToolErrorHistory()Clears the tool error history. -
addToolProvider
Adds a tool provider.- Parameters:
provider- the tool provider to add
-
removeToolProvider
Removes a tool provider.- Parameters:
provider- the tool provider to remove
-
addResourceProvider
Adds a resource provider.- Parameters:
provider- the resource provider to add
-
removeResourceProvider
Removes a resource provider.- Parameters:
provider- the resource provider to remove
-
addPromptProvider
Adds a prompt provider.- Parameters:
provider- the prompt provider to add
-
removePromptProvider
Removes a prompt provider.- Parameters:
provider- the prompt provider to remove
-
setResourceUpdateNotifier
Sets a callback for resource update notifications.- Parameters:
notifier- the callback that receives the updated resource URI
-
setSamplingHandler
Sets the handler for sampling/createMessage requests. The function receives a SamplingRequest and must return a SamplingResponse.- Parameters:
handler- the sampling handler function
-
getLogLevel
Returns the current log level.- Returns:
- the current minimum log level
-
setLogNotifier
Sets a callback for log message notifications.- Parameters:
notifier- the callback that receives log messages
-
emitLogMessage
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
Sets the roots provided by the client.- Parameters:
roots- the list of client roots
-
getClientRoots
Returns the roots provided by the client.- Returns:
- immutable list of client roots
-
setOnRootsChanged
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
Returns the server name.- Returns:
- the server name
-
getServerVersion
Returns the server version.- Returns:
- the server version
-