Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/app-bridge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions src/app-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,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 {
Expand Down
Loading