Skip to content

Email for professionals and teams

fix(security): mark MCP results untrusted and audit tool calls (#225)

Hieu Nguyen (Jack)9647c178c27e
diff --git a/src/lib/mcp/server.ts b/src/lib/mcp/server.ts
index f9846ec..41c4b20 100644
--- a/src/lib/mcp/server.ts
+++ b/src/lib/mcp/server.ts
@@ -22,6 +22,16 @@ function output(value: unknown, isError = false) {
return { content: [{ type: "text" as const, text: JSON.stringify(value) }], isError };
}
+/** Mail and calendar text came from third parties; say so in the data the model reads, not only in the server instructions. */
+function untrusted(value: unknown) {
+ return value && typeof value === "object" && !Array.isArray(value) ? { contentTrust: "untrusted_third_party_content", ...value } : value;
+}
+
+/** One line per tool call so reads and writes done through an MCP key can be traced afterwards. */
+function audit(principal: McpPrincipal, tool: string, mailboxId?: string) {
+ console.info(JSON.stringify({ audit: "mcp", tool, userId: principal.user.id, mailboxId }));
+}
+
export function createMailflareMcpHandler(env: CloudflareEnv, principal: McpPrincipal, baseUrl: string, authorization: string) {
return createMcpHandler(() => {
const server = new McpServer({ name: "mailflare", version: "0.1.0" }, {
@@ -31,7 +41,8 @@ export function createMailflareMcpHandler(env: CloudflareEnv, principal: McpPrin
for (const name of CALENDAR_TOOL_NAMES) {
server.registerTool(name, { description: calendarToolDescriptions[name], inputSchema: calendarToolSchemas[name], annotations: { readOnlyHint: calendarToolScope(name) === "mcp:calendar-read", destructiveHint: name === "update_events" || name === "delete_events", openWorldHint: false } }, async (args) => {
if (!principal.scopes.includes(calendarToolScope(name))) return output({ error: "Permission denied" }, true);
- try { return output(await runCalendarTool({ env, user: principal.user }, name, args)); }
+ audit(principal, name);
+ try { return output(untrusted(await runCalendarTool({ env, user: principal.user }, name, args))); }
catch (error) { return output({ error: error instanceof Error ? error.message : "Calendar tool failed" }, true); }
});
}
@@ -44,7 +55,8 @@ export function createMailflareMcpHandler(env: CloudflareEnv, principal: McpPrin
if (name === "edit_draft" || name === "review_draft_send") continue;
server.registerTool(name, { description: emailToolDescriptions[name], inputSchema: z.object({ mailboxId: z.string().min(1), ...emailToolSchemas[name].shape }) }, async (args) => {
if (!principal.scopes.includes(scopeByTool[name]) || !principal.mailboxIds.includes(args.mailboxId)) return output({ error: "Permission denied" }, true);
- try { return output(await runEmailTool({ env, user: principal.user, mailboxId: args.mailboxId, origin: "mcp" }, name, args)); }
+ audit(principal, name, args.mailboxId);
+ try { return output(untrusted(await runEmailTool({ env, user: principal.user, mailboxId: args.mailboxId, origin: "mcp" }, name, args))); }
catch (error) { return output({ error: error instanceof Error ? error.message : "Tool failed" }, true); }
});
}