imap-mcp 0.1.3 → 1.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-APACHE +190 -0
- package/{LICENSE → LICENSE-MIT} +1 -1
- package/README.md +24 -4
- package/dist/access.js +117 -0
- package/dist/access.test.js +80 -0
- package/dist/index.js +21 -7
- package/dist/index.test.js +59 -10
- package/dist/tools/create-draft.js +75 -77
- package/dist/tools/create-draft.test.js +54 -33
- package/package.json +6 -5
package/LICENSE-APACHE
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding any notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
Copyright 2025 Dominik Grusemann
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|
package/{LICENSE → LICENSE-MIT}
RENAMED
|
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
18
18
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
19
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
20
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
21
|
+
SOFTWARE.
|
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 save email drafts to any IMAP mailbox.</b><br>
|
|
14
|
-
It gives your agent one tool, <code>
|
|
14
|
+
It gives your agent one tool, <code>create_draft</code>, which saves a message to your Drafts folder for you to review and send. 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">
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
## ✨ Features
|
|
52
52
|
|
|
53
53
|
- **Email Draft Creation**: Create draft emails that are saved to your IMAP server's drafts folder
|
|
54
|
-
- **IMAP Integration**: Connect to
|
|
54
|
+
- **IMAP Integration**: Connect to an IMAP server that accepts username-and-password login, such as Gmail with an [app password](https://support.google.com/accounts/answer/185833) instead of your account password. Providers that allow only OAuth login over IMAP, such as Outlook.com, don't work.
|
|
55
55
|
- **Secure Authentication**: Uses environment variables for secure credential management
|
|
56
56
|
- **MCP Compatible**: Works with Claude and other AI assistants that support the Model Context Protocol
|
|
57
57
|
- **TypeScript**: Full TypeScript support with proper type definitions
|
|
@@ -68,9 +68,29 @@ npx tsc
|
|
|
68
68
|
node dist/index.js
|
|
69
69
|
```
|
|
70
70
|
|
|
71
|
+
## Access levels
|
|
72
|
+
|
|
73
|
+
`--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.
|
|
74
|
+
|
|
75
|
+
| Level | The agent may | Tools |
|
|
76
|
+
| --- | --- | --- |
|
|
77
|
+
| `read-only` | Read the mailbox | None yet |
|
|
78
|
+
| `no-send` | Also change the mailbox, but send nothing and reach no one else | `create_draft` |
|
|
79
|
+
| `everything` (default) | Also reach other people | `create_draft` |
|
|
80
|
+
|
|
81
|
+
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.
|
|
82
|
+
|
|
83
|
+
For example, to start the server read-only:
|
|
84
|
+
|
|
85
|
+
```json
|
|
86
|
+
"args": ["imap-mcp", "--access", "read-only"]
|
|
87
|
+
```
|
|
88
|
+
|
|
71
89
|
## Available Tools
|
|
72
90
|
|
|
73
|
-
### `
|
|
91
|
+
### `create_draft`
|
|
92
|
+
|
|
93
|
+
Access level: `no-send`.
|
|
74
94
|
|
|
75
95
|
Creates a draft email message and saves it to the IMAP server's drafts folder.
|
|
76
96
|
|
|
@@ -94,4 +114,4 @@ The tool saves the draft to the folder your server marks as the drafts folder (t
|
|
|
94
114
|
|
|
95
115
|
## License
|
|
96
116
|
|
|
97
|
-
MIT
|
|
117
|
+
MIT OR Apache-2.0, at your option. See [LICENSE-MIT](LICENSE-MIT) and [LICENSE-APACHE](LICENSE-APACHE).
|
package/dist/access.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// Access levels: what an agent may do through this server, set by the user
|
|
2
|
+
// with --access or MCP_ACCESS (the names, values and default are
|
|
3
|
+
// langmail-mcp's). Each level offers its own tools plus those of the levels
|
|
4
|
+
// below.
|
|
5
|
+
//
|
|
6
|
+
// read-only reads the mailbox.
|
|
7
|
+
// no-send also changes the mailbox, such as saving drafts. Nothing
|
|
8
|
+
// leaves it and nobody else is told.
|
|
9
|
+
// everything also reaches other people, such as sending mail.
|
|
10
|
+
//
|
|
11
|
+
// A tool outside the level is left out of the tool list. A client may still
|
|
12
|
+
// call it from a list it cached at a higher level; that call gets an in-band
|
|
13
|
+
// `isError` result that says why and where the level is changed, rather than
|
|
14
|
+
// the SDK's "Tool X not found", which gives the agent no reason and no next
|
|
15
|
+
// step.
|
|
16
|
+
import { parseArgs } from "node:util";
|
|
17
|
+
import { CallToolRequestSchema, } from "@modelcontextprotocol/sdk/types.js";
|
|
18
|
+
export const ACCESS_LEVELS = [
|
|
19
|
+
"read-only",
|
|
20
|
+
"no-send",
|
|
21
|
+
"everything",
|
|
22
|
+
];
|
|
23
|
+
const DEFAULT_ACCESS = "everything";
|
|
24
|
+
// The level of every tool. Registering a tool that has none here fails, so
|
|
25
|
+
// every test that builds the server fails until it gets one.
|
|
26
|
+
export const TOOL_ACCESS = {
|
|
27
|
+
create_draft: "no-send",
|
|
28
|
+
};
|
|
29
|
+
const NAMES = {
|
|
30
|
+
everything: "Everything",
|
|
31
|
+
"no-send": "Change, never send",
|
|
32
|
+
"read-only": "Read only",
|
|
33
|
+
};
|
|
34
|
+
const ALLOWS = {
|
|
35
|
+
everything: "",
|
|
36
|
+
"no-send": ", which lets it change the mailbox, such as saving drafts, but not send mail or reach anyone else",
|
|
37
|
+
"read-only": ", which lets it read the mailbox but not change or send anything",
|
|
38
|
+
};
|
|
39
|
+
const isAccessLevel = (raw) => ACCESS_LEVELS.includes(raw);
|
|
40
|
+
/**
|
|
41
|
+
* Reads the level from `--access`, else `MCP_ACCESS`, else the default. Pure:
|
|
42
|
+
* the caller prints the message and exits.
|
|
43
|
+
*/
|
|
44
|
+
export function parseAccess(argv, env) {
|
|
45
|
+
// Not strict: imap-mcp ignored its arguments before this flag, so a stray
|
|
46
|
+
// one must not stop a setup that started until now.
|
|
47
|
+
const { values } = parseArgs({
|
|
48
|
+
args: argv,
|
|
49
|
+
options: { access: { type: "string" } },
|
|
50
|
+
strict: false,
|
|
51
|
+
allowPositionals: true,
|
|
52
|
+
});
|
|
53
|
+
// A bare `--access` comes back as true: an empty level, not an unset one.
|
|
54
|
+
const flag = values.access === undefined
|
|
55
|
+
? undefined
|
|
56
|
+
: typeof values.access === "string"
|
|
57
|
+
? values.access.trim()
|
|
58
|
+
: "";
|
|
59
|
+
const raw = flag ?? (env.MCP_ACCESS?.trim() || DEFAULT_ACCESS);
|
|
60
|
+
if (isAccessLevel(raw))
|
|
61
|
+
return { ok: true, level: raw };
|
|
62
|
+
return {
|
|
63
|
+
ok: false,
|
|
64
|
+
message: `imap-mcp cannot start: MCP_ACCESS (--access): "${raw}" is not an access level; use ${ACCESS_LEVELS.join(", ")}`,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
const allows = (level, tool) => ACCESS_LEVELS.indexOf(TOOL_ACCESS[tool]) <= ACCESS_LEVELS.indexOf(level);
|
|
68
|
+
export const refusalText = (tool, level) => `${tool} is not available: the user started this server with ${NAMES[level]} access${ALLOWS[level]}. Nothing was changed or sent. The user can change the access level by restarting the server with the --access option.`;
|
|
69
|
+
/**
|
|
70
|
+
* Offers only the tools `level` allows. Call right after constructing the
|
|
71
|
+
* server, before any tool is registered.
|
|
72
|
+
*/
|
|
73
|
+
export function restrictToAccess(server, level) {
|
|
74
|
+
const excluded = new Set();
|
|
75
|
+
// A tool outside the level is still registered, but disabled, so the SDK
|
|
76
|
+
// leaves it out of tools/list and installs its tools/call handler even when
|
|
77
|
+
// the level allows no tool at all.
|
|
78
|
+
const restrict = (register) => {
|
|
79
|
+
return (name, ...rest) => {
|
|
80
|
+
if (!Object.hasOwn(TOOL_ACCESS, name)) {
|
|
81
|
+
throw new Error(`Tool ${name} has no access level: add it to TOOL_ACCESS in src/access.ts`);
|
|
82
|
+
}
|
|
83
|
+
const registered = register(name, ...rest);
|
|
84
|
+
if (!allows(level, name)) {
|
|
85
|
+
excluded.add(name);
|
|
86
|
+
registered.disable();
|
|
87
|
+
}
|
|
88
|
+
return registered;
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
server.tool = restrict(server.tool.bind(server));
|
|
92
|
+
server.registerTool = restrict(server.registerTool.bind(server));
|
|
93
|
+
// McpServer installs its tools/call handler with the first tool. Wrap it as
|
|
94
|
+
// it is installed, so a call to an excluded tool is answered here.
|
|
95
|
+
const lowLevel = server.server;
|
|
96
|
+
const setRequestHandler = lowLevel.setRequestHandler.bind(lowLevel);
|
|
97
|
+
const patched = (schema, handler) => {
|
|
98
|
+
if (schema !== CallToolRequestSchema) {
|
|
99
|
+
setRequestHandler(schema, handler);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
const call = handler;
|
|
103
|
+
setRequestHandler(schema, (request, extra) => {
|
|
104
|
+
const name = request.params?.name;
|
|
105
|
+
if (typeof name === "string" && excluded.has(name)) {
|
|
106
|
+
const result = {
|
|
107
|
+
content: [{ type: "text", text: refusalText(name, level) }],
|
|
108
|
+
isError: true,
|
|
109
|
+
};
|
|
110
|
+
return result;
|
|
111
|
+
}
|
|
112
|
+
return call(request, extra);
|
|
113
|
+
});
|
|
114
|
+
};
|
|
115
|
+
lowLevel.setRequestHandler =
|
|
116
|
+
patched;
|
|
117
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { z } from "zod/v4";
|
|
3
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
4
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
5
|
+
import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js";
|
|
6
|
+
import { parseAccess, refusalText, restrictToAccess, TOOL_ACCESS, } from "./access.js";
|
|
7
|
+
describe("parseAccess", () => {
|
|
8
|
+
it("defaults to everything", () => {
|
|
9
|
+
expect(parseAccess([], {})).toEqual({ ok: true, level: "everything" });
|
|
10
|
+
});
|
|
11
|
+
it("reads MCP_ACCESS", () => {
|
|
12
|
+
expect(parseAccess([], { MCP_ACCESS: "no-send" })).toEqual({
|
|
13
|
+
ok: true,
|
|
14
|
+
level: "no-send",
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
it("takes the flag over the variable", () => {
|
|
18
|
+
expect(parseAccess(["--access=read-only"], { MCP_ACCESS: "everything" })).toEqual({ ok: true, level: "read-only" });
|
|
19
|
+
});
|
|
20
|
+
it("rejects --access without a value", () => {
|
|
21
|
+
expect(parseAccess(["--access"], { MCP_ACCESS: "read-only" }).ok).toBe(false);
|
|
22
|
+
});
|
|
23
|
+
it("rejects an unknown value, naming the setting, flag and values", () => {
|
|
24
|
+
const result = parseAccess(["--access", "all"], {});
|
|
25
|
+
expect(result.ok).toBe(false);
|
|
26
|
+
expect(!result.ok && result.message).toBe('imap-mcp cannot start: MCP_ACCESS (--access): "all" is not an access level; use read-only, no-send, everything');
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
async function connect(level) {
|
|
30
|
+
const server = new McpServer({ name: "test", version: "0" });
|
|
31
|
+
restrictToAccess(server, level);
|
|
32
|
+
let calls = 0;
|
|
33
|
+
server.tool("create_draft", "Creates a draft", { to: z.string() }, () => {
|
|
34
|
+
calls++;
|
|
35
|
+
return { content: [{ type: "text", text: "saved" }] };
|
|
36
|
+
});
|
|
37
|
+
const client = new Client({ name: "client", version: "0" });
|
|
38
|
+
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
|
|
39
|
+
await server.connect(serverTransport);
|
|
40
|
+
await client.connect(clientTransport);
|
|
41
|
+
return { client, calls: () => calls };
|
|
42
|
+
}
|
|
43
|
+
describe("restrictToAccess", () => {
|
|
44
|
+
it("files create_draft under no-send", () => {
|
|
45
|
+
expect(TOOL_ACCESS.create_draft).toBe("no-send");
|
|
46
|
+
});
|
|
47
|
+
it.each(["no-send", "everything"])("offers and runs create_draft at %s", async (level) => {
|
|
48
|
+
const { client, calls } = await connect(level);
|
|
49
|
+
const { tools } = await client.listTools();
|
|
50
|
+
expect(tools.map((t) => t.name)).toEqual(["create_draft"]);
|
|
51
|
+
const result = await client.callTool({
|
|
52
|
+
name: "create_draft",
|
|
53
|
+
arguments: { to: "jane@example.com" },
|
|
54
|
+
});
|
|
55
|
+
expect(result.isError).toBeFalsy();
|
|
56
|
+
expect(calls()).toBe(1);
|
|
57
|
+
});
|
|
58
|
+
it("leaves create_draft out at read-only and refuses a cached call", async () => {
|
|
59
|
+
const { client, calls } = await connect("read-only");
|
|
60
|
+
const { tools } = await client.listTools();
|
|
61
|
+
expect(tools).toEqual([]);
|
|
62
|
+
const result = await client.callTool({
|
|
63
|
+
name: "create_draft",
|
|
64
|
+
arguments: { to: "jane@example.com" },
|
|
65
|
+
});
|
|
66
|
+
expect(result).toEqual({
|
|
67
|
+
content: [
|
|
68
|
+
{ type: "text", text: refusalText("create_draft", "read-only") },
|
|
69
|
+
],
|
|
70
|
+
isError: true,
|
|
71
|
+
});
|
|
72
|
+
expect(refusalText("create_draft", "read-only")).toBe("create_draft is not available: the user started this server with Read only access, which lets it read the mailbox but not change or send anything. Nothing was changed or sent. The user can change the access level by restarting the server with the --access option.");
|
|
73
|
+
expect(calls()).toBe(0);
|
|
74
|
+
});
|
|
75
|
+
it("fails to register a tool without a level", () => {
|
|
76
|
+
const server = new McpServer({ name: "test", version: "0" });
|
|
77
|
+
restrictToAccess(server, "everything");
|
|
78
|
+
expect(() => server.registerTool("send_draft", {}, () => ({ content: [] }))).toThrow("Tool send_draft has no access level");
|
|
79
|
+
});
|
|
80
|
+
});
|
package/dist/index.js
CHANGED
|
@@ -1,27 +1,41 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import "dotenv/config";
|
|
3
|
-
import
|
|
3
|
+
import { ImapFlow } from "imapflow";
|
|
4
4
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
5
5
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
6
|
+
import { parseAccess, restrictToAccess } from "./access.js";
|
|
6
7
|
import { registerCreateDraft } from "./tools/create-draft.js";
|
|
7
8
|
const server = new McpServer({
|
|
8
9
|
name: "imap-mcp",
|
|
9
10
|
version: "0.1.0",
|
|
10
11
|
});
|
|
11
12
|
export async function main() {
|
|
13
|
+
const access = parseAccess(process.argv.slice(2), process.env);
|
|
14
|
+
if (!access.ok) {
|
|
15
|
+
console.error(access.message);
|
|
16
|
+
process.exit(2);
|
|
17
|
+
}
|
|
18
|
+
restrictToAccess(server, access.level);
|
|
12
19
|
const imapConfig = {
|
|
13
|
-
user: process.env.IMAP_USERNAME || "",
|
|
14
|
-
password: process.env.IMAP_PASSWORD || "",
|
|
15
20
|
host: process.env.IMAP_HOST || "",
|
|
16
21
|
port: parseInt(process.env.IMAP_PORT || "993"),
|
|
17
|
-
|
|
18
|
-
|
|
22
|
+
secure: process.env.IMAP_USE_SSL === "true",
|
|
23
|
+
auth: {
|
|
24
|
+
user: process.env.IMAP_USERNAME || "",
|
|
25
|
+
pass: process.env.IMAP_PASSWORD || "",
|
|
26
|
+
},
|
|
27
|
+
tls: { rejectUnauthorized: false },
|
|
28
|
+
// Like the imap package, never upgrade a non-SSL connection with STARTTLS.
|
|
29
|
+
doSTARTTLS: false,
|
|
30
|
+
// imapflow logs to stdout by default, which carries the MCP protocol.
|
|
31
|
+
logger: false,
|
|
19
32
|
};
|
|
20
|
-
|
|
21
|
-
registerCreateDraft(client, server);
|
|
33
|
+
registerCreateDraft(() => new ImapFlow(imapConfig), server);
|
|
22
34
|
// Start receiving messages on stdin and sending messages on stdout
|
|
23
35
|
const transport = new StdioServerTransport();
|
|
24
36
|
await server.connect(transport);
|
|
37
|
+
// stdout is the MCP transport, so the log goes to stderr.
|
|
38
|
+
console.error(`imap-mcp started with access level ${access.level}`);
|
|
25
39
|
}
|
|
26
40
|
// Only run main if this file is executed directly (not imported)
|
|
27
41
|
if (import.meta.url === `file://${process.argv[1]}`) {
|
package/dist/index.test.js
CHANGED
|
@@ -1,17 +1,26 @@
|
|
|
1
1
|
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
2
2
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
3
3
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
4
|
-
import
|
|
4
|
+
import { ImapFlow } from "imapflow";
|
|
5
|
+
import { restrictToAccess } from "./access.js";
|
|
5
6
|
// Mock the dependencies
|
|
6
7
|
vi.mock("@modelcontextprotocol/sdk/server/mcp.js");
|
|
7
8
|
vi.mock("@modelcontextprotocol/sdk/server/stdio.js");
|
|
8
|
-
vi.mock("
|
|
9
|
+
vi.mock("imapflow");
|
|
9
10
|
vi.mock("./tools/create-draft.js", () => ({
|
|
10
11
|
registerCreateDraft: vi.fn(),
|
|
11
12
|
}));
|
|
13
|
+
vi.mock("./access.js", async (importOriginal) => ({
|
|
14
|
+
...(await importOriginal()),
|
|
15
|
+
restrictToAccess: vi.fn(),
|
|
16
|
+
}));
|
|
17
|
+
import { registerCreateDraft } from "./tools/create-draft.js";
|
|
12
18
|
describe("main", () => {
|
|
19
|
+
const argv = process.argv;
|
|
13
20
|
beforeEach(() => {
|
|
14
21
|
vi.clearAllMocks();
|
|
22
|
+
process.argv = ["node", "imap-mcp"];
|
|
23
|
+
delete process.env.MCP_ACCESS;
|
|
15
24
|
// Mock environment variables
|
|
16
25
|
process.env.IMAP_USERNAME = "test@example.com";
|
|
17
26
|
process.env.IMAP_PASSWORD = "testpass";
|
|
@@ -20,17 +29,23 @@ describe("main", () => {
|
|
|
20
29
|
process.env.IMAP_USE_SSL = "true";
|
|
21
30
|
});
|
|
22
31
|
afterEach(() => {
|
|
32
|
+
process.argv = argv;
|
|
33
|
+
delete process.env.MCP_ACCESS;
|
|
23
34
|
vi.restoreAllMocks();
|
|
24
35
|
});
|
|
36
|
+
function mockServer() {
|
|
37
|
+
const server = { connect: vi.fn().mockResolvedValue(undefined) };
|
|
38
|
+
vi.mocked(McpServer).mockImplementation(() => server);
|
|
39
|
+
vi.mocked(StdioServerTransport).mockImplementation(() => ({}));
|
|
40
|
+
return server;
|
|
41
|
+
}
|
|
25
42
|
it("starts the mcp server", async () => {
|
|
26
43
|
const mockServer = {
|
|
27
44
|
connect: vi.fn().mockResolvedValue(undefined),
|
|
28
45
|
};
|
|
29
46
|
const mockTransport = {};
|
|
30
|
-
const mockImap = {};
|
|
31
47
|
vi.mocked(McpServer).mockImplementation(() => mockServer);
|
|
32
48
|
vi.mocked(StdioServerTransport).mockImplementation(() => mockTransport);
|
|
33
|
-
vi.mocked(Imap).mockImplementation(() => mockImap);
|
|
34
49
|
// Import and run main function
|
|
35
50
|
const { main } = await import("./index.js");
|
|
36
51
|
await main();
|
|
@@ -39,17 +54,51 @@ describe("main", () => {
|
|
|
39
54
|
name: "imap-mcp",
|
|
40
55
|
version: "0.1.0",
|
|
41
56
|
});
|
|
42
|
-
// Verify IMAP client
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
57
|
+
// Verify each tool call gets an IMAP client built from the environment
|
|
58
|
+
const createClient = vi.mocked(registerCreateDraft).mock.calls[0][0];
|
|
59
|
+
createClient();
|
|
60
|
+
expect(ImapFlow).toHaveBeenCalledWith({
|
|
46
61
|
host: "imap.example.com",
|
|
47
62
|
port: 993,
|
|
48
|
-
|
|
49
|
-
|
|
63
|
+
secure: true,
|
|
64
|
+
auth: { user: "test@example.com", pass: "testpass" },
|
|
65
|
+
tls: { rejectUnauthorized: false },
|
|
66
|
+
doSTARTTLS: false,
|
|
67
|
+
logger: false,
|
|
50
68
|
});
|
|
51
69
|
// Verify transport was created and server connected
|
|
52
70
|
expect(StdioServerTransport).toHaveBeenCalled();
|
|
53
71
|
expect(mockServer.connect).toHaveBeenCalledWith(mockTransport);
|
|
54
72
|
});
|
|
73
|
+
it("defaults to everything and logs the level on stderr", async () => {
|
|
74
|
+
mockServer();
|
|
75
|
+
const stderr = vi.spyOn(console, "error").mockImplementation(() => { });
|
|
76
|
+
const { main } = await import("./index.js");
|
|
77
|
+
await main();
|
|
78
|
+
expect(restrictToAccess).toHaveBeenCalledWith(expect.anything(), "everything");
|
|
79
|
+
expect(stderr).toHaveBeenCalledWith("imap-mcp started with access level everything");
|
|
80
|
+
});
|
|
81
|
+
it("takes the --access flag over MCP_ACCESS", async () => {
|
|
82
|
+
mockServer();
|
|
83
|
+
vi.spyOn(console, "error").mockImplementation(() => { });
|
|
84
|
+
process.env.MCP_ACCESS = "no-send";
|
|
85
|
+
process.argv = ["node", "imap-mcp", "--access", "read-only"];
|
|
86
|
+
const { main } = await import("./index.js");
|
|
87
|
+
await main();
|
|
88
|
+
expect(restrictToAccess).toHaveBeenCalledWith(expect.anything(), "read-only");
|
|
89
|
+
});
|
|
90
|
+
it("stops with exit code 2 on an unknown level", async () => {
|
|
91
|
+
const server = mockServer();
|
|
92
|
+
const stderr = vi.spyOn(console, "error").mockImplementation(() => { });
|
|
93
|
+
const exit = vi.spyOn(process, "exit").mockImplementation(() => {
|
|
94
|
+
throw new Error("exit");
|
|
95
|
+
});
|
|
96
|
+
process.env.MCP_ACCESS = "send-only";
|
|
97
|
+
const { main } = await import("./index.js");
|
|
98
|
+
await expect(main()).rejects.toThrow("exit");
|
|
99
|
+
expect(exit).toHaveBeenCalledWith(2);
|
|
100
|
+
expect(stderr).toHaveBeenCalledTimes(1);
|
|
101
|
+
expect(stderr).toHaveBeenCalledWith('imap-mcp cannot start: MCP_ACCESS (--access): "send-only" is not an access level; use read-only, no-send, everything');
|
|
102
|
+
expect(server.connect).not.toHaveBeenCalled();
|
|
103
|
+
});
|
|
55
104
|
});
|
|
@@ -1,91 +1,89 @@
|
|
|
1
1
|
import { z } from "zod/v4";
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
/** Folders tried in order when the server marks none as `\Drafts`. */
|
|
3
|
+
const FALLBACK_DRAFTS_FOLDERS = ["INBOX.Drafts", "Drafts"];
|
|
4
|
+
export function registerCreateDraft(createClient, server) {
|
|
5
|
+
server.tool("create_draft", "Creates a draft email message", {
|
|
4
6
|
to: z.string(),
|
|
5
7
|
subject: z.string(),
|
|
6
8
|
body: z.string(),
|
|
7
9
|
from: z.string().optional(),
|
|
8
10
|
}, async ({ to, subject, body, from }) => {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
body,
|
|
48
|
-
].join("\r\n");
|
|
49
|
-
client.append(message, { flags: ["\\Draft"] }, (err) => {
|
|
50
|
-
if (err) {
|
|
51
|
-
reject(new Error(`Failed to create draft: ${err.message}`));
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
resolve({
|
|
55
|
-
content: [
|
|
56
|
-
{
|
|
57
|
-
type: "text",
|
|
58
|
-
text: `Draft created successfully for ${to}`,
|
|
59
|
-
},
|
|
60
|
-
],
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
client.once("error", (err) => {
|
|
66
|
-
reject(new Error(`IMAP connection error: ${err.message}`));
|
|
67
|
-
});
|
|
68
|
-
client.connect();
|
|
69
|
-
});
|
|
11
|
+
const client = createClient();
|
|
12
|
+
// An unhandled "error" event would crash the server; the failing
|
|
13
|
+
// command rejects as well, so the tool call reports it.
|
|
14
|
+
client.on("error", () => { });
|
|
15
|
+
try {
|
|
16
|
+
await client.connect();
|
|
17
|
+
}
|
|
18
|
+
catch (err) {
|
|
19
|
+
client.close();
|
|
20
|
+
throw new Error(`IMAP connection error: ${errorMessage(err)}`);
|
|
21
|
+
}
|
|
22
|
+
try {
|
|
23
|
+
const draftsPath = await openDraftsFolder(client);
|
|
24
|
+
const message = [
|
|
25
|
+
`From: ${from || process.env.IMAP_USERNAME || ""}`,
|
|
26
|
+
`To: ${to}`,
|
|
27
|
+
`Subject: ${subject}`,
|
|
28
|
+
"",
|
|
29
|
+
body,
|
|
30
|
+
].join("\r\n");
|
|
31
|
+
try {
|
|
32
|
+
await client.append(draftsPath, message, ["\\Draft"]);
|
|
33
|
+
}
|
|
34
|
+
catch (err) {
|
|
35
|
+
throw new Error(`Failed to create draft: ${errorMessage(err)}`);
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
content: [
|
|
39
|
+
{
|
|
40
|
+
type: "text",
|
|
41
|
+
text: `Draft created successfully for ${to}`,
|
|
42
|
+
},
|
|
43
|
+
],
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
finally {
|
|
47
|
+
await client.logout().catch(() => client.close());
|
|
48
|
+
}
|
|
70
49
|
});
|
|
71
50
|
}
|
|
72
51
|
/**
|
|
73
|
-
*
|
|
74
|
-
*
|
|
52
|
+
* Opens the folder marked `\Drafts`, or else the first fallback folder that
|
|
53
|
+
* exists, and returns its path.
|
|
75
54
|
*/
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
55
|
+
async function openDraftsFolder(client) {
|
|
56
|
+
const folders = await client.list().catch(() => []);
|
|
57
|
+
const marked = findDraftsFolder(folders);
|
|
58
|
+
const candidates = marked ? [marked] : FALLBACK_DRAFTS_FOLDERS;
|
|
59
|
+
let lastError;
|
|
60
|
+
for (const path of candidates) {
|
|
61
|
+
try {
|
|
62
|
+
await client.mailboxOpen(path);
|
|
63
|
+
return path;
|
|
83
64
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
if (found)
|
|
87
|
-
return found;
|
|
65
|
+
catch (err) {
|
|
66
|
+
lastError = err;
|
|
88
67
|
}
|
|
89
68
|
}
|
|
90
|
-
|
|
69
|
+
throw new Error(`Failed to open drafts folder: ${errorMessage(lastError)}`);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Returns the path of the folder the server marks with the `\Drafts`
|
|
73
|
+
* special-use attribute (RFC 6154), or undefined if none is marked.
|
|
74
|
+
*/
|
|
75
|
+
export function findDraftsFolder(folders) {
|
|
76
|
+
return folders.find((folder) => [...folder.flags].some((flag) => flag.toLowerCase() === "\\drafts"))?.path;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* imapflow reports a server's NO or BAD as "Command failed" and keeps the
|
|
80
|
+
* server's reason in `responseText`, which is what the user needs to see.
|
|
81
|
+
*/
|
|
82
|
+
function errorMessage(err) {
|
|
83
|
+
if (!(err instanceof Error))
|
|
84
|
+
return String(err);
|
|
85
|
+
const { responseText } = err;
|
|
86
|
+
return typeof responseText === "string" && responseText
|
|
87
|
+
? responseText
|
|
88
|
+
: err.message;
|
|
91
89
|
}
|
|
@@ -1,32 +1,38 @@
|
|
|
1
1
|
import { describe, expect, test, vi } from "vitest";
|
|
2
2
|
import { registerCreateDraft } from "./create-draft.js";
|
|
3
|
-
function
|
|
3
|
+
function folder(path, flags) {
|
|
4
|
+
const segments = path.split("/");
|
|
4
5
|
return {
|
|
5
|
-
|
|
6
|
+
path,
|
|
7
|
+
pathAsListed: path,
|
|
8
|
+
name: segments[segments.length - 1],
|
|
6
9
|
delimiter: "/",
|
|
7
|
-
|
|
8
|
-
|
|
10
|
+
parent: segments.slice(0, -1),
|
|
11
|
+
parentPath: segments.slice(0, -1).join("/"),
|
|
12
|
+
flags: new Set(flags),
|
|
13
|
+
listed: true,
|
|
14
|
+
subscribed: true,
|
|
9
15
|
};
|
|
10
16
|
}
|
|
11
|
-
function setup(
|
|
12
|
-
const listeners = {};
|
|
17
|
+
function setup(folders, existing, { connectError } = {}) {
|
|
13
18
|
let openedBox;
|
|
14
19
|
const client = {
|
|
15
|
-
|
|
16
|
-
|
|
20
|
+
on: vi.fn(),
|
|
21
|
+
connect: vi.fn(async () => {
|
|
22
|
+
if (connectError)
|
|
23
|
+
throw connectError;
|
|
17
24
|
}),
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
openedBox = name;
|
|
23
|
-
cb(null);
|
|
24
|
-
}
|
|
25
|
-
else {
|
|
26
|
-
cb(new Error(`Mailbox doesn't exist: ${name}`));
|
|
25
|
+
list: vi.fn(async () => folders),
|
|
26
|
+
mailboxOpen: vi.fn(async (path) => {
|
|
27
|
+
if (!existing.includes(path)) {
|
|
28
|
+
throw new Error(`Mailbox doesn't exist: ${path}`);
|
|
27
29
|
}
|
|
30
|
+
openedBox = path;
|
|
31
|
+
return { path };
|
|
28
32
|
}),
|
|
29
|
-
append: vi.fn((
|
|
33
|
+
append: vi.fn(async () => ({ destination: openedBox })),
|
|
34
|
+
logout: vi.fn(async () => { }),
|
|
35
|
+
close: vi.fn(),
|
|
30
36
|
};
|
|
31
37
|
let handler;
|
|
32
38
|
const server = {
|
|
@@ -34,29 +40,33 @@ function setup(boxes, existing) {
|
|
|
34
40
|
handler = args[args.length - 1];
|
|
35
41
|
}),
|
|
36
42
|
};
|
|
37
|
-
registerCreateDraft(client, server);
|
|
43
|
+
registerCreateDraft(() => client, server);
|
|
38
44
|
const run = () => handler({ to: "jane@example.com", subject: "Hi", body: "Hello" });
|
|
39
|
-
return { client, run, openedBox: () => openedBox };
|
|
45
|
+
return { client, server, run, openedBox: () => openedBox };
|
|
40
46
|
}
|
|
41
|
-
describe("
|
|
47
|
+
describe("create_draft", () => {
|
|
48
|
+
test("registers the tool as create_draft", () => {
|
|
49
|
+
const { server } = setup([folder("Drafts", ["\\Drafts"])], ["Drafts"]);
|
|
50
|
+
expect(server.tool).toHaveBeenCalledWith("create_draft", expect.any(String), expect.any(Object), expect.any(Function));
|
|
51
|
+
});
|
|
42
52
|
test("saves to the folder marked \\Drafts, whatever its name and nesting", async () => {
|
|
43
|
-
const { client, run, openedBox } = setup(
|
|
44
|
-
INBOX
|
|
45
|
-
"[Gmail]"
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}, ["[Gmail]/Entwürfe", "Drafts"]);
|
|
53
|
+
const { client, run, openedBox } = setup([
|
|
54
|
+
folder("INBOX", ["\\HasNoChildren"]),
|
|
55
|
+
folder("[Gmail]", ["\\Noselect", "\\HasChildren"]),
|
|
56
|
+
folder("[Gmail]/All Mail", ["\\All"]),
|
|
57
|
+
folder("[Gmail]/Entwürfe", ["\\HasNoChildren", "\\Drafts"]),
|
|
58
|
+
folder("Drafts", []),
|
|
59
|
+
], ["[Gmail]/Entwürfe", "Drafts"]);
|
|
51
60
|
const result = await run();
|
|
52
61
|
expect(openedBox()).toBe("[Gmail]/Entwürfe");
|
|
53
|
-
expect(client.append).
|
|
62
|
+
expect(client.append).toHaveBeenCalledExactlyOnceWith("[Gmail]/Entwürfe", "From: \r\nTo: jane@example.com\r\nSubject: Hi\r\n\r\nHello", ["\\Draft"]);
|
|
54
63
|
expect(result.content[0].text).toBe("Draft created successfully for jane@example.com");
|
|
64
|
+
expect(client.logout).toHaveBeenCalledOnce();
|
|
55
65
|
});
|
|
56
66
|
test("falls back to INBOX.Drafts or Drafts when no folder is marked \\Drafts", async () => {
|
|
57
|
-
const { client, run, openedBox } = setup(
|
|
67
|
+
const { client, run, openedBox } = setup([folder("INBOX", []), folder("Drafts", [])], ["Drafts"]);
|
|
58
68
|
await run();
|
|
59
|
-
expect(client.
|
|
69
|
+
expect(client.mailboxOpen.mock.calls.map((c) => c[0])).toEqual([
|
|
60
70
|
"INBOX.Drafts",
|
|
61
71
|
"Drafts",
|
|
62
72
|
]);
|
|
@@ -64,8 +74,19 @@ describe("create-draft", () => {
|
|
|
64
74
|
expect(client.append).toHaveBeenCalledOnce();
|
|
65
75
|
});
|
|
66
76
|
test("fails when no drafts folder can be opened", async () => {
|
|
67
|
-
const { client, run } = setup(
|
|
77
|
+
const { client, run } = setup([folder("INBOX", [])], []);
|
|
68
78
|
await expect(run()).rejects.toThrow("Failed to open drafts folder");
|
|
69
79
|
expect(client.append).not.toHaveBeenCalled();
|
|
80
|
+
expect(client.logout).toHaveBeenCalledOnce();
|
|
81
|
+
});
|
|
82
|
+
test("reports a connection or login failure as a tool error", async () => {
|
|
83
|
+
// imapflow's shape for a server's NO: a generic message, the reason apart
|
|
84
|
+
const connectError = Object.assign(new Error("Command failed"), {
|
|
85
|
+
responseText: "[AUTHENTICATIONFAILED] Invalid credentials",
|
|
86
|
+
});
|
|
87
|
+
const { client, run } = setup([], [], { connectError });
|
|
88
|
+
await expect(run()).rejects.toThrow("IMAP connection error: [AUTHENTICATIONFAILED] Invalid credentials");
|
|
89
|
+
expect(client.on).toHaveBeenCalledWith("error", expect.any(Function));
|
|
90
|
+
expect(client.list).not.toHaveBeenCalled();
|
|
70
91
|
});
|
|
71
92
|
});
|
package/package.json
CHANGED
|
@@ -2,14 +2,16 @@
|
|
|
2
2
|
"name": "imap-mcp",
|
|
3
3
|
"mcpName": "io.github.langmail/imap-mcp",
|
|
4
4
|
"description": "An MCP server that lets your AI agent save email drafts to any IMAP mailbox.",
|
|
5
|
-
"version": "
|
|
5
|
+
"version": "1.1.0",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"bin": {
|
|
9
9
|
"imap-mcp": "dist/index.js"
|
|
10
10
|
},
|
|
11
11
|
"files": [
|
|
12
|
-
"dist"
|
|
12
|
+
"dist",
|
|
13
|
+
"LICENSE-APACHE",
|
|
14
|
+
"LICENSE-MIT"
|
|
13
15
|
],
|
|
14
16
|
"scripts": {
|
|
15
17
|
"build": "tsc && shx chmod +x dist/*.js",
|
|
@@ -32,11 +34,11 @@
|
|
|
32
34
|
},
|
|
33
35
|
"homepage": "https://github.com/langmail/imap-mcp#readme",
|
|
34
36
|
"author": "Dominik Grusemann",
|
|
35
|
-
"license": "MIT",
|
|
37
|
+
"license": "MIT OR Apache-2.0",
|
|
36
38
|
"dependencies": {
|
|
37
39
|
"@modelcontextprotocol/sdk": "^1.32.2",
|
|
38
40
|
"dotenv": "^16.5.0",
|
|
39
|
-
"
|
|
41
|
+
"imapflow": "^2.12.2",
|
|
40
42
|
"zod": "^3.25.67"
|
|
41
43
|
},
|
|
42
44
|
"devDependencies": {
|
|
@@ -45,7 +47,6 @@
|
|
|
45
47
|
"@semantic-release/git": "^10.0.1",
|
|
46
48
|
"@semantic-release/github": "^12.0.11",
|
|
47
49
|
"@semantic-release/npm": "^13.2.0",
|
|
48
|
-
"@types/imap": "^0.8.42",
|
|
49
50
|
"@types/node": "^24.0.4",
|
|
50
51
|
"eslint": "^9.29.0",
|
|
51
52
|
"eslint-config-prettier": "^10.1.5",
|