validate_performance_gtmetrix
Analyze website performance using GTmetrix API to test page speed, identify optimization opportunities, and measure loading metrics for web pages.
Instructions
Analyze website performance using GTmetrix. Requires API key (free tier available).
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| apiKey | Yes | GTmetrix API key (required) | |
| browser | No | Browser type (e.g., chrome) | |
| location | No | Test location (e.g., vancouver-canada) | |
| url | Yes |
Implementation Reference
- src/performance/gtmetrix.ts:57-119 (handler)The main handler function `analyzeGTmetrix` that creates a GTmetrix test, fetches the API, and returns performance metrics including scores and timings.export async function analyzeGTmetrix( url: string, options: GTmetrixOptions ): Promise<GTmetrixResult> { try { if (!options.apiKey) { throw new Error('GTmetrix API key is required. Get one at https://gtmetrix.com/api/'); } // Start test const testResponse = await fetch('https://gtmetrix.com/api/2.0/tests', { method: 'POST', headers: { 'Authorization': `Basic ${Buffer.from(options.apiKey + ':').toString('base64')}`, 'Content-Type': 'application/vnd.api+json', }, body: JSON.stringify({ data: { type: 'test', attributes: { url, location: options.location || 'vancouver-canada', browser: options.browser || 'chrome', }, }, }), }); if (!testResponse.ok) { throw new Error(`GTmetrix API error: ${testResponse.status} ${testResponse.statusText}`); } const testData: GTmetrixResponse = await testResponse.json(); const testId = testData.data.id; // Poll for results (simplified - returns immediately with test ID) // For complete implementation, would poll until state === 'completed' return { tool: 'gtmetrix', success: testData.data.attributes.state !== 'error', url, test_id: testId, status: testData.data.attributes.state as any, lighthouse_score: testData.data.attributes.lighthouse_score, pagespeed_score: testData.data.attributes.pagespeed_score, yslow_score: testData.data.attributes.yslow_score, fully_loaded_time: testData.data.attributes.fully_loaded_time, page_bytes: testData.data.attributes.page_bytes, page_elements: testData.data.attributes.page_elements, report_url: testData.data.attributes.report_url, error: testData.data.attributes.error, raw: testData, }; } catch (error) { return { tool: 'gtmetrix', success: false, url, status: 'error', error: error instanceof Error ? error.message : String(error), }; } }
- src/performance/gtmetrix.ts:34-49 (schema)TypeScript interface defining the output schema/result structure for GTmetrix analysis.export interface GTmetrixResult { tool: 'gtmetrix'; success: boolean; url: string; test_id?: string; status: 'queued' | 'started' | 'completed' | 'error'; lighthouse_score?: number; pagespeed_score?: number; yslow_score?: number; fully_loaded_time?: number; page_bytes?: number; page_elements?: number; report_url?: string; error?: string; raw?: GTmetrixResponse; }
- index.ts:40-45 (schema)Zod schema for input argument validation of the GTmetrix tool.const GTmetrixArgsSchema = z.object({ url: z.string().url(), apiKey: z.string(), location: z.string().optional(), browser: z.string().optional(), });
- index.ts:144-157 (registration)Tool registration in the MCP tools array, including name, description, and input schema.{ name: 'validate_performance_gtmetrix', description: 'Analyze website performance using GTmetrix. Requires API key (free tier available).', inputSchema: { type: 'object', properties: { url: { type: 'string' }, apiKey: { type: 'string', description: 'GTmetrix API key (required)' }, location: { type: 'string', description: 'Test location (e.g., vancouver-canada)' }, browser: { type: 'string', description: 'Browser type (e.g., chrome)' }, }, required: ['url', 'apiKey'], }, },
- index.ts:331-339 (registration)Switch case in the tool call handler that invokes the analyzeGTmetrix function after validation.case 'validate_performance_gtmetrix': { const validatedArgs = GTmetrixArgsSchema.parse(args); const result = await analyzeGTmetrix(validatedArgs.url, { apiKey: validatedArgs.apiKey, location: validatedArgs.location, browser: validatedArgs.browser, }); return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }] }; }