Skip to main content
Glama

get_messages

Retrieve messages from a Discord channel by specifying server and channel IDs, with options to limit results or fetch messages around specific points.

Instructions

Get messages from a channel

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
guildIdYesThe ID of the server (guild)
channelIdYesThe ID of the channel
limitNoNumber of messages to fetch (1-100, default 50)
beforeNoGet messages before this message ID
afterNoGet messages after this message ID

Implementation Reference

  • The handler function for the 'get_messages' tool. Fetches recent messages from a Discord channel with optional pagination (before/after) and limit (clamped 1-100), checks channel type, formats message data including content, author, timestamps, attachments, embeds count, and reactions, wraps in error handling, and returns JSON string.
    async ({ guildId, channelId, limit = 50, before, after }) => { const result = await withErrorHandling(async () => { const client = await getDiscordClient(); const guild = await client.guilds.fetch(guildId); const channel = await guild.channels.fetch(channelId); if (!isMessageableChannel(channel)) { throw new Error('Channel does not support messages'); } const fetchOptions: { limit: number; before?: string; after?: string } = { limit: Math.min(Math.max(1, limit), 100), }; if (before) fetchOptions.before = before; if (after) fetchOptions.after = after; const messages = await channel.messages.fetch(fetchOptions); return messages.map((msg) => ({ id: msg.id, content: msg.content, authorId: msg.author.id, authorUsername: msg.author.username, createdAt: msg.createdAt.toISOString(), editedAt: msg.editedAt?.toISOString(), pinned: msg.pinned, attachments: msg.attachments.map((a) => ({ id: a.id, url: a.url, name: a.name })), embeds: msg.embeds.length, reactions: msg.reactions.cache.map((r) => ({ emoji: r.emoji.name, count: r.count, })), })); }); if (!result.success) { return { content: [{ type: 'text', text: result.error }], isError: true }; } return { content: [{ type: 'text', text: JSON.stringify(result.data, null, 2) }] }; }
  • Zod input schema for 'get_messages' tool defining required guildId and channelId, optional limit, before, and after parameters.
    { guildId: z.string().describe('The ID of the server (guild)'), channelId: z.string().describe('The ID of the channel'), limit: z.number().optional().describe('Number of messages to fetch (1-100, default 50)'), before: z.string().optional().describe('Get messages before this message ID'), after: z.string().optional().describe('Get messages after this message ID'), },
  • Registers the 'get_messages' tool on the MCP server within registerMessageTools function, specifying name, description, input schema, and handler.
    server.tool( 'get_messages', 'Get messages from a channel', { guildId: z.string().describe('The ID of the server (guild)'), channelId: z.string().describe('The ID of the channel'), limit: z.number().optional().describe('Number of messages to fetch (1-100, default 50)'), before: z.string().optional().describe('Get messages before this message ID'), after: z.string().optional().describe('Get messages after this message ID'), }, async ({ guildId, channelId, limit = 50, before, after }) => { const result = await withErrorHandling(async () => { const client = await getDiscordClient(); const guild = await client.guilds.fetch(guildId); const channel = await guild.channels.fetch(channelId); if (!isMessageableChannel(channel)) { throw new Error('Channel does not support messages'); } const fetchOptions: { limit: number; before?: string; after?: string } = { limit: Math.min(Math.max(1, limit), 100), }; if (before) fetchOptions.before = before; if (after) fetchOptions.after = after; const messages = await channel.messages.fetch(fetchOptions); return messages.map((msg) => ({ id: msg.id, content: msg.content, authorId: msg.author.id, authorUsername: msg.author.username, createdAt: msg.createdAt.toISOString(), editedAt: msg.editedAt?.toISOString(), pinned: msg.pinned, attachments: msg.attachments.map((a) => ({ id: a.id, url: a.url, name: a.name })), embeds: msg.embeds.length, reactions: msg.reactions.cache.map((r) => ({ emoji: r.emoji.name, count: r.count, })), })); }); if (!result.success) { return { content: [{ type: 'text', text: result.error }], isError: true }; } return { content: [{ type: 'text', text: JSON.stringify(result.data, null, 2) }] }; } );
  • src/index.ts:58-58 (registration)
    Calls registerMessageTools(server) in createMcpServer to register all message tools including 'get_messages'.
    registerMessageTools(server);
  • Helper type guard function used in get_messages handler to validate if the fetched channel supports sending/receiving messages (Text, News, Thread channels).
    function isMessageableChannel(channel: unknown): channel is MessageableChannel { if (!channel || typeof channel !== 'object') return false; const ch = channel as { type?: number }; return ch.type === ChannelType.GuildText || ch.type === ChannelType.GuildAnnouncement || ch.type === ChannelType.PublicThread || ch.type === ChannelType.PrivateThread || ch.type === ChannelType.AnnouncementThread; }

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

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