This example demonstrates a visual Gymnasium environment (LunarLander) integrated with the Model Context Protocol (MCP), featuring image rendering and complex dependencies that test conda isolation.
The LunarLander environment is a classic rocket trajectory optimization problem where an agent must learn to land a spacecraft safely on a landing pad. This example serves as a test case for:
- Visual Environment Support: Returns rendered frames as base64-encoded images
- Complex Dependencies: Requires
swigandgymnasium[box2d]for box2d physics - Conda Isolation Testing: Tests the managed simulation server's ability to handle environments with external dependencies
This example requires external dependencies that must be properly handled by conda isolation:
# Required system dependency
pip install swig
# Gymnasium with box2d physics
pip install gymnasium[box2d]
# Rendering support
pip install pygame- Action Space: Discrete(4) - NOTHING, FIRE_LEFT, FIRE_MAIN, FIRE_RIGHT
- Observation Space: Box(8) - [x, y, vx, vy, angle, angular_velocity, leg1_contact, leg2_contact]
- Reward: Based on distance to landing pad, velocity, angle, fuel consumption, and landing success
- Episode End: Landing (success/crash), going out of bounds, or time limit
cd examples/lunar_lander_mcp/mcp_server
python lunar_lander_mcp_server.py --port 8000 --seed 42cd examples/frozen_lake_mcp_complete/mcp_server
python managed_simulation_server.py --port 9002 --use-conda-isolation \
--production-script /path/to/lunar_lander_mcp_server.py \
--requirements /path/to/requirements.txtcd examples/lunar_lander_mcp
python generate_sample_images.pyThis creates a sample_trajectory/ directory with:
step_*.png- Rendered frames showing the lander in actiontrajectory_summary.json- Complete trajectory data with observations and rewards
cd examples/lunar_lander_mcp
python test_lunar_lander_conda.pyThis verifies that the managed simulation server can properly:
- Create isolated conda environments
- Install complex dependencies (swig, box2d)
- Run lunar lander simulations with visual rendering
game://initial_state- Initial environment state with rendered framegame://current_frame- Current rendered frame as base64 imagegame://action_space- Available actions and descriptionsgame://observation_space- Observation vector description
lander_action- Control the lunar lander with actions: NOTHING, FIRE_LEFT, FIRE_MAIN, FIRE_RIGHT
This example is specifically designed to test conda environment isolation:
- Complex Dependencies: Requires compilation of box2d via swig
- Visual Rendering: Tests pygame integration
- Dependency Management: Verifies requirements.txt installation in isolated environments
To test conda isolation:
# This should create a fresh conda environment and install all dependencies
python managed_simulation_server.py --port 9002 --use-conda-isolation --verbose- Successful Landing: Lander touches down gently with both legs, positive reward
- Crash: Lander hits ground too hard or at wrong angle, negative reward
- Out of Bounds: Lander leaves the visible area, episode terminates
- Visual Feedback: Each action returns a rendered frame showing the current state
lunar_lander_mcp/
├── README.md # This file
└── mcp_server/
├── requirements.txt # Dependencies for conda isolation
├── lunar_lander_adapter.py # Environment adapter with rendering
└── lunar_lander_mcp_server.py # MCP server implementation
If you encounter swig compilation errors:
# Ubuntu/Debian
sudo apt-get install swig
# macOS
brew install swig
# Windows
# Download from http://www.swig.org/download.html# Clear pip cache and reinstall
pip cache purge
pip install --no-cache-dir gymnasium[box2d]# Install pygame if rendering fails
pip install pygame
# For headless environments, you may need virtual display
sudo apt-get install xvfb
export DISPLAY=:99
Xvfb :99 -screen 0 1024x768x24 &