get_messages
Retrieve messages from a Discord channel by specifying server and channel IDs, with options to limit results or filter by message position.
Instructions
Get messages from a channel
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| guildId | Yes | The ID of the server (guild) | |
| channelId | Yes | The ID of the channel | |
| limit | No | Number of messages to fetch (1-100, default 50) | |
| before | No | Get messages before this message ID | |
| after | No | Get messages after this message ID |
Implementation Reference
- src/tools/message-tools.ts:73-112 (handler)Handler function that implements the core logic of the 'get_messages' tool. It fetches messages from a specified Discord channel using discord.js, applies pagination options (limit, before, after), validates the channel type, formats message details including content, author, timestamps, attachments, embeds, and reactions, handles errors with withErrorHandling, and returns a JSON-formatted response.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/tools/message-tools.ts:66-72 (schema)Input schema for the 'get_messages' tool defined using Zod, specifying required guildId and channelId, and optional limit (default 50, clamped 1-100), before, and after message IDs for pagination.{ 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'), },
- src/tools/message-tools.ts:63-113 (registration)Direct registration of the 'get_messages' tool on the MCP server instance within the registerMessageTools function, specifying name, description, input schema, and inline 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:59-59 (registration)Top-level call to registerMessageTools(server) during MCP server setup, which includes registration of the 'get_messages' tool among other message tools.registerMessageTools(server);
- src/tools/message-tools.ts:9-17 (helper)Type guard helper function used in the get_messages handler to validate that the fetched channel supports sending/receiving messages (TextChannel, NewsChannel, ThreadChannel types).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; }