Skip to content

Commit 621d054

Browse files
committed
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
1 parent 6403d7b commit 621d054

File tree

5 files changed

+154
-0
lines changed

5 files changed

+154
-0
lines changed

Server~/build/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { registerSelectGameObjectTool } from './tools/selectGameObjectTool.js';
88
import { registerAddPackageTool } from './tools/addPackageTool.js';
99
import { registerRunTestsTool } from './tools/runTestsTool.js';
1010
import { registerSendConsoleLogTool } from './tools/sendConsoleLogTool.js';
11+
import { registerGetConsoleLogsTool } from './tools/getConsoleLogsTool.js';
1112
import { registerUpdateComponentTool } from './tools/updateComponentTool.js';
1213
import { registerAddAssetToSceneTool } from './tools/addAssetToSceneTool.js';
1314
import { registerUpdateGameObjectTool } from './tools/updateGameObjectTool.js';
@@ -43,6 +44,7 @@ registerSelectGameObjectTool(server, mcpUnity, toolLogger);
4344
registerAddPackageTool(server, mcpUnity, toolLogger);
4445
registerRunTestsTool(server, mcpUnity, toolLogger);
4546
registerSendConsoleLogTool(server, mcpUnity, toolLogger);
47+
registerGetConsoleLogsTool(server, mcpUnity, toolLogger);
4648
registerUpdateComponentTool(server, mcpUnity, toolLogger);
4749
registerAddAssetToSceneTool(server, mcpUnity, toolLogger);
4850
registerUpdateGameObjectTool(server, mcpUnity, toolLogger);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Logger } from '../utils/logger.js';
2+
import { McpUnity } from '../unity/mcpUnity.js';
3+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
4+
/**
5+
* Creates and registers the Get Console Logs tool with the MCP server
6+
* This tool allows retrieving messages 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 registerGetConsoleLogsTool(server: McpServer, mcpUnity: McpUnity, logger: Logger): void;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import * as z from 'zod';
2+
import { McpUnityError, ErrorType } from '../utils/errors.js';
3+
// Constants for the tool
4+
const toolName = 'get_console_logs';
5+
const toolDescription = 'Retrieves logs from the Unity console';
6+
const paramsSchema = z.object({
7+
logType: z.string().optional().describe('The type of logs to retrieve (info, warning, error) - defaults to all logs if not specified')
8+
});
9+
/**
10+
* Creates and registers the Get Console Logs tool with the MCP server
11+
* This tool allows retrieving messages from the Unity console
12+
*
13+
* @param server The MCP server instance to register with
14+
* @param mcpUnity The McpUnity instance to communicate with Unity
15+
* @param logger The logger instance for diagnostic information
16+
*/
17+
export function registerGetConsoleLogsTool(server, mcpUnity, logger) {
18+
logger.info(`Registering tool: ${toolName}`);
19+
// Register this tool with the MCP server
20+
server.tool(toolName, toolDescription, paramsSchema.shape, async (params) => {
21+
try {
22+
logger.info(`Executing tool: ${toolName}`, params);
23+
const result = await toolHandler(mcpUnity, params);
24+
logger.info(`Tool execution successful: ${toolName}`);
25+
return result;
26+
}
27+
catch (error) {
28+
logger.error(`Tool execution failed: ${toolName}`, error);
29+
throw error;
30+
}
31+
});
32+
}
33+
/**
34+
* Handles requests for Unity console logs
35+
*
36+
* @param mcpUnity The McpUnity instance to communicate with Unity
37+
* @param params The parameters for the tool
38+
* @returns A promise that resolves to the tool execution result
39+
* @throws McpUnityError if the request to Unity fails
40+
*/
41+
async function toolHandler(mcpUnity, params) {
42+
const { logType } = params;
43+
// Send request to Unity using the same method name as the resource
44+
// This allows reusing the existing Unity-side implementation
45+
const response = await mcpUnity.sendRequest({
46+
method: 'get_console_logs',
47+
params: {
48+
logType: logType
49+
}
50+
});
51+
if (!response.success) {
52+
throw new McpUnityError(ErrorType.TOOL_EXECUTION, response.message || 'Failed to fetch logs from Unity');
53+
}
54+
return {
55+
content: [{
56+
type: 'text',
57+
text: JSON.stringify(response, null, 2)
58+
}]
59+
};
60+
}

Server~/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { registerSelectGameObjectTool } from './tools/selectGameObjectTool.js';
88
import { registerAddPackageTool } from './tools/addPackageTool.js';
99
import { registerRunTestsTool } from './tools/runTestsTool.js';
1010
import { registerSendConsoleLogTool } from './tools/sendConsoleLogTool.js';
11+
import { registerGetConsoleLogsTool } from './tools/getConsoleLogsTool.js';
1112
import { registerUpdateComponentTool } from './tools/updateComponentTool.js';
1213
import { registerAddAssetToSceneTool } from './tools/addAssetToSceneTool.js';
1314
import { registerUpdateGameObjectTool } from './tools/updateGameObjectTool.js';
@@ -50,6 +51,7 @@ registerSelectGameObjectTool(server, mcpUnity, toolLogger);
5051
registerAddPackageTool(server, mcpUnity, toolLogger);
5152
registerRunTestsTool(server, mcpUnity, toolLogger);
5253
registerSendConsoleLogTool(server, mcpUnity, toolLogger);
54+
registerGetConsoleLogsTool(server, mcpUnity, toolLogger);
5355
registerUpdateComponentTool(server, mcpUnity, toolLogger);
5456
registerAddAssetToSceneTool(server, mcpUnity, toolLogger);
5557
registerUpdateGameObjectTool(server, mcpUnity, toolLogger);
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)