"""
Text Processing Prompts Module
Provides text-related prompts such as summarization requests.
This module demonstrates migrating the existing prompt to the new structure.
"""
from fastmcp import FastMCP
from .base import TextPrompt
class TextProcessingPrompts(TextPrompt):
"""
Collection of text processing prompts.
Provides summarization and other text processing prompts that were originally
in the main.py file.
"""
def __init__(self):
super().__init__(name="text_processing")
# Set default template for summarization
self.template = "Please summarize the following text:\n\n{text}"
def register_with_mcp(self, mcp: FastMCP) -> None:
"""
Register text processing prompts with the FastMCP instance.
Args:
mcp: The FastMCP instance to register with
"""
# Register the summarize request prompt
@mcp.prompt
def summarize_request(text: str) -> str:
"""Generate a prompt asking for a summary."""
if not self.validate_text(text):
return "Please provide valid text to summarize."
self._log_prompt_call("summarize_request", text=text)
return self.format_text(text)
# Register additional text processing prompts
@mcp.prompt
def analyze_text(text: str, analysis_type: str = "general") -> str:
"""Generate a prompt asking for text analysis."""
if not self.validate_text(text):
return "Please provide valid text to analyze."
self._log_prompt_call("analyze_text", text=text, analysis_type=analysis_type)
analysis_templates = {
"general": "Please analyze the following text:\n\n{text}",
"sentiment": "Please analyze the sentiment of the following text:\n\n{text}",
"keywords": "Please extract keywords from the following text:\n\n{text}",
"structure": "Please analyze the structure and organization of the following text:\n\n{text}"
}
template = analysis_templates.get(analysis_type, analysis_templates["general"])
return template.format(text=text)
self.logger.info("Registered text processing prompts: summarize_request, analyze_text")
def create_summary_prompt(self, text: str) -> str:
"""
Direct method for creating summary prompts (can be used internally).
Args:
text: Text to create summary prompt for
Returns:
Formatted summary prompt
"""
if not self.validate_text(text):
raise ValueError("Invalid text provided")
return self.format_text(text)
def create_custom_prompt(self, text: str, instruction: str) -> str:
"""
Create a custom text processing prompt.
Args:
text: The text to process
instruction: Custom instruction for processing
Returns:
Formatted custom prompt
"""
if not self.validate_text(text):
raise ValueError("Invalid text provided")
if not instruction.strip():
raise ValueError("Instruction cannot be empty")
return f"{instruction}\n\n{text}"