Add double-connect guard to prevent protocol message handling errors (#450)
* 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 <noreply@anthropic.com>
This commit is contained in:
parent
8dbc06a355
commit
f615123711
3 changed files with 44 additions and 0 deletions
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -1118,6 +1118,11 @@ export class App extends Protocol<AppRequest, AppNotification, AppResult> {
|
|||
),
|
||||
options?: RequestOptions,
|
||||
): Promise<void> {
|
||||
if (this.transport) {
|
||||
throw new Error(
|
||||
"App is already connected. Call close() before connecting again.",
|
||||
);
|
||||
}
|
||||
await super.connect(transport);
|
||||
|
||||
try {
|
||||
|
|
|
|||
Loading…
Reference in a new issue