Skip to main content
Glama

MCP Unity

menuItemTool.tsโ€ข2.39 kB
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 = 'execute_menu_item'; const toolDescription = 'Executes a Unity menu item by path'; const paramsSchema = z.object({ menuPath: z.string().describe('The path to the menu item to execute (e.g. "GameObject/Create Empty")') }); /** * Creates and registers the Menu Item tool with the MCP server * This tool allows executing menu items in the Unity Editor * * @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 registerMenuItemTool(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 menu item execution requests * * @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<CallToolResult> { const { menuPath } = params; const response = await mcpUnity.sendRequest({ method: toolName, params: { menuPath } }); if (!response.success) { throw new McpUnityError( ErrorType.TOOL_EXECUTION, response.message || `Failed to execute menu item: ${menuPath}` ); } return { content: [{ type: response.type, text: response.message || `Successfully executed menu item: ${menuPath}` }] }; }

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/2996371275/unity_mcp_complex'

If you have feedback or need assistance with the MCP directory API, please join our Discord server