Class McpServerBase

java.lang.Object
com.github.thought2code.mcp.annotated.server.McpServerBase
All Implemented Interfaces:
McpServer
Direct Known Subclasses:
McpSseServer, McpStdioServer, McpStreamableServer

public abstract class McpServerBase extends Object implements McpServer
Abstract base class that provides a common implementation for MCP (Model Context Protocol) servers.

This class implements the core functionality shared across different MCP server implementations, including capability definition, component registration, and server creation. It serves as a foundation for concrete server implementations that handle different transport mechanisms (STDIO, SSE, STREAMABLE).

The class manages server configuration and provides default implementations for:

  • Defining server capabilities based on configuration
  • Registering MCP components (resources, prompts, tools)
  • Creating a configured synchronous server instance

Concrete implementations need only provide the specific synchronization specification for their transport mechanism by implementing the McpServer.createSyncSpecification() method.

Author:
codeboyzhou
See Also:
  • Field Details

    • configuration

      protected final McpServerConfiguration configuration
      The server configuration used by this MCP server.
  • Constructor Details

    • McpServerBase

      public McpServerBase(@NotNull @NotNull McpServerConfiguration configuration)
      Constructs a new McpServerBase with the specified configuration.
      Parameters:
      configuration - the server configuration containing all settings for the MCP server, including capabilities, timeouts, and transport settings
  • Method Details

    • defineCapabilities

      public io.modelcontextprotocol.spec.McpSchema.ServerCapabilities defineCapabilities()
      Defines and returns the server capabilities based on the configuration.

      This method reads the capability settings from the server configuration and constructs a McpSchema.ServerCapabilities object that specifies which features are enabled (resources, prompts, tools, completions) and their respective change notification settings.

      Specified by:
      defineCapabilities in interface McpServer
      Returns:
      a configured ServerCapabilities object reflecting the server's supported features
      See Also:
    • registerComponents

      public void registerComponents(io.modelcontextprotocol.server.McpSyncServer mcpSyncServer)
      Registers all MCP server components with the specified synchronous server.

      This method creates and registers the three main types of MCP components: resources, prompts, and tools. Each component type is handled by its respective registration class which scans for annotated methods and registers them with the server.

      Specified by:
      registerComponents in interface McpServer
      Parameters:
      mcpSyncServer - the synchronous server instance to register components with
      See Also:
    • createSyncServer

      public io.modelcontextprotocol.server.McpSyncServer createSyncServer()
      Creates and returns a fully configured MCP synchronous server instance.

      This method builds a synchronous server by combining:

      • The server capabilities defined by defineCapabilities()
      • All available completion specifications from McpServerCompletion.all()
      • Server information (name, version) from the configuration
      • Instructions and request timeout from the configuration

      The method uses the synchronization specification provided by the concrete implementation through McpServer.createSyncSpecification().

      Specified by:
      createSyncServer in interface McpServer
      Returns:
      a fully configured MCP synchronous server ready to start
      See Also: