Skip to main content
Glama

infracost_diff

Compare Terraform configurations to identify cost changes between baseline and current infrastructure, helping track cloud spending differences.

Instructions

Show cost differences between two Terraform configurations. Compares baseline and current infrastructure to identify cost changes. Requires infracost CLI to be installed.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYesPath to current Terraform directory or plan JSON file
compareToYesPath to baseline Terraform directory or plan JSON file
formatNoOutput format (default: diff)
outFileNoSave output to a file

Implementation Reference

  • Main handler function for the infracost_diff tool. Validates arguments using DiffSchema, checks if Infracost CLI is installed, executes the diff via executeDiff, and returns the output as MCP content.
    async handleDiff(args: z.infer<typeof DiffSchema>) { const isInstalled = await checkInfracostInstalled(); if (!isInstalled) { throw new Error( 'Infracost CLI is not installed. Please install it from https://www.infracost.io/docs/' ); } const result = await executeDiff(args); if (!result.success) { throw new Error(result.error || 'Diff command failed'); } return { content: [ { type: 'text', text: result.output || 'Diff completed successfully', }, ], }; }
  • Zod schema defining the input parameters and validation for the infracost_diff tool.
    export const DiffSchema = z.object({ path: z.string().describe('Path to Terraform directory or plan JSON file'), compareTo: z.string().describe('Path to baseline Terraform directory or plan JSON file'), format: z.enum(['json', 'diff']).optional().describe('Output format'), outFile: z.string().optional().describe('Save output to file'), });
  • src/index.ts:106-133 (registration)
    Registration of the infracost_diff tool in the ListTools response, including name, description, and input schema definition.
    { name: 'infracost_diff', description: 'Show cost differences between two Terraform configurations. Compares baseline and current infrastructure to identify cost changes. Requires infracost CLI to be installed.', inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'Path to current Terraform directory or plan JSON file', }, compareTo: { type: 'string', description: 'Path to baseline Terraform directory or plan JSON file', }, format: { type: 'string', enum: ['json', 'diff'], description: 'Output format (default: diff)', }, outFile: { type: 'string', description: 'Save output to a file', }, }, required: ['path', 'compareTo'], }, },
  • Dispatch handler in CallToolRequestSchema that parses arguments with DiffSchema and delegates to tools.handleDiff.
    case 'infracost_diff': { const validatedArgs = DiffSchema.parse(args); return await tools.handleDiff(validatedArgs); }
  • Helper function that executes the 'infracost diff' CLI command with resolved paths and options, captures output and parses JSON if applicable.
    export async function executeDiff(options: DiffOptions): Promise<CommandResult> { try { const args = ['diff', '--path', resolve(options.path), '--compare-to', resolve(options.compareTo)]; if (options.format) { args.push('--format', options.format); } if (options.outFile) { args.push('--out-file', resolve(options.outFile)); } const { stdout, stderr } = await execFileAsync('infracost', args, { maxBuffer: 10 * 1024 * 1024, }); let parsedData; if (options.format === 'json' && !options.outFile && stdout.trim()) { try { parsedData = JSON.parse(stdout); } catch (e) {} } return { success: true, output: options.outFile ? `Output saved to ${options.outFile}` : stdout, error: stderr || undefined, data: parsedData, }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : 'Unknown error occurred', }; } }

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/phildougherty/infracost_mcp'

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