Skip to main content
Glama

x402_session_end

Close an x402 V2 session manually to enhance security by terminating sessions no longer needed, preventing unauthorized access.

Instructions

Explicitly close an x402 V2 session before it expires naturally. After calling this, x402_session_fetch will return an error for the closed session. Useful for security hygiene or when you know a session is no longer needed.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
session_idYesSession ID to close (from x402_session_start)

Implementation Reference

  • The handler function handleX402SessionEnd executes the logic to close a session by ID.
    export async function handleX402SessionEnd(
      input: X402SessionEndInput
    ): Promise<{ content: Array<{ type: 'text'; text: string }>; isError?: boolean }> {
      try {
        const lookup = lookupSession(input.session_id);
    
        if (!lookup.found) {
          return {
            content: [
              textContent(`❌ Session not found: "${input.session_id}"`),
            ],
            isError: true,
          };
        }
    
        if (lookup.expired) {
          return {
            content: [
              textContent(
                `ℹ️ Session "${input.session_id}" was already expired.\n` +
                `Endpoint: ${lookup.session.endpoint}`
              ),
            ],
          };
        }
    
        const { session } = lookup;
        endSession(input.session_id);
    
        return {
          content: [
            textContent(
              `✅ **Session Closed**\n\n` +
              `  Session ID: ${session.sessionId}\n` +
              `  Endpoint:   ${session.endpoint}\n` +
              `  Calls made: ${session.callCount}\n\n` +
              `The session has been closed and can no longer be used.\n` +
              `Use x402_session_start to establish a new session when needed.`
            ),
          ],
        };
      } catch (error: unknown) {
        return {
          content: [textContent(formatError(error, 'x402_session_end'))],
          isError: true,
  • The Zod schema X402SessionEndSchema defines the required input structure (session_id).
    export const X402SessionEndSchema = z.object({
      session_id: z
        .string()
        .uuid()
        .describe('Session ID to close.'),
    });
  • The x402SessionEndTool object contains the metadata and schema definition for MCP tool registration.
    export const x402SessionEndTool = {
      name: 'x402_session_end',
      description:
        'Explicitly close an x402 V2 session before it expires naturally. ' +
        'After calling this, x402_session_fetch will return an error for the closed session. ' +
        'Useful for security hygiene or when you know a session is no longer needed.',
      inputSchema: {
        type: 'object' as const,
        properties: {
          session_id: {
            type: 'string',
            description: 'Session ID to close (from x402_session_start)',
          },
        },
        required: ['session_id'],
      },
    };

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/up2itnow0822/claw-pay-mcp'

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