Skip to content

Commit e65f5ae

Browse files
committed
Fixed issue where resources would provide the wrong output to the MCP host
Removed toolRegistry and made all tools follow the similar pattern as resources. Simplefied the code readability Fixed issue preventing tools and resources to work if they had white spaces in them
1 parent e09497a commit e65f5ae

34 files changed

+961
-661
lines changed

Editor/Resources/GetGameObjectResource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public override JObject Fetch(JObject parameters)
3636
};
3737
}
3838

39-
string objectPathId = parameters["objectPathId"].ToString();
39+
string objectPathId = parameters["objectPathId"]?.ToObject<string>();
4040
GameObject gameObject = null;
4141

4242
// Try to parse as an instance ID first

Editor/Tools/AddPackageTool.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,8 @@ private void ProcessCompletedOperation(PackageOperation operation)
302302
operation.CompletionSource.SetResult(new JObject
303303
{
304304
["success"] = true,
305-
["message"] = $"Successfully added package: {result.displayName} ({result.name}) version {result.version}",
306305
["type"] = "text",
306+
["message"] = $"Successfully added package: {result.displayName} ({result.name}) version {result.version}",
307307
["packageInfo"] = JObject.FromObject(new
308308
{
309309
name = result.name,
@@ -317,6 +317,7 @@ private void ProcessCompletedOperation(PackageOperation operation)
317317
operation.CompletionSource.SetResult(new JObject
318318
{
319319
["success"] = true,
320+
["type"] = "text",
320321
["message"] = $"Package operation completed successfully, but no package information was returned."
321322
});
322323
}

Editor/Tools/MenuItemTool.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public override JObject Execute(JObject parameters)
4444
return new JObject
4545
{
4646
["success"] = success,
47+
["type"] = "text",
4748
["message"] = success
4849
? $"Successfully executed menu item: {menuPath}"
4950
: $"Failed to execute menu item: {menuPath}"

Editor/Tools/NotifyMessageTool.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public override JObject Execute(JObject parameters)
5252
return new JObject
5353
{
5454
["success"] = true,
55-
["message"] = $"Message displayed: {message}",
56-
["type"] = "text"
55+
["type"] = "text",
56+
["message"] = $"Message displayed: {message}"
5757
};
5858
}
5959
}

Editor/Tools/RunTestsTool.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,12 @@ public void RunFinished(ITestResultAdaptor result)
161161
// Set the test run completion result
162162
try
163163
{
164-
_testRunCompletionSource.SetResult(summary);
164+
_testRunCompletionSource.SetResult(new JObject
165+
{
166+
["success"] = true,
167+
["type"] = "text",
168+
["message"] = summary["message"].Value<string>()
169+
});
165170
}
166171
catch (Exception ex)
167172
{

Editor/Tools/SelectGameObjectTool.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public override JObject Execute(JObject parameters)
6060
return new JObject
6161
{
6262
["success"] = true,
63+
["type"] = "text",
6364
["message"] = $"Successfully selected GameObject" +
6465
(instanceId.HasValue ? $" with instance ID: {instanceId.Value}" : $": {objectPath}")
6566
};

Editor/Tools/UpdateComponentTool.cs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,6 @@ public override JObject Execute(JObject parameters)
5757
{
5858
gameObject = EditorUtility.InstanceIDToObject(instanceId.Value) as GameObject;
5959
identifier = $"ID {instanceId.Value}";
60-
61-
if (gameObject == null)
62-
{
63-
return McpUnitySocketHandler.CreateErrorResponse(
64-
$"GameObject with instance ID {instanceId.Value} not found",
65-
"not_found_error"
66-
);
67-
}
6860
}
6961
else
7062
{
@@ -76,16 +68,16 @@ public override JObject Execute(JObject parameters)
7668
{
7769
// Try to find using the Unity Scene hierarchy path
7870
gameObject = FindGameObjectByPath(objectPath);
79-
80-
if (gameObject == null)
81-
{
82-
return McpUnitySocketHandler.CreateErrorResponse(
83-
$"GameObject with path '{objectPath}' not found",
84-
"not_found_error"
85-
);
86-
}
8771
}
8872
}
73+
74+
if (gameObject == null)
75+
{
76+
return McpUnitySocketHandler.CreateErrorResponse(
77+
$"GameObject with path '{objectPath}' or instance ID {instanceId} not found",
78+
"not_found_error"
79+
);
80+
}
8981

9082
Debug.Log($"[MCP Unity] Updating component '{componentName}' on GameObject '{gameObject.name}' (found by {identifier})");
9183

@@ -127,10 +119,10 @@ public override JObject Execute(JObject parameters)
127119
return new JObject
128120
{
129121
["success"] = true,
122+
["type"] = "text",
130123
["message"] = wasAdded
131124
? $"Successfully added component '{componentName}' to GameObject '{gameObject.name}' and updated its data"
132-
: $"Successfully updated component '{componentName}' on GameObject '{gameObject.name}'",
133-
["type"] = "text"
125+
: $"Successfully updated component '{componentName}' on GameObject '{gameObject.name}'"
134126
};
135127
}
136128

Server/build/index.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
33
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
44
import { McpUnity } from './unity/mcpUnity.js';
55
import { Logger, LogLevel } from './utils/logger.js';
6-
import { ToolRegistry } from './tools/toolRegistry.js';
7-
// No longer need to import from resourceRegistry
86
import { createMenuItemTool } from './tools/menuItemTool.js';
97
import { createSelectGameObjectTool } from './tools/selectGameObjectTool.js';
108
import { createAddPackageTool } from './tools/addPackageTool.js';
@@ -35,15 +33,13 @@ const server = new McpServer({
3533
});
3634
// Initialize MCP HTTP bridge with Unity editor
3735
const mcpUnity = new McpUnity(unityLogger);
38-
// Initialize the tool registry
39-
const toolRegistry = new ToolRegistry(toolLogger);
4036
// Add all tools to the registry
41-
toolRegistry.add(createMenuItemTool(mcpUnity, toolLogger));
42-
toolRegistry.add(createSelectGameObjectTool(mcpUnity, toolLogger));
43-
toolRegistry.add(createAddPackageTool(mcpUnity, toolLogger));
44-
toolRegistry.add(createRunTestsTool(mcpUnity, toolLogger));
45-
toolRegistry.add(createNotifyMessageTool(mcpUnity, toolLogger));
46-
toolRegistry.add(createUpdateComponentTool(mcpUnity, toolLogger));
37+
createMenuItemTool(server, mcpUnity, toolLogger);
38+
createSelectGameObjectTool(server, mcpUnity, toolLogger);
39+
createAddPackageTool(server, mcpUnity, toolLogger);
40+
createRunTestsTool(server, mcpUnity, toolLogger);
41+
createNotifyMessageTool(server, mcpUnity, toolLogger);
42+
createUpdateComponentTool(server, mcpUnity, toolLogger);
4743
// Create and register all resources with the MCP server
4844
createGetTestsResource(server, mcpUnity, resourceLogger);
4945
createGetGameObjectResource(server, mcpUnity, resourceLogger);
@@ -52,8 +48,6 @@ createGetConsoleLogsResource(server, mcpUnity, resourceLogger);
5248
createGetHierarchyResource(server, mcpUnity, resourceLogger);
5349
createGetPackagesResource(server, mcpUnity, resourceLogger);
5450
createGetAssetsResource(server, mcpUnity, resourceLogger);
55-
// Register all tools with the MCP server
56-
toolRegistry.registerWithServer(server);
5751
// Server startup function
5852
async function startServer() {
5953
try {

Server/build/resources/getGameObjectResource.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ResourceTemplate } from '@modelcontextprotocol/sdk/server/mcp.js';
22
import { McpUnityError, ErrorType } from '../utils/errors.js';
3+
import { resourceUri as hierarchyResourceUri } from './getHierarchyResource.js';
34
// Constants for the resource
45
const resourceName = 'get_gameobject';
56
const resourceUri = 'unity://gameobject/{id}';
@@ -15,7 +16,7 @@ const resourceMimeType = 'application/json';
1516
export function createGetGameObjectResource(server, mcpUnity, logger) {
1617
// Create a resource template with the MCP SDK
1718
const resourceTemplate = new ResourceTemplate(resourceUri, {
18-
list: () => listGameObjects(mcpUnity, logger, resourceMimeType)
19+
list: async () => listGameObjects(mcpUnity, logger, resourceMimeType)
1920
});
2021
logger.info(`Registering resource: ${resourceName}`);
2122
// Register this resource with the MCP server
@@ -44,7 +45,7 @@ export function createGetGameObjectResource(server, mcpUnity, logger) {
4445
*/
4546
async function resourceHandler(mcpUnity, uri, variables, logger) {
4647
// Extract and convert the parameter from the template variables
47-
const id = variables["id"];
48+
const id = decodeURIComponent(variables["id"]);
4849
// Send request to Unity
4950
const response = await mcpUnity.sendRequest({
5051
method: resourceName,
@@ -72,7 +73,7 @@ async function resourceHandler(mcpUnity, uri, variables, logger) {
7273
*/
7374
async function listGameObjects(mcpUnity, logger, resourceMimeType) {
7475
const hierarchyResponse = await mcpUnity.sendRequest({
75-
method: 'get_hierarchy',
76+
method: hierarchyResourceUri,
7677
params: {}
7778
});
7879
if (!hierarchyResponse.success) {
@@ -81,6 +82,7 @@ async function listGameObjects(mcpUnity, logger, resourceMimeType) {
8182
}
8283
// Process the hierarchy to create a list of GameObject references
8384
const gameObjects = processHierarchyToGameObjectList(hierarchyResponse.hierarchy || []);
85+
logger.info(`[getGameObjectResource] Fetched hierarchy with ${gameObjects.length} GameObjects ${hierarchyResponse.hierarchy}`);
8486
// Create resources array with both instance ID and path URIs
8587
const resources = [];
8688
// Add resources for each GameObject

Server/build/resources/getHierarchyResource.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { McpUnity } from '../unity/mcpUnity.js';
22
import { Logger } from '../utils/logger.js';
33
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
4+
export declare const resourceName = "get_hierarchy";
5+
export declare const resourceUri = "unity://hierarchy";
6+
export declare const resourceMimeType = "application/json";
47
/**
58
* Creates and registers the Hierarchy resource with the MCP server
69
* This resource provides access to the Unity scene hierarchy

0 commit comments

Comments
 (0)