From f615123711fff0cfa95e2b78cb425af878896dd4 Mon Sep 17 00:00:00 2001 From: Olivier Chafik Date: Tue, 17 Feb 2026 18:15:24 +0000 Subject: [PATCH] Add double-connect guard to prevent protocol message handling errors (#450) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: guard against double-connect in App and AppBridge (#429) Calling connect() twice on the same transport instance caused the MCP SDK's Protocol class to chain onmessage handlers, so each incoming message was processed twice. This made incoming responses get consumed by the first processing (resolving the handler) and then trigger a spurious "Received a response for an unknown message ID" error on the second processing pass, since the handler was already removed from the response map. Add an explicit guard in both App.connect() and AppBridge.connect() that throws if the instance is already connected. This converts the silent message-corruption into a clear, actionable error and prevents downstream confusion. Adds three regression tests: - AppBridge.connect() throws when already connected - App.connect() throws when already connected - Demonstrates the double-processing root cause and verifies it is fixed Fixes #429 https://claude.ai/code/session_01XU3FW1TpjBkyRbkBUH5gqR * test: replace redundant regression test with cleaner same-transport guard check The previous third test in "double-connect guard" never actually attempted a double-connect — it created fresh instances and verified normal operation, which every other test already covers. The stray bridge.onupdatemodelcontext and bridgeTransport.close() at the top were leftover noise. Replace it with a focused test that verifies the guard fires even when the caller passes the *same* transport object (not just a different one), which is the exact scenario described in #429. https://claude.ai/code/session_01XU3FW1TpjBkyRbkBUH5gqR --------- Co-authored-by: Claude --- src/app-bridge.test.ts | 34 ++++++++++++++++++++++++++++++++++ src/app-bridge.ts | 5 +++++ src/app.ts | 5 +++++ 3 files changed, 44 insertions(+) diff --git a/src/app-bridge.test.ts b/src/app-bridge.test.ts index ca38e2a..202b2ed 100644 --- a/src/app-bridge.test.ts +++ b/src/app-bridge.test.ts @@ -615,6 +615,40 @@ describe("App <-> AppBridge integration", () => { }); }); + describe("double-connect guard", () => { + it("AppBridge.connect() throws if already connected", async () => { + await bridge.connect(bridgeTransport); + await app.connect(appTransport); + + // Attempting to connect again with a different transport should throw + const [, secondBridgeTransport] = InMemoryTransport.createLinkedPair(); + await expect(bridge.connect(secondBridgeTransport)).rejects.toThrow( + "AppBridge is already connected", + ); + }); + + it("App.connect() throws if already connected", async () => { + await bridge.connect(bridgeTransport); + await app.connect(appTransport); + + // Attempting to connect again should throw + const [secondAppTransport] = InMemoryTransport.createLinkedPair(); + await expect(app.connect(secondAppTransport)).rejects.toThrow( + "App is already connected", + ); + }); + + it("AppBridge.connect() throws even when called with the same transport", async () => { + await bridge.connect(bridgeTransport); + await app.connect(appTransport); + + // Should throw regardless of whether it's the same or a different transport + await expect(bridge.connect(bridgeTransport)).rejects.toThrow( + "AppBridge is already connected", + ); + }); + }); + describe("ping", () => { it("App responds to ping from bridge", async () => { await bridge.connect(bridgeTransport); diff --git a/src/app-bridge.ts b/src/app-bridge.ts index 3322a0d..6e1c021 100644 --- a/src/app-bridge.ts +++ b/src/app-bridge.ts @@ -1409,6 +1409,11 @@ export class AppBridge extends Protocol< * ``` */ async connect(transport: Transport) { + if (this.transport) { + throw new Error( + "AppBridge is already connected. Call close() before connecting again.", + ); + } if (this._client) { // When a client was passed to the constructor, automatically forward // MCP requests/notifications between the view and the server diff --git a/src/app.ts b/src/app.ts index d8d49ed..16f8da6 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1118,6 +1118,11 @@ export class App extends Protocol { ), options?: RequestOptions, ): Promise { + if (this.transport) { + throw new Error( + "App is already connected. Call close() before connecting again.", + ); + } await super.connect(transport); try {