|
1 | 1 | 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 | + }; |
6 | 59 | 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) |
25 | 65 | } |
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 | + ] |
49 | 67 | }; |
50 | 68 | } |
0 commit comments