rts-commander / docs /MCP_INTEGRATION.md
Luigi's picture
Initial commit: Complete RTS project with MCP evaluation
551ad28

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:

  1. Tools - Functions that AI agents can call to interact with the game
  2. 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:

  1. Get the current game state
  2. Analyze the tactical situation
  3. Make strategic decisions
  4. 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 manager instance from app.py
  • It sends commands using the existing handle_command method
  • It integrates with the existing AI analysis system

Extending the Integration

To add new tools or resources:

  1. Add new methods to the _register_tools or _register_resources methods in RTSGameMCP
  2. Implement the functionality using existing game infrastructure
  3. 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