Class McpServers
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 Summary
Modifier and TypeMethodDescriptionstatic McpServersInitializes the McpServers singleton with the specified main class.voidStarts an MCP server using default configuration.voidstartServer(String configFileName) Starts an MCP server using configuration from the specified file.voidstartSseServer(McpServerConfiguration.Builder configuration) Starts an MCP server in SSE mode with the specified configuration.voidstartStdioServer(McpServerConfiguration.Builder configuration) Starts an MCP server in STDIO mode with the specified configuration.voidstartStreamableServer(McpServerConfiguration.Builder configuration) Starts an MCP server in STREAMABLE mode with the specified configuration.
-
Method Details
-
run
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
McpI18nEnabledannotation for i18n configuration.- Parameters:
mainClass- the main application class used for initializationargs- 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
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
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
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
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 nullMcpServerException- 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:
-