From 569e2b24a491e9097232972551643b9163a46b53 Mon Sep 17 00:00:00 2001 From: Joe Hanley Date: Fri, 25 Sep 2026 23:23:12 +0000 Subject: [PATCH 1/2] fix(mcp): start project detection in start() instead of constructor (b/566322255) --- src/mcp/index.spec.ts | 24 ++++++++++++++++++++++++ src/mcp/index.ts | 5 +++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/mcp/index.spec.ts b/src/mcp/index.spec.ts index e935bafc55e..c1d4c7bfc57 100644 --- a/src/mcp/index.spec.ts +++ b/src/mcp/index.spec.ts @@ -120,3 +120,27 @@ describe("FirebaseMcpServer.mcpCallTool", () => { expect(toolFn.firstCall.args[0]).to.deep.equal({ foo: "bar" }); }); }); + +describe("FirebaseMcpServer project detection", () => { + afterEach(() => { + sinon.restore(); + }); + + it("does not start project detection when constructed", () => { + const detectStub = sinon.stub(FirebaseMcpServer.prototype, "detectProjectSetup").resolves(); + + new FirebaseMcpServer({}); + + expect(detectStub.called).to.be.false; + }); + + it("starts project detection when started", async () => { + const detectStub = sinon.stub(FirebaseMcpServer.prototype, "detectProjectSetup").resolves(); + const server = new FirebaseMcpServer({}); + sinon.stub(server.server, "connect").resolves(); + + await server.start(); + + expect(detectStub.calledOnce).to.be.true; + }); +}); diff --git a/src/mcp/index.ts b/src/mcp/index.ts index 936f61e3a1e..230751725f1 100644 --- a/src/mcp/index.ts +++ b/src/mcp/index.ts @@ -156,8 +156,6 @@ export class FirebaseMcpServer { this.currentLogLevel = params.level; return {}; }); - - void this.detectProjectSetup(); } /** Wait until initialization has finished. */ @@ -508,6 +506,9 @@ export class FirebaseMcpServer { } async start(options?: { useSSE?: boolean; port?: number }): Promise { + // Background detection starts when the server is started, not when it is constructed, so constructing a server (as unit tests do) has no side effects. + void this.detectProjectSetup(); + if (options?.useSSE) { const app = express(); From 294160eb49619f3ada175392ddfaf0f40912f2d7 Mon Sep 17 00:00:00 2001 From: Joe Hanley Date: Sat, 3 Oct 2026 01:22:10 +0000 Subject: [PATCH 2/2] fix(mcp): catch unhandled rejection on background project detection --- src/mcp/index.spec.ts | 18 ++++++++++++++++++ src/mcp/index.ts | 4 +++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/mcp/index.spec.ts b/src/mcp/index.spec.ts index c1d4c7bfc57..df4606b0246 100644 --- a/src/mcp/index.spec.ts +++ b/src/mcp/index.spec.ts @@ -143,4 +143,22 @@ describe("FirebaseMcpServer project detection", () => { expect(detectStub.calledOnce).to.be.true; }); + + it("handles rejection in background project detection without unhandled rejection", async () => { + sinon + .stub(FirebaseMcpServer.prototype, "detectProjectSetup") + .rejects(new Error("detection failed")); + const server = new FirebaseMcpServer({}); + sinon.stub(server.server, "connect").resolves(); + const debugStub = sinon.stub(); + sinon.stub(server, "logger").get(() => ({ + debug: debugStub, + })); + + await server.start(); + await new Promise((resolve) => setTimeout(resolve, 10)); + + expect(debugStub.calledOnce).to.be.true; + expect(debugStub.firstCall.args[0]).to.include("Background project detection failed"); + }); }); diff --git a/src/mcp/index.ts b/src/mcp/index.ts index 230751725f1..03e67026822 100644 --- a/src/mcp/index.ts +++ b/src/mcp/index.ts @@ -507,7 +507,9 @@ export class FirebaseMcpServer { async start(options?: { useSSE?: boolean; port?: number }): Promise { // Background detection starts when the server is started, not when it is constructed, so constructing a server (as unit tests do) has no side effects. - void this.detectProjectSetup(); + void this.detectProjectSetup().catch((err: unknown) => { + this.logger.debug(`[mcp] Background project detection failed: ${err}`); + }); if (options?.useSSE) { const app = express();