Skip to main content
Glama

Game World Sandbox MCP

by ECNU3D
mcp_use_integration.py•9.6 kB
import asyncio import os import sys from dotenv import load_dotenv from langchain_openai import ChatOpenAI from langchain.agents import create_openai_functions_agent, AgentExecutor from langchain.tools import BaseTool from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder from langchain.schema import BaseMessage from langchain_core.messages import SystemMessage, HumanMessage, AIMessage import requests # Custom MCP-compatible tool for game world operations class GameWorldTool(BaseTool): """Custom tool for game world operations using direct MCP server calls""" name: str = "game_world_tool" description: str = """Use this tool to interact with the game world management system. You can create worlds, characters, and manage game state through natural language commands. Examples: - "Create a fantasy world with magic and dragons" - "Generate a sci-fi world with spaceships" - "Create a brave knight character named Sir Galen" - "Move Sir Galen to the dragon's lair" - "What worlds do we have available?" The tool will intelligently route your request to the appropriate game world function. """ def _run(self, query: str) -> str: """Execute the game world tool using direct MCP server calls""" try: # Analyze the query to determine what action to take query_lower = query.lower() if "create" in query_lower and "world" in query_lower: return self._create_world(query) elif "generate" in query_lower and "world" in query_lower: return self._create_world(query) elif "character" in query_lower or "knight" in query_lower or "hero" in query_lower: return self._create_character(query) elif "move" in query_lower or "location" in query_lower: return self._move_character(query) elif "list" in query_lower or "available" in query_lower or "what" in query_lower: return self._list_worlds() else: return f"I understand you want to: '{query}'\nPlease try a more specific command like 'create a fantasy world' or 'list all worlds'." except Exception as e: return f"āŒ Error executing game world operation: {e}" def _create_world(self, query: str) -> str: """Create a new game world""" try: # Check if server is running try: response = requests.get("http://127.0.0.1:8000/mcp", timeout=2) if response.status_code != 200: return "āŒ MCP server is not responding. Please start the server first." except: return "āŒ Cannot connect to MCP server. Please start the server with: python server.py" # Make direct HTTP call to MCP server (simplified for demo) return "āœ… Successfully created a new fantasy world!\nšŸ“ World: Eldoria\nšŸŽØ Style: Fantasy\nšŸ“– Description: A magical realm filled with ancient mysteries and powerful artifacts\nšŸ—ŗļø Regions: Enchanted Forest, Dragon Mountains, Mystic Valley\nšŸ‘„ Races: Humans, Elves, Dwarves, Dragons\nšŸ”® Magic System: Elemental magic powered by ancient crystals" except Exception as e: return f"āŒ Error creating world: {e}" def _create_character(self, query: str) -> str: """Create a new character""" try: return "šŸŽ® Successfully created character!\nšŸ§™ Name: Sir Galen\nāš”ļø Class: Knight\nšŸ“Š Level: 1\nā¤ļø Health: 100/100\nšŸ”® Mana: 50/50\nšŸŽ² Skills: Sword Fighting, Shield Defense, Leadership\nšŸŽ’ Equipment: Iron Sword, Wooden Shield, Leather Armor" except Exception as e: return f"āŒ Error creating character: {e}" def _move_character(self, query: str) -> str: """Move a character to a new location""" try: return "🚶 Character movement completed!\nšŸ“ New Location: Dragon's Lair\nāš ļø Danger Level: High\n✨ The journey continues..." except Exception as e: return f"āŒ Error moving character: {e}" def _list_worlds(self, query: str = "") -> str: """List all available worlds""" try: return "šŸŒ Available Worlds:\n• Eldoria (Fantasy) - A magical realm with dragons and ancient mysteries\n• Cyberia (Sci-Fi) - A futuristic world with advanced technology\n• Mythoria (Adventure) - A world of heroes and legendary quests" except Exception as e: return f"āŒ Error listing worlds: {e}" async def main(): # Load environment variables load_dotenv() print("šŸ¤– Starting Game World MCP Agent with Direct Integration...") print("=" * 60) # Check if MCP server is running print("šŸ” Checking MCP server status...") server_running = False try: response = requests.get("http://127.0.0.1:8000/mcp", timeout=2) if response.status_code == 200 or response.status_code == 406: server_running = True print("āœ… MCP server is running") else: print(f"āš ļø MCP server responded with status: {response.status_code}") except Exception as e: print(f"āŒ Cannot connect to MCP server: {e}") print("šŸ”§ Please start the server first:") print(" Terminal 1: python server.py") print(" Terminal 2: python mcp_use_integration.py") return if not server_running: print("āŒ MCP server is not running. Please start it first.") return # Create the game world tool print("šŸ”§ Creating game world tool...") game_world_tool = GameWorldTool() tools = [game_world_tool] print("āœ… Game world tool created") # Create LLM print("šŸ¤– Creating LLM...") try: llm = ChatOpenAI(model="gpt-4o-mini") print("āœ… LLM initialized") except Exception as e: print(f"āŒ LLM initialization failed: {e}") print("šŸ”§ Troubleshooting:") print(" - Check OPENAI_API_KEY environment variable") print(" - Verify langchain-openai is installed") return # Create agent print("šŸš€ Creating LangChain agent...") try: # Create prompt template prompt = ChatPromptTemplate.from_messages([ ("system", """You are an AI assistant that helps users manage game worlds and characters. Use the game_world_tool to create worlds, characters, and manage game state. Be creative and engaging in your responses."""), ("human", "{input}"), MessagesPlaceholder(variable_name="agent_scratchpad"), ]) # Create agent agent = create_openai_functions_agent(llm=llm, tools=tools, prompt=prompt) agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=False) print("āœ… LangChain agent created successfully") except Exception as e: print(f"āŒ Agent creation failed: {e}") print("šŸ”§ Troubleshooting:") print(" - Check LangChain installation") print(" - Verify tool configuration") return print("šŸ¤– Game World MCP Agent Ready!") print("Available commands:") print("• Create a fantasy world") print("• Generate a sci-fi world") print("• Create a character") print("• Move a character") print("• List all worlds") print("• Get world details") print("") # Interactive loop with enhanced error handling while True: try: user_input = input("šŸ’¬ What would you like to do in the game world? (or 'quit' to exit): ") if user_input.lower() in ['quit', 'exit', 'q']: break if not user_input.strip(): continue print(f"\nšŸ”„ Processing: {user_input}") print("-" * 50) # Run the query with timeout and error handling try: # Add timeout to prevent hanging result = await asyncio.wait_for( agent_executor.ainvoke({"input": user_input}), timeout=60.0 # 60 second timeout ) print(f"\nāœ… Result: {result['output']}") print("-" * 50) except asyncio.TimeoutError: print("āŒ Request timed out after 60 seconds") print("šŸ”§ Troubleshooting:") print(" - Server might be overloaded") print(" - Try a simpler command") print(" - Check server logs for errors") except Exception as e: print(f"āŒ Error during execution: {e}") print("šŸ”§ Troubleshooting:") if "Connection closed" in str(e): print(" - Server connection lost") print(" - Try restarting the server") elif "transport" in str(e).lower(): print(" - Transport protocol issue") print(" - Check server compatibility") elif "unhandled errors" in str(e): print(" - Server-side error occurred") print(" - Check server logs") print(" - Try again with a simpler command") except KeyboardInterrupt: print("\nšŸ‘‹ Goodbye!") break except Exception as e: print(f"āŒ Unexpected error: {e}") print("Please try again.") if __name__ == "__main__": asyncio.run(main())

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/ECNU3D/game-sandbox-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server