deepsource-mcp-server
Version:
Model Context Protocol server for DeepSource
151 lines • 7.03 kB
JavaScript
import { DeepSourceClient } from '../deepsource.js';
import { MetricThresholdStatus } from '../types/metrics.js';
/**
* Fetches and returns quality metrics from a specified DeepSource project
* @param params - Parameters for fetching metrics, including project key and optional filters
* @returns A response containing the metrics data with their values and thresholds
* @throws Error if the DEEPSOURCE_API_KEY environment variable is not set
* @public
*/
export async function handleDeepsourceQualityMetrics({ projectKey, shortcodeIn, }) {
const apiKey = process.env.DEEPSOURCE_API_KEY;
/* istanbul ignore if */
if (!apiKey) {
throw new Error('DEEPSOURCE_API_KEY environment variable is not set');
}
const client = new DeepSourceClient(apiKey);
const metrics = await client.getQualityMetrics(projectKey, { shortcodeIn });
return {
content: [
{
type: 'text',
text: JSON.stringify({
metrics: metrics.map((metric) => ({
name: metric.name,
shortcode: metric.shortcode,
description: metric.description,
positiveDirection: metric.positiveDirection,
unit: metric.unit,
minValueAllowed: metric.minValueAllowed,
maxValueAllowed: metric.maxValueAllowed,
isReported: metric.isReported,
isThresholdEnforced: metric.isThresholdEnforced,
items: metric.items.map((item) => ({
id: item.id,
key: item.key,
threshold: item.threshold,
latestValue: item.latestValue,
latestValueDisplay: item.latestValueDisplay,
thresholdStatus: item.thresholdStatus,
// Add helpful metadata for threshold values
thresholdInfo: item.threshold !== null &&
item.threshold !== undefined &&
item.latestValue !== null &&
item.latestValue !== undefined
? {
difference: item.latestValue - item.threshold,
percentDifference: item.threshold !== 0
? `${(((item.latestValue - item.threshold) / item.threshold) *
100).toFixed(2)}%`
: 'N/A',
isPassing: item.thresholdStatus === MetricThresholdStatus.PASSING,
}
: 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',
},
}, null, 2),
},
],
};
}
/**
* Updates the threshold for a specific metric in a project
* @param params - Parameters for updating the threshold
* @returns A response indicating whether the update was successful
* @throws Error if the DEEPSOURCE_API_KEY environment variable is not set
* @public
*/
export async function handleDeepsourceUpdateMetricThreshold({ projectKey, repositoryId, metricShortcode, metricKey, thresholdValue, }) {
const apiKey = process.env.DEEPSOURCE_API_KEY;
/* istanbul ignore if */
if (!apiKey) {
throw new Error('DEEPSOURCE_API_KEY environment variable is not set');
}
const client = new DeepSourceClient(apiKey);
const result = await client.setMetricThreshold({
repositoryId,
metricShortcode,
metricKey,
thresholdValue: thresholdValue ?? null,
});
return {
content: [
{
type: 'text',
text: JSON.stringify({
ok: result.ok,
projectKey, // Echo back the project key for context
metricShortcode,
metricKey,
thresholdValue,
message: result.ok
? `Successfully ${thresholdValue !== null && thresholdValue !== undefined ? 'updated' : 'removed'} threshold for ${metricShortcode} (${metricKey})`
: `Failed to update threshold for ${metricShortcode} (${metricKey})`,
next_steps: result.ok
? ['Use quality_metrics to view the updated metrics']
: ['Check if you have sufficient permissions', 'Verify the repository ID is correct'],
}, null, 2),
},
],
};
}
/**
* Updates the settings for a specific metric in a project
* @param params - Parameters for updating the metric settings
* @returns A response indicating whether the update was successful
* @throws Error if the DEEPSOURCE_API_KEY environment variable is not set
* @public
*/
export async function handleDeepsourceUpdateMetricSetting({ projectKey, repositoryId, metricShortcode, isReported, isThresholdEnforced, }) {
const apiKey = process.env.DEEPSOURCE_API_KEY;
/* istanbul ignore if */
if (!apiKey) {
throw new Error('DEEPSOURCE_API_KEY environment variable is not set');
}
const client = new DeepSourceClient(apiKey);
const result = await client.updateMetricSetting({
repositoryId,
metricShortcode,
isReported,
isThresholdEnforced,
});
return {
content: [
{
type: 'text',
text: JSON.stringify({
ok: result.ok,
projectKey, // Echo back the project key for context
metricShortcode,
settings: {
isReported,
isThresholdEnforced,
},
message: result.ok
? `Successfully updated settings for ${metricShortcode}`
: `Failed to update settings for ${metricShortcode}`,
next_steps: result.ok
? ['Use quality_metrics to view the updated metrics']
: ['Check if you have sufficient permissions', 'Verify the repository ID is correct'],
}, null, 2),
},
],
};
}
//# sourceMappingURL=quality-metrics.js.map