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
| 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)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) }] }; }
- src/tools/message-tools.ts:66-72 (schema)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'), },
- src/tools/message-tools.ts:63-113 (registration)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);
- src/tools/message-tools.ts:9-17 (helper)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; }