get-metrics
Retrieve metrics data from the Opik MCP Server by specifying filters like metric name, project ID, project name, or date range for precise analysis.
Instructions
Get metrics data
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| endDate | No | End date in ISO format (YYYY-MM-DD) | |
| metricName | No | Optional metric name to filter | |
| projectId | No | Optional project ID to filter metrics | |
| projectName | No | Optional project name to filter metrics | |
| startDate | No | Start date in ISO format (YYYY-MM-DD) |
Implementation Reference
- src/tools/metrics.ts:17-81 (handler)The main handler for the 'get-metrics' tool. It constructs an API URL with optional query parameters for filtering metrics by name, project, and date range. If no project is specified, it fetches the first available project. Returns metrics data as JSON or an error message.async (args: any) => { const { metricName, projectId, projectName, startDate, endDate } = args; let url = `/v1/private/metrics`; const queryParams = []; if (metricName) queryParams.push(`metric_name=${metricName}`); // Add project filtering - API requires either project_id or project_name if (projectId) { queryParams.push(`project_id=${projectId}`); } else if (projectName) { queryParams.push(`project_name=${encodeURIComponent(projectName)}`); } else { // If no project specified, we need to find one for the API to work const projectsResponse = await makeApiRequest<ProjectResponse>( `/v1/private/projects?page=1&size=1` ); if ( projectsResponse.data && projectsResponse.data.content && projectsResponse.data.content.length > 0 ) { const firstProject = projectsResponse.data.content[0]; queryParams.push(`project_id=${firstProject.id}`); logToFile( `No project specified, using first available: ${firstProject.name} (${firstProject.id})` ); } else { return { content: [ { type: 'text', text: 'Error: No project ID or name provided, and no projects found', }, ], }; } } if (startDate) queryParams.push(`start_date=${startDate}`); if (endDate) queryParams.push(`end_date=${endDate}`); if (queryParams.length > 0) { url += `?${queryParams.join('&')}`; } const response = await makeApiRequest<MetricsResponse>(url); if (!response.data) { return { content: [{ type: 'text', text: response.error || 'Failed to fetch metrics' }], }; } return { content: [ { type: 'text', text: JSON.stringify(response.data, null, 2), }, ], }; } );
- src/tools/metrics.ts:10-16 (schema)Input parameter schema for the 'get-metrics' tool, defined using Zod with optional string parameters for metricName, projectId, projectName, startDate, and endDate.{ metricName: z.string().optional().describe('Optional metric name to filter'), projectId: z.string().optional().describe('Optional project ID to filter metrics'), projectName: z.string().optional().describe('Optional project name to filter metrics'), startDate: z.string().optional().describe('Start date in ISO format (YYYY-MM-DD)'), endDate: z.string().optional().describe('End date in ISO format (YYYY-MM-DD)'), },
- src/tools/metrics.ts:6-84 (registration)The loadMetricTools function that registers the 'get-metrics' tool on the MCP server using server.tool, including schema and handler.export const loadMetricTools = (server: any) => { server.tool( 'get-metrics', 'Get metrics data', { metricName: z.string().optional().describe('Optional metric name to filter'), projectId: z.string().optional().describe('Optional project ID to filter metrics'), projectName: z.string().optional().describe('Optional project name to filter metrics'), startDate: z.string().optional().describe('Start date in ISO format (YYYY-MM-DD)'), endDate: z.string().optional().describe('End date in ISO format (YYYY-MM-DD)'), }, async (args: any) => { const { metricName, projectId, projectName, startDate, endDate } = args; let url = `/v1/private/metrics`; const queryParams = []; if (metricName) queryParams.push(`metric_name=${metricName}`); // Add project filtering - API requires either project_id or project_name if (projectId) { queryParams.push(`project_id=${projectId}`); } else if (projectName) { queryParams.push(`project_name=${encodeURIComponent(projectName)}`); } else { // If no project specified, we need to find one for the API to work const projectsResponse = await makeApiRequest<ProjectResponse>( `/v1/private/projects?page=1&size=1` ); if ( projectsResponse.data && projectsResponse.data.content && projectsResponse.data.content.length > 0 ) { const firstProject = projectsResponse.data.content[0]; queryParams.push(`project_id=${firstProject.id}`); logToFile( `No project specified, using first available: ${firstProject.name} (${firstProject.id})` ); } else { return { content: [ { type: 'text', text: 'Error: No project ID or name provided, and no projects found', }, ], }; } } if (startDate) queryParams.push(`start_date=${startDate}`); if (endDate) queryParams.push(`end_date=${endDate}`); if (queryParams.length > 0) { url += `?${queryParams.join('&')}`; } const response = await makeApiRequest<MetricsResponse>(url); if (!response.data) { return { content: [{ type: 'text', text: response.error || 'Failed to fetch metrics' }], }; } return { content: [ { type: 'text', text: JSON.stringify(response.data, null, 2), }, ], }; } ); return server; };
- src/index.ts:96-98 (registration)Conditional registration of the metrics tools by calling loadMetricTools when 'metrics' is in enabledToolsets.if (config.enabledToolsets.includes('metrics')) { server = loadMetricTools(server); logToFile('Loaded metrics toolset');