assign_role
Assign a specific role to a Discord server member using guild, member, and role IDs to manage permissions and organization.
Instructions
Assign a role to a member
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| guildId | Yes | The ID of the server (guild) | |
| memberId | Yes | The ID of the member | |
| roleId | Yes | The ID of the role to assign | |
| reason | No | Reason for assigning the role |
Implementation Reference
- src/tools/role-tools.ts:219-242 (handler)The core execution logic for the assign_role tool. Fetches the Discord guild, member, and role using the provided IDs, adds the role to the member with optional reason, handles errors with withErrorHandling, and returns formatted response or error.async ({ guildId, memberId, roleId, reason }) => { const result = await withErrorHandling(async () => { const client = await getDiscordClient(); const guild = await client.guilds.fetch(guildId); const member = await guild.members.fetch(memberId); const role = await guild.roles.fetch(roleId); if (!role) throw new Error('Role not found'); await member.roles.add(role, reason); return { memberId, roleId, roleName: role.name, memberName: member.displayName, message: 'Role assigned successfully', }; }); 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:213-218 (schema)Input schema using Zod for validating parameters of the assign_role tool: required guildId, memberId, roleId; optional reason.{ guildId: z.string().describe('The ID of the server (guild)'), memberId: z.string().describe('The ID of the member'), roleId: z.string().describe('The ID of the role to assign'), reason: z.string().optional().describe('Reason for assigning the role'), },
- src/tools/role-tools.ts:210-243 (registration)Registration of the assign_role tool on the MCP server using server.tool(), including name, description, input schema, and handler function.server.tool( 'assign_role', 'Assign a role to a member', { guildId: z.string().describe('The ID of the server (guild)'), memberId: z.string().describe('The ID of the member'), roleId: z.string().describe('The ID of the role to assign'), reason: z.string().optional().describe('Reason for assigning the role'), }, async ({ guildId, memberId, roleId, reason }) => { const result = await withErrorHandling(async () => { const client = await getDiscordClient(); const guild = await client.guilds.fetch(guildId); const member = await guild.members.fetch(memberId); const role = await guild.roles.fetch(roleId); if (!role) throw new Error('Role not found'); await member.roles.add(role, reason); return { memberId, roleId, roleName: role.name, memberName: member.displayName, message: 'Role assigned successfully', }; }); 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:56-56 (registration)High-level registration call to registerRoleTools(server), which registers the assign_role tool (and other role tools) on the MCP server instance.registerRoleTools(server);
- src/index.ts:14-14 (registration)Import of the registerRoleTools function from role-tools.ts, which contains the assign_role tool registration.import { registerRoleTools } from './tools/role-tools.js';