Skip to main content
Glama

xcresult_export_attachment

Export specific attachments from Xcode test results files, converting App UI hierarchies to JSON format for analysis.

Instructions

Export a specific attachment by index - can convert App UI hierarchy attachments to JSON

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
xcresult_pathYesAbsolute path to the .xcresult file
test_idYesTest ID or index number that contains the attachment
attachment_indexYesIndex number of the attachment to export (1-based, from xcresult-list-attachments)
convert_to_jsonNoIf true and attachment is an App UI hierarchy, convert to JSON format

Implementation Reference

  • Main handler implementation: Exports specific test attachments from XCResult files using xcresulttool. Handles validation, attachment extraction, type detection, special UI hierarchy processing, and JSON conversion.
    /**
     * Export a specific attachment by index
     */
    public static async xcresultExportAttachment(
      xcresultPath: string,
      testId: string,
      attachmentIndex: number,
      convertToJson: boolean = false
    ): Promise<McpResult> {
      // Validate xcresult path
      if (!existsSync(xcresultPath)) {
        throw new McpError(
          ErrorCode.InvalidParams,
          `XCResult file not found: ${xcresultPath}`
        );
      }
    
      if (!xcresultPath.endsWith('.xcresult')) {
        throw new McpError(
          ErrorCode.InvalidParams,
          `Path must be an .xcresult file: ${xcresultPath}`
        );
      }
    
      // Check if xcresult is readable
      if (!XCResultParser.isXCResultReadable(xcresultPath)) {
        throw new McpError(
          ErrorCode.InternalError,
          `XCResult file is not readable or incomplete: ${xcresultPath}`
        );
      }
    
      if (!testId || testId.trim() === '') {
        throw new McpError(
          ErrorCode.InvalidParams,
          'Test ID or index is required'
        );
      }
    
      if (attachmentIndex < 1) {
        throw new McpError(
          ErrorCode.InvalidParams,
          'Attachment index must be 1 or greater'
        );
      }
    
      try {
        const parser = new XCResultParser(xcresultPath);
        
        // First find the test node to get the actual test identifier
        const testNode = await parser.findTestNode(testId);
        if (!testNode) {
          throw new McpError(
            ErrorCode.InvalidParams,
            `Test '${testId}' not found. Run xcresult_browse "${xcresultPath}" to see all available tests`
          );
        }
    
        if (!testNode.nodeIdentifier) {
          throw new McpError(
            ErrorCode.InvalidParams,
            `Test '${testId}' does not have a valid identifier for attachment retrieval`
          );
        }
    
        // Get test attachments
        const attachments = await parser.getTestAttachments(testNode.nodeIdentifier);
        
        if (attachments.length === 0) {
          throw new McpError(
            ErrorCode.InvalidParams,
            `No attachments found for test '${testNode.name}'.`
          );
        }
    
        if (attachmentIndex > attachments.length) {
          throw new McpError(
            ErrorCode.InvalidParams,
            `Invalid attachment index ${attachmentIndex}. Test has ${attachments.length} attachments.`
          );
        }
    
        const attachment = attachments[attachmentIndex - 1];
        if (!attachment) {
          throw new McpError(
            ErrorCode.InternalError,
            `Attachment at index ${attachmentIndex} not found`
          );
        }
    
        const attachmentId = attachment.payloadId || attachment.payload_uuid || attachment.payloadUUID;
        if (!attachmentId) {
          throw new McpError(
            ErrorCode.InternalError,
            'Attachment does not have a valid ID for export'
          );
        }
    
        const filename = attachment.filename || attachment.name || `attachment_${attachmentIndex}`;
        
        // Determine type from identifier or filename first
        let type = attachment.uniform_type_identifier || attachment.uniformTypeIdentifier || '';
        if (!type || type === 'unknown') {
          // Infer type from filename extension or special patterns
          const ext = filename.toLowerCase().split('.').pop();
          if (ext === 'jpeg' || ext === 'jpg') type = 'public.jpeg';
          else if (ext === 'png') type = 'public.png';
          else if (ext === 'mp4') type = 'public.mpeg-4';
          else if (ext === 'mov') type = 'com.apple.quicktime-movie';
          else if (ext === 'txt') type = 'public.plain-text';
          else if (filename.toLowerCase().includes('app ui hierarchy')) type = 'ui-hierarchy';
          else if (filename.toLowerCase().includes('ui snapshot')) type = 'ui-snapshot';
          else if (filename.toLowerCase().includes('synthesized event')) type = 'synthesized-event';
          else type = 'unknown';
        }
    
        const exportedPath = await parser.exportAttachment(attachmentId, filename);
        
        // Handle UI hierarchy files specially  
        if (type === 'ui-hierarchy') {
          if (convertToJson) {
            const hierarchyJson = await this.convertUIHierarchyToJSON(exportedPath);
            return { 
              content: [{ 
                type: 'text', 
                text: JSON.stringify(hierarchyJson)
              }] 
            };
          }
          
          // Return the raw UI hierarchy content (it's already AI-friendly)
          const { readFile } = await import('fs/promises');
          const hierarchyContent = await readFile(exportedPath, 'utf-8');
          return { 
            content: [{ 
              type: 'text', 
              text: `UI Hierarchy for: ${filename}\nType: ${type}\n\n${hierarchyContent}`
            }] 
          };
        }
    
        return { 
          content: [{ 
            type: 'text', 
            text: `Attachment exported to: ${exportedPath}\nFilename: ${filename}\nType: ${type}`
          }] 
        };
    
      } catch (error) {
        if (error instanceof McpError) {
          throw error;
        }
    
        const errorMessage = error instanceof Error ? error.message : String(error);
        
        if (errorMessage.includes('xcresulttool')) {
          throw new McpError(
            ErrorCode.InternalError,
            `XCResult parsing failed. Make sure Xcode Command Line Tools are installed: ${errorMessage}`
          );
        }
        
        throw new McpError(
          ErrorCode.InternalError,
          `Failed to export attachment: ${errorMessage}`
        );
      }
    }
  • Input schema definition for the xcresult_export_attachment tool, defining parameters and validation rules
      name: 'xcresult_export_attachment',
      description: 'Export a specific attachment by index - can convert App UI hierarchy attachments to JSON',
      inputSchema: {
        type: 'object',
        properties: {
          xcresult_path: {
            type: 'string',
            description: 'Absolute path to the .xcresult file',
          },
          test_id: {
            type: 'string',
            description: 'Test ID or index number that contains the attachment',
          },
          attachment_index: {
            type: 'number',
            description: 'Index number of the attachment to export (1-based, from xcresult-list-attachments)',
          },
          convert_to_json: {
            type: 'boolean',
            description: 'If true and attachment is an App UI hierarchy, convert to JSON format',
          },
        },
        required: ['xcresult_path', 'test_id', 'attachment_index'],
      },
    },
  • MCP server registration: Dispatches xcresult_export_attachment tool calls to XCResultTools.xcresultExportAttachment handler in CallToolRequestSchema
    case 'xcresult_export_attachment':
      if (!args.xcresult_path) {
        throw new McpError(ErrorCode.InvalidParams, `Missing required parameter: xcresult_path`);
      }
      if (!args.test_id) {
        throw new McpError(ErrorCode.InvalidParams, `Missing required parameter: test_id`);
      }
      if (args.attachment_index === undefined) {
        throw new McpError(ErrorCode.InvalidParams, `Missing required parameter: attachment_index`);
      }
      return await XCResultTools.xcresultExportAttachment(
        args.xcresult_path as string,
        args.test_id as string,
        args.attachment_index as number,
        args.convert_to_json as boolean | undefined
      );
  • Fallback schema definition used by MCP library getTools() when CLI is unavailable
    name: 'xcresult_export_attachment',
    description: 'Export a specific attachment by index - can convert App UI hierarchy attachments to JSON',
    inputSchema: {
      type: 'object',
      properties: {
        xcresult_path: {
          type: 'string',
          description: 'Absolute path to the .xcresult file',
        },
        test_id: {
          type: 'string',
          description: 'Test ID or index number that contains the attachment',
        },
        attachment_index: {
          type: 'number',
          description: 'Index number of the attachment to export (1-based, from xcresult-list-attachments)',
        },
        convert_to_json: {
          type: 'boolean',
          description: 'If true and attachment is an App UI hierarchy, convert to JSON format',
        },
      },
      required: ['xcresult_path', 'test_id', 'attachment_index'],
    },
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It mentions conversion to JSON for App UI hierarchies, which is useful, but fails to describe critical behaviors such as output format, file handling, error conditions, or whether the operation is read-only or modifies data. For a tool with 4 parameters and no annotations, this leaves significant gaps.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that front-loads the core action ('Export a specific attachment by index') and adds a key capability ('can convert App UI hierarchy attachments to JSON'). There is no wasted verbiage, making it appropriately concise and well-structured.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity (4 parameters, no output schema, no annotations), the description is insufficient. It doesn't explain the output format, file handling, or behavioral traits like error handling, which are critical for an export operation. Without annotations or output schema, more detail is needed to guide effective use.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the input schema fully documents all parameters. The description adds minimal value beyond the schema by hinting at the conversion feature for App UI hierarchies, but doesn't provide additional semantics like usage examples or constraints. This meets the baseline for high schema coverage.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'Export a specific attachment by index' specifies the verb (export) and resource (attachment), and 'can convert App UI hierarchy attachments to JSON' adds a key capability. However, it doesn't explicitly differentiate from sibling tools like xcresult_list_attachments or xcresult_get_ui_hierarchy, which handle related but different operations.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies usage context through 'by index' and references to 'App UI hierarchy attachments,' suggesting it's for extracting specific test artifacts. However, it lacks explicit guidance on when to use this tool versus alternatives like xcresult_get_screenshot or xcresult_get_ui_hierarchy, and doesn't mention prerequisites or exclusions.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/lapfelix/XcodeMCP'

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