Package com.tnsai.mcp.server
Class McpServer
java.lang.Object
com.tnsai.mcp.server.McpServer
MCP Server implementation that exposes tools, resources, and prompts to MCP clients.
This server handles JSON-RPC 2.0 messages over stdio (stdin/stdout) and routes
requests to registered providers via McpRequestHandler.
Features:
- Full MCP protocol support with initialize handshake
- Multiple tool providers support
- Multiple resource providers support
- Multiple prompt providers support
- Sampling support (LLM generation)
- Logging with configurable levels
- Roots exchange
- Notification sending (list_changed events)
- Configurable server info
Example usage:
McpServer server = McpServer.builder()
.serverName("MyMCPServer")
.serverVersion("1.0.0")
.addToolProvider(new MyToolProvider())
.addResourceProvider(new MyResourceProvider())
.build();
server.start(); // Blocks and handles messages from stdin
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for creating McpServer instances. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddPromptProvider(PromptProvider provider) Adds a prompt provider to the server.voidaddResourceProvider(ResourceProvider provider) Adds a resource provider to the server.voidaddToolProvider(ToolProvider provider) Adds a tool provider to the server.static McpServer.Builderbuilder()Creates a new builder for McpServer.voidemitLogMessage(LogMessage logMessage) Emits a log message to the client if it meets the current log level.Returns the current client roots.Returns the current log level.booleanChecks if the server has been initialized by a client.booleanChecks if the server is running.voidprocessMessage(String message) Processes a single JSON-RPC message.voidremovePromptProvider(PromptProvider provider) Removes a prompt provider from the server.voidremoveResourceProvider(ResourceProvider provider) Removes a resource provider from the server.voidremoveToolProvider(ToolProvider provider) Removes a tool provider from the server.voidsendLogNotification(LogMessage logMessage) Sends a log message notification to the client.voidSends a notification that the prompts list has changed.voidSends a notification that the resources list has changed.voidSends a notification that a specific resource has been updated.voidSends a roots/list_changed notification to the client.voidSends a notification that the tools list has changed.voidsetClientRoots(List<Root> roots) Sets the client roots on the server.voidSets the sampling handler for this server.voidstart()Starts the server and begins processing messages from stdin.voidstart(InputStream input, OutputStream output) Starts the server with custom input/output streams.voidstop()Stops the server.
-
Method Details
-
builder
Creates a new builder for McpServer.- Returns:
- a new Builder instance
-
start
public void start()Starts the server and begins processing messages from stdin. This method blocks until the server is stopped or stdin is closed. -
start
Starts the server with custom input/output streams. Useful for testing.- Parameters:
input- the input stream to read messages fromoutput- the output stream to write messages to
-
stop
public void stop()Stops the server. -
isRunning
public boolean isRunning()Checks if the server is running.- Returns:
- true if the server is running
-
isInitialized
public boolean isInitialized()Checks if the server has been initialized by a client.- Returns:
- true if initialized
-
processMessage
Processes a single JSON-RPC message. Exposed for testing purposes.- Parameters:
message- the JSON message string
-
sendToolsListChangedNotification
public void sendToolsListChangedNotification()Sends a notification that the tools list has changed. Call this when tools are added or removed dynamically. -
sendResourcesListChangedNotification
public void sendResourcesListChangedNotification()Sends a notification that the resources list has changed. Call this when resources are added or removed dynamically. -
sendResourceUpdatedNotification
Sends a notification that a specific resource has been updated.- Parameters:
uri- the URI of the updated resource
-
sendPromptsListChangedNotification
public void sendPromptsListChangedNotification()Sends a notification that the prompts list has changed. Call this when prompts are added or removed dynamically. -
sendLogNotification
Sends a log message notification to the client. The message is only sent if it meets the current log level threshold.- Parameters:
logMessage- the log message to send
-
sendRootsListChangedNotification
public void sendRootsListChangedNotification()Sends a roots/list_changed notification to the client. -
addToolProvider
Adds a tool provider to the server.- Parameters:
provider- the tool provider to add
-
removeToolProvider
Removes a tool provider from the server.- Parameters:
provider- the tool provider to remove
-
addResourceProvider
Adds a resource provider to the server.- Parameters:
provider- the resource provider to add
-
removeResourceProvider
Removes a resource provider from the server.- Parameters:
provider- the resource provider to remove
-
addPromptProvider
Adds a prompt provider to the server.- Parameters:
provider- the prompt provider to add
-
removePromptProvider
Removes a prompt provider from the server.- Parameters:
provider- the prompt provider to remove
-
setSamplingHandler
Sets the sampling handler for this server.- Parameters:
handler- the function that processes sampling requests
-
setClientRoots
Sets the client roots on the server.- Parameters:
roots- the list of client roots
-
getClientRoots
Returns the current client roots.- Returns:
- the list of client roots
-
getLogLevel
Returns the current log level.- Returns:
- the log level
-
emitLogMessage
Emits a log message to the client if it meets the current log level.- Parameters:
logMessage- the log message
-