Skip to main content

Expose any Gymnasium environment as an MCP server

Project description

Gym MCP Server

Expose any Gymnasium environment as an MCP (Model Context Protocol) server, automatically converting the Gym API (reset, step, render) into MCP tools that any agent can call via standard JSON interfaces.

Python 3.12+ License: MIT Test Coverage

Features

  • 🎮 Works with any Gymnasium environment
  • 🔧 Exposes gym operations as MCP tools (reset, step, render, etc.)
  • 🚀 Simple API with automatic serialization and error handling
  • 🤖 Designed for AI agent integration (OpenAI Agents SDK, LangChain, etc.)
  • 🔍 Type safe with full type hints

Installation

pip install gym-mcp-server

Requirements: Python 3.12+

Quick Start

MCP Server

Run the server with the standard MCP protocol:

# Using stdio transport (default)
python -m gym_mcp_server --env CartPole-v1 --transport stdio

# Using HTTP transport
python -m gym_mcp_server --env CartPole-v1 --transport streamable-http --host localhost --port 8000

# Using SSE transport
python -m gym_mcp_server --env CartPole-v1 --transport sse --host localhost --port 8000

Programmatic Usage

from gym_mcp_server import GymMCPServer

# Create an MCP server with stdio transport
server = GymMCPServer(
    env_id="CartPole-v1",
    render_mode="rgb_array"
)

# Run the server (blocking call)
# server.run(transport="stdio")

# Or with HTTP transport
server_http = GymMCPServer(
    env_id="CartPole-v1",
    host="localhost",
    port=8000
)
# server_http.run(transport="streamable-http")

Available Tools

The server exposes these MCP tools:

  • reset_env - Reset to initial state (optional seed)
  • step_env - Take an action (required action)
  • render_env - Render current state (optional mode)
  • close_env - Close environment and free resources
  • get_env_info - Get environment metadata
  • get_available_tools - List all available tools

All tools return a standardized format:

{
    "success": bool,  # Whether the operation succeeded
    "error": str,     # Error message (if success=False)
    # ... tool-specific data
}

Examples

The examples/ directory contains complete working examples:

  • MCP Server - Creating and running MCP servers
  • MCP Client - Low-level MCP protocol usage
  • OpenAI Agents SDK (stdio) - AI agent with stdio transport
  • OpenAI Agents SDK (HTTP) - AI agent with HTTP transport

See examples/README.md for details and instructions.

Integration

OpenAI Agents SDK

Use the MCPServerStdio or MCPServerStreamableHttp classes to connect agents to gym environments:

from agents import Agent, Runner
from agents.mcp import MCPServerStdio

async with MCPServerStdio(
    name="Gym Environment",
    params={"command": "python", "args": ["-m", "gym_mcp_server", "--env", "CartPole-v1"]},
) as server:
    agent = Agent(name="GymAgent", instructions="...", mcp_servers=[server])
    result = await Runner.run(agent, "Play CartPole")

See examples/openai_agents_stdio_example.py and examples/openai_agents_http_example.py.

Documentation: OpenAI Agents SDK MCP Integration

Other Frameworks

Compatible with any MCP-compatible framework (LangChain, AutoGPT, custom MCP clients, etc.)

Configuration

Command Line Options

python -m gym_mcp_server --help
  • --env: Gymnasium environment ID (required)
  • --render-mode: Default render mode (e.g., rgb_array, human)
  • --transport: Transport type - stdio, streamable-http, or sse (default: stdio)
  • --host: Host for HTTP-based transports (default: localhost)
  • --port: Port for HTTP-based transports (default: 8000)

Transport Options

The server supports multiple transport mechanisms:

stdio (Default): Standard input/output, suitable for local MCP clients

python -m gym_mcp_server --env CartPole-v1 --transport stdio

streamable-http: HTTP-based transport with streaming support

python -m gym_mcp_server --env CartPole-v1 --transport streamable-http --host 0.0.0.0 --port 8000

sse: Server-Sent Events transport for real-time updates

python -m gym_mcp_server --env CartPole-v1 --transport sse --host 0.0.0.0 --port 8000

Troubleshooting

Environment-Specific Dependencies

Some environments require additional packages:

pip install gymnasium[atari]   # For Atari environments
pip install gymnasium[box2d]   # For Box2D environments
pip install gymnasium[mujoco]  # For MuJoCo environments

Python Version

Ensure you're using Python 3.12+:

python --version  # Should show 3.12 or higher

Development

For development and testing:

git clone https://github.com/haggaishachar/gym-mcp-server.git
cd gym-mcp-server
make install     # Install with dependencies
make check       # Run all checks (format, lint, typecheck, test)

See the Makefile for all available commands.

License

MIT License - see the LICENSE file for details.

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gym_mcp_server-0.2.0.tar.gz (141.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gym_mcp_server-0.2.0-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file gym_mcp_server-0.2.0.tar.gz.

File metadata

  • Download URL: gym_mcp_server-0.2.0.tar.gz
  • Upload date:
  • Size: 141.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for gym_mcp_server-0.2.0.tar.gz
Algorithm Hash digest
SHA256 b09c8141bb72be86b5718248108554220ccc1e050db709c152a6451fc6756b41
MD5 c5aabb71562f6809d251219c2c8d44a7
BLAKE2b-256 4cbb99f21dac71c88ed4176ea22567b35eb4e6e4d32e1f78adf19e4ef2a7af79

See more details on using hashes here.

File details

Details for the file gym_mcp_server-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: gym_mcp_server-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for gym_mcp_server-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6218c394c05f80ed43da8bf4c390dfe247dfb984ba2a2c495e26003f69a97b62
MD5 7fada84329ea937d90cfe24f00922cc9
BLAKE2b-256 59755ddd6da3beb73dd19ccb5a4ee8a36b932a0e206a5a885feaff77f75aef1a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page