"""
Prompt Registration System
Provides centralized registration and management of MCP prompts using
a registry pattern for dynamic component discovery.
"""
import logging
from typing import List, Type, Dict, Any
from fastmcp import FastMCP
from .base import BasePrompt
from .text_processing import TextProcessingPrompts
from ..config.logging import get_logger
logger = get_logger(__name__)
class PromptRegistry:
"""
Central registry for managing MCP prompts.
Provides functionality to register prompt classes and automatically
register all prompts with a FastMCP instance.
"""
def __init__(self):
"""Initialize the prompt registry."""
self._prompt_classes: List[Type[BasePrompt]] = []
self._registered_prompts: Dict[str, BasePrompt] = {}
def register(self, prompt_class: Type[BasePrompt]) -> None:
"""
Register a prompt class with the registry.
Args:
prompt_class: The prompt class to register
"""
if not issubclass(prompt_class, BasePrompt):
raise TypeError(f"Prompt class {prompt_class.__name__} must inherit from BasePrompt")
self._prompt_classes.append(prompt_class)
logger.info(f"Registered prompt class: {prompt_class.__name__}")
def register_all(self, mcp: FastMCP) -> None:
"""
Register all prompts with the FastMCP instance.
Args:
mcp: The FastMCP instance to register prompts with
"""
logger.info(f"Registering {len(self._prompt_classes)} prompt classes")
for prompt_class in self._prompt_classes:
try:
# Instantiate the prompt
prompt_instance = prompt_class()
# Register with MCP
prompt_instance.register_with_mcp(mcp)
# Store the instance for potential future use
self._registered_prompts[prompt_instance.name] = prompt_instance
logger.info(f"Successfully registered prompt: {prompt_instance.name}")
except Exception as e:
logger.error(f"Failed to register prompt {prompt_class.__name__}: {e}")
# Continue with other prompts even if one fails
def get_registered_prompts(self) -> Dict[str, BasePrompt]:
"""
Get all registered prompt instances.
Returns:
Dictionary mapping prompt names to instances
"""
return self._registered_prompts.copy()
def get_prompt_info(self) -> List[Dict[str, Any]]:
"""
Get information about all registered prompts.
Returns:
List of dictionaries containing prompt metadata
"""
return [prompt.get_info() for prompt in self._registered_prompts.values()]
# Global registry instance
registry = PromptRegistry()
# Register built-in prompts
registry.register(TextProcessingPrompts)
def register_all_prompts(mcp: FastMCP) -> None:
"""
Register all prompts with the MCP server.
This is the main entry point for prompt registration that should be
called from the server initialization.
Args:
mcp: The FastMCP instance to register prompts with
"""
registry.register_all(mcp)
def get_prompt_registry() -> PromptRegistry:
"""
Get the global prompt registry instance.
Returns:
The global prompt registry
"""
return registry
def register_prompt_class(prompt_class: Type[BasePrompt]) -> None:
"""
Convenience function to register a new prompt class.
Args:
prompt_class: The prompt class to register
"""
registry.register(prompt_class)