Spaces:
Sleeping
MCP (Model Context Protocol) Integration
This document explains how to use the MCP integration for the RTS Commander game.
Overview
The MCP integration allows AI agents to interact with the RTS game through the Model Context Protocol. It exposes game state information and provides tools for performing actions in the game.
Architecture
The MCP server runs on port 8001 and provides:
- Tools - Functions that AI agents can call to interact with the game
- Resources - Static information about the game (documentation, rules)
Available Tools
get_game_state()
Returns the current state of the game as JSON, including:
- Units (infantry, tanks, helicopters, harvesters)
- Buildings (HQ, power plants, barracks, etc.)
- Player resources (credits, power)
- Map terrain information
get_ai_analysis(language="en")
Returns AI tactical analysis of the current game state in the specified language.
move_units(unit_ids, target_x, target_y)
Moves the specified units to the target position.
attack_unit(attacker_ids, target_id)
Commands the specified units to attack an enemy unit.
build_building(building_type, position_x, position_y, player_id=0)
Builds a building at the specified position.
send_game_command(command_type, **kwargs)
Sends a generic command to the game.
Available Resources
game_documentation
Provides the game's README documentation.
game_rules
Provides the game's architecture and rules documentation.
Running the MCP Server
To start the MCP server:
cd web
python mcp_server.py
The server will start on port 8001.
Connecting an AI Client
AI clients can connect to the MCP server using any MCP-compatible client library. For example, with Claude:
claude --mcp-server localhost:8001
Example Usage
An AI agent might use the MCP integration to:
- Get the current game state
- Analyze the tactical situation
- Make strategic decisions
- Execute actions in the game
Implementation Details
The MCP server is implemented in mcp_server.py and uses the existing game infrastructure:
- It accesses the game state through the
managerinstance fromapp.py - It sends commands using the existing
handle_commandmethod - It integrates with the existing AI analysis system
Extending the Integration
To add new tools or resources:
- Add new methods to the
_register_toolsor_register_resourcesmethods inRTSGameMCP - Implement the functionality using existing game infrastructure
- Test the new functionality
Security Considerations
The MCP server runs on a separate port (8001) from the main game server (7860) to isolate AI access from player connections.
In a production environment, you should consider:
- Authentication for MCP clients
- Rate limiting for commands
- Input validation for all commands