Minecraft服务器控制工具

一个通过RCON控制Minecraft服务器的Python工具,支持与Claude Desktop等MCP客户端集成,提供命令执行和聊天监控功能。

  • by Peterson047
  • Python
  • 本地部署
  • 6 星标
  • 更新于
在 GitHub 查看 Peterson047/Minecraft-MCP-Server

使用说明

Minecraft MCP服务器

Python MCP服务器使用FastMCP通过RCON控制Minecraft服务器。


🔧 特性

  • 公开一组命令(字典)以使LLM上下文化
  • 通过RCON在Minecraft服务器上执行命令
  • 与Claude Desktop或任何MCP客户端集成
  • 结构简单: stdio (本地开发)或HTTP/SSE(生产)

📦 项目结构

mcp_server/
├── __pycache__/
├── .env                   # Environment variables for Gemini and paths
├── commands.json          # Commands dictionary and examples  
├── mcp_chat_client.py     # NEW: Client that listens to @ai messages from chat
├── server.py              # Main MCP server  
├── .gitattributes
├── .gitignore
├── readme.md
└── requirements.txt

⚙️ 安装

  1. 克隆存储库:

    cd Minecraft-MCP-Server/mcp_server
    
  2. 创建环境并安装依赖项:

    python -m venv venv
    venv\Scripts\activate
    pip install -r requirements.txt
    

📝 设置

在 commands.json 文件中,您将看到一系列命令,如 /give, /weather, /gamemode等,并附有描述和示例。

别忘了在Minecraft中启用RCON server.properties 文件:

enable-rcon=true
rcon.password=minemcp
rcon.port=25575

创建一个 .env 文件如下:

MINECRAFT_LOG_PATH=C:\Users\YourUser\Desktop\mineserver\logs\latest.log
MCP_SERVER_PATH=mcp_server/server.py
GEMINI_API_KEY=your_gemini_api_key

🚀 运行MCP服务器

激活虚拟环境并运行:

venv\Scripts\activate
python mcp_server/server.py

Monkey补丁:默认情况下在STDIO中启动MCP服务器(apidog.com, reddit.com, github)


💬 运行聊天客户端(@ai)

启动服务器后,在新的终端中运行聊天客户端:

venv\Scripts\activate
python mcp_server/mcp_chat_client.py

此脚本监视Minecraft服务器日志,并监听以以下开头的玩家聊天消息 @ai.它将消息发送到Gemini API,并通过MCP在服务器上执行生成的命令。

⚠️ 重要:在运行客户端之前,必须启动服务器。
如果客户端在服务器之前启动,则当前版本存在已知的内存溢出错误。


⚙️ 与Claude Desktop集成

在……里面 claude_desktop_config.json (例如。, %APPDATA%\Claude\):

{
  "mcpServers": {
    "minecraft-controller": {
      "type": "stdio",
      "command": "/home/qkeq/Documentos/GitHub/Minecraft-MCP-Server/venv/bin/python3",
      "args": ["C:\\...\\mcp_server\\server.py"],
      "env": {"PATH": "%PATH%"}
    }
  }
}

然后重新启动Claude——将出现“minecraft控制器”服务器。


🧪 使用Python进行本地测试

from fastmcp import Client
import asyncio

async def test():
    client = Client("mcp_server/server.py")
    async with client:
        res = await client.call_tool("run_minecraft_command", {"command": "/list"})
        print("Players:", res)
        cmds = await client.read_resource("minecraft://commands")
        print("Commands:", list(cmds.keys())[:5])

asyncio.run(test())

🧰 运作原理

  • 🎯 FastMCP 自动加载工具和资源(medium.com, github)
  • 资源 minecraft://commands 提供命令字典
  • 工具 run_minecraft_command 用途 mcrcon 向Minecraft发送命令

📚 参考文献

  • [FastMCP v2–示例自述](pypi.org)
  • [mcrcon–Python RCON客户端](pypi.org)

🛠 后续步骤

  • 使用Docker支持HTTP/SSE传输
  • 通过命令字典进行参数验证/自动补全
  • 记录额外操作: /start, /stop, /backup, /whitelist

准备好让您的服务器智能化! 🚀