This commit is contained in:
2026-07-10 16:47:35 +03:00
parent 1599bf3981
commit 979e974521
6 changed files with 268 additions and 72 deletions
+97 -43
View File
@@ -12,34 +12,40 @@ export function registerEmailTools(server: McpServer): void {
server.registerTool(
"get_emails",
{
description:
"Get emails from a mailbox folder. Supports optional text search via query/queryScope. Returns {emails, count} or {itemIds, count} if idsOnly. Defaults: folder=Inbox, limit=10, offset=0, includeBody=false, unreadOnly=false, idsOnly=false, queryScope=all. Note: idsOnly raises the limit to 500.",
description: "Get emails from a mailbox folder."
+ " Supports optional text search via query/queryScope.",
inputSchema: z.object({
folder: z.string().default("Inbox").describe(
"Folder name (Inbox, Sent, Drafts, Deleted, Junk, or custom). Supports Russian folder names",
),
limit: z.number().default(10).describe(
"Maximum number of emails to return. Default: 10. Max 50 (max 500 if idsOnly=true)",
"Maximum number of emails to return",
),
offset: z.number().default(0).describe(
"Number of emails to skip for pagination. Default: 0",
"Number of emails to skip for pagination",
),
includeBody: z.boolean().default(false).describe(
"If true, fetches full email body for each email (slower). Default: false",
"If true, fetches full email body for each email (slower)",
),
unreadOnly: z.boolean().default(false).describe(
"If true, only return unread emails. Default: false",
),
idsOnly: z.boolean().default(false).describe(
"If true, return only item IDs, dates, and subjects — faster with higher limit of 500. Default: false",
"If true, only return unread emails",
),
query: z.string().optional().describe(
"Optional text to search for within the folder",
),
queryScope: z.enum(["all", "subject", "body", "from"]).default("all")
.describe(
"Scope for text search. Values: all (subject+body), subject, body, from. Only used when query is set. Default: all",
),
.describe("Search scope: all, subject, body, or from"),
}),
outputSchema: z.object({
emails: z.array(z.object({
itemId: z.string(),
subject: z.string(),
from: z.string(),
date: z.string(),
isRead: z.boolean(),
hasAttachments: z.boolean(),
})),
count: z.number(),
}),
},
async (
@@ -49,19 +55,18 @@ export function registerEmailTools(server: McpServer): void {
offset,
includeBody,
unreadOnly,
idsOnly,
query,
queryScope,
},
) => {
try {
const client = getClient();
const maxLimit = idsOnly ? 500 : 50;
const maxLimit = 50;
if (limit > maxLimit) limit = maxLimit;
offset = Math.max(0, offset);
const folderId = await client.getFolderId(folder);
const baseShape = idsOnly ? "IdOnly" : "AllProperties";
const baseShape = "AllProperties";
if (query && !query.trim()) query = undefined;
@@ -112,16 +117,16 @@ export function registerEmailTools(server: McpServer): void {
let restriction = "";
if (restrictions.length === 1) {
restriction = `\
<m:Restriction>
${restrictions[0]}
</m:Restriction>`;
<m:Restriction>
${restrictions[0]}
</m:Restriction>`;
} else if (restrictions.length > 1) {
restriction = `\
<m:Restriction>
<t:And>
${restrictions.join("\n")}
</t:And>
</m:Restriction>`;
<m:Restriction>
<t:And>
${restrictions.join("\n")}
</t:And>
</m:Restriction>`;
}
const items = await client.findItems(folderId, {
@@ -131,20 +136,6 @@ export function registerEmailTools(server: McpServer): void {
restriction,
});
if (idsOnly) {
const result = items.map((item: any) => ({
itemId: item.ItemId?.["@_Id"] ?? "",
date: item.DateTimeReceived ?? "",
subject: item.Subject ?? "",
}));
return {
content: [{
type: "text" as const,
text: JSON.stringify({ itemIds: result, count: result.length }),
}],
};
}
const emails = [];
for (const item of items) {
const email = client.extractEmailSummary(item);
@@ -165,6 +156,7 @@ export function registerEmailTools(server: McpServer): void {
type: "text" as const,
text: JSON.stringify({ emails, count: emails.length }),
}],
structuredContent: { emails, count: emails.length },
};
} catch (error: any) {
return {
@@ -181,12 +173,32 @@ export function registerEmailTools(server: McpServer): void {
"get_email",
{
description:
"Get a single email with full body and details by Exchange ItemId. Returns the full Email object (subject, from, to, cc, body, date, attachments, etc.).",
"Get a single email with full body and details by Exchange ItemId.",
inputSchema: z.object({
itemId: z.string().describe(
"The Exchange ItemId of the email to retrieve",
),
}),
outputSchema: z.object({
itemId: z.string(),
subject: z.string(),
from: z.string(),
fromName: z.string().optional(),
to: z.array(z.string()).optional(),
cc: z.array(z.string()).optional(),
date: z.string(),
body: z.string().optional(),
isRead: z.boolean().optional(),
hasAttachments: z.boolean().optional(),
hasLinks: z.boolean().optional(),
attachments: z.array(z.object({
name: z.string(),
size: z.number(),
contentType: z.string(),
attachmentId: z.string().optional(),
isInline: z.boolean().optional(),
})).optional(),
}),
},
async ({ itemId }) => {
try {
@@ -196,6 +208,7 @@ export function registerEmailTools(server: McpServer): void {
return {
content: [{ type: "text" as const, text: JSON.stringify(email) }],
structuredContent: { emails: [email] },
};
} catch (error: any) {
return {
@@ -212,15 +225,20 @@ export function registerEmailTools(server: McpServer): void {
"mark_email_read",
{
description:
"Mark one or more emails as read or unread by their Exchange ItemIds. Returns {success, message} or {error}.",
"Mark one or more emails as read or unread by their Exchange ItemIds.",
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. Default: true",
"True to mark as read, false to mark as unread",
),
}),
outputSchema: z.object({
success: z.boolean(),
message: z.string().optional(),
error: z.string().optional(),
}),
},
async ({ itemIds, isRead }) => {
try {
@@ -239,6 +257,10 @@ export function registerEmailTools(server: McpServer): void {
type: "text" as const,
text: JSON.stringify({ error: errors.join("; ") }),
}],
structuredContent: {
success: false,
error: errors.join("; "),
},
};
}
@@ -251,6 +273,10 @@ export function registerEmailTools(server: McpServer): void {
message: `Marked ${itemIds.length} email(s) as ${status}.`,
}),
}],
structuredContent: {
success: true,
message: `Marked ${itemIds.length} email(s) as ${status}.`,
},
};
} catch (error: any) {
return {
@@ -258,6 +284,10 @@ export function registerEmailTools(server: McpServer): void {
type: "text" as const,
text: JSON.stringify({ error: error.message ?? String(error) }),
}],
structuredContent: {
success: false,
error: error.message ?? String(error),
},
};
}
},
@@ -266,14 +296,29 @@ export function registerEmailTools(server: McpServer): void {
server.registerTool(
"download_attachments",
{
description:
"Download all file attachments from an email to disk. Inline (embedded) attachments are skipped — only standalone file attachments are downloaded. Returns {success, downloaded, count} or {success, downloaded, count, errors}.",
description: "Download all file attachments from an email to disk."
+ " Inline (embedded) attachments are skipped — only standalone file attachments are downloaded.",
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",
"Local directory to save files",
),
}),
outputSchema: z.object({
success: z.boolean(),
downloaded: z.array(z.object({
name: z.string(),
path: z.string(),
size: z.number(),
contentType: z.string(),
})),
count: z.number(),
errors: z.array(z.object({
name: z.string(),
error: z.string(),
})).optional(),
message: z.string().optional(),
}),
},
async ({ itemId, targetFolder }) => {
try {
@@ -297,6 +342,12 @@ export function registerEmailTools(server: McpServer): void {
message: "No downloadable file attachments.",
}),
}],
structuredContent: {
success: true,
downloaded: [],
count: 0,
message: "No downloadable file attachments.",
},
};
}
@@ -354,6 +405,9 @@ export function registerEmailTools(server: McpServer): void {
...(errors.length ? { errors } : {}),
}),
}],
structuredContent: errors.length === 0
? { success: true, downloaded, count: downloaded.length }
: { success: false, downloaded, count: downloaded.length, errors },
};
} catch (error: any) {
return {