Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/mcp/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions tests/mcpHistory.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down