from abc import ABC, abstractmethod
from typing import Dict, Any, List, Optional
import logging
logger = logging.getLogger(__name__)
class BaseTool(ABC):
"""Base class for all MCP tools"""
def __init__(self, name: str, description: str):
self.name = name
self.description = description
@abstractmethod
def execute(self, **kwargs) -> Dict[str, Any]:
"""Execute the tool with given parameters"""
pass
def validate_params(self, params: Dict[str, Any], required_params: List[str]) -> None:
"""Validate required parameters"""
missing_params = [param for param in required_params if param not in params]
if missing_params:
raise ValueError(f"Missing required parameters: {', '.join(missing_params)}")
def format_response(self, success: bool, data: Any = None, error: str = None) -> Dict[str, Any]:
"""Format tool response"""
response = {
"tool": self.name,
"success": success,
"timestamp": None # Will be set by server
}
if success:
response["data"] = data
else:
response["error"] = error
return response