imap-mcp 1.4.0 → 1.5.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 +87 -6
- package/dist/access.js +5 -1
- package/dist/index.js +8 -0
- package/dist/index.test.js +20 -0
- 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
package/README.md
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
|
|
12
12
|
<p align="center">
|
|
13
13
|
<b>An MCP server that lets your AI agent search, read and draft email in any IMAP mailbox.</b><br>
|
|
14
|
-
Your agent finds and reads mail in every folder with <code>search_emails</code>, <code>get_email</code> and <code>get_thread</code>, and saves drafts for you to review and send with <code>create_draft</code>. It works with any IMAP server that accepts a password login and with any MCP client that starts local servers, such as Claude Desktop, Claude Code or Cursor.
|
|
14
|
+
Your agent finds and reads mail in every folder with <code>search_emails</code>, <code>get_email</code> and <code>get_thread</code>, files them with <code>archive_emails</code>, <code>move_emails</code> and <code>trash_emails</code>, marks them with <code>mark_emails</code>, and saves drafts for you to review and send with <code>create_draft</code>. It works with any IMAP server that accepts a password login and with any MCP client that starts local servers, such as Claude Desktop, Claude Code or Cursor.
|
|
15
15
|
</p>
|
|
16
16
|
|
|
17
17
|
<p align="center">
|
|
@@ -76,11 +76,11 @@ node dist/index.js
|
|
|
76
76
|
|
|
77
77
|
`--access` or `MCP_ACCESS` sets what the agent may do through imap-mcp. The flag wins when both are set; changing the level needs a restart of the server.
|
|
78
78
|
|
|
79
|
-
| Level | The agent may | Tools
|
|
80
|
-
| ---------------------- | --------------------------------------------------------------- |
|
|
81
|
-
| `read-only` | Read the mailbox | `search_emails`, `get_email`, `get_thread`
|
|
82
|
-
| `no-send` | Also change the mailbox, but send nothing and reach no one else | `search_emails`, `get_email`, `get_thread`, `create_draft` |
|
|
83
|
-
| `everything` (default) | Also reach other people | `search_emails`, `get_email`, `get_thread`, `create_draft`, `unsubscribe_from_list` |
|
|
79
|
+
| Level | The agent may | Tools |
|
|
80
|
+
| ---------------------- | --------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
81
|
+
| `read-only` | Read the mailbox | `search_emails`, `get_email`, `get_thread` |
|
|
82
|
+
| `no-send` | Also change the mailbox, but send nothing and reach no one else | `search_emails`, `get_email`, `get_thread`, `mark_emails`, `archive_emails`, `move_emails`, `trash_emails`, `create_draft` |
|
|
83
|
+
| `everything` (default) | Also reach other people | `search_emails`, `get_email`, `get_thread`, `mark_emails`, `archive_emails`, `move_emails`, `trash_emails`, `create_draft`, `unsubscribe_from_list` |
|
|
84
84
|
|
|
85
85
|
A tool outside the level is left out of the tool list, and a call to it is refused without changing anything. An unknown value stops the server at start with exit code 2. The level is logged on stderr at start.
|
|
86
86
|
|
|
@@ -158,6 +158,87 @@ Lists every message of a message's conversation, oldest first, in every folder,
|
|
|
158
158
|
}
|
|
159
159
|
```
|
|
160
160
|
|
|
161
|
+
### `mark_emails`
|
|
162
|
+
|
|
163
|
+
Access level: `no-send`.
|
|
164
|
+
|
|
165
|
+
Sets or clears read, to-do and starred on one or more messages. Read is the `\Seen` flag, starred `\Flagged`, and to-do the `$label:todo` keyword [Langmail](https://langmail.me/?ref=imap-mcp) sets, so both see the same to-dos; a server that keeps no custom keywords cannot store the to-do. A flag left out stays as it was, and nothing moves. Reports how many messages changed and names each id it could not change; re-running it changes nothing.
|
|
166
|
+
|
|
167
|
+
**Parameters** (at least one flag is required):
|
|
168
|
+
|
|
169
|
+
- `ids` (string array, required): 1–50 message ids from `search_emails` or `get_thread`
|
|
170
|
+
- `read` (boolean, optional): `true` marks read, `false` unread
|
|
171
|
+
- `starred` (boolean, optional): `true` stars, `false` removes the star
|
|
172
|
+
- `todo` (boolean, optional): `true` flags as a to-do, `false` clears it
|
|
173
|
+
|
|
174
|
+
**Example:**
|
|
175
|
+
|
|
176
|
+
```json
|
|
177
|
+
{
|
|
178
|
+
"ids": ["e.1784212345678901234"],
|
|
179
|
+
"read": true,
|
|
180
|
+
"todo": false
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### `archive_emails`
|
|
185
|
+
|
|
186
|
+
Access level: `no-send`.
|
|
187
|
+
|
|
188
|
+
Takes messages out of the Inbox, Junk and Trash and into the folder your server marks as Archive (RFC 6154); other folders they are in, such as Sent, stay as they are. On Gmail, which has no Archive folder, the message leaves the Inbox and stays in All Mail. Without an Archive folder the tool says so and creates none. Read state and flags are kept. Reports how many messages it archived and names each id it could not archive; re-running it changes nothing.
|
|
189
|
+
|
|
190
|
+
**Parameters:**
|
|
191
|
+
|
|
192
|
+
- `ids` (string array, required): 1–50 message ids from `search_emails` or `get_thread`
|
|
193
|
+
|
|
194
|
+
**Example:**
|
|
195
|
+
|
|
196
|
+
```json
|
|
197
|
+
{
|
|
198
|
+
"ids": ["e.1784212345678901234"]
|
|
199
|
+
}
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### `move_emails`
|
|
203
|
+
|
|
204
|
+
Access level: `no-send`.
|
|
205
|
+
|
|
206
|
+
Moves messages into a folder, out of every folder they were in (on Gmail they also stay in All Mail). The folder is named like the `mailbox` filter of `search_emails`; a folder that does not exist is an error that lists the folders there are, and nothing is created. Trash is refused: use `trash_emails`. Read state and flags are kept. Reports how many messages moved and names each id it could not move; re-running it changes nothing.
|
|
207
|
+
|
|
208
|
+
**Parameters:**
|
|
209
|
+
|
|
210
|
+
- `ids` (string array, required): 1–50 message ids from `search_emails` or `get_thread`
|
|
211
|
+
- `mailbox` (string, required): The destination folder, such as `Projects` or `Inbox`
|
|
212
|
+
|
|
213
|
+
**Example:**
|
|
214
|
+
|
|
215
|
+
```json
|
|
216
|
+
{
|
|
217
|
+
"ids": ["e.1784212345678901234"],
|
|
218
|
+
"mailbox": "Projects"
|
|
219
|
+
}
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
### `trash_emails`
|
|
223
|
+
|
|
224
|
+
Access level: `no-send`. Annotated as destructive, so clients may ask before running it.
|
|
225
|
+
|
|
226
|
+
Moves messages into the folder your server marks as Trash (RFC 6154), where they stay recoverable until Trash is emptied. There is no permanent delete. Reports how many messages it trashed and names each id it could not trash; re-running it changes nothing.
|
|
227
|
+
|
|
228
|
+
**Parameters:**
|
|
229
|
+
|
|
230
|
+
- `ids` (string array, required): 1–50 message ids from `search_emails` or `get_thread`
|
|
231
|
+
|
|
232
|
+
**Example:**
|
|
233
|
+
|
|
234
|
+
```json
|
|
235
|
+
{
|
|
236
|
+
"ids": ["e.1784212345678901234"]
|
|
237
|
+
}
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
On servers without MOVE (RFC 6851), the three moving tools copy each message, mark it `\Deleted` and expunge only those messages by UID (UIDPLUS, RFC 4315). A server with neither leaves a folder alone that holds other deleted messages, and the tool names those ids. On servers without a stable message id, a moved message gets a new id: search again before acting on it.
|
|
241
|
+
|
|
161
242
|
### `create_draft`
|
|
162
243
|
|
|
163
244
|
Access level: `no-send`.
|
package/dist/access.js
CHANGED
|
@@ -24,10 +24,14 @@ const DEFAULT_ACCESS = "everything";
|
|
|
24
24
|
// The level of every tool. Registering a tool that has none here fails, so
|
|
25
25
|
// every test that builds the server fails until it gets one.
|
|
26
26
|
export const TOOL_ACCESS = {
|
|
27
|
+
archive_emails: "no-send",
|
|
27
28
|
create_draft: "no-send",
|
|
28
29
|
get_email: "read-only",
|
|
29
30
|
get_thread: "read-only",
|
|
31
|
+
mark_emails: "no-send",
|
|
32
|
+
move_emails: "no-send",
|
|
30
33
|
search_emails: "read-only",
|
|
34
|
+
trash_emails: "no-send",
|
|
31
35
|
unsubscribe_from_list: "everything",
|
|
32
36
|
};
|
|
33
37
|
const NAMES = {
|
|
@@ -37,7 +41,7 @@ const NAMES = {
|
|
|
37
41
|
};
|
|
38
42
|
const ALLOWS = {
|
|
39
43
|
everything: "",
|
|
40
|
-
"no-send": ", which lets it change the mailbox, such as saving drafts, but not send mail or reach anyone else",
|
|
44
|
+
"no-send": ", which lets it change the mailbox, such as filing mail and saving drafts, but not send mail or reach anyone else",
|
|
41
45
|
"read-only": ", which lets it read the mailbox but not change or send anything",
|
|
42
46
|
};
|
|
43
47
|
const isAccessLevel = (raw) => ACCESS_LEVELS.includes(raw);
|
package/dist/index.js
CHANGED
|
@@ -7,10 +7,14 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
|
7
7
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
8
8
|
import { parseAccess, restrictToAccess } from "./access.js";
|
|
9
9
|
import { buildInstructions } from "./instructions.js";
|
|
10
|
+
import { registerArchiveEmails } from "./tools/archive-emails.js";
|
|
10
11
|
import { registerCreateDraft } from "./tools/create-draft.js";
|
|
11
12
|
import { registerGetEmail } from "./tools/get-email.js";
|
|
12
13
|
import { registerGetThread } from "./tools/get-thread.js";
|
|
14
|
+
import { registerMarkEmails } from "./tools/mark-emails.js";
|
|
15
|
+
import { registerMoveEmails } from "./tools/move-emails.js";
|
|
13
16
|
import { registerSearchEmails } from "./tools/search-emails.js";
|
|
17
|
+
import { registerTrashEmails } from "./tools/trash-emails.js";
|
|
14
18
|
import { registerUnsubscribeFromList } from "./tools/unsubscribe-from-list.js";
|
|
15
19
|
export async function main() {
|
|
16
20
|
const access = parseAccess(process.argv.slice(2), process.env);
|
|
@@ -42,6 +46,10 @@ export async function main() {
|
|
|
42
46
|
registerSearchEmails(createClient, server);
|
|
43
47
|
registerGetEmail(createClient, server);
|
|
44
48
|
registerGetThread(createClient, server);
|
|
49
|
+
registerMarkEmails(createClient, server);
|
|
50
|
+
registerArchiveEmails(createClient, server);
|
|
51
|
+
registerMoveEmails(createClient, server);
|
|
52
|
+
registerTrashEmails(createClient, server);
|
|
45
53
|
registerUnsubscribeFromList(createClient, server);
|
|
46
54
|
// Start receiving messages on stdin and sending messages on stdout
|
|
47
55
|
const transport = new StdioServerTransport();
|
package/dist/index.test.js
CHANGED
|
@@ -23,6 +23,18 @@ vi.mock("./tools/get-email.js", () => ({
|
|
|
23
23
|
vi.mock("./tools/get-thread.js", () => ({
|
|
24
24
|
registerGetThread: vi.fn(),
|
|
25
25
|
}));
|
|
26
|
+
vi.mock("./tools/mark-emails.js", () => ({
|
|
27
|
+
registerMarkEmails: vi.fn(),
|
|
28
|
+
}));
|
|
29
|
+
vi.mock("./tools/archive-emails.js", () => ({
|
|
30
|
+
registerArchiveEmails: vi.fn(),
|
|
31
|
+
}));
|
|
32
|
+
vi.mock("./tools/move-emails.js", () => ({
|
|
33
|
+
registerMoveEmails: vi.fn(),
|
|
34
|
+
}));
|
|
35
|
+
vi.mock("./tools/trash-emails.js", () => ({
|
|
36
|
+
registerTrashEmails: vi.fn(),
|
|
37
|
+
}));
|
|
26
38
|
vi.mock("./tools/unsubscribe-from-list.js", () => ({
|
|
27
39
|
registerUnsubscribeFromList: vi.fn(),
|
|
28
40
|
}));
|
|
@@ -34,6 +46,10 @@ import { registerCreateDraft } from "./tools/create-draft.js";
|
|
|
34
46
|
import { registerSearchEmails } from "./tools/search-emails.js";
|
|
35
47
|
import { registerGetEmail } from "./tools/get-email.js";
|
|
36
48
|
import { registerGetThread } from "./tools/get-thread.js";
|
|
49
|
+
import { registerMarkEmails } from "./tools/mark-emails.js";
|
|
50
|
+
import { registerArchiveEmails } from "./tools/archive-emails.js";
|
|
51
|
+
import { registerMoveEmails } from "./tools/move-emails.js";
|
|
52
|
+
import { registerTrashEmails } from "./tools/trash-emails.js";
|
|
37
53
|
import { registerUnsubscribeFromList } from "./tools/unsubscribe-from-list.js";
|
|
38
54
|
import { buildInstructions } from "./instructions.js";
|
|
39
55
|
describe("main", () => {
|
|
@@ -77,6 +93,10 @@ describe("main", () => {
|
|
|
77
93
|
expect(vi.mocked(registerSearchEmails).mock.calls[0][0]).toBe(createClient);
|
|
78
94
|
expect(vi.mocked(registerGetEmail).mock.calls[0][0]).toBe(createClient);
|
|
79
95
|
expect(vi.mocked(registerGetThread).mock.calls[0][0]).toBe(createClient);
|
|
96
|
+
expect(vi.mocked(registerMarkEmails).mock.calls[0][0]).toBe(createClient);
|
|
97
|
+
expect(vi.mocked(registerArchiveEmails).mock.calls[0][0]).toBe(createClient);
|
|
98
|
+
expect(vi.mocked(registerMoveEmails).mock.calls[0][0]).toBe(createClient);
|
|
99
|
+
expect(vi.mocked(registerTrashEmails).mock.calls[0][0]).toBe(createClient);
|
|
80
100
|
expect(vi.mocked(registerUnsubscribeFromList).mock.calls[0][0]).toBe(createClient);
|
|
81
101
|
// Verify each tool call gets an IMAP client built from the environment
|
|
82
102
|
createClient();
|
package/dist/instructions.js
CHANGED
|
@@ -8,11 +8,13 @@
|
|
|
8
8
|
// level offers: instructions.test.ts checks both.
|
|
9
9
|
const INTRO = "These tools reach the user’s own mailbox on their IMAP server. Every tool acts on that one account.";
|
|
10
10
|
const MAIL = "- Mail: search_emails finds messages (read: false finds everything not read yet, in every folder; todo: true finds the to-do list; each hit names its folders). A hit in Junk is mail the server filtered as spam. Read one with get_email or a whole conversation with get_thread. Transactional mail is titled after what it is about (a ticket after its route, an invoice after its vendor), so search for those words with a date window.";
|
|
11
|
+
const FILING = "- Filing: archive_emails takes mail out of the Inbox, Junk and Trash and keeps it; trash_emails moves it to Trash, where it stays recoverable; move_emails files it into a named folder.";
|
|
11
12
|
const WRITING = [
|
|
12
13
|
"Writing mail:",
|
|
13
14
|
"- create_draft stages a draft in the Drafts folder for the user to review and send themselves.",
|
|
14
15
|
"",
|
|
15
16
|
];
|
|
17
|
+
const MARK_READ = "Reading mail never marks it read. Use mark_emails when the user wants it marked read, flagged as a to-do, or starred.";
|
|
16
18
|
// langmail-mcp's confirm-first line, for the one tool here that reaches
|
|
17
19
|
// someone outside the mailbox.
|
|
18
20
|
const CONFIRM = "Confirm first: unsubscribing the user from a mailing list (unsubscribe_from_list) cannot be undone. Confirm with the user which sender it is first, unless they have already asked for exactly that.";
|
|
@@ -23,10 +25,11 @@ export function buildInstructions(level) {
|
|
|
23
25
|
"",
|
|
24
26
|
"Where to start:",
|
|
25
27
|
MAIL,
|
|
26
|
-
"",
|
|
27
28
|
// Read only offers no tool that writes.
|
|
29
|
+
...(level === "read-only" ? [] : [FILING]),
|
|
30
|
+
"",
|
|
28
31
|
...(level === "read-only" ? [] : WRITING),
|
|
29
|
-
"Reading mail never marks it read.",
|
|
32
|
+
level === "read-only" ? "Reading mail never marks it read." : MARK_READ,
|
|
30
33
|
"",
|
|
31
34
|
// Only Everything offers a tool that reaches other people.
|
|
32
35
|
...(level === "everything" ? [CONFIRM, ""] : []),
|
|
@@ -5,6 +5,10 @@ import { registerCreateDraft } from "./tools/create-draft.js";
|
|
|
5
5
|
import { registerGetEmail } from "./tools/get-email.js";
|
|
6
6
|
import { registerGetThread } from "./tools/get-thread.js";
|
|
7
7
|
import { registerSearchEmails } from "./tools/search-emails.js";
|
|
8
|
+
import { registerMarkEmails } from "./tools/mark-emails.js";
|
|
9
|
+
import { registerArchiveEmails } from "./tools/archive-emails.js";
|
|
10
|
+
import { registerMoveEmails } from "./tools/move-emails.js";
|
|
11
|
+
import { registerTrashEmails } from "./tools/trash-emails.js";
|
|
8
12
|
import { registerUnsubscribeFromList } from "./tools/unsubscribe-from-list.js";
|
|
9
13
|
function registeredTools() {
|
|
10
14
|
const names = [];
|
|
@@ -17,6 +21,10 @@ function registeredTools() {
|
|
|
17
21
|
registerSearchEmails(createClient, server);
|
|
18
22
|
registerGetEmail(createClient, server);
|
|
19
23
|
registerGetThread(createClient, server);
|
|
24
|
+
registerMarkEmails(createClient, server);
|
|
25
|
+
registerArchiveEmails(createClient, server);
|
|
26
|
+
registerMoveEmails(createClient, server);
|
|
27
|
+
registerTrashEmails(createClient, server);
|
|
20
28
|
registerUnsubscribeFromList(createClient, server);
|
|
21
29
|
return names;
|
|
22
30
|
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// archive_emails: takes messages out of the places mail waits in before it
|
|
2
|
+
// is handled (Inbox, Junk, Trash) and into the Archive folder, leaving every
|
|
3
|
+
// other folder they are in alone. Shaped like langmail-mcp's archive_emails;
|
|
4
|
+
// the differences are in the description.
|
|
5
|
+
import { copiesOutside, fileMessages, formatBatchResult, idsShape, isAllMail, locateMessages, uniqueIds, } from "./batch.js";
|
|
6
|
+
import { ACCOUNT_WRITE, errorResult, findPlace, placeOf, selectableFolders, withClient, } from "./imap.js";
|
|
7
|
+
const WAITING = ["Inbox", "Junk", "Trash"];
|
|
8
|
+
/**
|
|
9
|
+
* The folder marked Archive, or on Gmail, which has none, All Mail: moving a
|
|
10
|
+
* message there takes it out of the Inbox and keeps it.
|
|
11
|
+
*/
|
|
12
|
+
function archiveFolder(client, folders) {
|
|
13
|
+
return (findPlace(folders, "Archive") ??
|
|
14
|
+
(client.capabilities.has("X-GM-EXT-1")
|
|
15
|
+
? folders.find(isAllMail)
|
|
16
|
+
: undefined));
|
|
17
|
+
}
|
|
18
|
+
export async function archiveEmails(createClient, args) {
|
|
19
|
+
const ids = uniqueIds(args.ids);
|
|
20
|
+
return withClient(createClient, async (client) => {
|
|
21
|
+
const archive = archiveFolder(client, await selectableFolders(client));
|
|
22
|
+
if (!archive) {
|
|
23
|
+
// Never created here: a model-driven call must not invent folders.
|
|
24
|
+
return errorResult("This account has no Archive mailbox. Nothing was archived. Create one in the mail client, " +
|
|
25
|
+
"or use move_emails with an existing folder.");
|
|
26
|
+
}
|
|
27
|
+
const located = await locateMessages(client, ids);
|
|
28
|
+
const outside = copiesOutside(archive);
|
|
29
|
+
const outcomes = await fileMessages(client, ids, located, archive, (locs) => outside(locs).filter((l) => WAITING.includes(placeOf(l.folder)?.name ?? "")));
|
|
30
|
+
return formatBatchResult("Archived", outcomes, {
|
|
31
|
+
already: "already out of the Inbox, Junk and Trash",
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
export const ARCHIVE_EMAILS_DESCRIPTION = "Archive one or more messages: take them out of the Inbox, Junk and Trash and file them in the " +
|
|
36
|
+
"Archive mailbox; Sent and any other folder they are in stay as they are, and a message in none of " +
|
|
37
|
+
"the three is left where it is. On Gmail, which has no Archive folder, archiving takes the message " +
|
|
38
|
+
"out of the Inbox and keeps it in All Mail. Use this when the user " +
|
|
39
|
+
'says archive, clear, or "get this out of my inbox". A message in Junk is spam: archive it only ' +
|
|
40
|
+
"when the user says it is not spam. " +
|
|
41
|
+
"Read state and flags (to-do, starred) are untouched (use mark_emails for those), and " +
|
|
42
|
+
"re-running it on the same ids is harmless. Takes message ids " +
|
|
43
|
+
"from search_emails or get_thread; pass all of a thread's ids to archive the conversation. On " +
|
|
44
|
+
"servers without a stable message id (OBJECTID or Gmail) an archived message gets a new id, so " +
|
|
45
|
+
"search again before acting on it once more. On a server with neither MOVE nor UIDPLUS, a folder holding other deleted messages is left alone and its ids are listed. " +
|
|
46
|
+
"To file into a specific folder use move_emails; to delete use trash_emails. Reports how " +
|
|
47
|
+
"many messages were archived and lists any id that could not be archived, with the reason.";
|
|
48
|
+
export function registerArchiveEmails(createClient, server) {
|
|
49
|
+
server.registerTool("archive_emails", {
|
|
50
|
+
title: "Archive emails",
|
|
51
|
+
description: ARCHIVE_EMAILS_DESCRIPTION,
|
|
52
|
+
inputSchema: idsShape,
|
|
53
|
+
annotations: ACCOUNT_WRITE,
|
|
54
|
+
}, async (args) => archiveEmails(createClient, args));
|
|
55
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
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 { archiveEmails, registerArchiveEmails } from "./archive-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
|
+
uidValidity: 9,
|
|
17
|
+
messages: [msg(1, "m1", ["\\Seen", "$label:todo"]), msg(2)],
|
|
18
|
+
},
|
|
19
|
+
{ path: "Archive", specialUse: "\\Archive", messages: [] },
|
|
20
|
+
{ path: "Junk", specialUse: "\\Junk", messages: [msg(3, "m3")] },
|
|
21
|
+
{ path: "Projects", messages: [msg(4, "m4")] },
|
|
22
|
+
];
|
|
23
|
+
const stable = (emailId) => encodeMessageId({ kind: "stable", emailId });
|
|
24
|
+
const byUid = (path, uid, uidValidity = "9") => encodeMessageId({ kind: "uid", path, uidValidity, uid });
|
|
25
|
+
const subjects = (f) => f?.messages.map((m) => m.subject);
|
|
26
|
+
describe("archive_emails", () => {
|
|
27
|
+
test("registers as an account write named archive_emails", () => {
|
|
28
|
+
const server = { registerTool: vi.fn() };
|
|
29
|
+
registerArchiveEmails(fakeClient([]).createClient, server);
|
|
30
|
+
expect(server.registerTool).toHaveBeenCalledWith("archive_emails", expect.objectContaining({ annotations: ACCOUNT_WRITE }), expect.any(Function));
|
|
31
|
+
});
|
|
32
|
+
test("moves messages out of the Inbox and Junk into Archive, keeping their flags", async () => {
|
|
33
|
+
const data = folders();
|
|
34
|
+
const { createClient } = fakeClient(data);
|
|
35
|
+
const result = await archiveEmails(createClient, {
|
|
36
|
+
ids: [stable("m1"), stable("m3")],
|
|
37
|
+
});
|
|
38
|
+
expect(result).toEqual({
|
|
39
|
+
content: [{ type: "text", text: "Archived 2 message(s)." }],
|
|
40
|
+
});
|
|
41
|
+
expect(subjects(data[0])).toEqual(["Message 2"]);
|
|
42
|
+
expect(subjects(data[2])).toEqual([]);
|
|
43
|
+
expect(data[1].messages.map((m) => [m.subject, m.flags])).toEqual([
|
|
44
|
+
["Message 1", ["\\Seen", "$label:todo"]],
|
|
45
|
+
["Message 3", []],
|
|
46
|
+
]);
|
|
47
|
+
});
|
|
48
|
+
test("a second identical call changes nothing and is not an error", async () => {
|
|
49
|
+
const data = folders();
|
|
50
|
+
const { createClient } = fakeClient(data);
|
|
51
|
+
await archiveEmails(createClient, { ids: [stable("m1")] });
|
|
52
|
+
const again = await archiveEmails(createClient, { ids: [stable("m1")] });
|
|
53
|
+
expect(again).toEqual({
|
|
54
|
+
content: [
|
|
55
|
+
{
|
|
56
|
+
type: "text",
|
|
57
|
+
text: "Archived 0 message(s). 1 message(s) already out of the Inbox, Junk and Trash.",
|
|
58
|
+
},
|
|
59
|
+
],
|
|
60
|
+
});
|
|
61
|
+
expect(subjects(data[1])).toEqual(["Message 1"]);
|
|
62
|
+
});
|
|
63
|
+
test("leaves a message in another folder where it is", async () => {
|
|
64
|
+
const data = folders();
|
|
65
|
+
const { client, createClient } = fakeClient(data);
|
|
66
|
+
const result = await archiveEmails(createClient, { ids: [stable("m4")] });
|
|
67
|
+
expect(result.content[0].text).toBe("Archived 0 message(s). 1 message(s) already out of the Inbox, Junk and Trash.");
|
|
68
|
+
expect(client.messageMove).not.toHaveBeenCalled();
|
|
69
|
+
});
|
|
70
|
+
test("a UID id from before the move is listed as not found, not an error", async () => {
|
|
71
|
+
const data = folders();
|
|
72
|
+
const { createClient } = fakeClient(data);
|
|
73
|
+
const id = byUid("INBOX", 2);
|
|
74
|
+
expect((await archiveEmails(createClient, { ids: [id] })).content[0].text).toBe("Archived 1 message(s).");
|
|
75
|
+
const again = await archiveEmails(createClient, { ids: [id] });
|
|
76
|
+
expect(again.isError).toBeUndefined();
|
|
77
|
+
expect(again.content[0].text).toBe(`Archived 0 message(s). Not updated (1): ${id} (not found). Re-run search_emails or get_thread for current ids.`);
|
|
78
|
+
});
|
|
79
|
+
test("an id that is not a message id is refused", async () => {
|
|
80
|
+
const { createClient } = fakeClient(folders());
|
|
81
|
+
const result = await archiveEmails(createClient, { ids: ["nope"] });
|
|
82
|
+
expect(result.isError).toBe(true);
|
|
83
|
+
expect(result.content[0].text).toBe("Archived 0 message(s). Not updated (1): nope (not a message id). Re-run search_emails or get_thread for current ids.");
|
|
84
|
+
});
|
|
85
|
+
test("on Gmail, takes the message out of the Inbox into All Mail", async () => {
|
|
86
|
+
const data = [
|
|
87
|
+
{ path: "INBOX", messages: [msg(1, "g1")] },
|
|
88
|
+
{
|
|
89
|
+
path: "[Gmail]/All Mail",
|
|
90
|
+
specialUse: "\\All",
|
|
91
|
+
messages: [msg(7, "g1")],
|
|
92
|
+
},
|
|
93
|
+
{ path: "Work", messages: [msg(2, "g1")] },
|
|
94
|
+
];
|
|
95
|
+
const { client, createClient } = fakeClient(data, [
|
|
96
|
+
"MOVE",
|
|
97
|
+
"UIDPLUS",
|
|
98
|
+
"X-GM-EXT-1",
|
|
99
|
+
]);
|
|
100
|
+
const result = await archiveEmails(createClient, { ids: [stable("g1")] });
|
|
101
|
+
expect(result.content[0].text).toBe("Archived 1 message(s).");
|
|
102
|
+
expect(client.commands).toEqual(["MOVE INBOX 1 [Gmail]/All Mail"]);
|
|
103
|
+
});
|
|
104
|
+
test("without an Archive folder, says so and creates none", async () => {
|
|
105
|
+
const data = folders().filter((f) => f.path !== "Archive");
|
|
106
|
+
const { client, createClient } = fakeClient(data);
|
|
107
|
+
const result = await archiveEmails(createClient, { ids: [stable("m1")] });
|
|
108
|
+
expect(result).toEqual({
|
|
109
|
+
content: [
|
|
110
|
+
{
|
|
111
|
+
type: "text",
|
|
112
|
+
text: "This account has no Archive mailbox. Nothing was archived. Create one in the mail client, or use move_emails with an existing folder.",
|
|
113
|
+
},
|
|
114
|
+
],
|
|
115
|
+
isError: true,
|
|
116
|
+
});
|
|
117
|
+
expect(client.commands).toEqual([]);
|
|
118
|
+
expect(data.map((f) => f.path)).toEqual(["INBOX", "Junk", "Projects"]);
|
|
119
|
+
});
|
|
120
|
+
test("without MOVE, copies and expunges only those UIDs (UIDPLUS)", async () => {
|
|
121
|
+
const data = folders();
|
|
122
|
+
data[0].messages.push(msg(5, undefined, ["\\Deleted"]));
|
|
123
|
+
const { client, createClient } = fakeClient(data, ["UIDPLUS", "OBJECTID"]);
|
|
124
|
+
const result = await archiveEmails(createClient, { ids: [stable("m1")] });
|
|
125
|
+
expect(result.content[0].text).toBe("Archived 1 message(s).");
|
|
126
|
+
expect(client.commands).toEqual([
|
|
127
|
+
"COPY INBOX 1 Archive",
|
|
128
|
+
"UID EXPUNGE INBOX",
|
|
129
|
+
]);
|
|
130
|
+
// The other \Deleted message is still there
|
|
131
|
+
expect(subjects(data[0])).toEqual(["Message 2", "Message 5"]);
|
|
132
|
+
expect(subjects(data[1])).toEqual(["Message 1"]);
|
|
133
|
+
});
|
|
134
|
+
test("without MOVE and UIDPLUS, leaves a folder with other deleted messages alone", async () => {
|
|
135
|
+
const data = folders();
|
|
136
|
+
data[0].messages.push(msg(5, undefined, ["\\Deleted"]));
|
|
137
|
+
const { client, createClient } = fakeClient(data, ["OBJECTID"]);
|
|
138
|
+
const result = await archiveEmails(createClient, { ids: [stable("m1")] });
|
|
139
|
+
expect(result.isError).toBe(true);
|
|
140
|
+
expect(result.content[0].text).toContain("(the server cannot move without expunging the other deleted messages in INBOX)");
|
|
141
|
+
expect(client.commands).toEqual([]);
|
|
142
|
+
expect(subjects(data[0])).toEqual(["Message 1", "Message 2", "Message 5"]);
|
|
143
|
+
});
|
|
144
|
+
test("a folder that refuses the move names its ids, and the rest still move", async () => {
|
|
145
|
+
const data = folders();
|
|
146
|
+
data[2].refuseWrites = true;
|
|
147
|
+
const { createClient } = fakeClient(data);
|
|
148
|
+
const result = await archiveEmails(createClient, {
|
|
149
|
+
ids: [stable("m1"), stable("m3")],
|
|
150
|
+
});
|
|
151
|
+
expect(result.isError).toBeUndefined();
|
|
152
|
+
expect(result.content[0].text).toBe(`Archived 1 message(s). Not updated (1): ${stable("m3")} (the server refused the move out of Junk).`);
|
|
153
|
+
});
|
|
154
|
+
});
|