UNPKG

mcp-package-version

Version:

An MCP server to provide LLMs the latest (stable) version of packages in package.json and requirements.txt files

73 lines (72 loc) 2.69 kB
import axios from 'axios'; import { McpError, ErrorCode } from '@modelcontextprotocol/sdk/types.js'; export class GoHandler { proxyBase = 'https://proxy.golang.org'; async getPackageVersion(path, currentVersion) { try { // First get the list of versions const response = await axios.get(`${this.proxyBase}/${path}/@v/list`); const versions = response.data.trim().split('\n'); if (!versions.length) { throw new Error('No versions found'); } // Get the latest version (last in the list) const latestVersion = versions[versions.length - 1]; const result = { name: path, latestVersion, registry: 'go', }; if (currentVersion) { // Remove any 'v' prefix from the current version const cleanCurrentVersion = currentVersion.replace(/^v/, ''); result.currentVersion = cleanCurrentVersion; } return result; } catch (error) { console.error(`Error fetching Go package ${path}:`, error); throw new McpError(ErrorCode.InternalError, `Failed to fetch Go package ${path}`); } } async getLatestVersion(args) { if (!args.dependencies || !args.dependencies.require) { throw new McpError(ErrorCode.InvalidParams, 'Invalid Go module dependencies'); } const results = []; const { require = [], replace = [] } = args.dependencies; // Process required dependencies for (const dep of require) { if (!dep.path) continue; try { const result = await this.getPackageVersion(dep.path, dep.version); results.push(result); } catch (error) { console.error(`Error checking Go package ${dep.path}:`, error); } } // Process replaced dependencies for (const rep of replace) { if (!rep.old || !rep.new) continue; try { const result = await this.getPackageVersion(rep.new, rep.version); result.name = `${rep.new} (replaces ${rep.old})`; results.push(result); } catch (error) { console.error(`Error checking Go package ${rep.new}:`, error); } } return { content: [ { type: 'text', text: JSON.stringify(results, null, 2), }, ], }; } }