Skip to main content
Glama
Apple Messages MCP

Apple Messages MCP

by nweii
README.md
# Apple Messages MCP

Let Claude, ChatGPT, and other compatible AI assistants read selected iMessage conversations on your Mac, with optional approval before anything is sent.

`apple-messages-mcp` is a self-hosted MCP server that runs on a Mac signed into Apple Messages. Instead of handing an AI app your whole inbox, you use a private browser page to approve specific 1:1 chats and group chats for each connected app. Each server runs for the Messages account on one Mac.

## How it works

Once the server is running and connected to your AI app:

1. You ask your assistant something like "Check what Alex texted about Saturday" or "Summarize what I missed in the Weekend Crew group chat."
2. If you have already shared that conversation with that app, the assistant reads the recent messages and answers.
3. If you have not shared that chat yet, the assistant cannot see it. Instead, it gives you a link to your server's private approval page.
4. You open the link on your Mac or phone, sign in with your approval password, and choose **Allow** (for 24 hours or **Until revoked**) or **Deny**. You can also open `/approvals` anytime to add or revoke chats ahead of time.

If you enable sending in approval mode, outgoing texts work the same way: your assistant submits a draft and hands you a link showing the exact recipients, text, and attachments, and nothing leaves your Mac until you tap **Send**.

Because Apple does not offer a web API for iMessage, the server has to run on a Mac that stays signed into Messages.

### Why not just use `imsg`?

