Skip to main content
Glama
rog0x
by rog0x

jwt_decode

Decode JWT tokens to inspect header, payload, expiry, and issued-at time without verification. Use this tool to analyze token contents and check expiration status for debugging or testing purposes.

Instructions

Decode a JWT token without verification. Returns header, payload, expiry, issued-at time, and whether the token is expired.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
tokenYesThe JWT token string to decode

Implementation Reference

  • The implementation of the jwt_decode tool handler.
    export function jwtDecode(token: string): JwtDecodeResult {
      const trimmed = token.trim();
      const parts = trimmed.split(".");
    
      if (parts.length < 2 || parts.length > 3) {
        throw new Error(
          `Invalid JWT: expected 2 or 3 parts separated by dots, got ${parts.length}`
        );
      }
    
      let header: Record<string, unknown>;
      let payload: Record<string, unknown>;
    
      try {
        header = JSON.parse(base64UrlDecode(parts[0]));
      } catch {
        throw new Error("Invalid JWT: unable to decode header");
      }
    
      try {
        payload = JSON.parse(base64UrlDecode(parts[1]));
      } catch {
        throw new Error("Invalid JWT: unable to decode payload");
      }
    
      const now = Date.now();
      const exp = typeof payload.exp === "number" ? payload.exp * 1000 : null;
      const isExpired = exp !== null ? now > exp : false;
      const timeUntilExpiry = exp !== null ? formatDuration(exp - now) : null;
    
      return {
        header,
        payload,
        signature_present: parts.length === 3 && parts[2].length > 0,
        issued_at: timestampToIso(payload.iat),
        expires_at: timestampToIso(payload.exp),
        not_before: timestampToIso(payload.nbf),
        is_expired: isExpired,
        time_until_expiry: timeUntilExpiry,
      };
    }
  • The interface defining the structure of the decoded JWT result.
    export interface JwtDecodeResult {
      header: Record<string, unknown>;
      payload: Record<string, unknown>;
      signature_present: boolean;
      issued_at: string | null;
      expires_at: string | null;
      not_before: string | null;
      is_expired: boolean;
      time_until_expiry: string | null;
    }
  • src/index.ts:110-124 (registration)
    The MCP tool registration schema for jwt_decode.
    {
      name: "jwt_decode",
      description:
        "Decode a JWT token without verification. Returns header, payload, expiry, issued-at time, and whether the token is expired.",
      inputSchema: {
        type: "object" as const,
        properties: {
          token: {
            type: "string",
            description: "The JWT token string to decode",
          },
        },
        required: ["token"],
      },
    },
  • The request handler switch case for the jwt_decode tool.
    case "jwt_decode": {
      const result = jwtDecode(args?.token as string);
      return {
        content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
      };
    }

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/rog0x/mcp-api-tools'

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