shanghaitech-mail-mcp
Click on "Deploy Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@shanghaitech-mail-mcp search my inbox for unread emails from this week"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
上科大邮箱 MCP(shanghaitech-mail)
把 @shanghaitech.edu.cn 邮箱接到 DeepSeek Harness:能搜、能读、能存附件、能起草、能发、能归档、能删。
服务器是 Coremail,已实测:
协议 | 地址 | 端口 | 加密 |
IMAP | mail.shanghaitech.edu.cn | 993 | TLS 1.3(证书 |
SMTP | mail.shanghaitech.edu.cn | 465 | SSL/TLS |
⚠️ 这些地址解析到
10.15.x.x校内地址,必须连校园网或学校 VPN 才能用。不开 VPN 时工具会报连接超时。
一次性配置(1 分钟)
cd "D:\dongshen daily life\工具\mail-mcp"
powershell -ExecutionPolicy Bypass -File .\配置邮箱凭据.ps1脚本会问你三件事(账号前缀、发信署名、密码),密码输入时不回显,会先拿它真连一次 IMAP 验证,验证通过才写 secret.json(无 BOM、权限收紧到仅当前用户)。密码不会出现在对话里、不会进命令历史。
脚本化用法(明文密码会进命令历史,仅在自测时用):
.\配置邮箱凭据.ps1 -Account your-id -Password 'xxx' -DisplayName 'Your Name'如果学校开了「客户端专用密码 / 授权码」,请在网页版
设置 → 客户端设置里生成一个填进去,而不是登录密码。如果 IMAP 登录被拒,多半是网页版里 IMAP 服务没开。
必须连校园网或学校 VPN,邮箱域名解析到
10.15.x.x校内地址。
Related MCP server: email-mcp-server
十个工具
工具 | 干什么 | 会不会改邮箱 |
| 列文件夹 + 每箱邮件数/未读数 | 只读 |
| 按发件人/主题/正文/日期/未读/附件搜,返回元数据+摘要 | 只读 |
| 读全文(默认顺带标已读,可关) | 可关 |
| 附件存到工作区 | 只读 |
| 按发件人聚合「谁发得最多」,判断哪些是群发可清 | 只读 |
| 标已读/未读/星标 | 改标记 |
| 移动到其他文件夹 | 改位置 |
| 写进草稿箱,不发送;支持附件 | 加草稿 |
| 真发出去;支持附件 | 发信 |
| 删除 | 见下 |
附件怎么用
mail_send 和 mail_draft 都接受 attachments 参数,三种写法:
// 写法一:只给路径,文件名取自磁盘
"attachments": ["D:\\a\\b.pdf"]
// 写法二:给显示名(「显示名=路径」,左侧不能是盘符)
"attachments": ["实验报告.pdf=D:\\a\\b.pdf"]
// 写法三:对象,可指定文件名与 MIME
"attachments": [{"path": "D:\\a\\b.pdf", "filename": "报告.pdf", "contentType": "application/pdf"}]
// 可以混用多个
"attachments": ["报告.pdf=D:\\a\\r.pdf", "D:\\a\\图1.png"]规则:
项 | 说明 |
路径 | 绝对路径,或相对工作区( |
MIME | 按扩展名推断(常见 30 种);推断不出用 |
单个上限 | 20 MB |
合计上限 | 25 MB(学校 Coremail 通常卡在 25–30 MB) |
校验 | 文件不存在 / 是目录 / 0 字节 / 超限,都会在发信前报错,不会发出一封残缺邮件 |
返回值 | 只回 |
返回值示例:
{
"sent": true,
"messageId": "<...@shanghaitech.edu.cn>",
"accepted": ["teacher@example.edu.cn"],
"rejected": [],
"attachments": [
{ "filename": "聚餐照片_20261130.jpg", "contentType": "image/jpeg", "sizeKB": 563 }
]
}⚠️ 中文文件名会被同时编码成 RFC 2047(=?UTF-8?B?..?=``)和 RFC 2231(filename0=)两种形式, 这是 nodemailer 的正常行为,收件端能正确还原。自己写测试时**不要手工切 base64 段**,用 mailparser` 解析。
删除的保险设计
按你的要求做了「能主动删,但删错了能捞回来」:
默认只移入垃圾箱(
永久删除: false)——可恢复。只有
permanent: true才 EXPUNGE 真删,不可恢复。如果邮件本来就在垃圾箱里,再删才等于真删。
批量清理前先用
mail_cleanup_report出「按发件人聚合」清单,看清楚是群发通知(可清)还是真人邮件(别删)。
自检
node tests.mjs # 35 个纯函数单测(正文分段/HTML 转文本/地址格式化/附件校验)
node selftest.mjs # MCP 协议层 + 11 个工具注册
node integration.mjs # 端到端:起假 IMAP/SMTP 服务,跑完全部工具(21 项断言,含附件收发)
node selftest.mjs --live # 真连学校邮箱列文件夹(需要先配好凭据)集成测试不碰真实邮箱、不需要凭据:它用 hoodiecrow-imap 起一个严格的 IMAP 服务器、smtp-server 起一个收信桩,把 INBOX 种上纯文本/HTML/带附件的邮件,然后通过 stdio 驱动 server.js 走完「列文件夹 → 搜索 → 读全文 → 存附件 → 标已读 → 移动 → 删除(可恢复)→ 彻底删 → 清理体检 → 起草 → 发送」。
开发中踩到并修掉的坑(留档,改代码前先看)
PS 5.1 读无 BOM 的 UTF-8 脚本会把中文按 GBK 解码,导致引号配不上、整个脚本语法报错(
意外的标记、字符串缺少终止符)。所有.ps1必须存成 UTF-8 带 BOM。反过来,secret.json必须无 BOM——PS 5.1 的Set-Content -Encoding UTF8会加 BOM,而 Node 的JSON.parse不接受开头 U+FEFF。两边都用.NET的UTF8Encoding($true/$false)精确控制。中文检索词不能直接塞给 IMAP SEARCH。 RFC 3501 要求带
CHARSET UTF-8+ 8bit 字面量,而 imapflow 只发带引号的 8bit 字符串,严格服务器直接BAD。所以mail_search现在分两条路:ASCII 条件走服务端 SEARCH;非 ASCII 条件先按日期/未读收窄(上限 800 封),再本地逐封比对主题/发件人/正文。bodyStructure里没有subtype字段。 imapflow 把 MIME 类型合并成一个type字符串('text/plain'),要自己 split。imapflow 的
fetch是流式的,流没读完前不能发download,否则偶发 "connection busy"。摘要必须放第二趟。messageMove失败时返回false而不是抛错,必须自己判定,否则移动失败会被当成成功。mailparser的from是{value: [...]}对象,imapflow 的envelope.from是数组,两者要统一归一化,否则发件人显示为空。fetch按服务器顺序(升序)返回,跟我们排好的倒序 uid 列表对不上,必须按 uid 重排。工具返回值必须用
ok()包装,否则 MCP SDK 会把对象字段铺平到 result 上、content变成空数组。中文附件的文件名会被双重编码:MIME 里同时出现
Content-Type: ... name="=?UTF-8?B?..?="(RFC 2047) 和Content-Disposition: attachment; filename*0*=utf-8''%E8%81%9A...(RFC 2231)。 这是 nodemailer 的标准行为,收件端能还原。写测试时不要自己切 base64 段—— 按Content-Transfer-Encoding: base64切分会把后续边界一起吃掉,导致误判附件内容缺失。 正确做法:拿整封原文交给mailparser的simpleParser,读parsed.attachments[0].content。smtp-server 测试桩必须实现
onAuth,否则 nodemailer 连上后会收到535 Authentication not implemented。只设authOptional: true不够, 一定要给onAuth回调返回callback(null, { user: auth.username })。
文件
server.js—— MCP server 本体(IMAP/SMTP,每次调用独立建连用完即登出)配置邮箱凭据.ps1—— 交互式配置 + 登录验证tests.mjs/selftest.mjs/integration.mjs—— 三层测试secret.json—— 你的凭据(别提交、别外传)secret.example.json—— 凭据模板
Available Tools
11 toolsmail_autoclean按规则自动清理(幂等,可定时跑)A
按预设规则自动把垃圾邮件移入垃圾箱,不动任何正常邮件。默认 dry_run=true 只报告;dry_run=false 才真的移动。规则一:Twitch 直播推送,发出超过 10 小时即清。规则二:掠夺性期刊/期刊约稿与「代写代发」类骚扰(IntechOpen 等),见到即清,不看年龄。规则三:同一发件人在 24 小时内重复推送的直播/提醒类通知,只留最新一封。所有移动都进 Trash(可恢复),不会永久删除。
Inputs: folder, dry_run, twitch_max_age_hours.
mail_cleanup_report邮箱清理体检(按发件人聚合)A
扫描文件夹里最近 N 封邮件,按发件人地址聚合成「谁发得最多」的清单,附带每组的未读数、最早/最晚时间、若干封 uid 与主题样例,用来判断哪些是群发通知/订阅资讯(可批量清)、哪些是真人邮件(别删)。纯只读,不删任何东西。
Inputs: top, scan, folder, unread_only.
mail_delete删除邮件(默认移入垃圾箱,可恢复)A
删除邮件。默认行为是「移入垃圾箱」,可恢复、可再找回;只有 permanent=true 才真正 EXPUNGE 彻底删掉(不可恢复)。批量清理请先用 mail_cleanup_report 出清单、确认后再删。
Inputs: uids (required), folder, permanent.
mail_draft起草邮件并存入草稿箱A
把一封邮件写进邮箱的草稿箱(IMAP Drafts),不发送。适合「先拟好,我自己再改改看」的场景。发送请用 mail_send。
Inputs: to (required), body (required), subject (required), cc, +2 more.
mail_list_folders列出邮箱文件夹A
列出上科大邮箱里所有 IMAP 文件夹(收件箱、已发送、草稿箱、垃圾箱、自定义分类),含每个文件夹的邮件总数与未读数。写检索前先调用它确认文件夹名。
Inputs: no named parameters.
mail_mark标记邮件状态B
给邮件加/去标记:已读、未读、星标。批量操作传 uids 数组。
Inputs: flag (required), uids (required), folder.
mail_move移动邮件到其他文件夹B
把邮件从一个文件夹移动到另一个(例如归档到自定义分类、移到垃圾箱)。
Inputs: uids (required), to_folder (required), folder.
mail_read读取邮件全文A
按 uid 读取一封邮件的完整内容:主题、收发件人、时间、正文纯文本、附件清单(附件不会自动下载,需要正文后用 mail_save_attachment 另存)。默认读完即标记为已读;mark_read=false 可保持未读。
Inputs: uid (required), folder, mark_read, max_chars.
mail_save_attachment保存附件到工作区A
把某封邮件的附件落盘到工作区(可用于把助教的 PPT / 作业 PDF 存到本地再解析)。index 来自 mail_read 返回的 attachments 列表。默认存到 _素材/邮箱附件/。
Inputs: uid (required), index (required), folder, save_dir.
mail_search搜索邮件A
在指定文件夹内搜索邮件,返回元数据列表(uid / 主题 / 发件人 / 日期 / 已读状态 / 大小 / 可选摘要),不返回正文全文。支持按发件人、收件人、主题、正文关键词、日期区间、未读、附件存在等条件。用于「这周有哪些通知」「助教发过什么邮件」「找某个附件」这类需求。
Inputs: to, body, from, text, +9 more.
mail_send发送邮件A
通过学校 SMTP 直接发送邮件(会真的发出去)。除非用户明确要求发送,否则优先用 mail_draft 存草稿让用户过目。
Inputs: to (required), body (required), subject (required), cc, +4 more.
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
TDQS
Scored across 11 tools
Each tool targets a distinct resource+action: folder listing, search, read, attachment save, draft, send, mark, move, delete, cleanup report and autoclean. Minor overlap exists between mail_delete and mail_move (default delete is a move to Trash) and between mail_cleanup_report and mail_autoclean, but the descriptions explicitly state the difference (read-only report vs. dry-run action) so misselection is unlikely.
All names use the mail_ prefix with snake_case, which is highly consistent. Most follow verb_noun (mail_list_folders, mail_save_attachment, mail_mark), with a few noun-style names (mail_draft, mail_cleanup_report) that break the pattern slightly but remain readable and predictable.
11 tools is squarely within the well-scoped 3-15 range for an email client surface. Every tool covers a meaningful capability (read, send, organize, clean) rather than duplicating functionality.
The surface covers the full email lifecycle: discover folders, search, read, fetch attachments, draft, send, flag, move, delete, plus cleanup reporting and rule-based autoclean. Notable gaps are reply/forward helpers and any way to edit or delete an existing draft, but agents can work around these by composing a new draft or sending directly.
Related MCP Connectors
Email infrastructure for AI agents — send, receive, search, and reply to email over MCP.
Hosted email MCP for your own Gmail, Outlook.com, Microsoft 365, iCloud or IMAP inbox: read, search, draft, reply in thread, forward and file mail. It moves or flags up to 500 messages in one call, and a send leaves exactly one copy in Sent. A calendar is a separate connection, and connecting one adds diary and scheduling tools.
Connect any IMAP/SMTP mailbox to Claude & ChatGPT: search, read, sort, draft. Sends only with OK.
Your own IMAP mailbox in any MCP client: search, read, flag, move; send and delete need confirm.
Related MCP Servers
- AlicenseNot gradedqualityCmaintenanceConnects any IMAP/SMTP mailbox to AI agents via MCP, enabling email read, search, send, reply, and management through natural language.14 npmMIT
- FlicenseBqualityDmaintenanceEnables external AI agents to read, send, and manage email over IMAP/SMTP via MCP, including inbox listing, search, drafts, scheduled/batch sending, and operations like reply, archive, and labels.303-
- AlicenseBqualityBmaintenanceConnects MCP clients to any IMAP/SMTP email account, enabling email search, reading, sending, replying, forwarding, flagging, moving, and folder management via natural language.17MIT
- AlicenseNot gradedqualityCmaintenanceConnects MCP-compatible AI assistants to any IMAP/SMTP mailbox, enabling email search, reading, folder management, and sending with automatic saving to Sent.1MIT