Class McpServerComponentRegister
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:
McpResource- Server resources that can be accessed by clientsMcpPrompt- Interactive prompts for user inputMcpTool- Executable tools that perform specific operationsMcpPromptCompletion- Auto-completion for promptsMcpResourceCompletion- Auto-completion for resources
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
ConstructorsConstructorDescriptionCreates a new instance ofMcpServerComponentRegister. -
Method Summary
Modifier and TypeMethodDescriptionList<io.modelcontextprotocol.server.McpServerFeatures.SyncCompletionSpecification>Registers all completion specifications for prompts and resources.voidregisterComponents(io.modelcontextprotocol.server.McpSyncServer server) Registers all MCP server components (resources, prompts, and tools) with the specified server.
-
Constructor Details
-
McpServerComponentRegister
public McpServerComponentRegister()Creates a new instance ofMcpServerComponentRegister.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
DependencyInjectorProviderand 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
McpPromptCompletionandMcpResourceCompletionannotations, 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
McpServerCompletioncomponent.- 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, andMcpToolannotations, 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:
- Resources are handled by
McpServerResource - Prompts are handled by
McpServerPrompt - Tools are handled by
McpServerTool
- Parameters:
server- the MCP server to register components with- Throws:
IllegalArgumentException- if server is nullRuntimeException- if component registration fails
- Resources are handled by
-