From 621d054f71047f1e2f815f22964864ab75d72100 Mon Sep 17 00:00:00 2001 From: twilight07 Date: Tue, 20 May 2025 21:51:15 +0900 Subject: [PATCH 1/2] feat(server): add get_console_logs tool to MCP Server Tools - Added getConsoleLogsTool.ts to provide console logs access through tools interface - Reused existing Unity-side implementation to minimize code changes - Maintained the same functionality as unity://logs resource --- Server~/build/index.js | 2 + Server~/build/tools/getConsoleLogsTool.d.ts | 12 ++++ Server~/build/tools/getConsoleLogsTool.js | 60 ++++++++++++++++ Server~/src/index.ts | 2 + Server~/src/tools/getConsoleLogsTool.ts | 78 +++++++++++++++++++++ 5 files changed, 154 insertions(+) create mode 100644 Server~/build/tools/getConsoleLogsTool.d.ts create mode 100644 Server~/build/tools/getConsoleLogsTool.js create mode 100644 Server~/src/tools/getConsoleLogsTool.ts diff --git a/Server~/build/index.js b/Server~/build/index.js index 5175d0a8..cf0cc708 100644 --- a/Server~/build/index.js +++ b/Server~/build/index.js @@ -8,6 +8,7 @@ import { registerSelectGameObjectTool } from './tools/selectGameObjectTool.js'; import { registerAddPackageTool } from './tools/addPackageTool.js'; import { registerRunTestsTool } from './tools/runTestsTool.js'; import { registerSendConsoleLogTool } from './tools/sendConsoleLogTool.js'; +import { registerGetConsoleLogsTool } from './tools/getConsoleLogsTool.js'; import { registerUpdateComponentTool } from './tools/updateComponentTool.js'; import { registerAddAssetToSceneTool } from './tools/addAssetToSceneTool.js'; import { registerUpdateGameObjectTool } from './tools/updateGameObjectTool.js'; @@ -43,6 +44,7 @@ registerSelectGameObjectTool(server, mcpUnity, toolLogger); registerAddPackageTool(server, mcpUnity, toolLogger); registerRunTestsTool(server, mcpUnity, toolLogger); registerSendConsoleLogTool(server, mcpUnity, toolLogger); +registerGetConsoleLogsTool(server, mcpUnity, toolLogger); registerUpdateComponentTool(server, mcpUnity, toolLogger); registerAddAssetToSceneTool(server, mcpUnity, toolLogger); registerUpdateGameObjectTool(server, mcpUnity, toolLogger); diff --git a/Server~/build/tools/getConsoleLogsTool.d.ts b/Server~/build/tools/getConsoleLogsTool.d.ts new file mode 100644 index 00000000..76d13b64 --- /dev/null +++ b/Server~/build/tools/getConsoleLogsTool.d.ts @@ -0,0 +1,12 @@ +import { Logger } from '../utils/logger.js'; +import { McpUnity } from '../unity/mcpUnity.js'; +import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; +/** + * Creates and registers the Get Console Logs tool with the MCP server + * This tool allows retrieving messages from the Unity console + * + * @param server The MCP server instance to register with + * @param mcpUnity The McpUnity instance to communicate with Unity + * @param logger The logger instance for diagnostic information + */ +export declare function registerGetConsoleLogsTool(server: McpServer, mcpUnity: McpUnity, logger: Logger): void; diff --git a/Server~/build/tools/getConsoleLogsTool.js b/Server~/build/tools/getConsoleLogsTool.js new file mode 100644 index 00000000..bfdcd88a --- /dev/null +++ b/Server~/build/tools/getConsoleLogsTool.js @@ -0,0 +1,60 @@ +import * as z from 'zod'; +import { McpUnityError, ErrorType } from '../utils/errors.js'; +// Constants for the tool +const toolName = 'get_console_logs'; +const toolDescription = 'Retrieves logs from the Unity console'; +const paramsSchema = z.object({ + logType: z.string().optional().describe('The type of logs to retrieve (info, warning, error) - defaults to all logs if not specified') +}); +/** + * Creates and registers the Get Console Logs tool with the MCP server + * This tool allows retrieving messages from the Unity console + * + * @param server The MCP server instance to register with + * @param mcpUnity The McpUnity instance to communicate with Unity + * @param logger The logger instance for diagnostic information + */ +export function registerGetConsoleLogsTool(server, mcpUnity, logger) { + logger.info(`Registering tool: ${toolName}`); + // Register this tool with the MCP server + server.tool(toolName, toolDescription, paramsSchema.shape, async (params) => { + try { + logger.info(`Executing tool: ${toolName}`, params); + const result = await toolHandler(mcpUnity, params); + logger.info(`Tool execution successful: ${toolName}`); + return result; + } + catch (error) { + logger.error(`Tool execution failed: ${toolName}`, error); + throw error; + } + }); +} +/** + * Handles requests for Unity console logs + * + * @param mcpUnity The McpUnity instance to communicate with Unity + * @param params The parameters for the tool + * @returns A promise that resolves to the tool execution result + * @throws McpUnityError if the request to Unity fails + */ +async function toolHandler(mcpUnity, params) { + const { logType } = params; + // Send request to Unity using the same method name as the resource + // This allows reusing the existing Unity-side implementation + const response = await mcpUnity.sendRequest({ + method: 'get_console_logs', + params: { + logType: logType + } + }); + if (!response.success) { + throw new McpUnityError(ErrorType.TOOL_EXECUTION, response.message || 'Failed to fetch logs from Unity'); + } + return { + content: [{ + type: 'text', + text: JSON.stringify(response, null, 2) + }] + }; +} diff --git a/Server~/src/index.ts b/Server~/src/index.ts index 72dbfe65..d8d67e10 100644 --- a/Server~/src/index.ts +++ b/Server~/src/index.ts @@ -8,6 +8,7 @@ import { registerSelectGameObjectTool } from './tools/selectGameObjectTool.js'; import { registerAddPackageTool } from './tools/addPackageTool.js'; import { registerRunTestsTool } from './tools/runTestsTool.js'; import { registerSendConsoleLogTool } from './tools/sendConsoleLogTool.js'; +import { registerGetConsoleLogsTool } from './tools/getConsoleLogsTool.js'; import { registerUpdateComponentTool } from './tools/updateComponentTool.js'; import { registerAddAssetToSceneTool } from './tools/addAssetToSceneTool.js'; import { registerUpdateGameObjectTool } from './tools/updateGameObjectTool.js'; @@ -50,6 +51,7 @@ registerSelectGameObjectTool(server, mcpUnity, toolLogger); registerAddPackageTool(server, mcpUnity, toolLogger); registerRunTestsTool(server, mcpUnity, toolLogger); registerSendConsoleLogTool(server, mcpUnity, toolLogger); +registerGetConsoleLogsTool(server, mcpUnity, toolLogger); registerUpdateComponentTool(server, mcpUnity, toolLogger); registerAddAssetToSceneTool(server, mcpUnity, toolLogger); registerUpdateGameObjectTool(server, mcpUnity, toolLogger); diff --git a/Server~/src/tools/getConsoleLogsTool.ts b/Server~/src/tools/getConsoleLogsTool.ts new file mode 100644 index 00000000..ad03bc1c --- /dev/null +++ b/Server~/src/tools/getConsoleLogsTool.ts @@ -0,0 +1,78 @@ +import * as z from 'zod'; +import { Logger } from '../utils/logger.js'; +import { McpUnity } from '../unity/mcpUnity.js'; +import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; +import { McpUnityError, ErrorType } from '../utils/errors.js'; +import { CallToolResult } from '@modelcontextprotocol/sdk/types.js'; + +// Constants for the tool +const toolName = 'get_console_logs'; +const toolDescription = 'Retrieves logs from the Unity console'; +const paramsSchema = z.object({ + logType: z.string().optional().describe('The type of logs to retrieve (info, warning, error) - defaults to all logs if not specified') +}); + +/** + * Creates and registers the Get Console Logs tool with the MCP server + * This tool allows retrieving messages from the Unity console + * + * @param server The MCP server instance to register with + * @param mcpUnity The McpUnity instance to communicate with Unity + * @param logger The logger instance for diagnostic information + */ +export function registerGetConsoleLogsTool(server: McpServer, mcpUnity: McpUnity, logger: Logger) { + logger.info(`Registering tool: ${toolName}`); + + // Register this tool with the MCP server + server.tool( + toolName, + toolDescription, + paramsSchema.shape, + async (params: any) => { + try { + logger.info(`Executing tool: ${toolName}`, params); + const result = await toolHandler(mcpUnity, params); + logger.info(`Tool execution successful: ${toolName}`); + return result; + } catch (error) { + logger.error(`Tool execution failed: ${toolName}`, error); + throw error; + } + } + ); +} + +/** + * Handles requests for Unity console logs + * + * @param mcpUnity The McpUnity instance to communicate with Unity + * @param params The parameters for the tool + * @returns A promise that resolves to the tool execution result + * @throws McpUnityError if the request to Unity fails + */ +async function toolHandler(mcpUnity: McpUnity, params: any): Promise { + const { logType } = params; + + // Send request to Unity using the same method name as the resource + // This allows reusing the existing Unity-side implementation + const response = await mcpUnity.sendRequest({ + method: 'get_console_logs', + params: { + logType: logType + } + }); + + if (!response.success) { + throw new McpUnityError( + ErrorType.TOOL_EXECUTION, + response.message || 'Failed to fetch logs from Unity' + ); + } + + return { + content: [{ + type: 'text', + text: JSON.stringify(response, null, 2) + }] + }; +} From 2331c6fe356bdc5c3e042002c7c0307a51eb7d32 Mon Sep 17 00:00:00 2001 From: twilight07 Date: Tue, 20 May 2025 23:27:50 +0900 Subject: [PATCH 2/2] fix: address issues flagged by coderabbitai review --- Server~/build/tools/getConsoleLogsTool.d.ts | 6 +- Server~/build/tools/getConsoleLogsTool.js | 31 +++++---- Server~/src/tools/getConsoleLogsTool.ts | 70 +++++++++++++-------- 3 files changed, 65 insertions(+), 42 deletions(-) diff --git a/Server~/build/tools/getConsoleLogsTool.d.ts b/Server~/build/tools/getConsoleLogsTool.d.ts index 76d13b64..dcd84de4 100644 --- a/Server~/build/tools/getConsoleLogsTool.d.ts +++ b/Server~/build/tools/getConsoleLogsTool.d.ts @@ -1,6 +1,6 @@ -import { Logger } from '../utils/logger.js'; -import { McpUnity } from '../unity/mcpUnity.js'; -import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; +import { Logger } from "../utils/logger.js"; +import { McpUnity } from "../unity/mcpUnity.js"; +import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; /** * Creates and registers the Get Console Logs tool with the MCP server * This tool allows retrieving messages from the Unity console diff --git a/Server~/build/tools/getConsoleLogsTool.js b/Server~/build/tools/getConsoleLogsTool.js index bfdcd88a..7cff61fc 100644 --- a/Server~/build/tools/getConsoleLogsTool.js +++ b/Server~/build/tools/getConsoleLogsTool.js @@ -1,10 +1,13 @@ -import * as z from 'zod'; -import { McpUnityError, ErrorType } from '../utils/errors.js'; +import * as z from "zod"; +import { McpUnityError, ErrorType } from "../utils/errors.js"; // Constants for the tool -const toolName = 'get_console_logs'; -const toolDescription = 'Retrieves logs from the Unity console'; +const toolName = "get_console_logs"; +const toolDescription = "Retrieves logs from the Unity console"; const paramsSchema = z.object({ - logType: z.string().optional().describe('The type of logs to retrieve (info, warning, error) - defaults to all logs if not specified') + logType: z + .enum(["info", "warning", "error"]) + .optional() + .describe("The type of logs to retrieve (info, warning, error) - defaults to all logs if not specified"), }); /** * Creates and registers the Get Console Logs tool with the MCP server @@ -43,18 +46,20 @@ async function toolHandler(mcpUnity, params) { // Send request to Unity using the same method name as the resource // This allows reusing the existing Unity-side implementation const response = await mcpUnity.sendRequest({ - method: 'get_console_logs', + method: "get_console_logs", params: { - logType: logType - } + logType: logType, + }, }); if (!response.success) { - throw new McpUnityError(ErrorType.TOOL_EXECUTION, response.message || 'Failed to fetch logs from Unity'); + throw new McpUnityError(ErrorType.TOOL_EXECUTION, response.message || "Failed to fetch logs from Unity"); } return { - content: [{ - type: 'text', - text: JSON.stringify(response, null, 2) - }] + content: [ + { + type: "text", + text: JSON.stringify(response.data || response.logs || response, null, 2), + }, + ], }; } diff --git a/Server~/src/tools/getConsoleLogsTool.ts b/Server~/src/tools/getConsoleLogsTool.ts index ad03bc1c..a521aed4 100644 --- a/Server~/src/tools/getConsoleLogsTool.ts +++ b/Server~/src/tools/getConsoleLogsTool.ts @@ -1,34 +1,43 @@ -import * as z from 'zod'; -import { Logger } from '../utils/logger.js'; -import { McpUnity } from '../unity/mcpUnity.js'; -import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; -import { McpUnityError, ErrorType } from '../utils/errors.js'; -import { CallToolResult } from '@modelcontextprotocol/sdk/types.js'; +import * as z from "zod"; +import { Logger } from "../utils/logger.js"; +import { McpUnity } from "../unity/mcpUnity.js"; +import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; +import { McpUnityError, ErrorType } from "../utils/errors.js"; +import { CallToolResult } from "@modelcontextprotocol/sdk/types.js"; // Constants for the tool -const toolName = 'get_console_logs'; -const toolDescription = 'Retrieves logs from the Unity console'; +const toolName = "get_console_logs"; +const toolDescription = "Retrieves logs from the Unity console"; const paramsSchema = z.object({ - logType: z.string().optional().describe('The type of logs to retrieve (info, warning, error) - defaults to all logs if not specified') + logType: z + .enum(["info", "warning", "error"]) + .optional() + .describe( + "The type of logs to retrieve (info, warning, error) - defaults to all logs if not specified" + ), }); /** * Creates and registers the Get Console Logs tool with the MCP server * This tool allows retrieving messages from the Unity console - * + * * @param server The MCP server instance to register with * @param mcpUnity The McpUnity instance to communicate with Unity * @param logger The logger instance for diagnostic information */ -export function registerGetConsoleLogsTool(server: McpServer, mcpUnity: McpUnity, logger: Logger) { +export function registerGetConsoleLogsTool( + server: McpServer, + mcpUnity: McpUnity, + logger: Logger +) { logger.info(`Registering tool: ${toolName}`); - + // Register this tool with the MCP server server.tool( toolName, toolDescription, paramsSchema.shape, - async (params: any) => { + async (params: z.infer) => { try { logger.info(`Executing tool: ${toolName}`, params); const result = await toolHandler(mcpUnity, params); @@ -44,35 +53,44 @@ export function registerGetConsoleLogsTool(server: McpServer, mcpUnity: McpUnity /** * Handles requests for Unity console logs - * + * * @param mcpUnity The McpUnity instance to communicate with Unity * @param params The parameters for the tool * @returns A promise that resolves to the tool execution result * @throws McpUnityError if the request to Unity fails */ -async function toolHandler(mcpUnity: McpUnity, params: any): Promise { +async function toolHandler( + mcpUnity: McpUnity, + params: z.infer +): Promise { const { logType } = params; - + // Send request to Unity using the same method name as the resource // This allows reusing the existing Unity-side implementation const response = await mcpUnity.sendRequest({ - method: 'get_console_logs', + method: "get_console_logs", params: { - logType: logType - } + logType: logType, + }, }); - + if (!response.success) { throw new McpUnityError( ErrorType.TOOL_EXECUTION, - response.message || 'Failed to fetch logs from Unity' + response.message || "Failed to fetch logs from Unity" ); } - + return { - content: [{ - type: 'text', - text: JSON.stringify(response, null, 2) - }] + content: [ + { + type: "text", + text: JSON.stringify( + response.data || response.logs || response, + null, + 2 + ), + }, + ], }; }