Skip to content

MateusGurgel/minecraft_agent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

28 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Minecraft AI Agent

Python 3.12+ Pydantic AI License: MIT

An autonomous AI agent that plays Minecraft using LLMs and JavaScript interop.

Created for firs meeting of the Pupunha Code Community


Table of Contents


About The Project

This project was developed as a live-coding demonstration for a talk at Pupunha Code, a vibrant developer community. It showcases how to build an autonomous AI agent capable of playing Minecraft by integrating a powerful stack:

  • Python: For the core AI logic, orchestration, and machine learning.
  • JavaScript Interop: Using the javascript-async library to control a Mineflayer bot in Node.js.
  • Large Language Models: Leveraging Google Gemini for high-level reasoning and decision-making, orchestrated by Pydantic AI.
  • Machine Learning: A custom "Instinct Engine" for reactive, low-level behaviors.

The agent operates autonomously within a Minecraft server, making decisions based on environmental awareness, chat messages, and in-game events. It can navigate, fight, communicate, and survive with a human-like behavioral protocol.


Features

  • πŸ€– Autonomous Decision-Making: Powered by Google Gemini for context-aware reasoning and strategic planning, complemented by a persistent memory system.
  • πŸ‘οΈ Environmental Awareness: Scans the environment in real-time for entities, blocks, and players, while tracking events like attacks, deaths, and chat messages.
  • βš”οΈ Dynamic Combat System: Engages in both PvP and PvE combat automatically, featuring strategic retreat mechanics when health is low.
  • πŸŽ’ Intelligent Inventory Management: Capable of searching for items, managing equipment, dropping unneeded objects, and basic crafting.
  • 🧭 Advanced Navigation: Utilizes Mineflayer's pathfinding to navigate to specific coordinates, track entities, and avoid obstacles.
  • 🧠 Dual Memory System: Implements a long-term "Scratchpad" for persistent knowledge and a short-term event queue for immediate context.
  • ⚑ Instinct Module: A custom-trained ML model that provides fast, instinctual responses to common scenarios, bypassing the need for LLM inference for simple actions.

Architecture

The agent's architecture is designed for modularity, allowing clear separation between the high-level decision-making core and the low-level Minecraft interaction layer.

+------------------------------------+
|   Minecraft Server (Java Edition)  |
+------------------------------------+
                 |
+------------------------------------+
|      Mineflayer Bot (Node.js)      |
+------------------------------------+
                 | (via javascript-async)
+------------------------------------+
|        Python Agent Core           |
|------------------------------------|
| β”œβ”€ Agent Loop (Decision Cycle)    |
| β”œβ”€ Toolsets                       |
| β”‚  β”œβ”€ Chat                         |
| β”‚  β”œβ”€ Combat                       |
| β”‚  β”œβ”€ Movement                     |
| β”‚  β”œβ”€ Inventory                    |
| β”‚  β”œβ”€ Scan                         |
| β”‚  └─ Scratchpad (Memory)          |
| β”œβ”€ Listeners (Events, Messages)    |
| β”œβ”€ Instinct Engine (ML Model)     |
| └─ Plugins (PvP, AutoEat, etc.)   |
+------------------------------------+
                 |
+------------------------------------+
|   Pydantic AI + Google Gemini API  |
+------------------------------------+

Getting Started

Follow these steps to get the agent running on your local machine.

Prerequisites

  • Python 3.12+
  • Node.js 18+ with npm or yarn
  • A running Minecraft Server (Java Edition) that you can connect to.

Installation

  1. Clone the repository:
    git clone https://github.com/yourusername/minecraft-agent.git
    cd minecraft-agent

2Install Python dependencies: This command installs the project in "editable" mode, which is great for development. sh pip install -e .

3Set up environment variables: Create a .env file from the example template. sh cp .env.example .env Then, edit the .env file with your credentials and server information.


Configuration

All configuration is managed through the .env file.

# .env

# Required: Your Google Gemini API key
GOOGLE_API_KEY="your_google_api_key_here"

# Bot Configuration
BOT_NAME="Agent"
BOT_USERNAME="MinecraftUsername" # The username the bot will use to log in

# Server Connection
SERVER_HOST="127.0.0.1"
SERVER_PORT=25565

Usage

Running the Agent

To start the agent, run the main module:

python -m minecraft_agent.main

Or, if configured, use the command-line script:

agent_cli

