Skip to main content
Glama
scarecr0w12

discord-mcp

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
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

  • 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) }] };
    }
  • 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'),
    },
  • 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);
  • 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;
    }
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries full burden for behavioral disclosure but only states the basic function. It doesn't mention whether this is a read-only operation, what permissions are required, whether it's paginated, rate limits, or what format the messages are returned in. For a tool with 5 parameters and no annotations, this is inadequate.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence with zero wasted words. It's appropriately sized for a basic retrieval tool and gets straight to the point without unnecessary elaboration.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the complexity (5 parameters, no annotations, no output schema), the description is insufficient. It doesn't explain what the tool returns, how messages are ordered, whether there are limitations on historical access, or what authentication/authorization is required. For a data retrieval tool with multiple parameters, more context is needed.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema already fully documents all 5 parameters with clear descriptions. The tool description adds no additional parameter information beyond what's in the schema, which meets the baseline expectation when schema coverage is complete.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action ('Get messages') and target resource ('from a channel'), providing a specific verb+resource combination. However, it doesn't differentiate from sibling tools like 'get_pinned_messages' or 'list_messages' (if such existed), which would be needed for a perfect score.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

No guidance is provided about when to use this tool versus alternatives like 'get_pinned_messages' or 'search_messages' (if available). The description offers no context about prerequisites, limitations, or appropriate use cases beyond the basic function.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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