imap-mcp 1.1.0 → 1.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.
@@ -0,0 +1,298 @@
1
+ import { describe, expect, test, vi } from "vitest";
2
+ import { fakeClient } from "./fake-imap.js";
3
+ import { encodeMessageId, READ_ONLY, uidRanges } from "./imap.js";
4
+ import { queryTerms, registerSearchEmails, searchEmails, } from "./search-emails.js";
5
+ const gmail = () => [
6
+ {
7
+ path: "INBOX",
8
+ messages: [
9
+ {
10
+ uid: 1,
11
+ emailId: "m1",
12
+ date: "2026-11-01T09:00:00Z",
13
+ subject: "Invoice November",
14
+ from: "billing@example.com",
15
+ text: "Your invoice from Example Corp",
16
+ flags: ["$label:todo"],
17
+ },
18
+ {
19
+ uid: 2,
20
+ emailId: "m2",
21
+ date: "2026-11-02T09:00:00Z",
22
+ subject: "Lunch?",
23
+ from: "friend@example.org",
24
+ flags: ["\\Seen"],
25
+ },
26
+ ],
27
+ },
28
+ { path: "[Gmail]", flags: ["\\Noselect"], messages: [] },
29
+ {
30
+ path: "[Gmail]/Sent Mail",
31
+ specialUse: "\\Sent",
32
+ messages: [
33
+ {
34
+ uid: 7,
35
+ emailId: "m3",
36
+ date: "2026-11-03T09:00:00Z",
37
+ subject: "Re: Lunch?",
38
+ from: "me@example.com",
39
+ flags: ["\\Seen"],
40
+ },
41
+ ],
42
+ },
43
+ {
44
+ path: "Work",
45
+ messages: [
46
+ {
47
+ uid: 3,
48
+ emailId: "m1",
49
+ date: "2026-11-01T09:00:00Z",
50
+ subject: "Invoice November",
51
+ from: "billing@example.com",
52
+ text: "Your invoice from Example Corp",
53
+ flags: ["$label:todo"],
54
+ },
55
+ {
56
+ uid: 4,
57
+ emailId: "m4",
58
+ date: "2026-10-01T09:00:00Z",
59
+ subject: "Old invoice",
60
+ from: "billing@example.com",
61
+ flags: ["\\Flagged"],
62
+ bodyStructure: {
63
+ type: "multipart/mixed",
64
+ childNodes: [
65
+ { type: "text/plain", part: "1" },
66
+ {
67
+ type: "application/pdf",
68
+ part: "2",
69
+ disposition: "attachment",
70
+ dispositionParameters: { filename: "invoice.pdf" },
71
+ size: 2048,
72
+ },
73
+ {
74
+ type: "image/png",
75
+ part: "3",
76
+ disposition: "inline",
77
+ id: "<logo>",
78
+ dispositionParameters: { filename: "logo.png" },
79
+ size: 10,
80
+ },
81
+ ],
82
+ },
83
+ },
84
+ ],
85
+ },
86
+ ];
87
+ const search = (folders, args) => {
88
+ const fake = fakeClient(folders);
89
+ const result = searchEmails(fake.createClient, {
90
+ limit: 20,
91
+ ...args,
92
+ });
93
+ return { ...fake, result };
94
+ };
95
+ const text = async (p) => (await p).content[0].text;
96
+ describe("search_emails", () => {
97
+ test("registers as a read-only tool named search_emails", () => {
98
+ const server = { registerTool: vi.fn() };
99
+ registerSearchEmails(fakeClient([]).createClient, server);
100
+ expect(server.registerTool).toHaveBeenCalledWith("search_emails", expect.objectContaining({ annotations: READ_ONLY }), expect.any(Function));
101
+ });
102
+ test("requires at least one filter", async () => {
103
+ const server = { registerTool: vi.fn() };
104
+ registerSearchEmails(fakeClient([]).createClient, server);
105
+ const handler = server.registerTool.mock.calls[0][2];
106
+ const result = await handler({ limit: 20 });
107
+ expect(result.isError).toBe(true);
108
+ expect(result.content[0].text).toContain("Provide at least one filter");
109
+ });
110
+ test("read: false finds unread mail in every folder, merged by stable id, naming its folders", async () => {
111
+ const { client, result } = search(gmail(), { read: false });
112
+ const out = await text(result);
113
+ expect(out).toMatch(/^2 of 2 message\(s\) matched:/);
114
+ expect(out).toContain(`1. Invoice November\n id: ${encodeMessageId({ kind: "stable", emailId: "m1" })}\n from: billing@example.com\n date: 2026-11-01T09:00:00Z\n folders: Inbox, Work\n tags: todo\n flags: unread`);
115
+ expect(out).toContain("2. Old invoice");
116
+ expect(out).not.toContain("Lunch");
117
+ // \Noselect folders are never opened; every open is read-only
118
+ const opened = client.mailboxOpen.mock.calls;
119
+ expect(opened.map((c) => c[0])).not.toContain("[Gmail]");
120
+ expect(opened.every((c) => c[1]?.readOnly === true)).toBe(true);
121
+ });
122
+ test("Sent resolves through the special-use attribute", async () => {
123
+ const out = await text(search(gmail(), { mailbox: "Sent" }).result);
124
+ expect(out).toContain("Re: Lunch?");
125
+ expect(out).toContain("folders: Sent");
126
+ expect(out).not.toContain("Invoice");
127
+ });
128
+ test("any other mailbox matches a folder name exactly, and a miss lists the folders", async () => {
129
+ expect(await text(search(gmail(), { mailbox: "Work" }).result)).toContain("Old invoice");
130
+ const miss = await search(gmail(), { mailbox: "work" }).result;
131
+ expect(miss.isError).toBe(true);
132
+ expect(miss.content[0].text).toBe("Mailbox not found: work. Nothing was searched. Available folders: Inbox, Sent, Work.");
133
+ });
134
+ test("passes the flag and sender filters to IMAP SEARCH", async () => {
135
+ const { client, result } = search(gmail(), {
136
+ starred: true,
137
+ todo: false,
138
+ from: "billing",
139
+ mailbox: "Work",
140
+ });
141
+ expect(await text(result)).toContain("Old invoice");
142
+ expect(client.search).toHaveBeenCalledWith({ flagged: true, unKeyword: "$label:todo", from: "billing" }, { uid: true });
143
+ });
144
+ test("todo: true finds the to-do list", async () => {
145
+ const out = await text(search(gmail(), { todo: true }).result);
146
+ expect(out).toMatch(/^1 of 1 message/);
147
+ expect(out).toContain("Invoice November");
148
+ });
149
+ test("after and before bound the received time exactly", async () => {
150
+ const out = await text(search(gmail(), {
151
+ after: "2026-11-01T10:00:00Z",
152
+ before: "2026-11-03T09:00:00Z",
153
+ }).result);
154
+ expect(out).toMatch(/^1 of 1 message/);
155
+ expect(out).toContain("Lunch?");
156
+ });
157
+ test("lists attachments, hiding inline images", async () => {
158
+ const out = await text(search(gmail(), { starred: true }).result);
159
+ expect(out).toContain(" attachments:\n 1. invoice.pdf — application/pdf, 2.0 KB");
160
+ expect(out).not.toContain("logo.png");
161
+ });
162
+ test("ranks messages holding every term first and shows the terms matched", async () => {
163
+ const out = await text(search(gmail(), { query: "invoice lunch" }).result);
164
+ // every hit holds one term: newest first
165
+ expect(out.indexOf("Re: Lunch?")).toBeLessThan(out.indexOf("Old invoice"));
166
+ expect(out).toContain("matched: 1/2 terms");
167
+ const both = await text(search(gmail(), { query: 'invoice "example corp"' }).result);
168
+ expect(both.indexOf("Invoice November")).toBeLessThan(both.indexOf("Old invoice"));
169
+ expect(both).toContain("matched: 2/2 terms");
170
+ });
171
+ test("pages with a cursor, without skipping or repeating", async () => {
172
+ const first = await text(search(gmail(), { from: "e", limit: 2 }).result);
173
+ expect(first).toMatch(/^2 of 4 message\(s\) matched — 2 more not shown/);
174
+ const cursor = first.match(/cursor: "([^"]+)"/)[1];
175
+ const second = await text(search(gmail(), { from: "e", limit: 2, cursor }).result);
176
+ expect(second).toMatch(/^2 of 2 remaining message\(s\) matched:/);
177
+ expect(second).toContain("Invoice November");
178
+ expect(second).toContain("Old invoice");
179
+ expect(first).not.toContain("Invoice November");
180
+ const other = await search(gmail(), { from: "x", cursor }).result;
181
+ expect(other.content[0].text).toContain("cursor belongs to a search");
182
+ });
183
+ test("without a stable id, each folder's copy has its own id", async () => {
184
+ const folders = gmail().map((f) => ({
185
+ ...f,
186
+ messages: f.messages.map((m) => ({ ...m, emailId: undefined })),
187
+ }));
188
+ const out = await text(search(folders, { todo: true }).result);
189
+ expect(out).toMatch(/^2 of 2 message/);
190
+ expect(out).toContain(encodeMessageId({ kind: "uid", path: "Work", uidValidity: "1", uid: 3 }));
191
+ });
192
+ test("pages through hits received in the same second without skipping or repeating", async () => {
193
+ const folders = [
194
+ {
195
+ path: "INBOX",
196
+ messages: [1, 2, 3].map((uid) => ({
197
+ uid,
198
+ date: "2026-11-01T09:00:00Z",
199
+ subject: `Same second ${uid}`,
200
+ from: "a@example.com",
201
+ })),
202
+ },
203
+ ];
204
+ const seen = [];
205
+ let cursor;
206
+ do {
207
+ const out = await text(search(folders, { from: "a", limit: 1, cursor }).result);
208
+ seen.push(...(out.match(/Same second \d/g) ?? []));
209
+ cursor = out.match(/cursor: "([^"]+)"/)?.[1];
210
+ } while (cursor);
211
+ expect(seen.sort()).toEqual([
212
+ "Same second 1",
213
+ "Same second 2",
214
+ "Same second 3",
215
+ ]);
216
+ });
217
+ test("fetches hits with UID ranges, not one UID after another", async () => {
218
+ const { client, result } = search(gmail(), { mailbox: "Work" });
219
+ await result;
220
+ expect(client.fetch.mock.calls[0][0]).toBe("3:4");
221
+ expect(uidRanges([7, 1, 2, 3, 9, 10])).toBe("1:3,7,9:10");
222
+ });
223
+ test("a refused SEARCH is an error, not an empty result", async () => {
224
+ const fake = fakeClient(gmail());
225
+ fake.client.search.mockResolvedValueOnce(false);
226
+ await expect(searchEmails(fake.createClient, { limit: 20, read: false })).rejects.toThrow("The server refused the search in INBOX.");
227
+ });
228
+ test("skips a folder the account cannot open", async () => {
229
+ const fake = fakeClient(gmail());
230
+ const open = fake.client.mailboxOpen.getMockImplementation();
231
+ fake.client.mailboxOpen.mockImplementation(async (path, opts) => {
232
+ if (path === "Work")
233
+ throw new Error("NO [NOPERM]");
234
+ return open(path, opts);
235
+ });
236
+ const out = await text(searchEmails(fake.createClient, { limit: 20, todo: true }));
237
+ expect(out).toContain("folders: Inbox\n");
238
+ });
239
+ test("a mailbox the account cannot open is an error, not an empty result", async () => {
240
+ const fake = fakeClient(gmail());
241
+ const open = fake.client.mailboxOpen.getMockImplementation();
242
+ fake.client.mailboxOpen.mockImplementation(async (path, opts) => {
243
+ if (path === "Work")
244
+ throw new Error("NO [NOPERM]");
245
+ return open(path, opts);
246
+ });
247
+ const result = await searchEmails(fake.createClient, {
248
+ limit: 20,
249
+ mailbox: "Work",
250
+ });
251
+ expect(result.isError).toBe(true);
252
+ expect(result.content[0].text).toContain("Mailbox Work (Work) could not be opened, so nothing was searched.");
253
+ });
254
+ test("lists a forwarded message as one attachment", async () => {
255
+ const folders = [
256
+ {
257
+ path: "INBOX",
258
+ messages: [
259
+ {
260
+ uid: 1,
261
+ date: "2026-11-01T09:00:00Z",
262
+ subject: "Fwd: report",
263
+ from: "a@example.com",
264
+ bodyStructure: {
265
+ type: "multipart/mixed",
266
+ childNodes: [
267
+ { type: "text/plain", part: "1" },
268
+ {
269
+ type: "message/rfc822",
270
+ part: "2",
271
+ size: 3000,
272
+ childNodes: [
273
+ {
274
+ type: "application/pdf",
275
+ part: "2.1",
276
+ disposition: "attachment",
277
+ dispositionParameters: { filename: "inner.pdf" },
278
+ },
279
+ ],
280
+ },
281
+ ],
282
+ },
283
+ },
284
+ ],
285
+ },
286
+ ];
287
+ const out = await text(search(folders, { from: "a" }).result);
288
+ expect(out).toContain("1. (unnamed) — message/rfc822, 2.9 KB");
289
+ expect(out).not.toContain("inner.pdf");
290
+ });
291
+ test("splits a query into words and quoted phrases", () => {
292
+ expect(queryTerms('invoice "example corp" 2026')).toEqual([
293
+ "invoice",
294
+ "example corp",
295
+ "2026",
296
+ ]);
297
+ });
298
+ });
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "imap-mcp",
3
3
  "mcpName": "io.github.langmail/imap-mcp",
4
- "description": "An MCP server that lets your AI agent save email drafts to any IMAP mailbox.",
5
- "version": "1.1.0",
4
+ "description": "An MCP server that lets your AI agent search, read and draft email in any IMAP mailbox.",
5
+ "version": "1.3.0",
6
6
  "main": "dist/index.js",
7
7
  "type": "module",
8
8
  "bin": {
@@ -39,6 +39,7 @@
39
39
  "@modelcontextprotocol/sdk": "^1.32.2",
40
40
  "dotenv": "^16.5.0",
41
41
  "imapflow": "^2.12.2",
42
+ "langmail": "^0.11.2",
42
43
  "zod": "^3.25.67"
43
44
  },
44
45
  "devDependencies": {