hypermail-mcp 0.1.0 → 0.3.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/README.md +6 -2
- package/dist/cli.js +466 -57
- package/dist/cli.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -81,11 +81,15 @@ account store.
|
|
|
81
81
|
| `list_accounts` | — | Returns registered emails + provider, no secrets. |
|
|
82
82
|
| `add_account` | `provider`, `email?`, `config?` | Starts device-code (Outlook). Returns `{handle, verification:{userCode, verificationUri, expiresAt}}`. |
|
|
83
83
|
| `complete_add_account` | `provider`, `handle` | Returns `pending` / `ready` / `expired` / `error`. |
|
|
84
|
+
| `get_account_settings` | `account` | Get signature (HTML) and style preferences for an account. |
|
|
85
|
+
| `set_account_settings` | `account`, `signature?`, `style?` | Set signature HTML and font preferences. Disabled under `--read-only`. |
|
|
84
86
|
| `remove_account` | `email` | Deletes tokens for the account. |
|
|
85
87
|
| `list_emails` | `account`, `folder?`, `limit?`, `unreadOnly?` | Defaults: folder=`inbox`, limit=25. |
|
|
86
88
|
| `search_emails` | `account`, `query`, `limit?` | KQL on Outlook. |
|
|
87
|
-
| `read_email` | `account`, `id` | Returns full body + recipients + attachment metadata. |
|
|
88
|
-
| `
|
|
89
|
+
| `read_email` | `account`, `id`, `format?` | Returns full body + recipients + attachment metadata. `format`: `markdown` (default), `html`, or `text`. |
|
|
90
|
+
| `read_attachment` | `account`, `messageId`, `attachmentId` | Download an attachment to a temporary file and return its path. |
|
|
91
|
+
| `send_email` | `account`, `to[]`, `cc?`, `bcc?`, `subject`, `body`, `isHtml?`, `include_signature?`, `inReplyTo?`, `replyAll?`, `forwardMessageId?` | Send an email. Appends signature when `include_signature` is true. `inReplyTo` sends as threaded reply; `forwardMessageId` sends as forward. Disabled under `--read-only`. |
|
|
92
|
+
| `draft_email` | `account`, `to[]`, `cc?`, `bcc?`, `subject`, `body`, `isHtml?`, `include_signature?`, `inReplyTo?`, `replyAll?`, `forwardMessageId?` | Save as draft instead of sending. Returns the draft message ID. Disabled under `--read-only`. |
|
|
89
93
|
|
|
90
94
|
### Add-account flow (Outlook)
|
|
91
95
|
|
package/dist/cli.js
CHANGED
|
@@ -341,6 +341,24 @@ var OutlookClientFactory = class {
|
|
|
341
341
|
};
|
|
342
342
|
|
|
343
343
|
// src/providers/outlook/index.ts
|
|
344
|
+
function convertInlineImages(body) {
|
|
345
|
+
const attachments = [];
|
|
346
|
+
const re = /src="data:image\/([\w+]+);base64,([^"]+)"/gi;
|
|
347
|
+
const transformed = body.replace(re, (_fullMatch, mimeSubtype, b64) => {
|
|
348
|
+
const contentId = `sig-img-${randomUUID()}`;
|
|
349
|
+
const ext = mimeSubtype.toLowerCase().replace(/\+/g, "-") === "svg-xml" ? "svg" : mimeSubtype.toLowerCase().replace(/\+/g, "-");
|
|
350
|
+
attachments.push({
|
|
351
|
+
"@odata.type": "#microsoft.graph.fileAttachment",
|
|
352
|
+
name: `signature-image.${ext}`,
|
|
353
|
+
contentType: `image/${mimeSubtype}`,
|
|
354
|
+
contentId,
|
|
355
|
+
contentBytes: b64,
|
|
356
|
+
isInline: true
|
|
357
|
+
});
|
|
358
|
+
return `src="cid:${contentId}"`;
|
|
359
|
+
});
|
|
360
|
+
return { body: transformed, attachments };
|
|
361
|
+
}
|
|
344
362
|
var OutlookProvider = class {
|
|
345
363
|
constructor(opts) {
|
|
346
364
|
this.opts = opts;
|
|
@@ -506,14 +524,67 @@ var OutlookProvider = class {
|
|
|
506
524
|
path: outPath
|
|
507
525
|
};
|
|
508
526
|
}
|
|
527
|
+
// Shared helper — creates a draft from a reference message (forward or
|
|
528
|
+
// reply), prepends our composed body before the existing content, and
|
|
529
|
+
// attaches inline images. Returns the draft message ID.
|
|
530
|
+
async buildDraftFromReference(client, createEndpoint, createPayload, converted) {
|
|
531
|
+
const draft = await client.api(createEndpoint).post(createPayload);
|
|
532
|
+
const draftMsg = await client.api(`/me/messages/${draft.id}`).select("body").get();
|
|
533
|
+
const draftBody = draftMsg.body?.content ?? "";
|
|
534
|
+
const draftContentType = draftMsg.body?.contentType ?? "HTML";
|
|
535
|
+
const spacer = '<div style="line-height:12px"><br></div>';
|
|
536
|
+
const prepend = converted.body + spacer;
|
|
537
|
+
const finalBody = draftBody.includes("<body") ? draftBody.replace(/(<body[^>]*>)/i, `$1${prepend}`) : prepend + draftBody;
|
|
538
|
+
await client.api(`/me/messages/${draft.id}`).patch({
|
|
539
|
+
body: { contentType: draftContentType, content: finalBody }
|
|
540
|
+
});
|
|
541
|
+
for (const att of converted.attachments) {
|
|
542
|
+
await client.api(`/me/messages/${draft.id}/attachments`).post(att);
|
|
543
|
+
}
|
|
544
|
+
return draft.id;
|
|
545
|
+
}
|
|
509
546
|
async sendEmail(account, msg) {
|
|
510
547
|
const client = this.clients.get(account);
|
|
548
|
+
if (msg.inReplyTo && msg.forwardMessageId) {
|
|
549
|
+
throw new Error(
|
|
550
|
+
"inReplyTo and forwardMessageId are mutually exclusive \u2014 use one or the other"
|
|
551
|
+
);
|
|
552
|
+
}
|
|
553
|
+
const converted = convertInlineImages(msg.body);
|
|
554
|
+
if (msg.forwardMessageId) {
|
|
555
|
+
const draftId = await this.buildDraftFromReference(
|
|
556
|
+
client,
|
|
557
|
+
`/me/messages/${encodeURIComponent(msg.forwardMessageId)}/createForward`,
|
|
558
|
+
{
|
|
559
|
+
message: {
|
|
560
|
+
toRecipients: msg.to.map(toRecipient),
|
|
561
|
+
ccRecipients: (msg.cc ?? []).map(toRecipient),
|
|
562
|
+
bccRecipients: (msg.bcc ?? []).map(toRecipient)
|
|
563
|
+
},
|
|
564
|
+
comment: ""
|
|
565
|
+
},
|
|
566
|
+
converted
|
|
567
|
+
);
|
|
568
|
+
await client.api(`/me/messages/${draftId}/send`).post({});
|
|
569
|
+
return { id: draftId };
|
|
570
|
+
}
|
|
571
|
+
if (msg.inReplyTo) {
|
|
572
|
+
const createEndpoint = msg.replyAll ? `/me/messages/${encodeURIComponent(msg.inReplyTo)}/createReplyAll` : `/me/messages/${encodeURIComponent(msg.inReplyTo)}/createReply`;
|
|
573
|
+
const draftId = await this.buildDraftFromReference(
|
|
574
|
+
client,
|
|
575
|
+
createEndpoint,
|
|
576
|
+
{},
|
|
577
|
+
converted
|
|
578
|
+
);
|
|
579
|
+
await client.api(`/me/messages/${draftId}/send`).post({});
|
|
580
|
+
return { id: draftId };
|
|
581
|
+
}
|
|
511
582
|
const payload = {
|
|
512
583
|
message: {
|
|
513
584
|
subject: msg.subject,
|
|
514
585
|
body: {
|
|
515
586
|
contentType: msg.isHtml ? "HTML" : "Text",
|
|
516
|
-
content:
|
|
587
|
+
content: converted.body
|
|
517
588
|
},
|
|
518
589
|
toRecipients: msg.to.map(toRecipient),
|
|
519
590
|
ccRecipients: (msg.cc ?? []).map(toRecipient),
|
|
@@ -521,9 +592,66 @@ var OutlookProvider = class {
|
|
|
521
592
|
},
|
|
522
593
|
saveToSentItems: true
|
|
523
594
|
};
|
|
595
|
+
if (converted.attachments.length > 0) {
|
|
596
|
+
payload.message.attachments = converted.attachments;
|
|
597
|
+
}
|
|
524
598
|
await client.api("/me/sendMail").post(payload);
|
|
525
599
|
return { id: "" };
|
|
526
600
|
}
|
|
601
|
+
async saveDraft(account, msg) {
|
|
602
|
+
const client = this.clients.get(account);
|
|
603
|
+
if (msg.inReplyTo && msg.forwardMessageId) {
|
|
604
|
+
throw new Error(
|
|
605
|
+
"inReplyTo and forwardMessageId are mutually exclusive \u2014 use one or the other"
|
|
606
|
+
);
|
|
607
|
+
}
|
|
608
|
+
const converted = convertInlineImages(msg.body);
|
|
609
|
+
if (msg.forwardMessageId) {
|
|
610
|
+
const draftId = await this.buildDraftFromReference(
|
|
611
|
+
client,
|
|
612
|
+
`/me/messages/${encodeURIComponent(msg.forwardMessageId)}/createForward`,
|
|
613
|
+
{
|
|
614
|
+
message: {
|
|
615
|
+
toRecipients: msg.to.map(toRecipient),
|
|
616
|
+
ccRecipients: (msg.cc ?? []).map(toRecipient),
|
|
617
|
+
bccRecipients: (msg.bcc ?? []).map(toRecipient)
|
|
618
|
+
},
|
|
619
|
+
comment: ""
|
|
620
|
+
},
|
|
621
|
+
converted
|
|
622
|
+
);
|
|
623
|
+
return { id: draftId };
|
|
624
|
+
}
|
|
625
|
+
if (msg.inReplyTo) {
|
|
626
|
+
const createEndpoint = msg.replyAll ? `/me/messages/${encodeURIComponent(msg.inReplyTo)}/createReplyAll` : `/me/messages/${encodeURIComponent(msg.inReplyTo)}/createReply`;
|
|
627
|
+
const draftId = await this.buildDraftFromReference(
|
|
628
|
+
client,
|
|
629
|
+
createEndpoint,
|
|
630
|
+
{},
|
|
631
|
+
converted
|
|
632
|
+
);
|
|
633
|
+
return { id: draftId };
|
|
634
|
+
}
|
|
635
|
+
const draftPayload = {
|
|
636
|
+
subject: msg.subject,
|
|
637
|
+
body: {
|
|
638
|
+
contentType: msg.isHtml ? "HTML" : "Text",
|
|
639
|
+
content: converted.body
|
|
640
|
+
},
|
|
641
|
+
toRecipients: msg.to.map(toRecipient),
|
|
642
|
+
ccRecipients: (msg.cc ?? []).map(toRecipient),
|
|
643
|
+
bccRecipients: (msg.bcc ?? []).map(toRecipient)
|
|
644
|
+
};
|
|
645
|
+
if (converted.attachments.length > 0) {
|
|
646
|
+
draftPayload.attachments = converted.attachments;
|
|
647
|
+
}
|
|
648
|
+
const draft = await client.api("/me/messages").post(draftPayload);
|
|
649
|
+
return { id: draft.id };
|
|
650
|
+
}
|
|
651
|
+
async moveEmail(account, id, destinationId) {
|
|
652
|
+
const client = this.clients.get(account);
|
|
653
|
+
await client.api(`/me/messages/${encodeURIComponent(id)}/move`).post({ destinationId });
|
|
654
|
+
}
|
|
527
655
|
};
|
|
528
656
|
function mapRecipient(r) {
|
|
529
657
|
return {
|
|
@@ -579,6 +707,12 @@ var ImapProvider = class {
|
|
|
579
707
|
async sendEmail(_account, _msg) {
|
|
580
708
|
throw new Error(NOT_IMPLEMENTED);
|
|
581
709
|
}
|
|
710
|
+
async saveDraft(_account, _msg) {
|
|
711
|
+
throw new Error(NOT_IMPLEMENTED);
|
|
712
|
+
}
|
|
713
|
+
async moveEmail(_account, _id, _destinationId) {
|
|
714
|
+
throw new Error(NOT_IMPLEMENTED);
|
|
715
|
+
}
|
|
582
716
|
};
|
|
583
717
|
|
|
584
718
|
// src/providers/registry.ts
|
|
@@ -637,12 +771,16 @@ function selectBody(msg, format) {
|
|
|
637
771
|
}
|
|
638
772
|
|
|
639
773
|
// src/tools/index.ts
|
|
640
|
-
function ok(data) {
|
|
641
|
-
|
|
774
|
+
function ok(data, structuredContent) {
|
|
775
|
+
const result = {
|
|
642
776
|
content: [
|
|
643
777
|
{ type: "text", text: JSON.stringify(data, null, 2) }
|
|
644
778
|
]
|
|
645
779
|
};
|
|
780
|
+
if (structuredContent !== void 0) {
|
|
781
|
+
result.structuredContent = structuredContent;
|
|
782
|
+
}
|
|
783
|
+
return result;
|
|
646
784
|
}
|
|
647
785
|
function fail(message) {
|
|
648
786
|
return {
|
|
@@ -654,13 +792,51 @@ var emailAddrSchema = z.object({
|
|
|
654
792
|
address: z.string().email(),
|
|
655
793
|
name: z.string().optional()
|
|
656
794
|
});
|
|
795
|
+
var emailAddrOutputSchema = z.object({
|
|
796
|
+
name: z.string().optional(),
|
|
797
|
+
address: z.string()
|
|
798
|
+
});
|
|
799
|
+
var accountSummaryOutputSchema = z.object({
|
|
800
|
+
email: z.string(),
|
|
801
|
+
provider: z.enum(["outlook", "imap", "gmail"]),
|
|
802
|
+
displayName: z.string().optional(),
|
|
803
|
+
addedAt: z.string(),
|
|
804
|
+
hasSignature: z.boolean(),
|
|
805
|
+
hasStyle: z.boolean()
|
|
806
|
+
});
|
|
807
|
+
var emailSummaryOutputSchema = z.object({
|
|
808
|
+
id: z.string(),
|
|
809
|
+
subject: z.string(),
|
|
810
|
+
from: emailAddrOutputSchema.optional(),
|
|
811
|
+
to: z.array(emailAddrOutputSchema).optional(),
|
|
812
|
+
receivedAt: z.string().optional(),
|
|
813
|
+
preview: z.string().optional(),
|
|
814
|
+
isRead: z.boolean().optional(),
|
|
815
|
+
hasAttachments: z.boolean().optional(),
|
|
816
|
+
folder: z.string().optional()
|
|
817
|
+
});
|
|
818
|
+
var attachmentMetaOutputSchema = z.object({
|
|
819
|
+
id: z.string(),
|
|
820
|
+
name: z.string(),
|
|
821
|
+
contentType: z.string().optional(),
|
|
822
|
+
size: z.number().optional()
|
|
823
|
+
});
|
|
824
|
+
var styleOutputSchema = z.object({
|
|
825
|
+
fontFamily: z.string().optional(),
|
|
826
|
+
fontSize: z.string().optional(),
|
|
827
|
+
fontColor: z.string().optional()
|
|
828
|
+
});
|
|
657
829
|
function registerTools(server, opts) {
|
|
658
|
-
const { store, registry, readOnly = false } = opts;
|
|
830
|
+
const { store, registry, readOnly = false, draftOnly = false } = opts;
|
|
831
|
+
const listAccountsOutputSchema = {
|
|
832
|
+
accounts: z.array(accountSummaryOutputSchema)
|
|
833
|
+
};
|
|
659
834
|
server.registerTool(
|
|
660
835
|
"list_accounts",
|
|
661
836
|
{
|
|
662
837
|
description: "List all email accounts known to this server (no secrets). Use the returned `email` value as the `account` argument to other tools.",
|
|
663
|
-
inputSchema: {}
|
|
838
|
+
inputSchema: {},
|
|
839
|
+
outputSchema: listAccountsOutputSchema
|
|
664
840
|
},
|
|
665
841
|
async () => {
|
|
666
842
|
const rows = store.listAccounts().map((a) => ({
|
|
@@ -671,9 +847,34 @@ function registerTools(server, opts) {
|
|
|
671
847
|
hasSignature: !!a.signature,
|
|
672
848
|
hasStyle: !!(a.style && (a.style.fontFamily || a.style.fontSize || a.style.fontColor))
|
|
673
849
|
}));
|
|
674
|
-
|
|
850
|
+
const data = { accounts: rows };
|
|
851
|
+
return ok(data, data);
|
|
675
852
|
}
|
|
676
853
|
);
|
|
854
|
+
const addAccountOutputSchema = z.discriminatedUnion("status", [
|
|
855
|
+
z.object({
|
|
856
|
+
status: z.literal("pending"),
|
|
857
|
+
handle: z.string(),
|
|
858
|
+
verification: z.object({
|
|
859
|
+
userCode: z.string(),
|
|
860
|
+
verificationUri: z.string(),
|
|
861
|
+
expiresAt: z.string(),
|
|
862
|
+
message: z.string()
|
|
863
|
+
})
|
|
864
|
+
}),
|
|
865
|
+
z.object({
|
|
866
|
+
status: z.literal("ready"),
|
|
867
|
+
account: z.object({
|
|
868
|
+
email: z.string(),
|
|
869
|
+
provider: z.enum(["outlook", "imap", "gmail"]),
|
|
870
|
+
displayName: z.string().optional(),
|
|
871
|
+
tokens: z.record(z.unknown()),
|
|
872
|
+
addedAt: z.string(),
|
|
873
|
+
signature: z.string().optional(),
|
|
874
|
+
style: styleOutputSchema.optional()
|
|
875
|
+
})
|
|
876
|
+
})
|
|
877
|
+
]);
|
|
677
878
|
server.registerTool(
|
|
678
879
|
"add_account",
|
|
679
880
|
{
|
|
@@ -682,19 +883,33 @@ function registerTools(server, opts) {
|
|
|
682
883
|
provider: z.enum(["outlook", "imap", "gmail"]).describe("Email backend. v1 only fully implements 'outlook'."),
|
|
683
884
|
email: z.string().email().optional().describe("Optional hint \u2014 the provider will verify it against the auth result."),
|
|
684
885
|
config: z.record(z.unknown()).optional().describe("Provider-specific config (e.g. IMAP host/port). Unused for Outlook.")
|
|
685
|
-
}
|
|
886
|
+
},
|
|
887
|
+
outputSchema: addAccountOutputSchema
|
|
686
888
|
},
|
|
687
889
|
async (args) => {
|
|
688
890
|
if (readOnly) return fail("server is in --read-only mode; add_account is disabled");
|
|
689
891
|
const provider = registry.get(args.provider);
|
|
690
892
|
try {
|
|
691
893
|
const res = await provider.addAccount({ email: args.email, config: args.config });
|
|
692
|
-
return ok(res);
|
|
894
|
+
return ok(res, res);
|
|
693
895
|
} catch (err) {
|
|
694
896
|
return fail(errMsg(err));
|
|
695
897
|
}
|
|
696
898
|
}
|
|
697
899
|
);
|
|
900
|
+
const completeAddAccountOutputSchema = z.object({
|
|
901
|
+
status: z.enum(["pending", "ready", "expired", "error"]),
|
|
902
|
+
account: z.object({
|
|
903
|
+
email: z.string(),
|
|
904
|
+
provider: z.enum(["outlook", "imap", "gmail"]),
|
|
905
|
+
displayName: z.string().optional(),
|
|
906
|
+
tokens: z.record(z.unknown()),
|
|
907
|
+
addedAt: z.string(),
|
|
908
|
+
signature: z.string().optional(),
|
|
909
|
+
style: styleOutputSchema.optional()
|
|
910
|
+
}).optional(),
|
|
911
|
+
error: z.string().optional()
|
|
912
|
+
});
|
|
698
913
|
server.registerTool(
|
|
699
914
|
"complete_add_account",
|
|
700
915
|
{
|
|
@@ -702,7 +917,8 @@ function registerTools(server, opts) {
|
|
|
702
917
|
inputSchema: {
|
|
703
918
|
provider: z.enum(["outlook", "imap", "gmail"]),
|
|
704
919
|
handle: z.string().min(1)
|
|
705
|
-
}
|
|
920
|
+
},
|
|
921
|
+
outputSchema: completeAddAccountOutputSchema
|
|
706
922
|
},
|
|
707
923
|
async (args) => {
|
|
708
924
|
const provider = registry.get(args.provider);
|
|
@@ -711,23 +927,29 @@ function registerTools(server, opts) {
|
|
|
711
927
|
}
|
|
712
928
|
try {
|
|
713
929
|
const res = await provider.completeAddAccount(args.handle);
|
|
714
|
-
return ok(res);
|
|
930
|
+
return ok(res, res);
|
|
715
931
|
} catch (err) {
|
|
716
932
|
return fail(errMsg(err));
|
|
717
933
|
}
|
|
718
934
|
}
|
|
719
935
|
);
|
|
936
|
+
const accountSettingsOutputSchema = {
|
|
937
|
+
signature: z.string().nullable(),
|
|
938
|
+
style: styleOutputSchema.nullable()
|
|
939
|
+
};
|
|
720
940
|
server.registerTool(
|
|
721
941
|
"get_account_settings",
|
|
722
942
|
{
|
|
723
943
|
description: "Get signature (HTML) and style preferences for an account.",
|
|
724
|
-
inputSchema: { account: z.string().email() }
|
|
944
|
+
inputSchema: { account: z.string().email() },
|
|
945
|
+
outputSchema: accountSettingsOutputSchema
|
|
725
946
|
},
|
|
726
947
|
async (args) => {
|
|
727
948
|
try {
|
|
728
949
|
const acct = store.getAccount(args.account);
|
|
729
950
|
if (!acct) return fail(`no account registered for "${args.account}"`);
|
|
730
|
-
|
|
951
|
+
const data = { signature: acct.signature ?? null, style: acct.style ?? null };
|
|
952
|
+
return ok(data, data);
|
|
731
953
|
} catch (err) {
|
|
732
954
|
return fail(errMsg(err));
|
|
733
955
|
}
|
|
@@ -745,7 +967,8 @@ function registerTools(server, opts) {
|
|
|
745
967
|
fontSize: z.string().optional(),
|
|
746
968
|
fontColor: z.string().optional()
|
|
747
969
|
}).optional().describe("Font preferences applied to outgoing HTML emails. Pass null to clear.")
|
|
748
|
-
}
|
|
970
|
+
},
|
|
971
|
+
outputSchema: accountSettingsOutputSchema
|
|
749
972
|
},
|
|
750
973
|
async (args) => {
|
|
751
974
|
if (readOnly) return fail("server is in --read-only mode; set_account_settings is disabled");
|
|
@@ -757,24 +980,36 @@ function registerTools(server, opts) {
|
|
|
757
980
|
signature: args.signature ?? acct.signature,
|
|
758
981
|
style: args.style ?? acct.style
|
|
759
982
|
});
|
|
760
|
-
|
|
983
|
+
const data = { signature: updated.signature ?? null, style: updated.style ?? null };
|
|
984
|
+
return ok(data, data);
|
|
761
985
|
} catch (err) {
|
|
762
986
|
return fail(errMsg(err));
|
|
763
987
|
}
|
|
764
988
|
}
|
|
765
989
|
);
|
|
990
|
+
const removeAccountOutputSchema = {
|
|
991
|
+
removed: z.boolean(),
|
|
992
|
+
email: z.string()
|
|
993
|
+
};
|
|
766
994
|
server.registerTool(
|
|
767
995
|
"remove_account",
|
|
768
996
|
{
|
|
769
997
|
description: "Forget an account and delete its stored tokens. Disabled in --read-only mode.",
|
|
770
|
-
inputSchema: { email: z.string().email() }
|
|
998
|
+
inputSchema: { email: z.string().email() },
|
|
999
|
+
outputSchema: removeAccountOutputSchema
|
|
771
1000
|
},
|
|
772
1001
|
async (args) => {
|
|
773
1002
|
if (readOnly) return fail("server is in --read-only mode; remove_account is disabled");
|
|
774
1003
|
const removed = await store.removeAccount(args.email);
|
|
775
|
-
|
|
1004
|
+
const data = { removed, email: args.email };
|
|
1005
|
+
return ok(data, data);
|
|
776
1006
|
}
|
|
777
1007
|
);
|
|
1008
|
+
const emailListOutputSchema = {
|
|
1009
|
+
account: z.string(),
|
|
1010
|
+
count: z.number(),
|
|
1011
|
+
items: z.array(emailSummaryOutputSchema)
|
|
1012
|
+
};
|
|
778
1013
|
server.registerTool(
|
|
779
1014
|
"list_emails",
|
|
780
1015
|
{
|
|
@@ -784,7 +1019,8 @@ function registerTools(server, opts) {
|
|
|
784
1019
|
folder: z.string().default("inbox").optional(),
|
|
785
1020
|
limit: z.number().int().positive().max(100).optional(),
|
|
786
1021
|
unreadOnly: z.boolean().optional()
|
|
787
|
-
}
|
|
1022
|
+
},
|
|
1023
|
+
outputSchema: emailListOutputSchema
|
|
788
1024
|
},
|
|
789
1025
|
async (args) => {
|
|
790
1026
|
try {
|
|
@@ -794,7 +1030,8 @@ function registerTools(server, opts) {
|
|
|
794
1030
|
limit: args.limit,
|
|
795
1031
|
unreadOnly: args.unreadOnly
|
|
796
1032
|
});
|
|
797
|
-
|
|
1033
|
+
const data = { account: account.email, count: items.length, items };
|
|
1034
|
+
return ok(data, data);
|
|
798
1035
|
} catch (err) {
|
|
799
1036
|
return fail(errMsg(err));
|
|
800
1037
|
}
|
|
@@ -808,7 +1045,8 @@ function registerTools(server, opts) {
|
|
|
808
1045
|
account: z.string().email(),
|
|
809
1046
|
query: z.string().min(1),
|
|
810
1047
|
limit: z.number().int().positive().max(100).optional()
|
|
811
|
-
}
|
|
1048
|
+
},
|
|
1049
|
+
outputSchema: emailListOutputSchema
|
|
812
1050
|
},
|
|
813
1051
|
async (args) => {
|
|
814
1052
|
try {
|
|
@@ -816,12 +1054,29 @@ function registerTools(server, opts) {
|
|
|
816
1054
|
const items = await provider.searchEmails(account, args.query, {
|
|
817
1055
|
limit: args.limit
|
|
818
1056
|
});
|
|
819
|
-
|
|
1057
|
+
const data = { account: account.email, count: items.length, items };
|
|
1058
|
+
return ok(data, data);
|
|
820
1059
|
} catch (err) {
|
|
821
1060
|
return fail(errMsg(err));
|
|
822
1061
|
}
|
|
823
1062
|
}
|
|
824
1063
|
);
|
|
1064
|
+
const readEmailOutputSchema = {
|
|
1065
|
+
id: z.string(),
|
|
1066
|
+
subject: z.string(),
|
|
1067
|
+
from: emailAddrOutputSchema.optional(),
|
|
1068
|
+
to: z.array(emailAddrOutputSchema).optional(),
|
|
1069
|
+
cc: z.array(emailAddrOutputSchema).optional(),
|
|
1070
|
+
bcc: z.array(emailAddrOutputSchema).optional(),
|
|
1071
|
+
receivedAt: z.string().optional(),
|
|
1072
|
+
preview: z.string().optional(),
|
|
1073
|
+
isRead: z.boolean().optional(),
|
|
1074
|
+
hasAttachments: z.boolean().optional(),
|
|
1075
|
+
folder: z.string().optional(),
|
|
1076
|
+
attachments: z.array(attachmentMetaOutputSchema).optional(),
|
|
1077
|
+
body: z.string(),
|
|
1078
|
+
bodyFormat: z.enum(["markdown", "html", "text"])
|
|
1079
|
+
};
|
|
825
1080
|
server.registerTool(
|
|
826
1081
|
"read_email",
|
|
827
1082
|
{
|
|
@@ -832,7 +1087,8 @@ function registerTools(server, opts) {
|
|
|
832
1087
|
format: z.enum(["markdown", "html", "text"]).default("markdown").optional().describe(
|
|
833
1088
|
"Output body format. 'markdown' converts HTML to Markdown (default), 'html' returns the raw HTML, 'text' returns plain text."
|
|
834
1089
|
)
|
|
835
|
-
}
|
|
1090
|
+
},
|
|
1091
|
+
outputSchema: readEmailOutputSchema
|
|
836
1092
|
},
|
|
837
1093
|
async (args) => {
|
|
838
1094
|
try {
|
|
@@ -840,7 +1096,7 @@ function registerTools(server, opts) {
|
|
|
840
1096
|
const msg = await provider.readEmail(account, args.id);
|
|
841
1097
|
const format = args.format ?? "markdown";
|
|
842
1098
|
const body = selectBody(msg, format);
|
|
843
|
-
|
|
1099
|
+
const data = {
|
|
844
1100
|
id: msg.id,
|
|
845
1101
|
subject: msg.subject,
|
|
846
1102
|
from: msg.from,
|
|
@@ -855,12 +1111,18 @@ function registerTools(server, opts) {
|
|
|
855
1111
|
attachments: msg.attachments,
|
|
856
1112
|
body,
|
|
857
1113
|
bodyFormat: format
|
|
858
|
-
}
|
|
1114
|
+
};
|
|
1115
|
+
return ok(data, data);
|
|
859
1116
|
} catch (err) {
|
|
860
1117
|
return fail(errMsg(err));
|
|
861
1118
|
}
|
|
862
1119
|
}
|
|
863
1120
|
);
|
|
1121
|
+
const readAttachmentOutputSchema = {
|
|
1122
|
+
name: z.string(),
|
|
1123
|
+
contentType: z.string().optional(),
|
|
1124
|
+
path: z.string()
|
|
1125
|
+
};
|
|
864
1126
|
server.registerTool(
|
|
865
1127
|
"read_attachment",
|
|
866
1128
|
{
|
|
@@ -869,62 +1131,207 @@ function registerTools(server, opts) {
|
|
|
869
1131
|
account: z.string().email(),
|
|
870
1132
|
messageId: z.string().min(1),
|
|
871
1133
|
attachmentId: z.string().min(1)
|
|
872
|
-
}
|
|
1134
|
+
},
|
|
1135
|
+
outputSchema: readAttachmentOutputSchema
|
|
873
1136
|
},
|
|
874
1137
|
async (args) => {
|
|
875
1138
|
try {
|
|
876
1139
|
const { provider, account } = registry.resolveByEmail(args.account);
|
|
877
1140
|
const res = await provider.readAttachment(account, args.messageId, args.attachmentId);
|
|
878
|
-
return ok(res);
|
|
1141
|
+
return ok(res, res);
|
|
879
1142
|
} catch (err) {
|
|
880
1143
|
return fail(errMsg(err));
|
|
881
1144
|
}
|
|
882
1145
|
}
|
|
883
1146
|
);
|
|
1147
|
+
{
|
|
1148
|
+
const schema = {
|
|
1149
|
+
account: z.string().email(),
|
|
1150
|
+
id: z.string().min(1).describe("Message ID to move")
|
|
1151
|
+
};
|
|
1152
|
+
const archiveOutputSchema = {
|
|
1153
|
+
archived: z.literal(true),
|
|
1154
|
+
id: z.string()
|
|
1155
|
+
};
|
|
1156
|
+
server.registerTool(
|
|
1157
|
+
"archive_email",
|
|
1158
|
+
{
|
|
1159
|
+
description: "Move a message to the Archive folder. Disabled in --read-only mode.",
|
|
1160
|
+
inputSchema: schema,
|
|
1161
|
+
outputSchema: archiveOutputSchema
|
|
1162
|
+
},
|
|
1163
|
+
async (args) => {
|
|
1164
|
+
if (readOnly) return fail("server is in --read-only mode; archive_email is disabled");
|
|
1165
|
+
try {
|
|
1166
|
+
const { provider, account } = registry.resolveByEmail(args.account);
|
|
1167
|
+
await provider.moveEmail(account, args.id, "archive");
|
|
1168
|
+
const data = { archived: true, id: args.id };
|
|
1169
|
+
return ok(data, data);
|
|
1170
|
+
} catch (err) {
|
|
1171
|
+
return fail(errMsg(err));
|
|
1172
|
+
}
|
|
1173
|
+
}
|
|
1174
|
+
);
|
|
1175
|
+
const trashOutputSchema = {
|
|
1176
|
+
trashed: z.literal(true),
|
|
1177
|
+
id: z.string()
|
|
1178
|
+
};
|
|
1179
|
+
server.registerTool(
|
|
1180
|
+
"trash_email",
|
|
1181
|
+
{
|
|
1182
|
+
description: "Move a message to the Deleted Items (trash) folder. Disabled in --read-only mode.",
|
|
1183
|
+
inputSchema: schema,
|
|
1184
|
+
outputSchema: trashOutputSchema
|
|
1185
|
+
},
|
|
1186
|
+
async (args) => {
|
|
1187
|
+
if (readOnly) return fail("server is in --read-only mode; trash_email is disabled");
|
|
1188
|
+
try {
|
|
1189
|
+
const { provider, account } = registry.resolveByEmail(args.account);
|
|
1190
|
+
await provider.moveEmail(account, args.id, "deleteditems");
|
|
1191
|
+
const data = { trashed: true, id: args.id };
|
|
1192
|
+
return ok(data, data);
|
|
1193
|
+
} catch (err) {
|
|
1194
|
+
return fail(errMsg(err));
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
);
|
|
1198
|
+
}
|
|
1199
|
+
const moveEmailOutputSchema = {
|
|
1200
|
+
moved: z.literal(true),
|
|
1201
|
+
id: z.string(),
|
|
1202
|
+
destination: z.string()
|
|
1203
|
+
};
|
|
884
1204
|
server.registerTool(
|
|
885
|
-
"
|
|
1205
|
+
"move_email",
|
|
886
1206
|
{
|
|
887
|
-
description: "
|
|
1207
|
+
description: "Move a message to any folder by well-known name (e.g. 'inbox', 'drafts', 'junkemail', 'sentitems', 'outbox') or custom folder ID. Disabled in --read-only mode.",
|
|
888
1208
|
inputSchema: {
|
|
889
1209
|
account: z.string().email(),
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
remove_signature: z.boolean().default(false).optional().describe("Skip appending the account signature. Style is still applied.")
|
|
897
|
-
}
|
|
1210
|
+
id: z.string().min(1).describe("Message ID to move"),
|
|
1211
|
+
destination: z.string().min(1).describe(
|
|
1212
|
+
"Destination folder \u2014 a well-known folder name ('archive', 'deleteditems', 'inbox', 'drafts', 'junkemail', 'sentitems', 'outbox') or a raw folder ID."
|
|
1213
|
+
)
|
|
1214
|
+
},
|
|
1215
|
+
outputSchema: moveEmailOutputSchema
|
|
898
1216
|
},
|
|
899
1217
|
async (args) => {
|
|
900
|
-
if (readOnly) return fail("server is in --read-only mode;
|
|
1218
|
+
if (readOnly) return fail("server is in --read-only mode; move_email is disabled");
|
|
901
1219
|
try {
|
|
902
1220
|
const { provider, account } = registry.resolveByEmail(args.account);
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
signature: account.signature,
|
|
907
|
-
style: account.style,
|
|
908
|
-
removeSignature: args.remove_signature
|
|
909
|
-
});
|
|
910
|
-
const res = await provider.sendEmail(account, {
|
|
911
|
-
to: args.to,
|
|
912
|
-
cc: args.cc,
|
|
913
|
-
bcc: args.bcc,
|
|
914
|
-
subject: args.subject,
|
|
915
|
-
body: composed.body,
|
|
916
|
-
isHtml: composed.isHtml
|
|
917
|
-
});
|
|
918
|
-
return ok({ sent: true, ...res });
|
|
1221
|
+
await provider.moveEmail(account, args.id, args.destination);
|
|
1222
|
+
const data = { moved: true, id: args.id, destination: args.destination };
|
|
1223
|
+
return ok(data, data);
|
|
919
1224
|
} catch (err) {
|
|
920
1225
|
return fail(errMsg(err));
|
|
921
1226
|
}
|
|
922
1227
|
}
|
|
923
1228
|
);
|
|
1229
|
+
const sendEmailSchema = z.object({
|
|
1230
|
+
account: z.string().email(),
|
|
1231
|
+
to: z.array(emailAddrSchema).min(1),
|
|
1232
|
+
cc: z.array(emailAddrSchema).optional(),
|
|
1233
|
+
bcc: z.array(emailAddrSchema).optional(),
|
|
1234
|
+
subject: z.string(),
|
|
1235
|
+
body: z.string(),
|
|
1236
|
+
isHtml: z.boolean().optional(),
|
|
1237
|
+
include_signature: z.boolean().describe(
|
|
1238
|
+
"Whether to append the account's saved HTML signature to the email. If true, don't include a signature in the body param to avoid double signature. Returns an error if true but no signature is configured for this account."
|
|
1239
|
+
),
|
|
1240
|
+
inReplyTo: z.string().optional().describe(
|
|
1241
|
+
"Message ID to reply to. When set, sends as a threaded reply which includes the quoted thread history automatically."
|
|
1242
|
+
),
|
|
1243
|
+
replyAll: z.boolean().default(false).optional().describe(
|
|
1244
|
+
"When true and `inReplyTo` is set, reply to all recipients instead of just the sender."
|
|
1245
|
+
),
|
|
1246
|
+
forwardMessageId: z.string().optional().describe(
|
|
1247
|
+
"Message ID to forward. When set, sends as a forward of the specified message, preserving the original content. Mutually exclusive with `inReplyTo`."
|
|
1248
|
+
)
|
|
1249
|
+
});
|
|
1250
|
+
async function handleSendOrDraft(args, action, resultKey, toolName) {
|
|
1251
|
+
if (readOnly) return fail(`server is in --read-only mode; ${toolName} is disabled`);
|
|
1252
|
+
try {
|
|
1253
|
+
const { provider, account } = registry.resolveByEmail(args.account);
|
|
1254
|
+
if (args.include_signature && !account.signature) {
|
|
1255
|
+
return fail(
|
|
1256
|
+
"include_signature is true but no signature is configured for this account. Set up a signature first with set_account_settings."
|
|
1257
|
+
);
|
|
1258
|
+
}
|
|
1259
|
+
const composed = composeBody({
|
|
1260
|
+
body: args.body,
|
|
1261
|
+
isHtml: args.isHtml,
|
|
1262
|
+
signature: account.signature,
|
|
1263
|
+
style: account.style,
|
|
1264
|
+
includeSignature: args.include_signature
|
|
1265
|
+
});
|
|
1266
|
+
if (args.inReplyTo && args.forwardMessageId) {
|
|
1267
|
+
return fail(
|
|
1268
|
+
"inReplyTo and forwardMessageId are mutually exclusive \u2014 use one or the other"
|
|
1269
|
+
);
|
|
1270
|
+
}
|
|
1271
|
+
const res = await action(provider, account, {
|
|
1272
|
+
to: args.to,
|
|
1273
|
+
cc: args.cc,
|
|
1274
|
+
bcc: args.bcc,
|
|
1275
|
+
subject: args.subject,
|
|
1276
|
+
body: composed.body,
|
|
1277
|
+
isHtml: composed.isHtml,
|
|
1278
|
+
inReplyTo: args.inReplyTo,
|
|
1279
|
+
replyAll: args.replyAll,
|
|
1280
|
+
forwardMessageId: args.forwardMessageId
|
|
1281
|
+
});
|
|
1282
|
+
const result = { [resultKey]: true, ...res };
|
|
1283
|
+
if (toolName === "draft_email" && res.id) {
|
|
1284
|
+
const draft = await provider.readEmail(account, res.id);
|
|
1285
|
+
result.draftHtml = draft.bodyHtml;
|
|
1286
|
+
}
|
|
1287
|
+
return ok(result, result);
|
|
1288
|
+
} catch (err) {
|
|
1289
|
+
return fail(errMsg(err));
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1292
|
+
const sendEmailOutputSchema = {
|
|
1293
|
+
sent: z.literal(true),
|
|
1294
|
+
id: z.string()
|
|
1295
|
+
};
|
|
1296
|
+
if (!draftOnly) {
|
|
1297
|
+
server.registerTool(
|
|
1298
|
+
"send_email",
|
|
1299
|
+
{
|
|
1300
|
+
description: "Send an email from the given account. Appends the account's signature (HTML) and applies style preferences when `include_signature` is true. Returns an error if `include_signature` is true but no signature is configured. When `inReplyTo` is set, sends as a reply (or reply-all) which preserves thread history and conversation threading. When `forwardMessageId` is set, sends as a forward of the specified message, preserving the original content. `inReplyTo` and `forwardMessageId` are mutually exclusive. Disabled in --read-only mode.",
|
|
1301
|
+
inputSchema: sendEmailSchema,
|
|
1302
|
+
outputSchema: sendEmailOutputSchema
|
|
1303
|
+
},
|
|
1304
|
+
async (args) => handleSendOrDraft(
|
|
1305
|
+
args,
|
|
1306
|
+
(p, a, m) => p.sendEmail(a, m),
|
|
1307
|
+
"sent",
|
|
1308
|
+
"send_email"
|
|
1309
|
+
)
|
|
1310
|
+
);
|
|
1311
|
+
}
|
|
1312
|
+
const draftEmailOutputSchema = {
|
|
1313
|
+
draft: z.literal(true),
|
|
1314
|
+
id: z.string(),
|
|
1315
|
+
draftHtml: z.string().optional()
|
|
1316
|
+
};
|
|
1317
|
+
server.registerTool(
|
|
1318
|
+
"draft_email",
|
|
1319
|
+
{
|
|
1320
|
+
description: "Create a draft email from the given account without sending it. Works identically to send_email \u2014 appends signature when `include_signature` is true, applies style, and supports replies and forwards \u2014 but saves the message to the Drafts folder instead of sending. Returns the draft message ID and the draft's HTML body content (`draftHtml`). Before sending the draft, inspect `draftHtml` to verify the draft looks correct: no duplicate signature blocks, no broken or missing inline images, no malformed HTML, and no other formatting issues. Disabled in --read-only mode.",
|
|
1321
|
+
inputSchema: sendEmailSchema,
|
|
1322
|
+
outputSchema: draftEmailOutputSchema
|
|
1323
|
+
},
|
|
1324
|
+
async (args) => handleSendOrDraft(
|
|
1325
|
+
args,
|
|
1326
|
+
(p, a, m) => p.saveDraft(a, m),
|
|
1327
|
+
"draft",
|
|
1328
|
+
"draft_email"
|
|
1329
|
+
)
|
|
1330
|
+
);
|
|
924
1331
|
}
|
|
925
1332
|
function composeBody(input) {
|
|
926
|
-
const { body, isHtml = false, signature, style,
|
|
927
|
-
const hasSignature =
|
|
1333
|
+
const { body, isHtml = false, signature, style, includeSignature } = input;
|
|
1334
|
+
const hasSignature = includeSignature && !!signature;
|
|
928
1335
|
const hasStyle = !!(style && (style.fontFamily || style.fontSize || style.fontColor));
|
|
929
1336
|
if (!hasSignature && !hasStyle) {
|
|
930
1337
|
return { body, isHtml };
|
|
@@ -958,7 +1365,7 @@ function errMsg(err) {
|
|
|
958
1365
|
}
|
|
959
1366
|
|
|
960
1367
|
// src/version.ts
|
|
961
|
-
var VERSION = "0.
|
|
1368
|
+
var VERSION = "0.3.0";
|
|
962
1369
|
|
|
963
1370
|
// src/server.ts
|
|
964
1371
|
async function startServer(opts = {}) {
|
|
@@ -968,7 +1375,7 @@ async function startServer(opts = {}) {
|
|
|
968
1375
|
{ name: "hypermail-mcp", version: VERSION },
|
|
969
1376
|
{ capabilities: { tools: {}, logging: {} } }
|
|
970
1377
|
);
|
|
971
|
-
registerTools(server, { store, registry, readOnly: !!opts.readOnly });
|
|
1378
|
+
registerTools(server, { store, registry, readOnly: !!opts.readOnly, draftOnly: !!opts.draftOnly });
|
|
972
1379
|
if (opts.http) {
|
|
973
1380
|
await startHttp(server, opts.host ?? "127.0.0.1", opts.port ?? 3e3);
|
|
974
1381
|
} else {
|
|
@@ -1086,12 +1493,14 @@ async function main() {
|
|
|
1086
1493
|
printHelp();
|
|
1087
1494
|
return;
|
|
1088
1495
|
}
|
|
1496
|
+
const draftOnly = process.env.HYPERMAIL_DRAFT_ONLY === "true";
|
|
1089
1497
|
await startServer({
|
|
1090
1498
|
http: opts.http,
|
|
1091
1499
|
port: opts.port,
|
|
1092
1500
|
host: opts.host,
|
|
1093
1501
|
dataDir: opts.dataDir,
|
|
1094
|
-
readOnly: opts.readOnly
|
|
1502
|
+
readOnly: opts.readOnly,
|
|
1503
|
+
draftOnly
|
|
1095
1504
|
});
|
|
1096
1505
|
}
|
|
1097
1506
|
main().catch((err) => {
|
package/dist/cli.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/server.ts","../src/store/account-store.ts","../src/providers/outlook/index.ts","../src/providers/outlook/client.ts","../src/providers/outlook/auth.ts","../src/providers/imap/index.ts","../src/providers/registry.ts","../src/tools/index.ts","../src/html-to-markdown.ts","../src/version.ts","../src/cli.ts"],"sourcesContent":["import { McpServer } from \"@modelcontextprotocol/sdk/server/mcp.js\";\nimport { StdioServerTransport } from \"@modelcontextprotocol/sdk/server/stdio.js\";\nimport { StreamableHTTPServerTransport } from \"@modelcontextprotocol/sdk/server/streamableHttp.js\";\nimport { randomUUID } from \"node:crypto\";\nimport { createServer as createHttpServer, type IncomingMessage, type ServerResponse } from \"node:http\";\n\nimport { AccountStore } from \"./store/account-store.js\";\nimport { buildRegistry } from \"./providers/registry.js\";\nimport { registerTools } from \"./tools/index.js\";\nimport { VERSION } from \"./version.js\";\n\nexport interface ServerOptions {\n http?: boolean;\n port?: number;\n host?: string;\n dataDir?: string;\n readOnly?: boolean;\n}\n\nexport async function startServer(opts: ServerOptions = {}): Promise<void> {\n const store = await AccountStore.open({ dataDir: opts.dataDir });\n const registry = buildRegistry({ store });\n\n const server = new McpServer(\n { name: \"hypermail-mcp\", version: VERSION },\n { capabilities: { tools: {}, logging: {} } },\n );\n\n registerTools(server, { store, registry, readOnly: !!opts.readOnly });\n\n if (opts.http) {\n await startHttp(server, opts.host ?? \"127.0.0.1\", opts.port ?? 3000);\n } else {\n const transport = new StdioServerTransport();\n await server.connect(transport);\n }\n}\n\nasync function startHttp(server: McpServer, host: string, port: number): Promise<void> {\n // One transport per session, keyed by Mcp-Session-Id header.\n const sessions = new Map<string, StreamableHTTPServerTransport>();\n\n const http = createHttpServer(async (req: IncomingMessage, res: ServerResponse) => {\n try {\n if (!req.url || !req.url.startsWith(\"/mcp\")) {\n res.statusCode = 404;\n res.end(\"not found\");\n return;\n }\n const sessionId = (req.headers[\"mcp-session-id\"] as string | undefined) ?? undefined;\n let transport = sessionId ? sessions.get(sessionId) : undefined;\n\n if (!transport) {\n transport = new StreamableHTTPServerTransport({\n sessionIdGenerator: () => randomUUID(),\n onsessioninitialized: (sid: string) => {\n sessions.set(sid, transport!);\n },\n });\n transport.onclose = () => {\n if (transport!.sessionId) sessions.delete(transport!.sessionId);\n };\n await server.connect(transport);\n }\n\n // Buffer body for POST / DELETE\n let body: unknown = undefined;\n if (req.method === \"POST\" || req.method === \"DELETE\") {\n const chunks: Buffer[] = [];\n for await (const chunk of req) chunks.push(chunk as Buffer);\n const raw = Buffer.concat(chunks).toString(\"utf8\");\n body = raw ? JSON.parse(raw) : undefined;\n }\n await transport.handleRequest(req, res, body);\n } catch (err) {\n // eslint-disable-next-line no-console\n console.error(\"[hypermail-mcp] http error:\", err);\n if (!res.headersSent) {\n res.statusCode = 500;\n res.end(\"internal error\");\n }\n }\n });\n\n await new Promise<void>((resolve) => http.listen(port, host, resolve));\n // eslint-disable-next-line no-console\n console.error(`[hypermail-mcp] listening on http://${host}:${port}/mcp`);\n}\n","import {\n createCipheriv,\n createDecipheriv,\n randomBytes,\n createHash,\n} from \"node:crypto\";\nimport { promises as fs } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport path from \"node:path\";\n\n/**\n * One stored account. `tokens` is provider-specific (e.g. serialized MSAL cache\n * for Outlook, host/port/password blob for IMAP) — the store is opaque to it.\n */\nexport interface AccountRecord {\n email: string;\n provider: \"outlook\" | \"imap\" | \"gmail\";\n displayName?: string;\n tokens: Record<string, unknown>;\n addedAt: string;\n /** HTML snippet — may contain formatting, images, links. Injected at end of outgoing emails. */\n signature?: string;\n /** Font/style preferences applied to outgoing HTML emails. */\n style?: { fontFamily?: string; fontSize?: string; fontColor?: string };\n}\n\ninterface StoreFile {\n version: 1;\n accounts: AccountRecord[];\n}\n\nexport interface OpenOptions {\n dataDir?: string;\n /** Inject the encryption key directly (mostly for tests). Otherwise resolved\n * from `HYPERMAIL_MCP_KEY` env, then OS keychain, then auto-generated. */\n key?: Buffer;\n}\n\nconst FILE_NAME = \"accounts.json.enc\";\nconst ALGO = \"aes-256-gcm\";\nconst KEY_LEN = 32;\n\nexport class AccountStore {\n private constructor(\n private readonly filePath: string,\n private readonly key: Buffer,\n private data: StoreFile,\n ) {}\n\n static async open(opts: OpenOptions = {}): Promise<AccountStore> {\n const dataDir = resolveDataDir(opts.dataDir);\n await fs.mkdir(dataDir, { recursive: true, mode: 0o700 });\n const filePath = path.join(dataDir, FILE_NAME);\n const key = opts.key ?? (await resolveKey(dataDir));\n\n let data: StoreFile;\n try {\n const buf = await fs.readFile(filePath);\n data = decrypt(buf, key);\n } catch (err: unknown) {\n if ((err as NodeJS.ErrnoException).code === \"ENOENT\") {\n data = { version: 1, accounts: [] };\n } else {\n throw err;\n }\n }\n return new AccountStore(filePath, key, data);\n }\n\n listAccounts(): AccountRecord[] {\n // Return shallow clones without tokens leaking out unintentionally is the\n // caller's job; here we expose the full record for provider use.\n return this.data.accounts.map((a) => ({ ...a }));\n }\n\n getAccount(email: string): AccountRecord | undefined {\n const norm = email.trim().toLowerCase();\n const rec = this.data.accounts.find((a) => a.email.toLowerCase() === norm);\n return rec ? { ...rec } : undefined;\n }\n\n async upsertAccount(rec: AccountRecord): Promise<AccountRecord> {\n const norm = rec.email.trim().toLowerCase();\n const next: AccountRecord = { ...rec, email: norm };\n const idx = this.data.accounts.findIndex((a) => a.email.toLowerCase() === norm);\n if (idx >= 0) this.data.accounts[idx] = next;\n else this.data.accounts.push(next);\n await this.flush();\n return { ...next };\n }\n\n async removeAccount(email: string): Promise<boolean> {\n const norm = email.trim().toLowerCase();\n const before = this.data.accounts.length;\n this.data.accounts = this.data.accounts.filter((a) => a.email.toLowerCase() !== norm);\n if (this.data.accounts.length === before) return false;\n await this.flush();\n return true;\n }\n\n private async flush(): Promise<void> {\n const buf = encrypt(this.data, this.key);\n const tmp = `${this.filePath}.${process.pid}.${Date.now()}.tmp`;\n await fs.writeFile(tmp, buf, { mode: 0o600 });\n await fs.rename(tmp, this.filePath);\n }\n}\n\n// ---------- encryption helpers ----------\n\nfunction encrypt(data: StoreFile, key: Buffer): Buffer {\n const iv = randomBytes(12);\n const cipher = createCipheriv(ALGO, key, iv);\n const plaintext = Buffer.from(JSON.stringify(data), \"utf8\");\n const ct = Buffer.concat([cipher.update(plaintext), cipher.final()]);\n const tag = cipher.getAuthTag();\n // layout: [1-byte version=1][12 iv][16 tag][ct...]\n return Buffer.concat([Buffer.from([1]), iv, tag, ct]);\n}\n\nfunction decrypt(buf: Buffer, key: Buffer): StoreFile {\n if (buf.length < 1 + 12 + 16 + 1) throw new Error(\"accounts file truncated\");\n const v = buf[0];\n if (v !== 1) throw new Error(`unsupported accounts file version: ${v}`);\n const iv = buf.subarray(1, 13);\n const tag = buf.subarray(13, 29);\n const ct = buf.subarray(29);\n const decipher = createDecipheriv(ALGO, key, iv);\n decipher.setAuthTag(tag);\n const pt = Buffer.concat([decipher.update(ct), decipher.final()]);\n const parsed = JSON.parse(pt.toString(\"utf8\")) as StoreFile;\n if (parsed.version !== 1 || !Array.isArray(parsed.accounts)) {\n throw new Error(\"accounts file is malformed\");\n }\n return parsed;\n}\n\n// ---------- key + path resolution ----------\n\nfunction resolveDataDir(explicit?: string): string {\n if (explicit && explicit.length > 0) return path.resolve(explicit);\n const env = process.env.HYPERMAIL_MCP_DATA_DIR;\n if (env && env.length > 0) return path.resolve(env);\n return path.join(homedir(), \".hypermail-mcp\");\n}\n\nfunction parseEnvKey(raw: string): Buffer | undefined {\n const s = raw.trim();\n // hex (64 chars)\n if (/^[0-9a-fA-F]{64}$/.test(s)) return Buffer.from(s, \"hex\");\n // base64 — accept any length, then check\n try {\n const buf = Buffer.from(s, \"base64\");\n if (buf.length === KEY_LEN) return buf;\n } catch {\n /* ignore */\n }\n // last-resort: derive 32 bytes via SHA-256 over the raw string. This lets\n // users pass any passphrase; not ideal but predictable.\n return createHash(\"sha256\").update(s, \"utf8\").digest();\n}\n\nasync function resolveKey(dataDir: string): Promise<Buffer> {\n const env = process.env.HYPERMAIL_MCP_KEY;\n if (env && env.length > 0) {\n const k = parseEnvKey(env);\n if (k) return k;\n }\n\n // Try OS keychain via keytar (optional dep).\n const fromKeytar = await tryKeytarGet();\n if (fromKeytar) return fromKeytar;\n\n // Local-dev fallback: persist a generated key to a 0600 file next to the\n // accounts blob so subsequent runs can decrypt. Hosted deployments should\n // always set HYPERMAIL_MCP_KEY explicitly.\n const keyFile = path.join(dataDir, \"master.key\");\n try {\n const existing = await fs.readFile(keyFile);\n if (existing.length === KEY_LEN) return existing;\n } catch {\n /* fall through to generate */\n }\n const gen = randomBytes(KEY_LEN);\n await fs.writeFile(keyFile, gen, { mode: 0o600 });\n await tryKeytarSet(gen);\n return gen;\n}\n\nasync function tryKeytarGet(): Promise<Buffer | undefined> {\n try {\n const mod = (await import(\"keytar\")) as typeof import(\"keytar\");\n const val = await mod.getPassword(\"hypermail-mcp\", \"master\");\n if (val) {\n const buf = Buffer.from(val, \"base64\");\n if (buf.length === KEY_LEN) return buf;\n }\n } catch {\n /* keytar not installed or unsupported platform */\n }\n return undefined;\n}\n\nasync function tryKeytarSet(key: Buffer): Promise<void> {\n try {\n const mod = (await import(\"keytar\")) as typeof import(\"keytar\");\n await mod.setPassword(\"hypermail-mcp\", \"master\", key.toString(\"base64\"));\n } catch {\n /* ignore */\n }\n}\n","import { randomUUID } from \"node:crypto\";\nimport { writeFileSync } from \"node:fs\";\nimport { tmpdir } from \"node:os\";\nimport { join as pathJoin } from \"node:path\";\n\nimport { ResponseType } from \"@microsoft/microsoft-graph-client\";\n\nimport type { AccountRecord, AccountStore } from \"../../store/account-store.js\";\nimport type {\n AddAccountInput,\n AddAccountResult,\n AttachmentContent,\n CompleteAddAccountResult,\n EmailFull,\n EmailProvider,\n EmailSummary,\n ListEmailsOptions,\n SearchEmailsOptions,\n SendInput,\n EmailAddress,\n} from \"../types.js\";\nimport { OutlookClientFactory } from \"./client.js\";\nimport {\n awaitDeviceCodeReady,\n beginDeviceCode,\n type DeviceCodeBegin,\n type SerializedTokens,\n} from \"./auth.js\";\n\ninterface PendingFlow {\n begin: DeviceCodeBegin;\n emailHint?: string;\n startedAt: number;\n settled: \"pending\" | \"ready\" | \"error\" | \"expired\";\n account?: AccountRecord;\n error?: string;\n}\n\nexport interface OutlookProviderOptions {\n store: AccountStore;\n}\n\nexport class OutlookProvider implements EmailProvider {\n readonly id = \"outlook\" as const;\n private readonly clients: OutlookClientFactory;\n private readonly pending = new Map<string, PendingFlow>();\n\n constructor(private readonly opts: OutlookProviderOptions) {\n this.clients = new OutlookClientFactory(opts.store);\n }\n\n // ---------- account lifecycle ----------\n\n async addAccount(input: AddAccountInput): Promise<AddAccountResult> {\n const begin = beginDeviceCode();\n await awaitDeviceCodeReady(begin);\n\n const handle = randomUUID();\n const flow: PendingFlow = {\n begin,\n emailHint: input.email,\n startedAt: Date.now(),\n settled: \"pending\",\n };\n this.pending.set(handle, flow);\n\n // Fire-and-forget: when the user finishes, persist and update flow.\n begin.result\n .then(async ({ tokens, account }) => {\n const email = (account.username || input.email || \"\").toLowerCase();\n if (!email) {\n flow.settled = \"error\";\n flow.error = \"no email returned from Microsoft account\";\n return;\n }\n const rec: AccountRecord = {\n email,\n provider: \"outlook\",\n displayName: account.name ?? undefined,\n tokens: tokens as unknown as Record<string, unknown>,\n addedAt: new Date().toISOString(),\n };\n const saved = await this.opts.store.upsertAccount(rec);\n flow.account = saved;\n flow.settled = \"ready\";\n })\n .catch((err: unknown) => {\n flow.settled = \"error\";\n flow.error = err instanceof Error ? err.message : String(err);\n });\n\n return {\n status: \"pending\",\n handle,\n verification: {\n userCode: begin.userCode,\n verificationUri: begin.verificationUri,\n expiresAt: begin.expiresAt,\n message: begin.message,\n },\n };\n }\n\n async completeAddAccount(handle: string): Promise<CompleteAddAccountResult> {\n const flow = this.pending.get(handle);\n if (!flow) return { status: \"error\", error: \"unknown handle\" };\n // expire after 20 minutes regardless\n if (Date.now() - flow.startedAt > 20 * 60_000 && flow.settled === \"pending\") {\n flow.settled = \"expired\";\n flow.begin.cancel();\n }\n if (flow.settled === \"ready\" && flow.account) {\n this.pending.delete(handle);\n return { status: \"ready\", account: flow.account };\n }\n if (flow.settled === \"error\") {\n this.pending.delete(handle);\n return { status: \"error\", error: flow.error ?? \"unknown error\" };\n }\n if (flow.settled === \"expired\") {\n this.pending.delete(handle);\n return { status: \"expired\" };\n }\n return { status: \"pending\" };\n }\n\n // ---------- email ops ----------\n\n async listEmails(\n account: AccountRecord,\n opts: ListEmailsOptions,\n ): Promise<EmailSummary[]> {\n const client = this.clients.get(account);\n const limit = clampLimit(opts.limit, 25, 100);\n const folder = opts.folder ?? \"inbox\";\n const filterParts: string[] = [];\n if (opts.unreadOnly) filterParts.push(\"isRead eq false\");\n\n let req = client\n .api(`/me/mailFolders/${encodeURIComponent(folder)}/messages`)\n .top(limit)\n .select([\n \"id\",\n \"subject\",\n \"from\",\n \"toRecipients\",\n \"receivedDateTime\",\n \"bodyPreview\",\n \"isRead\",\n \"hasAttachments\",\n ].join(\",\"))\n .orderby(\"receivedDateTime DESC\");\n\n if (filterParts.length > 0) req = req.filter(filterParts.join(\" and \"));\n\n const res = (await req.get()) as { value: GraphMessage[] };\n return res.value.map((m) => mapSummary(m, folder));\n }\n\n async searchEmails(\n account: AccountRecord,\n query: string,\n opts: SearchEmailsOptions,\n ): Promise<EmailSummary[]> {\n const client = this.clients.get(account);\n const limit = clampLimit(opts.limit, 25, 100);\n // $search requires the ConsistencyLevel: eventual header\n const res = (await client\n .api(\"/me/messages\")\n .header(\"ConsistencyLevel\", \"eventual\")\n .top(limit)\n .search(`\"${query.replace(/\"/g, '\\\\\"')}\"`)\n .select(\n [\n \"id\",\n \"subject\",\n \"from\",\n \"toRecipients\",\n \"receivedDateTime\",\n \"bodyPreview\",\n \"isRead\",\n \"hasAttachments\",\n ].join(\",\"),\n )\n .get()) as { value: GraphMessage[] };\n return res.value.map((m) => mapSummary(m));\n }\n\n async readEmail(account: AccountRecord, id: string): Promise<EmailFull> {\n const client = this.clients.get(account);\n const m = (await client\n .api(`/me/messages/${encodeURIComponent(id)}`)\n .select(\n [\n \"id\",\n \"subject\",\n \"from\",\n \"toRecipients\",\n \"ccRecipients\",\n \"bccRecipients\",\n \"receivedDateTime\",\n \"bodyPreview\",\n \"isRead\",\n \"hasAttachments\",\n \"body\",\n ].join(\",\"),\n )\n .get()) as GraphMessage;\n\n let attachments: EmailFull[\"attachments\"] = undefined;\n if (m.hasAttachments) {\n try {\n const attRes = (await client\n .api(`/me/messages/${encodeURIComponent(id)}/attachments`)\n .select(\"id,name,contentType,size\")\n .get()) as { value: GraphAttachment[] };\n attachments = attRes.value.map((a) => ({\n id: a.id,\n name: a.name,\n contentType: a.contentType,\n size: a.size,\n }));\n } catch {\n /* ignore attachment listing failure */\n }\n }\n\n const summary = mapSummary(m);\n const body = m.body;\n return {\n ...summary,\n cc: (m.ccRecipients ?? []).map(mapRecipient),\n bcc: (m.bccRecipients ?? []).map(mapRecipient),\n bodyText: body?.contentType === \"text\" ? body.content : undefined,\n bodyHtml: body?.contentType === \"html\" ? body.content : undefined,\n attachments,\n };\n }\n\n async readAttachment(\n account: AccountRecord,\n messageId: string,\n attachmentId: string,\n ): Promise<AttachmentContent> {\n const client = this.clients.get(account);\n // First, get the attachment metadata to know the filename\n const att = (await client\n .api(`/me/messages/${encodeURIComponent(messageId)}/attachments/${encodeURIComponent(attachmentId)}`)\n .select(\"name,contentType\")\n .get()) as { name: string; contentType?: string };\n\n // Download the raw content as ArrayBuffer\n const data = (await client\n .api(`/me/messages/${encodeURIComponent(messageId)}/attachments/${encodeURIComponent(attachmentId)}/$value`)\n .responseType(ResponseType.ARRAYBUFFER)\n .get()) as ArrayBuffer;\n\n // Write to temp file with original name\n const outPath = pathJoin(tmpdir(), att.name);\n writeFileSync(outPath, Buffer.from(data));\n\n return {\n name: att.name,\n contentType: att.contentType,\n path: outPath,\n };\n }\n\n async sendEmail(\n account: AccountRecord,\n msg: SendInput,\n ): Promise<{ id: string }> {\n const client = this.clients.get(account);\n const payload = {\n message: {\n subject: msg.subject,\n body: {\n contentType: msg.isHtml ? \"HTML\" : \"Text\",\n content: msg.body,\n },\n toRecipients: msg.to.map(toRecipient),\n ccRecipients: (msg.cc ?? []).map(toRecipient),\n bccRecipients: (msg.bcc ?? []).map(toRecipient),\n },\n saveToSentItems: true,\n };\n await client.api(\"/me/sendMail\").post(payload);\n // Graph's sendMail returns 202 with no body; we don't have an id back.\n return { id: \"\" };\n }\n}\n\n// ---------- mapping helpers ----------\n\ninterface GraphRecipient {\n emailAddress?: { name?: string; address?: string };\n}\ninterface GraphMessage {\n id: string;\n subject?: string;\n from?: GraphRecipient;\n toRecipients?: GraphRecipient[];\n ccRecipients?: GraphRecipient[];\n bccRecipients?: GraphRecipient[];\n receivedDateTime?: string;\n bodyPreview?: string;\n isRead?: boolean;\n hasAttachments?: boolean;\n body?: { contentType?: \"text\" | \"html\"; content?: string };\n}\ninterface GraphAttachment {\n id: string;\n name: string;\n contentType?: string;\n size?: number;\n}\n\nfunction mapRecipient(r: GraphRecipient): EmailAddress {\n return {\n name: r.emailAddress?.name,\n address: r.emailAddress?.address ?? \"\",\n };\n}\n\nfunction mapSummary(m: GraphMessage, folder?: string): EmailSummary {\n return {\n id: m.id,\n subject: m.subject ?? \"\",\n from: m.from ? mapRecipient(m.from) : undefined,\n to: (m.toRecipients ?? []).map(mapRecipient),\n receivedAt: m.receivedDateTime,\n preview: m.bodyPreview,\n isRead: m.isRead,\n hasAttachments: m.hasAttachments,\n folder,\n };\n}\n\nfunction toRecipient(a: EmailAddress): GraphRecipient {\n return { emailAddress: { name: a.name, address: a.address } };\n}\n\nfunction clampLimit(v: number | undefined, dflt: number, max: number): number {\n if (!v || v <= 0) return dflt;\n return Math.min(v, max);\n}\n","import \"isomorphic-fetch\";\nimport {\n Client,\n type AuthenticationProvider,\n} from \"@microsoft/microsoft-graph-client\";\n\nimport type { AccountStore, AccountRecord } from \"../../store/account-store.js\";\nimport { acquireAccessToken, type SerializedTokens } from \"./auth.js\";\n\n/**\n * Builds a Graph `Client` bound to a stored account. The client uses an\n * `AuthenticationProvider` that calls msal silently on every request, and\n * writes the (possibly-refreshed) cache back into the AccountStore.\n *\n * Clients are cached per email since the underlying SDK reuses connections.\n */\nexport class OutlookClientFactory {\n private readonly cache = new Map<string, Client>();\n\n constructor(private readonly store: AccountStore) {}\n\n get(account: AccountRecord): Client {\n const key = account.email.toLowerCase();\n const existing = this.cache.get(key);\n if (existing) return existing;\n\n const store = this.store;\n const provider: AuthenticationProvider = {\n getAccessToken: async () => {\n const fresh = store.getAccount(account.email) ?? account;\n const tokens = fresh.tokens as unknown as SerializedTokens;\n const { accessToken, tokens: nextTokens } = await acquireAccessToken(tokens);\n // Persist refreshed cache opportunistically; failures here shouldn't\n // break the in-flight Graph call.\n if (nextTokens.msalCache !== tokens.msalCache) {\n store\n .upsertAccount({\n ...fresh,\n tokens: nextTokens as unknown as Record<string, unknown>,\n })\n .catch(() => {\n /* swallow — next call will refresh again */\n });\n }\n return accessToken;\n },\n };\n\n const client = Client.initWithMiddleware({ authProvider: provider });\n this.cache.set(key, client);\n return client;\n }\n\n /** Drop a cached client (e.g. after removeAccount). */\n invalidate(email: string): void {\n this.cache.delete(email.toLowerCase());\n }\n}\n","import {\n PublicClientApplication,\n type Configuration,\n type AuthenticationResult,\n type AccountInfo,\n} from \"@azure/msal-node\";\n\n/**\n * Public client app id. Default is the well-known `ms-365` client id used by\n * the softeria/ms-365-mcp-server project — it's a public client registered\n * for personal MSA + multi-tenant work/school. Users can override via\n * MS_CLIENT_ID for their own Entra app registrations.\n */\nconst DEFAULT_CLIENT_ID = \"084a3e9f-a9f4-43f7-89f9-d229cf97853e\";\n// ^ Pre-registered public client app (same one used by softeria/ms-365-mcp-server).\n// Supports personal MSA + multi-tenant work/school via device-code flow.\n// Operators should set MS_CLIENT_ID to a client they control for production.\n\nconst DEFAULT_SCOPES = [\n \"offline_access\",\n \"User.Read\",\n \"Mail.ReadWrite\",\n \"Mail.Send\",\n];\n\nexport interface DeviceCodeBegin {\n userCode: string;\n verificationUri: string;\n message: string;\n expiresAt: string; // ISO\n /** Resolves with the auth result once the user completes the flow. */\n result: Promise<{ tokens: SerializedTokens; account: AccountInfo }>;\n /** Aborts the in-flight polling promise. */\n cancel(): void;\n}\n\nexport interface SerializedTokens {\n /** MSAL cache JSON, encrypted at rest by the account store. */\n msalCache: string;\n /** Home account id used to look up the account in the rehydrated cache. */\n homeAccountId: string;\n /** Tenant id captured at sign-in. */\n tenantId: string;\n /** Username captured at sign-in (typically the primary email). */\n username: string;\n scopes: string[];\n}\n\nfunction makeConfig(prevCacheJson?: string): Configuration {\n const clientId = process.env.MS_CLIENT_ID || DEFAULT_CLIENT_ID;\n const tenant = process.env.MS_TENANT_ID || \"common\";\n return {\n auth: {\n clientId,\n authority: `https://login.microsoftonline.com/${tenant}`,\n },\n cache: prevCacheJson\n ? {\n // msal-node supports an in-memory cache plugin; we hydrate manually\n // below via deserialize after construction.\n }\n : undefined,\n };\n}\n\nexport function buildPca(prevCacheJson?: string): PublicClientApplication {\n const pca = new PublicClientApplication(makeConfig(prevCacheJson));\n if (prevCacheJson) {\n pca.getTokenCache().deserialize(prevCacheJson);\n }\n return pca;\n}\n\n/**\n * Start a device-code flow. The returned `result` promise resolves once the\n * user has entered the code and consented; callers should poll it (or await it)\n * via `complete_add_account`.\n */\nexport function beginDeviceCode(scopes: string[] = DEFAULT_SCOPES): DeviceCodeBegin {\n const pca = buildPca();\n let resolve!: (v: { tokens: SerializedTokens; account: AccountInfo }) => void;\n let reject!: (err: unknown) => void;\n const result = new Promise<{ tokens: SerializedTokens; account: AccountInfo }>(\n (res, rej) => {\n resolve = res;\n reject = rej;\n },\n );\n\n // We capture the deviceCodeCallback synchronously to surface the user-facing\n // info back out via this object before awaiting the long-running poll.\n let userCode = \"\";\n let verificationUri = \"\";\n let message = \"\";\n let expiresAt = new Date(Date.now() + 15 * 60_000).toISOString();\n let aborted = false;\n\n const ready = new Promise<void>((r) => {\n pca\n .acquireTokenByDeviceCode({\n scopes,\n deviceCodeCallback: (info) => {\n if (!info.userCode || !info.verificationUri) {\n // MSAL may fire the callback with an empty object if the\n // downstream HTTP request fails — reject so the caller\n // gets a clear error instead of silent empty strings.\n reject(\n new Error(\n \"Microsoft device-code endpoint returned no code. \" +\n \"Check MS_CLIENT_ID is a valid Azure Entra public-client application.\",\n ),\n );\n return;\n }\n userCode = info.userCode;\n verificationUri = info.verificationUri;\n message = info.message;\n if (info.expiresIn) {\n expiresAt = new Date(Date.now() + info.expiresIn * 1000).toISOString();\n }\n r();\n },\n })\n .then((authResult: AuthenticationResult | null) => {\n if (aborted) return;\n if (!authResult || !authResult.account) {\n reject(new Error(\"device-code flow returned no account\"));\n return;\n }\n const cache = pca.getTokenCache().serialize();\n const tokens: SerializedTokens = {\n msalCache: cache,\n homeAccountId: authResult.account.homeAccountId,\n tenantId: authResult.account.tenantId,\n username: authResult.account.username,\n scopes,\n };\n resolve({ tokens, account: authResult.account });\n })\n .catch((err) => {\n if (!aborted) reject(err);\n });\n });\n\n // Surface device-code info synchronously via a wrapper Promise:\n // we return the object but its strings are populated once `ready` settles.\n // To keep the API simple, we attach a getter that callers must `await` on.\n // Instead of getters, we wait for `ready` inside the begin helper:\n return {\n // these are placeholders until ready resolves\n get userCode() {\n return userCode;\n },\n get verificationUri() {\n return verificationUri;\n },\n get message() {\n return message;\n },\n get expiresAt() {\n return expiresAt;\n },\n result,\n cancel() {\n aborted = true;\n },\n // hidden helper for the caller to await initial code\n // (typed via a cast below where used)\n ...({ _ready: ready } as Record<string, unknown>),\n } as DeviceCodeBegin;\n}\n\n/** Await `_ready` so the user-code fields are populated. */\nexport async function awaitDeviceCodeReady(b: DeviceCodeBegin): Promise<void> {\n const r = (b as unknown as { _ready: Promise<void> })._ready;\n await r;\n}\n\n/**\n * Acquire a fresh access token for a stored account, refreshing silently from\n * the persisted MSAL cache. Returns the (possibly-updated) cache so the caller\n * can write it back to the store.\n */\nexport async function acquireAccessToken(\n tokens: SerializedTokens,\n scopes: string[] = DEFAULT_SCOPES,\n): Promise<{ accessToken: string; tokens: SerializedTokens }> {\n const pca = buildPca(tokens.msalCache);\n const cache = pca.getTokenCache();\n const account =\n (await cache.getAccountByHomeId(tokens.homeAccountId)) ??\n (await cache.getAllAccounts()).find((a) => a.username === tokens.username);\n if (!account) {\n throw new Error(\"no MSAL account in cache — re-run add_account\");\n }\n const res = await pca.acquireTokenSilent({ account, scopes });\n if (!res?.accessToken) {\n throw new Error(\"acquireTokenSilent returned no access token\");\n }\n const next: SerializedTokens = {\n ...tokens,\n msalCache: cache.serialize(),\n scopes,\n };\n return { accessToken: res.accessToken, tokens: next };\n}\n","import type { AccountRecord } from \"../../store/account-store.js\";\nimport type {\n AddAccountInput,\n AddAccountResult,\n AttachmentContent,\n CompleteAddAccountResult,\n EmailFull,\n EmailProvider,\n EmailSummary,\n ListEmailsOptions,\n SearchEmailsOptions,\n SendInput,\n} from \"../types.js\";\n\nconst NOT_IMPLEMENTED =\n \"IMAP provider is not yet implemented in v1. Tracked at \" +\n \"src/providers/imap/index.ts — see src/providers/types.ts for the contract.\";\n\n/**\n * Placeholder IMAP provider — registered so the contract is locked in and\n * `add_account` returns a clear \"coming soon\" error instead of \"unknown provider\".\n *\n * v2 plan: use `imapflow` + `nodemailer` (for send). Tokens shape will be\n * `{ host, port, secure, user, password|appPassword }` encrypted at rest by\n * the AccountStore, identical to how Outlook's MSAL cache is stored.\n */\nexport class ImapProvider implements EmailProvider {\n readonly id = \"imap\" as const;\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n async addAccount(_input: AddAccountInput): Promise<AddAccountResult> {\n throw new Error(NOT_IMPLEMENTED);\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n async completeAddAccount(_handle: string): Promise<CompleteAddAccountResult> {\n return { status: \"error\", error: NOT_IMPLEMENTED };\n }\n\n async listEmails(\n _account: AccountRecord,\n _opts: ListEmailsOptions,\n ): Promise<EmailSummary[]> {\n throw new Error(NOT_IMPLEMENTED);\n }\n\n async searchEmails(\n _account: AccountRecord,\n _query: string,\n _opts: SearchEmailsOptions,\n ): Promise<EmailSummary[]> {\n throw new Error(NOT_IMPLEMENTED);\n }\n\n async readEmail(_account: AccountRecord, _id: string): Promise<EmailFull> {\n throw new Error(NOT_IMPLEMENTED);\n }\n\n async readAttachment(\n _account: AccountRecord,\n _messageId: string,\n _attachmentId: string,\n ): Promise<AttachmentContent> {\n throw new Error(NOT_IMPLEMENTED);\n }\n\n async sendEmail(\n _account: AccountRecord,\n _msg: SendInput,\n ): Promise<{ id: string }> {\n throw new Error(NOT_IMPLEMENTED);\n }\n}\n","import type { AccountStore, AccountRecord } from \"../store/account-store.js\";\nimport type { EmailProvider, ProviderId } from \"./types.js\";\nimport { OutlookProvider } from \"./outlook/index.js\";\nimport { ImapProvider } from \"./imap/index.js\";\n\nexport interface Registry {\n get(id: ProviderId): EmailProvider;\n resolveByEmail(email: string): { provider: EmailProvider; account: AccountRecord };\n list(): EmailProvider[];\n}\n\nexport interface BuildRegistryOptions {\n store: AccountStore;\n}\n\nexport function buildRegistry(opts: BuildRegistryOptions): Registry {\n const providers = new Map<ProviderId, EmailProvider>();\n providers.set(\"outlook\", new OutlookProvider({ store: opts.store }));\n providers.set(\"imap\", new ImapProvider());\n // gmail can be added later — registry will return a clear error if asked.\n\n function get(id: ProviderId): EmailProvider {\n const p = providers.get(id);\n if (!p) throw new Error(`unknown provider: ${id}`);\n return p;\n }\n\n function resolveByEmail(email: string): {\n provider: EmailProvider;\n account: AccountRecord;\n } {\n const account = opts.store.getAccount(email);\n if (!account) {\n throw new Error(\n `no account registered for \"${email}\". Call add_account first.`,\n );\n }\n return { provider: get(account.provider), account };\n }\n\n return {\n get,\n resolveByEmail,\n list: () => Array.from(providers.values()),\n };\n}\n","import type { McpServer } from \"@modelcontextprotocol/sdk/server/mcp.js\";\nimport { z } from \"zod\";\n\nimport type { AccountStore } from \"../store/account-store.js\";\nimport type { Registry } from \"../providers/registry.js\";\nimport type { ProviderId } from \"../providers/types.js\";\nimport { selectBody } from \"../html-to-markdown.js\";\n\nexport interface RegisterToolsOptions {\n store: AccountStore;\n registry: Registry;\n readOnly?: boolean;\n}\n\n/** JSON-stringify a value into a single MCP text content block. */\nfunction ok(data: unknown) {\n return {\n content: [\n { type: \"text\" as const, text: JSON.stringify(data, null, 2) },\n ],\n };\n}\nfunction fail(message: string) {\n return {\n isError: true,\n content: [{ type: \"text\" as const, text: message }],\n };\n}\n\nconst emailAddrSchema = z.object({\n address: z.string().email(),\n name: z.string().optional(),\n});\n\nexport function registerTools(server: McpServer, opts: RegisterToolsOptions): void {\n const { store, registry, readOnly = false } = opts;\n\n // ---------- account management ----------\n\n server.registerTool(\n \"list_accounts\",\n {\n description:\n \"List all email accounts known to this server (no secrets). \" +\n \"Use the returned `email` value as the `account` argument to other tools.\",\n inputSchema: {},\n },\n async () => {\n const rows = store.listAccounts().map((a) => ({\n email: a.email,\n provider: a.provider,\n displayName: a.displayName,\n addedAt: a.addedAt,\n hasSignature: !!a.signature,\n hasStyle: !!(a.style && (a.style.fontFamily || a.style.fontSize || a.style.fontColor)),\n }));\n return ok({ accounts: rows });\n },\n );\n\n server.registerTool(\n \"add_account\",\n {\n description:\n \"Start adding an email account. For Outlook this returns a device code \" +\n \"the user must enter at the verification URL; then call `complete_add_account` \" +\n \"with the returned `handle` to finalize. Disabled in --read-only mode.\",\n inputSchema: {\n provider: z\n .enum([\"outlook\", \"imap\", \"gmail\"])\n .describe(\"Email backend. v1 only fully implements 'outlook'.\"),\n email: z\n .string()\n .email()\n .optional()\n .describe(\"Optional hint — the provider will verify it against the auth result.\"),\n config: z\n .record(z.unknown())\n .optional()\n .describe(\"Provider-specific config (e.g. IMAP host/port). Unused for Outlook.\"),\n },\n },\n async (args) => {\n if (readOnly) return fail(\"server is in --read-only mode; add_account is disabled\");\n const provider = registry.get(args.provider as ProviderId);\n try {\n const res = await provider.addAccount({ email: args.email, config: args.config });\n return ok(res);\n } catch (err) {\n return fail(errMsg(err));\n }\n },\n );\n\n server.registerTool(\n \"complete_add_account\",\n {\n description:\n \"Poll/finalize a pending add_account flow. Returns `pending` until the user \" +\n \"completes the device-code step, then `ready` with the persisted account.\",\n inputSchema: {\n provider: z.enum([\"outlook\", \"imap\", \"gmail\"]),\n handle: z.string().min(1),\n },\n },\n async (args) => {\n const provider = registry.get(args.provider as ProviderId);\n if (!provider.completeAddAccount) {\n return fail(`provider ${args.provider} has no async add-account flow`);\n }\n try {\n const res = await provider.completeAddAccount(args.handle);\n return ok(res);\n } catch (err) {\n return fail(errMsg(err));\n }\n },\n );\n\n // ---------- account settings ----------\n\n server.registerTool(\n \"get_account_settings\",\n {\n description:\n \"Get signature (HTML) and style preferences for an account.\",\n inputSchema: { account: z.string().email() },\n },\n async (args) => {\n try {\n const acct = store.getAccount(args.account);\n if (!acct) return fail(`no account registered for \"${args.account}\"`);\n return ok({ signature: acct.signature ?? null, style: acct.style ?? null });\n } catch (err) {\n return fail(errMsg(err));\n }\n },\n );\n\n server.registerTool(\n \"set_account_settings\",\n {\n description:\n \"Set signature (HTML snippet) and/or style preferences for an account. \" +\n \"Disabled in --read-only mode.\",\n inputSchema: {\n account: z.string().email(),\n signature: z\n .string()\n .optional()\n .describe(\"HTML snippet — may contain formatting, images, links. Pass null to clear.\"),\n style: z\n .object({\n fontFamily: z.string().optional(),\n fontSize: z.string().optional(),\n fontColor: z.string().optional(),\n })\n .optional()\n .describe(\"Font preferences applied to outgoing HTML emails. Pass null to clear.\"),\n },\n },\n async (args) => {\n if (readOnly) return fail(\"server is in --read-only mode; set_account_settings is disabled\");\n try {\n const acct = store.getAccount(args.account);\n if (!acct) return fail(`no account registered for \"${args.account}\"`);\n const updated = await store.upsertAccount({\n ...acct,\n signature: args.signature ?? acct.signature,\n style: args.style ?? acct.style,\n });\n return ok({ signature: updated.signature ?? null, style: updated.style ?? null });\n } catch (err) {\n return fail(errMsg(err));\n }\n },\n );\n\n server.registerTool(\n \"remove_account\",\n {\n description: \"Forget an account and delete its stored tokens. Disabled in --read-only mode.\",\n inputSchema: { email: z.string().email() },\n },\n async (args) => {\n if (readOnly) return fail(\"server is in --read-only mode; remove_account is disabled\");\n const removed = await store.removeAccount(args.email);\n return ok({ removed, email: args.email });\n },\n );\n\n // ---------- email ops ----------\n\n server.registerTool(\n \"list_emails\",\n {\n description:\n \"List recent emails in a folder of the given account. Pass the user's email \" +\n \"address as `account`; the server routes to the correct backend automatically.\",\n inputSchema: {\n account: z.string().email(),\n folder: z.string().default(\"inbox\").optional(),\n limit: z.number().int().positive().max(100).optional(),\n unreadOnly: z.boolean().optional(),\n },\n },\n async (args) => {\n try {\n const { provider, account } = registry.resolveByEmail(args.account);\n const items = await provider.listEmails(account, {\n folder: args.folder,\n limit: args.limit,\n unreadOnly: args.unreadOnly,\n });\n return ok({ account: account.email, count: items.length, items });\n } catch (err) {\n return fail(errMsg(err));\n }\n },\n );\n\n server.registerTool(\n \"search_emails\",\n {\n description:\n \"Search emails by free-text query (KQL on Outlook). Returns lightweight summaries.\",\n inputSchema: {\n account: z.string().email(),\n query: z.string().min(1),\n limit: z.number().int().positive().max(100).optional(),\n },\n },\n async (args) => {\n try {\n const { provider, account } = registry.resolveByEmail(args.account);\n const items = await provider.searchEmails(account, args.query, {\n limit: args.limit,\n });\n return ok({ account: account.email, count: items.length, items });\n } catch (err) {\n return fail(errMsg(err));\n }\n },\n );\n\n server.registerTool(\n \"read_email\",\n {\n description:\n \"Fetch a single email with full body and recipients by id. \" +\n \"Body is returned as `body` with `bodyFormat` indicating the format. \" +\n \"Default format is 'markdown' — HTML is automatically converted to save context tokens.\",\n inputSchema: {\n account: z.string().email(),\n id: z.string().min(1),\n format: z\n .enum([\"markdown\", \"html\", \"text\"])\n .default(\"markdown\")\n .optional()\n .describe(\n \"Output body format. 'markdown' converts HTML to Markdown (default), \" +\n \"'html' returns the raw HTML, 'text' returns plain text.\",\n ),\n },\n },\n async (args) => {\n try {\n const { provider, account } = registry.resolveByEmail(args.account);\n const msg = await provider.readEmail(account, args.id);\n const format = args.format ?? \"markdown\";\n const body = selectBody(msg, format);\n return ok({\n id: msg.id,\n subject: msg.subject,\n from: msg.from,\n to: msg.to,\n cc: msg.cc,\n bcc: msg.bcc,\n receivedAt: msg.receivedAt,\n preview: msg.preview,\n isRead: msg.isRead,\n hasAttachments: msg.hasAttachments,\n folder: msg.folder,\n attachments: msg.attachments,\n body,\n bodyFormat: format,\n });\n } catch (err) {\n return fail(errMsg(err));\n }\n },\n );\n\n server.registerTool(\n \"read_attachment\",\n {\n description:\n \"Download an email attachment to a temporary file and return its path. \" +\n \"Use messageId and attachmentId from a prior read_email call.\",\n inputSchema: {\n account: z.string().email(),\n messageId: z.string().min(1),\n attachmentId: z.string().min(1),\n },\n },\n async (args) => {\n try {\n const { provider, account } = registry.resolveByEmail(args.account);\n const res = await provider.readAttachment(account, args.messageId, args.attachmentId);\n return ok(res);\n } catch (err) {\n return fail(errMsg(err));\n }\n },\n );\n\n server.registerTool(\n \"send_email\",\n {\n description:\n \"Send an email from the given account. Automatically appends the \" +\n \"account's signature (HTML) and applies style preferences unless \" +\n \"`remove_signature` is true. Disabled in --read-only mode.\",\n inputSchema: {\n account: z.string().email(),\n to: z.array(emailAddrSchema).min(1),\n cc: z.array(emailAddrSchema).optional(),\n bcc: z.array(emailAddrSchema).optional(),\n subject: z.string(),\n body: z.string(),\n isHtml: z.boolean().optional(),\n remove_signature: z\n .boolean()\n .default(false)\n .optional()\n .describe(\"Skip appending the account signature. Style is still applied.\"),\n },\n },\n async (args) => {\n if (readOnly) return fail(\"server is in --read-only mode; send_email is disabled\");\n try {\n const { provider, account } = registry.resolveByEmail(args.account);\n const composed = composeBody({\n body: args.body,\n isHtml: args.isHtml,\n signature: account.signature,\n style: account.style,\n removeSignature: args.remove_signature,\n });\n const res = await provider.sendEmail(account, {\n to: args.to,\n cc: args.cc,\n bcc: args.bcc,\n subject: args.subject,\n body: composed.body,\n isHtml: composed.isHtml,\n });\n return ok({ sent: true, ...res });\n } catch (err) {\n return fail(errMsg(err));\n }\n },\n );\n}\n\n// ---------- body composition helpers ----------\n\ninterface ComposeBodyInput {\n body: string;\n isHtml?: boolean;\n signature?: string;\n style?: { fontFamily?: string; fontSize?: string; fontColor?: string };\n removeSignature?: boolean;\n}\n\nexport function composeBody(input: ComposeBodyInput): { body: string; isHtml: boolean } {\n const { body, isHtml = false, signature, style, removeSignature = false } = input;\n const hasSignature = !removeSignature && !!signature;\n const hasStyle = !!(style && (style.fontFamily || style.fontSize || style.fontColor));\n\n // Nothing to inject — pass through unchanged\n if (!hasSignature && !hasStyle) {\n return { body, isHtml };\n }\n\n // Need HTML for signature or style injection\n const styleAttr = hasStyle ? buildStyleAttr(style!) : \"\";\n\n if (isHtml) {\n let result = hasStyle ? `<div style=\"${styleAttr}\">${body}</div>` : body;\n if (hasSignature) result += `\\n<div class=\"signature\">${signature}</div>`;\n return { body: result, isHtml: true };\n }\n\n // Auto-upgrade plain text to HTML\n const escaped = escapeHtml(body);\n let result = `<div style=\"${styleAttr}\">${escaped}</div>`;\n if (hasSignature) result += `\\n<div class=\"signature\">${signature}</div>`;\n return { body: result, isHtml: true };\n}\n\nexport function buildStyleAttr(style: { fontFamily?: string; fontSize?: string; fontColor?: string }): string {\n const parts: string[] = [];\n if (style.fontFamily) parts.push(`font-family: ${style.fontFamily}`);\n if (style.fontSize) parts.push(`font-size: ${style.fontSize}`);\n if (style.fontColor) parts.push(`color: ${style.fontColor}`);\n return parts.join(\"; \");\n}\n\nexport function escapeHtml(text: string): string {\n return text\n .replace(/&/g, \"&\")\n .replace(/</g, \"<\")\n .replace(/>/g, \">\")\n .replace(/\"/g, \""\")\n .replace(/\\n/g, \"<br>\");\n}\n\nfunction errMsg(err: unknown): string {\n if (err instanceof Error) return err.message;\n return String(err);\n}\n","import TurndownService from \"turndown\";\n\nconst turndown = new TurndownService();\n\n/** Light wrapper — callers just pass HTML, get markdown back. */\nexport function htmlToMarkdown(html: string): string {\n return turndown.turndown(html);\n}\n\n/** Pick the right body for the requested format. */\nexport function selectBody(\n msg: { bodyHtml?: string; bodyText?: string },\n format: \"markdown\" | \"html\" | \"text\",\n): string {\n switch (format) {\n case \"markdown\": {\n if (msg.bodyHtml) return htmlToMarkdown(msg.bodyHtml);\n if (msg.bodyText) return msg.bodyText;\n return \"\";\n }\n case \"html\": {\n if (msg.bodyHtml) return msg.bodyHtml;\n if (msg.bodyText) return msg.bodyText;\n return \"\";\n }\n case \"text\": {\n if (msg.bodyText) return msg.bodyText;\n if (msg.bodyHtml) return msg.bodyHtml.replace(/<[^>]*>/g, \"\");\n return \"\";\n }\n }\n}\n","export const VERSION = \"0.1.0\";\n","import { startServer } from \"./server.js\";\n\ntype ParsedArgs = {\n http: boolean;\n port: number;\n host: string;\n dataDir?: string;\n readOnly: boolean;\n help: boolean;\n};\n\nfunction parseArgs(argv: string[]): ParsedArgs {\n const out: ParsedArgs = {\n http: false,\n port: 3000,\n host: \"127.0.0.1\",\n readOnly: false,\n help: false,\n };\n for (let i = 0; i < argv.length; i++) {\n const a = argv[i];\n switch (a) {\n case \"--http\":\n out.http = true;\n break;\n case \"--port\":\n out.port = Number(argv[++i] ?? \"3000\");\n break;\n case \"--host\":\n out.host = String(argv[++i] ?? \"127.0.0.1\");\n break;\n case \"--data-dir\":\n out.dataDir = String(argv[++i] ?? \"\");\n break;\n case \"--read-only\":\n out.readOnly = true;\n break;\n case \"-h\":\n case \"--help\":\n out.help = true;\n break;\n default:\n if (a && a.startsWith(\"--\")) {\n // ignore unknown flags rather than crashing — keeps the CLI forgiving\n // when embedded in MCP host configs.\n }\n }\n }\n return out;\n}\n\nfunction printHelp(): void {\n const msg = `hypermail-mcp — unified email MCP server\n\nUsage:\n hypermail-mcp [options]\n\nOptions:\n --http Run as Streamable HTTP server (default: stdio)\n --port <n> HTTP port (default: 3000)\n --host <addr> HTTP bind address (default: 127.0.0.1)\n --data-dir <path> Where to store the encrypted accounts file\n (default: $HYPERMAIL_MCP_DATA_DIR or ~/.hypermail-mcp)\n --read-only Disable tools that modify state (send_email, remove_account, add_account)\n -h, --help Show this help\n\nEnvironment:\n HYPERMAIL_MCP_DATA_DIR Same as --data-dir\n HYPERMAIL_MCP_KEY 32-byte key (base64 or hex) for at-rest encryption\n MS_CLIENT_ID Azure AD public client (application) ID\n MS_TENANT_ID Tenant (default: \"common\")\n`;\n process.stdout.write(msg);\n}\n\nasync function main(): Promise<void> {\n const opts = parseArgs(process.argv.slice(2));\n if (opts.help) {\n printHelp();\n return;\n }\n await startServer({\n http: opts.http,\n port: opts.port,\n host: opts.host,\n dataDir: opts.dataDir,\n readOnly: opts.readOnly,\n });\n}\n\nmain().catch((err) => {\n // eslint-disable-next-line no-console\n console.error(\"[hypermail-mcp] fatal:\", err);\n process.exit(1);\n});\n"],"mappings":";;;AAAA,SAAS,iBAAiB;AAC1B,SAAS,4BAA4B;AACrC,SAAS,qCAAqC;AAC9C,SAAS,cAAAA,mBAAkB;AAC3B,SAAS,gBAAgB,wBAAmE;;;ACJ5F;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,YAAY,UAAU;AAC/B,SAAS,eAAe;AACxB,OAAO,UAAU;AA8BjB,IAAM,YAAY;AAClB,IAAM,OAAO;AACb,IAAM,UAAU;AAET,IAAM,eAAN,MAAM,cAAa;AAAA,EAChB,YACW,UACA,KACT,MACR;AAHiB;AACA;AACT;AAAA,EACP;AAAA,EAHgB;AAAA,EACA;AAAA,EACT;AAAA,EAGV,aAAa,KAAK,OAAoB,CAAC,GAA0B;AAC/D,UAAM,UAAU,eAAe,KAAK,OAAO;AAC3C,UAAM,GAAG,MAAM,SAAS,EAAE,WAAW,MAAM,MAAM,IAAM,CAAC;AACxD,UAAM,WAAW,KAAK,KAAK,SAAS,SAAS;AAC7C,UAAM,MAAM,KAAK,OAAQ,MAAM,WAAW,OAAO;AAEjD,QAAI;AACJ,QAAI;AACF,YAAM,MAAM,MAAM,GAAG,SAAS,QAAQ;AACtC,aAAO,QAAQ,KAAK,GAAG;AAAA,IACzB,SAAS,KAAc;AACrB,UAAK,IAA8B,SAAS,UAAU;AACpD,eAAO,EAAE,SAAS,GAAG,UAAU,CAAC,EAAE;AAAA,MACpC,OAAO;AACL,cAAM;AAAA,MACR;AAAA,IACF;AACA,WAAO,IAAI,cAAa,UAAU,KAAK,IAAI;AAAA,EAC7C;AAAA,EAEA,eAAgC;AAG9B,WAAO,KAAK,KAAK,SAAS,IAAI,CAAC,OAAO,EAAE,GAAG,EAAE,EAAE;AAAA,EACjD;AAAA,EAEA,WAAW,OAA0C;AACnD,UAAM,OAAO,MAAM,KAAK,EAAE,YAAY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UAAU,KAAK;AAAA,EACjB,CAAC;AACH;AAEA,KAAK,EAAE,MAAM,CAAC,QAAQ;AAEpB,UAAQ,MAAM,0BAA0B,GAAG;AAC3C,UAAQ,KAAK,CAAC;AAChB,CAAC;","names":["randomUUID","result","randomUUID"]}
|
|
1
|
+
{"version":3,"sources":["../src/server.ts","../src/store/account-store.ts","../src/providers/outlook/index.ts","../src/providers/outlook/client.ts","../src/providers/outlook/auth.ts","../src/providers/imap/index.ts","../src/providers/registry.ts","../src/tools/index.ts","../src/html-to-markdown.ts","../src/version.ts","../src/cli.ts"],"sourcesContent":["import { McpServer } from \"@modelcontextprotocol/sdk/server/mcp.js\";\nimport { StdioServerTransport } from \"@modelcontextprotocol/sdk/server/stdio.js\";\nimport { StreamableHTTPServerTransport } from \"@modelcontextprotocol/sdk/server/streamableHttp.js\";\nimport { randomUUID } from \"node:crypto\";\nimport { createServer as createHttpServer, type IncomingMessage, type ServerResponse } from \"node:http\";\n\nimport { AccountStore } from \"./store/account-store.js\";\nimport { buildRegistry } from \"./providers/registry.js\";\nimport { registerTools } from \"./tools/index.js\";\nimport { VERSION } from \"./version.js\";\n\nexport interface ServerOptions {\n http?: boolean;\n port?: number;\n host?: string;\n dataDir?: string;\n readOnly?: boolean;\n /** When true, hide send_email and only expose draft_email. */\n draftOnly?: boolean;\n}\n\nexport async function startServer(opts: ServerOptions = {}): Promise<void> {\n const store = await AccountStore.open({ dataDir: opts.dataDir });\n const registry = buildRegistry({ store });\n\n const server = new McpServer(\n { name: \"hypermail-mcp\", version: VERSION },\n { capabilities: { tools: {}, logging: {} } },\n );\n\n registerTools(server, { store, registry, readOnly: !!opts.readOnly, draftOnly: !!opts.draftOnly });\n\n if (opts.http) {\n await startHttp(server, opts.host ?? \"127.0.0.1\", opts.port ?? 3000);\n } else {\n const transport = new StdioServerTransport();\n await server.connect(transport);\n }\n}\n\nasync function startHttp(server: McpServer, host: string, port: number): Promise<void> {\n // One transport per session, keyed by Mcp-Session-Id header.\n const sessions = new Map<string, StreamableHTTPServerTransport>();\n\n const http = createHttpServer(async (req: IncomingMessage, res: ServerResponse) => {\n try {\n if (!req.url || !req.url.startsWith(\"/mcp\")) {\n res.statusCode = 404;\n res.end(\"not found\");\n return;\n }\n const sessionId = (req.headers[\"mcp-session-id\"] as string | undefined) ?? undefined;\n let transport = sessionId ? sessions.get(sessionId) : undefined;\n\n if (!transport) {\n transport = new StreamableHTTPServerTransport({\n sessionIdGenerator: () => randomUUID(),\n onsessioninitialized: (sid: string) => {\n sessions.set(sid, transport!);\n },\n });\n transport.onclose = () => {\n if (transport!.sessionId) sessions.delete(transport!.sessionId);\n };\n await server.connect(transport);\n }\n\n // Buffer body for POST / DELETE\n let body: unknown = undefined;\n if (req.method === \"POST\" || req.method === \"DELETE\") {\n const chunks: Buffer[] = [];\n for await (const chunk of req) chunks.push(chunk as Buffer);\n const raw = Buffer.concat(chunks).toString(\"utf8\");\n body = raw ? JSON.parse(raw) : undefined;\n }\n await transport.handleRequest(req, res, body);\n } catch (err) {\n // eslint-disable-next-line no-console\n console.error(\"[hypermail-mcp] http error:\", err);\n if (!res.headersSent) {\n res.statusCode = 500;\n res.end(\"internal error\");\n }\n }\n });\n\n await new Promise<void>((resolve) => http.listen(port, host, resolve));\n // eslint-disable-next-line no-console\n console.error(`[hypermail-mcp] listening on http://${host}:${port}/mcp`);\n}\n","import {\n createCipheriv,\n createDecipheriv,\n randomBytes,\n createHash,\n} from \"node:crypto\";\nimport { promises as fs } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport path from \"node:path\";\n\n/**\n * One stored account. `tokens` is provider-specific (e.g. serialized MSAL cache\n * for Outlook, host/port/password blob for IMAP) — the store is opaque to it.\n */\nexport interface AccountRecord {\n email: string;\n provider: \"outlook\" | \"imap\" | \"gmail\";\n displayName?: string;\n tokens: Record<string, unknown>;\n addedAt: string;\n /** HTML snippet — may contain formatting, images, links. Injected at end of outgoing emails. */\n signature?: string;\n /** Font/style preferences applied to outgoing HTML emails. */\n style?: { fontFamily?: string; fontSize?: string; fontColor?: string };\n}\n\ninterface StoreFile {\n version: 1;\n accounts: AccountRecord[];\n}\n\nexport interface OpenOptions {\n dataDir?: string;\n /** Inject the encryption key directly (mostly for tests). Otherwise resolved\n * from `HYPERMAIL_MCP_KEY` env, then OS keychain, then auto-generated. */\n key?: Buffer;\n}\n\nconst FILE_NAME = \"accounts.json.enc\";\nconst ALGO = \"aes-256-gcm\";\nconst KEY_LEN = 32;\n\nexport class AccountStore {\n private constructor(\n private readonly filePath: string,\n private readonly key: Buffer,\n private data: StoreFile,\n ) {}\n\n static async open(opts: OpenOptions = {}): Promise<AccountStore> {\n const dataDir = resolveDataDir(opts.dataDir);\n await fs.mkdir(dataDir, { recursive: true, mode: 0o700 });\n const filePath = path.join(dataDir, FILE_NAME);\n const key = opts.key ?? (await resolveKey(dataDir));\n\n let data: StoreFile;\n try {\n const buf = await fs.readFile(filePath);\n data = decrypt(buf, key);\n } catch (err: unknown) {\n if ((err as NodeJS.ErrnoException).code === \"ENOENT\") {\n data = { version: 1, accounts: [] };\n } else {\n throw err;\n }\n }\n return new AccountStore(filePath, key, data);\n }\n\n listAccounts(): AccountRecord[] {\n // Return shallow clones without tokens leaking out unintentionally is the\n // caller's job; here we expose the full record for provider use.\n return this.data.accounts.map((a) => ({ ...a }));\n }\n\n getAccount(email: string): AccountRecord | undefined {\n const norm = email.trim().toLowerCase();\n const rec = this.data.accounts.find((a) => a.email.toLowerCase() === norm);\n return rec ? { ...rec } : undefined;\n }\n\n async upsertAccount(rec: AccountRecord): Promise<AccountRecord> {\n const norm = rec.email.trim().toLowerCase();\n const next: AccountRecord = { ...rec, email: norm };\n const idx = this.data.accounts.findIndex((a) => a.email.toLowerCase() === norm);\n if (idx >= 0) this.data.accounts[idx] = next;\n else this.data.accounts.push(next);\n await this.flush();\n return { ...next };\n }\n\n async removeAccount(email: string): Promise<boolean> {\n const norm = email.trim().toLowerCase();\n const before = this.data.accounts.length;\n this.data.accounts = this.data.accounts.filter((a) => a.email.toLowerCase() !== norm);\n if (this.data.accounts.length === before) return false;\n await this.flush();\n return true;\n }\n\n private async flush(): Promise<void> {\n const buf = encrypt(this.data, this.key);\n const tmp = `${this.filePath}.${process.pid}.${Date.now()}.tmp`;\n await fs.writeFile(tmp, buf, { mode: 0o600 });\n await fs.rename(tmp, this.filePath);\n }\n}\n\n// ---------- encryption helpers ----------\n\nfunction encrypt(data: StoreFile, key: Buffer): Buffer {\n const iv = randomBytes(12);\n const cipher = createCipheriv(ALGO, key, iv);\n const plaintext = Buffer.from(JSON.stringify(data), \"utf8\");\n const ct = Buffer.concat([cipher.update(plaintext), cipher.final()]);\n const tag = cipher.getAuthTag();\n // layout: [1-byte version=1][12 iv][16 tag][ct...]\n return Buffer.concat([Buffer.from([1]), iv, tag, ct]);\n}\n\nfunction decrypt(buf: Buffer, key: Buffer): StoreFile {\n if (buf.length < 1 + 12 + 16 + 1) throw new Error(\"accounts file truncated\");\n const v = buf[0];\n if (v !== 1) throw new Error(`unsupported accounts file version: ${v}`);\n const iv = buf.subarray(1, 13);\n const tag = buf.subarray(13, 29);\n const ct = buf.subarray(29);\n const decipher = createDecipheriv(ALGO, key, iv);\n decipher.setAuthTag(tag);\n const pt = Buffer.concat([decipher.update(ct), decipher.final()]);\n const parsed = JSON.parse(pt.toString(\"utf8\")) as StoreFile;\n if (parsed.version !== 1 || !Array.isArray(parsed.accounts)) {\n throw new Error(\"accounts file is malformed\");\n }\n return parsed;\n}\n\n// ---------- key + path resolution ----------\n\nfunction resolveDataDir(explicit?: string): string {\n if (explicit && explicit.length > 0) return path.resolve(explicit);\n const env = process.env.HYPERMAIL_MCP_DATA_DIR;\n if (env && env.length > 0) return path.resolve(env);\n return path.join(homedir(), \".hypermail-mcp\");\n}\n\nfunction parseEnvKey(raw: string): Buffer | undefined {\n const s = raw.trim();\n // hex (64 chars)\n if (/^[0-9a-fA-F]{64}$/.test(s)) return Buffer.from(s, \"hex\");\n // base64 — accept any length, then check\n try {\n const buf = Buffer.from(s, \"base64\");\n if (buf.length === KEY_LEN) return buf;\n } catch {\n /* ignore */\n }\n // last-resort: derive 32 bytes via SHA-256 over the raw string. This lets\n // users pass any passphrase; not ideal but predictable.\n return createHash(\"sha256\").update(s, \"utf8\").digest();\n}\n\nasync function resolveKey(dataDir: string): Promise<Buffer> {\n const env = process.env.HYPERMAIL_MCP_KEY;\n if (env && env.length > 0) {\n const k = parseEnvKey(env);\n if (k) return k;\n }\n\n // Try OS keychain via keytar (optional dep).\n const fromKeytar = await tryKeytarGet();\n if (fromKeytar) return fromKeytar;\n\n // Local-dev fallback: persist a generated key to a 0600 file next to the\n // accounts blob so subsequent runs can decrypt. Hosted deployments should\n // always set HYPERMAIL_MCP_KEY explicitly.\n const keyFile = path.join(dataDir, \"master.key\");\n try {\n const existing = await fs.readFile(keyFile);\n if (existing.length === KEY_LEN) return existing;\n } catch {\n /* fall through to generate */\n }\n const gen = randomBytes(KEY_LEN);\n await fs.writeFile(keyFile, gen, { mode: 0o600 });\n await tryKeytarSet(gen);\n return gen;\n}\n\nasync function tryKeytarGet(): Promise<Buffer | undefined> {\n try {\n const mod = (await import(\"keytar\")) as typeof import(\"keytar\");\n const val = await mod.getPassword(\"hypermail-mcp\", \"master\");\n if (val) {\n const buf = Buffer.from(val, \"base64\");\n if (buf.length === KEY_LEN) return buf;\n }\n } catch {\n /* keytar not installed or unsupported platform */\n }\n return undefined;\n}\n\nasync function tryKeytarSet(key: Buffer): Promise<void> {\n try {\n const mod = (await import(\"keytar\")) as typeof import(\"keytar\");\n await mod.setPassword(\"hypermail-mcp\", \"master\", key.toString(\"base64\"));\n } catch {\n /* ignore */\n }\n}\n","import { randomUUID } from \"node:crypto\";\nimport { writeFileSync } from \"node:fs\";\nimport { tmpdir } from \"node:os\";\nimport { join as pathJoin } from \"node:path\";\n\nimport { ResponseType, type Client } from \"@microsoft/microsoft-graph-client\";\n\nimport type { AccountRecord, AccountStore } from \"../../store/account-store.js\";\nimport type {\n AddAccountInput,\n AddAccountResult,\n AttachmentContent,\n CompleteAddAccountResult,\n EmailFull,\n EmailProvider,\n EmailSummary,\n ListEmailsOptions,\n SearchEmailsOptions,\n SendInput,\n EmailAddress,\n} from \"../types.js\";\nimport { OutlookClientFactory } from \"./client.js\";\nimport {\n awaitDeviceCodeReady,\n beginDeviceCode,\n type DeviceCodeBegin,\n type SerializedTokens,\n} from \"./auth.js\";\n\n// ---------- inline image conversion ----------\n\ninterface InlineAttachment {\n \"@odata.type\": string;\n name: string;\n contentType: string;\n contentId: string;\n contentBytes: string;\n isInline: boolean;\n}\n\n/**\n * Scans HTML for data:image/...;base64,... URIs, extracts the raw base64\n * data, assigns unique contentIds, and returns the transformed body\n * (with src=\"cid:...\" references) plus an array of inline fileAttachment\n * objects ready for the Graph API.\n *\n * Pass-through when there are no matches — returns the original body with\n * an empty attachments array.\n */\nexport function convertInlineImages(body: string): {\n body: string;\n attachments: InlineAttachment[];\n} {\n const attachments: InlineAttachment[] = [];\n // Match src=\"data:image/<subtype>;base64,<payload>\"\n // Supports png, jpg, jpeg, gif, svg+xml, webp, bmp, etc.\n const re = /src=\"data:image\\/([\\w+]+);base64,([^\"]+)\"/gi;\n\n const transformed = body.replace(re, (_fullMatch, mimeSubtype, b64) => {\n const contentId = `sig-img-${randomUUID()}`;\n const ext = mimeSubtype.toLowerCase().replace(/\\+/g, \"-\") === \"svg-xml\"\n ? \"svg\"\n : mimeSubtype.toLowerCase().replace(/\\+/g, \"-\");\n attachments.push({\n \"@odata.type\": \"#microsoft.graph.fileAttachment\",\n name: `signature-image.${ext}`,\n contentType: `image/${mimeSubtype}`,\n contentId,\n contentBytes: b64,\n isInline: true,\n });\n return `src=\"cid:${contentId}\"`;\n });\n\n return { body: transformed, attachments };\n}\n\ninterface PendingFlow {\n begin: DeviceCodeBegin;\n emailHint?: string;\n startedAt: number;\n settled: \"pending\" | \"ready\" | \"error\" | \"expired\";\n account?: AccountRecord;\n error?: string;\n}\n\nexport interface OutlookProviderOptions {\n store: AccountStore;\n}\n\nexport class OutlookProvider implements EmailProvider {\n readonly id = \"outlook\" as const;\n private readonly clients: OutlookClientFactory;\n private readonly pending = new Map<string, PendingFlow>();\n\n constructor(private readonly opts: OutlookProviderOptions) {\n this.clients = new OutlookClientFactory(opts.store);\n }\n\n // ---------- account lifecycle ----------\n\n async addAccount(input: AddAccountInput): Promise<AddAccountResult> {\n const begin = beginDeviceCode();\n await awaitDeviceCodeReady(begin);\n\n const handle = randomUUID();\n const flow: PendingFlow = {\n begin,\n emailHint: input.email,\n startedAt: Date.now(),\n settled: \"pending\",\n };\n this.pending.set(handle, flow);\n\n // Fire-and-forget: when the user finishes, persist and update flow.\n begin.result\n .then(async ({ tokens, account }) => {\n const email = (account.username || input.email || \"\").toLowerCase();\n if (!email) {\n flow.settled = \"error\";\n flow.error = \"no email returned from Microsoft account\";\n return;\n }\n const rec: AccountRecord = {\n email,\n provider: \"outlook\",\n displayName: account.name ?? undefined,\n tokens: tokens as unknown as Record<string, unknown>,\n addedAt: new Date().toISOString(),\n };\n const saved = await this.opts.store.upsertAccount(rec);\n flow.account = saved;\n flow.settled = \"ready\";\n })\n .catch((err: unknown) => {\n flow.settled = \"error\";\n flow.error = err instanceof Error ? err.message : String(err);\n });\n\n return {\n status: \"pending\",\n handle,\n verification: {\n userCode: begin.userCode,\n verificationUri: begin.verificationUri,\n expiresAt: begin.expiresAt,\n message: begin.message,\n },\n };\n }\n\n async completeAddAccount(handle: string): Promise<CompleteAddAccountResult> {\n const flow = this.pending.get(handle);\n if (!flow) return { status: \"error\", error: \"unknown handle\" };\n // expire after 20 minutes regardless\n if (Date.now() - flow.startedAt > 20 * 60_000 && flow.settled === \"pending\") {\n flow.settled = \"expired\";\n flow.begin.cancel();\n }\n if (flow.settled === \"ready\" && flow.account) {\n this.pending.delete(handle);\n return { status: \"ready\", account: flow.account };\n }\n if (flow.settled === \"error\") {\n this.pending.delete(handle);\n return { status: \"error\", error: flow.error ?? \"unknown error\" };\n }\n if (flow.settled === \"expired\") {\n this.pending.delete(handle);\n return { status: \"expired\" };\n }\n return { status: \"pending\" };\n }\n\n // ---------- email ops ----------\n\n async listEmails(\n account: AccountRecord,\n opts: ListEmailsOptions,\n ): Promise<EmailSummary[]> {\n const client = this.clients.get(account);\n const limit = clampLimit(opts.limit, 25, 100);\n const folder = opts.folder ?? \"inbox\";\n const filterParts: string[] = [];\n if (opts.unreadOnly) filterParts.push(\"isRead eq false\");\n\n let req = client\n .api(`/me/mailFolders/${encodeURIComponent(folder)}/messages`)\n .top(limit)\n .select([\n \"id\",\n \"subject\",\n \"from\",\n \"toRecipients\",\n \"receivedDateTime\",\n \"bodyPreview\",\n \"isRead\",\n \"hasAttachments\",\n ].join(\",\"))\n .orderby(\"receivedDateTime DESC\");\n\n if (filterParts.length > 0) req = req.filter(filterParts.join(\" and \"));\n\n const res = (await req.get()) as { value: GraphMessage[] };\n return res.value.map((m) => mapSummary(m, folder));\n }\n\n async searchEmails(\n account: AccountRecord,\n query: string,\n opts: SearchEmailsOptions,\n ): Promise<EmailSummary[]> {\n const client = this.clients.get(account);\n const limit = clampLimit(opts.limit, 25, 100);\n // $search requires the ConsistencyLevel: eventual header\n const res = (await client\n .api(\"/me/messages\")\n .header(\"ConsistencyLevel\", \"eventual\")\n .top(limit)\n .search(`\"${query.replace(/\"/g, '\\\\\"')}\"`)\n .select(\n [\n \"id\",\n \"subject\",\n \"from\",\n \"toRecipients\",\n \"receivedDateTime\",\n \"bodyPreview\",\n \"isRead\",\n \"hasAttachments\",\n ].join(\",\"),\n )\n .get()) as { value: GraphMessage[] };\n return res.value.map((m) => mapSummary(m));\n }\n\n async readEmail(account: AccountRecord, id: string): Promise<EmailFull> {\n const client = this.clients.get(account);\n const m = (await client\n .api(`/me/messages/${encodeURIComponent(id)}`)\n .select(\n [\n \"id\",\n \"subject\",\n \"from\",\n \"toRecipients\",\n \"ccRecipients\",\n \"bccRecipients\",\n \"receivedDateTime\",\n \"bodyPreview\",\n \"isRead\",\n \"hasAttachments\",\n \"body\",\n ].join(\",\"),\n )\n .get()) as GraphMessage;\n\n let attachments: EmailFull[\"attachments\"] = undefined;\n if (m.hasAttachments) {\n try {\n const attRes = (await client\n .api(`/me/messages/${encodeURIComponent(id)}/attachments`)\n .select(\"id,name,contentType,size\")\n .get()) as { value: GraphAttachment[] };\n attachments = attRes.value.map((a) => ({\n id: a.id,\n name: a.name,\n contentType: a.contentType,\n size: a.size,\n }));\n } catch {\n /* ignore attachment listing failure */\n }\n }\n\n const summary = mapSummary(m);\n const body = m.body;\n return {\n ...summary,\n cc: (m.ccRecipients ?? []).map(mapRecipient),\n bcc: (m.bccRecipients ?? []).map(mapRecipient),\n bodyText: body?.contentType === \"text\" ? body.content : undefined,\n bodyHtml: body?.contentType === \"html\" ? body.content : undefined,\n attachments,\n };\n }\n\n async readAttachment(\n account: AccountRecord,\n messageId: string,\n attachmentId: string,\n ): Promise<AttachmentContent> {\n const client = this.clients.get(account);\n // First, get the attachment metadata to know the filename\n const att = (await client\n .api(`/me/messages/${encodeURIComponent(messageId)}/attachments/${encodeURIComponent(attachmentId)}`)\n .select(\"name,contentType\")\n .get()) as { name: string; contentType?: string };\n\n // Download the raw content as ArrayBuffer\n const data = (await client\n .api(`/me/messages/${encodeURIComponent(messageId)}/attachments/${encodeURIComponent(attachmentId)}/$value`)\n .responseType(ResponseType.ARRAYBUFFER)\n .get()) as ArrayBuffer;\n\n // Write to temp file with original name\n const outPath = pathJoin(tmpdir(), att.name);\n writeFileSync(outPath, Buffer.from(data));\n\n return {\n name: att.name,\n contentType: att.contentType,\n path: outPath,\n };\n }\n\n // Shared helper — creates a draft from a reference message (forward or\n // reply), prepends our composed body before the existing content, and\n // attaches inline images. Returns the draft message ID.\n private async buildDraftFromReference(\n client: Client,\n createEndpoint: string,\n createPayload: Record<string, unknown>,\n converted: { body: string; attachments: InlineAttachment[] },\n ): Promise<string> {\n const draft: { id: string } = await client\n .api(createEndpoint)\n .post(createPayload);\n\n const draftMsg: { body?: { content?: string; contentType?: string } } =\n await client.api(`/me/messages/${draft.id}`).select(\"body\").get();\n\n const draftBody = draftMsg.body?.content ?? \"\";\n const draftContentType = draftMsg.body?.contentType ?? \"HTML\";\n const spacer = '<div style=\"line-height:12px\"><br></div>';\n const prepend = converted.body + spacer;\n const finalBody = draftBody.includes(\"<body\")\n ? draftBody.replace(/(<body[^>]*>)/i, `$1${prepend}`)\n : prepend + draftBody;\n\n await client.api(`/me/messages/${draft.id}`).patch({\n body: { contentType: draftContentType, content: finalBody },\n });\n\n for (const att of converted.attachments) {\n await client.api(`/me/messages/${draft.id}/attachments`).post(att);\n }\n\n return draft.id;\n }\n\n async sendEmail(\n account: AccountRecord,\n msg: SendInput,\n ): Promise<{ id: string }> {\n const client = this.clients.get(account);\n\n if (msg.inReplyTo && msg.forwardMessageId) {\n throw new Error(\n \"inReplyTo and forwardMessageId are mutually exclusive — use one or the other\",\n );\n }\n\n // Convert data:image URIs to cid: references + inline attachments.\n const converted = convertInlineImages(msg.body);\n\n if (msg.forwardMessageId) {\n const draftId = await this.buildDraftFromReference(\n client,\n `/me/messages/${encodeURIComponent(msg.forwardMessageId)}/createForward`,\n {\n message: {\n toRecipients: msg.to.map(toRecipient),\n ccRecipients: (msg.cc ?? []).map(toRecipient),\n bccRecipients: (msg.bcc ?? []).map(toRecipient),\n },\n comment: \"\",\n },\n converted,\n );\n await client.api(`/me/messages/${draftId}/send`).post({});\n return { id: draftId };\n }\n\n if (msg.inReplyTo) {\n const createEndpoint = msg.replyAll\n ? `/me/messages/${encodeURIComponent(msg.inReplyTo)}/createReplyAll`\n : `/me/messages/${encodeURIComponent(msg.inReplyTo)}/createReply`;\n const draftId = await this.buildDraftFromReference(\n client,\n createEndpoint,\n {},\n converted,\n );\n await client.api(`/me/messages/${draftId}/send`).post({});\n return { id: draftId };\n }\n\n // New email — use sendMail with inline attachments\n const payload: Record<string, unknown> = {\n message: {\n subject: msg.subject,\n body: {\n contentType: msg.isHtml ? \"HTML\" : \"Text\",\n content: converted.body,\n },\n toRecipients: msg.to.map(toRecipient),\n ccRecipients: (msg.cc ?? []).map(toRecipient),\n bccRecipients: (msg.bcc ?? []).map(toRecipient),\n },\n saveToSentItems: true,\n };\n if (converted.attachments.length > 0) {\n (payload.message as Record<string, unknown>).attachments =\n converted.attachments;\n }\n await client.api(\"/me/sendMail\").post(payload);\n // Graph's sendMail returns 202 with no body; we don't have an id back.\n return { id: \"\" };\n }\n\n async saveDraft(\n account: AccountRecord,\n msg: SendInput,\n ): Promise<{ id: string }> {\n const client = this.clients.get(account);\n\n if (msg.inReplyTo && msg.forwardMessageId) {\n throw new Error(\n \"inReplyTo and forwardMessageId are mutually exclusive — use one or the other\",\n );\n }\n\n const converted = convertInlineImages(msg.body);\n\n // Forward/reply drafts: same flow as sendEmail, but skip the final send.\n if (msg.forwardMessageId) {\n const draftId = await this.buildDraftFromReference(\n client,\n `/me/messages/${encodeURIComponent(msg.forwardMessageId)}/createForward`,\n {\n message: {\n toRecipients: msg.to.map(toRecipient),\n ccRecipients: (msg.cc ?? []).map(toRecipient),\n bccRecipients: (msg.bcc ?? []).map(toRecipient),\n },\n comment: \"\",\n },\n converted,\n );\n return { id: draftId };\n }\n\n if (msg.inReplyTo) {\n const createEndpoint = msg.replyAll\n ? `/me/messages/${encodeURIComponent(msg.inReplyTo)}/createReplyAll`\n : `/me/messages/${encodeURIComponent(msg.inReplyTo)}/createReply`;\n const draftId = await this.buildDraftFromReference(\n client,\n createEndpoint,\n {},\n converted,\n );\n return { id: draftId };\n }\n\n // New email — create a draft message directly (POST /me/messages).\n // Graph creates drafts by default when posting to /me/messages.\n const draftPayload: Record<string, unknown> = {\n subject: msg.subject,\n body: {\n contentType: msg.isHtml ? \"HTML\" : \"Text\",\n content: converted.body,\n },\n toRecipients: msg.to.map(toRecipient),\n ccRecipients: (msg.cc ?? []).map(toRecipient),\n bccRecipients: (msg.bcc ?? []).map(toRecipient),\n };\n if (converted.attachments.length > 0) {\n draftPayload.attachments = converted.attachments;\n }\n const draft: { id: string } = await client\n .api(\"/me/messages\")\n .post(draftPayload);\n return { id: draft.id };\n }\n\n async moveEmail(\n account: AccountRecord,\n id: string,\n destinationId: string,\n ): Promise<void> {\n const client = this.clients.get(account);\n await client\n .api(`/me/messages/${encodeURIComponent(id)}/move`)\n .post({ destinationId });\n }\n}\n\n// ---------- mapping helpers ----------\n\ninterface GraphRecipient {\n emailAddress?: { name?: string; address?: string };\n}\ninterface GraphMessage {\n id: string;\n subject?: string;\n from?: GraphRecipient;\n toRecipients?: GraphRecipient[];\n ccRecipients?: GraphRecipient[];\n bccRecipients?: GraphRecipient[];\n receivedDateTime?: string;\n bodyPreview?: string;\n isRead?: boolean;\n hasAttachments?: boolean;\n body?: { contentType?: \"text\" | \"html\"; content?: string };\n}\ninterface GraphAttachment {\n id: string;\n name: string;\n contentType?: string;\n size?: number;\n}\n\nfunction mapRecipient(r: GraphRecipient): EmailAddress {\n return {\n name: r.emailAddress?.name,\n address: r.emailAddress?.address ?? \"\",\n };\n}\n\nfunction mapSummary(m: GraphMessage, folder?: string): EmailSummary {\n return {\n id: m.id,\n subject: m.subject ?? \"\",\n from: m.from ? mapRecipient(m.from) : undefined,\n to: (m.toRecipients ?? []).map(mapRecipient),\n receivedAt: m.receivedDateTime,\n preview: m.bodyPreview,\n isRead: m.isRead,\n hasAttachments: m.hasAttachments,\n folder,\n };\n}\n\nfunction toRecipient(a: EmailAddress): GraphRecipient {\n return { emailAddress: { name: a.name, address: a.address } };\n}\n\nfunction clampLimit(v: number | undefined, dflt: number, max: number): number {\n if (!v || v <= 0) return dflt;\n return Math.min(v, max);\n}\n","import \"isomorphic-fetch\";\nimport {\n Client,\n type AuthenticationProvider,\n} from \"@microsoft/microsoft-graph-client\";\n\nimport type { AccountStore, AccountRecord } from \"../../store/account-store.js\";\nimport { acquireAccessToken, type SerializedTokens } from \"./auth.js\";\n\n/**\n * Builds a Graph `Client` bound to a stored account. The client uses an\n * `AuthenticationProvider` that calls msal silently on every request, and\n * writes the (possibly-refreshed) cache back into the AccountStore.\n *\n * Clients are cached per email since the underlying SDK reuses connections.\n */\nexport class OutlookClientFactory {\n private readonly cache = new Map<string, Client>();\n\n constructor(private readonly store: AccountStore) {}\n\n get(account: AccountRecord): Client {\n const key = account.email.toLowerCase();\n const existing = this.cache.get(key);\n if (existing) return existing;\n\n const store = this.store;\n const provider: AuthenticationProvider = {\n getAccessToken: async () => {\n const fresh = store.getAccount(account.email) ?? account;\n const tokens = fresh.tokens as unknown as SerializedTokens;\n const { accessToken, tokens: nextTokens } = await acquireAccessToken(tokens);\n // Persist refreshed cache opportunistically; failures here shouldn't\n // break the in-flight Graph call.\n if (nextTokens.msalCache !== tokens.msalCache) {\n store\n .upsertAccount({\n ...fresh,\n tokens: nextTokens as unknown as Record<string, unknown>,\n })\n .catch(() => {\n /* swallow — next call will refresh again */\n });\n }\n return accessToken;\n },\n };\n\n const client = Client.initWithMiddleware({ authProvider: provider });\n this.cache.set(key, client);\n return client;\n }\n\n /** Drop a cached client (e.g. after removeAccount). */\n invalidate(email: string): void {\n this.cache.delete(email.toLowerCase());\n }\n}\n","import {\n PublicClientApplication,\n type Configuration,\n type AuthenticationResult,\n type AccountInfo,\n} from \"@azure/msal-node\";\n\n/**\n * Public client app id. Default is the well-known `ms-365` client id used by\n * the softeria/ms-365-mcp-server project — it's a public client registered\n * for personal MSA + multi-tenant work/school. Users can override via\n * MS_CLIENT_ID for their own Entra app registrations.\n */\nconst DEFAULT_CLIENT_ID = \"084a3e9f-a9f4-43f7-89f9-d229cf97853e\";\n// ^ Pre-registered public client app (same one used by softeria/ms-365-mcp-server).\n// Supports personal MSA + multi-tenant work/school via device-code flow.\n// Operators should set MS_CLIENT_ID to a client they control for production.\n\nconst DEFAULT_SCOPES = [\n \"offline_access\",\n \"User.Read\",\n \"Mail.ReadWrite\",\n \"Mail.Send\",\n];\n\nexport interface DeviceCodeBegin {\n userCode: string;\n verificationUri: string;\n message: string;\n expiresAt: string; // ISO\n /** Resolves with the auth result once the user completes the flow. */\n result: Promise<{ tokens: SerializedTokens; account: AccountInfo }>;\n /** Aborts the in-flight polling promise. */\n cancel(): void;\n}\n\nexport interface SerializedTokens {\n /** MSAL cache JSON, encrypted at rest by the account store. */\n msalCache: string;\n /** Home account id used to look up the account in the rehydrated cache. */\n homeAccountId: string;\n /** Tenant id captured at sign-in. */\n tenantId: string;\n /** Username captured at sign-in (typically the primary email). */\n username: string;\n scopes: string[];\n}\n\nfunction makeConfig(prevCacheJson?: string): Configuration {\n const clientId = process.env.MS_CLIENT_ID || DEFAULT_CLIENT_ID;\n const tenant = process.env.MS_TENANT_ID || \"common\";\n return {\n auth: {\n clientId,\n authority: `https://login.microsoftonline.com/${tenant}`,\n },\n cache: prevCacheJson\n ? {\n // msal-node supports an in-memory cache plugin; we hydrate manually\n // below via deserialize after construction.\n }\n : undefined,\n };\n}\n\nexport function buildPca(prevCacheJson?: string): PublicClientApplication {\n const pca = new PublicClientApplication(makeConfig(prevCacheJson));\n if (prevCacheJson) {\n pca.getTokenCache().deserialize(prevCacheJson);\n }\n return pca;\n}\n\n/**\n * Start a device-code flow. The returned `result` promise resolves once the\n * user has entered the code and consented; callers should poll it (or await it)\n * via `complete_add_account`.\n */\nexport function beginDeviceCode(scopes: string[] = DEFAULT_SCOPES): DeviceCodeBegin {\n const pca = buildPca();\n let resolve!: (v: { tokens: SerializedTokens; account: AccountInfo }) => void;\n let reject!: (err: unknown) => void;\n const result = new Promise<{ tokens: SerializedTokens; account: AccountInfo }>(\n (res, rej) => {\n resolve = res;\n reject = rej;\n },\n );\n\n // We capture the deviceCodeCallback synchronously to surface the user-facing\n // info back out via this object before awaiting the long-running poll.\n let userCode = \"\";\n let verificationUri = \"\";\n let message = \"\";\n let expiresAt = new Date(Date.now() + 15 * 60_000).toISOString();\n let aborted = false;\n\n const ready = new Promise<void>((r) => {\n pca\n .acquireTokenByDeviceCode({\n scopes,\n deviceCodeCallback: (info) => {\n if (!info.userCode || !info.verificationUri) {\n // MSAL may fire the callback with an empty object if the\n // downstream HTTP request fails — reject so the caller\n // gets a clear error instead of silent empty strings.\n reject(\n new Error(\n \"Microsoft device-code endpoint returned no code. \" +\n \"Check MS_CLIENT_ID is a valid Azure Entra public-client application.\",\n ),\n );\n return;\n }\n userCode = info.userCode;\n verificationUri = info.verificationUri;\n message = info.message;\n if (info.expiresIn) {\n expiresAt = new Date(Date.now() + info.expiresIn * 1000).toISOString();\n }\n r();\n },\n })\n .then((authResult: AuthenticationResult | null) => {\n if (aborted) return;\n if (!authResult || !authResult.account) {\n reject(new Error(\"device-code flow returned no account\"));\n return;\n }\n const cache = pca.getTokenCache().serialize();\n const tokens: SerializedTokens = {\n msalCache: cache,\n homeAccountId: authResult.account.homeAccountId,\n tenantId: authResult.account.tenantId,\n username: authResult.account.username,\n scopes,\n };\n resolve({ tokens, account: authResult.account });\n })\n .catch((err) => {\n if (!aborted) reject(err);\n });\n });\n\n // Surface device-code info synchronously via a wrapper Promise:\n // we return the object but its strings are populated once `ready` settles.\n // To keep the API simple, we attach a getter that callers must `await` on.\n // Instead of getters, we wait for `ready` inside the begin helper:\n return {\n // these are placeholders until ready resolves\n get userCode() {\n return userCode;\n },\n get verificationUri() {\n return verificationUri;\n },\n get message() {\n return message;\n },\n get expiresAt() {\n return expiresAt;\n },\n result,\n cancel() {\n aborted = true;\n },\n // hidden helper for the caller to await initial code\n // (typed via a cast below where used)\n ...({ _ready: ready } as Record<string, unknown>),\n } as DeviceCodeBegin;\n}\n\n/** Await `_ready` so the user-code fields are populated. */\nexport async function awaitDeviceCodeReady(b: DeviceCodeBegin): Promise<void> {\n const r = (b as unknown as { _ready: Promise<void> })._ready;\n await r;\n}\n\n/**\n * Acquire a fresh access token for a stored account, refreshing silently from\n * the persisted MSAL cache. Returns the (possibly-updated) cache so the caller\n * can write it back to the store.\n */\nexport async function acquireAccessToken(\n tokens: SerializedTokens,\n scopes: string[] = DEFAULT_SCOPES,\n): Promise<{ accessToken: string; tokens: SerializedTokens }> {\n const pca = buildPca(tokens.msalCache);\n const cache = pca.getTokenCache();\n const account =\n (await cache.getAccountByHomeId(tokens.homeAccountId)) ??\n (await cache.getAllAccounts()).find((a) => a.username === tokens.username);\n if (!account) {\n throw new Error(\"no MSAL account in cache — re-run add_account\");\n }\n const res = await pca.acquireTokenSilent({ account, scopes });\n if (!res?.accessToken) {\n throw new Error(\"acquireTokenSilent returned no access token\");\n }\n const next: SerializedTokens = {\n ...tokens,\n msalCache: cache.serialize(),\n scopes,\n };\n return { accessToken: res.accessToken, tokens: next };\n}\n","import type { AccountRecord } from \"../../store/account-store.js\";\nimport type {\n AddAccountInput,\n AddAccountResult,\n AttachmentContent,\n CompleteAddAccountResult,\n EmailFull,\n EmailProvider,\n EmailSummary,\n ListEmailsOptions,\n SearchEmailsOptions,\n SendInput,\n} from \"../types.js\";\n\nconst NOT_IMPLEMENTED =\n \"IMAP provider is not yet implemented in v1. Tracked at \" +\n \"src/providers/imap/index.ts — see src/providers/types.ts for the contract.\";\n\n/**\n * Placeholder IMAP provider — registered so the contract is locked in and\n * `add_account` returns a clear \"coming soon\" error instead of \"unknown provider\".\n *\n * v2 plan: use `imapflow` + `nodemailer` (for send). Tokens shape will be\n * `{ host, port, secure, user, password|appPassword }` encrypted at rest by\n * the AccountStore, identical to how Outlook's MSAL cache is stored.\n */\nexport class ImapProvider implements EmailProvider {\n readonly id = \"imap\" as const;\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n async addAccount(_input: AddAccountInput): Promise<AddAccountResult> {\n throw new Error(NOT_IMPLEMENTED);\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n async completeAddAccount(_handle: string): Promise<CompleteAddAccountResult> {\n return { status: \"error\", error: NOT_IMPLEMENTED };\n }\n\n async listEmails(\n _account: AccountRecord,\n _opts: ListEmailsOptions,\n ): Promise<EmailSummary[]> {\n throw new Error(NOT_IMPLEMENTED);\n }\n\n async searchEmails(\n _account: AccountRecord,\n _query: string,\n _opts: SearchEmailsOptions,\n ): Promise<EmailSummary[]> {\n throw new Error(NOT_IMPLEMENTED);\n }\n\n async readEmail(_account: AccountRecord, _id: string): Promise<EmailFull> {\n throw new Error(NOT_IMPLEMENTED);\n }\n\n async readAttachment(\n _account: AccountRecord,\n _messageId: string,\n _attachmentId: string,\n ): Promise<AttachmentContent> {\n throw new Error(NOT_IMPLEMENTED);\n }\n\n async sendEmail(\n _account: AccountRecord,\n _msg: SendInput,\n ): Promise<{ id: string }> {\n throw new Error(NOT_IMPLEMENTED);\n }\n\n async saveDraft(\n _account: AccountRecord,\n _msg: SendInput,\n ): Promise<{ id: string }> {\n throw new Error(NOT_IMPLEMENTED);\n }\n\n async moveEmail(\n _account: AccountRecord,\n _id: string,\n _destinationId: string,\n ): Promise<void> {\n throw new Error(NOT_IMPLEMENTED);\n }\n}\n","import type { AccountStore, AccountRecord } from \"../store/account-store.js\";\nimport type { EmailProvider, ProviderId } from \"./types.js\";\nimport { OutlookProvider } from \"./outlook/index.js\";\nimport { ImapProvider } from \"./imap/index.js\";\n\nexport interface Registry {\n get(id: ProviderId): EmailProvider;\n resolveByEmail(email: string): { provider: EmailProvider; account: AccountRecord };\n list(): EmailProvider[];\n}\n\nexport interface BuildRegistryOptions {\n store: AccountStore;\n}\n\nexport function buildRegistry(opts: BuildRegistryOptions): Registry {\n const providers = new Map<ProviderId, EmailProvider>();\n providers.set(\"outlook\", new OutlookProvider({ store: opts.store }));\n providers.set(\"imap\", new ImapProvider());\n // gmail can be added later — registry will return a clear error if asked.\n\n function get(id: ProviderId): EmailProvider {\n const p = providers.get(id);\n if (!p) throw new Error(`unknown provider: ${id}`);\n return p;\n }\n\n function resolveByEmail(email: string): {\n provider: EmailProvider;\n account: AccountRecord;\n } {\n const account = opts.store.getAccount(email);\n if (!account) {\n throw new Error(\n `no account registered for \"${email}\". Call add_account first.`,\n );\n }\n return { provider: get(account.provider), account };\n }\n\n return {\n get,\n resolveByEmail,\n list: () => Array.from(providers.values()),\n };\n}\n","import type { McpServer } from \"@modelcontextprotocol/sdk/server/mcp.js\";\nimport { z } from \"zod\";\n\nimport type { AccountRecord, AccountStore } from \"../store/account-store.js\";\nimport type { Registry } from \"../providers/registry.js\";\nimport type { EmailProvider, ProviderId, SendInput } from \"../providers/types.js\";\nimport { selectBody } from \"../html-to-markdown.js\";\n\nexport interface RegisterToolsOptions {\n store: AccountStore;\n registry: Registry;\n readOnly?: boolean;\n /** When true, send_email is not registered — only send_draft is available. */\n draftOnly?: boolean;\n}\n\n/** JSON-stringify a value into a single MCP text content block. */\nfunction ok(data: unknown, structuredContent?: Record<string, unknown>) {\n const result: { content: Array<{ type: \"text\"; text: string }>; structuredContent?: Record<string, unknown> } = {\n content: [\n { type: \"text\" as const, text: JSON.stringify(data, null, 2) },\n ],\n };\n if (structuredContent !== undefined) {\n result.structuredContent = structuredContent;\n }\n return result;\n}\nfunction fail(message: string) {\n return {\n isError: true,\n content: [{ type: \"text\" as const, text: message }],\n };\n}\n\nconst emailAddrSchema = z.object({\n address: z.string().email(),\n name: z.string().optional(),\n});\n\n// ---------- shared output schemas ----------\n\nconst emailAddrOutputSchema = z.object({\n name: z.string().optional(),\n address: z.string(),\n});\n\nconst accountSummaryOutputSchema = z.object({\n email: z.string(),\n provider: z.enum([\"outlook\", \"imap\", \"gmail\"]),\n displayName: z.string().optional(),\n addedAt: z.string(),\n hasSignature: z.boolean(),\n hasStyle: z.boolean(),\n});\n\nconst emailSummaryOutputSchema = z.object({\n id: z.string(),\n subject: z.string(),\n from: emailAddrOutputSchema.optional(),\n to: z.array(emailAddrOutputSchema).optional(),\n receivedAt: z.string().optional(),\n preview: z.string().optional(),\n isRead: z.boolean().optional(),\n hasAttachments: z.boolean().optional(),\n folder: z.string().optional(),\n});\n\nconst attachmentMetaOutputSchema = z.object({\n id: z.string(),\n name: z.string(),\n contentType: z.string().optional(),\n size: z.number().optional(),\n});\n\nconst styleOutputSchema = z.object({\n fontFamily: z.string().optional(),\n fontSize: z.string().optional(),\n fontColor: z.string().optional(),\n});\n\nexport function registerTools(server: McpServer, opts: RegisterToolsOptions): void {\n const { store, registry, readOnly = false, draftOnly = false } = opts;\n\n // ---------- account management ----------\n\n const listAccountsOutputSchema = {\n accounts: z.array(accountSummaryOutputSchema),\n };\n\n server.registerTool(\n \"list_accounts\",\n {\n description:\n \"List all email accounts known to this server (no secrets). \" +\n \"Use the returned `email` value as the `account` argument to other tools.\",\n inputSchema: {},\n outputSchema: listAccountsOutputSchema,\n },\n async () => {\n const rows = store.listAccounts().map((a) => ({\n email: a.email,\n provider: a.provider,\n displayName: a.displayName,\n addedAt: a.addedAt,\n hasSignature: !!a.signature,\n hasStyle: !!(a.style && (a.style.fontFamily || a.style.fontSize || a.style.fontColor)),\n }));\n const data = { accounts: rows };\n return ok(data, data);\n },\n );\n\n const addAccountOutputSchema = z.discriminatedUnion(\"status\", [\n z.object({\n status: z.literal(\"pending\"),\n handle: z.string(),\n verification: z.object({\n userCode: z.string(),\n verificationUri: z.string(),\n expiresAt: z.string(),\n message: z.string(),\n }),\n }),\n z.object({\n status: z.literal(\"ready\"),\n account: z.object({\n email: z.string(),\n provider: z.enum([\"outlook\", \"imap\", \"gmail\"]),\n displayName: z.string().optional(),\n tokens: z.record(z.unknown()),\n addedAt: z.string(),\n signature: z.string().optional(),\n style: styleOutputSchema.optional(),\n }),\n }),\n ]);\n\n server.registerTool(\n \"add_account\",\n {\n description:\n \"Start adding an email account. For Outlook this returns a device code \" +\n \"the user must enter at the verification URL; then call `complete_add_account` \" +\n \"with the returned `handle` to finalize. Disabled in --read-only mode.\",\n inputSchema: {\n provider: z\n .enum([\"outlook\", \"imap\", \"gmail\"])\n .describe(\"Email backend. v1 only fully implements 'outlook'.\"),\n email: z\n .string()\n .email()\n .optional()\n .describe(\"Optional hint — the provider will verify it against the auth result.\"),\n config: z\n .record(z.unknown())\n .optional()\n .describe(\"Provider-specific config (e.g. IMAP host/port). Unused for Outlook.\"),\n },\n outputSchema: addAccountOutputSchema,\n },\n async (args) => {\n if (readOnly) return fail(\"server is in --read-only mode; add_account is disabled\");\n const provider = registry.get(args.provider as ProviderId);\n try {\n const res = await provider.addAccount({ email: args.email, config: args.config });\n return ok(res, res as unknown as Record<string, unknown>);\n } catch (err) {\n return fail(errMsg(err));\n }\n },\n );\n\n const completeAddAccountOutputSchema = z.object({\n status: z.enum([\"pending\", \"ready\", \"expired\", \"error\"]),\n account: z\n .object({\n email: z.string(),\n provider: z.enum([\"outlook\", \"imap\", \"gmail\"]),\n displayName: z.string().optional(),\n tokens: z.record(z.unknown()),\n addedAt: z.string(),\n signature: z.string().optional(),\n style: styleOutputSchema.optional(),\n })\n .optional(),\n error: z.string().optional(),\n });\n\n server.registerTool(\n \"complete_add_account\",\n {\n description:\n \"Poll/finalize a pending add_account flow. Returns `pending` until the user \" +\n \"completes the device-code step, then `ready` with the persisted account.\",\n inputSchema: {\n provider: z.enum([\"outlook\", \"imap\", \"gmail\"]),\n handle: z.string().min(1),\n },\n outputSchema: completeAddAccountOutputSchema,\n },\n async (args) => {\n const provider = registry.get(args.provider as ProviderId);\n if (!provider.completeAddAccount) {\n return fail(`provider ${args.provider} has no async add-account flow`);\n }\n try {\n const res = await provider.completeAddAccount(args.handle);\n return ok(res, res as unknown as Record<string, unknown>);\n } catch (err) {\n return fail(errMsg(err));\n }\n },\n );\n\n // ---------- account settings ----------\n\n const accountSettingsOutputSchema = {\n signature: z.string().nullable(),\n style: styleOutputSchema.nullable(),\n };\n\n server.registerTool(\n \"get_account_settings\",\n {\n description:\n \"Get signature (HTML) and style preferences for an account.\",\n inputSchema: { account: z.string().email() },\n outputSchema: accountSettingsOutputSchema,\n },\n async (args) => {\n try {\n const acct = store.getAccount(args.account);\n if (!acct) return fail(`no account registered for \"${args.account}\"`);\n const data = { signature: acct.signature ?? null, style: acct.style ?? null };\n return ok(data, data as Record<string, unknown>);\n } catch (err) {\n return fail(errMsg(err));\n }\n },\n );\n\n server.registerTool(\n \"set_account_settings\",\n {\n description:\n \"Set signature (HTML snippet) and/or style preferences for an account. \" +\n \"Disabled in --read-only mode.\",\n inputSchema: {\n account: z.string().email(),\n signature: z\n .string()\n .optional()\n .describe(\"HTML snippet — may contain formatting, images, links. Pass null to clear.\"),\n style: z\n .object({\n fontFamily: z.string().optional(),\n fontSize: z.string().optional(),\n fontColor: z.string().optional(),\n })\n .optional()\n .describe(\"Font preferences applied to outgoing HTML emails. Pass null to clear.\"),\n },\n outputSchema: accountSettingsOutputSchema,\n },\n async (args) => {\n if (readOnly) return fail(\"server is in --read-only mode; set_account_settings is disabled\");\n try {\n const acct = store.getAccount(args.account);\n if (!acct) return fail(`no account registered for \"${args.account}\"`);\n const updated = await store.upsertAccount({\n ...acct,\n signature: args.signature ?? acct.signature,\n style: args.style ?? acct.style,\n });\n const data = { signature: updated.signature ?? null, style: updated.style ?? null };\n return ok(data, data as Record<string, unknown>);\n } catch (err) {\n return fail(errMsg(err));\n }\n },\n );\n\n const removeAccountOutputSchema = {\n removed: z.boolean(),\n email: z.string(),\n };\n\n server.registerTool(\n \"remove_account\",\n {\n description: \"Forget an account and delete its stored tokens. Disabled in --read-only mode.\",\n inputSchema: { email: z.string().email() },\n outputSchema: removeAccountOutputSchema,\n },\n async (args) => {\n if (readOnly) return fail(\"server is in --read-only mode; remove_account is disabled\");\n const removed = await store.removeAccount(args.email);\n const data = { removed, email: args.email };\n return ok(data, data);\n },\n );\n\n // ---------- email ops ----------\n\n const emailListOutputSchema = {\n account: z.string(),\n count: z.number(),\n items: z.array(emailSummaryOutputSchema),\n };\n\n server.registerTool(\n \"list_emails\",\n {\n description:\n \"List recent emails in a folder of the given account. Pass the user's email \" +\n \"address as `account`; the server routes to the correct backend automatically.\",\n inputSchema: {\n account: z.string().email(),\n folder: z.string().default(\"inbox\").optional(),\n limit: z.number().int().positive().max(100).optional(),\n unreadOnly: z.boolean().optional(),\n },\n outputSchema: emailListOutputSchema,\n },\n async (args) => {\n try {\n const { provider, account } = registry.resolveByEmail(args.account);\n const items = await provider.listEmails(account, {\n folder: args.folder,\n limit: args.limit,\n unreadOnly: args.unreadOnly,\n });\n const data = { account: account.email, count: items.length, items };\n return ok(data, data);\n } catch (err) {\n return fail(errMsg(err));\n }\n },\n );\n\n server.registerTool(\n \"search_emails\",\n {\n description:\n \"Search emails by free-text query (KQL on Outlook). Returns lightweight summaries.\",\n inputSchema: {\n account: z.string().email(),\n query: z.string().min(1),\n limit: z.number().int().positive().max(100).optional(),\n },\n outputSchema: emailListOutputSchema,\n },\n async (args) => {\n try {\n const { provider, account } = registry.resolveByEmail(args.account);\n const items = await provider.searchEmails(account, args.query, {\n limit: args.limit,\n });\n const data = { account: account.email, count: items.length, items };\n return ok(data, data);\n } catch (err) {\n return fail(errMsg(err));\n }\n },\n );\n\n const readEmailOutputSchema = {\n id: z.string(),\n subject: z.string(),\n from: emailAddrOutputSchema.optional(),\n to: z.array(emailAddrOutputSchema).optional(),\n cc: z.array(emailAddrOutputSchema).optional(),\n bcc: z.array(emailAddrOutputSchema).optional(),\n receivedAt: z.string().optional(),\n preview: z.string().optional(),\n isRead: z.boolean().optional(),\n hasAttachments: z.boolean().optional(),\n folder: z.string().optional(),\n attachments: z.array(attachmentMetaOutputSchema).optional(),\n body: z.string(),\n bodyFormat: z.enum([\"markdown\", \"html\", \"text\"]),\n };\n\n server.registerTool(\n \"read_email\",\n {\n description:\n \"Fetch a single email with full body and recipients by id. \" +\n \"Body is returned as `body` with `bodyFormat` indicating the format. \" +\n \"Default format is 'markdown' — HTML is automatically converted to save context tokens.\",\n inputSchema: {\n account: z.string().email(),\n id: z.string().min(1),\n format: z\n .enum([\"markdown\", \"html\", \"text\"])\n .default(\"markdown\")\n .optional()\n .describe(\n \"Output body format. 'markdown' converts HTML to Markdown (default), \" +\n \"'html' returns the raw HTML, 'text' returns plain text.\",\n ),\n },\n outputSchema: readEmailOutputSchema,\n },\n async (args) => {\n try {\n const { provider, account } = registry.resolveByEmail(args.account);\n const msg = await provider.readEmail(account, args.id);\n const format = args.format ?? \"markdown\";\n const body = selectBody(msg, format);\n const data = {\n id: msg.id,\n subject: msg.subject,\n from: msg.from,\n to: msg.to,\n cc: msg.cc,\n bcc: msg.bcc,\n receivedAt: msg.receivedAt,\n preview: msg.preview,\n isRead: msg.isRead,\n hasAttachments: msg.hasAttachments,\n folder: msg.folder,\n attachments: msg.attachments,\n body,\n bodyFormat: format,\n };\n return ok(data, data as Record<string, unknown>);\n } catch (err) {\n return fail(errMsg(err));\n }\n },\n );\n\n const readAttachmentOutputSchema = {\n name: z.string(),\n contentType: z.string().optional(),\n path: z.string(),\n };\n\n server.registerTool(\n \"read_attachment\",\n {\n description:\n \"Download an email attachment to a temporary file and return its path. \" +\n \"Use messageId and attachmentId from a prior read_email call.\",\n inputSchema: {\n account: z.string().email(),\n messageId: z.string().min(1),\n attachmentId: z.string().min(1),\n },\n outputSchema: readAttachmentOutputSchema,\n },\n async (args) => {\n try {\n const { provider, account } = registry.resolveByEmail(args.account);\n const res = await provider.readAttachment(account, args.messageId, args.attachmentId);\n return ok(res, res as unknown as Record<string, unknown>);\n } catch (err) {\n return fail(errMsg(err));\n }\n },\n );\n\n // ---------- move / archive ----------\n\n {\n const schema = {\n account: z.string().email(),\n id: z.string().min(1).describe(\"Message ID to move\"),\n };\n\n const archiveOutputSchema = {\n archived: z.literal(true),\n id: z.string(),\n };\n\n server.registerTool(\n \"archive_email\",\n {\n description:\n \"Move a message to the Archive folder. Disabled in --read-only mode.\",\n inputSchema: schema,\n outputSchema: archiveOutputSchema,\n },\n async (args) => {\n if (readOnly) return fail(\"server is in --read-only mode; archive_email is disabled\");\n try {\n const { provider, account } = registry.resolveByEmail(args.account);\n await provider.moveEmail(account, args.id, \"archive\");\n const data = { archived: true as const, id: args.id };\n return ok(data, data);\n } catch (err) {\n return fail(errMsg(err));\n }\n },\n );\n\n const trashOutputSchema = {\n trashed: z.literal(true),\n id: z.string(),\n };\n\n server.registerTool(\n \"trash_email\",\n {\n description:\n \"Move a message to the Deleted Items (trash) folder. Disabled in --read-only mode.\",\n inputSchema: schema,\n outputSchema: trashOutputSchema,\n },\n async (args) => {\n if (readOnly) return fail(\"server is in --read-only mode; trash_email is disabled\");\n try {\n const { provider, account } = registry.resolveByEmail(args.account);\n await provider.moveEmail(account, args.id, \"deleteditems\");\n const data = { trashed: true as const, id: args.id };\n return ok(data, data);\n } catch (err) {\n return fail(errMsg(err));\n }\n },\n );\n }\n\n const moveEmailOutputSchema = {\n moved: z.literal(true),\n id: z.string(),\n destination: z.string(),\n };\n\n server.registerTool(\n \"move_email\",\n {\n description:\n \"Move a message to any folder by well-known name (e.g. 'inbox', 'drafts', \" +\n \"'junkemail', 'sentitems', 'outbox') or custom folder ID. \" +\n \"Disabled in --read-only mode.\",\n inputSchema: {\n account: z.string().email(),\n id: z.string().min(1).describe(\"Message ID to move\"),\n destination: z\n .string()\n .min(1)\n .describe(\n \"Destination folder — a well-known folder name \" +\n \"('archive', 'deleteditems', 'inbox', 'drafts', 'junkemail', \" +\n \"'sentitems', 'outbox') or a raw folder ID.\",\n ),\n },\n outputSchema: moveEmailOutputSchema,\n },\n async (args) => {\n if (readOnly) return fail(\"server is in --read-only mode; move_email is disabled\");\n try {\n const { provider, account } = registry.resolveByEmail(args.account);\n await provider.moveEmail(account, args.id, args.destination);\n const data = { moved: true as const, id: args.id, destination: args.destination };\n return ok(data, data);\n } catch (err) {\n return fail(errMsg(err));\n }\n },\n );\n\n const sendEmailSchema = z.object({\n account: z.string().email(),\n to: z.array(emailAddrSchema).min(1),\n cc: z.array(emailAddrSchema).optional(),\n bcc: z.array(emailAddrSchema).optional(),\n subject: z.string(),\n body: z.string(),\n isHtml: z.boolean().optional(),\n include_signature: z\n .boolean()\n .describe(\n \"Whether to append the account's saved HTML signature to the email. \" +\n \"If true, don't include a signature in the body param to avoid double signature. \" +\n \"Returns an error if true but no signature is configured for this account.\",\n ),\n inReplyTo: z\n .string()\n .optional()\n .describe(\n \"Message ID to reply to. When set, sends as a threaded reply \" +\n \"which includes the quoted thread history automatically.\",\n ),\n replyAll: z\n .boolean()\n .default(false)\n .optional()\n .describe(\n \"When true and `inReplyTo` is set, reply to all recipients \" +\n \"instead of just the sender.\",\n ),\n forwardMessageId: z\n .string()\n .optional()\n .describe(\n \"Message ID to forward. When set, sends as a forward of the \" +\n \"specified message, preserving the original content. \" +\n \"Mutually exclusive with `inReplyTo`.\",\n ),\n });\n\n type SendEmailArgs = z.infer<typeof sendEmailSchema>;\n\n async function handleSendOrDraft(\n args: SendEmailArgs,\n action: (\n provider: EmailProvider,\n account: AccountRecord,\n msg: SendInput,\n ) => Promise<{ id: string }>,\n resultKey: string,\n toolName: string,\n ) {\n if (readOnly) return fail(`server is in --read-only mode; ${toolName} is disabled`);\n try {\n const { provider, account } = registry.resolveByEmail(args.account);\n if (args.include_signature && !account.signature) {\n return fail(\n \"include_signature is true but no signature is configured for this account. \" +\n \"Set up a signature first with set_account_settings.\",\n );\n }\n const composed = composeBody({\n body: args.body,\n isHtml: args.isHtml,\n signature: account.signature,\n style: account.style,\n includeSignature: args.include_signature,\n });\n if (args.inReplyTo && args.forwardMessageId) {\n return fail(\n \"inReplyTo and forwardMessageId are mutually exclusive — use one or the other\",\n );\n }\n const res = await action(provider, account, {\n to: args.to,\n cc: args.cc,\n bcc: args.bcc,\n subject: args.subject,\n body: composed.body,\n isHtml: composed.isHtml,\n inReplyTo: args.inReplyTo,\n replyAll: args.replyAll,\n forwardMessageId: args.forwardMessageId,\n });\n const result: Record<string, unknown> = { [resultKey]: true, ...res };\n // For draft_email, fetch the created draft back so the agent can\n // inspect the actual HTML content before deciding to send it.\n if (toolName === \"draft_email\" && res.id) {\n const draft = await provider.readEmail(account, res.id);\n result.draftHtml = draft.bodyHtml;\n }\n return ok(result, result);\n } catch (err) {\n return fail(errMsg(err));\n }\n }\n\n // ---------- send / draft ----------\n\n const sendEmailOutputSchema = {\n sent: z.literal(true),\n id: z.string(),\n };\n\n if (!draftOnly) {\n server.registerTool(\n \"send_email\",\n {\n description:\n \"Send an email from the given account. Appends the \" +\n \"account's signature (HTML) and applies style preferences when \" +\n \"`include_signature` is true. Returns an error if \" +\n \"`include_signature` is true but no signature is configured. \" +\n \"When `inReplyTo` is set, sends as a reply (or reply-all) which \" +\n \"preserves thread history and conversation threading. \" +\n \"When `forwardMessageId` is set, sends as a forward of the \" +\n \"specified message, preserving the original content. \" +\n \"`inReplyTo` and `forwardMessageId` are mutually exclusive. \" +\n \"Disabled in --read-only mode.\",\n inputSchema: sendEmailSchema,\n outputSchema: sendEmailOutputSchema,\n },\n async (args) =>\n handleSendOrDraft(\n args as SendEmailArgs,\n (p, a, m) => p.sendEmail(a, m),\n \"sent\",\n \"send_email\",\n ),\n );\n }\n\n const draftEmailOutputSchema = {\n draft: z.literal(true),\n id: z.string(),\n draftHtml: z.string().optional(),\n };\n\n server.registerTool(\n \"draft_email\",\n {\n description:\n \"Create a draft email from the given account without sending it. \" +\n \"Works identically to send_email — appends signature when \" +\n \"`include_signature` is true, applies style, and supports replies \" +\n \"and forwards — but saves the message to the Drafts folder \" +\n \"instead of sending. Returns the draft message ID and the draft's \" +\n \"HTML body content (`draftHtml`). Before sending the draft, \" +\n \"inspect `draftHtml` to verify the draft looks correct: no \" +\n \"duplicate signature blocks, no broken or missing inline images, \" +\n \"no malformed HTML, and no other formatting issues. \" +\n \"Disabled in --read-only mode.\",\n inputSchema: sendEmailSchema,\n outputSchema: draftEmailOutputSchema,\n },\n async (args) =>\n handleSendOrDraft(\n args as SendEmailArgs,\n (p, a, m) => p.saveDraft(a, m),\n \"draft\",\n \"draft_email\",\n ),\n );\n}\n\n// ---------- body composition helpers ----------\n\ninterface ComposeBodyInput {\n body: string;\n isHtml?: boolean;\n signature?: string;\n style?: { fontFamily?: string; fontSize?: string; fontColor?: string };\n includeSignature: boolean;\n}\n\nexport function composeBody(input: ComposeBodyInput): { body: string; isHtml: boolean } {\n const { body, isHtml = false, signature, style, includeSignature } = input;\n const hasSignature = includeSignature && !!signature;\n const hasStyle = !!(style && (style.fontFamily || style.fontSize || style.fontColor));\n\n // Nothing to inject — pass through unchanged\n if (!hasSignature && !hasStyle) {\n return { body, isHtml };\n }\n\n // Need HTML for signature or style injection\n const styleAttr = hasStyle ? buildStyleAttr(style!) : \"\";\n\n if (isHtml) {\n let result = hasStyle ? `<div style=\"${styleAttr}\">${body}</div>` : body;\n if (hasSignature) result += `\\n<div class=\"signature\">${signature}</div>`;\n return { body: result, isHtml: true };\n }\n\n // Auto-upgrade plain text to HTML\n const escaped = escapeHtml(body);\n let result = `<div style=\"${styleAttr}\">${escaped}</div>`;\n if (hasSignature) result += `\\n<div class=\"signature\">${signature}</div>`;\n return { body: result, isHtml: true };\n}\n\nexport function buildStyleAttr(style: { fontFamily?: string; fontSize?: string; fontColor?: string }): string {\n const parts: string[] = [];\n if (style.fontFamily) parts.push(`font-family: ${style.fontFamily}`);\n if (style.fontSize) parts.push(`font-size: ${style.fontSize}`);\n if (style.fontColor) parts.push(`color: ${style.fontColor}`);\n return parts.join(\"; \");\n}\n\nexport function escapeHtml(text: string): string {\n return text\n .replace(/&/g, \"&\")\n .replace(/</g, \"<\")\n .replace(/>/g, \">\")\n .replace(/\"/g, \""\")\n .replace(/\\n/g, \"<br>\");\n}\n\nfunction errMsg(err: unknown): string {\n if (err instanceof Error) return err.message;\n return String(err);\n}\n","import TurndownService from \"turndown\";\n\nconst turndown = new TurndownService();\n\n/** Light wrapper — callers just pass HTML, get markdown back. */\nexport function htmlToMarkdown(html: string): string {\n return turndown.turndown(html);\n}\n\n/** Pick the right body for the requested format. */\nexport function selectBody(\n msg: { bodyHtml?: string; bodyText?: string },\n format: \"markdown\" | \"html\" | \"text\",\n): string {\n switch (format) {\n case \"markdown\": {\n if (msg.bodyHtml) return htmlToMarkdown(msg.bodyHtml);\n if (msg.bodyText) return msg.bodyText;\n return \"\";\n }\n case \"html\": {\n if (msg.bodyHtml) return msg.bodyHtml;\n if (msg.bodyText) return msg.bodyText;\n return \"\";\n }\n case \"text\": {\n if (msg.bodyText) return msg.bodyText;\n if (msg.bodyHtml) return msg.bodyHtml.replace(/<[^>]*>/g, \"\");\n return \"\";\n }\n }\n}\n","export const VERSION = \"0.3.0\";\n","import { startServer } from \"./server.js\";\n\ntype ParsedArgs = {\n http: boolean;\n port: number;\n host: string;\n dataDir?: string;\n readOnly: boolean;\n help: boolean;\n};\n\nfunction parseArgs(argv: string[]): ParsedArgs {\n const out: ParsedArgs = {\n http: false,\n port: 3000,\n host: \"127.0.0.1\",\n readOnly: false,\n help: false,\n };\n for (let i = 0; i < argv.length; i++) {\n const a = argv[i];\n switch (a) {\n case \"--http\":\n out.http = true;\n break;\n case \"--port\":\n out.port = Number(argv[++i] ?? \"3000\");\n break;\n case \"--host\":\n out.host = String(argv[++i] ?? \"127.0.0.1\");\n break;\n case \"--data-dir\":\n out.dataDir = String(argv[++i] ?? \"\");\n break;\n case \"--read-only\":\n out.readOnly = true;\n break;\n case \"-h\":\n case \"--help\":\n out.help = true;\n break;\n default:\n if (a && a.startsWith(\"--\")) {\n // ignore unknown flags rather than crashing — keeps the CLI forgiving\n // when embedded in MCP host configs.\n }\n }\n }\n return out;\n}\n\nfunction printHelp(): void {\n const msg = `hypermail-mcp — unified email MCP server\n\nUsage:\n hypermail-mcp [options]\n\nOptions:\n --http Run as Streamable HTTP server (default: stdio)\n --port <n> HTTP port (default: 3000)\n --host <addr> HTTP bind address (default: 127.0.0.1)\n --data-dir <path> Where to store the encrypted accounts file\n (default: $HYPERMAIL_MCP_DATA_DIR or ~/.hypermail-mcp)\n --read-only Disable tools that modify state (send_email, remove_account, add_account)\n -h, --help Show this help\n\nEnvironment:\n HYPERMAIL_MCP_DATA_DIR Same as --data-dir\n HYPERMAIL_MCP_KEY 32-byte key (base64 or hex) for at-rest encryption\n MS_CLIENT_ID Azure AD public client (application) ID\n MS_TENANT_ID Tenant (default: \"common\")\n`;\n process.stdout.write(msg);\n}\n\nasync function main(): Promise<void> {\n const opts = parseArgs(process.argv.slice(2));\n if (opts.help) {\n printHelp();\n return;\n }\n const draftOnly = process.env.HYPERMAIL_DRAFT_ONLY === \"true\";\n\n await startServer({\n http: opts.http,\n port: opts.port,\n host: opts.host,\n dataDir: opts.dataDir,\n readOnly: opts.readOnly,\n draftOnly,\n });\n}\n\nmain().catch((err) => {\n // eslint-disable-next-line no-console\n console.error(\"[hypermail-mcp] fatal:\", err);\n process.exit(1);\n});\n"],"mappings":";;;AAAA,SAAS,iBAAiB;AAC1B,SAAS,4BAA4B;AACrC,SAAS,qCAAqC;AAC9C,SAAS,cAAAA,mBAAkB;AAC3B,SAAS,gBAAgB,wBAAmE;;;ACJ5F;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,YAAY,UAAU;AAC/B,SAAS,eAAe;AACxB,OAAO,UAAU;AA8BjB,IAAM,YAAY;AAClB,IAAM,OAAO;AACb,IAAM,UAAU;AAET,IAAM,eAAN,MAAM,cAAa;AAAA,EAChB,YACW,UACA,KACT,MACR;AAHiB;AACA;AACT;AAAA,EACP;AAAA,EAHgB;AAAA,EACA;AAAA,EACT;AAAA,EAGV,aAAa,KAAK,OAAoB,CAAC,GAA0B;AAC/D,UAAM,UAAU,eAAe,KAAK,OAAO;AAC3C,UAAM,GAAG,MAAM,SAAS,EAAE,WAAW,MAAM,MAAM,IAAM,CAAC;AACxD,UAAM,WAAW,KAAK,KAAK,SAAS,SAAS;AAC7C,UAAM,MAAM,KAAK,OAAQ,MAAM,WAAW,OAAO;AAEjD,QAAI;AACJ,QAAI;AACF,YAAM,MAAM,MAAM,GAAG,SAAS,QAAQ;AACtC,aAAO,QAAQ,KAAK,GAAG;AAAA,IACzB,SAAS,KAAc;AACrB,UAAK,IAA8B,SAAS,UAAU;AACpD,eAAO,EAAE,SAAS,GAAG,UAAU,CAAC,EAAE;AAAA,MACpC,OAAO;AACL,cAAM;AAAA,MACR;AAAA,IACF;AACA,WAAO,IAAI,cAAa,UAAU,KAAK,IAAI;AAAA,EAC7C;AAAA,EAEA,eAAgC;AAG9B,WAAO,KAAK,KAAK,SAAS,IA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