MCPEnv
An environment that exposes MCP server tools to language models using the official MCP SDK.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:- Connects to each server via stdio
- Calls
list_tools()to discover available tools - Wraps each tool in an
MCPToolWrapperinstance - Converts MCP tool schemas to
vf.Toolformat - Registers tools with the environment
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 viaatexit 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
See Also
- ToolEnv - Base class for tool-based environments
- StatefulToolEnv - Per-rollout stateful tools
- MCP Documentation - Official MCP specification