@mindstone/mcp-server-microsoft-mail 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +76 -0
- package/README.md +38 -8
- package/dist/compose.d.ts +2 -2
- package/dist/compose.js +6 -8
- package/dist/mail.d.ts +108 -0
- package/dist/mail.js +566 -2
- package/dist/resources/compose-email-template.d.ts +1 -1
- package/dist/resources/compose-email-template.js +281 -26
- package/dist/tools.js +363 -6
- package/dist/untrusted-content.d.ts +1 -1
- package/dist/untrusted-content.js +2 -2
- package/dist/utils.d.ts +1 -1
- package/dist/utils.js +111 -5
- package/package.json +1 -1
- package/server.json +9 -2
package/dist/tools.js
CHANGED
|
@@ -1,11 +1,54 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
|
-
import {
|
|
2
|
+
import { hasScope } from '@mindstone/mcp-server-microsoft-shared';
|
|
3
|
+
import { callGraph, getTokenProvider } from './client.js';
|
|
3
4
|
import { handleComposeEmail } from './compose.js';
|
|
4
5
|
import { authRequiredJson, errorResponse, successJson, withErrorHandling, } from './utils.js';
|
|
5
6
|
import { AUTH_TOOL_NAME } from './types.js';
|
|
6
|
-
import { createDraft, createReplyDraft, deleteEmail, forwardEmail, getEmail, listEmails, listFolders, moveEmail, replyToEmail, searchEmails, sendEmail, } from './mail.js';
|
|
7
|
-
|
|
7
|
+
import { createDraft, createReplyDraft, deleteEmail, downloadAttachment, forwardEmail, getAutomaticReplies, getConversation, getEmail, listAttachments, listEmails, listFolders, markEmailRead, moveEmail, normalizeScheduledWindow, replyToEmail, searchEmails, sendDraft, sendEmail, setAutomaticReplies, setEmailFlag, updateDraft, } from './mail.js';
|
|
8
|
+
// Bounded, validated recipient addresses: trimmed, RFC-shaped, and capped in
|
|
9
|
+
// length and count so arbitrary strings cannot flow into a Graph send payload.
|
|
10
|
+
const MAX_RECIPIENT_ADDRESS_LENGTH = 254;
|
|
11
|
+
const MAX_RECIPIENT_COUNT = 500;
|
|
12
|
+
const EmailAddress = z.string().trim().max(MAX_RECIPIENT_ADDRESS_LENGTH).email();
|
|
13
|
+
const RecipientList = z.array(EmailAddress).max(MAX_RECIPIENT_COUNT);
|
|
14
|
+
const RecipientField = z.union([RecipientList, EmailAddress]);
|
|
8
15
|
const ImportanceEnum = z.enum(['low', 'normal', 'high']);
|
|
16
|
+
// Automatic-replies management lives under MailboxSettings, a separate Graph
|
|
17
|
+
// permission family from Mail.* that many tenants gate behind admin consent
|
|
18
|
+
// (mirrors the SharePoint Sites.Read.All gate in microsoft-sharepoint).
|
|
19
|
+
const MAILBOX_SETTINGS_READ_SCOPES = ['MailboxSettings.Read', 'MailboxSettings.ReadWrite'];
|
|
20
|
+
const MAILBOX_SETTINGS_WRITE_SCOPE = 'MailboxSettings.ReadWrite';
|
|
21
|
+
async function requireMailboxSettingsScope(write) {
|
|
22
|
+
const guidance = {
|
|
23
|
+
action_required: `Call ${AUTH_TOOL_NAME} and grant the MailboxSettings permissions. Note: in many organizations an administrator must approve additional Microsoft Graph permissions.`,
|
|
24
|
+
next_step: AUTH_TOOL_NAME,
|
|
25
|
+
};
|
|
26
|
+
try {
|
|
27
|
+
const tokenData = await getTokenProvider().loadToken();
|
|
28
|
+
if (!tokenData) {
|
|
29
|
+
return errorResponse({
|
|
30
|
+
error: 'No Microsoft account connected.',
|
|
31
|
+
...guidance,
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
const granted = write
|
|
35
|
+
? hasScope(tokenData.scope, MAILBOX_SETTINGS_WRITE_SCOPE)
|
|
36
|
+
: MAILBOX_SETTINGS_READ_SCOPES.some((scope) => hasScope(tokenData.scope, scope));
|
|
37
|
+
if (!granted) {
|
|
38
|
+
return errorResponse({
|
|
39
|
+
error: `Automatic-replies management requires the ${write ? MAILBOX_SETTINGS_WRITE_SCOPE : 'MailboxSettings.Read'} permission, which this Microsoft connection has not granted.`,
|
|
40
|
+
...guidance,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
catch (err) {
|
|
46
|
+
return errorResponse({
|
|
47
|
+
error: `Failed to check mailbox-settings permissions: ${err instanceof Error ? err.message : String(err)}`,
|
|
48
|
+
...guidance,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
9
52
|
// send_email accepts unknown keys so the handler can surface bundled-parity
|
|
10
53
|
// alias guidance (`recipient`/`recipients`, `message`/`content`/`text`)
|
|
11
54
|
// rather than letting Zod silently strip them.
|
|
@@ -15,6 +58,7 @@ const SendEmailSchema = z
|
|
|
15
58
|
subject: z.string().optional().describe('Email subject'),
|
|
16
59
|
body: z.string().optional().describe('Email body (HTML supported)'),
|
|
17
60
|
cc: RecipientField.optional().describe('CC recipient(s)'),
|
|
61
|
+
bcc: RecipientField.optional().describe('BCC recipient(s)'),
|
|
18
62
|
importance: ImportanceEnum.optional().describe('Email importance'),
|
|
19
63
|
})
|
|
20
64
|
.passthrough();
|
|
@@ -89,10 +133,94 @@ sign-in, Microsoft 365 tools become available.`,
|
|
|
89
133
|
return successJson(result);
|
|
90
134
|
}));
|
|
91
135
|
// ---------------------------------------------------------------------
|
|
136
|
+
// get_conversation
|
|
137
|
+
// ---------------------------------------------------------------------
|
|
138
|
+
server.registerTool('get_conversation', {
|
|
139
|
+
description: 'List all messages in an email thread (conversation), oldest first. Provide a message ID from list_emails/search_emails or a conversationId.',
|
|
140
|
+
inputSchema: z.object({
|
|
141
|
+
id: z
|
|
142
|
+
.string()
|
|
143
|
+
.optional()
|
|
144
|
+
.describe('Any message ID in the thread; its conversationId is resolved automatically'),
|
|
145
|
+
conversationId: z
|
|
146
|
+
.string()
|
|
147
|
+
.optional()
|
|
148
|
+
.describe('Graph conversation ID, if already known'),
|
|
149
|
+
top: z.number().optional().describe('Number of messages to return (default: 25, max: 100)'),
|
|
150
|
+
}).shape,
|
|
151
|
+
annotations: {
|
|
152
|
+
readOnlyHint: true,
|
|
153
|
+
destructiveHint: false,
|
|
154
|
+
openWorldHint: true,
|
|
155
|
+
},
|
|
156
|
+
}, withErrorHandling(async (args, extra) => {
|
|
157
|
+
if (!args.id && !args.conversationId) {
|
|
158
|
+
return errorResponse({
|
|
159
|
+
error: 'Missing required parameter: "id" (any message ID in the thread) or "conversationId". Example: { "id": "AAMkAGI2..." }. Use list_emails or search_emails to find message IDs.',
|
|
160
|
+
action_required: 'Provide a message ID or a conversationId.',
|
|
161
|
+
next_step: 'list_emails',
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
const result = await callGraph(extra, (c, signal) => getConversation(c, { id: args.id, conversationId: args.conversationId, top: args.top }, signal));
|
|
165
|
+
return successJson(result);
|
|
166
|
+
}));
|
|
167
|
+
// ---------------------------------------------------------------------
|
|
168
|
+
// list_attachments
|
|
169
|
+
// ---------------------------------------------------------------------
|
|
170
|
+
server.registerTool('list_attachments', {
|
|
171
|
+
description: 'List attachments on an email message. Returns attachment IDs, names, types, and sizes. Use download_attachment to save one locally.',
|
|
172
|
+
inputSchema: z.object({
|
|
173
|
+
id: z.string().optional().describe('Email message ID'),
|
|
174
|
+
}).shape,
|
|
175
|
+
annotations: {
|
|
176
|
+
readOnlyHint: true,
|
|
177
|
+
destructiveHint: false,
|
|
178
|
+
openWorldHint: true,
|
|
179
|
+
},
|
|
180
|
+
}, withErrorHandling(async (args, extra) => {
|
|
181
|
+
if (!args.id) {
|
|
182
|
+
return errorResponse({
|
|
183
|
+
error: 'Missing required parameter: "id" (the email message ID). Example: { "id": "AAMkAGI2..." }. Use list_emails or search_emails to find message IDs; messages with hasAttachments=true have attachments.',
|
|
184
|
+
action_required: 'Provide the message ID of an email that has attachments.',
|
|
185
|
+
next_step: 'list_emails',
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
const result = await callGraph(extra, (c, signal) => listAttachments(c, { id: args.id }, signal));
|
|
189
|
+
return successJson(result);
|
|
190
|
+
}));
|
|
191
|
+
// ---------------------------------------------------------------------
|
|
192
|
+
// download_attachment
|
|
193
|
+
// ---------------------------------------------------------------------
|
|
194
|
+
server.registerTool('download_attachment', {
|
|
195
|
+
description: 'Download an email attachment and save it into the workspace (MCP_WORKSPACE_PATH, or the OS temp directory when unset). Use list_attachments to find attachment IDs. The file is written inside a fresh private staging directory created under the workspace root, and the reported savedTo path points there.',
|
|
196
|
+
inputSchema: z.object({
|
|
197
|
+
id: z.string().optional().describe('Email message ID'),
|
|
198
|
+
attachmentId: z
|
|
199
|
+
.string()
|
|
200
|
+
.optional()
|
|
201
|
+
.describe('Attachment ID from list_attachments'),
|
|
202
|
+
}).shape,
|
|
203
|
+
annotations: {
|
|
204
|
+
readOnlyHint: false,
|
|
205
|
+
destructiveHint: false,
|
|
206
|
+
openWorldHint: true,
|
|
207
|
+
},
|
|
208
|
+
}, withErrorHandling(async (args, extra) => {
|
|
209
|
+
if (!args.id || !args.attachmentId) {
|
|
210
|
+
return errorResponse({
|
|
211
|
+
error: 'Missing required parameters: "id" (email message ID) and "attachmentId" (attachment to save). Example: { "id": "AAMkAGI2...", "attachmentId": "AAMkADA1..." }. Use list_attachments to find attachment IDs.',
|
|
212
|
+
action_required: 'Provide both the message ID and the attachment ID.',
|
|
213
|
+
next_step: 'list_attachments',
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
const result = await callGraph(extra, (c, signal) => downloadAttachment(c, { id: args.id, attachmentId: args.attachmentId }, signal));
|
|
217
|
+
return successJson(result);
|
|
218
|
+
}));
|
|
219
|
+
// ---------------------------------------------------------------------
|
|
92
220
|
// send_email
|
|
93
221
|
// ---------------------------------------------------------------------
|
|
94
222
|
server.registerTool('send_email', {
|
|
95
|
-
description: 'Send a new email message. "to" and "
|
|
223
|
+
description: 'Send a new email message. "to", "cc", and "bcc" accept a string or an array of strings. Prefer arrays for multiple recipients (e.g. ["alice@example.com"]).',
|
|
96
224
|
inputSchema: SendEmailSchema,
|
|
97
225
|
annotations: {
|
|
98
226
|
readOnlyHint: false,
|
|
@@ -127,6 +255,7 @@ sign-in, Microsoft 365 tools become available.`,
|
|
|
127
255
|
subject: args.subject,
|
|
128
256
|
body: args.body,
|
|
129
257
|
cc: args.cc,
|
|
258
|
+
bcc: args.bcc,
|
|
130
259
|
importance: args.importance,
|
|
131
260
|
}, signal));
|
|
132
261
|
return successJson(result);
|
|
@@ -140,8 +269,9 @@ sign-in, Microsoft 365 tools become available.`,
|
|
|
140
269
|
server.registerTool('compose_email', {
|
|
141
270
|
description: 'Open an inline editable email compose form before sending. Use this when the user wants to write or send an email, so they can review and edit the draft first. Do NOT use when the user asks to save a draft (use create_draft). This tool does not send the email directly.',
|
|
142
271
|
inputSchema: z.object({
|
|
143
|
-
to:
|
|
144
|
-
cc:
|
|
272
|
+
to: RecipientList.min(1).describe('Recipient email address(es), e.g. ["alice@example.com"]'),
|
|
273
|
+
cc: RecipientList.optional().describe('CC recipient(s)'),
|
|
274
|
+
bcc: RecipientList.optional().describe('BCC recipient(s)'),
|
|
145
275
|
subject: z.string().describe('Email subject'),
|
|
146
276
|
body: z.string().describe('Email body'),
|
|
147
277
|
}).shape,
|
|
@@ -354,6 +484,7 @@ sign-in, Microsoft 365 tools become available.`,
|
|
|
354
484
|
subject: z.string().optional().describe('Email subject'),
|
|
355
485
|
body: z.string().optional().describe('Email body (HTML supported)'),
|
|
356
486
|
cc: RecipientField.optional().describe('CC recipient(s)'),
|
|
487
|
+
bcc: RecipientField.optional().describe('BCC recipient(s)'),
|
|
357
488
|
}).shape,
|
|
358
489
|
annotations: {
|
|
359
490
|
readOnlyHint: false,
|
|
@@ -373,6 +504,232 @@ sign-in, Microsoft 365 tools become available.`,
|
|
|
373
504
|
subject: args.subject,
|
|
374
505
|
body: args.body,
|
|
375
506
|
cc: args.cc,
|
|
507
|
+
bcc: args.bcc,
|
|
508
|
+
}, signal));
|
|
509
|
+
return successJson(result);
|
|
510
|
+
}));
|
|
511
|
+
// ---------------------------------------------------------------------
|
|
512
|
+
// mark_email_read
|
|
513
|
+
// ---------------------------------------------------------------------
|
|
514
|
+
server.registerTool('mark_email_read', {
|
|
515
|
+
description: 'Mark an email as read or unread.',
|
|
516
|
+
inputSchema: z.object({
|
|
517
|
+
id: z.string().optional().describe('Email message ID'),
|
|
518
|
+
isRead: z
|
|
519
|
+
.boolean()
|
|
520
|
+
.optional()
|
|
521
|
+
.describe('true to mark as read, false to mark as unread (default: true)'),
|
|
522
|
+
}).shape,
|
|
523
|
+
annotations: {
|
|
524
|
+
readOnlyHint: false,
|
|
525
|
+
destructiveHint: false,
|
|
526
|
+
idempotentHint: true,
|
|
527
|
+
openWorldHint: true,
|
|
528
|
+
},
|
|
529
|
+
}, withErrorHandling(async (args, extra) => {
|
|
530
|
+
if (!args.id) {
|
|
531
|
+
return errorResponse({
|
|
532
|
+
error: 'Missing required parameter: "id" (the email message ID). Example: { "id": "AAMkAGI2...", "isRead": true }',
|
|
533
|
+
action_required: 'Provide the message ID.',
|
|
534
|
+
next_step: 'list_emails',
|
|
535
|
+
});
|
|
536
|
+
}
|
|
537
|
+
const result = await callGraph(extra, (c, signal) => markEmailRead(c, { id: args.id, isRead: args.isRead }, signal));
|
|
538
|
+
return successJson(result);
|
|
539
|
+
}));
|
|
540
|
+
// ---------------------------------------------------------------------
|
|
541
|
+
// set_email_flag
|
|
542
|
+
// ---------------------------------------------------------------------
|
|
543
|
+
server.registerTool('set_email_flag', {
|
|
544
|
+
description: 'Flag an email for follow-up, mark a follow-up complete, or clear the flag.',
|
|
545
|
+
inputSchema: z.object({
|
|
546
|
+
id: z.string().optional().describe('Email message ID'),
|
|
547
|
+
flag: z
|
|
548
|
+
.enum(['flagged', 'complete', 'notFlagged'])
|
|
549
|
+
.optional()
|
|
550
|
+
.describe('Follow-up state: "flagged" (flag for follow-up), "complete" (mark done), "notFlagged" (clear the flag)'),
|
|
551
|
+
}).shape,
|
|
552
|
+
annotations: {
|
|
553
|
+
readOnlyHint: false,
|
|
554
|
+
destructiveHint: false,
|
|
555
|
+
idempotentHint: true,
|
|
556
|
+
openWorldHint: true,
|
|
557
|
+
},
|
|
558
|
+
}, withErrorHandling(async (args, extra) => {
|
|
559
|
+
if (!args.id || !args.flag) {
|
|
560
|
+
return errorResponse({
|
|
561
|
+
error: 'Missing required parameters: "id" (email message ID) and "flag" ("flagged", "complete", or "notFlagged"). Example: { "id": "AAMkAGI2...", "flag": "flagged" }',
|
|
562
|
+
action_required: 'Provide both id and flag.',
|
|
563
|
+
next_step: 'set_email_flag',
|
|
564
|
+
});
|
|
565
|
+
}
|
|
566
|
+
const result = await callGraph(extra, (c, signal) => setEmailFlag(c, { id: args.id, flag: args.flag }, signal));
|
|
567
|
+
return successJson(result);
|
|
568
|
+
}));
|
|
569
|
+
// ---------------------------------------------------------------------
|
|
570
|
+
// send_draft
|
|
571
|
+
// ---------------------------------------------------------------------
|
|
572
|
+
server.registerTool('send_draft', {
|
|
573
|
+
description: 'Send an existing draft email (created by create_draft or create_reply_draft). The draft is sent and saved to Sent Items.',
|
|
574
|
+
inputSchema: z.object({
|
|
575
|
+
id: z.string().optional().describe('Draft message ID (draftId from create_draft)'),
|
|
576
|
+
}).shape,
|
|
577
|
+
annotations: {
|
|
578
|
+
readOnlyHint: false,
|
|
579
|
+
destructiveHint: false,
|
|
580
|
+
openWorldHint: true,
|
|
581
|
+
},
|
|
582
|
+
}, withErrorHandling(async (args, extra) => {
|
|
583
|
+
if (!args.id) {
|
|
584
|
+
return errorResponse({
|
|
585
|
+
error: 'Missing required parameter: "id" (the draft message ID). Example: { "id": "AAMkAGI2..." }. Use the draftId returned by create_draft or create_reply_draft, or list the drafts folder with list_emails.',
|
|
586
|
+
action_required: 'Provide the draft message ID.',
|
|
587
|
+
next_step: 'create_draft',
|
|
588
|
+
});
|
|
589
|
+
}
|
|
590
|
+
const result = await callGraph(extra, (c, signal) => sendDraft(c, { id: args.id }, signal));
|
|
591
|
+
return successJson(result);
|
|
592
|
+
}));
|
|
593
|
+
// ---------------------------------------------------------------------
|
|
594
|
+
// update_draft
|
|
595
|
+
// ---------------------------------------------------------------------
|
|
596
|
+
server.registerTool('update_draft', {
|
|
597
|
+
description: 'Update an existing draft email. Only the provided fields are changed (subject, body, to, cc, importance). Use send_draft to send it afterwards.',
|
|
598
|
+
inputSchema: z.object({
|
|
599
|
+
id: z.string().optional().describe('Draft message ID (draftId from create_draft)'),
|
|
600
|
+
to: RecipientField.optional().describe('Replace the recipient list'),
|
|
601
|
+
cc: RecipientField.optional().describe('Replace the CC recipient list'),
|
|
602
|
+
subject: z.string().optional().describe('New email subject'),
|
|
603
|
+
body: z.string().optional().describe('New email body (HTML supported)'),
|
|
604
|
+
importance: ImportanceEnum.optional().describe('Email importance'),
|
|
605
|
+
}).shape,
|
|
606
|
+
annotations: {
|
|
607
|
+
readOnlyHint: false,
|
|
608
|
+
destructiveHint: false,
|
|
609
|
+
idempotentHint: true,
|
|
610
|
+
openWorldHint: true,
|
|
611
|
+
},
|
|
612
|
+
}, withErrorHandling(async (args, extra) => {
|
|
613
|
+
if (!args.id) {
|
|
614
|
+
return errorResponse({
|
|
615
|
+
error: 'Missing required parameter: "id" (the draft message ID). Example: { "id": "AAMkAGI2...", "subject": "Updated subject" }. Use the draftId returned by create_draft or create_reply_draft.',
|
|
616
|
+
action_required: 'Provide the draft message ID.',
|
|
617
|
+
next_step: 'create_draft',
|
|
618
|
+
});
|
|
619
|
+
}
|
|
620
|
+
if (args.to === undefined &&
|
|
621
|
+
args.cc === undefined &&
|
|
622
|
+
args.subject === undefined &&
|
|
623
|
+
args.body === undefined &&
|
|
624
|
+
args.importance === undefined) {
|
|
625
|
+
return errorResponse({
|
|
626
|
+
error: 'Nothing to update. Provide at least one of "to", "cc", "subject", "body", or "importance". Example: { "id": "AAMkAGI2...", "body": "Updated content" }',
|
|
627
|
+
action_required: 'Provide at least one field to update.',
|
|
628
|
+
next_step: 'update_draft',
|
|
629
|
+
});
|
|
630
|
+
}
|
|
631
|
+
const result = await callGraph(extra, (c, signal) => updateDraft(c, {
|
|
632
|
+
id: args.id,
|
|
633
|
+
to: args.to,
|
|
634
|
+
cc: args.cc,
|
|
635
|
+
subject: args.subject,
|
|
636
|
+
body: args.body,
|
|
637
|
+
importance: args.importance,
|
|
638
|
+
}, signal));
|
|
639
|
+
return successJson(result);
|
|
640
|
+
}));
|
|
641
|
+
// ---------------------------------------------------------------------
|
|
642
|
+
// get_automatic_replies
|
|
643
|
+
// ---------------------------------------------------------------------
|
|
644
|
+
server.registerTool('get_automatic_replies', {
|
|
645
|
+
description: 'Read the current out-of-office (automatic replies) configuration: status, internal/external messages, and schedule. Requires the MailboxSettings.Read permission.',
|
|
646
|
+
inputSchema: z.object({}).strict().shape,
|
|
647
|
+
annotations: {
|
|
648
|
+
readOnlyHint: true,
|
|
649
|
+
destructiveHint: false,
|
|
650
|
+
openWorldHint: true,
|
|
651
|
+
},
|
|
652
|
+
}, withErrorHandling(async (_args, extra) => {
|
|
653
|
+
const scopeError = await requireMailboxSettingsScope(false);
|
|
654
|
+
if (scopeError)
|
|
655
|
+
return scopeError;
|
|
656
|
+
const result = await callGraph(extra, (c, signal) => getAutomaticReplies(c, signal));
|
|
657
|
+
return successJson(result);
|
|
658
|
+
}));
|
|
659
|
+
// ---------------------------------------------------------------------
|
|
660
|
+
// set_automatic_replies
|
|
661
|
+
// ---------------------------------------------------------------------
|
|
662
|
+
server.registerTool('set_automatic_replies', {
|
|
663
|
+
description: 'Turn out-of-office (automatic replies) on, off, or schedule it. Status "scheduled" requires scheduledStart and scheduledEnd. Requires the MailboxSettings.ReadWrite permission.',
|
|
664
|
+
inputSchema: z.object({
|
|
665
|
+
status: z
|
|
666
|
+
.enum(['disabled', 'alwaysEnabled', 'scheduled'])
|
|
667
|
+
.optional()
|
|
668
|
+
.describe('"alwaysEnabled" turns automatic replies on until changed, "scheduled" turns them on between scheduledStart and scheduledEnd, "disabled" turns them off'),
|
|
669
|
+
internalReplyMessage: z
|
|
670
|
+
.string()
|
|
671
|
+
.optional()
|
|
672
|
+
.describe('Reply sent to people inside the organization (HTML supported)'),
|
|
673
|
+
externalReplyMessage: z
|
|
674
|
+
.string()
|
|
675
|
+
.optional()
|
|
676
|
+
.describe('Reply sent to people outside the organization (HTML supported)'),
|
|
677
|
+
externalAudience: z
|
|
678
|
+
.enum(['none', 'contactsOnly', 'all'])
|
|
679
|
+
.optional()
|
|
680
|
+
.describe('Who outside the organization receives the external reply (default: none)'),
|
|
681
|
+
scheduledStart: z
|
|
682
|
+
.string()
|
|
683
|
+
.optional()
|
|
684
|
+
.describe('Start of the scheduled window as an ISO 8601 date-time (e.g. "2026-08-10T09:00:00Z"); explicit offsets are converted to UTC, zone-less values are treated as UTC'),
|
|
685
|
+
scheduledEnd: z
|
|
686
|
+
.string()
|
|
687
|
+
.optional()
|
|
688
|
+
.describe('End of the scheduled window as an ISO 8601 date-time (e.g. "2026-08-14T18:00:00Z"); explicit offsets are converted to UTC, zone-less values are treated as UTC'),
|
|
689
|
+
}).shape,
|
|
690
|
+
annotations: {
|
|
691
|
+
readOnlyHint: false,
|
|
692
|
+
destructiveHint: false,
|
|
693
|
+
idempotentHint: true,
|
|
694
|
+
openWorldHint: true,
|
|
695
|
+
},
|
|
696
|
+
}, withErrorHandling(async (args, extra) => {
|
|
697
|
+
if (!args.status) {
|
|
698
|
+
return errorResponse({
|
|
699
|
+
error: 'Missing required parameter: "status" ("disabled", "alwaysEnabled", or "scheduled"). Example: { "status": "alwaysEnabled", "internalReplyMessage": "I am away until Friday." }',
|
|
700
|
+
action_required: 'Provide the automatic-replies status.',
|
|
701
|
+
next_step: 'set_automatic_replies',
|
|
702
|
+
});
|
|
703
|
+
}
|
|
704
|
+
if (args.status === 'scheduled' && (!args.scheduledStart || !args.scheduledEnd)) {
|
|
705
|
+
return errorResponse({
|
|
706
|
+
error: 'Status "scheduled" requires "scheduledStart" and "scheduledEnd" (ISO 8601 date-times, e.g. "2026-08-10T09:00:00Z").',
|
|
707
|
+
action_required: 'Provide scheduledStart and scheduledEnd.',
|
|
708
|
+
next_step: 'set_automatic_replies',
|
|
709
|
+
});
|
|
710
|
+
}
|
|
711
|
+
if (args.status === 'scheduled') {
|
|
712
|
+
try {
|
|
713
|
+
normalizeScheduledWindow(args.scheduledStart, args.scheduledEnd);
|
|
714
|
+
}
|
|
715
|
+
catch (err) {
|
|
716
|
+
return errorResponse({
|
|
717
|
+
error: err instanceof Error ? err.message : String(err),
|
|
718
|
+
action_required: 'Provide ISO 8601 date-times with an explicit offset or "Z" (or a UTC wall-clock time), with scheduledStart earlier than scheduledEnd.',
|
|
719
|
+
next_step: 'set_automatic_replies',
|
|
720
|
+
});
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
const scopeError = await requireMailboxSettingsScope(true);
|
|
724
|
+
if (scopeError)
|
|
725
|
+
return scopeError;
|
|
726
|
+
const result = await callGraph(extra, (c, signal) => setAutomaticReplies(c, {
|
|
727
|
+
status: args.status,
|
|
728
|
+
internalReplyMessage: args.internalReplyMessage,
|
|
729
|
+
externalReplyMessage: args.externalReplyMessage,
|
|
730
|
+
externalAudience: args.externalAudience,
|
|
731
|
+
scheduledStart: args.scheduledStart,
|
|
732
|
+
scheduledEnd: args.scheduledEnd,
|
|
376
733
|
}, signal));
|
|
377
734
|
return successJson(result);
|
|
378
735
|
}));
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
* broken out of. `undefined` passes through untouched. Idempotent for the same
|
|
25
25
|
* `source`.
|
|
26
26
|
*/
|
|
27
|
-
export declare function wrapUntrusted(text: string | undefined, source: string): string | undefined;
|
|
27
|
+
export declare function wrapUntrusted(text: string | null | undefined, source: string): string | undefined;
|
|
28
28
|
/**
|
|
29
29
|
* Recursively wrap every string value reachable inside `value`. Object keys are
|
|
30
30
|
* structural and NOT wrapped; non-string leaves pass through unchanged.
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
* `untrusted-content` in any connector that talks to an external system; this
|
|
19
19
|
* file (and the call sites that import from it) is what satisfies that gate.
|
|
20
20
|
*/
|
|
21
|
-
const UNTRUSTED_CLOSE_TAG_VARIANT = /<\/untrusted-content
|
|
21
|
+
const UNTRUSTED_CLOSE_TAG_VARIANT = /<\/untrusted-content\s*>/gi;
|
|
22
22
|
const ESCAPED_UNTRUSTED_CLOSE_TAG = '<\\/untrusted-content>';
|
|
23
23
|
function escapeAttr(s) {
|
|
24
24
|
return s.replaceAll('&', '&').replaceAll('"', '"').replaceAll('<', '<').replaceAll('>', '>');
|
|
@@ -33,7 +33,7 @@ function escapeCloseTagSentinels(s) {
|
|
|
33
33
|
* `source`.
|
|
34
34
|
*/
|
|
35
35
|
export function wrapUntrusted(text, source) {
|
|
36
|
-
if (text === undefined)
|
|
36
|
+
if (text === undefined || text === null)
|
|
37
37
|
return undefined;
|
|
38
38
|
const open = `<untrusted-content source="${escapeAttr(source)}">`;
|
|
39
39
|
const close = '</untrusted-content>';
|
package/dist/utils.d.ts
CHANGED
package/dist/utils.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ZodError } from 'zod';
|
|
2
|
+
import { MicrosoftRefreshDisabledError, createLogger, detectAuthRequiredReason, formatGraphError, } from '@mindstone/mcp-server-microsoft-shared';
|
|
2
3
|
import { AUTH_TOOL_NAME, REQUEST_TIMEOUT_MS, getMsPackageId } from './types.js';
|
|
4
|
+
import { wrapUntrusted } from './untrusted-content.js';
|
|
5
|
+
const log = createLogger('microsoft-mail-mcp');
|
|
3
6
|
/**
|
|
4
7
|
* Compose a per-call abort signal with the cohort timeout.
|
|
5
8
|
*
|
|
@@ -152,6 +155,76 @@ function detectTokenProviderAuthReason(err) {
|
|
|
152
155
|
return 'token_expired';
|
|
153
156
|
return null;
|
|
154
157
|
}
|
|
158
|
+
/**
|
|
159
|
+
* Graph rejects a $filter it cannot service efficiently (including $filter
|
|
160
|
+
* combined with $orderby) with HTTP 400 InefficientFilter. Retry and re-auth
|
|
161
|
+
* cannot help — only a simpler query can — so this failure class gets honest
|
|
162
|
+
* guidance instead of the generic retry/re-auth copy below.
|
|
163
|
+
*/
|
|
164
|
+
function isInefficientFilterError(err) {
|
|
165
|
+
if (!(err instanceof Error))
|
|
166
|
+
return false;
|
|
167
|
+
const graphErr = err;
|
|
168
|
+
const code = (graphErr.code ?? '').toLowerCase();
|
|
169
|
+
const msg = err.message.toLowerCase();
|
|
170
|
+
return code.includes('inefficientfilter') || msg.includes('sort order is too complex');
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Extract the upstream Graph error-body message, mirroring the shared
|
|
174
|
+
* formatGraphError() body parsing. Kept local because the 403 text below must
|
|
175
|
+
* drop the shared package's reconnect advice without forking the formatter.
|
|
176
|
+
*/
|
|
177
|
+
function extractUpstreamErrorMessage(err) {
|
|
178
|
+
const graphErr = err;
|
|
179
|
+
if (graphErr.body) {
|
|
180
|
+
try {
|
|
181
|
+
const inner = JSON.parse(graphErr.body)?.error
|
|
182
|
+
?.message;
|
|
183
|
+
if (typeof inner === 'string' && inner.length > 0)
|
|
184
|
+
return inner;
|
|
185
|
+
}
|
|
186
|
+
catch {
|
|
187
|
+
// body is not JSON — fall back to the top-level message
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return err.message;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Connector-local error text for model-visible non-auth failures.
|
|
194
|
+
*
|
|
195
|
+
* The shared formatGraphError() is not usable as-is on this path:
|
|
196
|
+
* - it interpolates the upstream Graph error-body message raw, and that text
|
|
197
|
+
* is attacker-influenceable (invariant #6 requires an untrusted-content
|
|
198
|
+
* envelope around it), and
|
|
199
|
+
* - for every HTTP 403 it advises disconnecting and reconnecting the
|
|
200
|
+
* account. A permissions denial is not repaired by re-authenticating the
|
|
201
|
+
* same account, and the auth-required branch above already covers the
|
|
202
|
+
* failures where re-authentication genuinely helps (token expiry, consent,
|
|
203
|
+
* tenant blocks).
|
|
204
|
+
*
|
|
205
|
+
* Connector-authored messages (plain Errors without a Graph statusCode/body,
|
|
206
|
+
* e.g. the download_attachment validation errors, which already envelope
|
|
207
|
+
* their attacker-controlled fields) pass through untouched so their
|
|
208
|
+
* envelopes stay singular.
|
|
209
|
+
*/
|
|
210
|
+
function formatGenericGraphError(err) {
|
|
211
|
+
if (err instanceof Error) {
|
|
212
|
+
const graphErr = err;
|
|
213
|
+
if (graphErr.statusCode === 403) {
|
|
214
|
+
const detail = wrapUntrusted(extractUpstreamErrorMessage(err), 'microsoft-mail:graph-error');
|
|
215
|
+
return (`${detail} (HTTP 403${graphErr.code ? `: ${graphErr.code}` : ''}). ` +
|
|
216
|
+
'The connected account does not have permission for this operation, or a tenant policy blocks it — ' +
|
|
217
|
+
're-authenticating the same account will not change that. ' +
|
|
218
|
+
'An administrator may need to grant the permission or adjust the policy.');
|
|
219
|
+
}
|
|
220
|
+
if (graphErr.statusCode !== undefined || graphErr.body !== undefined) {
|
|
221
|
+
// Graph SDK error: the formatted text embeds the upstream error-body
|
|
222
|
+
// message, so the whole string is enveloped before it reaches the model.
|
|
223
|
+
return wrapUntrusted(formatGraphError(err), 'microsoft-mail:graph-error') ?? 'Unknown error';
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
return formatGraphError(err);
|
|
227
|
+
}
|
|
155
228
|
/**
|
|
156
229
|
* Map a Microsoft Graph error / shared TokenProvider error into the right
|
|
157
230
|
* tool response. Returns `auth_required` for token-related failures, the
|
|
@@ -173,7 +246,11 @@ export function buildErrorResponse(err) {
|
|
|
173
246
|
text: JSON.stringify({
|
|
174
247
|
...buildAuthRequiredResponse(),
|
|
175
248
|
reason,
|
|
176
|
-
error
|
|
249
|
+
// The shared formatter embeds the upstream Graph error-body
|
|
250
|
+
// message raw for consent/tenant-classified 403s — that text is
|
|
251
|
+
// attacker-influenceable, so it is enveloped before it reaches
|
|
252
|
+
// the model (invariant #6).
|
|
253
|
+
error: wrapUntrusted(formatGraphError(err), 'microsoft-mail:graph-error') ?? 'Unknown error',
|
|
177
254
|
package_id: getMsPackageId(),
|
|
178
255
|
}),
|
|
179
256
|
},
|
|
@@ -181,14 +258,43 @@ export function buildErrorResponse(err) {
|
|
|
181
258
|
isError: true,
|
|
182
259
|
};
|
|
183
260
|
}
|
|
261
|
+
// A ZodError here means a Graph response failed boundary validation. Its
|
|
262
|
+
// formatted issues can echo attacker-controlled values (e.g. an unexpected
|
|
263
|
+
// enum value from the mailbox), so never pass the raw message through —
|
|
264
|
+
// report the failure class only, with the offending field paths logged
|
|
265
|
+
// locally for debugging. This branch must run before the InefficientFilter
|
|
266
|
+
// check below: ZodError.message serializes its issues, and an invalid_enum
|
|
267
|
+
// issue embeds the upstream `received` value, so a poisoned enum could
|
|
268
|
+
// otherwise smuggle the filter phrase into the message match and skip this
|
|
269
|
+
// sanitizer entirely.
|
|
270
|
+
if (err instanceof ZodError) {
|
|
271
|
+
log.warn('Microsoft Graph response failed schema validation', {
|
|
272
|
+
paths: err.issues.map((issue) => issue.path.join('.')),
|
|
273
|
+
});
|
|
274
|
+
return errorResponse({
|
|
275
|
+
error: 'Microsoft Graph returned a response that failed schema validation, so it was not processed.',
|
|
276
|
+
action_required: 'Retry the call. If it keeps failing, the upstream response shape has changed and the connector needs an update.',
|
|
277
|
+
next_step: 'Retry the same tool call',
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
if (isInefficientFilterError(err)) {
|
|
281
|
+
return errorResponse({
|
|
282
|
+
error: formatGenericGraphError(err),
|
|
283
|
+
action_required: 'Simplify or remove the $filter argument and retry. Microsoft Graph cannot service this filter (for example when combined with a sort order), so retrying as-is or re-authenticating will not help.',
|
|
284
|
+
next_step: 'list_emails',
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
// Generic non-auth failure: re-authentication cannot fix filesystem,
|
|
288
|
+
// validation, permission, or unexpected Graph failures, so the guidance
|
|
289
|
+
// must not send the user (or the model) down that path at all — the
|
|
290
|
+
// auth-required branch above handles every case where it genuinely helps.
|
|
184
291
|
return {
|
|
185
292
|
content: [
|
|
186
293
|
{
|
|
187
294
|
type: 'text',
|
|
188
295
|
text: errorJson({
|
|
189
|
-
error:
|
|
190
|
-
action_required: '
|
|
191
|
-
next_step: AUTH_TOOL_NAME,
|
|
296
|
+
error: formatGenericGraphError(err),
|
|
297
|
+
action_required: 'Check the error details and retry the call. If it keeps failing, verify the request arguments and that the mailbox item still exists. Authentication and permission problems are reported separately with their own guidance.',
|
|
192
298
|
}),
|
|
193
299
|
},
|
|
194
300
|
],
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mindstone/mcp-server-microsoft-mail",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"mcpName": "io.github.mindstone/mcp-server-microsoft-mail",
|
|
5
5
|
"description": "Microsoft 365 Outlook mail via Graph: read, search, send, reply, forward, draft, move, delete.",
|
|
6
6
|
"license": "FSL-1.1-MIT",
|
package/server.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"name": "io.github.mindstone/mcp-server-microsoft-mail",
|
|
4
4
|
"title": "Microsoft 365 Mail",
|
|
5
5
|
"description": "Microsoft 365 Outlook mail via Graph: read, search, send, reply, forward, draft, move, delete.",
|
|
6
|
-
"version": "0.
|
|
6
|
+
"version": "0.4.0",
|
|
7
7
|
"repository": {
|
|
8
8
|
"url": "https://github.com/mindstone/mcp-servers",
|
|
9
9
|
"source": "github",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"registryType": "npm",
|
|
15
15
|
"registryBaseUrl": "https://registry.npmjs.org",
|
|
16
16
|
"identifier": "@mindstone/mcp-server-microsoft-mail",
|
|
17
|
-
"version": "0.
|
|
17
|
+
"version": "0.4.0",
|
|
18
18
|
"runtimeHint": "npx",
|
|
19
19
|
"transport": {
|
|
20
20
|
"type": "stdio"
|
|
@@ -56,6 +56,13 @@
|
|
|
56
56
|
"isSecret": false,
|
|
57
57
|
"default": "60000",
|
|
58
58
|
"format": "number"
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
"name": "MCP_WORKSPACE_PATH",
|
|
62
|
+
"description": "Workspace root where download_attachment saves files (falls back to the OS temp directory when unset)",
|
|
63
|
+
"isRequired": false,
|
|
64
|
+
"isSecret": false,
|
|
65
|
+
"format": "filepath"
|
|
59
66
|
}
|
|
60
67
|
]
|
|
61
68
|
}
|