Class McpServers

java.lang.Object
com.github.thought2code.mcp.annotated.McpServers

public final class McpServers extends Object
Singleton class that provides methods to start and manage MCP (Model Context Protocol) servers.

This class serves as the main entry point for starting MCP servers in different modes. It follows the singleton design pattern and provides convenient methods for starting servers in STDIO, SSE, or STREAMABLE modes, either programmatically or through configuration files.

The class supports three server modes:

  • STDIO - Standard input/output communication (default for CLI tools)
  • SSE - Server-Sent Events for HTTP-based real-time communication
  • STREAMABLE - HTTP streaming for web applications

Before starting a server, the class must be initialized by calling run(Class, String[]) with the main application class. This initializes reflection scanning and resource bundles for i18n support.

Author:
codeboyzhou
See Also:
  • Method Details

    • run

      public static McpServers run(Class<?> mainClass, String[] args)
      Initializes the McpServers singleton with the specified main class.

      This method must be called before starting any MCP server. It performs necessary initialization tasks including:

      • Initializing reflection scanning for annotated components
      • Loading resource bundles for internationalization support

      The main class is used to determine the base package for reflection scanning and to check for the McpI18nEnabled annotation for i18n configuration.

      Parameters:
      mainClass - the main application class used for initialization
      args - command line arguments (currently not used but reserved for future use)
      Returns:
      the singleton instance of McpServers
      Throws:
      NullPointerException - if mainClass is null
      See Also:
    • startStdioServer

      public void startStdioServer(McpServerConfiguration.Builder configuration)
      Starts an MCP server in STDIO mode with the specified configuration.

      This method configures and starts a server that communicates via standard input/output. The server is enabled and the mode is set to STDIO before starting.

      STDIO mode is typically used for command-line tools and applications that communicate through standard streams.

      Parameters:
      configuration - the builder containing server configuration settings
      Throws:
      NullPointerException - if configuration is null
      See Also:
    • startSseServer

      public void startSseServer(McpServerConfiguration.Builder configuration)
      Starts an MCP server in SSE mode with the specified configuration.

      This method configures and starts a server that uses Server-Sent Events for HTTP-based real-time communication. The server is enabled and the mode is set to SSE before starting.

      SSE mode is suitable for web applications requiring real-time updates and bidirectional communication.

      Parameters:
      configuration - the builder containing server configuration settings
      Throws:
      NullPointerException - if configuration is null
      See Also:
    • startStreamableServer

      public void startStreamableServer(McpServerConfiguration.Builder configuration)
      Starts an MCP server in STREAMABLE mode with the specified configuration.

      This method configures and starts a server that uses HTTP streaming for communication. The server is enabled and the mode is set to STREAMABLE before starting.

      STREAMABLE mode is designed for web applications that require HTTP-based streaming communication.

      Parameters:
      configuration - the builder containing server configuration settings
      Throws:
      NullPointerException - if configuration is null
      See Also:
    • startServer

      public void startServer(String configFileName)
      Starts an MCP server using configuration from the specified file.

      This method loads server configuration from a JSON or YAML file and starts the server according to the loaded configuration. The configuration file must contain valid server settings including mode, port, and other parameters.

      Parameters:
      configFileName - the path to the configuration file
      Throws:
      IllegalArgumentException - if configFileName is null
      McpServerException - if the configuration file cannot be loaded or is invalid
      See Also:
    • startServer

      public void startServer()
      Starts an MCP server using default configuration.

      This method loads server configuration from the default location (typically a configuration file in the classpath or working directory) and starts the server according to the loaded configuration.

      Throws:
      McpServerException - if the default configuration cannot be loaded or is invalid
      See Also: