Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Apple-Tools-MCP/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
"mail_visible_drafts": {
"type": "boolean",
"title": "Show Mail drafts on screen",
"description": "Open new drafts in a visible Mail window so you can review them. Sets APPLE_MAIL_MCP_VISIBLE_DRAFTS.",
"description": "Leave new drafts open in a Mail window so you can review them. Turn off on a Mac nobody watches: drafts, and replies and forwards saved as drafts, then go to the Drafts mailbox with no window left open. Sets APPLE_MAIL_MCP_VISIBLE_DRAFTS.",
"required": false,
"default": true
},
Expand Down
10 changes: 10 additions & 0 deletions AppleMail-MCP/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ To change it in Claude Desktop, open Settings > Extensions, choose Apple Mail, c

Attachments are off until you set `APPLE_MAIL_MCP_ALLOWED_ATTACHMENT_ROOT` (bundle setting: "Mail attachment folder") to a dedicated folder.

## Draft Windows

By default a new draft stays open in a Mail window so you can review it. So does a reply or forward that was saved as a draft.

On a Mac nobody watches, those windows pile up. Set `APPLE_MAIL_MCP_VISIBLE_DRAFTS=false` (bundle setting: "Show Mail drafts on screen", turned off) and every draft is saved to the Drafts mailbox with no window left on screen. The tool result reports `visible: false`, and for replies and forwards the `note` says to open the draft from Drafts.

- A reply, a forward, or a new draft with `from_account` still opens its window for a moment before it is hidden. A new draft without `from_account` never opens one.
- Mail keeps a hidden draft loaded until Mail next quits. The draft is already saved, so quitting loses nothing.
- A draft is hidden only after Mail saved it. If the save fails, the window stays open and the result reports `visible: true`. A new draft that has no window fails with `DRAFT_SAVE_UNCONFIRMED` instead; check Drafts before retrying, because Mail may still save it.

## Transport

`stdio` is the default and recommended transport. Set `APPLE_MAIL_MCP_TRANSPORT=streamable-http` (with optional `APPLE_MAIL_MCP_HOST` and `APPLE_MAIL_MCP_PORT`) to serve Streamable HTTP instead.
Expand Down
2 changes: 1 addition & 1 deletion AppleMail-MCP/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"mail_visible_drafts": {
"type": "boolean",
"title": "Show Mail drafts on screen",
"description": "Open new drafts in a visible Mail window so you can review them. Sets APPLE_MAIL_MCP_VISIBLE_DRAFTS.",
"description": "Leave new drafts open in a Mail window so you can review them. Turn off on a Mac nobody watches: drafts, and replies and forwards saved as drafts, then go to the Drafts mailbox with no window left open. Sets APPLE_MAIL_MCP_VISIBLE_DRAFTS.",
"required": false,
"default": true
}
Expand Down
2 changes: 1 addition & 1 deletion AppleMail-MCP/server.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
},
{
"name": "APPLE_MAIL_MCP_VISIBLE_DRAFTS",
"description": "Whether compose tools open visible draft windows in Mail.app.",
"description": "Whether drafts stay open in a Mail window. false saves them to the Drafts mailbox and leaves no window open.",
"isRequired": false,
"format": "boolean",
"default": "true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,12 @@ on run argv
set senderString to ""

tell application "Mail"
set effectiveVisible to shouldBeVisible
-- A draft with a sender starts in a window even when drafts are
-- hidden: the From pop-up fallback needs one, and Mail never shows a
-- draft that was created hidden. It is hidden again after the save.
set openInWindow to shouldBeVisible
if senderRaw is not "" then
set effectiveVisible to true
set openInWindow to true
end if
-- Resolve the sender before opening a compose window, so an unknown
-- account fails with ACCOUNT_NOT_FOUND without leaving a stray draft.
Expand All @@ -299,7 +302,7 @@ on run argv
set senderString to my senderStringForAccount(senderEmail, my fullNameForAccount(matchedAccount))
end if

set newMessage to make new outgoing message with properties {visible:effectiveVisible, subject:subjectText, content:bodyText}
set newMessage to make new outgoing message with properties {visible:openInWindow, subject:subjectText, content:bodyText}
tell newMessage
repeat with addressText in toList
if (addressText as text) is not "" then
Expand Down Expand Up @@ -329,6 +332,27 @@ on run argv
end if

tell application "Mail"
-- Save now: a hidden draft otherwise reaches Drafts only when Mail
-- next autosaves, and is lost if Mail quits first.
set draftSaved to false
try
save newMessage
set draftSaved to true
end try
if not shouldBeVisible then
if draftSaved then
-- Hide only a saved draft, and never close it. Closing the
-- draft or its window while the save is still running
-- deletes the draft, and Mail does not say when it finished.
try
set visible of newMessage to false
end try
else if not openInWindow then
-- Unsaved with no window: nobody could find it. A draft that
-- has a window just stays open and is reported as visible.
error "DRAFT_SAVE_UNCONFIRMED: Mail did not save the hidden draft. It can still reach the Drafts mailbox when Mail next autosaves, so check Drafts for this subject before retrying, or a second copy may be created."
end if
end if
return my sanitizeText((id of newMessage) as text) & fieldSeparator & my boolText(visible of newMessage) & fieldSeparator & resolvedAccountLabel & recordSeparator
end tell
end run
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ on run argv
if (count of argv) ≥ 7 then set sendNow to ((item 7 of argv) is not "false")
set attachmentDir to ""
if (count of argv) ≥ 8 then set attachmentDir to item 8 of argv
-- "false" hides a saved draft's window so none is left on screen.
set showDraft to true
if (count of argv) ≥ 9 then set showDraft to ((item 9 of argv) is not "false")

set listSeparator to ASCII character 29
set fieldSeparator to ASCII character 31
Expand Down Expand Up @@ -397,13 +400,29 @@ on run argv
set resolvedAccountLabel to my sanitizeText(my applySender(forwardMessage, senderRaw, senderEmail, senderString))
end if

set draftSaved to false
set draftVisible to true
tell application "Mail"
if sendNow then
send forwardMessage
else
-- Keep the compose window open; Mail autosaves it to Drafts.
-- Save the draft to Drafts and leave it open, or hidden when asked.
try
save forwardMessage
set draftSaved to true
end try
if draftSaved and not showDraft then
-- Hide only a saved draft, and never close it. Closing the
-- draft or its window while the save is still running
-- deletes the draft.
try
set visible of forwardMessage to false
end try
end if
-- Report what Mail shows, so a failed save or hide is not
-- described as a hidden draft.
try
set draftVisible to visible of forwardMessage
end try
end if

Expand All @@ -412,6 +431,6 @@ on run argv
set subjectText to my sanitizeText(subject of forwardMessage)
end try

return my boolText(sendNow) & fieldSeparator & subjectText & fieldSeparator & resolvedAccountLabel & recordSeparator
return my boolText(sendNow) & fieldSeparator & subjectText & fieldSeparator & resolvedAccountLabel & fieldSeparator & my boolText(draftVisible) & recordSeparator
end tell
end run
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,9 @@ on run argv
-- "false" leaves the reply open in Mail as an unsent draft for the user to review.
set sendNow to true
if (count of argv) ≥ 7 then set sendNow to ((item 7 of argv) is not "false")
-- "false" hides a saved draft's window so none is left on screen.
set showDraft to true
if (count of argv) ≥ 8 then set showDraft to ((item 8 of argv) is not "false")

set fieldSeparator to ASCII character 31
set recordSeparator to ASCII character 30
Expand Down Expand Up @@ -352,13 +355,29 @@ on run argv
set resolvedAccountLabel to my sanitizeText(my applySender(replyMessage, senderRaw, senderEmail, senderString))
end if

set draftSaved to false
set draftVisible to true
tell application "Mail"
if sendNow then
send replyMessage
else
-- Keep the compose window open; Mail autosaves it to Drafts.
-- Save the draft to Drafts and leave it open, or hidden when asked.
try
save replyMessage
set draftSaved to true
end try
if draftSaved and not showDraft then
-- Hide only a saved draft, and never close it. Closing the
-- draft or its window while the save is still running
-- deletes the draft.
try
set visible of replyMessage to false
end try
end if
-- Report what Mail shows, so a failed save or hide is not
-- described as a hidden draft.
try
set draftVisible to visible of replyMessage
end try
end if

Expand All @@ -367,6 +386,6 @@ on run argv
set subjectText to my sanitizeText(subject of replyMessage)
end try

return my boolText(sendNow) & fieldSeparator & subjectText & fieldSeparator & my boolText(shouldReplyAll) & fieldSeparator & resolvedAccountLabel & recordSeparator
return my boolText(sendNow) & fieldSeparator & subjectText & fieldSeparator & my boolText(shouldReplyAll) & fieldSeparator & resolvedAccountLabel & fieldSeparator & my boolText(draftVisible) & recordSeparator
end tell
end run
3 changes: 2 additions & 1 deletion AppleMail-MCP/src/apple_mail_mcp/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
SUPPORTED_TRANSPORTS = {"stdio", "streamable-http"}
DEFAULT_SCRIPT_TIMEOUT_SECONDS = 120
MIN_SCRIPT_TIMEOUT_SECONDS = 5
VISIBLE_DRAFTS_ENV_VAR = "APPLE_MAIL_MCP_VISIBLE_DRAFTS"


@dataclass(slots=True)
Expand Down Expand Up @@ -85,6 +86,6 @@ def load_settings() -> Settings:
log_level=os.getenv("APPLE_MAIL_MCP_LOG_LEVEL", "INFO").upper(),
allowed_attachment_root=allowed_attachment_root,
default_search_limit=default_search_limit,
visible_drafts=_parse_bool(os.getenv("APPLE_MAIL_MCP_VISIBLE_DRAFTS"), True),
visible_drafts=_parse_bool(os.getenv(VISIBLE_DRAFTS_ENV_VAR), True),
script_timeout_seconds=_parse_script_timeout(os.getenv("APPLE_MAIL_MCP_SCRIPT_TIMEOUT_SECONDS")),
)
34 changes: 32 additions & 2 deletions AppleMail-MCP/src/apple_mail_mcp/mail_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ def _send_status_unknown(exc: MailScriptTimeoutError, action: str, account: str
)


def _draft_visible(row: list[str], index: int) -> bool:
# The script reports whether the draft's window is open. A window is
# assumed when the field is missing, which is what the draft tools did
# before they could hide one.
return _parse_bool(row[index]) if len(row) > index and row[index] else True


def _split_list(raw: str) -> list[str]:
if not raw:
return []
Expand Down Expand Up @@ -287,12 +294,22 @@ def reply_message(
reply_all: bool = False,
from_account: str | None = None,
send: bool = True,
visible: bool = True,
) -> ReplyRecord:
account, mailbox, apple_id = decode_message_id(message_id)
try:
raw = self._run_script(
"reply_message.applescript",
[account, mailbox, apple_id, body, "true" if reply_all else "false", from_account or "", "true" if send else "false"],
[
account,
mailbox,
apple_id,
body,
"true" if reply_all else "false",
from_account or "",
"true" if send else "false",
"true" if visible else "false",
],
)
except MailScriptTimeoutError as exc:
# A draft-mode reply never sends, so its timeout is an ordinary failure.
Expand All @@ -311,6 +328,7 @@ def reply_message(
subject=_restore_text(subject),
reply_all=_parse_bool(reply_all_flag),
from_account=resolved_from_account,
visible=None if sent else _draft_visible(rows[0], 4),
)

