discord_read_messages
Retrieve messages from a Discord text channel by specifying the channel ID and a message limit. Designed for the MCP-Discord server to enable AI assistants to access and process channel data.
Instructions
Retrieves messages from a Discord text channel with a configurable limit
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| channelId | Yes | ||
| limit | No |
Implementation Reference
- src/index.ts:862-928 (handler)The handler function for the 'discord_read_messages' tool. It validates input using ReadMessagesSchema, fetches the Discord channel, retrieves the specified number of recent messages, formats them with details like author, timestamp, attachments, etc., sorts them chronologically, and returns the formatted data as JSON.case "discord_read_messages": { const { channelId, limit } = ReadMessagesSchema.parse(args); try { if (!client.isReady()) { return { content: [{ type: "text", text: "Discord client not logged in. Please use discord_login tool first." }], isError: true }; } const channel = await client.channels.fetch(channelId); if (!channel) { return { content: [{ type: "text", text: `Cannot find channel with ID: ${channelId}` }], isError: true }; } // Check if channel has messages (text channel, thread, etc.) if (!channel.isTextBased() || !('messages' in channel)) { return { content: [{ type: "text", text: `Channel type does not support reading messages` }], isError: true }; } // Fetch messages const messages = await channel.messages.fetch({ limit }); if (messages.size === 0) { return { content: [{ type: "text", text: `No messages found in channel` }] }; } // Format messages const formattedMessages = messages.map(msg => ({ id: msg.id, content: msg.content, author: { id: msg.author.id, username: msg.author.username, bot: msg.author.bot }, timestamp: msg.createdAt, attachments: msg.attachments.size, embeds: msg.embeds.length, replyTo: msg.reference ? msg.reference.messageId : null })).sort((a, b) => a.timestamp.getTime() - b.timestamp.getTime()); return { content: [{ type: "text", text: JSON.stringify({ channelId, messageCount: formattedMessages.length, messages: formattedMessages }, null, 2) }] }; } catch (error) { return { content: [{ type: "text", text: `Failed to read messages: ${error}` }], isError: true }; } }
- src/index.ts:179-182 (schema)Zod schema defining the input parameters for the 'discord_read_messages' tool: required channelId (string) and optional limit (number between 1-100, default 50). Used for input validation in the handler.const ReadMessagesSchema = z.object({ channelId: z.string(), limit: z.number().min(1).max(100).optional().default(50) });
- src/index.ts:303-319 (registration)Tool registration in the ListToolsRequestSchema handler. Defines the tool name, description, and input schema matching ReadMessagesSchema for MCP tool discovery.{ name: "discord_read_messages", description: "Retrieves messages from a Discord text channel with a configurable limit", inputSchema: { type: "object", properties: { channelId: { type: "string" }, limit: { type: "number", minimum: 1, maximum: 100, default: 50 } }, required: ["channelId"] } },