Class McpServer

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

public class McpServer extends Object
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
 
  • Method Details

    • builder

      public static McpServer.Builder 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

      public void start(InputStream input, OutputStream output)
      Starts the server with custom input/output streams. Useful for testing.
      Parameters:
      input - the input stream to read messages from
      output - 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

      public void processMessage(String message)
      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

      public void sendResourceUpdatedNotification(String uri)
      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

      public void sendLogNotification(LogMessage logMessage)
      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

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

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

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

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

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

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

      public void setSamplingHandler(Function<SamplingRequest,SamplingResponse> handler)
      Sets the sampling handler for this server.
      Parameters:
      handler - the function that processes sampling requests
    • setClientRoots

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

      public List<Root> getClientRoots()
      Returns the current client roots.
      Returns:
      the list of client roots
    • getLogLevel

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

      public void emitLogMessage(LogMessage logMessage)
      Emits a log message to the client if it meets the current log level.
      Parameters:
      logMessage - the log message