imap-mcp 1.4.0 → 2.0.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 +93 -6
- package/dist/access.js +5 -1
- package/dist/index.js +13 -1
- package/dist/index.test.js +32 -1
- package/dist/instructions.js +5 -2
- package/dist/instructions.test.js +8 -0
- package/dist/tools/archive-emails.js +55 -0
- package/dist/tools/archive-emails.test.js +154 -0
- package/dist/tools/batch.js +219 -0
- package/dist/tools/fake-imap.js +94 -5
- package/dist/tools/imap.js +21 -3
- package/dist/tools/mark-emails.js +131 -0
- package/dist/tools/mark-emails.test.js +124 -0
- package/dist/tools/move-emails.js +55 -0
- package/dist/tools/move-emails.test.js +169 -0
- package/dist/tools/trash-emails.js +37 -0
- package/dist/tools/trash-emails.test.js +73 -0
- package/package.json +1 -1
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// move_emails: files messages into a named folder. Trash is refused, by
|
|
2
|
+
// place name and by the folder's special-use attribute, so this tool stays a
|
|
3
|
+
// non-destructive write; trash_emails carries that annotation. Shaped like
|
|
4
|
+
// langmail-mcp's move_emails; the differences are in the description.
|
|
5
|
+
import { z } from "zod/v4";
|
|
6
|
+
import { copiesOutside, fileMessages, formatBatchResult, idsShape, locateMessages, uniqueIds, } from "./batch.js";
|
|
7
|
+
import { ACCOUNT_WRITE, errorResult, placeOf, resolveMailbox, selectableFolders, withClient, } from "./imap.js";
|
|
8
|
+
export const moveEmailsShape = {
|
|
9
|
+
...idsShape,
|
|
10
|
+
mailbox: z
|
|
11
|
+
.string()
|
|
12
|
+
.min(1)
|
|
13
|
+
.max(200)
|
|
14
|
+
.describe("Destination folder. The standard places — Inbox, Archive, Sent, Drafts, Junk — resolve to " +
|
|
15
|
+
"the folder holding that role whatever the server calls it; any other value must match an " +
|
|
16
|
+
"existing folder name exactly. Folders are never created. Trash is not " +
|
|
17
|
+
"accepted here: use trash_emails."),
|
|
18
|
+
};
|
|
19
|
+
export async function moveEmails(createClient, args) {
|
|
20
|
+
const ids = uniqueIds(args.ids);
|
|
21
|
+
const { mailbox } = args;
|
|
22
|
+
const trashRefusal = errorResult(`Moving to ${mailbox} is a delete. Use trash_emails instead. Nothing was moved.`);
|
|
23
|
+
if (mailbox.toLowerCase() === "trash")
|
|
24
|
+
return trashRefusal;
|
|
25
|
+
return withClient(createClient, async (client) => {
|
|
26
|
+
const resolved = resolveMailbox(await selectableFolders(client), mailbox, "Nothing was moved.");
|
|
27
|
+
if ("content" in resolved)
|
|
28
|
+
return resolved;
|
|
29
|
+
if (placeOf(resolved)?.name === "Trash")
|
|
30
|
+
return trashRefusal;
|
|
31
|
+
const located = await locateMessages(client, ids);
|
|
32
|
+
const outcomes = await fileMessages(client, ids, located, resolved, copiesOutside(resolved));
|
|
33
|
+
return formatBatchResult("Moved", outcomes, {
|
|
34
|
+
suffix: ` to ${mailbox}`,
|
|
35
|
+
already: `already only in ${mailbox}`,
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
export const MOVE_EMAILS_DESCRIPTION = "Move one or more messages into a folder. They are removed from every folder they were in " +
|
|
40
|
+
'and end up only in the destination (on Gmail they also stay in All Mail). Use this when the user names a destination ("put this ' +
|
|
41
|
+
'in Projects", "move it back to the inbox"). Takes message ids from search_emails or ' +
|
|
42
|
+
"get_thread. The mailbox is resolved the same way as the search_emails mailbox filter and is " +
|
|
43
|
+
"never created; an unknown name returns the folders that exist. Read state and flags (to-do, " +
|
|
44
|
+
"starred) are untouched; use mark_emails to change them. Re-running it on the same " +
|
|
45
|
+
"ids is harmless; on servers without a stable message id (OBJECTID or Gmail) a moved message " +
|
|
46
|
+
"gets a new id. On a server with neither MOVE nor UIDPLUS, a folder holding other deleted messages is left alone and its ids are listed. To archive use archive_emails; to delete use trash_emails — this tool " +
|
|
47
|
+
"refuses Trash. Reports how many messages moved and lists any id that could not be moved, with the reason.";
|
|
48
|
+
export function registerMoveEmails(createClient, server) {
|
|
49
|
+
server.registerTool("move_emails", {
|
|
50
|
+
title: "Move emails",
|
|
51
|
+
description: MOVE_EMAILS_DESCRIPTION,
|
|
52
|
+
inputSchema: moveEmailsShape,
|
|
53
|
+
annotations: ACCOUNT_WRITE,
|
|
54
|
+
}, async (args) => moveEmails(createClient, args));
|
|
55
|
+
}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { describe, expect, test, vi } from "vitest";
|
|
2
|
+
import { fakeClient } from "./fake-imap.js";
|
|
3
|
+
import { ACCOUNT_WRITE, encodeMessageId } from "./imap.js";
|
|
4
|
+
import { moveEmails, registerMoveEmails } from "./move-emails.js";
|
|
5
|
+
const msg = (uid, emailId, flags) => ({
|
|
6
|
+
uid,
|
|
7
|
+
emailId,
|
|
8
|
+
date: "2026-11-01T09:00:00Z",
|
|
9
|
+
subject: `Message ${uid}`,
|
|
10
|
+
from: "sender@example.com",
|
|
11
|
+
flags,
|
|
12
|
+
});
|
|
13
|
+
const folders = () => [
|
|
14
|
+
{
|
|
15
|
+
path: "INBOX",
|
|
16
|
+
messages: [msg(1, "m1", ["\\Flagged"]), msg(2, "m2")],
|
|
17
|
+
},
|
|
18
|
+
{ path: "Projects", messages: [] },
|
|
19
|
+
{ path: "Bin", specialUse: "\\Trash", messages: [] },
|
|
20
|
+
{ path: "Work", messages: [msg(3, "m1", ["\\Flagged"])] },
|
|
21
|
+
];
|
|
22
|
+
const stable = (emailId) => encodeMessageId({ kind: "stable", emailId });
|
|
23
|
+
const subjects = (f) => f?.messages.map((m) => m.subject);
|
|
24
|
+
describe("move_emails", () => {
|
|
25
|
+
test("registers as an account write named move_emails", () => {
|
|
26
|
+
const server = { registerTool: vi.fn() };
|
|
27
|
+
registerMoveEmails(fakeClient([]).createClient, server);
|
|
28
|
+
expect(server.registerTool).toHaveBeenCalledWith("move_emails", expect.objectContaining({ annotations: ACCOUNT_WRITE }), expect.any(Function));
|
|
29
|
+
});
|
|
30
|
+
test("moves every copy into the folder, keeping flags", async () => {
|
|
31
|
+
const data = folders();
|
|
32
|
+
const { createClient } = fakeClient(data);
|
|
33
|
+
const result = await moveEmails(createClient, {
|
|
34
|
+
ids: [stable("m1")],
|
|
35
|
+
mailbox: "Projects",
|
|
36
|
+
});
|
|
37
|
+
expect(result).toEqual({
|
|
38
|
+
content: [{ type: "text", text: "Moved 1 message(s) to Projects." }],
|
|
39
|
+
});
|
|
40
|
+
expect(subjects(data[0])).toEqual(["Message 2"]);
|
|
41
|
+
expect(subjects(data[3])).toEqual([]);
|
|
42
|
+
expect(data[1].messages.every((m) => m.flags?.includes("\\Flagged"))).toBe(true);
|
|
43
|
+
});
|
|
44
|
+
test("a place name resolves to its folder, and a re-run changes nothing", async () => {
|
|
45
|
+
const data = folders();
|
|
46
|
+
const { createClient } = fakeClient(data);
|
|
47
|
+
await moveEmails(createClient, { ids: [stable("m2")], mailbox: "Projects" });
|
|
48
|
+
const back = await moveEmails(createClient, {
|
|
49
|
+
ids: [stable("m2")],
|
|
50
|
+
mailbox: "inbox",
|
|
51
|
+
});
|
|
52
|
+
expect(back.content[0].text).toBe("Moved 1 message(s) to inbox.");
|
|
53
|
+
const again = await moveEmails(createClient, {
|
|
54
|
+
ids: [stable("m2")],
|
|
55
|
+
mailbox: "inbox",
|
|
56
|
+
});
|
|
57
|
+
expect(again).toEqual({
|
|
58
|
+
content: [
|
|
59
|
+
{
|
|
60
|
+
type: "text",
|
|
61
|
+
text: "Moved 0 message(s) to inbox. 1 message(s) already only in inbox.",
|
|
62
|
+
},
|
|
63
|
+
],
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
test("an unknown folder is an error listing the folders, and nothing is created", async () => {
|
|
67
|
+
const data = folders();
|
|
68
|
+
const { client, createClient } = fakeClient(data);
|
|
69
|
+
const result = await moveEmails(createClient, {
|
|
70
|
+
ids: [stable("m1")],
|
|
71
|
+
mailbox: "Receipts",
|
|
72
|
+
});
|
|
73
|
+
expect(result).toEqual({
|
|
74
|
+
content: [
|
|
75
|
+
{
|
|
76
|
+
type: "text",
|
|
77
|
+
text: "Mailbox not found: Receipts. Nothing was moved. Available folders: Inbox, Trash, Projects, Work.",
|
|
78
|
+
},
|
|
79
|
+
],
|
|
80
|
+
isError: true,
|
|
81
|
+
});
|
|
82
|
+
expect(client.commands).toEqual([]);
|
|
83
|
+
});
|
|
84
|
+
test.each(["Trash", "trash", "Bin"])("refuses %s as the destination", async (mailbox) => {
|
|
85
|
+
const { client, createClient } = fakeClient(folders());
|
|
86
|
+
const result = await moveEmails(createClient, {
|
|
87
|
+
ids: [stable("m1")],
|
|
88
|
+
mailbox,
|
|
89
|
+
});
|
|
90
|
+
expect(result).toEqual({
|
|
91
|
+
content: [
|
|
92
|
+
{
|
|
93
|
+
type: "text",
|
|
94
|
+
text: `Moving to ${mailbox} is a delete. Use trash_emails instead. Nothing was moved.`,
|
|
95
|
+
},
|
|
96
|
+
],
|
|
97
|
+
isError: true,
|
|
98
|
+
});
|
|
99
|
+
expect(client.commands).toEqual([]);
|
|
100
|
+
});
|
|
101
|
+
test("on Gmail, a message only in All Mail is copied into the folder and stays in All Mail", async () => {
|
|
102
|
+
const data = [
|
|
103
|
+
{
|
|
104
|
+
path: "[Gmail]/All Mail",
|
|
105
|
+
specialUse: "\\All",
|
|
106
|
+
messages: [msg(7, "g1")],
|
|
107
|
+
},
|
|
108
|
+
{ path: "Work", messages: [] },
|
|
109
|
+
];
|
|
110
|
+
const { client, createClient } = fakeClient(data);
|
|
111
|
+
const result = await moveEmails(createClient, {
|
|
112
|
+
ids: [stable("g1")],
|
|
113
|
+
mailbox: "Work",
|
|
114
|
+
});
|
|
115
|
+
expect(result.content[0].text).toBe("Moved 1 message(s) to Work.");
|
|
116
|
+
// A COPY adds the label; a MOVE out of All Mail would delete the message
|
|
117
|
+
expect(client.commands).toEqual(["COPY [Gmail]/All Mail 7 Work"]);
|
|
118
|
+
expect(data[0].messages).toHaveLength(1);
|
|
119
|
+
});
|
|
120
|
+
test("on Gmail, a message in the destination and All Mail is already there", async () => {
|
|
121
|
+
const data = [
|
|
122
|
+
{
|
|
123
|
+
path: "[Gmail]/All Mail",
|
|
124
|
+
specialUse: "\\All",
|
|
125
|
+
messages: [msg(7, "g1")],
|
|
126
|
+
},
|
|
127
|
+
{ path: "Work", messages: [msg(2, "g1")] },
|
|
128
|
+
];
|
|
129
|
+
const { client, createClient } = fakeClient(data);
|
|
130
|
+
const result = await moveEmails(createClient, {
|
|
131
|
+
ids: [stable("g1")],
|
|
132
|
+
mailbox: "Work",
|
|
133
|
+
});
|
|
134
|
+
expect(result.content[0].text).toBe("Moved 0 message(s) to Work. 1 message(s) already only in Work.");
|
|
135
|
+
expect(client.commands).toEqual([]);
|
|
136
|
+
});
|
|
137
|
+
test("on Gmail, never moves out of Starred or Important, so the star stays", async () => {
|
|
138
|
+
const data = [
|
|
139
|
+
{ path: "INBOX", messages: [msg(1, "g1", ["\\Flagged"])] },
|
|
140
|
+
{
|
|
141
|
+
path: "[Gmail]/All Mail",
|
|
142
|
+
specialUse: "\\All",
|
|
143
|
+
messages: [msg(7, "g1")],
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
path: "[Gmail]/Starred",
|
|
147
|
+
specialUse: "\\Flagged",
|
|
148
|
+
messages: [msg(8, "g1")],
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
path: "[Gmail]/Important",
|
|
152
|
+
specialUse: "\\Important",
|
|
153
|
+
messages: [msg(9, "g1")],
|
|
154
|
+
},
|
|
155
|
+
{ path: "Work", messages: [] },
|
|
156
|
+
];
|
|
157
|
+
const { client, createClient } = fakeClient(data, [
|
|
158
|
+
"MOVE",
|
|
159
|
+
"UIDPLUS",
|
|
160
|
+
"X-GM-EXT-1",
|
|
161
|
+
]);
|
|
162
|
+
const result = await moveEmails(createClient, {
|
|
163
|
+
ids: [stable("g1")],
|
|
164
|
+
mailbox: "Work",
|
|
165
|
+
});
|
|
166
|
+
expect(result.content[0].text).toBe("Moved 1 message(s) to Work.");
|
|
167
|
+
expect(client.commands).toEqual(["MOVE INBOX 1 Work"]);
|
|
168
|
+
});
|
|
169
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// trash_emails: moves messages to the Trash folder, where they stay
|
|
2
|
+
// recoverable until Trash is emptied. Its own tool rather than a destination
|
|
3
|
+
// move_emails accepts, so a client gating on destructiveHint confirms only
|
|
4
|
+
// this. Shaped like langmail-mcp's trash_emails; the differences are in the
|
|
5
|
+
// description. Permanent deletion is deliberately not offered.
|
|
6
|
+
import { copiesOutside, fileMessages, formatBatchResult, idsShape, locateMessages, uniqueIds, } from "./batch.js";
|
|
7
|
+
import { ACCOUNT_DESTRUCTIVE, errorResult, findPlace, selectableFolders, withClient, } from "./imap.js";
|
|
8
|
+
export async function trashEmails(createClient, args) {
|
|
9
|
+
const ids = uniqueIds(args.ids);
|
|
10
|
+
return withClient(createClient, async (client) => {
|
|
11
|
+
const trash = findPlace(await selectableFolders(client), "Trash");
|
|
12
|
+
if (!trash) {
|
|
13
|
+
return errorResult("This account has no Trash mailbox. Nothing was trashed. Create one in the mail client, or use " +
|
|
14
|
+
"archive_emails to clear the inbox or move_emails to file into an existing folder.");
|
|
15
|
+
}
|
|
16
|
+
const located = await locateMessages(client, ids);
|
|
17
|
+
const outcomes = await fileMessages(client, ids, located, trash, copiesOutside(trash));
|
|
18
|
+
return formatBatchResult("Trashed", outcomes, {
|
|
19
|
+
already: "already in Trash",
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
export const TRASH_EMAILS_DESCRIPTION = "Move one or more messages to the Trash. Use this only when the user asks to delete, trash, " +
|
|
24
|
+
"or bin mail; to clear the inbox without deleting, use archive_emails, and to file into a " +
|
|
25
|
+
"folder use move_emails. Trashed messages stay recoverable in the mail client until Trash " +
|
|
26
|
+
"is emptied; there is no permanent delete of mail. Re-running it on the same ids is harmless. " +
|
|
27
|
+
"Takes message ids from search_emails or get_thread; on servers without a stable message id " +
|
|
28
|
+
"(OBJECTID or Gmail) a trashed message gets a new id. On a server with neither MOVE nor UIDPLUS, a folder holding other deleted messages is left alone and its ids are listed. Reports how many messages were trashed " +
|
|
29
|
+
"and lists any id that could not be trashed, with the reason.";
|
|
30
|
+
export function registerTrashEmails(createClient, server) {
|
|
31
|
+
server.registerTool("trash_emails", {
|
|
32
|
+
title: "Trash emails",
|
|
33
|
+
description: TRASH_EMAILS_DESCRIPTION,
|
|
34
|
+
inputSchema: idsShape,
|
|
35
|
+
annotations: ACCOUNT_DESTRUCTIVE,
|
|
36
|
+
}, async (args) => trashEmails(createClient, args));
|
|
37
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { describe, expect, test, vi } from "vitest";
|
|
2
|
+
import { fakeClient } from "./fake-imap.js";
|
|
3
|
+
import { ACCOUNT_DESTRUCTIVE, encodeMessageId } from "./imap.js";
|
|
4
|
+
import { registerTrashEmails, trashEmails } from "./trash-emails.js";
|
|
5
|
+
const msg = (uid, emailId, flags) => ({
|
|
6
|
+
uid,
|
|
7
|
+
emailId,
|
|
8
|
+
date: "2026-11-01T09:00:00Z",
|
|
9
|
+
subject: `Message ${uid}`,
|
|
10
|
+
from: "sender@example.com",
|
|
11
|
+
flags,
|
|
12
|
+
});
|
|
13
|
+
const folders = () => [
|
|
14
|
+
{ path: "INBOX", messages: [msg(1, "m1", ["\\Seen"]), msg(2, "m2")] },
|
|
15
|
+
{ path: "Deleted Items", specialUse: "\\Trash", messages: [] },
|
|
16
|
+
];
|
|
17
|
+
const stable = (emailId) => encodeMessageId({ kind: "stable", emailId });
|
|
18
|
+
describe("trash_emails", () => {
|
|
19
|
+
test("registers as an account-destructive tool named trash_emails", () => {
|
|
20
|
+
const server = { registerTool: vi.fn() };
|
|
21
|
+
registerTrashEmails(fakeClient([]).createClient, server);
|
|
22
|
+
expect(server.registerTool).toHaveBeenCalledWith("trash_emails", expect.objectContaining({ annotations: ACCOUNT_DESTRUCTIVE }), expect.any(Function));
|
|
23
|
+
});
|
|
24
|
+
test("moves messages into the folder marked Trash, where they stay", async () => {
|
|
25
|
+
const data = folders();
|
|
26
|
+
const { createClient } = fakeClient(data);
|
|
27
|
+
const result = await trashEmails(createClient, {
|
|
28
|
+
ids: [stable("m1"), stable("m1")],
|
|
29
|
+
});
|
|
30
|
+
expect(result).toEqual({
|
|
31
|
+
content: [{ type: "text", text: "Trashed 1 message(s)." }],
|
|
32
|
+
});
|
|
33
|
+
expect(data[0].messages.map((m) => m.subject)).toEqual(["Message 2"]);
|
|
34
|
+
expect(data[1].messages.map((m) => [m.subject, m.flags])).toEqual([
|
|
35
|
+
["Message 1", ["\\Seen"]],
|
|
36
|
+
]);
|
|
37
|
+
});
|
|
38
|
+
test("a second identical call changes nothing and is not an error", async () => {
|
|
39
|
+
const { createClient } = fakeClient(folders());
|
|
40
|
+
await trashEmails(createClient, { ids: [stable("m1")] });
|
|
41
|
+
const again = await trashEmails(createClient, { ids: [stable("m1")] });
|
|
42
|
+
expect(again).toEqual({
|
|
43
|
+
content: [
|
|
44
|
+
{
|
|
45
|
+
type: "text",
|
|
46
|
+
text: "Trashed 0 message(s). 1 message(s) already in Trash.",
|
|
47
|
+
},
|
|
48
|
+
],
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
test("without a Trash folder, says so and moves nothing", async () => {
|
|
52
|
+
const data = folders().slice(0, 1);
|
|
53
|
+
const { client, createClient } = fakeClient(data);
|
|
54
|
+
const result = await trashEmails(createClient, { ids: [stable("m1")] });
|
|
55
|
+
expect(result.isError).toBe(true);
|
|
56
|
+
expect(result.content[0].text).toMatch(/^This account has no Trash mailbox\. Nothing was trashed\./);
|
|
57
|
+
expect(client.commands).toEqual([]);
|
|
58
|
+
});
|
|
59
|
+
test("on Gmail, an archived message moves out of All Mail into Trash", async () => {
|
|
60
|
+
const data = [
|
|
61
|
+
{
|
|
62
|
+
path: "[Gmail]/All Mail",
|
|
63
|
+
specialUse: "\\All",
|
|
64
|
+
messages: [msg(7, "g1")],
|
|
65
|
+
},
|
|
66
|
+
{ path: "[Gmail]/Trash", specialUse: "\\Trash", messages: [] },
|
|
67
|
+
];
|
|
68
|
+
const { client, createClient } = fakeClient(data);
|
|
69
|
+
const result = await trashEmails(createClient, { ids: [stable("g1")] });
|
|
70
|
+
expect(result.content[0].text).toBe("Trashed 1 message(s).");
|
|
71
|
+
expect(client.commands).toEqual(["MOVE [Gmail]/All Mail 7 [Gmail]/Trash"]);
|
|
72
|
+
});
|
|
73
|
+
});
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "imap-mcp",
|
|
3
3
|
"mcpName": "io.github.langmail/imap-mcp",
|
|
4
4
|
"description": "An MCP server that lets your AI agent search, read and draft email in any IMAP mailbox.",
|
|
5
|
-
"version": "
|
|
5
|
+
"version": "2.0.0",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"bin": {
|