"""
Example Usage of Claude-Modeling-Labs MCP Server
This example demonstrates how to use the modular CML toolkit to create
a simple networking lab for educational purposes.
"""
import asyncio
import sys
import os
# Add the src directory to Python path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src'))
from client import CMLAuth, set_client
from handlers.lab_management import create_lab, start_lab, wait_for_lab_nodes
from handlers.topology import create_router, create_switch, link_nodes, get_lab_topology
from handlers.configuration import configure_node
from handlers.console import send_console_command
async def create_ospf_example_lab():
"""
Create a simple OSPF lab with two routers connected via a switch
"""
# Initialize CML client (replace with your server details)
cml_auth = CMLAuth(
base_url="https://10.10.20.161",
username="developer",
password="C1sco12345",
verify_ssl=False # Set to True for production
)
try:
# Authenticate and set global client
await cml_auth.authenticate()
set_client(cml_auth)
print("✓ Connected to CML server")
# Create a new lab
lab_result = await create_lab(
title="OSPF Learning Lab",
description="Two routers connected via OSPF for learning routing protocols"
)
if "error" in lab_result:
print(f"✗ Failed to create lab: {lab_result['error']}")
return
lab_id = lab_result["lab_id"]
print(f"✓ Created lab: {lab_result['message']}")
# Create network devices
print("\nCreating network devices...")
# Create Router 1
r1_result = await create_router(lab_id, "Router1", x=100, y=100)
if "error" in r1_result:
print(f"✗ Failed to create Router1: {r1_result['error']}")
return
r1_id = r1_result["node_id"]
print(f"✓ Created Router1")
# Create Router 2
r2_result = await create_router(lab_id, "Router2", x=300, y=100)
if "error" in r2_result:
print(f"✗ Failed to create Router2: {r2_result['error']}")
return
r2_id = r2_result["node_id"]
print(f"✓ Created Router2")
# Create a switch to connect them
sw1_result = await create_switch(lab_id, "Switch1", x=200, y=200)
if "error" in sw1_result:
print(f"✗ Failed to create Switch1: {sw1_result['error']}")
return
sw1_id = sw1_result["node_id"]
print(f"✓ Created Switch1")
# Connect the devices
print("\nCreating network links...")
# Connect R1 to Switch
link1_result = await link_nodes(lab_id, r1_id, sw1_id)
if "error" in link1_result:
print(f"✗ Failed to link Router1 to Switch1: {link1_result['error']}")
else:
print(f"✓ Connected Router1 to Switch1")
# Connect R2 to Switch
link2_result = await link_nodes(lab_id, r2_id, sw1_id)
if "error" in link2_result:
print(f"✗ Failed to link Router2 to Switch1: {link2_result['error']}")
else:
print(f"✓ Connected Router2 to Switch1")
# Configure the routers with OSPF
print("\nConfiguring devices...")
# Router 1 configuration
r1_config = """
hostname Router1
!
interface GigabitEthernet0/0
ip address 10.1.1.1 255.255.255.0
no shutdown
!
router ospf 1
router-id 1.1.1.1
network 10.1.1.0 0.0.0.255 area 0
!
"""
await configure_node(lab_id, r1_id, r1_config)
print("✓ Configured Router1 with OSPF")
# Router 2 configuration
r2_config = """
hostname Router2
!
interface GigabitEthernet0/0
ip address 10.1.1.2 255.255.255.0
no shutdown
!
router ospf 1
router-id 2.2.2.2
network 10.1.1.0 0.0.0.255 area 0
!
"""
await configure_node(lab_id, r2_id, r2_config)
print("✓ Configured Router2 with OSPF")
# Start the lab
print("\nStarting lab...")
start_result = await start_lab(lab_id)
print(f"✓ {start_result}")
# Wait for nodes to initialize
print("Waiting for nodes to initialize (this may take a few minutes)...")
wait_result = await wait_for_lab_nodes(lab_id, timeout=300)
print(f"✓ {wait_result}")
# Display topology summary
print("\n" + "="*50)
print("LAB TOPOLOGY SUMMARY")
print("="*50)
topology = await get_lab_topology(lab_id)
print(topology)
# Test connectivity
print("\n" + "="*50)
print("TESTING CONNECTIVITY")
print("="*50)
# Test ping from Router1 to Router2
ping_result = await send_console_command(lab_id, r1_id, "ping 10.1.1.2")
print(f"\nPing from Router1 to Router2:")
print(ping_result.get("output", "No output"))
# Show OSPF neighbors on Router1
ospf_result = await send_console_command(lab_id, r1_id, "show ip ospf neighbor")
print(f"\nOSPF Neighbors on Router1:")
print(ospf_result.get("output", "No output"))
print("\n" + "="*50)
print("LAB READY FOR USE!")
print("="*50)
print(f"Lab ID: {lab_id}")
print("You can now access the devices through the CML interface")
print("or continue using the console commands through this toolkit.")
return lab_id
except Exception as e:
print(f"✗ Error during lab creation: {str(e)}")
return None
async def main():
"""Main example function"""
print("Claude-Modeling-Labs Example: Creating OSPF Learning Lab")
print("="*60)
lab_id = await create_ospf_example_lab()
if lab_id:
print(f"\n🎉 Successfully created and started lab: {lab_id}")
print("\nNext steps:")
print("1. Access devices through CML web interface")
print("2. Use console commands to explore OSPF configuration")
print("3. Experiment with routing scenarios")
print("4. Try breaking/fixing connectivity for learning")
else:
print("\n❌ Failed to create lab. Check your CML server connection and credentials.")
if __name__ == "__main__":
# Run the example
asyncio.run(main())