Files
2025-09-11 23:13:58 +01:00

188 lines
6.6 KiB
Python

"""
Text Processing Tools for MCP
"""
import re
from typing import List
from ..core.types import MCPTool
class TextTools:
"""Collection of text processing tools"""
@staticmethod
def get_tools() -> List[MCPTool]:
"""Get all text tools"""
return [
TextTools._create_word_count_tool(),
TextTools._create_text_search_tool(),
TextTools._create_text_replace_tool(),
TextTools._create_text_uppercase_tool(),
TextTools._create_text_lowercase_tool(),
TextTools._create_text_length_tool(),
TextTools._create_generate_greeting_tool(),
]
@staticmethod
def _create_word_count_tool() -> MCPTool:
"""Create word count tool"""
async def count_words(text: str) -> int:
"""Count the number of words in a text"""
words = text.strip().split()
return len(words)
return MCPTool(
name="count_words",
description="Count the number of words in a text",
input_schema={
"type": "object",
"properties": {
"text": {"type": "string", "description": "Text to count words in"},
},
"required": ["text"],
},
handler=count_words,
)
@staticmethod
def _create_text_search_tool() -> MCPTool:
"""Create text search tool"""
async def search_text(text: str, pattern: str, case_sensitive: bool = False) -> List[str]:
"""Search for a pattern in text and return all matches"""
flags = 0 if case_sensitive else re.IGNORECASE
matches = re.findall(pattern, text, flags)
return matches
return MCPTool(
name="search_text",
description="Search for a pattern in text and return all matches",
input_schema={
"type": "object",
"properties": {
"text": {"type": "string", "description": "Text to search in"},
"pattern": {"type": "string", "description": "Regular expression pattern to search for"},
"case_sensitive": {"type": "boolean", "description": "Whether search should be case sensitive", "default": False},
},
"required": ["text", "pattern"],
},
handler=search_text,
)
@staticmethod
def _create_text_replace_tool() -> MCPTool:
"""Create text replace tool"""
async def replace_text(text: str, old_pattern: str, new_text: str) -> str:
"""Replace all occurrences of a pattern in text"""
return text.replace(old_pattern, new_text)
return MCPTool(
name="replace_text",
description="Replace all occurrences of a pattern in text",
input_schema={
"type": "object",
"properties": {
"text": {"type": "string", "description": "Original text"},
"old_pattern": {"type": "string", "description": "Text to replace"},
"new_text": {"type": "string", "description": "Replacement text"},
},
"required": ["text", "old_pattern", "new_text"],
},
handler=replace_text,
)
@staticmethod
def _create_text_uppercase_tool() -> MCPTool:
"""Create uppercase tool"""
async def to_uppercase(text: str) -> str:
"""Convert text to uppercase"""
return text.upper()
return MCPTool(
name="to_uppercase",
description="Convert text to uppercase",
input_schema={
"type": "object",
"properties": {
"text": {"type": "string", "description": "Text to convert to uppercase"},
},
"required": ["text"],
},
handler=to_uppercase,
)
@staticmethod
def _create_text_lowercase_tool() -> MCPTool:
"""Create lowercase tool"""
async def to_lowercase(text: str) -> str:
"""Convert text to lowercase"""
return text.lower()
return MCPTool(
name="to_lowercase",
description="Convert text to lowercase",
input_schema={
"type": "object",
"properties": {
"text": {"type": "string", "description": "Text to convert to lowercase"},
},
"required": ["text"],
},
handler=to_lowercase,
)
@staticmethod
def _create_text_length_tool() -> MCPTool:
"""Create text length tool"""
async def text_length(text: str) -> int:
"""Get the length of text"""
return len(text)
return MCPTool(
name="text_length",
description="Get the length of text (number of characters)",
input_schema={
"type": "object",
"properties": {
"text": {"type": "string", "description": "Text to measure length of"},
},
"required": ["text"],
},
handler=text_length,
)
@staticmethod
def _create_generate_greeting_tool() -> MCPTool:
"""Create greeting generation tool"""
async def generate_greeting(name: str, style: str = "casual") -> str:
"""Generate a personalized greeting"""
name = name.strip()
if not name:
raise ValueError("Name cannot be empty")
if style == "casual":
return f"Hey {name}! Welcome! 👋"
elif style == "formal":
return f"Good day, {name}. Welcome to our platform."
elif style == "professional":
return f"Hello, {name}. Thank you for joining us."
else:
return f"Hi {name}! Welcome!"
return MCPTool(
name="generate_greeting",
description="Generate a personalized greeting with different styles",
input_schema={
"type": "object",
"properties": {
"name": {"type": "string", "description": "Person's name"},
"style": {
"type": "string",
"enum": ["casual", "formal", "professional"],
"description": "Greeting style",
"default": "casual"
},
},
"required": ["name"],
},
handler=generate_greeting,
)