Repository navigation
Connect imcp-server over a verified socket and trust clients by credential - #328
Merged
Merged
Conversation
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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".
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.
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.
Xcode 26.0 can't type-check the mixed String and Substring array literal in reasonable time.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
iMCP 1.8.1 and earlier let another process on the same Mac intercept an MCP client's session with iMCP, or connect as a client that the user trusted (GHSA-q4pc-f59f-9xmw).
imcp-serverfound iMCP with Bonjour and relayed the session over loopback TCP, so any local process could advertise the service or connect to iMCP's port, and iMCP trusted a client by the name that it reported ininitialize. The fix shipped in 1.8.2 fromrelease/1.8.2.This PR brings the fix to
main. 1.8.2 has the same fix, backported to the 1.8.x line; its version bump comes tomainin a separate PR.imcp-serverconnects to iMCP over a Unix domain socket in iMCP's container, and each side checks the other's code signature before it sends anything.imcp-serveralso checks that iMCP's process holds the other end of the connection, and iMCP listens again if the socket file is removed or replaced.imcp-serverno longer uses Bonjour or needs Local Network access.imcp-serverasIMCP_CLIENT_CREDENTIAL, instead of by its reported name. The Keychain stores only the credential's SHA-256 hash. Clients that earlier versions trusted by name are listed under "Approve Again" in settings, and removing a client closes its connections.tools/list_changedgoes only to approved connections, and Return no longer clicks "Allow".imcp-serverexits instead of reconnecting, because a new connection has no approval.imcp-serverto the app".