Skip to main content

MCPEnv

An environment that exposes MCP server tools to language models using the official MCP SDK.
MCPEnv is experimental and subject to breaking changes. The API may change in future releases.

Overview

MCPEnv connects to MCP servers and exposes their tools to the model as callable functions. It manages:
  • MCP server lifecycle (connection, tool discovery, cleanup)
  • Persistent background event loops for server processes
  • Tool call routing and error handling
  • Concurrent multi-server support
MCPEnv is designed for globally available, read-only MCP servers where the same toolset can be shared across all rollouts. For per-rollout, stateful servers with mutable task-specific data, consider using a custom environment.

Inheritance

Constructor

list[MCPServerConfig | dict]
default:"[]"
required
List of MCP server configurations. Can be MCPServerConfig objects or dicts with keys: name, command, args, env, description.
int
default:"10"
Maximum turns per rollout. Inherited from ToolEnv.
Callable[[Exception], str]
Function to format tool execution errors for the model.
Additional arguments passed to ToolEnv (dataset, rubric, system_prompt, etc.).

MCPServerConfig

str
required
Unique identifier for the server.
str
required
Executable command to start the MCP server (e.g., "uvx", "npx", "python").
list[str] | None
Command-line arguments for the server.
dict[str, str] | None
Environment variables to pass to the server process.
str
Human-readable description of the server’s purpose.

Example Usage

Basic Setup

Using Dict Configs

Tool Discovery

MCPEnv automatically:
  1. Connects to each server via stdio
  2. Calls list_tools() to discover available tools
  3. Wraps each tool in an MCPToolWrapper instance
  4. Converts MCP tool schemas to vf.Tool format
  5. Registers tools with the environment
Tools are available to the model immediately after initialization.

Tool Call Flow

Error Handling

Tool execution errors are caught and returned as error messages:

Lifecycle Management

MCPEnv runs MCP servers in a persistent background event loop that starts during __init__ and automatically cleans up on exit.

Server Connection

  • Servers connect during environment initialization (blocking)
  • Connection failures raise immediately
  • Tools are registered once servers are ready

Cleanup

Cleanup is automatic via atexit hooks:

Multi-Server Example

Tool Schema Conversion

MCP tools are automatically converted to Verifiers tool format:

Debugging

Enable detailed MCP logging:

Limitations

  • Global servers only: Not designed for per-rollout stateful servers
  • Stdio only: Uses stdio transport (not SSE or other protocols)
  • No streaming: Tool results are returned as complete strings
  • Single event loop: All servers share one background event loop

When to Use

Use MCPEnv when:
  • You have existing MCP servers with read-only tools
  • Tools can be shared across all rollouts
  • You need multi-server tool composition
For stateful, per-rollout tools, use StatefulToolEnv instead.

See Also