From 36bcc3f0c1777ff2821df7790751da1f87a47646 Mon Sep 17 00:00:00 2001 From: vycdev2 Date: Tue, 25 Aug 2026 07:04:55 +0000 Subject: [PATCH] fix: normalize MCP history channel filters --- src/mcp/server.ts | 15 ++++++++++++--- tests/mcpHistory.test.mjs | 4 ++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/mcp/server.ts b/src/mcp/server.ts index 7b2ae8e..2ca87ec 100644 --- a/src/mcp/server.ts +++ b/src/mcp/server.ts @@ -17,7 +17,10 @@ import { PENDING_DIR, } from "../config.js"; import { client } from "../discord/client.js"; -import { findChannel } from "../discord/helpers.js"; +import { + findChannel, + normalizeChannelIdentifier, +} from "../discord/helpers.js"; import { downloadAttachment } from "../storage/images.js"; import { compareHistoryFilenames, @@ -203,8 +206,9 @@ const ReadMessageHistorySchema = z.object({ type: z.enum(["history", "pending"]).default("history"), channel: z .string() + .trim() .refine( - (channel) => /\S/u.test(channel), + (channel) => normalizeChannelIdentifier(channel).length > 0, "Channel must contain at least one non-whitespace character", ) .optional(), @@ -486,7 +490,12 @@ export function createMcpServer(): Server { const { limit, type, channel, date, search, maxLines } = ReadMessageHistorySchema.parse(args ?? {}); const dir = type === "pending" ? PENDING_DIR : HISTORY_DIR; - const safeChannel = channel?.replace(/[^a-zA-Z0-9-_]/g, "_"); + const safeChannel = channel + ? normalizeChannelIdentifier(channel).replace( + /[^a-zA-Z0-9-_]/g, + "_", + ) + : undefined; let files = fs .readdirSync(dir, { withFileTypes: true }) .filter( diff --git a/tests/mcpHistory.test.mjs b/tests/mcpHistory.test.mjs index 531fa11..b103812 100644 --- a/tests/mcpHistory.test.mjs +++ b/tests/mcpHistory.test.mjs @@ -218,7 +218,7 @@ test("MCP history filters matching regular files and preserves pending indentati const channelResult = await client.callTool({ name: "read-message-history", - arguments: { channel: "general", date: "2026-08-01" }, + arguments: { channel: " #general ", date: "2026-08-01" }, }); const channelText = channelResult.content.find( (item) => item.type === "text", @@ -245,7 +245,7 @@ test("MCP history filters matching regular files and preserves pending indentati String.raw`\S`, ); - for (const channel of ["", " \t\n "]) { + for (const channel of ["", " \t\n ", " ## "]) { await assert.rejects( client.callTool({ name: "read-message-history",