def forward_message(
Expand All @@ -320,14 +338,25 @@ def forward_message(
body: str = "",
from_account: str | None = None,
send: bool = True,
visible: bool = True,
) -> ForwardRecord:
account, mailbox, apple_id = decode_message_id(message_id)
try:
# A forward with a body copies the original's attachments here to add them back.
with TemporaryDirectory(prefix="apple-mail-mcp-") as attachment_dir:
raw = self._run_script(
"forward_message.applescript",
[account, mailbox, apple_id, LIST_SEPARATOR.join(to), body, from_account or "", "true" if send else "false", attachment_dir],
[
account,
mailbox,
apple_id,
LIST_SEPARATOR.join(to),
body,
from_account or "",
"true" if send else "false",
attachment_dir,
"true" if visible else "false",
],
)
except MailScriptTimeoutError as exc:
# A draft-mode forward never sends, so its timeout is an ordinary failure.
Expand All @@ -346,6 +375,7 @@ def forward_message(
subject=_restore_text(subject),
to=to,
from_account=resolved_from_account,
visible=None if sent else _draft_visible(rows[0], 3),
)

def mark_message(self, message_id: str, is_read: bool) -> MarkRecord:
Expand Down
6 changes: 6 additions & 0 deletions AppleMail-MCP/src/apple_mail_mcp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ class ReplyRecord:
from_account: str | None = None
draft: bool = False
note: str | None = None
# For a draft: whether its window is open in Mail. None when sent.
visible: bool | None = None

def to_dict(self) -> dict[str, Any]:
return {
Expand All @@ -179,6 +181,7 @@ def to_dict(self) -> dict[str, Any]:
"from_account": self.from_account,
"draft": self.draft,
"note": self.note,
"visible": self.visible,
}


Expand All @@ -190,6 +193,8 @@ class ForwardRecord:
from_account: str | None = None
draft: bool = False
note: str | None = None
# For a draft: whether its window is open in Mail. None when sent.
visible: bool | None = None

def to_dict(self) -> dict[str, Any]:
return {
Expand All @@ -199,6 +204,7 @@ def to_dict(self) -> dict[str, Any]:
"from_account": self.from_account,
"draft": self.draft,
"note": self.note,
"visible": self.visible,
}


Expand Down
16 changes: 13 additions & 3 deletions AppleMail-MCP/src/apple_mail_mcp/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from mcp.types import Annotations, InputRequiredResult, PromptMessage, ToolAnnotations
from pydantic import Field

from apple_mail_mcp.config import Settings, load_settings
from apple_mail_mcp.config import VISIBLE_DRAFTS_ENV_VAR, Settings, load_settings
from apple_mail_mcp.mail_bridge import AppleMailBridge, MailBridgeError, MailSendStatusUnknownError
from apple_mail_mcp.models import (
DeleteRecord,
Expand Down Expand Up @@ -265,6 +265,14 @@ def mail_send_message_tool(
return result


def _draft_review_hint(visible: bool | None) -> str:
# Follows what Mail reported, not the setting: a draft Mail could not
# save or hide is still open on screen.
if visible is False:
return f"No window was left open, because {VISIBLE_DRAFTS_ENV_VAR} is off. Ask the user to open it from the Drafts mailbox in Mail to review and send it."
return "Ask the user to review and send it in Mail."


def mail_reply_message_tool(
bridge: AppleMailBridge,
settings: Settings,
Expand All @@ -284,11 +292,12 @@ def mail_reply_message_tool(
reply_all=reply_all,
from_account=from_account,
send=send,
visible=settings.visible_drafts,
)
if not result.sent:
result.note = (
f"Reply saved as a draft in Mail and NOT sent, because {SAFETY_ENV_VAR} is "
f"'{settings.safety_profile.value}'. Ask the user to review and send it in Mail. "
f"'{settings.safety_profile.value}'. {_draft_review_hint(result.visible)} "
f"To send replies directly, {how_to_set(SAFETY_ENV_VAR, SafetyProfile.FULL_ACCESS.value)}."
)
LOGGER.info("mail_reply_message ok message_id=%s reply_all=%s sent=%s", message_id, reply_all, result.sent)
Expand All @@ -314,11 +323,12 @@ def mail_forward_message_tool(
body=body,
from_account=from_account,
send=send,
visible=settings.visible_drafts,
)
if not result.sent:
result.note = (
f"Forward saved as a draft in Mail and NOT sent, because {SAFETY_ENV_VAR} is "
f"'{settings.safety_profile.value}'. Ask the user to review and send it in Mail. "
f"'{settings.safety_profile.value}'. {_draft_review_hint(result.visible)} "
f"To send forwards directly, {how_to_set(SAFETY_ENV_VAR, SafetyProfile.FULL_ACCESS.value)}."
)
LOGGER.info("mail_forward_message ok message_id=%s recipients=%s sent=%s", message_id, len(to), result.sent)
Expand Down
Loading
Loading