|
| 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 | +} |
0 commit comments