这是一个可扩展的框架,用于将 Unity 和 Model Context Protocol (MCP) 进行整合。通过这个框架,AI语言模型如Claude可以通过一个可扩展的处理器架构直接与Unity编辑器进行交互。
https://github.com/isuzu-shiranui/UnityMCP.git?path=jp.shiranui-isuzu.unity-mcpUnity MCP包含了一个简单的安装和配置TypeScript客户端的工具。
这样,Claude Desktop将自动连接到Unity MCP客户端,实现与Unity编辑器的无缝集成。
build/index.js文件的完整路径claude_desktop_config.json{
"mcpServers": {
"unity-mcp": {
"command": "node",
"args": [
"path/to/index.js"
]
}
}
}
※ 请将path/to/index.js替换为实际路径(在Windows上,请使用双反斜杠"\\"或正斜杠"/")
Unity MCP框架主要由两个组件构成:
Unity MCP基于Model Context Protocol (MCP)支持以下三种处理器类型:
该包包含以下示例:
Unity MCP处理器示例
Unity MCP处理器示例JavaScript
build/handlers目录中使用⚠️ 注意:示例代码包含任意代码执行功能。在生产环境中使用时请注意安全。
示例导入方法:
创建一个新的实现IMcpCommandHandler的类:
using Newtonsoft.Json.Linq;
using UnityMCP.Editor.Core;
namespace YourNamespace.Handlers
{
internal sealed class YourCommandHandler : IMcpCommandHandler
{
public string CommandPrefix => "yourprefix";
public string Description => "处理器描述";
public JObject Execute(string action, JObject parameters)
{
// 实现命令逻辑
if (action == "yourAction")
{
// 使用参数执行某些操作
return new JObject
{
["success"] = true,
["result"] = "结果数据"
};
}
return new JObject
{
["success"]_ = false,
["error"] = $"未知动作: {action}"
};
}
}
}
创建一个新的实现IMcpResourceHandler的类:
using Newtonsoft.Json.Linq;
using UnityMCP.Editor.Resources;
namespace YourNamespace.Resources
{
internal sealed class YourResourceHandler : IMcpResourceHandler
{
public string ResourceName => "yourresource";
public string Description => "资源描述";
public string ResourceUri => "unity://yourresource";
public JObject FetchResource(JObject parameters)
{
// 实现获取资源数据的处理
var data = new JArray();
// 获取并处理一些数据,添加到JArray中
data.Add(new JObject
{
["name"] = "项1",
["value"] = "值1"
});
return new JObject
{
["success"] = true,
["items"] = data
};
}
}
}
扩展BaseCommandHandler创建新的处理器:
import { IMcpToolDefinition } from "../core/interfaces/ICommandHandler.js";
import { JObject } from "../types/index.js";
import { z } from "zod";
import { BaseCommandHandler } from "../core/BaseCommandHandler.js";
export class YourCommandHandler extends BaseCommandHandler {
public get commandPrefix(): string {
return "yourprefix";
}
public get description(): string {
return "处理器描述";
}
public getToolDefinitions(): Map<string, IMcpToolDefinition> {
const tools = new Map<string, IMcpToolDefinition>();
// 定义工具
tools.set("yourprefix_yourAction", {
description: "动作描述",
parameterSchema: {
param1: z.string().describe("参数描述"),
param2: z.number().optional().describe("可选参数")
},
annotations: {
title: "工具标题",
readOnlyHint: true,
openWorldHint: false
}
});
return tools;
}
protected async executeCommand(action: string, parameters: JObject): Promise<JObject> {
// 实现命令逻辑
// 将请求转发给Unity
return await this.sendUnityRequest(
`${this.commandPrefix}.${action}`,
parameters
);
}
}
扩展BaseResourceHandler创建新的资源处理器:
import { BaseResourceHandler } from "../core/BaseResourceHandler.js";
import { JObject } from "../types/index.js";
import { URL } from "url";
export class YourResourceHandler extends BaseResourceHandler {
public get resourceName(): string {
return "yourresource";
}
public get description(): string {
return "资源描述";
}
public get resourceUriTemplate(): string {
return "unity://yourresource";
}
protected async fetchResourceData(uri: URL, parameters?: JObject): Promise<JObject> {
// 处理请求参数
const param1 = parameters?.param1 as string;
// 向Unity发送请求
const response = await this.sendUnityRequest("yourresource.get", {
param1: param1
});
if (!response.success) {
throw new Error(response.error as string || "获取资源失败");
}
// 整理响应数据并返回
return {
items: response.items || []
};
}
}
扩展BasePromptHandler创建新的提示处理器:
import { BasePromptHandler } from "../core/BasePromptHandler.js";
import { IMcpPromptDefinition } from "../core/interfaces/IPromptHandler.js";
import { z } from "zod";
export class YourPromptHandler extends BasePromptHandler {
public get promptName(): string {
return "yourprompt";
}
public get description(): string {
return "提示描述";
}
public getPromptDefinitions(): Map<string, IMcpPromptDefinition> {
const prompts = new Map<string, IMcpPromptDefinition>();
// 注册提示定义
prompts.set("analyze-component", {
description: "分析Unity组件",
template: "详细分析以下Unity组件,并提出改进建议:\n\n```csharp\n{code}\n```",
additionalProperties: {
code: z.string().describe("待分析的C#代码")
}
});
return prompts;
}
}
注意:C#侧实现IMcpCommandHandler或IMcpResourceHandler的类可以在项目中的任何位置放置,通过程序集搜索会被自动检测并注册。同样地,在TypeScript侧,只要将类放在handlers目录中,也会被自动检测。
通过Edit > Preferences > Unity MCP访问设置:
TypeScript服务器的环境变量:
MCP_HOST:Unity服务器主机(默认:127.0.0.1)MCP_PORT:Unity服务器端口(默认:27182)连接错误
处理器未注册
资源未找到
code_execute命令的处理器,由于其任意代码执行能力,在生产环境中应考虑禁用。本项目在MIT许可证下提供 - 详情请参阅许可证文件。
Shiranui-Isuzu いすず