arcade-mcp/libs/arcade-evals/arcade_evals/_evalsuite/_openai_schema.py
jottakka 98fad93d21
Adding MCP Servers supports to Arcade Evals (#689)
# MCP Server Tool Evaluation Support

## Overview
Add support for evaluating tools from remote MCP servers without
requiring Python callables. Enables direct evaluation of any
MCP-compatible tool server.

## What's New

### Core Features
- **`MCPToolRegistry`**: Evaluate tools from a single MCP server
- **`CompositeMCPRegistry`**: Evaluate tools from multiple MCP servers
simultaneously
- **Automatic loaders**: `load_from_stdio()` and `load_from_http()` to
fetch tools from running servers
- **Automatic namespacing**: Tools prefixed with server name (e.g.,
`server_tool_name`)
- **Smart name resolution**: Use short names if unique, full names if
ambiguous
- **OpenAI strict mode**: Automatic schema conversion prevents parameter
hallucinations

### Usage

**Automatic Loading:**
```python
from arcade_evals import load_from_stdio, MCPToolRegistry

# Load tools automatically from MCP server
tools = load_from_stdio(["npx", "-y", "@modelcontextprotocol/server-github"])
registry = MCPToolRegistry(tools)
```

**Single MCP Server:**
```python
from arcade_evals import MCPToolRegistry, ExpectedToolCall

registry = MCPToolRegistry(mcp_tools)
suite = EvalSuite(catalog=registry)

suite.add_case(
    expected_tool_calls=[
        ExpectedToolCall(tool_name="tool_name", args={...})
    ]
)
```

**Multiple MCP Servers:**
```python
from arcade_evals import CompositeMCPRegistry, load_from_stdio

# Load from multiple servers
github_tools = load_from_stdio(["npx", "-y", "@modelcontextprotocol/server-github"])
slack_tools = load_from_stdio(["npx", "-y", "@modelcontextprotocol/server-slack"])

composite = CompositeMCPRegistry(
    tool_lists={
        "github": github_tools,
        "slack": slack_tools,
    }
)

suite = EvalSuite(catalog=composite)

suite.add_case(
    expected_tool_calls=[
        ExpectedToolCall(tool_name="github_list_issues", args={...})
    ]
)
```

## Implementation

### Files Changed
- **`libs/arcade-evals/arcade_evals/registry.py`** (NEW): Registry
abstractions and implementations
- **`libs/arcade-evals/arcade_evals/loaders.py`** (NEW): Automatic tool
loading from MCP servers
- **`libs/arcade-evals/arcade_evals/eval.py`** (MODIFIED): Enhanced
`ExpectedToolCall` and evaluation logic
- **`libs/arcade-evals/arcade_evals/__init__.py`** (MODIFIED): Exported
new registries and loaders

### Key Technical Details
- Added `BaseToolRegistry` interface for abstraction
- `MCPToolRegistry` handles single server tools
- `CompositeMCPRegistry` manages multiple servers with collision
detection
- `load_from_stdio()` and `load_from_http()` for automatic tool
discovery
- Fixed name normalization bug: MCP tools use underscores (not dots)
- Optimized tool copying: 2.5x faster via shallow copy

## Testing
-  41 tests passing (25 new tests added)
-  `test_eval_mcp_registry.py`: MCPToolRegistry functionality
-  `test_eval_composite_mcp.py`: CompositeMCPRegistry with multiple
servers
-  Verified backward compatibility with Python tools

## Backward Compatibility
 **100% backward compatible** - No breaking changes


## Breaking Changes
**None**


<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> Adds end-to-end eval UX: examples, a robust CLI runner, and rich
outputs.
> 
> - **New examples**: `eval_arcade_gateway.py`,
`eval_stdio_mcp_server.py`, `eval_http_mcp_server.py`,
`eval_comprehensive_comparison.py` with timeouts, error handling, and
track-based comparisons; detailed `README.md`
> - **CLI runner**: `arcade_cli/evals_runner.py` to execute
evals/capture in parallel with progress, error isolation, failed-only
filtering, context inclusion, and multi-provider/model support
> - **Output formatters**: `arcade_cli/formatters/` (txt, md, html,
json) for evals and capture; comparative and multi-model HTML with tabs
and context rendering
> - **Display refactor**: `display.py` now supports writing multiple
formats, failed-only disclaimers, include-context, and improved console
summaries
> 
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
ff8acf9c34a6b61462a019a1ee9df081006517d0. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->

---------

Co-authored-by: Francisco Liberal <francisco@arcade.dev>
Co-authored-by: Mateo Torres <torresmateo@gmail.com>
2026-01-07 20:26:23 -03:00

149 lines
5.4 KiB
Python

"""OpenAI tool schema conversion (internal).
Converts MCP-style tool schemas to OpenAI's tool format with strict mode support.
OpenAI strict mode requirements:
- additionalProperties: false at all object levels
- properties and required present on all object schemas
- required includes ALL properties (optional params use null union types)
- Unsupported JSON Schema keywords are stripped
"""
from __future__ import annotations
import copy
from typing import Any
# Maximum recursion depth to prevent infinite loops in circular schema references
_MAX_SCHEMA_DEPTH = 50
# Keywords not supported by OpenAI strict mode that should be stripped
_UNSUPPORTED_STRICT_MODE_KEYWORDS = frozenset({
"minimum",
"maximum",
"exclusiveMinimum",
"exclusiveMaximum",
"minLength",
"maxLength",
"pattern",
"format",
"default",
"nullable",
"minItems",
"maxItems",
"uniqueItems",
"minProperties",
"maxProperties",
})
class SchemaConversionError(Exception):
"""Raised when schema conversion fails."""
def convert_to_strict_mode_schema(parameters: dict[str, Any]) -> dict[str, Any]:
"""
Convert an input JSON schema (MCP `inputSchema`) to OpenAI strict mode format.
OpenAI strict mode requires:
- additionalProperties: false at all object levels
- properties and required present on all object schemas
- required includes ALL properties
- optional params become union types with null (e.g., ["string", "null"])
- unsupported JSON Schema keywords stripped
"""
result = copy.deepcopy(parameters)
strict_schema = _apply_strict_mode_recursive(result, depth=0)
return {
"type": "object",
"properties": strict_schema.get("properties", {}),
"required": strict_schema.get("required", []),
"additionalProperties": False,
}
def _apply_strict_mode_recursive(schema: dict[str, Any], *, depth: int = 0) -> dict[str, Any]:
if depth > _MAX_SCHEMA_DEPTH:
raise SchemaConversionError(
f"Schema nesting exceeds maximum depth of {_MAX_SCHEMA_DEPTH}. "
"This may indicate a circular reference in the schema."
)
# Strip unsupported keywords that OpenAI strict mode doesn't allow
for keyword in _UNSUPPORTED_STRICT_MODE_KEYWORDS:
schema.pop(keyword, None)
# OpenAI strict mode enum handling:
# 1. OpenAI requires enum values to be strings
# 2. OpenAI validates that enum values match the declared type
# 3. When we convert enum values to strings, we must also change the type to "string"
#
# Example: {"type": "integer", "enum": [0, 1, 2]} becomes {"type": "string", "enum": ["0", "1", "2"]}
# Example: {"type": ["integer", "null"], "enum": [0, 1]} becomes {"type": ["string", "null"], "enum": ["0", "1"]}
#
# Without this fix, OpenAI returns: "enum value 0 does not validate against {'type': ['integer', 'null']}"
if "enum" in schema:
schema["enum"] = [str(v) for v in schema["enum"]]
# Change type to string to match the stringified enum values
current_type = schema.get("type")
if current_type and current_type != "string":
if isinstance(current_type, str):
schema["type"] = "string"
elif isinstance(current_type, list) and "string" not in current_type:
# Replace non-string types with string, preserve null if present
has_null = "null" in current_type
if has_null:
schema["type"] = ["string", "null"]
else:
# Single type without null should be simplified to string
schema["type"] = "string"
schema_type = schema.get("type")
if schema_type == "object":
schema["additionalProperties"] = False
schema.setdefault("properties", {})
properties = schema.get("properties", {})
required = set(schema.get("required", []))
new_properties: dict[str, Any] = {}
all_param_names: list[str] = []
for param_name, param_schema in properties.items():
if not isinstance(param_schema, dict):
new_properties[param_name] = param_schema
all_param_names.append(param_name)
continue
processed_schema = _apply_strict_mode_recursive(param_schema, depth=depth + 1)
# Optional param: add null to type union
if param_name not in required:
param_type = processed_schema.get("type")
if isinstance(param_type, str):
processed_schema["type"] = [param_type, "null"]
elif isinstance(param_type, list) and "null" not in param_type:
processed_schema["type"] = [*param_type, "null"]
new_properties[param_name] = processed_schema
all_param_names.append(param_name)
schema["properties"] = new_properties
schema["required"] = all_param_names
elif schema_type == "array":
items = schema.get("items")
if isinstance(items, dict):
schema["items"] = _apply_strict_mode_recursive(items, depth=depth + 1)
for combiner in ("anyOf", "oneOf", "allOf"):
if combiner in schema:
schema[combiner] = [
_apply_strict_mode_recursive(option, depth=depth + 1)
if isinstance(option, dict)
else option
for option in schema[combiner]
]
return schema