|  | 
|  | 1 | +import * as z from 'zod'; | 
|  | 2 | +import { Logger } from '../utils/logger.js'; | 
|  | 3 | +import { McpUnity } from '../unity/mcpUnity.js'; | 
|  | 4 | +import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; | 
|  | 5 | +import { McpUnityError, ErrorType } from '../utils/errors.js'; | 
|  | 6 | +import { CallToolResult } from '@modelcontextprotocol/sdk/types.js'; | 
|  | 7 | + | 
|  | 8 | +// Constants for the tool | 
|  | 9 | +const toolName = 'get_console_logs'; | 
|  | 10 | +const toolDescription = 'Retrieves logs from the Unity console'; | 
|  | 11 | +const paramsSchema = z.object({ | 
|  | 12 | +  logType: z.string().optional().describe('The type of logs to retrieve (info, warning, error) - defaults to all logs if not specified') | 
|  | 13 | +}); | 
|  | 14 | + | 
|  | 15 | +/** | 
|  | 16 | + * Creates and registers the Get Console Logs tool with the MCP server | 
|  | 17 | + * This tool allows retrieving messages from the Unity console | 
|  | 18 | + *  | 
|  | 19 | + * @param server The MCP server instance to register with | 
|  | 20 | + * @param mcpUnity The McpUnity instance to communicate with Unity | 
|  | 21 | + * @param logger The logger instance for diagnostic information | 
|  | 22 | + */ | 
|  | 23 | +export function registerGetConsoleLogsTool(server: McpServer, mcpUnity: McpUnity, logger: Logger) { | 
|  | 24 | +  logger.info(`Registering tool: ${toolName}`); | 
|  | 25 | +   | 
|  | 26 | +  // Register this tool with the MCP server | 
|  | 27 | +  server.tool( | 
|  | 28 | +    toolName, | 
|  | 29 | +    toolDescription, | 
|  | 30 | +    paramsSchema.shape, | 
|  | 31 | +    async (params: any) => { | 
|  | 32 | +      try { | 
|  | 33 | +        logger.info(`Executing tool: ${toolName}`, params); | 
|  | 34 | +        const result = await toolHandler(mcpUnity, params); | 
|  | 35 | +        logger.info(`Tool execution successful: ${toolName}`); | 
|  | 36 | +        return result; | 
|  | 37 | +      } catch (error) { | 
|  | 38 | +        logger.error(`Tool execution failed: ${toolName}`, error); | 
|  | 39 | +        throw error; | 
|  | 40 | +      } | 
|  | 41 | +    } | 
|  | 42 | +  ); | 
|  | 43 | +} | 
|  | 44 | + | 
|  | 45 | +/** | 
|  | 46 | + * Handles requests for Unity console logs | 
|  | 47 | + *  | 
|  | 48 | + * @param mcpUnity The McpUnity instance to communicate with Unity | 
|  | 49 | + * @param params The parameters for the tool | 
|  | 50 | + * @returns A promise that resolves to the tool execution result | 
|  | 51 | + * @throws McpUnityError if the request to Unity fails | 
|  | 52 | + */ | 
|  | 53 | +async function toolHandler(mcpUnity: McpUnity, params: any): Promise<CallToolResult> { | 
|  | 54 | +  const { logType } = params; | 
|  | 55 | +   | 
|  | 56 | +  // Send request to Unity using the same method name as the resource | 
|  | 57 | +  // This allows reusing the existing Unity-side implementation | 
|  | 58 | +  const response = await mcpUnity.sendRequest({ | 
|  | 59 | +    method: 'get_console_logs', | 
|  | 60 | +    params: { | 
|  | 61 | +      logType: logType | 
|  | 62 | +    } | 
|  | 63 | +  }); | 
|  | 64 | +   | 
|  | 65 | +  if (!response.success) { | 
|  | 66 | +    throw new McpUnityError( | 
|  | 67 | +      ErrorType.TOOL_EXECUTION, | 
|  | 68 | +      response.message || 'Failed to fetch logs from Unity' | 
|  | 69 | +    ); | 
|  | 70 | +  } | 
|  | 71 | +   | 
|  | 72 | +  return { | 
|  | 73 | +    content: [{ | 
|  | 74 | +      type: 'text', | 
|  | 75 | +      text: JSON.stringify(response, null, 2) | 
|  | 76 | +    }] | 
|  | 77 | +  }; | 
|  | 78 | +} | 
0 commit comments