From 9cdafc31c077f293a9cae0f67fa7966b86072ccf Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 11 Oct 2025 22:54:40 +0000 Subject: [PATCH] Add discord_list_guilds tool to list bot's guild memberships Co-authored-by: SootyOwl <7614538+SootyOwl@users.noreply.github.com> --- src/schemas.ts | 4 +++- src/server.ts | 8 +++++++- src/toolList.ts | 9 +++++++++ src/tools/channel.ts | 39 ++++++++++++++++++++++++++++++++++++++- src/tools/tools.ts | 6 ++++-- src/transport.ts | 3 ++- 6 files changed, 63 insertions(+), 6 deletions(-) diff --git a/src/schemas.ts b/src/schemas.ts index a492989..a37b4a1 100644 --- a/src/schemas.ts +++ b/src/schemas.ts @@ -129,4 +129,6 @@ export const DeleteWebhookSchema = z.object({ webhookId: z.string(), webhookToken: z.string().optional(), reason: z.string().optional() -}); \ No newline at end of file +}); + +export const ListGuildsSchema = z.object({}); \ No newline at end of file diff --git a/src/server.ts b/src/server.ts index 9983e7a..0b66570 100644 --- a/src/server.ts +++ b/src/server.ts @@ -29,7 +29,8 @@ import { deleteWebhookHandler, createCategoryHandler, editCategoryHandler, - deleteCategoryHandler + deleteCategoryHandler, + listGuildsHandler } from './tools/tools.js'; import { MCPTransport } from './transport.js'; import { info, error } from './logger.js'; @@ -178,6 +179,11 @@ export class DiscordMCPServer { toolResponse = await deleteWebhookHandler(args, this.toolContext); return toolResponse; + case "discord_list_guilds": + this.logClientState("before discord_list_guilds handler"); + toolResponse = await listGuildsHandler(args, this.toolContext); + return toolResponse; + default: throw new Error(`Unknown tool: ${name}`); } diff --git a/src/toolList.ts b/src/toolList.ts index bf4b939..6cde2db 100644 --- a/src/toolList.ts +++ b/src/toolList.ts @@ -292,5 +292,14 @@ export const toolList = [ }, required: ["webhookId"] } + }, + { + name: "discord_list_guilds", + description: "Lists all Discord servers (guilds) the bot is a member of", + inputSchema: { + type: "object", + properties: {}, + required: [] + } } ]; \ No newline at end of file diff --git a/src/tools/channel.ts b/src/tools/channel.ts index 1a406c5..8ff6e4a 100644 --- a/src/tools/channel.ts +++ b/src/tools/channel.ts @@ -8,7 +8,8 @@ import { GetServerInfoSchema, CreateCategorySchema, EditCategorySchema, - DeleteCategorySchema + DeleteCategorySchema, + ListGuildsSchema } from "../schemas.js"; import { handleDiscordError } from "../errorHandler.js"; @@ -355,4 +356,40 @@ export async function getServerInfoHandler( } catch (error) { return handleDiscordError(error); } +} + +// List guilds handler +export async function listGuildsHandler( + args: unknown, + context: ToolContext +): Promise { + ListGuildsSchema.parse(args); + try { + if (!context.client.isReady()) { + return { + content: [{ type: "text", text: "Discord client not logged in." }], + isError: true + }; + } + + const guilds = await context.client.guilds.fetch(); + + if (guilds.size === 0) { + return { + content: [{ type: "text", text: "No guilds found. The bot is not a member of any servers." }] + }; + } + + const guildsInfo = guilds.map(guild => ({ + id: guild.id, + name: guild.name, + icon: guild.iconURL() + })); + + return { + content: [{ type: "text", text: JSON.stringify(guildsInfo, null, 2) }] + }; + } catch (error) { + return handleDiscordError(error); + } } \ No newline at end of file diff --git a/src/tools/tools.ts b/src/tools/tools.ts index 4540b62..2d8606f 100644 --- a/src/tools/tools.ts +++ b/src/tools/tools.ts @@ -17,7 +17,8 @@ import { getServerInfoHandler, createCategoryHandler, editCategoryHandler, - deleteCategoryHandler + deleteCategoryHandler, + listGuildsHandler } from './channel.js'; import { addReactionHandler, @@ -55,7 +56,8 @@ export { deleteWebhookHandler, createCategoryHandler, editCategoryHandler, - deleteCategoryHandler + deleteCategoryHandler, + listGuildsHandler }; // Export common types diff --git a/src/transport.ts b/src/transport.ts index aebf81d..f35979f 100644 --- a/src/transport.ts +++ b/src/transport.ts @@ -26,7 +26,8 @@ import { deleteWebhookHandler, editCategoryHandler, createCategoryHandler, - deleteCategoryHandler + deleteCategoryHandler, + listGuildsHandler } from './tools/tools.js'; import { Client, GatewayIntentBits } from "discord.js"; import { info, error } from './logger.js';