Skip to main content
Glama

get_package_versions

Retrieve all available versions of an npm package to check compatibility, track updates, or analyze release history.

Instructions

Get all available versions of a package

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
packageNameYes

Implementation Reference

  • The handler function for the 'get_package_versions' tool. It fetches package data from the npm registry, validates it using PackageVersionSchema, extracts versions and dist-tags, formats the output as text and structured content.
    async ({ packageName }) => {
      try {
        const response = await fetch(
          `https://registry.npmjs.org/${encodeURIComponent(packageName)}`
        );
    
        if (!response.ok) {
          throw new Error(
            `Failed to fetch package versions: ${response.statusText}`
          );
        }
    
        const rawData = await response.json();
        const parseResult = PackageVersionSchema.safeParse(rawData);
    
        if (!parseResult.success) {
          throw new Error(
            `Invalid package versions structure: ${parseResult.error.message}`
          );
        }
    
        const data = parseResult.data;
        const versions = Object.keys(data.versions);
        const latest = data["dist-tags"].latest;
        const tags = Object.entries(data["dist-tags"])
          .map(([tag, version]) => `${tag}: ${version}`)
          .join("\n");
    
        const output = {
          name: data.name,
          latest,
          distTags: data["dist-tags"],
          versions,
          versionCount: versions.length,
        };
    
        return {
          content: [
            {
              type: "text",
              text: `Package: ${
                data.name
              }\n\nLatest version: ${latest}\n\nDist tags:\n${tags}\n\nAll versions (${
                versions.length
              }):\n${versions.join(", ")}`,
            },
          ],
          structuredContent: output,
        };
      } catch (error) {
        return {
          content: [
            {
              type: "text",
              text: `Error fetching package versions: ${
                error instanceof Error ? error.message : "Unknown error"
              }`,
            },
          ],
          isError: true,
        };
      }
    }
  • Zod schema for validating the npm registry response containing package versions, versions object, and dist-tags.
    const PackageVersionSchema = z.object({
      name: z.string(),
      versions: z.record(
        z.string(),
        z.object({
          version: z.string(),
          publish_time: z.string().optional(),
        })
      ),
      "dist-tags": z.record(z.string(), z.string()),
    });
  • src/index.ts:360-439 (registration)
    MCP server registration of the 'get_package_versions' tool, defining title, description, input/output schemas, and attaching the handler function.
    server.registerTool(
      "get_package_versions",
      {
        title: "Get Package Versions",
        description: "Get all available versions of a package",
        inputSchema: {
          packageName: z.string(),
        },
        outputSchema: {
          name: z.string(),
          latest: z.string(),
          distTags: z.record(z.string(), z.string()),
          versions: z.array(z.string()),
          versionCount: z.number(),
        },
      },
      async ({ packageName }) => {
        try {
          const response = await fetch(
            `https://registry.npmjs.org/${encodeURIComponent(packageName)}`
          );
    
          if (!response.ok) {
            throw new Error(
              `Failed to fetch package versions: ${response.statusText}`
            );
          }
    
          const rawData = await response.json();
          const parseResult = PackageVersionSchema.safeParse(rawData);
    
          if (!parseResult.success) {
            throw new Error(
              `Invalid package versions structure: ${parseResult.error.message}`
            );
          }
    
          const data = parseResult.data;
          const versions = Object.keys(data.versions);
          const latest = data["dist-tags"].latest;
          const tags = Object.entries(data["dist-tags"])
            .map(([tag, version]) => `${tag}: ${version}`)
            .join("\n");
    
          const output = {
            name: data.name,
            latest,
            distTags: data["dist-tags"],
            versions,
            versionCount: versions.length,
          };
    
          return {
            content: [
              {
                type: "text",
                text: `Package: ${
                  data.name
                }\n\nLatest version: ${latest}\n\nDist tags:\n${tags}\n\nAll versions (${
                  versions.length
                }):\n${versions.join(", ")}`,
              },
            ],
            structuredContent: output,
          };
        } catch (error) {
          return {
            content: [
              {
                type: "text",
                text: `Error fetching package versions: ${
                  error instanceof Error ? error.message : "Unknown error"
                }`,
              },
            ],
            isError: true,
          };
        }
      }
    );

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/JuanSebastianGB/npm-context-agent-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server