Skip to content

Conversation

@supmo668
Copy link

@supmo668 supmo668 commented Jan 19, 2026

Overview

Bidirectional MCP wrappers for AgentGym environments. Addresses feedback from PR #68.

Architecture

flowchart LR
    subgraph AgentEnvToMCP["AgentEnv → MCP"]
        A[BaseEnvClient] --> B[AgentEnvMCPServer]
        B --> C[MCP Tools]
    end
    
    subgraph MCPToAgentEnv["MCP → AgentEnv"]
        D[MCP Server] --> E[MCPEnvClient]
        E --> F[BaseEnvClient API]
    end
Loading

Components

Component Purpose
schema_utils.py Bidirectional schema conversion
AgentEnvMCPServer Wrap any BaseEnvClient as MCP server
MCPEnvClient Implement BaseEnvClient for MCP servers
MCPAdapter Action parsing for all ActionFormat types
MCPTask Task wrapper for experience generation

Compatibility

  • BaseEnvClient, BaseTask, BaseAdapter
  • ✅ All ActionFormat types (REACT, FUNCTION_CALLING, CODE_AS_ACTION)
  • ✅ SciWorld proof of concept included

Tests

pytest tests/ -v  # 17 passed, 18 skipped

Commits

  1. Initialize package structure
  2. Schema conversion utilities
  3. AgentEnvToMCP wrapper
  4. MCPToAgentEnv wrapper
  5. SciWorld examples
  6. Test suite

Add new agentenv-mcp package for MCP (Model Context Protocol) integration:

- pyproject.toml: Package configuration with dependencies
- README.md: Documentation with architecture overview
- __init__.py: Lazy imports for optional dependencies

This package provides bidirectional wrappers between AgentGym
environments and MCP servers.
Add schema_utils.py with functions to convert between formats:

- function_desc_to_mcp_tool(): AgentEnv → MCP tool schema
- mcp_tool_to_function_desc(): MCP tool → AgentEnv function desc
- Batch conversion helpers for tool lists

These utilities enable seamless conversion between AgentGym's
function description format and MCP's tool schema format.
Add AgentEnvMCPServer class that wraps any BaseEnvClient as MCP server:

- Exposes environment actions as MCP tools (action_*)
- Provides management tools: env_reset, env_step, env_observe, env_info
- Supports both ReAct and function_calling action formats
- Lazy client creation for efficient resource usage
- Async handlers for all MCP tool calls

Usage:
  server = AgentEnvMCPServer(
      env_client_cls=SciworldEnvClient,
      client_args={...},
      function_descriptions=SCIWORLD_FUNCTION_DESCRIPTION,
  )
  server.run()
Add classes to wrap MCP servers as AgentGym-compatible clients:

MCPAdapter:
- Parses actions in REACT, FUNCTION_CALLING, CODE_AS_ACTION formats
- Generates conversation_start prompts from function descriptions
- Converts parsed actions to MCP tool calls

MCPEnvClient (implements BaseEnvClient interface):
- Connects to MCP servers via stdio transport
- Auto-discovers tools and generates function descriptions
- Maps step() calls to MCP tool invocations
- Full compatibility with AgentGym evaluation pipeline

MCPTask:
- Task wrapper for experience generation
- Compatible with Agent and APIAgent classes
Add proof-of-concept examples using SciWorld environment:

sciworld_mcp_server.py:
- Wraps SciWorld as an MCP server
- Includes full SCIWORLD_FUNCTION_DESCRIPTION
- Demonstrates AgentEnvMCPServer usage

mcp_client_demo.py:
- Shows how to use MCP server as AgentEnv client
- Demonstrates reset, observe, step operations
- Example of function_calling format actions

Usage:
  # Start SciWorld env server first
  uvicorn agentenv_sciworld.server:app --port 8000

  # Run MCP server
  python -m agentenv_mcp.examples.sciworld_mcp_server
Add tests for all wrapper components:

test_schema_utils.py (9 tests):
- Schema conversion functions
- Round-trip conversion verification
- SciWorld function description handling

test_agentenv_to_mcp.py (9 tests):
- AgentEnvMCPServer initialization
- Handler methods (reset, step, observe, info, action)
- ReAct formatting

test_mcp_to_agentenv.py (8 tests):
- Action parsing (ReAct, function_calling)
- Tool call conversion
- Interface compatibility checks

test_sciworld_integration.py (7 tests):
- Integration tests with SciWorld environment
- End-to-end workflow verification
- Skipped by default (requires running server)

Run tests: pytest tests/ -v
@supmo668
Copy link
Author

Closing to rewrite using FastMCP (the standard MCP framework). The initial implementation used low-level mcp library but FastMCP provides better patterns including:

  • @mcp.tool decorator-based API
  • HTTP/SSE transports for LLM client compatibility (OpenAI, etc.)
  • FastMCP.from_fastapi() to wrap existing AgentGym FastAPI servers
  • Enterprise-grade auth and deployment support

Will reopen with FastMCP-based implementation that better integrates with the existing AgentGym architecture.

@supmo668 supmo668 closed this Jan 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant