w
This commit is contained in:
+146
@@ -1,5 +1,7 @@
|
||||
import { type McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
||||
import { z } from "zod/v4";
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { EwsClient, loadConfig } from "../ews_client.ts";
|
||||
|
||||
function getClient(): EwsClient {
|
||||
@@ -285,6 +287,150 @@ export function registerEmailTools(server: McpServer): void {
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
server.registerTool(
|
||||
"mark_email_read",
|
||||
{
|
||||
description: "Mark one or more emails as read or unread",
|
||||
inputSchema: z.object({
|
||||
itemIds: z.array(z.string()).describe("List of Exchange ItemIds to update"),
|
||||
isRead: z.boolean().default(true).describe("True to mark as read, False to mark as unread"),
|
||||
}),
|
||||
},
|
||||
async ({ itemIds, isRead }) => {
|
||||
try {
|
||||
const client = new EwsClient(loadConfig());
|
||||
const responses = await client.updateItems(itemIds, [
|
||||
{ fieldUri: "message:IsRead", value: isRead },
|
||||
]);
|
||||
|
||||
const errors = responses
|
||||
.filter((r: any) => r.ResponseClass === "Error")
|
||||
.map((r: any) => r.MessageText ?? "Unknown error");
|
||||
|
||||
if (errors.length) {
|
||||
return {
|
||||
content: [{ type: "text" as const, text: JSON.stringify({ error: errors.join("; ") }) }],
|
||||
};
|
||||
}
|
||||
|
||||
const status = isRead ? "read" : "unread";
|
||||
return {
|
||||
content: [{
|
||||
type: "text" as const,
|
||||
text: JSON.stringify({
|
||||
success: true,
|
||||
message: `Marked ${itemIds.length} email(s) as ${status}.`,
|
||||
}),
|
||||
}],
|
||||
};
|
||||
} catch (error: any) {
|
||||
return {
|
||||
content: [{
|
||||
type: "text" as const,
|
||||
text: JSON.stringify({ error: error.message ?? String(error) }),
|
||||
}],
|
||||
};
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
server.registerTool(
|
||||
"download_attachments",
|
||||
{
|
||||
description: "Download all file attachments from an email to disk",
|
||||
inputSchema: z.object({
|
||||
itemId: z.string().describe("The Exchange ItemId of the email"),
|
||||
targetFolder: z.string().default("/tmp/attachments").describe(
|
||||
"Local directory to save files (default /tmp/attachments)",
|
||||
),
|
||||
}),
|
||||
},
|
||||
async ({ itemId, targetFolder }) => {
|
||||
try {
|
||||
const client = new EwsClient(loadConfig());
|
||||
const item = await client.getItem(itemId);
|
||||
const email = client.extractEmailDetails(item);
|
||||
const attachments = email.attachments ?? [];
|
||||
|
||||
const fileAttachments = attachments.filter(
|
||||
(a: any) => a.attachmentId && !a.isInline,
|
||||
);
|
||||
|
||||
if (!fileAttachments.length) {
|
||||
return {
|
||||
content: [{
|
||||
type: "text" as const,
|
||||
text: JSON.stringify({
|
||||
success: true,
|
||||
downloaded: [],
|
||||
count: 0,
|
||||
message: "No downloadable file attachments.",
|
||||
}),
|
||||
}],
|
||||
};
|
||||
}
|
||||
|
||||
fs.mkdirSync(targetFolder, { recursive: true });
|
||||
|
||||
const downloaded = [];
|
||||
const errors = [];
|
||||
const usedNames = new Set<string>();
|
||||
|
||||
for (const att of fileAttachments) {
|
||||
try {
|
||||
const result = await client.getAttachment(att.attachmentId);
|
||||
let filename = path.basename(result.name);
|
||||
if (!filename) filename = att.name || "attachment";
|
||||
|
||||
const baseName = filename;
|
||||
const dotIdx = baseName.lastIndexOf(".");
|
||||
const namePart = dotIdx > 0 ? baseName.slice(0, dotIdx) : baseName;
|
||||
const extPart = dotIdx > 0 ? baseName.slice(dotIdx) : "";
|
||||
|
||||
let counter = 1;
|
||||
let finalName = filename;
|
||||
while (usedNames.has(finalName.toLowerCase())) {
|
||||
finalName = extPart ? `${namePart}_${counter}${extPart}` : `${namePart}_${counter}`;
|
||||
counter++;
|
||||
}
|
||||
usedNames.add(finalName.toLowerCase());
|
||||
|
||||
const filepath = path.join(targetFolder, finalName);
|
||||
fs.writeFileSync(filepath, result.content);
|
||||
|
||||
downloaded.push({
|
||||
name: finalName,
|
||||
path: filepath,
|
||||
size: result.content.length,
|
||||
contentType: result.contentType,
|
||||
});
|
||||
} catch (e: any) {
|
||||
errors.push({ name: att.name || "unknown", error: e.message ?? String(e) });
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
content: [{
|
||||
type: "text" as const,
|
||||
text: JSON.stringify({
|
||||
success: errors.length === 0,
|
||||
downloaded,
|
||||
count: downloaded.length,
|
||||
...(errors.length ? { errors } : {}),
|
||||
}),
|
||||
}],
|
||||
};
|
||||
} catch (error: any) {
|
||||
return {
|
||||
content: [{
|
||||
type: "text" as const,
|
||||
text: JSON.stringify({ error: error.message ?? String(error) }),
|
||||
}],
|
||||
};
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
function formatSearchResults(
|
||||
|
||||
Reference in New Issue
Block a user