Skip to content

Commit e09497a

Browse files
committed
Update all resources to remove the dependency of resourceRegistry
Simplefies the code readibility and functionality custom definition Fixed resource templates. Now they are working properly
1 parent db2a05d commit e09497a

26 files changed

+912
-645
lines changed

Editor/Resources/GetGameObjectResource.cs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,42 @@ public GetGameObjectResource()
2222
/// <summary>
2323
/// Fetch information about a specific GameObject
2424
/// </summary>
25-
/// <param name="parameters">Resource parameters as a JObject. Should include 'instanceId' for the GameObject to fetch</param>
25+
/// <param name="parameters">Resource parameters as a JObject. Should include 'objectPathId' which can be either an instance ID or a path</param>
2626
/// <returns>A JObject containing the GameObject data</returns>
2727
public override JObject Fetch(JObject parameters)
2828
{
2929
// Validate parameters
30-
if (parameters == null || !parameters.ContainsKey("instanceId"))
30+
if (parameters == null || !parameters.ContainsKey("objectPathId"))
3131
{
3232
return new JObject
3333
{
3434
["success"] = false,
35-
["message"] = "Missing required parameter: instanceId"
35+
["message"] = "Missing required parameter: objectPathId"
3636
};
3737
}
3838

39-
// Try to parse the instance ID
40-
if (!int.TryParse(parameters["instanceId"].ToString(), out int instanceId))
39+
string objectPathId = parameters["objectPathId"].ToString();
40+
GameObject gameObject = null;
41+
42+
// Try to parse as an instance ID first
43+
if (int.TryParse(objectPathId, out int instanceId))
4144
{
42-
return new JObject
43-
{
44-
["success"] = false,
45-
["message"] = "Invalid instanceId format. Must be an integer."
46-
};
45+
// If it's a valid integer, try to find by instance ID
46+
gameObject = EditorUtility.InstanceIDToObject(instanceId) as GameObject;
4747
}
48-
49-
// Try to find the GameObject with the given instance ID
50-
GameObject gameObject = EditorUtility.InstanceIDToObject(instanceId) as GameObject;
48+
else
49+
{
50+
// Otherwise, treat it as a path
51+
gameObject = GameObject.Find(objectPathId);
52+
}
53+
54+
// Check if the GameObject was found
5155
if (gameObject == null)
5256
{
5357
return new JObject
5458
{
5559
["success"] = false,
56-
["message"] = $"GameObject with instanceId {instanceId} not found"
60+
["message"] = $"GameObject with path '{objectPathId}' not found"
5761
};
5862
}
5963

Server/build/index.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
44
import { McpUnity } from './unity/mcpUnity.js';
55
import { Logger, LogLevel } from './utils/logger.js';
66
import { ToolRegistry } from './tools/toolRegistry.js';
7-
import { ResourceRegistry } from './resources/resourceRegistry.js';
7+
// No longer need to import from resourceRegistry
88
import { createMenuItemTool } from './tools/menuItemTool.js';
99
import { createSelectGameObjectTool } from './tools/selectGameObjectTool.js';
1010
import { createAddPackageTool } from './tools/addPackageTool.js';
@@ -35,27 +35,25 @@ const server = new McpServer({
3535
});
3636
// Initialize MCP HTTP bridge with Unity editor
3737
const mcpUnity = new McpUnity(unityLogger);
38-
// Initialize the registries
38+
// Initialize the tool registry
3939
const toolRegistry = new ToolRegistry(toolLogger);
40-
const resourceRegistry = new ResourceRegistry(resourceLogger);
4140
// Add all tools to the registry
4241
toolRegistry.add(createMenuItemTool(mcpUnity, toolLogger));
4342
toolRegistry.add(createSelectGameObjectTool(mcpUnity, toolLogger));
4443
toolRegistry.add(createAddPackageTool(mcpUnity, toolLogger));
4544
toolRegistry.add(createRunTestsTool(mcpUnity, toolLogger));
4645
toolRegistry.add(createNotifyMessageTool(mcpUnity, toolLogger));
4746
toolRegistry.add(createUpdateComponentTool(mcpUnity, toolLogger));
48-
// Add all resources to the registry
49-
resourceRegistry.add(createGetMenuItemsResource(mcpUnity, resourceLogger));
50-
resourceRegistry.add(createGetConsoleLogsResource(mcpUnity, resourceLogger));
51-
resourceRegistry.add(createGetHierarchyResource(mcpUnity, resourceLogger));
52-
resourceRegistry.add(createGetPackagesResource(mcpUnity, resourceLogger));
53-
resourceRegistry.add(createGetAssetsResource(mcpUnity, resourceLogger));
54-
resourceRegistry.add(createGetTestsResource(mcpUnity, resourceLogger));
55-
resourceRegistry.add(createGetGameObjectResource(mcpUnity, resourceLogger));
56-
// Register all tools and resources with the MCP server
47+
// Create and register all resources with the MCP server
48+
createGetTestsResource(server, mcpUnity, resourceLogger);
49+
createGetGameObjectResource(server, mcpUnity, resourceLogger);
50+
createGetMenuItemsResource(server, mcpUnity, resourceLogger);
51+
createGetConsoleLogsResource(server, mcpUnity, resourceLogger);
52+
createGetHierarchyResource(server, mcpUnity, resourceLogger);
53+
createGetPackagesResource(server, mcpUnity, resourceLogger);
54+
createGetAssetsResource(server, mcpUnity, resourceLogger);
55+
// Register all tools with the MCP server
5756
toolRegistry.registerWithServer(server);
58-
resourceRegistry.registerWithServer(server);
5957
// Server startup function
6058
async function startServer() {
6159
try {
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
import { McpUnity } from '../unity/mcpUnity.js';
22
import { Logger } from '../utils/logger.js';
3-
import { ResourceDefinition } from './resourceRegistry.js';
4-
export declare function createGetAssetsResource(mcpUnity: McpUnity, logger: Logger): ResourceDefinition;
3+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
4+
/**
5+
* Creates and registers the Assets resource with the MCP server
6+
* This resource provides access to assets in the Unity project
7+
*
8+
* @param server The MCP server instance to register with
9+
* @param mcpUnity The McpUnity instance to communicate with Unity
10+
* @param logger The logger instance for diagnostic information
11+
*/
12+
export declare function createGetAssetsResource(server: McpServer, mcpUnity: McpUnity, logger: Logger): void;
Lines changed: 63 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,68 @@
11
import { McpUnityError, ErrorType } from '../utils/errors.js';
2-
export function createGetAssetsResource(mcpUnity, logger) {
3-
const resourceName = 'get_assets';
4-
const resourceUri = `unity://assets`;
5-
const resourceMimeType = 'application/json';
2+
// Constants for the resource
3+
const resourceName = 'get_assets';
4+
const resourceUri = 'unity://assets';
5+
const resourceMimeType = 'application/json';
6+
/**
7+
* Creates and registers the Assets resource with the MCP server
8+
* This resource provides access to assets in the Unity project
9+
*
10+
* @param server The MCP server instance to register with
11+
* @param mcpUnity The McpUnity instance to communicate with Unity
12+
* @param logger The logger instance for diagnostic information
13+
*/
14+
export function createGetAssetsResource(server, mcpUnity, logger) {
15+
logger.info(`Registering resource: ${resourceName}`);
16+
// Register this resource with the MCP server
17+
server.resource(resourceName, resourceUri, {
18+
description: 'Retrieve assets from the Unity Asset Database',
19+
mimeType: resourceMimeType
20+
}, async () => {
21+
try {
22+
return await resourceHandler(mcpUnity);
23+
}
24+
catch (error) {
25+
logger.error(`Error handling resource ${resourceName}: ${error}`);
26+
throw error;
27+
}
28+
});
29+
}
30+
/**
31+
* Handles requests for asset information from Unity's Asset Database
32+
*
33+
* @param mcpUnity The McpUnity instance to communicate with Unity
34+
* @returns A promise that resolves to the assets data
35+
* @throws McpUnityError if the request to Unity fails
36+
*/
37+
async function resourceHandler(mcpUnity) {
38+
// Since we're using a non-templated ResourceDefinition, we need to handle all assets without parameters
39+
const response = await mcpUnity.sendRequest({
40+
method: resourceName,
41+
params: {}
42+
});
43+
if (!response.success) {
44+
throw new McpUnityError(ErrorType.RESOURCE_FETCH, response.message || 'Failed to fetch assets from Unity Asset Database');
45+
}
46+
// Transform the data into a structured format
47+
const assets = response.assets || [];
48+
const assetsData = {
49+
assets: assets.map((asset) => ({
50+
name: asset.name,
51+
filename: asset.filename,
52+
path: asset.path,
53+
type: asset.type,
54+
extension: asset.extension,
55+
guid: asset.guid,
56+
size: asset.size
57+
}))
58+
};
659
return {
7-
name: resourceName,
8-
uri: resourceUri,
9-
metadata: {
10-
description: 'Retrieve assets from the Unity Asset Database',
11-
mimeType: resourceMimeType
12-
},
13-
handler: async (params) => {
14-
const assetType = params?.assetType;
15-
const searchPattern = params?.searchPattern;
16-
const response = await mcpUnity.sendRequest({
17-
method: resourceName,
18-
params: {
19-
assetType,
20-
searchPattern
21-
}
22-
});
23-
if (!response.success) {
24-
throw new McpUnityError(ErrorType.RESOURCE_FETCH, response.message || 'Failed to fetch assets from Unity Asset Database');
60+
contents: [
61+
{
62+
uri: resourceUri,
63+
mimeType: resourceMimeType,
64+
text: JSON.stringify(assetsData, null, 2)
2565
}
26-
// Transform the data into a structured format
27-
const assets = response.assets || [];
28-
const assetsData = {
29-
assets: assets.map((asset) => ({
30-
name: asset.name,
31-
filename: asset.filename,
32-
path: asset.path,
33-
type: asset.type,
34-
extension: asset.extension,
35-
guid: asset.guid,
36-
size: asset.size
37-
}))
38-
};
39-
return {
40-
contents: [
41-
{
42-
uri: resourceUri,
43-
mimeType: resourceMimeType,
44-
text: JSON.stringify(assetsData, null, 2)
45-
}
46-
]
47-
};
48-
}
66+
]
4967
};
5068
}
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
import { McpUnity } from '../unity/mcpUnity.js';
22
import { Logger } from '../utils/logger.js';
3-
import { ResourceDefinition } from './resourceRegistry.js';
4-
export declare function createGetConsoleLogsResource(mcpUnity: McpUnity, logger: Logger): ResourceDefinition;
3+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
4+
/**
5+
* Creates and registers the Console Logs resource with the MCP server
6+
* This resource provides access to logs from the Unity console
7+
*
8+
* @param server The MCP server instance to register with
9+
* @param mcpUnity The McpUnity instance to communicate with Unity
10+
* @param logger The logger instance for diagnostic information
11+
*/
12+
export declare function createGetConsoleLogsResource(server: McpServer, mcpUnity: McpUnity, logger: Logger): void;
Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,52 @@
11
import { McpUnityError, ErrorType } from '../utils/errors.js';
2-
export function createGetConsoleLogsResource(mcpUnity, logger) {
3-
const resourceName = 'get_console_logs';
4-
const resourceUri = `logs://console`;
5-
const resourceMimeType = 'application/json';
6-
return {
7-
name: resourceName,
8-
uri: resourceUri,
9-
metadata: {
10-
description: 'Retrieve all logs from the Unity console',
11-
mimeType: resourceMimeType
12-
},
13-
handler: async (params) => {
14-
const response = await mcpUnity.sendRequest({
15-
method: resourceName,
16-
params: {}
17-
});
18-
if (!response.success) {
19-
throw new McpUnityError(ErrorType.RESOURCE_FETCH, response.message || 'Failed to fetch console logs from Unity');
20-
}
21-
return {
22-
contents: [{
23-
uri: resourceUri,
24-
mimeType: resourceMimeType,
25-
text: JSON.stringify(response.logs, null, 2)
26-
}]
27-
};
2+
// Constants for the resource
3+
const resourceName = 'get_console_logs';
4+
const resourceUri = 'logs://console';
5+
const resourceMimeType = 'application/json';
6+
/**
7+
* Creates and registers the Console Logs resource with the MCP server
8+
* This resource provides access to logs from the Unity console
9+
*
10+
* @param server The MCP server instance to register with
11+
* @param mcpUnity The McpUnity instance to communicate with Unity
12+
* @param logger The logger instance for diagnostic information
13+
*/
14+
export function createGetConsoleLogsResource(server, mcpUnity, logger) {
15+
logger.info(`Registering resource: ${resourceName}`);
16+
// Register this resource with the MCP server
17+
server.resource(resourceName, resourceUri, {
18+
description: 'Retrieve all logs from the Unity console',
19+
mimeType: resourceMimeType
20+
}, async () => {
21+
try {
22+
return await resourceHandler(mcpUnity);
23+
}
24+
catch (error) {
25+
logger.error(`Error handling resource ${resourceName}: ${error}`);
26+
throw error;
2827
}
28+
});
29+
}
30+
/**
31+
* Handles requests for console log information from Unity
32+
*
33+
* @param mcpUnity The McpUnity instance to communicate with Unity
34+
* @returns A promise that resolves to the console logs data
35+
* @throws McpUnityError if the request to Unity fails
36+
*/
37+
async function resourceHandler(mcpUnity) {
38+
const response = await mcpUnity.sendRequest({
39+
method: resourceName,
40+
params: {}
41+
});
42+
if (!response.success) {
43+
throw new McpUnityError(ErrorType.RESOURCE_FETCH, response.message || 'Failed to fetch console logs from Unity');
44+
}
45+
return {
46+
contents: [{
47+
uri: resourceUri,
48+
mimeType: resourceMimeType,
49+
text: JSON.stringify(response.logs, null, 2)
50+
}]
2951
};
3052
}
Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,12 @@
1-
import { z } from 'zod';
21
import { Logger } from '../utils/logger.js';
2+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
33
import { McpUnity } from '../unity/mcpUnity.js';
4-
import { ResourceDefinition } from './resourceRegistry.js';
5-
export declare const GameObjectParamsSchema: z.ZodEffects<z.ZodObject<{
6-
instanceId: z.ZodOptional<z.ZodNumber>;
7-
objectPath: z.ZodOptional<z.ZodString>;
8-
}, "strip", z.ZodTypeAny, {
9-
objectPath?: string | undefined;
10-
instanceId?: number | undefined;
11-
}, {
12-
objectPath?: string | undefined;
13-
instanceId?: number | undefined;
14-
}>, {
15-
objectPath?: string | undefined;
16-
instanceId?: number | undefined;
17-
}, {
18-
objectPath?: string | undefined;
19-
instanceId?: number | undefined;
20-
}>;
21-
export type GameObjectParams = z.infer<typeof GameObjectParamsSchema>;
22-
export declare function createGetGameObjectResource(mcpUnity: McpUnity, logger: Logger): ResourceDefinition;
4+
/**
5+
* Creates and registers the GameObject resource with the MCP server
6+
* This resource provides access to GameObjects in Unity scenes
7+
*
8+
* @param server The MCP server instance to register with
9+
* @param mcpUnity The McpUnity instance to communicate with Unity
10+
* @param logger The logger instance for diagnostic information
11+
*/
12+
export declare function createGetGameObjectResource(server: McpServer, mcpUnity: McpUnity, logger: Logger): void;

0 commit comments

Comments
 (0)