Skip to content

feat(server): add get_console_logs tool to MCP Server Tools #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Server~/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions Server~/build/tools/getConsoleLogsTool.d.ts
Original file line number Diff line number Diff line change
@@ -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;
65 changes: 65 additions & 0 deletions Server~/build/tools/getConsoleLogsTool.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
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
.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, 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.data || response.logs || response, null, 2),
},
],
};
}
2 changes: 2 additions & 0 deletions Server~/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down
96 changes: 96 additions & 0 deletions Server~/src/tools/getConsoleLogsTool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
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
.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
) {
logger.info(`Registering tool: ${toolName}`);

// Register this tool with the MCP server
server.tool(
toolName,
toolDescription,
paramsSchema.shape,
async (params: z.infer<typeof paramsSchema>) => {
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: z.infer<typeof paramsSchema>
): Promise<CallToolResult> {
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.data || response.logs || response,
null,
2
),
},
],
};
}