project_tools.py•10.3 kB
#!/usr/bin/env python
"""
Project Tools for Unreal Engine MCP
Provides tools for project-level operations in Unreal Engine.
"""
import logging
from typing import Dict, Any, List, Optional, Union
logger = logging.getLogger("UnrealMCP.ProjectTools")
def register_project_tools(mcp):
"""Register all project tools with the MCP server."""
@mcp.tool()
def create_input_mapping(
action_name: str,
key: str,
input_type: str = "Action", # "Action" or "Axis"
scale: float = 1.0,
category: str = "Default"
) -> Dict[str, Any]:
"""Create an input mapping in the project settings.
Args:
action_name: The name of the input action or axis to create.
key: The key to bind to the action (e.g., "E", "LeftMouseButton", "Gamepad_FaceButton_Bottom").
input_type: The type of input mapping ("Action" or "Axis").
scale: The scale factor for axis mappings.
category: The category for organizing the input mapping.
Returns:
A dictionary containing the result of the operation.
"""
from unreal_mcp_server import get_unreal_connection
conn = get_unreal_connection()
if not conn:
return {"status": "error", "error": "Not connected to Unreal Engine"}
params = {
"action_name": action_name,
"key": key,
"input_type": input_type,
"scale": scale,
"category": category
}
response = conn.send_command("create_input_mapping", params)
return response
@mcp.tool()
def get_project_settings(
section: str,
key: str = None
) -> Dict[str, Any]:
"""Get project settings.
Args:
section: The section of the project settings to retrieve.
key: Optional specific key within the section to retrieve.
Returns:
A dictionary containing the requested project settings.
"""
from unreal_mcp_server import get_unreal_connection
conn = get_unreal_connection()
if not conn:
return {"status": "error", "error": "Not connected to Unreal Engine"}
params = {"section": section}
if key:
params["key"] = key
response = conn.send_command("get_project_settings", params)
return response
@mcp.tool()
def set_project_setting(
section: str,
key: str,
value: Any
) -> Dict[str, Any]:
"""Set a project setting.
Args:
section: The section of the project settings to modify.
key: The key within the section to set.
value: The value to set.
Returns:
A dictionary containing the result of the operation.
"""
from unreal_mcp_server import get_unreal_connection
conn = get_unreal_connection()
if not conn:
return {"status": "error", "error": "Not connected to Unreal Engine"}
params = {
"section": section,
"key": key,
"value": value
}
response = conn.send_command("set_project_setting", params)
return response
@mcp.tool()
def create_game_mode(
name: str,
default_pawn_class: str = None,
hud_class: str = None,
player_controller_class: str = None,
game_state_class: str = None,
player_state_class: str = None,
path: str = "/Game/Blueprints"
) -> Dict[str, Any]:
"""Create a new GameMode Blueprint.
Args:
name: The name of the GameMode Blueprint to create.
default_pawn_class: Optional default pawn class to use.
hud_class: Optional HUD class to use.
player_controller_class: Optional player controller class to use.
game_state_class: Optional game state class to use.
player_state_class: Optional player state class to use.
path: The content browser path where the Blueprint should be created.
Returns:
A dictionary containing the result of the operation and the GameMode details.
"""
from unreal_mcp_server import get_unreal_connection
conn = get_unreal_connection()
if not conn:
return {"status": "error", "error": "Not connected to Unreal Engine"}
params = {
"name": name,
"path": path
}
if default_pawn_class:
params["default_pawn_class"] = default_pawn_class
if hud_class:
params["hud_class"] = hud_class
if player_controller_class:
params["player_controller_class"] = player_controller_class
if game_state_class:
params["game_state_class"] = game_state_class
if player_state_class:
params["player_state_class"] = player_state_class
response = conn.send_command("create_game_mode", params)
return response
@mcp.tool()
def set_default_game_mode(
game_mode_class: str
) -> Dict[str, Any]:
"""Set the default GameMode for the project.
Args:
game_mode_class: The class path of the GameMode to set as default.
Returns:
A dictionary containing the result of the operation.
"""
from unreal_mcp_server import get_unreal_connection
conn = get_unreal_connection()
if not conn:
return {"status": "error", "error": "Not connected to Unreal Engine"}
params = {"game_mode_class": game_mode_class}
response = conn.send_command("set_default_game_mode", params)
return response
@mcp.tool()
def create_level(
name: str,
template: str = "Empty",
path: str = "/Game/Maps"
) -> Dict[str, Any]:
"""Create a new level.
Args:
name: The name of the level to create.
template: The template to use for the level (e.g., "Empty", "Default", "VR-Basic").
path: The content browser path where the level should be created.
Returns:
A dictionary containing the result of the operation and the level details.
"""
from unreal_mcp_server import get_unreal_connection
conn = get_unreal_connection()
if not conn:
return {"status": "error", "error": "Not connected to Unreal Engine"}
params = {
"name": name,
"template": template,
"path": path
}
response = conn.send_command("create_level", params)
return response
@mcp.tool()
def open_level(
level_path: str
) -> Dict[str, Any]:
"""Open a level in the editor.
Args:
level_path: The path to the level to open.
Returns:
A dictionary containing the result of the operation.
"""
from unreal_mcp_server import get_unreal_connection
conn = get_unreal_connection()
if not conn:
return {"status": "error", "error": "Not connected to Unreal Engine"}
params = {"level_path": level_path}
response = conn.send_command("open_level", params)
return response
@mcp.tool()
def save_current_level() -> Dict[str, Any]:
"""Save the current level.
Returns:
A dictionary containing the result of the operation.
"""
from unreal_mcp_server import get_unreal_connection
conn = get_unreal_connection()
if not conn:
return {"status": "error", "error": "Not connected to Unreal Engine"}
response = conn.send_command("save_current_level")
return response
@mcp.tool()
def get_current_level_name() -> Dict[str, Any]:
"""Get the name of the current level.
Returns:
A dictionary containing the name of the current level.
"""
from unreal_mcp_server import get_unreal_connection
conn = get_unreal_connection()
if not conn:
return {"status": "error", "error": "Not connected to Unreal Engine"}
response = conn.send_command("get_current_level_name")
return response
@mcp.tool()
def play_in_editor(
mode: str = "PlayInViewport",
num_players: int = 1,
dedicated_server: bool = False
) -> Dict[str, Any]:
"""Start Play-in-Editor mode.
Args:
mode: The play mode ("PlayInViewport", "PlayInNewWindow", "PlayInStandaloneGame").
num_players: The number of players for multiplayer testing.
dedicated_server: Whether to run as a dedicated server.
Returns:
A dictionary containing the result of the operation.
"""
from unreal_mcp_server import get_unreal_connection
conn = get_unreal_connection()
if not conn:
return {"status": "error", "error": "Not connected to Unreal Engine"}
params = {
"mode": mode,
"num_players": num_players,
"dedicated_server": dedicated_server
}
response = conn.send_command("play_in_editor", params)
return response
@mcp.tool()
def stop_play_in_editor() -> Dict[str, Any]:
"""Stop Play-in-Editor mode.
Returns:
A dictionary containing the result of the operation.
"""
from unreal_mcp_server import get_unreal_connection
conn = get_unreal_connection()
if not conn:
return {"status": "error", "error": "Not connected to Unreal Engine"}
response = conn.send_command("stop_play_in_editor")
return response
logger.info("Registered project tools")