imap-mcp 0.1.1 → 0.1.2

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 CHANGED
@@ -78,7 +78,7 @@ Creates a draft email message and saves it to the IMAP server's drafts folder.
78
78
  }
79
79
  ```
80
80
 
81
- The tool will attempt to save the draft to either "INBOX.Drafts" or "Drafts" folder, depending on your email server's folder structure.
81
+ The tool saves the draft to the folder your server marks as the drafts folder (the `\Drafts` special-use attribute, RFC 6154), such as `[Gmail]/Drafts` on Gmail. If the server marks no folder that way, it tries `INBOX.Drafts`, then `Drafts`.
82
82
 
83
83
  ## License
84
84
 
@@ -8,20 +8,36 @@ export function registerCreateDraft(client, server) {
8
8
  }, async ({ to, subject, body, from }) => {
9
9
  return new Promise((resolve, reject) => {
10
10
  client.once("ready", () => {
11
- client.openBox("INBOX.Drafts", false, (err) => {
12
- if (err) {
13
- // Try opening "Drafts" folder instead
14
- client.openBox("Drafts", false, (err2) => {
15
- if (err2) {
16
- reject(new Error(`Failed to open drafts folder: ${err2.message}`));
11
+ client.getBoxes((err, boxes) => {
12
+ const draftsPath = err ? undefined : findDraftsFolder(boxes);
13
+ if (draftsPath) {
14
+ client.openBox(draftsPath, false, (err) => {
15
+ if (err) {
16
+ reject(new Error(`Failed to open drafts folder: ${err.message}`));
17
17
  return;
18
18
  }
19
19
  createDraftMessage();
20
20
  });
21
21
  return;
22
22
  }
23
- createDraftMessage();
23
+ openFallbackDraftsFolder();
24
24
  });
25
+ function openFallbackDraftsFolder() {
26
+ client.openBox("INBOX.Drafts", false, (err) => {
27
+ if (err) {
28
+ // Try opening "Drafts" folder instead
29
+ client.openBox("Drafts", false, (err2) => {
30
+ if (err2) {
31
+ reject(new Error(`Failed to open drafts folder: ${err2.message}`));
32
+ return;
33
+ }
34
+ createDraftMessage();
35
+ });
36
+ return;
37
+ }
38
+ createDraftMessage();
39
+ });
40
+ }
25
41
  function createDraftMessage() {
26
42
  const message = [
27
43
  `From: ${from || process.env.IMAP_USERNAME || ""}`,
@@ -53,3 +69,23 @@ export function registerCreateDraft(client, server) {
53
69
  });
54
70
  });
55
71
  }
72
+ /**
73
+ * Returns the full path of the folder the server marks with the `\Drafts`
74
+ * special-use attribute (RFC 6154), or undefined if none is marked.
75
+ */
76
+ export function findDraftsFolder(boxes, parents = []) {
77
+ for (const [name, box] of Object.entries(boxes)) {
78
+ const segments = [...parents, name];
79
+ if (box.attribs?.some((a) => a.toLowerCase() === "\\drafts")) {
80
+ // A parent the server doesn't list has no delimiter of its own, so join
81
+ // with the matched folder's, the server's one hierarchy separator.
82
+ return segments.join(box.delimiter ?? "");
83
+ }
84
+ if (box.children) {
85
+ const found = findDraftsFolder(box.children, segments);
86
+ if (found)
87
+ return found;
88
+ }
89
+ }
90
+ return undefined;
91
+ }
@@ -1,4 +1,71 @@
1
- import { describe, test } from "vitest";
2
- describe("registerCreateDraft", () => {
3
- test("to be implemented", () => { });
1
+ import { describe, expect, test, vi } from "vitest";
2
+ import { registerCreateDraft } from "./create-draft.js";
3
+ function box(attribs, children = null) {
4
+ return {
5
+ attribs,
6
+ delimiter: "/",
7
+ children: children,
8
+ parent: null,
9
+ };
10
+ }
11
+ function setup(boxes, existing) {
12
+ const listeners = {};
13
+ let openedBox;
14
+ const client = {
15
+ once: vi.fn((event, cb) => {
16
+ listeners[event] = cb;
17
+ }),
18
+ connect: vi.fn(() => listeners.ready()),
19
+ getBoxes: vi.fn((cb) => cb(null, boxes)),
20
+ openBox: vi.fn((name, _readOnly, cb) => {
21
+ if (existing.includes(name)) {
22
+ openedBox = name;
23
+ cb(null);
24
+ }
25
+ else {
26
+ cb(new Error(`Mailbox doesn't exist: ${name}`));
27
+ }
28
+ }),
29
+ append: vi.fn((_msg, _opts, cb) => cb(null)),
30
+ };
31
+ let handler;
32
+ const server = {
33
+ tool: vi.fn((...args) => {
34
+ handler = args[args.length - 1];
35
+ }),
36
+ };
37
+ registerCreateDraft(client, server);
38
+ const run = () => handler({ to: "jane@example.com", subject: "Hi", body: "Hello" });
39
+ return { client, run, openedBox: () => openedBox };
40
+ }
41
+ describe("create-draft", () => {
42
+ test("saves to the folder marked \\Drafts, whatever its name and nesting", async () => {
43
+ const { client, run, openedBox } = setup({
44
+ INBOX: box(["\\HasNoChildren"]),
45
+ "[Gmail]": box(["\\Noselect", "\\HasChildren"], {
46
+ "All Mail": box(["\\All"]),
47
+ Entwürfe: box(["\\HasNoChildren", "\\Drafts"]),
48
+ }),
49
+ Drafts: box([]),
50
+ }, ["[Gmail]/Entwürfe", "Drafts"]);
51
+ const result = await run();
52
+ expect(openedBox()).toBe("[Gmail]/Entwürfe");
53
+ expect(client.append).toHaveBeenCalledOnce();
54
+ expect(result.content[0].text).toBe("Draft created successfully for jane@example.com");
55
+ });
56
+ test("falls back to INBOX.Drafts or Drafts when no folder is marked \\Drafts", async () => {
57
+ const { client, run, openedBox } = setup({ INBOX: box([]), Drafts: box([]) }, ["Drafts"]);
58
+ await run();
59
+ expect(client.openBox.mock.calls.map((c) => c[0])).toEqual([
60
+ "INBOX.Drafts",
61
+ "Drafts",
62
+ ]);
63
+ expect(openedBox()).toBe("Drafts");
64
+ expect(client.append).toHaveBeenCalledOnce();
65
+ });
66
+ test("fails when no drafts folder can be opened", async () => {
67
+ const { client, run } = setup({ INBOX: box([]) }, []);
68
+ await expect(run()).rejects.toThrow("Failed to open drafts folder");
69
+ expect(client.append).not.toHaveBeenCalled();
70
+ });
4
71
  });
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 IMAP Model Context Protocol (MCP) server to expose IMAP operations as tools for AI assistants.",
5
- "version": "0.1.1",
5
+ "version": "0.1.2",
6
6
  "main": "dist/index.js",
7
7
  "type": "module",
8
8
  "bin": {