Skip to content

Require a pairing code to connect to iMCP Helper - #294

Merged
mattt merged 47 commits into
mainfrom
mattt/helper-launch-secret
Oct 23, 2026
Merged

mattt merged 47 commits into
mainfrom
mattt/helper-launch-secret

Conversation

@mattt

@mattt mattt commented Oct 23, 2026 •

Copy link
Copy Markdown
Owner

iMCP Helper served the Home tools to any process that connected to its loopback port, as reported in #288. Another process on the Mac could connect and skip iMCP's approval prompt and tool settings.

This PR pairs iMCP with the helper once, through a one-time code. The helper shows an 8-character code; you copy it, click Open iMCP… to open iMCP's pairing window, and paste it there. Both apps derive a key from the code with PBKDF2 and connect over TLS 1.2 with an ECDHE-PSK cipher suite. On that connection, iMCP sends a new random 256-bit key, which each app keeps in its own keychain. The code then stops working, and every later connection uses the stored key, so a process without the key can't connect, even if it launches the helper itself. The pairing connection can't list or call tools, and the helper's MCP server also runs in strict mode. After 5 wrong codes, the helper shows a new code. Pair Again… in the helper ends the pairing and shows a new code.

iMCP finds the helper with Bonjour and refuses a helper built before this change, with a message to rebuild it. iMCP no longer passes the port or its process ID when it launches the helper, because the sandbox ignores launch arguments; that's why #290 saw no --port. The helper now uses the Mac interface idiom, with a Dock icon and the standard app menu, and iMCP has an imcp://pair URL that only opens its pairing window.

Fixes #288
Fixes #290

mattt added 4 commits October 23, 2026 04:41
iMCP passes a new random secret in the helper's launch environment
and presents it in the initialize request.
The helper runs its MCP server in strict mode,
ends connections that present a wrong or missing secret,
and accepts no connections when it has no secret.
Remove the Bonjour advertisement and discovery fallback,
which can't supply a secret.

