Skip to content

iMCP for iPhone and iPad: keep the link alive, activity history, approvals, outbox fix, and routes the Mac chooses - #381

Closed
oliverames wants to merge 27 commits into
mattt:mainfrom
oliverames:pr/iphone-ipad
Closed

oliverames wants to merge 27 commits into
mattt:mainfrom
oliverames:pr/iphone-ipad

Conversation

@oliverames

@oliverames oliverames commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Stack. Stacked on #281. Review only the commits after 25ffc7e, which is #281's head merged with current main. It replaces #365, #366, #367, #368, #369, #370 and #372 (listed at the end). Nothing here needs #376.

What it does and why

iMCP for iPhone and iPad (#281) can serve Health and Alarms, but the link is fragile, the Mac gives no sign of what agents asked for, and the phone can only reach the Mac on the local network. This adds, on top of #281:

  • A phone link that stays up: the Mac keeps a live link when the same device connects twice, the phone shares one in-flight dial, and pull to refresh returns within 8 seconds.
  • An activity history of tool calls, synced to the phone.
  • Approving client connections on the phone.
  • A fix for the iOS outbox, which was killed with 0xdead10cc when it was suspended holding a SQLite lock in the App Group container.
  • Several ways to reach the Mac: local network, Tailscale VPN, and a Cloudflare Tunnel you set up yourself, with a Connection Mode setting on the phone and route settings on the Mac.
  • The Mac deciding the phone's routes: it sends a numbered route list and the phone follows it.

What changed

Five commits, in this order:

  1. Keep the phone link alive, sync tool-call history, and fix the outbox crash.
    • Mac: attach keeps a live link when the same device connects again (a ping check decides), and cancellation is logged at debug, not error.
    • Phone: one shared in-flight dial, refresh bounded to 8 seconds, "Last connected".
    • Activity: the Mac records client, service, tool, read or write (from readOnlyHint), outcome and duration for the last 500 calls and pushes unsent events with a new activity_sync call. The phone acknowledges the highest sequence it stored. The store opens from disk when a phone connects, and activity syncs before the batch drain. The phone has an Activity screen with filters, search and Clear.
    • Outbox: it now lives in the app's own Application Support directory. The widget extension has empty entitlements and reads nothing from the group, and only iOS/Backend.swift opens the outbox. A migration checkpoints and moves an old outbox with its -wal and -shm files, so queued batches and HealthKit anchors survive. Syncs run under a background task assertion and an expired assertion cancels the sync so the transaction rolls back.
  2. Let the phone reach the Mac over local, Tailscale, or Cloudflare routes. MobileTransportMode, the pairing code and welcome carry route addresses, an encrypted WebSocket channel (X25519, HKDF, ChaChaPoly with counter nonces), candidate dialing with a 6 second budget per route, the Connection Mode screen and "Via" row on the phone, and Connection Route settings on the Mac. Tailscale VPN dials the Mac's own Tailscale interface address (or a typed host) on the port the TLS listener bound. It does not use the embedded tailnet node from Serve MCP over HTTP to your other devices on Tailscale #308. Cloudflare Tunnel uses a loopback WebSocket listener that is off unless you turn it on and enter the tunnel's public address.
  3. Let the Mac decide the phone's routes and send them over the connection. A versioned MobileConfiguration (revision that only goes up, ordered routes, features) is sent in the welcome and with a config_apply call when the Mac's settings change. The phone applies only newer revisions and follows the Mac's order. The Mac shows the revision each phone has applied and has drag to reorder and a toggle per route. Older phones that ignore it still use the addresses from pairing.
  4. Approve client connections from the paired phone. The Mac offers each connection request to the phone as well as showing its own dialog. The first answer wins. Approving on the phone allows one connection and never trusts the client. The Mac withdraws the request when it answers, expires an unanswered one as a denial, and ignores answers from devices that aren't paired. The phone lists waiting requests and answers from a notification after device authentication. A toggle, "Approve on iPhone", is on by default.
  5. README. Describes the above.

I also ported the fix for the timing assertion in MobileRetryTests (wait for the replaced link to close before checking it).

What needs #376

Nothing in this PR. The Tailscale Funnel route exists in the phone and in the configuration, but nothing on the Mac supplies a Funnel address here, so the phone lists it as unavailable and Automatic skips it. The follow-up PR (stacked on this one and #376) lets the Mac advertise its Funnel and Cloudflare Tunnel addresses from remote access and accept the phone's WebSocket on those hosts.

Testing

  • imcp-serverTests, full suite, on this branch: 306 passed, 1 skipped, 0 failed. New tests are in MobileRetryTests, MobileActivityTests, MobileOutboxTests, MobileTransportTests, MobileConfigurationTests and RemoteApprovalTests. The outbox tests fail without the fix: with the move stubbed out the container and migration tests fail, and with Task.checkCancellation() removed from enqueue the cancellation test fails.
  • The iMCP scheme builds for macOS and the iMCP iOS scheme builds for generic iOS, both Debug with signing off, as in CI. swift format lint --strict --recursive . is clean.
  • On a device: I ran the same code from my fork's integration branch on my iPhone through TestFlight builds 30, 31 and 32 (uploaded and VALID). They are not builds of this exact branch, and they carry fork-only pieces. I confirmed on the phone: the Activity screen fills with recent tool calls, including right after the Mac app restarts; the phone connected over a Cloudflare Tunnel route; Connection Mode showed which Mac configured it with a revision and the routes in the Mac's order; a live route-order change on the Mac changed the revision on the phone; and the settings screens worked.

Not verified

  • Approving on the phone has not been run on a device. It is covered by tests against a fake phone link, including a concurrent Mac-versus-phone race, but notification delivery and the Approve and Deny buttons are untested.
  • I never reproduced the 0xdead10cc crash. The evidence is one TestFlight crash report on build 26 and no reports since the same change shipped in build 27.
  • This exact branch has not been on a device, and Tailscale VPN has not been tried with real Tailscale on both devices from this branch.
  • The phone's connection code and screens have no test target (as in Add iMCP for iPhone and iPad with Health and Alarms #281) and are compile-checked only. The Mac's recording hook in the tool-call handler has no test seam.
  • MobileFeatures.approvals is not set by the Mac yet and the phone does not read it.
  • The revision number lives in the Mac's UserDefaults. If those are reset, the numbers restart low and a paired phone ignores them until it re-pairs.

Replaces

#365, #366, #367, #368, #369, #370 and #372. #371 stays open and is replaced by the follow-up that builds on #376.

Screenshots

The Mac side of the phone connection: the paired device, the routes the phone tries in order, the connection mode, approvals and activity history.

iPhone & iPad settings on the Mac: devices, routes, connection mode, approve on iPhone and activity history

This is from my integration build, whose Settings window is a separate redesign (#388), and its Cloudflare Tunnel route is the one #386 adds. The Cloudflare Tunnel and Tailscale public address rows are not part of this PR.

mattt and others added 27 commits November 6, 2026 10:20
Define the Health, Finance, and Alarms tools once in Shared/Companion,
store what the iPhone sends in an encrypted SQLite cache,
and answer read-only calls from that cache with synced_at and stale.
Generalize HomeInput into ToolInput so both services validate arguments.
The iPhone app dials the Mac over TLS with pre-shared keys,
pairs with a one-time code from a QR code in Settings > Companion,
and serves the companion tools as an MCP server on that connection.
The Mac calls tools live when the phone is connected,
retries a lost read or an alarm change that carries its ID,
and pulls queued changes from the phone's outbox into its cache.
Scripts/install-companion.sh builds and installs the app with your team.
Answer the Health tools live from HealthKit,
let the user allow each group of types from the app,
and queue anchored changes for the Mac: daily statistics for quantity types
and full records for sleep, workouts, and health records.
When HealthKit is locked, the Mac answers reads from its cache.
Create one-time, weekly, and countdown alarms with the ID the Mac sends,
so a retried call returns the existing alarm instead of a second one.
A widget extension shows a running timer as a Live Activity with a stop button,
the app lists the alarms that AI apps created and cancels them,
and the phone sends its alarm list to the Mac whenever AlarmKit reports a change.
Answer the Finance tools live and queue account, balance, and transaction
changes for the Mac with FinanceKit history tokens.
A background delivery extension queues changes while the app isn't running.
The code stays inactive until a build carries the FinanceKit entitlement,
which Scripts/install-companion.sh --finance requests;
without it, the Finance tools say why they're unavailable.
Add Alarms, Finance, and Health to the README and its hero image,
describe how to install, pair, and use the companion,
add Scripts/check-companion.py for read-only checks through imcp-server,
and build the companion unsigned in CI.
When the iPhone drops a connection, disconnect the Mac's MCP client,
so pending calls fail over to the cache and syncing resumes on the next connection.
Keep lines that arrive before MCP reads them, end a closed channel's stream
with an error, and enforce read and open time limits.
Reject outbox writes from a sync that a cache reset overtook, run each
service's sync one at a time, and give the FinanceKit reason in cached answers.
The Mac used to send the device key inside the pairing session,
so anyone with the QR secret who recorded that session could recover it.
Now each side sends a one-time X25519 public key, both derive the device key
from the shared secret salted with the pairing key, and each side's proof
covers the public keys so neither can be swapped.
The iOS app is now iMCP on the device and builds for iPhone and iPad.
Targets, folders, and shared code use iOS and Mobile names,
the Mac's Settings pane is iPhone & iPad,
and the Bonjour type, pairing link, and status tool drop the companion name.
The name no longer collides with the separate Companion debugging app
that the README describes.
The macOS 26 runner's Xcode 26.0 has no iOS platform,
so the iOS app's build couldn't find a destination.
Download the platform when it's missing and allow time for the download.
AlarmKit shows its own stop control on the alarm alert,
so the app doesn't need an App Intent for it.
The timer Live Activity now shows the countdown without a stop button.
… crash

The Mac keeps a live link when the same device connects twice, and the phone
shares one in-flight dial and bounds pull to refresh to 8 seconds.
The Mac records each tool call in an activity history that syncs to the
phone, which shows it under Activity. Syncs run under a background task
assertion and the outbox moves out of the App Group container, which ended
the app with 0xdead10cc when it suspended holding a SQLite lock.
Adds the transport modes, an encrypted WebSocket channel, candidate dialing
with per-route and total time budgets, the Connection Mode screen on the
phone, and route settings in the Mac's iPhone & iPad pane.
The Mac numbers its route list with a revision, sends it in the welcome and
with config_apply when its settings change, and shows the revision each
phone has applied. The phone follows the Mac's route order.
The Mac offers each connection request to the paired phone as well as showing
its own dialog, and the first answer wins. Approving on the phone allows one
connection and never trusts the client. The Mac withdraws the request when it
answers, expires an unanswered one as a denial, and ignores answers from
devices that aren't paired. The phone lists waiting requests and can answer
from a notification after device authentication.
@oliverames

Copy link
Copy Markdown
Contributor Author

Split the outbox move and link liveness check into #391 against mattt/ios-app per #364, so I'm closing this one.

@oliverames oliverames closed this Nov 20, 2026
@oliverames
oliverames deleted the pr/iphone-ipad branch November 20, 2026 21:34
@oliverames oliverames mentioned this pull request Nov 21, 2026
15 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants