Skip to main content
Glama
isdaniel

PostgreSQL-Performance-Tuner-Mcp

toolhandler.cpython-310.pyc5.04 kB
o �?%i��@s\dZddlmZddlmZmZddlmZddlm Z ddl m Z m Z Gdd�de�Z d S) zHAbstract base class for tool handlers following the ToolHandler pattern.�)� annotations)�ABC�abstractmethod)�Sequence)�Any)� TextContent�Toolc@sneZdZUdZdZded<dZded<ed dd ��Zed!d d��Z d"dd�Z d#dd�Z d$dd�Z d%dd�Z dS)&� ToolHandlera� Abstract base class for MCP tool handlers. This pattern allows for modular organization of tools where each handler is responsible for defining its schema and executing its logic. Subclasses must implement: - name: The unique tool name - description: Human-readable description - get_tool_definition(): Returns the Tool schema - run_tool(): Executes the tool logic Example: class MyToolHandler(ToolHandler): name = "my_tool" description = "Does something useful" def get_tool_definition(self) -> Tool: return Tool( name=self.name, description=self.description, inputSchema={ "type": "object", "properties": { "param": {"type": "string", "description": "A parameter"} }, "required": ["param"] } ) async def run_tool(self, arguments: dict[str, Any]) -> Sequence[TextContent]: result = do_something(arguments["param"]) return [TextContent(type="text", text=result)] ��str�name� description�returnrcCsdS)z� Return the MCP Tool definition including input schema. Returns: Tool: The tool definition with name, description, and inputSchema N�)�selfrr�R/home/azureuser/mcp_pg_tuning_expert/src/mcp_pg_tuning_expert/tools/toolhandler.py�get_tool_definition3szToolHandler.get_tool_definition� arguments�dict[str, Any]�Sequence[TextContent]c�s�dS)aw Execute the tool logic with the provided arguments. Args: arguments: Dictionary of arguments matching the input schema Returns: Sequence[TextContent]: The tool output as text content Raises: ValueError: If required arguments are missing or invalid Exception: If tool execution fails Nr)rrrrr�run_tool=s�zToolHandler.run_tool�required� list[str]�Nonecs.�fdd�|D�}|rtdd�|�����dS)a  Validate that required arguments are present. Args: arguments: The arguments dictionary to validate required: List of required argument names Raises: ValueError: If any required argument is missing cs$g|]}|�vs�|dur|�qS)Nr)�.0�arg�rrr� <listcomp>]s$z6ToolHandler.validate_required_args.<locals>.<listcomp>zMissing required arguments: z, N)� ValueError�join)rrr�missingrrr�validate_required_argsNs�z"ToolHandler.validate_required_args�error� ExceptioncCstddt|���d�gS)z� Format an error as TextContent for tool response. Args: error: The exception to format Returns: Sequence[TextContent]: Error message as text content �textzError: ��typer$)rr )rr"rrr� format_erroras zToolHandler.format_error�resultcCstd|d�gS)z� Format a string result as TextContent. Args: result: The result string to format Returns: Sequence[TextContent]: Result as text content r$r%)r)rr(rrr� format_resultms zToolHandler.format_result�datarcCs"ddl}td|j|dtd�d�gS)z� Format data as JSON TextContent. Args: data: The data to serialize as JSON Returns: Sequence[TextContent]: JSON-formatted text content rNr$�)�indent�defaultr%)�jsonr�dumpsr )rr*r.rrr�format_json_resultys zToolHandler.format_json_resultN)rr)rrrr)rrrrrr)r"r#rr)r(r rr)r*rrr)�__name__� __module__� __qualname__�__doc__r �__annotations__r rrrr!r'r)r0rrrrr s  #       r N)r4� __future__r�abcrr�collections.abcr�typingr� mcp.typesrrr rrrr�<module>s   

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/isdaniel/pgtuner-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server