Skip to main content
Glama

script_read

Retrieve and read C# script files from Unity projects using a direct path input, facilitating script access and management for efficient development workflows.

Instructions

Read a C# script from Unity project

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYesPath to the script file

Implementation Reference

  • Input schema definition for the script_read tool, specifying the required 'path' parameter.
    name: 'script_read', description: 'Read a C# script from Unity project', inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'Path to the script file' } }, required: ['path'] }
  • Main handler for script_read tool in executeTool method. Validates input, calls adapter.readScript, and returns formatted content.
    case 'script_read': { if (!args.path) { throw new Error('path is required'); } const result = await this.adapter.readScript(args.path); return { content: [{ type: 'text', text: `Script content from ${result.path}:\n\n${result.content}` }] }; }
  • Adapter implementation that sends HTTP POST request to Unity server's 'script/read' endpoint with the file path.
    async readScript(path: string): Promise<any> { return this.call('script/read', { path }); }
  • Generic HTTP call method used by readScript to communicate with Unity MCP server, including retry logic and error handling.
    async call(method: string, params: Record<string, any> = {}): Promise<any> { const startTime = Date.now(); console.error(`[Unity HTTP] Calling method: ${method}`); const maxRetries = 3; let lastError: any; for (let retry = 0; retry < maxRetries; retry++) { if (retry > 0) { console.error(`[Unity HTTP] Retry ${retry}/${maxRetries - 1} for method: ${method}`); await new Promise(resolve => setTimeout(resolve, 1000 * retry)); // Exponential backoff } try { const controller = new AbortController(); const timeoutId = setTimeout(() => { console.error(`[Unity HTTP] Request timeout after ${this.timeout}ms for method: ${method}`); controller.abort(); }, this.timeout); const response = await fetch(this.url, { method: 'POST', headers: { 'Content-Type': 'application/json; charset=utf-8', 'Accept': 'application/json; charset=utf-8' }, body: JSON.stringify({ method, ...params }), signal: controller.signal }); clearTimeout(timeoutId); const elapsed = Date.now() - startTime; console.error(`[Unity HTTP] Response received in ${elapsed}ms for method: ${method}`); const result = await response.json() as UnityResponse; if (!result.success) { throw new Error(result.error || 'Unknown error'); } return result.result; } catch (error: any) { lastError = error; if (error.name === 'AbortError') { lastError = new Error('Request timeout'); } else if (error.message?.includes('ECONNREFUSED')) { lastError = new Error('Unity HTTP server is not running'); } else if (error.message?.includes('Failed to fetch')) { lastError = new Error('Failed to connect to Unity HTTP server'); } console.error(`[Unity HTTP] Error on attempt ${retry + 1}: ${lastError.message}`); // Don't retry on certain errors if (error.message?.includes('Method not found')) { throw error; } } } // All retries failed throw lastError || new Error('Unknown error after retries'); }
  • getTools method registers all available tools including script_read by returning the Tool[] array with its definition.
    getTools(): Tool[] { return [ // Script tools { name: 'script_create', description: 'Create a new C# script in Unity project', inputSchema: { type: 'object', properties: { fileName: { type: 'string', description: 'Name of the script file (without .cs extension)' }, content: { type: 'string', description: 'Script content (optional, will use template if not provided)' }, folder: { type: 'string', description: 'Target folder path (default: Assets/Scripts)' } }, required: ['fileName'] } }, { name: 'script_read', description: 'Read a C# script from Unity project', inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'Path to the script file' } }, required: ['path'] } }, { name: 'script_delete', description: 'Delete a C# script from Unity project', inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'Path to the script file' } }, required: ['path'] } }, { name: 'script_apply_diff', description: 'Apply a unified diff to a C# script', inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'Path to the script file' }, diff: { type: 'string', description: 'Unified diff content to apply' }, options: { type: 'object', description: 'Optional diff application settings', properties: { dryRun: { type: 'boolean', description: 'Preview changes without applying (default: false)' } } } }, required: ['path', 'diff'] } }, // Shader tools { name: 'shader_create', description: 'Create a new shader in Unity project', inputSchema: { type: 'object', properties: { name: { type: 'string', description: 'Name of the shader (without .shader extension)' }, content: { type: 'string', description: 'Shader content (optional, will use template if not provided)' }, folder: { type: 'string', description: 'Target folder path (default: Assets/Shaders)' } }, required: ['name'] } }, { name: 'shader_read', description: 'Read a shader from Unity project', inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'Path to the shader file' } }, required: ['path'] } }, { name: 'shader_delete', description: 'Delete a shader from Unity project', inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'Path to the shader file' } }, required: ['path'] } }, // Project tools { name: 'project_info', description: 'Get comprehensive Unity project information including render pipeline details, project path, Unity version, and platform info', inputSchema: { type: 'object', properties: {} } }, { name: 'project_status', description: 'Check Unity MCP server connection status (simple connectivity test only)', inputSchema: { type: 'object', properties: {} } }, { name: 'setup_unity_bridge', description: 'Install/update Unity MCP bridge scripts to a Unity project (works even if Unity server is not running)', inputSchema: { type: 'object', properties: { projectPath: { type: 'string', description: 'Path to the Unity project' }, forceUpdate: { type: 'boolean', description: 'Force update even if scripts are up to date', default: false } }, required: ['projectPath'] } }, // Folder tools { name: 'folder_create', description: 'Create a new folder in Unity project', inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'Path for the new folder (e.g., Assets/MyFolder)' } }, required: ['path'] } }, { name: 'folder_rename', description: 'Rename a folder in Unity project', inputSchema: { type: 'object', properties: { oldPath: { type: 'string', description: 'Current path of the folder' }, newName: { type: 'string', description: 'New name for the folder' } }, required: ['oldPath', 'newName'] } }, { name: 'folder_move', description: 'Move a folder to a new location in Unity project', inputSchema: { type: 'object', properties: { sourcePath: { type: 'string', description: 'Current path of the folder' }, targetPath: { type: 'string', description: 'Target path for the folder' } }, required: ['sourcePath', 'targetPath'] } }, { name: 'folder_delete', description: 'Delete a folder from Unity project', inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'Path of the folder to delete' }, recursive: { type: 'boolean', description: 'Delete all contents recursively (default: true)' } }, required: ['path'] } }, { name: 'folder_list', description: 'List contents of a folder in Unity project', inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'Path of the folder to list (default: Assets)' }, recursive: { type: 'boolean', description: 'List all subdirectories recursively (default: false)' } } } } ];

Latest Blog Posts

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/zabaglione/mcp-server-unity'

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