Class McpServerComponentRegister

java.lang.Object
com.github.thought2code.mcp.annotated.server.component.McpServerComponentRegister

public final class McpServerComponentRegister extends Object
Central registry for MCP server components including resources, prompts, tools, and completions.

This class is responsible for discovering, creating, and registering all MCP server components with the appropriate server instances. It uses reflection to scan for methods annotated with MCP annotations and creates the corresponding server components using dependency injection.

The register supports the following component types:

This class is designed to be used once during server initialization to register all available components. It leverages the dependency injection system to obtain necessary dependencies and create component instances.

Author:
codeboyzhou
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new instance of McpServerComponentRegister.
  • Method Summary

    Modifier and Type
    Method
    Description
    List<io.modelcontextprotocol.server.McpServerFeatures.SyncCompletionSpecification>
    Registers all completion specifications for prompts and resources.
    void
    registerComponents(io.modelcontextprotocol.server.McpSyncServer server)
    Registers all MCP server components (resources, prompts, and tools) with the specified server.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • McpServerComponentRegister

      public McpServerComponentRegister()
      Creates a new instance of McpServerComponentRegister.

      This constructor initializes the dependency injector and reflections instance that will be used for component discovery and creation. The dependency injector is obtained from the DependencyInjectorProvider and is used to create component instances and obtain the reflections scanner.

  • Method Details

    • registerCompletions

      public List<io.modelcontextprotocol.server.McpServerFeatures.SyncCompletionSpecification> registerCompletions()
      Registers all completion specifications for prompts and resources.

      This method scans for methods annotated with McpPromptCompletion and McpResourceCompletion annotations, creates the corresponding completion specifications, and returns them as a list. The returned specifications can be used to configure the server's completion capabilities.

      The method uses reflection to discover all annotated methods and creates completion specifications using the McpServerCompletion component.

      Returns:
      a list of synchronous completion specifications for all discovered prompt and resource completion methods
      Throws:
      RuntimeException - if component creation fails for any method
    • registerComponents

      public void registerComponents(io.modelcontextprotocol.server.McpSyncServer server)
      Registers all MCP server components (resources, prompts, and tools) with the specified server.

      This method is the main entry point for component registration. It scans for methods annotated with McpResource, McpPrompt, and McpTool annotations, creates the corresponding server components, and registers them with the provided MCP server instance.

      Each component type is processed by its respective server component class:

      Parameters:
      server - the MCP server to register components with
      Throws:
      IllegalArgumentException - if server is null
      RuntimeException - if component registration fails