base_tool.py•1.93 kB
"""Abstract base class for all MCP tools."""
from abc import ABC, abstractmethod
from typing import Any, Dict, Optional
class BaseTool(ABC):
"""
Abstract base class for all MCP tools.
All tools must inherit from this class and implement required methods.
"""
@abstractmethod
def get_name(self) -> str:
"""
Return the unique name of this tool.
Returns:
Tool name
"""
pass
@abstractmethod
async def execute(self, params: Dict[str, Any]) -> Dict[str, Any]:
"""
Execute the tool with given parameters.
Args:
params: Validated input parameters
Returns:
Tool execution result
"""
pass
def validate_input(self, params: Dict[str, Any]) -> Dict[str, Any]:
"""
Validate input parameters against the tool's schema.
Override for custom validation logic.
Args:
params: Input parameters
Returns:
Validated parameters
"""
# Default implementation - subclasses can override
return params
def get_cache_key(self, params: Dict[str, Any]) -> Optional[str]:
"""
Generate cache key for this tool execution.
Return None to disable caching for this execution.
Args:
params: Input parameters
Returns:
Cache key or None
"""
return None
def get_cache_ttl(self) -> int:
"""
Get cache TTL for this tool.
Returns:
TTL in seconds
"""
return 3600 # Default 1 hour
def should_retry(self, error: Exception) -> bool:
"""
Determine if the operation should be retried on this error.
Args:
error: Exception that occurred
Returns:
True if should retry, False otherwise
"""
return False