deepsource_quality_metrics
Analyze code quality metrics for DeepSource projects, including coverage, duplication, and thresholds, with filtering options. Provides detailed insights for individual languages and repository-wide aggregation.
Instructions
Get quality metrics from a DeepSource project with optional filtering by metric type.
Metrics include code coverage, duplicate code percentage, and more, along with their:
Current values
Threshold settings
Pass/fail status
Configuration status (reporting and enforcement)
For each metric, detailed information is provided for different programming languages and the aggregated metrics for the entire repository.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| projectKey | Yes | The unique identifier for the DeepSource project | |
| shortcodeIn | No | Filter metrics by specific shortcodes (e.g., ["LCV", "BCV"]) |
Implementation Reference
- src/handlers/quality-metrics.ts:255-275 (handler)Primary handler function that sets up dependencies (repository, logger) and invokes the core quality metrics fetching logic for the tool.export async function handleDeepsourceQualityMetrics(params: DeepsourceQualityMetricsParams) { try { const baseDeps = createDefaultHandlerDeps({ logger }); const apiKey = baseDeps.getApiKey(); const repositoryFactory = new RepositoryFactory({ apiKey }); const qualityMetricsRepository = repositoryFactory.createQualityMetricsRepository(); const deps: QualityMetricsHandlerDeps = { qualityMetricsRepository, logger, }; const handler = createQualityMetricsHandlerWithRepo(deps); const result = await handler(params); return result; } catch (error) { // Handle configuration errors and other setup issues return MCPErrorFormatter.createErrorResponse(error, 'quality-metrics-setup'); } }
- src/handlers/quality-metrics.ts:77-170 (handler)Core handler logic that fetches quality metrics from the repository, processes them, formats the response with threshold info and usage examples.export function createQualityMetricsHandlerWithRepo(deps: QualityMetricsHandlerDeps) { return async function handleQualityMetrics(params: DeepsourceQualityMetricsParams) { try { const { projectKey, shortcodeIn } = params; // Validate required parameters using MCP error handling validateNonEmptyString(projectKey, 'projectKey'); const projectKeyBranded = asProjectKey(projectKey); deps.logger.info('Fetching quality metrics from repository', { projectKey, shortcodeIn }); // Get metrics from repository with server-side filtering if shortcodes specified const filteredMetrics = shortcodeIn ? await deps.qualityMetricsRepository.findByProjectWithFilter( projectKeyBranded, shortcodeIn ) : await deps.qualityMetricsRepository.findByProject(projectKeyBranded); deps.logger.info('Successfully fetched quality metrics', { count: filteredMetrics.length, projectKey, }); const metricsData = { metrics: filteredMetrics.map((domainMetric: QualityMetrics) => ({ name: domainMetric.configuration.name, shortcode: domainMetric.configuration.shortcode, description: domainMetric.configuration.description, positiveDirection: domainMetric.configuration.positiveDirection, unit: domainMetric.configuration.unit, minValueAllowed: domainMetric.configuration.minAllowed, maxValueAllowed: domainMetric.configuration.maxAllowed, isReported: domainMetric.configuration.isReported, isThresholdEnforced: domainMetric.configuration.isThresholdEnforced, items: [ { id: domainMetric.repositoryId, key: domainMetric.configuration.metricKey, threshold: domainMetric.configuration.threshold?.value ?? null, latestValue: domainMetric.currentValue?.value ?? null, latestValueDisplay: domainMetric.currentValue?.displayValue ?? null, thresholdStatus: domainMetric.thresholdStatus, // Add helpful metadata for threshold values thresholdInfo: domainMetric.configuration.threshold && domainMetric.currentValue && domainMetric.configuration.threshold.value !== null && domainMetric.currentValue.value !== null ? { difference: domainMetric.currentValue.value - domainMetric.configuration.threshold.value, percentDifference: domainMetric.configuration.threshold.value !== 0 ? `${(((domainMetric.currentValue.value - domainMetric.configuration.threshold.value) / domainMetric.configuration.threshold.value) * 100).toFixed(2)}%` : 'N/A', isPassing: domainMetric.isCompliant, } : null, }, ], })), // Add helpful examples for threshold management usage_examples: { filtering: 'To filter metrics by type, use the shortcodeIn parameter with specific metric codes (e.g., ["LCV", "BCV"])', updating_threshold: 'To update a threshold, use the update_metric_threshold tool', updating_settings: 'To update metric settings (e.g., enable reporting or threshold enforcement), use the update_metric_setting tool', }, }; return { content: [ { type: 'text' as const, text: JSON.stringify(metricsData), }, ], }; } catch (error) { deps.logger.error('Error in handleQualityMetrics', { errorType: typeof error, errorName: error instanceof Error ? error.name : 'Unknown', errorMessage: error instanceof Error ? error.message : String(error), errorStack: error instanceof Error ? error.stack : 'No stack available', }); // Use MCP-compliant error formatting return MCPErrorFormatter.createErrorResponse(error, 'quality-metrics-fetch'); } }; }
- src/server/tool-definitions.ts:30-72 (schema)Zod schema definition for the quality_metrics tool, including input parameters (projectKey, optional shortcodeIn) and detailed output schema.export const qualityMetricsToolSchema = { name: 'quality_metrics', description: 'Get quality metrics from a DeepSource project with optional filtering', inputSchema: { projectKey: z.string().describe('DeepSource project key to fetch quality metrics for'), shortcodeIn: z .array(z.nativeEnum(MetricShortcode)) .optional() .describe('Optional filter for specific metric shortcodes'), }, outputSchema: { metrics: z.array( z.object({ name: z.string(), shortcode: z.string(), description: z.string(), positiveDirection: z.string(), unit: z.string(), minValueAllowed: z.number().nullable(), maxValueAllowed: z.number().nullable(), isReported: z.boolean(), isThresholdEnforced: z.boolean(), items: z.array( z.object({ id: z.string(), key: z.string(), threshold: z.number().nullable(), latestValue: z.number().nullable(), latestValueDisplay: z.string().nullable(), thresholdStatus: z.string(), thresholdInfo: z .object({ difference: z.number(), percentDifference: z.string(), isPassing: z.boolean(), }) .nullable(), }) ), }) ), }, };
- src/server/tool-registration.ts:57-69 (registration)Tool handler registration mapping for 'quality_metrics' tool, which adapts parameters and calls handleDeepsourceQualityMetrics.quality_metrics: async (params: unknown) => { const typedParams = params as Record<string, unknown>; const metricsParams: { projectKey: string; shortcodeIn?: MetricShortcode[] } = { projectKey: typedParams.projectKey as string, }; const shortcodeIn = typedParams.shortcodeIn as MetricShortcode[] | undefined; if (shortcodeIn) { metricsParams.shortcodeIn = shortcodeIn; } return handleDeepsourceQualityMetrics(metricsParams); },
- TypeScript interface defining input parameters for the quality metrics handler.export interface DeepsourceQualityMetricsParams { /** DeepSource project key to fetch quality metrics for */ projectKey: string; /** Optional filter for specific metric shortcodes */ shortcodeIn?: MetricShortcode[]; }