This server uses [`imsg`](https://github.com/steipete/imsg) under the hood. If you only run local command-line agents on your Mac and are comfortable giving them access to your full Messages database, calling `imsg` directly takes less setup.

`apple-messages-mcp` adds the remote access and guardrails on top of `imsg`: OAuth sign-in for cloud and mobile AI apps (such as Claude and ChatGPT), per-app and per-chat read approvals from a browser, a rolling message age limit, read-only defaults that never mark messages read, and browser draft review with an hourly send cap if you enable sending.

## Features

- **Approve access one chat at a time.** Works for both 1:1 conversations and group chats (named or unnamed). Each connected AI app gets its own list of allowed chats, managed from a private browser page, and you can revoke access immediately without restarting the server.
- **Read-only by default, with unread badges intact.** Sending is off until you turn it on, and reading messages never marks them read in Apple Messages.
- **Control how far back apps can read.** A rolling age limit (30 days by default) applies to every read, search, and attachment. Choose whether apps receive attachment files, file details only, or no attachments at all, up to a configurable size limit.
- **Approve one-off searches across all chats.** Normal search only checks chats the AI app can already see. Optionally let an app request a single search across your inbox. You review the exact search text, date range, and match limit before releasing the results once.
- **Optional sending, with draft approval and an hourly cap.** Sending stays off by default. Turn it on when you want assistants to send from non-Apple devices, attach generated files, queue drafts for one-tap browser approval, or send automatically to preapproved chats. Every mode enforces an hourly send cap and never retries an uncertain send.
- **Manage each connected AI app separately.** Connect compatible AI apps with standard OAuth sign-in. From `/approvals/connections`, see paired apps and when they were last used, disconnect one to revoke its access, or replace a re-paired app while choosing which chat permissions to carry over.
- **Keep message text out of instructions and logs.** The server returns incoming message text and text attachments in a separate `sender_text` field rather than mixing them into server prose. Local logs record call metadata only and exclude message contents, search queries, and attachment files.

## What you need

To run the server on your Mac, you need:

- A Mac running macOS and signed into Apple Messages.
- [Bun](https://bun.sh/) 1.3.11 or later.
- [`imsg`](https://github.com/steipete/imsg) installed with Homebrew (tested with `0.21.1`; other versions log a startup warning).
- An AI app that supports remote MCP servers (Streamable HTTP with OAuth login, dynamic client registration, and PKCE).

## Setup

### Let an agent set it up (recommended)

A full setup involves several macOS and networking pieces (`.env`, Full Disk Access, optional code signing, Keychain, a `launchd` LaunchAgent, and a tunnel or Tailscale). You do not need to configure those by hand. Open Claude Code, Codex, or another coding agent on your Mac and paste the [agent setup prompt](docs/agent-setup-prompt.md).

The agent asks which pieces you want, runs the local checks and config steps for you, and pauses for you to enter passwords and click macOS permission dialogs yourself.

### Set it up manually

Start by testing the server locally on your Mac, then layer on remote access, a background service, or both:

1. Install `imsg` and the server's dependencies:

   ```bash
   brew install steipete/tap/imsg
   bun install
   ```

2. Copy the example settings file:

   ```bash
   cp .env.example .env
   ```

3. Open `.env` and set at least these two values for local testing:

   ```dotenv
   MCP_BASE_URL=http://127.0.0.1:3460
   APPROVAL_PASSWORD=choose-a-strong-password
   ```

   Leave `MESSAGES_ALLOWLIST` empty to approve chats per app in your browser, or add entries there to preapprove chats for every app. Run `bun run list-chats` to print every chat's ID, name, and participants.

4. Start the server:

   ```bash
   bun run start
   ```

   On startup, the server logs its active settings, the `imsg` version, how many chats matched `MESSAGES_ALLOWLIST`, and whether it can read your Messages database, Attachments folder, and Contacts, then listens on `127.0.0.1:3460`.

5. Layer on what you need for day-to-day use:
   - **Remote AI apps or phone approvals.** Follow [Connect remotely and approve from your phone](docs/remote-access.md) (covers Tailscale Serve, Cloudflare Tunnel with Cloudflare Access, Cloudflare Tunnel with Tailscale Serve, and MacRemote with Tailscale Serve).
   - **Always-on Mac or Mac mini.** Follow [Run apple-messages-mcp as a macOS background service](docs/background-service.md) to compile a signed binary, store the password in Keychain, grant Full Disk Access, and run it at login as a LaunchAgent (works with either a local or remote setup).

### Connect an AI app

1. In your AI app, add a remote MCP server at `http://127.0.0.1:3460/mcp` (or your HTTPS `/mcp` URL if configured remotely).
2. Start connecting. Your browser opens the server's sign-in page.
3. Confirm the app name, enter `APPROVAL_PASSWORD`, and approve the connection.
4. If another paired app already uses that name, choose **Keep both** for separate permissions, or **Replace connection** to disconnect the older app and choose which chat permissions carry over.
5. Open `http://127.0.0.1:3460/approvals` (or `<MESSAGES_APPROVAL_ORIGIN>/approvals`), sign in with `APPROVAL_PASSWORD`, and choose **Add a chat** to share a conversation.

### Verify the connection

Run this check from your AI app before relying on the server:

1. Call `get_access_info` (for example, ask "What Messages access do you have?"). Confirm the access mode, whether sending is enabled, and the link to `/approvals`.
2. Call `list_chats`. Confirm that only the chats you preapproved or granted appear, with contact names shown if Contacts access is granted.
3. Call `read_chat` on an allowed chat. Confirm that messages appear under `sender_text`.
4. In the default `ask` mode, call `read_chat` on an unlisted chat. Confirm that it returns an approval link instead of message contents.

## How access works

You can combine server-wide defaults in `.env` with per-app permissions in your browser:

- **Preapproved chats (`MESSAGES_ALLOWLIST`).** Chats listed in `.env` are readable by every connected app without asking. Supported formats are phone numbers (`+15551234567`, `+1 (555) 123-4567`; bare 10-digit numbers default to `+1`), email addresses (`friend@example.com`), named groups (`group: Weekend Crew`), unnamed groups by exact members (`group: +15551230001 & friend@example.com`), and exact chat IDs (`iMessage;+;chat123...`). Startup logs warn if an entry matches zero or multiple chats. Remove an entry and restart to withdraw it.
- **Approving chats on request (`MESSAGES_ACCESS=ask`).** When an app asks for an unlisted chat, the server returns a link to `/approvals/r/<id>`. Sign in and choose **Allow** or **Deny**. Access defaults to **24 hours** (set by `MESSAGES_APPROVAL_HOURS`) and clears on restart. Choose **Until revoked** to save access across restarts in `chat-access.sqlite` beside `TOKEN_STORE_PATH`. Every grant applies to one exact chat and one connected app.
- **Adding or revoking chats anytime.** Open `/approvals` and select **Add a chat** to search 1:1 chats and group chats by contact name, group name, phone number, or email, then share temporarily or until revoked. Select **Revoke** on `/approvals` to cut off access immediately. Browser grants apply to reading only and never unlock automatic sending.
- **One-time search across all chats (`MESSAGES_WIDE_SEARCH=ask`).** Optional (requires `MESSAGES_ACCESS=ask`). Lets an app request a single search across your inbox when it does not know which chat to check. You review the exact search text, date range, match limit, and app name at `/approvals/s/<id>`. After approval, the app can collect up to 20 matches (default 10) once within 15 minutes, covering at most the last 30 days (bounded by `MESSAGES_MAX_DAYS`). Reading a full conversation or its attachments still requires chat access.
- **Managing connected apps.** Open `/approvals/connections` to see every paired app and when it was last used. Selecting **Disconnect** immediately revokes that app's tokens, pending requests, and chat permissions.
- **Shared gateways.** If a gateway routes several AI agents through one connection to `apple-messages-mcp`, those agents share that connection's chat permissions. Connect apps directly when each one needs its own chat list.
- **History age limit (`MESSAGES_MAX_DAYS`).** Limits every read, search, and attachment to the last 30 days by default. Set `0` to allow full history.
- **Optional sending (`MESSAGES_SEND`).** Off by default, which hides `send_message` and disables sending inside the server. In `approve` mode, every outgoing message waits as a locked draft at `/approvals/d/<id>` until you tap **Send** or **Discard** (or it expires after `MESSAGES_DRAFT_MINUTES`). In `allowlist` mode, messages to chats in `MESSAGES_ALLOWLIST` send right away while all other chats and new recipients wait for approval. In `direct` mode, every message sends immediately (logs a startup warning). `MESSAGES_SEND_PER_HOUR` caps total sends in every mode, and if `imsg` cannot confirm whether a send went through, the server reports it as uncertain and never retries automatically.

## Tools

Which tools appear in your AI app depends on `MESSAGES_WIDE_SEARCH`, `MESSAGES_ATTACHMENTS`, `MESSAGES_SEND`, and `MESSAGES_FEEDBACK`:

| Tool | Available when | What it does |
| --- | --- | --- |
| `get_access_info` | Always | Returns your `/approvals` link, current reading policy, and whether sending is on. |
| `list_chats` | Always | Lists only the chats this app can read, with contact names, participants, group flag, last activity, unread count, and the `/approvals` link. |
| `read_chat` | Always | Reads messages from one 1:1 or group chat over a time window (default last 7 days, up to 100 messages, max 500; optional `unread_only`). Returns an approval link for unlisted chats in `ask` mode. |
| `search_messages` | Always | Searches message text across readable chats (default 50, max 100 matches). Restricted modes check the latest 1,000 messages per readable chat within `MESSAGES_MAX_DAYS`; `all` mode uses `imsg` search (up to 100 hits). `capped` flags incomplete coverage or extra matches. |
| `search_all_chats_once` | `MESSAGES_WIDE_SEARCH=ask` | Requests or collects one owner-approved search across all chats (default 10, max 20 matches; collected once within 15 minutes). |
| `get_attachment` | `MESSAGES_ATTACHMENTS=contents` | Fetches one attachment from `read_chat`. Returns images (HEIC or large photos converted to JPEG), video poster frames (PNG), voice memos as audio, PDFs as embedded files, and text files in `sender_text`. |
| `search_contacts` | Always | Searches macOS Contacts read-only by name, phone number, or email (returns up to 10 matching contacts by default, max 50). Independent of the chat allowlist. |
| `send_message` | `MESSAGES_SEND` is not `off` | **Write.** Sends text and up to 5 attachments (totaling at most `MESSAGES_ATTACHMENT_MAX_MB`) to an existing 1:1 or group chat, or to a new phone number or email address. Sends immediately or returns a draft approval link per `MESSAGES_SEND`. |
| `report_feedback` | `MESSAGES_FEEDBACK=on` | **Local write.** Saves user-requested notes about tool behavior to `LOG_DIR/feedback.jsonl` on your Mac. |

## Configuration

Set options through environment variables or a `.env` file in the folder where the server runs. Invalid settings stop startup and list every problem. See [`.env.example`](.env.example) for examples.

| Variable | Default | Purpose |
| --- | --- | --- |
| `PORT` | `3460` | Local port the server listens on. |
| `HOST` | `127.0.0.1` | Local network address. Keep `127.0.0.1` when putting a tunnel or reverse proxy in front. |
| `MCP_BASE_URL` | `http://localhost:3460` | Base URL your AI apps connect to (no trailing `/mcp`). Used for OAuth login and default approval links. |
| `MESSAGES_APPROVAL_ORIGIN` | `MCP_BASE_URL` | Separate HTTPS address for `/approvals` links (for example, a Tailscale address for phone approvals while `/mcp` stays local). Origin only, with no path or query. |
| `APPROVAL_PASSWORD` | (unset) | Password for pairing new apps and signing into `/approvals`. Required unless `MESSAGES_APPROVAL_KEYCHAIN_ITEM` is set; cannot be combined with it. |
| `MESSAGES_APPROVAL_KEYCHAIN_ITEM` | (unset) | macOS login Keychain generic-password service name (account must match your macOS username) so you do not have to store the password in `.env`. |
| `TOKEN_STORE_PATH` | `./tokens.json` | File storing paired app logins. `chat-access.sqlite` is created in the same folder for saved chat permissions. |
| `HEALTH_TOKEN` | (unset) | Secret token for `GET /health` and `GET /health/messages`. When unset, both return `404`. |
| `LOG_DIR` | `./logs` | Folder for local `tool-calls.jsonl` (and `feedback.jsonl` when `MESSAGES_FEEDBACK=on`). |
| `IMSG_BIN` | `imsg` | Path or command name for the `imsg` binary. |
| `MESSAGES_ACCESS` | `ask` | `allowlist`, `ask`, or `all` (`all` logs a startup warning). |
| `MESSAGES_WIDE_SEARCH` | `off` | `off` or `ask` (requires `MESSAGES_ACCESS=ask`). |
| `MESSAGES_FEEDBACK` | `off` | `off` or `on` (`on` enables the local `report_feedback` tool). |
| `MESSAGES_ALLOWLIST` | (empty) | Comma- or newline-separated chats readable by every connected app. |
| `MESSAGES_APPROVAL_HOURS` | `24` | Hours before temporary chat permissions and pending read requests expire. |
| `MESSAGES_MAX_DAYS` | `30` | Maximum message age in days for reads, searches, and attachments. `0` disables the limit. |
| `MESSAGES_ATTACHMENTS` | `contents` | `none`, `metadata`, or `contents`. |
| `MESSAGES_ATTACHMENT_MAX_MB` | `25` | Size limit in MB for one returned attachment, and for all attachments combined on one outgoing message. |
| `MESSAGES_SEND` | `off` | `off`, `approve`, `allowlist`, or `direct` (`direct` logs a startup warning). |
| `MESSAGES_SEND_PER_HOUR` | `20` | Maximum outgoing messages per rolling hour when sending is enabled. |
| `MESSAGES_DRAFT_MINUTES` | `60` | Minutes before an unapproved outgoing draft expires. |

## Privacy and security

This server runs locally on your Mac under your control. You control the binary, approval password, network exposure, and which AI apps can connect.

- **What it reads.** The server reads `~/Library/Messages/chat.db` through `imsg`, reads attachment files strictly inside `~/Library/Messages/Attachments` (blocking symlinks or paths outside that folder), and reads `~/Library/Application Support/AddressBook` in read-only mode.
- **No phone-home or telemetry.** The server makes no outbound network requests of its own. If you enable `MESSAGES_FEEDBACK=on`, `report_feedback` appends only to `LOG_DIR/feedback.jsonl` on your Mac. Connected AI apps receive the messages, attachments, and contacts they request and handle that data under their own privacy policies.
- **Contacts access is separate from the chat allowlist.** `search_contacts` can look up names, phone numbers, and emails in macOS Contacts even for people whose chats are not shared. If you do not want connected apps to search Contacts, do not grant Full Disk Access to `~/Library/Application Support/AddressBook`.
- **What is saved on disk.** Paired app logins are saved in `TOKEN_STORE_PATH`, long-term chat permissions and app names are saved in `chat-access.sqlite` beside it, and call metadata appends to `LOG_DIR/tool-calls.jsonl`. Message text, search queries, and attachment files are never saved to SQLite or the tool-call log. Temporary grants, pending requests, drafts, and browser sign-ins live in memory and clear when the server restarts.
- **Approval page protection.** The `/approvals` page is password-protected, rate-limits failed logins (10 attempts per 15 minutes per IP), runs with no client-side JavaScript, and blocks cross-site form submissions.
- **Revoking access.** Changing `APPROVAL_PASSWORD` stops new logins and approvals, but it does not disconnect already-paired apps. To cut off an existing app right away, open `/approvals/connections` and select **Disconnect**.
- **Large upload limit when sending is on.** When `MESSAGES_SEND` is enabled, `POST /mcp` accepts request bodies large enough for attachments up to `MESSAGES_ATTACHMENT_MAX_MB` (about 34 MB at the 25 MB default), parsed before checking the app's token. Keep `/mcp` behind an authenticated tunnel or private network if you turn sending on.

## Troubleshooting

- **`The Messages database is unavailable ... Grant Full Disk Access to the server binary for ~/Library/Messages.`** Grant Full Disk Access to the compiled binary (or to your terminal app if running `bun run start` directly). If you rebuilt an unsigned binary, remove and re-add it, or sign builds with a consistent identity. See [Run apple-messages-mcp as a macOS background service](docs/background-service.md).
- **`Can't read ... Grant Full Disk Access to the server binary for ~/Library/Application Support/AddressBook.`** Grant access to `~/Library/Application Support/AddressBook`. Contacts reopen on every search, so the permission takes effect without restarting.
- **`Cross-site request refused.`** The address in your browser does not match `MESSAGES_APPROVAL_ORIGIN` (or `MCP_BASE_URL`). Open the exact address you configured, such as `http://127.0.0.1:3460/approvals` rather than `http://localhost:3460/approvals` when `MCP_BASE_URL=http://127.0.0.1:3460`.
- **`APPROVAL_PASSWORD or MESSAGES_APPROVAL_KEYCHAIN_ITEM is required`.** Set exactly one of those variables in `.env` or your service environment. Setting both stops startup.
- **`Cannot read MESSAGES_APPROVAL_KEYCHAIN_ITEM from the login Keychain`.** Check that a generic-password item with that exact service name exists in your login Keychain, its account matches `whoami`, and the binary can read it without a prompt.
- **Missing `send_message`, `get_attachment`, `search_all_chats_once`, or `report_feedback`.** These tools only appear when enabled in `.env` (`MESSAGES_SEND`, `MESSAGES_ATTACHMENTS=contents`, `MESSAGES_WIDE_SEARCH=ask` with `MESSAGES_ACCESS=ask`, and `MESSAGES_FEEDBACK=on`). After changing settings and restarting, refresh the tool list in your AI app.
- **`Your session ended. Sign in again.`** Browser approval sessions last 12 hours and reset whenever the server restarts. Sign in again at `/approvals`.

## Develop locally

Run the tests and typecheck without touching your real Messages database or Contacts:

```bash
bun test
bun run typecheck
bun run lint
bun run audit
```

Tests run against a fake `imsg` helper (`test/fake-imsg.ts`) and temporary fixture databases. To run the full server against sample data, point `IMSG_BIN` at a wrapper script that runs `bun test/fake-imsg-bin.ts`.

Never run development tests with `MESSAGES_SEND` enabled against the real `imsg` binary.

Architecture and implementation notes live in [`docs/design/architecture.md`](docs/design/architecture.md) and [`docs/design/kit-workarounds.md`](docs/design/kit-workarounds.md).

## Credits

- [`imsg`](https://github.com/steipete/imsg) by Peter Steinberger provides the `imsg rpc` bridge to Apple Messages.
- [`mcp-server-kit`](https://github.com/nweii/mcp-server-kit) provides the MCP HTTP transport, OAuth sign-in, audit logging, and server lifecycle helpers.

## License

MIT. See [LICENSE](LICENSE).