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

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.1.tar.gz (118.5 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.1-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: gym_mcp_server-0.2.1.tar.gz
  • Upload date:
  • Size: 118.5 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.1.tar.gz
Algorithm Hash digest
SHA256 fe37fc7ee0d8dedf62ae7b2013ed89ab857dbd18aa2000029e07d5c3133fcc3f
MD5 551b84d61ae005c666910d9aac8f7e96
BLAKE2b-256 c67bc282b0617acb2af319dbc9d8a705cac47fee3403f6704c2b033ebee5fae3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gym_mcp_server-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 11.7 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b590968d114f745967f4f9eb717646db1a11ab583c033fb6ad7471087cb2b481
MD5 d027e561f7d724fa47178e5286e5b795
BLAKE2b-256 ffd78e6e3a86810f8c45903bc1aa8fc2685156df141e9b3001f4bec0467d5004

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