Skip to content

Commit a648497

Browse files
Merge pull request #33 from twilight07/tool-unity-logs
feat(server): add get_console_logs tool to MCP Server Tools
2 parents 6403d7b + 2331c6f commit a648497

File tree

5 files changed

+177
-0
lines changed

5 files changed

+177
-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: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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
8+
.enum(["info", "warning", "error"])
9+
.optional()
10+
.describe("The type of logs to retrieve (info, warning, error) - defaults to all logs if not specified"),
11+
});
12+
/**
13+
* Creates and registers the Get Console Logs tool with the MCP server
14+
* This tool allows retrieving messages from the Unity console
15+
*
16+
* @param server The MCP server instance to register with
17+
* @param mcpUnity The McpUnity instance to communicate with Unity
18+
* @param logger The logger instance for diagnostic information
19+
*/
20+
export function registerGetConsoleLogsTool(server, mcpUnity, logger) {
21+
logger.info(`Registering tool: ${toolName}`);
22+
// Register this tool with the MCP server
23+
server.tool(toolName, toolDescription, paramsSchema.shape, async (params) => {
24+
try {
25+
logger.info(`Executing tool: ${toolName}`, params);
26+
const result = await toolHandler(mcpUnity, params);
27+
logger.info(`Tool execution successful: ${toolName}`);
28+
return result;
29+
}
30+
catch (error) {
31+
logger.error(`Tool execution failed: ${toolName}`, error);
32+
throw error;
33+
}
34+
});
35+
}
36+
/**
37+
* Handles requests for Unity console logs
38+
*
39+
* @param mcpUnity The McpUnity instance to communicate with Unity
40+
* @param params The parameters for the tool
41+
* @returns A promise that resolves to the tool execution result
42+
* @throws McpUnityError if the request to Unity fails
43+
*/
44+
async function toolHandler(mcpUnity, params) {
45+
const { logType } = params;
46+
// Send request to Unity using the same method name as the resource
47+
// This allows reusing the existing Unity-side implementation
48+
const response = await mcpUnity.sendRequest({
49+
method: "get_console_logs",
50+
params: {
51+
logType: logType,
52+
},
53+
});
54+
if (!response.success) {
55+
throw new McpUnityError(ErrorType.TOOL_EXECUTION, response.message || "Failed to fetch logs from Unity");
56+
}
57+
return {
58+
content: [
59+
{
60+
type: "text",
61+
text: JSON.stringify(response.data || response.logs || response, null, 2),
62+
},
63+
],
64+
};
65+
}

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: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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
13+
.enum(["info", "warning", "error"])
14+
.optional()
15+
.describe(
16+
"The type of logs to retrieve (info, warning, error) - defaults to all logs if not specified"
17+
),
18+
});
19+
20+
/**
21+
* Creates and registers the Get Console Logs tool with the MCP server
22+
* This tool allows retrieving messages from the Unity console
23+
*
24+
* @param server The MCP server instance to register with
25+
* @param mcpUnity The McpUnity instance to communicate with Unity
26+
* @param logger The logger instance for diagnostic information
27+
*/
28+
export function registerGetConsoleLogsTool(
29+
server: McpServer,
30+
mcpUnity: McpUnity,
31+
logger: Logger
32+
) {
33+
logger.info(`Registering tool: ${toolName}`);
34+
35+
// Register this tool with the MCP server
36+
server.tool(
37+
toolName,
38+
toolDescription,
39+
paramsSchema.shape,
40+
async (params: z.infer<typeof paramsSchema>) => {
41+
try {
42+
logger.info(`Executing tool: ${toolName}`, params);
43+
const result = await toolHandler(mcpUnity, params);
44+
logger.info(`Tool execution successful: ${toolName}`);
45+
return result;
46+
} catch (error) {
47+
logger.error(`Tool execution failed: ${toolName}`, error);
48+
throw error;
49+
}
50+
}
51+
);
52+
}
53+
54+
/**
55+
* Handles requests for Unity console logs
56+
*
57+
* @param mcpUnity The McpUnity instance to communicate with Unity
58+
* @param params The parameters for the tool
59+
* @returns A promise that resolves to the tool execution result
60+
* @throws McpUnityError if the request to Unity fails
61+
*/
62+
async function toolHandler(
63+
mcpUnity: McpUnity,
64+
params: z.infer<typeof paramsSchema>
65+
): Promise<CallToolResult> {
66+
const { logType } = params;
67+
68+
// Send request to Unity using the same method name as the resource
69+
// This allows reusing the existing Unity-side implementation
70+
const response = await mcpUnity.sendRequest({
71+
method: "get_console_logs",
72+
params: {
73+
logType: logType,
74+
},
75+
});
76+
77+
if (!response.success) {
78+
throw new McpUnityError(
79+
ErrorType.TOOL_EXECUTION,
80+
response.message || "Failed to fetch logs from Unity"
81+
);
82+
}
83+
84+
return {
85+
content: [
86+
{
87+
type: "text",
88+
text: JSON.stringify(
89+
response.data || response.logs || response,
90+
null,
91+
2
92+
),
93+
},
94+
],
95+
};
96+
}

0 commit comments

Comments
 (0)