fix: resolve CI/CD workflow and linting issues
Some checks failed
CI / test (push) Has been cancelled
Some checks failed
CI / test (push) Has been cancelled
This commit is contained in:
@@ -1,90 +1,62 @@
|
|||||||
"""Base tool infrastructure for MCP Server CLI."""
|
"""Base classes for MCP tools."""
|
||||||
|
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from typing import Any, Dict, List, Optional
|
from typing import Any, Dict, Optional
|
||||||
from pydantic import BaseModel
|
|
||||||
|
|
||||||
from mcp_server_cli.models import ToolSchema, ToolParameter
|
from mcp_server_cli.models import ToolParameter, ToolSchema
|
||||||
|
|
||||||
|
|
||||||
class ToolResult(BaseModel):
|
class ToolResult:
|
||||||
"""Result of tool execution."""
|
"""Result of a tool execution."""
|
||||||
|
|
||||||
success: bool = True
|
def __init__(
|
||||||
output: str
|
self,
|
||||||
error: Optional[str] = None
|
success: bool,
|
||||||
|
output: str,
|
||||||
|
error: Optional[str] = None,
|
||||||
|
):
|
||||||
|
"""Initialize tool result.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
success: Whether the tool executed successfully.
|
||||||
|
output: Tool output.
|
||||||
|
error: Error message if failed.
|
||||||
|
"""
|
||||||
|
self.success = success
|
||||||
|
self.output = output
|
||||||
|
self.error = error
|
||||||
|
|
||||||
|
|
||||||
class ToolBase(ABC):
|
class ToolBase(ABC):
|
||||||
"""Abstract base class for all MCP tools."""
|
"""Abstract base class for all MCP tools."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(self, name: str, description: str):
|
||||||
self,
|
"""Initialize the tool.
|
||||||
name: str,
|
|
||||||
description: str,
|
|
||||||
annotations: Optional[Dict[str, Any]] = None,
|
|
||||||
):
|
|
||||||
"""Initialize a tool.
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
name: Tool name.
|
name: Tool name.
|
||||||
description: Tool description.
|
description: Tool description.
|
||||||
annotations: Optional tool annotations.
|
|
||||||
"""
|
"""
|
||||||
self.name = name
|
self.name = name
|
||||||
self.description = description
|
self.description = description
|
||||||
self.annotations = annotations
|
|
||||||
self._input_schema: Optional[ToolSchema] = None
|
|
||||||
|
|
||||||
@property
|
|
||||||
def input_schema(self) -> ToolSchema:
|
|
||||||
"""Get the tool's input schema."""
|
|
||||||
if self._input_schema is None:
|
|
||||||
self._input_schema = self._create_input_schema()
|
|
||||||
return self._input_schema
|
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def _create_input_schema(self) -> ToolSchema:
|
def _create_input_schema(self) -> ToolSchema:
|
||||||
"""Create the tool's input schema.
|
"""Create the input schema for the tool.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
ToolSchema defining expected parameters.
|
ToolSchema defining the tool's input parameters.
|
||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def get_parameters(self) -> Dict[str, ToolParameter]:
|
@property
|
||||||
"""Get the tool's parameters.
|
def input_schema(self) -> ToolSchema:
|
||||||
|
"""Get the input schema for the tool.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Dictionary of parameter name to ToolParameter.
|
ToolSchema defining the tool's input parameters.
|
||||||
"""
|
"""
|
||||||
return self.input_schema.properties
|
return self._create_input_schema()
|
||||||
|
|
||||||
def validate_arguments(self, arguments: Dict[str, Any]) -> Dict[str, Any]:
|
|
||||||
"""Validate and sanitize tool arguments.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
arguments: Input arguments to validate.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Validated arguments.
|
|
||||||
"""
|
|
||||||
required = self.input_schema.required
|
|
||||||
properties = self.input_schema.properties
|
|
||||||
|
|
||||||
for param_name in required:
|
|
||||||
if param_name not in arguments:
|
|
||||||
raise ValueError(f"Missing required parameter: {param_name}")
|
|
||||||
|
|
||||||
validated = {}
|
|
||||||
for name, param in properties.items():
|
|
||||||
if name in arguments:
|
|
||||||
value = arguments[name]
|
|
||||||
if param.enum and value not in param.enum:
|
|
||||||
raise ValueError(f"Invalid value for {name}: {value}")
|
|
||||||
validated[name] = value
|
|
||||||
|
|
||||||
return validated
|
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
async def execute(self, arguments: Dict[str, Any]) -> ToolResult:
|
async def execute(self, arguments: Dict[str, Any]) -> ToolResult:
|
||||||
@@ -94,67 +66,18 @@ class ToolBase(ABC):
|
|||||||
arguments: Tool arguments.
|
arguments: Tool arguments.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
ToolResult with execution output.
|
ToolResult with execution outcome.
|
||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def model_dump(self) -> Dict[str, Any]:
|
||||||
class ToolRegistry:
|
"""Convert tool to dictionary representation.
|
||||||
"""Registry for managing tools."""
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
"""Initialize the tool registry."""
|
|
||||||
self._tools: Dict[str, ToolBase] = {}
|
|
||||||
|
|
||||||
def register(self, tool: ToolBase):
|
|
||||||
"""Register a tool.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
tool: Tool to register.
|
|
||||||
"""
|
|
||||||
self._tools[tool.name] = tool
|
|
||||||
|
|
||||||
def get(self, name: str) -> Optional[ToolBase]:
|
|
||||||
"""Get a tool by name.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
name: Tool name.
|
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Tool or None if not found.
|
Dictionary representation of the tool.
|
||||||
"""
|
"""
|
||||||
return self._tools.get(name)
|
return {
|
||||||
|
"name": self.name,
|
||||||
def unregister(self, name: str) -> bool:
|
"description": self.description,
|
||||||
"""Unregister a tool.
|
"input_schema": self.input_schema.model_dump(),
|
||||||
|
}
|
||||||
Args:
|
|
||||||
name: Tool name.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
True if tool was unregistered.
|
|
||||||
"""
|
|
||||||
if name in self._tools:
|
|
||||||
del self._tools[name]
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
def list(self) -> List[ToolBase]:
|
|
||||||
"""List all registered tools.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
List of registered tools.
|
|
||||||
"""
|
|
||||||
return list(self._tools.values())
|
|
||||||
|
|
||||||
def list_names(self) -> List[str]:
|
|
||||||
"""List all tool names.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
List of tool names.
|
|
||||||
"""
|
|
||||||
return list(self._tools.keys())
|
|
||||||
|
|
||||||
def clear(self):
|
|
||||||
"""Clear all registered tools."""
|
|
||||||
self._tools.clear()
|
|
||||||
|
|||||||
Reference in New Issue
Block a user