Skip to main content
Glama

discord_login

Authenticate to Discord using a token to enable AI assistants to interact with Discord servers, channels, and messages through the MCP-Discord server.

Instructions

Logs in to Discord using the configured token

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
tokenNo

Implementation Reference

  • The primary handler function for the 'discord_login' tool. Parses input args using DiscordLoginSchema, checks if already logged in, uses provided or existing token to login the Discord client, waits for 'ready' event using waitForReady helper, updates context, and returns success/error text response.
    export const loginHandler: ToolHandler = async (args, context) => { DiscordLoginSchema.parse(args); try { // Check if client is already logged in if (context.client.isReady()) { return { content: [ { type: 'text', text: `Already logged in as: ${context.client.user?.tag}`, }, ], }; } // Use token from args if provided, otherwise fall back to the client's token const token = args.token || context.client.token; // Check if we have a token to use if (!token) { return { content: [ { type: 'text', text: 'Discord token not provided and not configured. Cannot log in. Please check the following: 1. Provide a token in the login command or make sure the token is correctly set in your config or environment variables. 2. Ensure all required privileged intents (Message Content, Server Members, Presence) are enabled in the Discord Developer Portal for your bot application.', }, ], isError: true, }; } // If token is provided in args, update the client's token if (args.token) { context.client.token = args.token; } // Attempt to log in with the token and get the ready client const readyClient = await waitForReady(context.client, token); // Update the context client with the ready client context.client = readyClient; return { content: [ { type: 'text', text: `Successfully logged in to Discord: ${context.client.user?.tag}`, }, ], }; } catch (err) { error( `Error in login handler: ${err instanceof Error ? err.message : String(err)}` ); return handleDiscordError(err); } };
  • Zod schema defining the input for discord_login tool: optional 'token' string.
    export const DiscordLoginSchema = z.object({ token: z.string().optional(), });
  • src/tool-list.ts:42-52 (registration)
    Tool registration entry in the exported toolList array, providing name, description, and inputSchema matching the Zod schema for MCP list_tools response.
    { name: 'discord_login', description: 'Logs in to Discord using the configured token', inputSchema: { type: 'object', properties: { token: { type: 'string' }, }, required: [], }, },
  • Helper function used by the handler to login the client and wait for the 'ready' event with timeout handling, proper event listener cleanup, and error propagation.
    const waitForReady = async ( client: Client, token: string, timeoutMs = 30_000 ): Promise<Client> => { return await new Promise<Client>((resolve, reject) => { const timeout = setTimeout(() => { reject(new Error(`Client ready event timed out after ${timeoutMs}ms`)); }, timeoutMs); if (client.isReady()) { clearTimeout(timeout); resolve(client); return; } const readyHandler = () => { info('Client ready event received'); clearTimeout(timeout); client.removeListener('error', errorHandler); resolve(client); }; const errorHandler = (err: Error) => { clearTimeout(timeout); client.removeListener('ready', readyHandler); reject(err); }; client.once('ready', readyHandler); client.once('error', errorHandler); info('Starting login process and waiting for ready event'); client.login(token).catch((err: Error) => { clearTimeout(timeout); client.removeListener('ready', readyHandler); client.removeListener('error', errorHandler); reject(err); }); }); };

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/IQAIcom/mcp-discord'

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