Skip to content

Register tool handlers only after a connection is approved - #325

Merged
mattt merged 1 commit into
mainfrom
mattt/fix-approval-bypass
Nov 1, 2026
Merged

mattt merged 1 commit into
mainfrom
mattt/fix-approval-bypass

Conversation

@mattt

@mattt mattt commented Nov 1, 2026

Copy link
Copy Markdown
Owner

This PR changes when iMCP registers its MCP handlers for a connection. Server.start(transport:) returns before the client sends initialize, and iMCP registered its handlers right after that, so a client could list and call tools without initializing, or while its initialize waited on the approval dialog.

iMCP now registers the handlers from the initialize hook after the user approves the connection, and each handler checks that approval again through a new ConnectionApprovalGate. Until then, tools/list and tools/call get a "Method not found" error. A denied connection gets no handlers, and a repeated initialize doesn't register them again. One consequence: when imcp-server reconnects in the middle of a session, the new connection gets no tools until the client initializes again.

1.8.1 ships the same change from the release/1.8.1 branch, on top of 1.8.0. This PR brings it to main.

Server.start returns before the client sends initialize, and the SDK
runs each request in its own task. Registering handlers after start
returned let a client list and call tools without initializing, or
while its initialize waited on the approval dialog.

Register the handlers from the initialize hook once the user approves,
and make each handler check the connection's approval state. Until
then, tools/list and tools/call get a "method not found" error. A
denied connection or a repeated initialize gets no handlers.
@mattt
mattt merged commit 9c241d6 into main Nov 1, 2026
3 checks passed
@mattt
mattt deleted the mattt/fix-approval-bypass branch November 1, 2026 14:55
mattt added a commit that referenced this pull request Nov 3, 2026
…ntial (#328)

* Connect imcp-server to the app over a verified Unix domain socket

imcp-server found the app with Bonjour, preferred any local _mcp._tcp
service whose name contained "iMCP", fell back to any local service,
and proxied the session over plain loopback TCP. Any local process
could advertise the service and receive the session, or connect to
the app's port.

The app now listens on a Unix domain socket in its sandbox container,
at Library/Caches/imcp.sock, and imcp-server derives the path from the
app's bundle identifier. Before either side sends anything, it reads
the peer's audit token with LOCAL_PEERTOKEN and checks the peer's code
signature with SecCodeCopyGuestWithAttributes. imcp-server requires
the app to be signed by team TTY35UM57S as co.dododo.iMCP, and the app
requires imcp-server to be signed by the same team as imcp-server.
The app replaces a stale socket or one held by a process that fails
the check, and doesn't take over from another signed copy of iMCP.

Unsigned Debug builds also accept a peer with the expected identifier,
and an unsandboxed Debug app listens in ~/Library/Caches. Release
builds don't contain this code.

The app serves MCP over the socket with SocketTransport, and
imcp-server sends a one-line hello before relaying stdio.
imcp-server no longer needs Local Network access. The app no longer
advertises or declares _mcp._tcp, and BonjourDiscovery keeps only
isLocal, which the app uses to find iMCP Helper.

* Trust clients by an issued credential instead of their reported name

iMCP identified a client only by the clientInfo.name it sent in the
initialize request, and auto-approved any connection whose name was
on the trusted list in UserDefaults. Any process that could connect
could claim a trusted name.

iMCP now issues a credential, a random 256-bit token, and stores only
its SHA-256 hash in the Keychain, with the client's name and whether
it's trusted. Release builds use the data protection keychain; unsigned
Debug builds, which it refuses, fall back to the login keychain.
imcp-server reads the credential from IMCP_CLIENT_CREDENTIAL in the
client's MCP settings and sends it in its hello, after it verifies
the app. Only a trusted credential skips the approval dialog.

"Always trust this client" trusts the connection's credential if iMCP
issued it. Otherwise iMCP issues a trusted credential and shows it once
for the person to add to the client's settings. Setting up Claude
Desktop writes an untrusted credential into its config, so the first
approval applies to it. Approval dialogs are coalesced by credential,
not by name, and show what iMCP knows about the credential.

Names that earlier versions trusted no longer approve a connection.
Settings lists them under "Approve Again", and the dialog says that
the client needs a credential.

* Bound, expire, and cool down connection approval requests

Each client name could open its own approval dialog without limit,
the setup timeout was cancelled while a dialog was open, so a
connection could wait on an unanswered dialog forever, and the app
accepted new connections while the server was turned off.

ApprovalQueue now holds the requests that wait for a decision. It shows
up to three dialogs and queues the rest, refuses connections when eight
are waiting, and denies a request that isn't answered within two
minutes. After a denial or an expired request, it denies requests with
the same credential, or the same claimed name if there's no
credential, for 30 seconds. Closing a waiting connection removes it
from its dialog, and closes the dialog if no other connection waits.

The network manager refuses a new connection while the server is
disabled or while ten connections are unapproved, before it reads
anything from the socket. A connection has 10 seconds to send its
hello and initialize request. While it waits for a decision, it has
the dialog's expiry plus a margin. The setup timer used to be cancelled
as soon as the MCP server started, so it closed no stalled connections.

* Close the remaining gaps in connection approval

The approval gate from #325 keeps requests from reaching the handlers
before approval and asks for approval only once for each connection.
These changes cover what happens before and around that step:

- The server reads the stored "Enable MCP Server" setting before it
  listens. The menu sent the setting only when its content appeared,
  so a disabled server accepted connections after launch.
- The dialog shows the reported name on one line, without control or
  formatting characters, and at most 64 characters long, so a name
  can't imitate the dialog's other text.
- The hello has 5 seconds in total instead of 5 seconds for each byte.
- "Copy Value" marks the credential as concealed and keeps it on this
  Mac.

* Send tools/list_changed only to approved connections

The server sent the notification to every open connection when a
service, a tool, or the enabled state changed, including connections
that were waiting for approval and had no handlers. A connection that
waits for approval now gets no notification. The gate decides, so the
check matches the one that the handlers make.

* Exit instead of reconnecting once a session has started

After an unexpected error, imcp-server connected to the app again and
went on relaying stdin. The app approves a connection when the client
initializes, and a new connection has no approval, so the client's
later requests failed on the new connection without a new initialize.

imcp-server now exits after an error once it has read anything from
the client, and the client must start it again for a new session,
which asks for approval. It reconnects after an error only if it hasn't
read any input yet, which is uncommon: a closed stdin, a closed
connection, and an oversized input frame end the session either way.
The retries while the app isn't running happen before a session starts
and don't change, because the client's input waits in stdin until the
first connection.

* Check that the app's process holds the connection to imcp-server

For the connecting side, LOCAL_PEERTOKEN doesn't come from the
listener itself. The kernel takes the process ID that the listening
socket last recorded and returns the audit token of whatever process
has that ID now. A process could listen on the app's path, leave the
socket to a child, and exit, and a signed copy of iMCP that later
reuses the ID would pass imcp-server's code-signing check for it.

After the check, imcp-server now finds the other end of its connection
among the sockets that the process in the token has open, comparing
the control blocks that proc_pidfdinfo reports for both ends. It waits
up to 3 seconds for the app to accept the connection, then reads the
token again and requires the same process ID and ID version. If the
app closes the connection instead, for example because its server is
off, imcp-server reports that it couldn't connect and tries again.

When the app looks for a stale socket at its path, it now treats a
listener in its own process as stale.

* Listen again when the socket file is removed or replaced

Clients connect to the socket by path. If the file was unlinked or
replaced while the app ran, the app went on listening on a socket that
no client could reach, and another process's socket at the path got
the connections.

On each pass of the listener retry task, every 10 seconds, the app now
compares the device and inode at the path with those of the file it
bound. If they differ, it stops listening and binds again, which
replaces another process's socket unless a copy of iMCP listens on it.
A file that isn't a socket still isn't removed; the app now reports it
once as a fault that says clients can't connect, and it reports each
other failure to listen once instead of on every pass.

* Reject a hello with an unknown version

The app decoded the hello's version but ignored it, so it would have
read a hello from a newer or older exchange as if it were version 1.
It now refuses a connection whose hello isn't version 1.

* Cool down only clients with a credential

After a denial or an expired request, the approval queue refused
requests with the same claimed name for 30 seconds when the client had
no credential. A client chooses its name freely, so it could change
the name to get past the cooldown, or claim another client's name to
keep that client from asking.

Only a request with a credential that iMCP issued has a cooldown now,
keyed by that credential. Requests without one are bounded by the
limits on open dialogs, waiting connections, and expiry.

The approval dialog now says when a client presents no credential,
which means that any app could have sent its name.

* Keep Return from approving a connection

The approval window takes focus and floats above other windows when it
opens, and "Allow" was its default button, so a Return or a click meant
for another window could approve a client.

"Allow" now has no keyboard shortcut and turns on one second after the
dialog appears. Escape still clicks "Deny".

* Close a client's connections when its credential is removed

Removing a trusted client, or all of them, revoked the credentials so
that they approved no new connections, but sessions that they had
already approved stayed open.

The app now records the connections that presented each credential
that iMCP issued, and the connections that a new credential from
"Always trust this client" was issued for. Removing a client closes
its connections, including ones that wait for approval, and removing
all clients closes every connection with a credential. Connections
without a credential, which a person approved, stay open. The network
manager now reports every closed connection to the controller, not
only those that wait for approval, so that it can forget them.

* Note that the Debug keychain fallback doesn't protect trust

Unsigned Debug builds store client records in the login keychain,
where any process that runs as the same user can add one, including a
trusted record. A comment on the fallback now says so.

* Split a test's malformed credentials into a typed array

Xcode 26.0 can't type-check the mixed String and Substring array literal in reasonable time.
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.

1 participant