虚幻引擎MCP Python桥接插件

一个为虚幻引擎设计的插件,通过Model Context Protocol (MCP)实现Python API的服务器端访问,支持AI代理如Claude进行工具开发、工作流自动化和协作开发。

  • by appleweed
  • C++
  • 本地部署
  • 64 星标
  • 更新于
在 GitHub 查看 appleweed/UnrealMCPBridge

使用说明

虚幻引擎MCP Python桥插件

这是虚幻引擎(UE)的一个插件,它创建了一个服务器实现 模型上下文协议(MCP). 这允许MCP客户端,如Anthropic的 克劳德,以访问完整的UE Python API。

用例

  • 使用Claude这样的代理在Python中开发工具和工作流。
  • 此类工作流程的智能和动态自动化。
  • 与代理进行一般协作开发。

先决条件

  • 您的系统上提供Python 3.10或更高版本 PATH,与官方 MCP Python SDK 安装:

    pip install mcp
    

    这是唯一的Python依赖项。客户端脚本 unreal_mcp_client.py 用途 from mcp.server.fastmcp import FastMCP 将桥接工具暴露给您的代理。A. MCPClient/requirements.txt 提供文件是为了方便(pip install -r MCPClient/requirements.txt).注:这是你的 系统 Python——该脚本作为代理的子进程运行,而不是在编辑器的捆绑Python中运行。

  • AI代理。下面,我们假设将使用Claude。但任何实现MCP的AI代理都应该足够了。

  • 启用Python编辑器脚本插件的虚幻引擎5。

  • Visual Studio 2019或更高版本(仅在从源代码构建插件时需要,Fab安装不需要)。

  • 注意 虚幻引擎Python API.

从Fab安装

安装MCP Python Bridge插件的最简单方法是从 Fab商店列表。一旦您购买了该插件,它将出现在Epic Games Launcher的库中。点击“安装到引擎”并选择合适的虚幻引擎版本。

从GitHub安装

  1. 创建一个新的虚幻引擎C++项目。
  2. 在项目根目录下,找到 Plugins 文件夹。
  3. 将此仓库克隆到 Plugins 文件夹,以便在下面有一个新的包含所有项目内容的文件夹。
  4. 右键单击UE项目文件(以结尾 .uproject)并选择“生成Visual Studio项目文件”。如果您没有立即看到该选项,请先选择“显示更多选项”,它应该会出现。
  5. 打开新的Visual Studio项目并生成。
  6. 复制 unreal_mcp_client.py 从“MCPClient”文件夹移动到您选择的位置。
  7. 找到你的 claude_desktop_config.json 配置文件。 Mac位置: ~/Library/Application\ Support/Claude/claude_desktop_config.json Windows位置: [path_to_your_user_account]\AppData\Roaming\Claude\claude_desktop_config.json
  8. 添加 unreal-engine 将服务器部分添加到配置文件中,并更新下面除方括号外的路径位置。 Mac路径格式: /[path_from_step_4]/unreal_mcp_client.py Windows路径格式: C:\\[path_from_step_4]\\unreal_mcp_client.py
{
  "mcpServers": {
    "unreal-engine": {
      "command": "python",
      "args": [
        "[mac_or_windows_format_path_to_unreal_mcp_client.py]"
      ]
    } 
  }
}
  1. 启动虚幻引擎并加载新项目。
  2. 启用新 UnrealMCPBridge 插件,然后重新启动。
  3. 当你用鼠标悬停时,应该有一个新的工具栏按钮,上面写着“启动MCP桥”。
  4. 单击MCP桥接按钮。一个弹出窗口将显示“MCP Python桥正在运行”。输出日志将记录一个新的套接字服务器正在监听127.0.0.1:9000。
  5. 以管理员身份启动Claude。
  6. 单击“从MCP连接”插头图标。在“选择集成”下有两个测试提示: create_castle 和 create_town。您可以在中编辑它们的实现 unreal_server_init.py 在...之下 Content 插件的文件夹。请确保在任何更改后重新启动虚幻引擎。
  7. 或者,让Claude使用UEPython API构建您的项目。
  8. 通过单击插头图标左侧的锤子图标,可以找到当前实施的工具列表。

开发新工具和提示

检查在 unreal_mcp_client.py 您将看到MCP如何使用Python装饰器在上述函数中定义工具和提示。例如:

@mcp.tool()
def get_project_dir() -> str:
    """Get the top level project directory"""
    result = send_command("get_project_dir")
    if result.get("status") == "success":
        response = result.get("result")
        return response
    else:
        return json.dumps(result)

这发送了 get_project_dir 命令到虚幻引擎执行,并返回当前项目的项目级目录。上下 Content 在插件的文件夹中,您将看到此工具命令的服务器端实现:

@staticmethod
def get_project_dir():
    """Get the top level project directory"""
    try:
        project_dir = unreal.Paths.project_dir()
        return json.dumps({
            "status": "success",
            "result" : f"{project_dir}"
        })
    except Exception as e:
        return json.dumps({ "status": "error", "message": str(e) })

按照此模式创建新工具,这些工具将出现在Claude桌面界面的锤子图标下。

实现新提示的问题是将它们添加到 unreal_mcp_client.py例如,以下是 create_castle 提示从 unreal_mcp_client.py:

@mcp.prompt()
def create_castle() -> str:
    """Create a castle"""
    return f"""
Please create a castle in the current Unreal Engine project.
0. Refer to the Unreal Engine Python API when creating new python code: https://dev.epicgames.com/documentation/en-us/unreal-engine/python-api/?application_version=5.5
1. Clear all the StaticMeshActors in the scene.
2. Get the project directory and the content directory.
3. Find basic shapes to use for building structures.
4. Create a castle using these basic shapes.
"""

请确保保持三引号,以便返回整个提示。迭代创建提示的一个好方法是使用Claude迭代每个步骤号,直到得到满意的结果。然后将它们组合成一个编号的逐步提示,如图所示。

您必须重新启动Claude才能对以下内容进行任何更改 unreal_mcp_client.py 生效。注意,对于Windows,您可能需要在任务管理器中结束Claude进程才能真正重新启动Claude。