get_script_metrics
Retrieve performance and usage metrics for Google Apps Script projects to monitor execution patterns and identify optimization opportunities.
Instructions
Get metrics data for Google Apps Script projects.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| scriptId | Yes | The ID of the script project. | |
| deploymentId | Yes | The ID of the deployment to filter metrics. | |
| metricsGranularity | Yes | The granularity of the metrics data. | |
| fields | Yes | Selector specifying which fields to include in a partial response. | |
| key | Yes | API key for the request. | |
| access_token | Yes | OAuth access token for authorization. | |
| oauth_token | Yes | OAuth 2.0 token for the current user. | |
| prettyPrint | No | Whether to return the response with indentations and line breaks. |
Implementation Reference
- The executeFunction that implements the core logic of the get_script_metrics tool by making an API request to the Google Apps Script metrics endpoint.const executeFunction = async ({ scriptId, deploymentId, metricsGranularity, fields, key, access_token, oauth_token, prettyPrint = true }) => { const baseUrl = 'https://script.googleapis.com'; const token = process.env.GOOGLE_APP_SCRIPT_API_API_KEY; try { // Construct the URL with query parameters const url = new URL(`${baseUrl}/v1/projects/${scriptId}/metrics`); url.searchParams.append('metricsFilter.deploymentId', deploymentId); url.searchParams.append('metricsGranularity', metricsGranularity); url.searchParams.append('fields', fields); url.searchParams.append('alt', 'json'); url.searchParams.append('key', key); url.searchParams.append('$.xgafv', '1'); url.searchParams.append('access_token', access_token); url.searchParams.append('oauth_token', oauth_token); url.searchParams.append('prettyPrint', prettyPrint.toString()); // Set up headers for the request const headers = { 'Accept': 'application/json' }; // If a token is provided, add it to the Authorization header if (token) { headers['Authorization'] = `Bearer ${token}`; } // Perform the fetch request const response = await fetch(url.toString(), { method: 'GET', headers }); // Check if the response was successful if (!response.ok) { const errorData = await response.json(); throw new Error(errorData); } // Parse and return the response data const data = await response.json(); return data; } catch (error) { const errorDetails = { message: error.message, stack: error.stack, scriptId, deploymentId, timestamp: new Date().toISOString(), errorType: error.name || 'Unknown' }; logger.error('METRICS_GET', 'Error getting metrics data', errorDetails); console.error('❌ Error getting metrics data:', errorDetails); // Return detailed error information for debugging return { error: true, message: error.message, details: errorDetails, rawError: { name: error.name, stack: error.stack } }; } };
- The schema definition for the get_script_metrics tool, including input parameters and their types.type: 'function', function: { name: 'get_script_metrics', description: 'Get metrics data for Google Apps Script projects.', parameters: { type: 'object', properties: { scriptId: { type: 'string', description: 'The ID of the script project.' }, deploymentId: { type: 'string', description: 'The ID of the deployment to filter metrics.' }, metricsGranularity: { type: 'string', description: 'The granularity of the metrics data.' }, fields: { type: 'string', description: 'Selector specifying which fields to include in a partial response.' }, key: { type: 'string', description: 'API key for the request.' }, access_token: { type: 'string', description: 'OAuth access token for authorization.' }, oauth_token: { type: 'string', description: 'OAuth 2.0 token for the current user.' }, prettyPrint: { type: 'boolean', description: 'Whether to return the response with indentations and line breaks.' } }, required: ['scriptId', 'deploymentId', 'metricsGranularity', 'fields', 'key', 'access_token', 'oauth_token'] } } }
- The apiTool object that bundles the handler and schema, exported for registration in the MCP tools system.const apiTool = { function: executeFunction, definition: { type: 'function', function: { name: 'get_script_metrics', description: 'Get metrics data for Google Apps Script projects.', parameters: { type: 'object', properties: { scriptId: { type: 'string', description: 'The ID of the script project.' }, deploymentId: { type: 'string', description: 'The ID of the deployment to filter metrics.' }, metricsGranularity: { type: 'string', description: 'The granularity of the metrics data.' }, fields: { type: 'string', description: 'Selector specifying which fields to include in a partial response.' }, key: { type: 'string', description: 'API key for the request.' }, access_token: { type: 'string', description: 'OAuth access token for authorization.' }, oauth_token: { type: 'string', description: 'OAuth 2.0 token for the current user.' }, prettyPrint: { type: 'boolean', description: 'Whether to return the response with indentations and line breaks.' } }, required: ['scriptId', 'deploymentId', 'metricsGranularity', 'fields', 'key', 'access_token', 'oauth_token'] } } } }; export { apiTool };