|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Automatic Demo: Random MuJoCo Menagerie Model via MCP |
| 4 | +Shows testing, loading, and controlling random models |
| 5 | +""" |
| 6 | + |
| 7 | +import asyncio |
| 8 | +import json |
| 9 | +import random |
| 10 | +import sys |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | +# Add src to path for testing |
| 14 | +sys.path.insert(0, str(Path(__file__).parent / "src")) |
| 15 | + |
| 16 | +async def demo_random_menagerie_model(): |
| 17 | + """Automatic demo with random model selection""" |
| 18 | + print("π Automatic MuJoCo Menagerie MCP Demo") |
| 19 | + print("=" * 45) |
| 20 | + |
| 21 | + try: |
| 22 | + # Import enhanced MCP server |
| 23 | + from mujoco_mcp.mcp_server_menagerie import handle_list_tools, handle_call_tool |
| 24 | + |
| 25 | + # Step 1: Show MCP capabilities |
| 26 | + print("\nπ§ Step 1: MCP Server Capabilities") |
| 27 | + print("-" * 35) |
| 28 | + |
| 29 | + tools = await handle_list_tools() |
| 30 | + print(f"β
Enhanced MCP Server with {len(tools)} tools:") |
| 31 | + |
| 32 | + menagerie_tools = [] |
| 33 | + built_in_tools = [] |
| 34 | + |
| 35 | + for tool in tools: |
| 36 | + if "menagerie" in tool.name.lower(): |
| 37 | + menagerie_tools.append(tool.name) |
| 38 | + else: |
| 39 | + built_in_tools.append(tool.name) |
| 40 | + |
| 41 | + print(f" π New Menagerie tools: {', '.join(menagerie_tools)}") |
| 42 | + print(f" π Built-in tools: {', '.join(built_in_tools)}") |
| 43 | + |
| 44 | + # Step 2: Discover all available models |
| 45 | + print("\nπ¦ Step 2: Model Discovery") |
| 46 | + print("-" * 28) |
| 47 | + |
| 48 | + models_result = await handle_call_tool("list_menagerie_models", {}) |
| 49 | + models_data = json.loads(models_result[0].text) |
| 50 | + |
| 51 | + print(f"π Discovered {models_data['total_models']} models across {models_data['categories']} categories:") |
| 52 | + |
| 53 | + # Collect all models and show category breakdown |
| 54 | + all_models = [] |
| 55 | + for category, info in models_data['models'].items(): |
| 56 | + print(f" π·οΈ {category.upper()}: {info['count']} models") |
| 57 | + |
| 58 | + # Show 2 examples from each category |
| 59 | + examples = info['models'][:2] |
| 60 | + if examples: |
| 61 | + print(f" Examples: {', '.join(examples)}") |
| 62 | + |
| 63 | + all_models.extend(info['models']) |
| 64 | + |
| 65 | + # Step 3: Random model selection |
| 66 | + print("\nπ² Step 3: Random Model Selection") |
| 67 | + print("-" * 32) |
| 68 | + |
| 69 | + # Select models from different categories for variety |
| 70 | + categories = list(models_data['models'].keys()) |
| 71 | + selected_models = [] |
| 72 | + |
| 73 | + for category in random.sample(categories, min(3, len(categories))): |
| 74 | + category_models = models_data['models'][category]['models'] |
| 75 | + selected_model = random.choice(category_models) |
| 76 | + selected_models.append((selected_model, category)) |
| 77 | + |
| 78 | + print("π― Selected models for testing:") |
| 79 | + for i, (model, category) in enumerate(selected_models, 1): |
| 80 | + print(f" {i}. {model} ({category})") |
| 81 | + |
| 82 | + # Step 4: Test each selected model |
| 83 | + for i, (model_name, category) in enumerate(selected_models, 1): |
| 84 | + print(f"\n{'=' * 20} MODEL {i}: {model_name.upper()} {'=' * 20}") |
| 85 | + |
| 86 | + # Validate the model |
| 87 | + print(f"π¬ Validating {model_name}...") |
| 88 | + validation_result = await handle_call_tool("validate_menagerie_model", { |
| 89 | + "model_name": model_name |
| 90 | + }) |
| 91 | + |
| 92 | + validation_text = validation_result[0].text |
| 93 | + print(f" {validation_text}") |
| 94 | + |
| 95 | + # Create scene from the model |
| 96 | + print("ποΈ Creating scene...") |
| 97 | + scene_name = f"demo_{model_name}_{i}" |
| 98 | + scene_result = await handle_call_tool("create_menagerie_scene", { |
| 99 | + "model_name": model_name, |
| 100 | + "scene_name": scene_name |
| 101 | + }) |
| 102 | + |
| 103 | + scene_text = scene_result[0].text |
| 104 | + print(f" π {scene_text}") |
| 105 | + |
| 106 | + # Test simulation control |
| 107 | + if "β
" in scene_text or "XML generated" in scene_text: |
| 108 | + print("β‘ Testing simulation control...") |
| 109 | + |
| 110 | + # Step simulation |
| 111 | + step_result = await handle_call_tool("step_simulation", { |
| 112 | + "model_id": scene_name, |
| 113 | + "steps": 3 |
| 114 | + }) |
| 115 | + print(f" π Step: {step_result[0].text}") |
| 116 | + |
| 117 | + # Get state |
| 118 | + state_result = await handle_call_tool("get_state", { |
| 119 | + "model_id": scene_name |
| 120 | + }) |
| 121 | + state_preview = state_result[0].text[:100] + "..." if len(state_result[0].text) > 100 else state_result[0].text |
| 122 | + print(f" π State: {state_preview}") |
| 123 | + |
| 124 | + # Reset simulation |
| 125 | + reset_result = await handle_call_tool("reset_simulation", { |
| 126 | + "model_id": scene_name |
| 127 | + }) |
| 128 | + print(f" π Reset: {reset_result[0].text}") |
| 129 | + |
| 130 | + # Step 5: Demonstrate enhanced create_scene |
| 131 | + print("\n⨠Step 5: Enhanced Scene Creation") |
| 132 | + print("-" * 35) |
| 133 | + |
| 134 | + # Pick a final model for enhanced demo |
| 135 | + final_model = random.choice(all_models) |
| 136 | + print(f"πͺ Demonstrating enhanced create_scene with {final_model}") |
| 137 | + |
| 138 | + enhanced_result = await handle_call_tool("create_scene", { |
| 139 | + "scene_type": "pendulum", # Built-in scene type |
| 140 | + "menagerie_model": final_model # Our Menagerie enhancement! |
| 141 | + }) |
| 142 | + |
| 143 | + enhanced_text = enhanced_result[0].text |
| 144 | + print(f" β¨ Enhanced: {enhanced_text}") |
| 145 | + |
| 146 | + # Demo Summary |
| 147 | + print(f"\n{'=' * 60}") |
| 148 | + print("π AUTOMATIC DEMO COMPLETE") |
| 149 | + print(f"{'=' * 60}") |
| 150 | + print(f"π― Models Tested: {len(selected_models)}") |
| 151 | + print(f"π Total Available: {models_data['total_models']} models") |
| 152 | + print(f"π·οΈ Categories: {', '.join(models_data['models'].keys())}") |
| 153 | + print(f"π§ MCP Tools: {len(menagerie_tools)} Menagerie + {len(built_in_tools)} built-in") |
| 154 | + print("β
Status: All models accessible via MCP!") |
| 155 | + |
| 156 | + print("\nπ‘ What you can do now:") |
| 157 | + print(f" β’ Use any of the {models_data['total_models']} models in Claude Desktop") |
| 158 | + print(" β’ Call list_menagerie_models to browse all models") |
| 159 | + print(" β’ Call create_menagerie_scene with any model name") |
| 160 | + print(" β’ Use enhanced create_scene with menagerie_model parameter") |
| 161 | + print(" β’ Control simulations with step_simulation, get_state, reset_simulation") |
| 162 | + |
| 163 | + return True |
| 164 | + |
| 165 | + except Exception as e: |
| 166 | + print(f"\nβ Demo failed: {e}") |
| 167 | + import traceback |
| 168 | + traceback.print_exc() |
| 169 | + return False |
| 170 | + |
| 171 | +if __name__ == "__main__": |
| 172 | + try: |
| 173 | + success = asyncio.run(demo_random_menagerie_model()) |
| 174 | + print(f"\nπ Demo {'successful' if success else 'failed'}!") |
| 175 | + sys.exit(0 if success else 1) |
| 176 | + except Exception as e: |
| 177 | + print(f"\nπ₯ Demo crashed: {e}") |
| 178 | + sys.exit(1) |
0 commit comments