import { MavenCoordinate, VersionComparisonResult } from '../types.js';
import { fetchMavenMetadata, extractVersions } from '../maven-client.js';
import {
isStableVersion,
filterStableVersions,
determineUpgradeType,
compareVersions,
} from '../version-utils.js';
export const schema = {
name: 'compare_versions',
description: 'Compare two versions and suggest an upgrade path',
inputSchema: {
type: 'object',
properties: {
dependency: {
type: 'string',
description:
'Maven coordinate in format "groupId:artifactId" (e.g., "org.springframework:spring-core")',
},
currentVersion: {
type: 'string',
description: 'The version you are currently using (e.g., "5.3.20")',
},
targetVersion: {
type: 'string',
description:
'Optional: The version you want to upgrade to. If not provided, suggests the latest stable.',
},
},
required: ['dependency', 'currentVersion'],
},
};
export async function handler(
coord: MavenCoordinate,
currentVersion: string,
targetVersion?: string
) {
const metadata = await fetchMavenMetadata(coord);
const allVersions = extractVersions(metadata);
// Verify current version exists
if (!allVersions.includes(currentVersion)) {
throw new Error(
`Current version ${currentVersion} not found for ${coord.groupId}:${coord.artifactId}`
);
}
// If no target specified, use latest stable
let target = targetVersion;
if (!target) {
const stableVersions = filterStableVersions(allVersions);
if (stableVersions.length === 0) {
throw new Error('No stable versions available to suggest as upgrade target');
}
const sorted = stableVersions.sort(compareVersions);
target = sorted[sorted.length - 1];
}
// Verify target version exists
if (!allVersions.includes(target)) {
throw new Error(
`Target version ${target} not found for ${coord.groupId}:${coord.artifactId}`
);
}
const upgradeType = determineUpgradeType(currentVersion, target);
const isCurrentStable = isStableVersion(currentVersion);
const isTargetStable = isStableVersion(target);
// Generate recommendation
let recommendation = '';
if (upgradeType === 'same') {
recommendation = 'You are already on this version.';
}
if (upgradeType === 'downgrade') {
recommendation = 'Warning: This would be a downgrade. Generally not recommended.';
}
if (upgradeType === 'patch') {
recommendation = 'Safe upgrade. Patch versions typically contain bug fixes only.';
}
if (upgradeType === 'minor') {
recommendation =
'Review changelog. Minor versions may add new features but should be backward compatible.';
}
if (upgradeType === 'major') {
recommendation =
'Caution: Major version upgrade may contain breaking changes. Review migration guide.';
}
if (!isTargetStable) {
recommendation += ' Warning: Target version is a pre-release.';
}
const result: VersionComparisonResult = {
currentVersion,
targetVersion: target,
upgradeType,
recommendation,
isCurrentStable,
isTargetStable,
};
return {
content: [
{
type: 'text',
text: JSON.stringify(result, null, 2),
},
],
};
}