tool_operations.py•2.29 kB
"""Tool operations for MCP testing."""
from typing import Any
READ_OPERATION_KEYWORDS = ["find", "get", "list"]
MINIMUM_EXPECTED_TOOLS = 5
def find_read_operation_tool(tools: list[dict[str, Any]]) -> dict[str, Any] | None:
"""Find a tool that performs a read operation.
Args:
tools: List of available tools
Returns:
First tool matching read operation criteria, or None
"""
for tool in tools:
name = tool.get("name", "").lower()
if any(keyword in name for keyword in READ_OPERATION_KEYWORDS):
return tool
return None
def build_tool_arguments(tool: dict[str, Any]) -> dict[str, Any]:
"""Build minimal required arguments for a tool.
Args:
tool: Tool definition with input schema
Returns:
Dictionary of argument name to value
"""
input_schema = tool.get("inputSchema", {})
properties = input_schema.get("properties", {})
required_params = input_schema.get("required", [])
arguments = {}
for param_name in required_params:
if param_name not in properties:
continue
param_value = _create_param_value(properties[param_name])
arguments[param_name] = param_value
return arguments
def _create_param_value(param_schema: dict[str, Any]) -> Any:
"""Create a default value for a parameter based on its schema.
Args:
param_schema: JSON schema for the parameter
Returns:
Default value appropriate for the parameter type
"""
param_type = param_schema.get("type", "string")
if param_type == "string":
return _create_string_value(param_schema)
elif param_type == "integer":
return 1
elif param_type == "boolean":
return True
elif param_type == "array":
return []
return None
def _create_string_value(param_schema: dict[str, Any]) -> str:
"""Create a string value, using enum if available."""
if "enum" in param_schema:
return param_schema["enum"][0]
return "test"
def extract_tool_names(tools: list[dict[str, Any]]) -> list[str]:
"""Extract tool names from tools list.
Args:
tools: List of tool definitions
Returns:
List of tool names
"""
return [tool["name"] for tool in tools]