list_roles
Retrieve all role names and IDs from a Discord server using the server's guild ID to manage permissions and member assignments.
Instructions
List all roles in a Discord server
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| guildId | Yes | The ID of the server (guild) |
Implementation Reference
- src/tools/role-tools.ts:15-40 (handler)Executes the list_roles tool: fetches Discord guild roles using discord.js, extracts key properties (id, name, color, position, hoist, mentionable, managed, permissions array, member count), sorts by descending position, handles errors with withErrorHandling, and returns JSON-formatted list or error.async ({ guildId }) => { const result = await withErrorHandling(async () => { const client = await getDiscordClient(); const guild = await client.guilds.fetch(guildId); const roles = await guild.roles.fetch(); return roles.map((role) => ({ id: role.id, name: role.name, color: role.hexColor, position: role.position, hoist: role.hoist, mentionable: role.mentionable, managed: role.managed, permissions: role.permissions.toArray(), memberCount: role.members.size, })).sort((a, b) => b.position - a.position); }); 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/role-tools.ts:12-14 (schema)Input schema for list_roles tool: requires 'guildId' as a string, described as the ID of the server (guild). Uses Zod for validation.{ guildId: z.string().describe('The ID of the server (guild)'), },
- src/tools/role-tools.ts:9-40 (registration)Registers the 'list_roles' tool on the MCP server with name, description, input schema (guildId), and inline handler function.server.tool( 'list_roles', 'List all roles in a Discord server', { guildId: z.string().describe('The ID of the server (guild)'), }, async ({ guildId }) => { const result = await withErrorHandling(async () => { const client = await getDiscordClient(); const guild = await client.guilds.fetch(guildId); const roles = await guild.roles.fetch(); return roles.map((role) => ({ id: role.id, name: role.name, color: role.hexColor, position: role.position, hoist: role.hoist, mentionable: role.mentionable, managed: role.managed, permissions: role.permissions.toArray(), memberCount: role.members.size, })).sort((a, b) => b.position - a.position); }); if (!result.success) { return { content: [{ type: 'text', text: result.error }], isError: true }; } return { content: [{ type: 'text', text: JSON.stringify(result.data, null, 2) }] }; } );