imap-mcp 0.1.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/LICENSE +21 -0
- package/README.md +83 -0
- package/dist/index.js +29 -0
- package/dist/index.test.js +55 -0
- package/dist/tools/create-draft.js +55 -0
- package/dist/tools/create-draft.test.js +4 -0
- package/dist/tools/create-event.js +29 -0
- package/dist/tools/create-event.test.js +4 -0
- package/dist/tools/delete-event.js +9 -0
- package/dist/tools/index.js +1 -0
- package/dist/tools/list-calendars.js +7 -0
- package/dist/tools/list-events.js +21 -0
- package/package.json +65 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Dominik Grusemann
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# imap-mcp
|
|
2
|
+
|
|
3
|
+
<div align="center">
|
|
4
|
+
|
|
5
|
+
📧 An IMAP Model Context Protocol (MCP) server to expose IMAP operations as tools for AI assistants.
|
|
6
|
+
|
|
7
|
+
[](https://choosealicense.com/licenses/mit/)
|
|
8
|
+
[](https://github.com/prettier/prettier)
|
|
9
|
+
[](https://modelcontextprotocol.io)
|
|
10
|
+
[](https://github.com/semantic-release/semantic-release)
|
|
11
|
+
|
|
12
|
+
</div>
|
|
13
|
+
|
|
14
|
+
## ✨ Features
|
|
15
|
+
|
|
16
|
+
- **Email Draft Creation**: Create draft emails that are saved to your IMAP server's drafts folder
|
|
17
|
+
- **IMAP Integration**: Connect to any IMAP-compatible email server (Gmail, Outlook, etc.)
|
|
18
|
+
- **Secure Authentication**: Uses environment variables for secure credential management
|
|
19
|
+
- **MCP Compatible**: Works with Claude and other AI assistants that support the Model Context Protocol
|
|
20
|
+
- **TypeScript**: Full TypeScript support with proper type definitions
|
|
21
|
+
|
|
22
|
+
## Setup
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
{
|
|
26
|
+
"mcpServers": {
|
|
27
|
+
...,
|
|
28
|
+
"imap": {
|
|
29
|
+
"command": "npx",
|
|
30
|
+
"args": [
|
|
31
|
+
"imap-mcp"
|
|
32
|
+
],
|
|
33
|
+
"env": {
|
|
34
|
+
"IMAP_HOST": "<IMAP host>",
|
|
35
|
+
"IMAP_PORT": "<IMAP port>",
|
|
36
|
+
"IMAP_USERNAME": "<IMAP username>",
|
|
37
|
+
"IMAP_PASSWORD": "<IMAP password>",
|
|
38
|
+
"IMAP_USE_SSL": "<true or false>"
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Usage
|
|
46
|
+
|
|
47
|
+
1. Compile TypeScript to JavaScript:
|
|
48
|
+
```bash
|
|
49
|
+
npx tsc
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
2. Run the MCP server:
|
|
53
|
+
```bash
|
|
54
|
+
node dist/index.js
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Available Tools
|
|
58
|
+
|
|
59
|
+
### `create-draft`
|
|
60
|
+
|
|
61
|
+
Creates a draft email message and saves it to the IMAP server's drafts folder.
|
|
62
|
+
|
|
63
|
+
**Parameters:**
|
|
64
|
+
- `to` (string, required): The recipient's email address
|
|
65
|
+
- `subject` (string, required): The email subject line
|
|
66
|
+
- `body` (string, required): The email body content
|
|
67
|
+
- `from` (string, optional): The sender's email address (defaults to IMAP_USERNAME)
|
|
68
|
+
|
|
69
|
+
**Example:**
|
|
70
|
+
```json
|
|
71
|
+
{
|
|
72
|
+
"to": "recipient@example.com",
|
|
73
|
+
"subject": "Meeting Reminder",
|
|
74
|
+
"body": "Don't forget about our meeting tomorrow at 2 PM.",
|
|
75
|
+
"from": "sender@example.com"
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
The tool will attempt to save the draft to either "INBOX.Drafts" or "Drafts" folder, depending on your email server's folder structure.
|
|
80
|
+
|
|
81
|
+
## License
|
|
82
|
+
|
|
83
|
+
MIT
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import "dotenv/config";
|
|
3
|
+
import Imap from "imap";
|
|
4
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
5
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
6
|
+
import { registerCreateDraft } from "./tools/create-draft.js";
|
|
7
|
+
const server = new McpServer({
|
|
8
|
+
name: "imap-mcp",
|
|
9
|
+
version: "0.1.0",
|
|
10
|
+
});
|
|
11
|
+
export async function main() {
|
|
12
|
+
const imapConfig = {
|
|
13
|
+
user: process.env.IMAP_USERNAME || "",
|
|
14
|
+
password: process.env.IMAP_PASSWORD || "",
|
|
15
|
+
host: process.env.IMAP_HOST || "",
|
|
16
|
+
port: parseInt(process.env.IMAP_PORT || "993"),
|
|
17
|
+
tls: process.env.IMAP_USE_SSL === "true",
|
|
18
|
+
tlsOptions: { rejectUnauthorized: false },
|
|
19
|
+
};
|
|
20
|
+
const client = new Imap(imapConfig);
|
|
21
|
+
registerCreateDraft(client, server);
|
|
22
|
+
// Start receiving messages on stdin and sending messages on stdout
|
|
23
|
+
const transport = new StdioServerTransport();
|
|
24
|
+
await server.connect(transport);
|
|
25
|
+
}
|
|
26
|
+
// Only run main if this file is executed directly (not imported)
|
|
27
|
+
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
28
|
+
main();
|
|
29
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
2
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
3
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
4
|
+
import Imap from "imap";
|
|
5
|
+
// Mock the dependencies
|
|
6
|
+
vi.mock("@modelcontextprotocol/sdk/server/mcp.js");
|
|
7
|
+
vi.mock("@modelcontextprotocol/sdk/server/stdio.js");
|
|
8
|
+
vi.mock("imap");
|
|
9
|
+
vi.mock("./tools/create-draft.js", () => ({
|
|
10
|
+
registerCreateDraft: vi.fn(),
|
|
11
|
+
}));
|
|
12
|
+
describe("main", () => {
|
|
13
|
+
beforeEach(() => {
|
|
14
|
+
vi.clearAllMocks();
|
|
15
|
+
// Mock environment variables
|
|
16
|
+
process.env.IMAP_USERNAME = "test@example.com";
|
|
17
|
+
process.env.IMAP_PASSWORD = "testpass";
|
|
18
|
+
process.env.IMAP_HOST = "imap.example.com";
|
|
19
|
+
process.env.IMAP_PORT = "993";
|
|
20
|
+
process.env.IMAP_USE_SSL = "true";
|
|
21
|
+
});
|
|
22
|
+
afterEach(() => {
|
|
23
|
+
vi.restoreAllMocks();
|
|
24
|
+
});
|
|
25
|
+
it("starts the mcp server", async () => {
|
|
26
|
+
const mockServer = {
|
|
27
|
+
connect: vi.fn().mockResolvedValue(undefined),
|
|
28
|
+
};
|
|
29
|
+
const mockTransport = {};
|
|
30
|
+
const mockImap = {};
|
|
31
|
+
vi.mocked(McpServer).mockImplementation(() => mockServer);
|
|
32
|
+
vi.mocked(StdioServerTransport).mockImplementation(() => mockTransport);
|
|
33
|
+
vi.mocked(Imap).mockImplementation(() => mockImap);
|
|
34
|
+
// Import and run main function
|
|
35
|
+
const { main } = await import("./index.js");
|
|
36
|
+
await main();
|
|
37
|
+
// Verify MCP server was created with correct config
|
|
38
|
+
expect(McpServer).toHaveBeenCalledWith({
|
|
39
|
+
name: "imap-mcp",
|
|
40
|
+
version: "0.1.0",
|
|
41
|
+
});
|
|
42
|
+
// Verify IMAP client was created with correct config
|
|
43
|
+
expect(Imap).toHaveBeenCalledWith({
|
|
44
|
+
user: "test@example.com",
|
|
45
|
+
password: "testpass",
|
|
46
|
+
host: "imap.example.com",
|
|
47
|
+
port: 993,
|
|
48
|
+
tls: true,
|
|
49
|
+
tlsOptions: { rejectUnauthorized: false },
|
|
50
|
+
});
|
|
51
|
+
// Verify transport was created and server connected
|
|
52
|
+
expect(StdioServerTransport).toHaveBeenCalled();
|
|
53
|
+
expect(mockServer.connect).toHaveBeenCalledWith(mockTransport);
|
|
54
|
+
});
|
|
55
|
+
});
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export function registerCreateDraft(client, server) {
|
|
3
|
+
server.tool("create-draft", "Creates a draft email message", {
|
|
4
|
+
to: z.string(),
|
|
5
|
+
subject: z.string(),
|
|
6
|
+
body: z.string(),
|
|
7
|
+
from: z.string().optional(),
|
|
8
|
+
}, async ({ to, subject, body, from }) => {
|
|
9
|
+
return new Promise((resolve, reject) => {
|
|
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}`));
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
createDraftMessage();
|
|
20
|
+
});
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
createDraftMessage();
|
|
24
|
+
});
|
|
25
|
+
function createDraftMessage() {
|
|
26
|
+
const message = [
|
|
27
|
+
`From: ${from || process.env.IMAP_USERNAME || ""}`,
|
|
28
|
+
`To: ${to}`,
|
|
29
|
+
`Subject: ${subject}`,
|
|
30
|
+
"",
|
|
31
|
+
body,
|
|
32
|
+
].join("\r\n");
|
|
33
|
+
client.append(message, { flags: ["\\Draft"] }, (err) => {
|
|
34
|
+
if (err) {
|
|
35
|
+
reject(new Error(`Failed to create draft: ${err.message}`));
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
resolve({
|
|
39
|
+
content: [
|
|
40
|
+
{
|
|
41
|
+
type: "text",
|
|
42
|
+
text: `Draft created successfully for ${to}`,
|
|
43
|
+
},
|
|
44
|
+
],
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
client.once("error", (err) => {
|
|
50
|
+
reject(new Error(`IMAP connection error: ${err.message}`));
|
|
51
|
+
});
|
|
52
|
+
client.connect();
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
const recurrenceRuleSchema = z.object({
|
|
3
|
+
freq: z.enum(["DAILY", "WEEKLY", "MONTHLY", "YEARLY"]).optional(),
|
|
4
|
+
interval: z.number().optional(),
|
|
5
|
+
count: z.number().optional(),
|
|
6
|
+
until: z.string().datetime().optional(), // ISO 8601 string
|
|
7
|
+
byday: z.array(z.string()).optional(), // e.g. ["MO", "TU"]
|
|
8
|
+
bymonthday: z.array(z.number()).optional(),
|
|
9
|
+
bymonth: z.array(z.number()).optional(),
|
|
10
|
+
});
|
|
11
|
+
export function registerCreateEvent(client, server) {
|
|
12
|
+
server.tool("create-event", "Creates an event in the calendar specified by its URL", {
|
|
13
|
+
summary: z.string(),
|
|
14
|
+
start: z.string().datetime(),
|
|
15
|
+
end: z.string().datetime(),
|
|
16
|
+
calendarUrl: z.string(),
|
|
17
|
+
recurrenceRule: recurrenceRuleSchema.optional(),
|
|
18
|
+
}, async ({ calendarUrl, summary, start, end, recurrenceRule }) => {
|
|
19
|
+
const event = await client.createEvent(calendarUrl, {
|
|
20
|
+
summary: summary,
|
|
21
|
+
start: new Date(start),
|
|
22
|
+
end: new Date(end),
|
|
23
|
+
recurrenceRule: recurrenceRule,
|
|
24
|
+
});
|
|
25
|
+
return {
|
|
26
|
+
content: [{ type: "text", text: event.uid }],
|
|
27
|
+
};
|
|
28
|
+
});
|
|
29
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export function registerDeleteEvent(client, server) {
|
|
3
|
+
server.tool("delete-event", "Deletes an event in the calendar specified by its URL", { uid: z.string(), calendarUrl: z.string() }, async ({ uid, calendarUrl }) => {
|
|
4
|
+
await client.deleteEvent(calendarUrl, uid);
|
|
5
|
+
return {
|
|
6
|
+
content: [{ type: "text", text: "Event deleted" }],
|
|
7
|
+
};
|
|
8
|
+
});
|
|
9
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./create-draft.js";
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export async function registerListCalendars(client, server) {
|
|
2
|
+
const calendars = await client.getCalendars();
|
|
3
|
+
server.tool("list-calendars", "List all calendars returning both name and URL", {}, async () => {
|
|
4
|
+
const data = calendars.map((c) => ({ name: c.displayName, url: c.url }));
|
|
5
|
+
return { content: [{ type: "text", text: JSON.stringify(data) }] };
|
|
6
|
+
});
|
|
7
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
const dateString = z.string().refine((val) => !isNaN(Date.parse(val)), {
|
|
3
|
+
message: "Invalid date string",
|
|
4
|
+
});
|
|
5
|
+
export function registerListEvents(client, server) {
|
|
6
|
+
server.tool("list-events", "List all events between start and end date in the calendar specified by its URL", { start: dateString, end: dateString, calendarUrl: z.string() }, async ({ calendarUrl, start, end }) => {
|
|
7
|
+
const options = {
|
|
8
|
+
start: new Date(start),
|
|
9
|
+
end: new Date(end),
|
|
10
|
+
};
|
|
11
|
+
const allEvents = await client.getEvents(calendarUrl, options);
|
|
12
|
+
const data = allEvents.map((e) => ({
|
|
13
|
+
summary: e.summary,
|
|
14
|
+
start: e.start,
|
|
15
|
+
end: e.end,
|
|
16
|
+
}));
|
|
17
|
+
return {
|
|
18
|
+
content: [{ type: "text", text: JSON.stringify(data) }],
|
|
19
|
+
};
|
|
20
|
+
});
|
|
21
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "imap-mcp",
|
|
3
|
+
"description": "📧 An IMAP Model Context Protocol (MCP) server to expose IMAP operations as tools for AI assistants.",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"bin": {
|
|
8
|
+
"imap-mcp": "dist/index.js"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "tsc && shx chmod +x dist/*.js",
|
|
15
|
+
"prepare": "npm run build && husky",
|
|
16
|
+
"test": "vitest run",
|
|
17
|
+
"test:watch": "vitest",
|
|
18
|
+
"lint": "eslint src/",
|
|
19
|
+
"lint:fix": "eslint src/ --fix",
|
|
20
|
+
"watch": "tsc --watch",
|
|
21
|
+
"semantic-release": "semantic-release",
|
|
22
|
+
"format": "prettier --write src/.",
|
|
23
|
+
"format:check": "prettier --check src/."
|
|
24
|
+
},
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "https://github.com/dominik1001/imap-mcp.git"
|
|
28
|
+
},
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/dominik1001/imap-mcp/issues"
|
|
31
|
+
},
|
|
32
|
+
"homepage": "https://github.com/dominik1001/imap-mcp#readme",
|
|
33
|
+
"author": "Dominik Grusemann",
|
|
34
|
+
"license": "MIT",
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@modelcontextprotocol/sdk": "^1.13.1",
|
|
37
|
+
"dotenv": "^16.5.0",
|
|
38
|
+
"imap": "^0.8.19",
|
|
39
|
+
"zod": "^3.25.67"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@types/imap": "^0.8.42",
|
|
43
|
+
"@eslint/js": "^9.29.0",
|
|
44
|
+
"@semantic-release/changelog": "^6.0.3",
|
|
45
|
+
"@semantic-release/git": "^10.0.1",
|
|
46
|
+
"@semantic-release/github": "^11.0.3",
|
|
47
|
+
"@types/node": "^24.0.4",
|
|
48
|
+
"eslint": "^9.29.0",
|
|
49
|
+
"eslint-config-prettier": "^10.1.5",
|
|
50
|
+
"husky": "^9.1.7",
|
|
51
|
+
"lint-staged": "^16.1.2",
|
|
52
|
+
"prettier": "3.6.1",
|
|
53
|
+
"semantic-release": "^24.2.5",
|
|
54
|
+
"shx": "^0.4.0",
|
|
55
|
+
"typescript": "^5.8.3",
|
|
56
|
+
"typescript-eslint": "^8.35.0",
|
|
57
|
+
"vitest": "^3.2.4"
|
|
58
|
+
},
|
|
59
|
+
"lint-staged": {
|
|
60
|
+
"src/**/*.{ts,tsx,js,jsx}": [
|
|
61
|
+
"eslint --fix",
|
|
62
|
+
"prettier --write"
|
|
63
|
+
]
|
|
64
|
+
}
|
|
65
|
+
}
|