create_webhook
Create a Discord webhook in a specified channel to automate message delivery and integrate external services with your server.
Instructions
Create a webhook in 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 | |
| name | Yes | Name for the webhook | |
| avatar | No | URL of the avatar image | |
| reason | No | Reason for creating |
Implementation Reference
- src/tools/webhook-tools.ts:106-133 (handler)The handler function that executes the tool: fetches guild and channel, checks if channel supports webhooks, creates the webhook using Discord.js, handles errors with withErrorHandling, and returns JSON response.async ({ guildId, channelId, name, avatar, reason }) => { const result = await withErrorHandling(async () => { const client = await getDiscordClient(); const guild = await client.guilds.fetch(guildId); const channel = await guild.channels.fetch(channelId); if (!isWebhookableChannel(channel)) { throw new Error('Channel does not support webhooks'); } const webhook = await channel.createWebhook({ name, avatar, reason }); return { id: webhook.id, name: webhook.name, channelId: webhook.channelId, token: webhook.token, url: webhook.url, message: 'Webhook created 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/webhook-tools.ts:99-105 (schema)Zod input schema defining required parameters (guildId, channelId, name) and optional (avatar, reason).{ guildId: z.string().describe('The ID of the server (guild)'), channelId: z.string().describe('The ID of the channel'), name: z.string().describe('Name for the webhook'), avatar: z.string().optional().describe('URL of the avatar image'), reason: z.string().optional().describe('Reason for creating'), },
- src/tools/webhook-tools.ts:96-134 (registration)The server.tool() call that registers the 'create_webhook' tool with name, description, input schema, and handler function on the MCP server.server.tool( 'create_webhook', 'Create a webhook in a channel', { guildId: z.string().describe('The ID of the server (guild)'), channelId: z.string().describe('The ID of the channel'), name: z.string().describe('Name for the webhook'), avatar: z.string().optional().describe('URL of the avatar image'), reason: z.string().optional().describe('Reason for creating'), }, async ({ guildId, channelId, name, avatar, reason }) => { const result = await withErrorHandling(async () => { const client = await getDiscordClient(); const guild = await client.guilds.fetch(guildId); const channel = await guild.channels.fetch(channelId); if (!isWebhookableChannel(channel)) { throw new Error('Channel does not support webhooks'); } const webhook = await channel.createWebhook({ name, avatar, reason }); return { id: webhook.id, name: webhook.name, channelId: webhook.channelId, token: webhook.token, url: webhook.url, message: 'Webhook created 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/webhook-tools.ts:9-16 (helper)Helper type guard function used in the handler to validate if the channel type supports webhooks.function isWebhookableChannel(channel: unknown): channel is WebhookableChannel { 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.GuildVoice || ch.type === ChannelType.GuildForum; }