Building an Autonomous AI Agent Bridge for Factorio 2.0
Introduction
Bridging the gap between complex, real-time simulation environments and external AI reasoning engines presents a unique set of challenges. Factorio AI 2.0 is an autonomous-agent bridge that enables any external Large Language Model (LLM) equipped with the Model Context Protocol (MCP)—such as Claude Desktop—to play Factorio 2.0 autonomously. By exposing game state as discrete context and in-game actions as functional tools, the project transforms a highly dynamic factory-building game into a programmable playground for LLMs.
System Architecture: The Pause-Think-Execute Loop
At the heart of the system is a carefully orchestrated "Pause-Think-Execute" loop, designed to give the AI time to reason without the game state running away from it. The architecture is split into two main components:
- The Lua Mod (
ai-interactor): Extracts the agent's Field-of-View (FOV) into a structured JSON payload, pauses the game tick, and executes fail-fast command chains sent by the AI. - The Rust Backend (
factorio-mop-mcp): Acts as the heavy-lifting middleware. It watches telemetry, maintains a permanent spatial memory in a PostgreSQL database, interfaces with the game via RCON, and exposes the system to the LLM via MCP overstdioor Streamable-HTTP.
Key Technical Achievements
- Permanent Spatial Memory: To allow the AI to remember the world beyond its immediate FOV, the backend utilizes PostgreSQL with GiST indexes. Every observed entity and resource is persisted in an R-Tree spatial index, enabling the LLM to run bounding-box and nearest-neighbor queries dynamically.
- Model Context Protocol (MCP) Integration: Game actions (e.g.,
move_to,mine,craft,place,configure_building) are exposed as strongly typed tools. This abstracts the complexity of the Lua engine away from the LLM, allowing it to focus purely on logistics and strategy. - A Pathfinding in Rust:* Relying on the game's internal pathfinder proved brittle for AI use cases. I rebuilt the Factorio world grid externally in Rust, enabling synchronous, inspectable, and replannable A* pathfinding with obstacle avoidance before commands ever reach the game engine.
- Real-time Telemetry & WebGUI: A live dashboard built with Axum streams game state and execution events via WebSockets. It overlays the FOV bounding box, color-codes entities, and provides an expandable, human-readable event log of the LLM's actions in real-time.
Challenges Overcome
Building a robust asynchronous bridge required solving several edge cases involving state synchronization, game engine quirks, and LLM behavior:
- Handling Factorio 2.0's Data Structures: Adapted ingestion and interaction logic (like
takeanddrop) to handle Factorio 2.0's new array-of-tables inventory format and quality-aware item logistics. - Action Queue Deadlocks & RCON Synchronization: Addressed issues where frozen Factorio instances or RCON sockets would deadlock the server. I implemented global timeouts and decoupled RCON execution from game-pause states, ensuring the MCP server remains responsive even if the game stalls.
- Non-Blocking Research API: Initially, instructing the AI to research a technology blocked the command queue until the research finished. I redesigned this into a fire-and-forget
start_researchtool accompanied by a read-onlycheck_research_statuspoller. This allows the AI to continue mining, crafting, and walking while labs operate in the background. - Mitigating "Mining-on-Player" Collisions: LLMs often struggle with exact spatial reasoning. I enhanced placement and mining validation to explicitly reject commands that would cause the agent to build on top of itself or attempt to mine out-of-reach objects. The system now returns targeted error feedback, prompting the LLM to correct its behavior.
- "Stale FOV" & Telemetry Blackouts: Fixed complex race conditions between the game's paused state, the file watcher (keyed by an
mtime,size, andhashtriple), and synchronous RCON exports. This guarantees the AI never hallucinates actions based on outdated perception data.
Benchmarking & Agent Evaluation
Beyond just playing the game, the system serves as an evaluation harness for LLMs. A built-in metrics aggregator tracks key performance indicators—such as task success rates, time-to-first-command (thinking time), distance traveled, and "cheat" usage.
Tasks are declaratively defined in a tasks.toml file (e.g., inventory_at_least), and the agent uses a mark_task_complete tool to claim progress. The Rust backend then automatically cross-checks the agent's claim against the ground truth of the game state, identifying instances where the LLM successfully completes a task but fails to recognize it, or hallucinates completing a task it hasn't.
Conclusion
Factorio AI 2.0 demonstrates how robust middleware, explicit spatial memory, and carefully scoped tool exposure can enable general-purpose LLMs to interact with complex, continuous, and highly structured environments.