Training the Instinct Model

To train the custom ML model for the Instinct Engine, run:

train_model

Note: Ensure you have training data available in the data/ directory.

Agent Behavior Protocol

The agent operates based on a set of core principles:

  • Survival First: Prioritize safety, health, and resource gathering.
  • Situational Awareness: Constantly monitor players, threats, and environmental changes.
  • Pragmatism: Every action must serve a clear purpose toward a goal.
  • Decisiveness: Act quickly and confidently based on available information.
  • Social Interaction: Communicate with other players in Portuguese (pt-BR).

Project Structure

minecraft-agent/
β”œβ”€β”€ data/                  # Training data for the Instinct Model
β”œβ”€β”€ src/minecraft_agent/
β”‚   β”œβ”€β”€ agent/             # Core agent logic and decision loop
β”‚   β”‚   β”œβ”€β”€ agent.py
β”‚   β”‚   β”œβ”€β”€ agent_loop.py
β”‚   β”‚   └── agent_context.py
β”‚   β”œβ”€β”€ tooling/           # Tools the agent can use
β”‚   β”‚   β”œβ”€β”€ chat/
β”‚   β”‚   β”œβ”€β”€ combat/
β”‚   β”‚   β”œβ”€β”€ inventory/
β”‚   β”‚   β”œβ”€β”€ movement.py
β”‚   β”‚   β”œβ”€β”€ scan/
β”‚   β”‚   └── scratchpad/
β”‚   β”œβ”€β”€ events/            # Event handling logic
β”‚   β”œβ”€β”€ instinct/          # The ML-based instinct engine
β”‚   β”œβ”€β”€ plugins/           # Wrappers for Mineflayer plugins
β”‚   β”‚   β”œβ”€β”€ pathfinder.py
β”‚   β”‚   β”œβ”€β”€ pvp.py
β”‚   β”‚   β”œβ”€β”€ auto_eat.py
β”‚   β”‚   β”œβ”€β”€ armor_manager.py
β”‚   β”‚   └── blood_hound.py
β”‚   β”œβ”€β”€ listeners/         # Listeners for bot events
β”‚   β”œβ”€β”€ stubs/             # Type stubs for interop
β”‚   └── main.py            # Main entry point
β”œβ”€β”€ pyproject.toml         # Project metadata and dependencies
└── README.md

Tools & Capabilities

The agent is equipped with a variety of tools to interact with the world.

Scan Tool

  • Scan Environment: Gathers information about nearby entities, players, and blocks within a configurable radius.

Chat Tools

  • Send Message: Broadcasts messages to other players in the chat.
  • Get Last Messages: Reads recent chat history to understand conversations.

Combat Tools

  • Engage Combat: Automatically attacks and pursues a target entity.
  • Strategic Retreat: Disengages from combat and flees when health is critical.

Inventory Tools

  • List Items: Lists all contents of the bot's inventory.
  • Toss Item: Drops a specified item from inventory.
  • Equip/Unequip: Manages armor and held items.
  • Craft Item: Crafts a new item using a known recipe and available materials.

Movement Tools

  • Go to Position: Navigates to a specific set of coordinates.
  • Go to Entity: Follows and approaches a target entity.

Memory Tools (Scratchpad)

  • Write to Scratchpad: Saves important information for long-term recall.
  • Read from Scratchpad: Retrieves previously saved memories.
  • Clear Scratchpad: Resets the agent's long-term memory.

Contributing

This project was created for educational purposes as part of the Pupunha Code community. Contributions are welcome! Please feel free to fork the repository, make improvements, and submit a pull request.

  1. Fork the repository.
  2. Create your feature branch (git checkout -b feature/AmazingFeature).
  3. Commit your changes (git commit -m 'Add some AmazingFeature').
  4. Push to the branch (git push origin feature/AmazingFeature).
  5. Open a Pull Request.

License

Distributed under the MIT License. See LICENSE for more information.


Acknowledgments

  • Pupunha Code Community: For the opportunity to build and present this project.
  • Pydantic AI: For the excellent and intuitive LLM framework.
  • Mineflayer: For the comprehensive and powerful Minecraft bot API.
  • Google Gemini: For powering the agent's cognitive abilities.

Contact

For questions or feedback, please open an issue on GitHub or reach out through the Pupunha Code community channels.


Built with ❀️ for Pupunha Code
Making AI play Minecraft, one block at a time.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages