This document details the architecture of the Godot Model Context Protocol (MCP) integration, explaining the design decisions, component interactions, and communication protocols.
The Godot MCP architecture enables bidirectional communication between Claude AI and the Godot game engine using the Model Context Protocol. This allows Claude to interact with Godot projects through natural language commands, performing operations like node creation, script editing, and resource management.
- Bidirectional Communication: Enable Claude to both receive information from and send commands to Godot
- Robust Error Handling: Provide clear error messages and recovery mechanisms
- Modularity: Separate concerns between Godot-specific logic and MCP protocol handling
- Extensibility: Make it easy to add new commands and capabilities
- Performance: Minimize impact on Godot editor performance
The Godot addon runs within the Godot editor and provides:
- Creates a WebSocket server within Godot
- Handles client connections and message routing
- Processes incoming JSON commands
- Formats and sends JSON responses
- Routes commands to appropriate handlers
- Executes Godot API calls
- Manages command execution context
- Provides error handling and response formatting
- Provides a dock panel UI for server control
- Displays connection status and logs
- Offers configuration options
- Ensures proper lifecycle management within Godot
The MCP server bridges Claude and Godot:
- Uses FastMCP to implement the Model Context Protocol
- Handles communication with Claude
- Manages tool registration and execution
- Provides authentication and session management
- Maintains WebSocket connection to Godot
- Sends commands and receives responses
- Handles reconnection and timeout logic
- Provides a promise-based API for commands
- Defines available operations for Claude
- Validates command parameters
- Formats responses for Claude's consumption
- Provides help text and examples
- Claude to MCP Server: Claude generates a command request through the MCP protocol
- MCP Server to Godot: MCP server sends a WebSocket command message to Godot
- Godot Execution: Godot processes the command and executes relevant engine API calls
- Godot to MCP Server: Godot sends a response message to the MCP server
- MCP Server to Claude: MCP server formats the response for Claude
{
"type": "command_type",
"params": {
"param1": "value1",
"param2": "value2"
},
"commandId": "cmd_123"
}{
"status": "success",
"result": {
"key1": "value1",
"key2": "value2"
},
"commandId": "cmd_123"
}{
"status": "error",
"message": "Detailed error message",
"commandId": "cmd_123"
}Commands are encapsulated as objects with a type and parameters, enabling flexible execution and routing.
The MCP server acts as a proxy for Godot, providing Claude with a simplified interface to Godot functionality.
Command handlers act as factories for creating and executing Godot operations.
The WebSocket server uses events to notify the system about connections, disconnections, and messages.
The Godot connection manager uses promises for async command execution and response handling.
The architecture implements a comprehensive error handling strategy:
- Validation: Commands are validated before execution
- Contextual Errors: Error messages include context about what went wrong
- Error Propagation: Errors are properly propagated through the system
- Recovery Mechanisms: Connection failures trigger automatic reconnection attempts
- Timeout Handling: Long-running commands have timeout protection
- WebSocket connections are reused to reduce overhead
- Connection pool allows for multiple clients if needed
- Automatic reconnection on failure
- Commands are executed asynchronously
- Long-running operations report progress
- Resource cleanup ensures efficient memory usage
- WebSocket server runs in a separate thread
- UI updates are throttled to prevent editor slowdown
- Command execution is optimized to minimize impact
- Local-Only Communication: By default, the WebSocket server only accepts connections from localhost
- Authentication Options: Optional authentication can be implemented
- Command Validation: All commands are validated before execution
- Error Isolation: Errors in command execution don't crash the Godot editor
The architecture is designed for extensibility:
- Command Registry: New commands can be added by registering them with the command handler
- Tool Registration: New tools can be added to the MCP server
- Modular Design: Components can be extended or replaced independently
- Event System: Events allow for hooking into system operations
- Multiple Sessions: Support for multiple simultaneous Claude sessions
- Remote Connections: Secure connections from remote MCP servers
- Command Queueing: Queue for complex multi-step operations
- Advanced Authentication: More robust authentication mechanisms
- Performance Optimizations: Further reduce overhead for large projects