Skip to content

Commit 3004f95

Browse files
committed
refactor: rename hierarchy resource and update documentation
- Renamed GetHierarchyResource to GetScenesHierarchyResource - Updated resource URI from unity://hierarchy to unity://scenes-hierarchy
1 parent d66a971 commit 3004f95

18 files changed

+197
-35
lines changed

Editor/Resources/GetHierarchyResource.cs renamed to Editor/Resources/GetScenesHierarchyResource.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77
namespace McpUnity.Resources
88
{
99
/// <summary>
10-
/// Resource for retrieving all game objects in the Unity hierarchy
10+
/// Resource for retrieving all game objects in the Unity scenes hierarchy
1111
/// </summary>
12-
public class GetHierarchyResource : McpResourceBase
12+
/// <summary>
13+
/// Resource for retrieving all game objects in the Unity scenes hierarchy
14+
/// </summary>
15+
public class GetScenesHierarchyResource : McpResourceBase
1316
{
14-
public GetHierarchyResource()
17+
public GetScenesHierarchyResource()
1518
{
16-
Name = "get_hierarchy";
19+
Name = "get_scenes_hierarchy";
1720
Description = "Retrieves all game objects in the Unity loaded scenes with their active state";
18-
Uri = "unity://hierarchy";
21+
Uri = "unity://scenes_hierarchy";
1922
}
2023

2124
/// <summary>

Editor/UnityBridge/McpUnityServer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,9 @@ private void RegisterResources()
186186
GetConsoleLogsResource getConsoleLogsResource = new GetConsoleLogsResource(_consoleLogsService);
187187
_resources.Add(getConsoleLogsResource.Name, getConsoleLogsResource);
188188

189-
// Register GetHierarchyResource
190-
GetHierarchyResource getHierarchyResource = new GetHierarchyResource();
191-
_resources.Add(getHierarchyResource.Name, getHierarchyResource);
189+
// Register GetScenesHierarchyResource
190+
GetScenesHierarchyResource getScenesHierarchyResource = new GetScenesHierarchyResource();
191+
_resources.Add(getScenesHierarchyResource.Name, getScenesHierarchyResource);
192192

193193
// Register GetPackagesResource
194194
GetPackagesResource getPackagesResource = new GetPackagesResource();

README-ja.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ MCP Unityは、Unityの`Library/PackedCache`フォルダーをワークスペー
8787
- `unity://menu-items`: `execute_menu_item`ツールを容易にするために、Unityエディターで利用可能なすべてのメニュー項目のリストを取得
8888
> **例:** "GameObject作成に関連する利用可能なすべてのメニュー項目を表示"
8989
90-
- `unity://hierarchy`: Unity階層内のすべてのゲームオブジェクトのリストを取得
91-
> **例:** "現在のシーンの階層構造を表示"
90+
- `unity://scenes-hierarchy`: 現在のUnityシーン階層内のすべてのゲームオブジェクトのリストを取得
91+
> **例:** "現在のシーン階層構造を表示"
9292
9393
- `unity://gameobject/{id}`: シーン階層内のインスタンスIDまたはオブジェクトパスで特定のGameObjectに関する詳細情報を取得
9494
> **例:** "Player GameObjectに関する詳細情報を取得"

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ MCP Unity provides automatic integration with VSCode-like IDEs (Visual Studio Co
8787
- `unity://menu-items`: Retrieves a list of all available menu items in the Unity Editor to facilitate `execute_menu_item` tool
8888
> **Example prompt:** "Show me all available menu items related to GameObject creation"
8989
90-
- `unity://hierarchy`: Retrieves a list of all game objects in the Unity hierarchy
91-
> **Example prompt:** "Show me the current scene hierarchy structure"
90+
- `unity://scenes-hierarchy`: Retrieves a list of all game objects in the current Unity scene hierarchy
91+
> **Example prompt:** "Show me the current scenes hierarchy structure"
9292
9393
- `unity://gameobject/{id}`: Retrieves detailed information about a specific GameObject by instance ID or object path in the scene hierarchy, including all GameObject components with it's serialized properties and fields
9494
> **Example prompt:** "Get me detailed information about the Player GameObject"

README_zh-CN.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ MCP Unity 通过将 Unity `Library/PackedCache` 文件夹添加到您的工作
8989
- `unity://menu-items`: 获取 Unity 编辑器中所有可用的菜单项列表,以方便 `execute_menu_item` 工具
9090
> **示例提示:** "显示与 GameObject 创建相关的所有可用菜单项"
9191
92-
- `unity://hierarchy`: 获取 Unity 层次结构中所有游戏对象的列表
93-
> **示例提示:** "显示当前场景的层次结构"
92+
- `unity://scenes-hierarchy`: 获取当前 Unity 场景层次结构中所有游戏对象的列表
93+
> **示例提示:** "显示当前场景层次结构"
9494
9595
- `unity://gameobject/{id}`: 通过实例 ID 或场景层次结构中的对象路径获取特定 GameObject 的详细信息,包括所有 GameObject 组件及其序列化的属性和字段
9696
> **示例提示:** "获取 Player GameObject 的详细信息"

Server~/build/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import { registerSendConsoleLogTool } from './tools/sendConsoleLogTool.js';
1111
import { registerUpdateComponentTool } from './tools/updateComponentTool.js';
1212
import { registerAddAssetToSceneTool } from './tools/addAssetToSceneTool.js';
1313
import { registerGetMenuItemsResource } from './resources/getMenuItemResource.js';
14-
import { registerGetConsoleLogsResource } from './resources/getConsoleLogResource.js';
15-
import { registerGetHierarchyResource } from './resources/getHierarchyResource.js';
14+
import { registerGetConsoleLogsResource } from './resources/getConsoleLogsResource.js';
15+
import { registerGetHierarchyResource } from './resources/getScenesHierarchyResource.js';
1616
import { registerGetPackagesResource } from './resources/getPackagesResource.js';
1717
import { registerGetAssetsResource } from './resources/getAssetsResource.js';
1818
import { registerGetTestsResource } from './resources/getTestsResource.js';

Server~/build/prompts/gameobjectHandlingPrompt.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ export function registerGameObjectHandlingPrompt(server) {
1414
role: 'user',
1515
content: {
1616
type: 'text',
17-
text: `You are an expert AI assistant integrated with Unity via MCP. You have access to the following resources and tools:
18-
+- Resource "get_hierarchy" (unity://hierarchy) to list all GameObjects.
17+
text: `You are an expert AI assistant integrated with Unity via MCP.
18+
When working directly with GameObjects or any of it's commopenents in the Unity scenes, you have access to the following resources and tools:
19+
+- Resource "get_scenes_hierarchy" (unity://scenes_hierarchy) to list all GameObjects.
1920
+- Resource "get_gameobject" (unity://gameobject/{id}) to fetch detailed GameObject info, with the id being the name of the GameObject or the path to the GameObject.
2021
+- Tool "select_gameobject" to select a GameObject by ID or path.
2122
+- Tool "update_component" to update or add a component on a GameObject.
2223
+
2324
+Workflow:
24-
+1. Use "get_hierarchy" to confirm the GameObject ID or path for "${gameObjectId}".
25+
+1. Use "get_scenes_hierarchy" to confirm the GameObject ID or path for "${gameObjectId}".
2526
+2. Invoke "select_gameobject" to focus on the target GameObject in Unity.
2627
+3. Optionally, use "unity://gameobject/${gameObjectId}" to retrieve detailed properties.
2728
+4. Apply changes with "update_component" as per user requirements.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Logger } from '../utils/logger.js';
2+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
3+
import { McpUnity } from '../unity/mcpUnity.js';
4+
/**
5+
* Registers the get_console_logs resource with the MCP server
6+
*/
7+
export declare function registerGetConsoleLogsResource(server: McpServer, mcpUnity: McpUnity, logger: Logger): void;
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { ResourceTemplate } from '@modelcontextprotocol/sdk/server/mcp.js';
2+
import { McpUnityError, ErrorType } from '../utils/errors.js';
3+
// Constants for the resource
4+
const resourceName = 'get_console_logs';
5+
const resourceMimeType = 'application/json';
6+
const resourceUri = 'unity://logs/{logType}';
7+
const resourceTemplate = new ResourceTemplate(resourceUri, {
8+
list: () => listLogTypes(resourceMimeType)
9+
});
10+
function listLogTypes(resourceMimeType) {
11+
return {
12+
resources: [
13+
{
14+
uri: `unity://logs/`,
15+
name: "All logs",
16+
description: "Retrieve all Unity console logs",
17+
mimeType: resourceMimeType
18+
},
19+
{
20+
uri: `unity://logs/error`,
21+
name: "Error logs",
22+
description: "Retrieve only error logs from the Unity console",
23+
mimeType: resourceMimeType
24+
},
25+
{
26+
uri: `unity://logs/warning`,
27+
name: "Warning logs",
28+
description: "Retrieve only warning logs from the Unity console",
29+
mimeType: resourceMimeType
30+
},
31+
{
32+
uri: `unity://logs/info`,
33+
name: "Info logs",
34+
description: "Retrieve only info logs from the Unity console",
35+
mimeType: resourceMimeType
36+
}
37+
]
38+
};
39+
}
40+
/**
41+
* Registers the get_console_logs resource with the MCP server
42+
*/
43+
export function registerGetConsoleLogsResource(server, mcpUnity, logger) {
44+
logger.info(`Registering resource: ${resourceName}`);
45+
server.resource(resourceName, resourceTemplate, {
46+
description: 'Retrieve Unity console logs by type',
47+
mimeType: resourceMimeType
48+
}, async (uri, variables) => {
49+
try {
50+
return await resourceHandler(mcpUnity, uri, variables, logger);
51+
}
52+
catch (error) {
53+
logger.error(`Error handling resource ${resourceName}: ${error}`);
54+
throw error;
55+
}
56+
});
57+
}
58+
/**
59+
* Handles requests for Unity console logs by log type
60+
*/
61+
async function resourceHandler(mcpUnity, uri, variables, logger) {
62+
// Extract and convert the parameter from the template variables
63+
let logType = variables["logType"] ? decodeURIComponent(variables["logType"]) : undefined;
64+
if (logType === '')
65+
logType = undefined;
66+
// Send request to Unity
67+
const response = await mcpUnity.sendRequest({
68+
method: resourceName,
69+
params: {
70+
logType: logType
71+
}
72+
});
73+
if (!response.success) {
74+
throw new McpUnityError(ErrorType.RESOURCE_FETCH, response.message || 'Failed to fetch logs from Unity');
75+
}
76+
return {
77+
contents: [{
78+
uri: `unity://logs/${logType ?? ''}`,
79+
mimeType: resourceMimeType,
80+
text: JSON.stringify(response, null, 2)
81+
}]
82+
};
83+
}

Server~/build/resources/getGameObjectResource.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ResourceTemplate } from '@modelcontextprotocol/sdk/server/mcp.js';
22
import { McpUnityError, ErrorType } from '../utils/errors.js';
3-
import { resourceName as hierarchyResourceName } from './getHierarchyResource.js';
3+
import { resourceName as hierarchyResourceName } from './getScenesHierarchyResource.js';
44
// Constants for the resource
55
const resourceName = 'get_gameobject';
66
const resourceUri = 'unity://gameobject/{id}';

0 commit comments

Comments
 (0)