arcade-mcp/libs/arcade-evals/arcade_evals/_evalsuite/_convenience.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

265 lines
8.8 KiB
Python

"""EvalSuite convenience methods (internal-only).
This module contains only the functionality introduced in this PR:
- tool registration convenience methods
- unified internal registry plumbing helpers
- track-based tool registration for comparative evaluations
It is intentionally not exported from `arcade_evals.__init__`.
"""
from __future__ import annotations
import warnings
from typing import TYPE_CHECKING, Any, Callable
from arcade_evals._evalsuite._tool_registry import EvalSuiteToolRegistry, MCPToolDefinition
from arcade_evals._evalsuite._tracks import TrackManager
from arcade_evals.loaders import (
load_arcade_mcp_gateway_async,
load_from_stdio_async,
load_mcp_remote_async,
)
if TYPE_CHECKING:
from arcade_core import ToolCatalog
class _EvalSuiteConvenienceMixin:
"""Mixin providing convenience tool registration methods."""
_internal_registry: EvalSuiteToolRegistry | None
_track_manager: TrackManager
_python_tool_func_map: dict[str, Callable]
_python_func_to_tool_name: dict[Callable, str]
strict_mode: bool # Attribute from EvalSuite dataclass
def _get_registry(self, track: str | None = None) -> EvalSuiteToolRegistry:
"""Get the registry for a track or the default internal registry.
Args:
track: Optional track name. If provided, gets or creates the track registry.
If None, uses the default internal registry.
Returns:
The appropriate EvalSuiteToolRegistry.
Raises:
RuntimeError: If internal registry not initialized.
"""
if track is not None:
# Get existing track registry or create new one
registry = self._track_manager.get_registry(track)
if registry is None:
# Create new registry for this track
registry = EvalSuiteToolRegistry(strict_mode=self.strict_mode)
self._track_manager.create_track(track, registry)
return registry
# Default: use internal registry
if self._internal_registry is None:
raise RuntimeError("Internal registry not initialized. This should not happen.")
return self._internal_registry
def get_tracks(self) -> list[str]:
"""Get all registered track names.
Returns:
List of track names in registration order.
"""
return self._track_manager.get_track_names()
def add_tool_definitions(
self,
tools: list[MCPToolDefinition],
*,
track: str | None = None,
) -> Any:
"""Add tool definitions directly from MCP-style dictionaries.
Args:
tools: List of tool definitions. Each must have:
- name (str): Required. The unique tool name.
- description (str): Optional. Defaults to "".
- inputSchema (dict): Optional. JSON Schema for parameters.
Defaults to {"type": "object", "properties": {}}.
track: Optional track name. If provided, tools are added to that track's
isolated registry. Use for comparative evaluations.
Returns:
Self for method chaining.
Raises:
TypeError: If a tool definition is not a dictionary.
ValueError: If a tool definition is missing 'name' or the name is already registered.
"""
registry = self._get_registry(track)
for tool in tools:
if not isinstance(tool, dict):
raise TypeError("Tool definitions must be dictionaries")
if "name" not in tool:
raise ValueError("Tool definition must include 'name'")
# Copy to avoid mutating input dict
tool_copy = dict(tool)
tool_copy.setdefault("description", "")
tool_copy.setdefault("inputSchema", {"type": "object", "properties": {}})
registry.add_tool(tool_copy)
return self
async def add_mcp_server(
self,
url: str,
*,
headers: dict[str, str] | None = None,
timeout: int = 10,
track: str | None = None,
use_sse: bool = False,
) -> Any:
"""Add tools from an MCP HTTP server.
Args:
url: The MCP server URL.
headers: Optional HTTP headers.
timeout: Connection timeout in seconds.
track: Optional track name for comparative evaluations.
use_sse: If True, use Server-Sent Events (SSE) transport.
Returns:
Self for method chaining.
"""
registry = self._get_registry(track)
tools = await load_mcp_remote_async(url, timeout=timeout, headers=headers, use_sse=use_sse)
if not tools:
warnings.warn(
f"No tools loaded from {url}. Server may be unavailable.",
UserWarning,
stacklevel=2,
)
return self
registry.add_tools(tools)
return self
async def add_mcp_stdio_server(
self,
command: list[str],
*,
env: dict[str, str] | None = None,
timeout: int = 10,
track: str | None = None,
) -> Any:
"""Add tools from an MCP stdio server.
Args:
command: Command to start the MCP server.
env: Optional environment variables.
timeout: Connection timeout in seconds.
track: Optional track name for comparative evaluations.
Returns:
Self for method chaining.
"""
registry = self._get_registry(track)
tools = await load_from_stdio_async(command, timeout=timeout, env=env)
if not tools:
warnings.warn(
f"No tools loaded from stdio command: {' '.join(command)}",
UserWarning,
stacklevel=2,
)
return self
registry.add_tools(tools)
return self
async def add_arcade_gateway(
self,
gateway_slug: str,
*,
arcade_api_key: str | None = None,
arcade_user_id: str | None = None,
base_url: str | None = None,
timeout: int = 10,
track: str | None = None,
) -> Any:
"""Add tools from an Arcade MCP gateway.
Args:
gateway_slug: The Arcade gateway slug.
arcade_api_key: Optional API key.
arcade_user_id: Optional user ID.
base_url: Optional base URL.
timeout: Connection timeout in seconds.
track: Optional track name for comparative evaluations.
Returns:
Self for method chaining.
"""
registry = self._get_registry(track)
tools = await load_arcade_mcp_gateway_async(
gateway_slug,
arcade_api_key=arcade_api_key,
arcade_user_id=arcade_user_id,
base_url=base_url, # Let loader handle default/env var
timeout=timeout,
)
if not tools:
warnings.warn(
f"No tools loaded from Arcade gateway: {gateway_slug}",
UserWarning,
stacklevel=2,
)
return self
registry.add_tools(tools)
return self
def add_tool_catalog(
self,
catalog: ToolCatalog,
*,
track: str | None = None,
) -> Any:
"""Add tools from a ToolCatalog to the internal registry.
Args:
catalog: A ToolCatalog containing registered Python tools.
track: Optional track name for comparative evaluations.
Returns:
Self for method chaining.
"""
# Delegate to the shared helper method defined in EvalSuite
self._register_catalog_tools(catalog, track=track) # type: ignore[attr-defined]
return self
def get_tool_count(self, track: str | None = None) -> int:
"""Get the number of registered tools.
Args:
track: Optional track name. If provided, counts tools in that track.
Returns:
Number of tools.
"""
if track is not None:
registry = self._track_manager.get_registry(track)
return registry.tool_count() if registry else 0
if self._internal_registry is None:
return 0
return self._internal_registry.tool_count()
def list_tool_names(self, track: str | None = None) -> list[str]:
"""List all registered tool names.
Args:
track: Optional track name. If provided, lists tools in that track.
Returns:
List of tool names.
"""
if track is not None:
registry = self._track_manager.get_registry(track)
return registry.tool_names() if registry else []
if self._internal_registry is None:
return []
return self._internal_registry.tool_names()