Skip to main content
Glama
rwxproject
by rwxproject
base.py3.36 kB
""" Base Classes for MCP Prompts Provides abstract base classes and common patterns for implementing MCP prompts with consistent interfaces and logging. """ import logging from abc import ABC, abstractmethod from typing import Any, Dict, Optional from fastmcp import FastMCP from ..config.logging import get_logger logger = get_logger(__name__) class BasePrompt(ABC): """ Abstract base class for all MCP prompts. Provides common functionality and enforces consistent interface for prompt registration and logging. """ def __init__(self, name: Optional[str] = None): """ Initialize the prompt. Args: name: Optional prompt name override """ self.name = name or self.__class__.__name__.lower().replace('prompts', '') self.logger = get_logger(f"prompts.{self.name}") @abstractmethod def register_with_mcp(self, mcp: FastMCP) -> None: """ Register this prompt's methods with the FastMCP instance. Args: mcp: The FastMCP instance to register with """ pass def _log_prompt_call(self, method_name: str, **kwargs) -> None: """ Log a prompt method call with consistent formatting. Args: method_name: Name of the prompt method being called **kwargs: Arguments passed to the method """ # Filter out sensitive information and truncate long text safe_kwargs = {} for k, v in kwargs.items(): if k.lower().startswith(('password', 'token', 'secret')): continue # Truncate long text for logging if isinstance(v, str) and len(v) > 100: safe_kwargs[k] = v[:97] + "..." else: safe_kwargs[k] = v self.logger.info(f">>> 📝 Prompt '{method_name}' called with: {safe_kwargs}") def get_info(self) -> Dict[str, Any]: """ Get information about this prompt. Returns: Dictionary containing prompt metadata """ return { "name": self.name, "class": self.__class__.__name__, "module": self.__class__.__module__ } class TextPrompt(BasePrompt): """ Base class for text-based prompts. Provides common functionality for text processing and generation. """ def __init__(self, name: Optional[str] = None, template: Optional[str] = None): super().__init__(name) self.template = template def format_text(self, text: str, **kwargs) -> str: """ Format text using the prompt template. Args: text: The input text **kwargs: Additional formatting parameters Returns: Formatted text """ if self.template: return self.template.format(text=text, **kwargs) return text def validate_text(self, text: str) -> bool: """ Validate input text. Args: text: Text to validate Returns: True if valid, False otherwise """ if not text or not isinstance(text, str): return False return len(text.strip()) > 0

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/rwxproject/mcp-server-template'

If you have feedback or need assistance with the MCP directory API, please join our Discord server