Skip to main content

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.10+ 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.10+

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.10+:

python --version  # Should show 3.10 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

Metadata

Release files for gym-mcp-server 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gym-mcp-server 0.2.1
File Size Uploaded
gym_mcp_server-0.2.1.tar.gz 118.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for gym-mcp-server 0.2.1
File Interpreter ABI Platform
gym_mcp_server-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 130.2 kB

Release files / gym_mcp_server-0.2.1.tar.gz

Download URL gym_mcp_server-0.2.1.tar.gz
Size 118.5 kB
Tags Source
SHA-256 checksum
How to use checksums
fe37fc7ee0d8dedf62ae7b2013ed89ab857dbd18aa2000029e07d5c3133fcc3f
BLAKE2b-256 checksum
How to use checksums
c67bc282b0617acb2af319dbc9d8a705cac47fee3403f6704c2b033ebee5fae3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release files / gym_mcp_server-0.2.1-py3-none-any.whl

Download URL gym_mcp_server-0.2.1-py3-none-any.whl
Size 11.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b590968d114f745967f4f9eb717646db1a11ab583c033fb6ad7471087cb2b481
BLAKE2b-256 checksum
How to use checksums
ffd78e6e3a86810f8c45903bc1aa8fc2685156df141e9b3001f4bec0467d5004
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page