Fixes #288
iMCP picks the port before it launches the helper.
If another process takes the port first,
iMCP sends that process its initialize request with the secret.
Retrying the listener with the same secret would let that process connect,
so the helper now logs the error and exits,
and iMCP launches a new helper with a new port and secret.
Other processes can read the launch arguments,
so a process could bind the port before the helper
and receive iMCP's initialize request with the secret.
@mattt
mattt marked this pull request as draft October 23, 2026 12:06
mattt added 16 commits October 23, 2026 05:24
The sandbox ignores the launch arguments and environment,
so the helper never received the port or the secret (#290).
The helper now makes a pairing code, keeps it in its keychain,
and shows it in its window.
iMCP asks for the code and keeps it in its own keychain,
and both apps derive a TLS pre-shared key from it.
iMCP finds the helper through its local-only Bonjour advertisement.
The helper's Info.plist and TXT record declare a protocol version,
which iMCP checks before it launches or connects to a helper.
A restart during a start replaced the listener and its startup continuation,
so the first start never returned.
Concurrent starts now share one task, and a restart waits for it.
Discovery picked one advertisement, so with two helpers running
it could report another helper's version or code.
iMCP now resolves every local advertisement,
tries the helper it launched or found running first,
and reports a version or pairing failure only after every helper has answered.
The helper's TXT record gives its process ID for this ordering;
the TLS handshake still decides which helper iMCP trusts.
A restart could reuse a finished start task and return the old,
cancelled port, and repeated clicks could overlap.
The helper now runs each listener and advertisement change after the earlier one,
disables New Code while a replacement runs,
and gives each restart a generation, so older work can't replace newer work.
Note that tool calls already accepted can still finish after a restart.
Copying selected text bypassed the Copy button's local-only,
two-minute pasteboard item.
When the helper that iMCP launched never answered,
another helper's rejection was reported as a pairing failure.
Replace the generation scheme with rules in the helper's window:
the code and New Code appear only after the listener is ready and advertised,
and New Code and Retry are disabled while a replacement runs.
The server's start no longer shares a task that a later start could reuse.
Resolution and connection attempts ran one after another
with their own timeouts, so stalled helpers could delay a failure for minutes.
One 15-second deadline now limits discovery, resolution,
which runs for all services at once, and each connection attempt.
iMCP also keeps the chosen helper's latest results across discovery passes,
so another helper can't decide the error while the chosen one briefly
stops advertising.
A helper from before pairing doesn't advertise its process ID,
so another helper's rejection could be reported instead.
iMCP now checks a running helper's Info.plist before it uses it,
and if the chosen helper never advertises,
an outdated advertisement comes before a rejection.
Unsigned builds use the login keychain,
where the same service and account named one item for both apps.
The shared 15-second deadline could end the first homes_list read
while a person answered the HomeKit prompt.
After the PSK handshake and initialize,
the read now has 20 seconds of its own.
Each pass started with no results from other helpers,
so a pass at the deadline could report that no helper appeared
after another helper had rejected the code.
iMCP now keeps each helper's latest result by service name,
and decides from the stored results when the deadline passes.
A failed read was reported as an outdated helper,
which asked the user to rebuild it.
@mattt mattt changed the title Limit iMCP Helper to the iMCP instance that launches it Require a pairing code to connect to iMCP Helper Oct 23, 2026
mattt added 8 commits October 23, 2026 06:38
As an agent app, the helper had no Dock icon or menu bar,
so its window stayed hidden behind other windows,
and it could quit only through a button in the window.
It now shows in the Dock and quits with Quit iMCP Helper (Command-Q).
A short code can't be the permanent TLS pre-shared key:
a fake helper that iMCP tries receives a TLS Finished message,
which allows an offline search of the code.
The helper now shows an eight-character code until it is paired.
iMCP derives a pairing key from the code with PBKDF2,
and on that connection sends a new random 256-bit key,
which both apps keep in their keychains and use from then on.
The code works once, and after five failed handshakes the helper shows a new one.
Pair Again in the helper deletes the key and shows a new code.

The helper's window is now compact, with a status symbol,
the code in large text, and Copy Code and Pair Again buttons.
iMCP brings the helper forward when it has no key,
and its Pair alert can show the helper's window.
Try Again could restart the listener but left its error in the window,
because the HomeKit status and the listener's error shared one field.
TLS_PSK_WITH_AES_128_GCM_SHA256 has no forward secrecy,
so a recorded pairing connection could reveal the key once the code is found.
Both apps now append TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256.
Network.framework still offers and accepts its default PSK suites,
so both apps also check the negotiated suite before they exchange anything.
A connection registered only after its handlers could miss a restart
and serve the old key.
A pairing handler that ran after its connection ended could claim the code
and stop listening without reporting the pairing.
The helper now registers a connection before it suspends,
rejects pairing from an ended session,
stores the key before it acknowledges the pairing,
and reports a stored pairing exactly once.
iMCP saves the key only after the helper acknowledges it,
and reports an incomplete pairing if the helper doesn't switch to the key.
If Pair Again can't delete the key, the helper ends every session
and shows an error instead of a new code.
Two overlapping pairings could leave iMCP with a key that the helper didn't accept.
HomeService now runs pairings one after another,
and Settings disables Pair… while one runs.
mattt added 18 commits October 23, 2026 10:36
When failed pairings replaced the code, a pairing that claimed the old code
could store its key just before, and a relaunch would serve that key.
Automatic replacement now does what Pair Again does:
it clears the shown code, so the old code can't store a key,
then deletes any key that one stored, before it shows a new code.
If the helper stored the key but its acknowledgment was lost,
or iMCP exited before it saved the key, the apps had different keys.
iMCP now saves the key as pending before it sends it,
promotes it after the acknowledgment or after a connection with it,
tries a pending key when the helper rejects the saved one,
and deletes the pending key only when the helper refuses the pairing.
A failed replacement retried its event, whose code check failed
once the code was cleared, so Try Again did nothing.
It now retries as an unconditional replacement.
Any early error deleted the pending key,
but swift-sdk's Client.send also throws local send errors.
The helper now refuses a pairing with its own JSON-RPC error code,
and iMCP deletes the pending key only for that code.
A connection to an older helper with the saved key deleted the pending key,
which the chosen helper may have stored after a lost acknowledgment.
iMCP now tries the saved and pending keys with each helper, the chosen one first,
and deletes the pending key only when the chosen helper accepts the saved key.
The modal Pair alert stayed above every app's windows,
so the helper's window, with the code, stayed hidden behind it.
iMCP now asks for the code in a small window at the normal level,
to the left of the screen's center, where the helper's window opens.
Show iMCP Helper brings the helper in front of it,
and the window stays open and shows why a code didn't work.
The window is for pairing, so it only needs to say that HomeKit is ready.
Cancel, Escape, or closing the window during an attempt reported false
while the helper could still accept the key,
and a second caller got false while the first window stayed open.
While an attempt runs, Cancel and the close button are now disabled,
and every caller awaits the window's one result.
iMCP now handles the imcp URL scheme.
For imcp://pair it opens the pairing window, as Pair… in Settings does,
and ignores every other part of the URL, so a link can't pass data.
After Pair Again, nothing told the user where to enter the code.
While it shows a code, the helper's window now lists three steps,
with Copy Code and an Open iMCP… button that opens iMCP's pairing window.
The helper opens imcp://pair only with the app that has iMCP's bundle ID,
so another app that claims the scheme can't show a fake pairing window.
Without iMCP, the step says where Pair… is in iMCP's settings.
Each open of the URL waited on the pairing window and reconnected afterward,
so several opens before pairing finished caused several reconnects.
Later opens now bring the window forward and await the first open's pairing.
Open iMCP… opened the copy that LaunchServices found for the bundle ID,
which can be an older iMCP that doesn't handle imcp://pair.
The helper now prefers the running iMCP, uses a copy only if
LaunchServices lists it as able to open the URL,
and yields activation to iMCP before it opens the URL.
With cooperative activation, the helper launched for pairing stayed behind,
because iMCP didn't yield to it.
iMCP now yields activation to the helper before it launches or activates it.
The helper scaled its iPad interface on the Mac,
with iPad controls and smaller text.
It now uses the Mac idiom (device family 6) on Mac Catalyst.
While the helper shows a code, the window now has one instruction,
the code in a large monospaced text style, the steps as a plain list,
the note on wrong codes, and one row of buttons:
Get New Code, then Open iMCP… and Copy Code, the default.
When paired, it shows a status row and Pair Again…,
which asks for confirmation before it deletes the pairing.
VoiceOver reads the code as two groups, and the window doesn't go full screen.
Messages now talk about pairing and codes instead of keys,
say "wrong codes" in both apps, and call the button Open iMCP Helper….
The code field in iMCP's pairing window is narrower, with an example code
as its placeholder, and Pair is disabled until the field holds a code.
LaunchServices lists only one copy of an app for each bundle ID
as able to open a URL, so the running iMCP could fail the check
when another copy, such as a development build, was listed instead.
The helper now trusts the running iMCP unless its Info.plist
shows that it lacks the imcp scheme,
checks a copy that isn't running by its Info.plist,
checks again whenever the helper becomes active,
and logs each decision.
@mattt
mattt force-pushed the mattt/helper-launch-secret branch from be4a1d0 to 1468496 Compare October 23, 2026 17:36
…main actor

The log messages for finding iMCP showed full app paths in collected logs,
which can include user or folder names; they are now private.
Reading an app's Info.plist ran on the main actor,
so a stalled volume could freeze the window; it now runs off the main actor.
@mattt
mattt marked this pull request as ready for review October 23, 2026 18:17
@mattt
mattt merged commit 244ee4a into main Oct 23, 2026
2 checks passed
@mattt
mattt deleted the mattt/helper-launch-secret branch October 23, 2026 18:17
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.

Home fails when iMCP launches the helper: --port argument isn't delivered iMCP Helper accepts unauthenticated connections on its loopback port

1 participant