Repository navigation
perf: create McpContext on the first tool call - #3568
Conversation
72fe69e to
bd0ca1a
Compare
OrKoN
left a comment
There was a problem hiding this comment.
I think we need to check the following:
-
src/index.ts,#ensureContext(): Reject before theimport()when#closedis true, and keep the post-import check for aclose()that occurs during the import. The current code loads the browser bundle and constructsMcpContext(which callsoverrideDevToolsGlobals) afterclose(), then gives the disposed context to the tool handler. -
eslint.config.js: Add a restriction on a value import of./McpContext.jsinsrc/index.ts, or add a test that walks the static import graph of the two entrypoints. The rule matches only a direct import ofthird_party/browser.js, soimport {McpContext} from './McpContext.js'passes lint and loads the bundle at startup again. -
tests/McpServer.test.ts: Add a test that starts two tool calls before the firstimport()resolves and assertscalledOnceWithExactlyonMcpContext.create. No test calls the#contextPromise ??=branch with two concurrent callers, and that branch prevents a second context. -
tests/McpServer.test.ts,disposes a context created after the server is already closed: Replacesinon.assert.calledOnce(createContext)withcalledOnceWithExactly, as the first test does.AGENTS.mdrequires exact-argument assertions.
3bccb16 to
a55a7ce
Compare
a55a7ce to
0ca4675
Compare
src/index.ts now imports McpContext only as a type and loads it via a cached dynamic import on the first #getContext() call, cutting idle server private memory by ~55%. Roots updates, config reloads, and close() before any tool call run without creating McpContext, #ensureContext() rejects immediately when closed (and disposes and rejects if closed while import() is in flight), and ESLint now forbids browser.js value imports in src/index.ts, src/bin/**/*.ts, and src/daemon/**/*.ts as well as ./McpContext.js value imports in src/index.ts. Refs #3427
0ca4675 to
29533a8
Compare
|
Thanks for the review! Addressed all 4 items and rebased onto
|
Refs #3427
Lazy loading: server startup part d (parts a–c merged in #3563–#3566). Deferring
McpContextuntil the first tool call removes the last static startup path from the server and daemon entrypoints tothird_party/browser.js.Idle memory footprint (before vs. after)
Measured on release builds (
npm run profile:footprint, 3000 ms settle time afterinitialize+tools/list) on LTS Node.js releases:test-memory-leaks.yml, Node 22 LTS, 3 launches):main)default(--usage-statistics)default(--usage-statistics)no-usage-statistics(--no-usage-statistics)no-usage-statistics(--no-usage-statistics)v24.12.0LTS, 5 launches,--ignore-budgets):default: RSS98.54 MiB, PSS71.41 MiB, Private48.07 MiB(down from103.09 MiBprivate before lazyMcpContext, -55.02 MiB / -53.4%).no-usage-statistics: RSS92.11 MiB, PSS66.14 MiB, Private43.95 MiB(down from101.61 MiBprivate, -57.66 MiB / -56.7%).Tightening the private-memory budgets in
scripts/profile/footprint_mcp.tsfollows in #3574.What changed
src/index.ts:McpContextonly as a type and stops constructingMcpContextinMcpServer's constructor.#ensureContext(), called from#getContext(), rejects immediately whenthis.#closedis alreadytruebefore importing./McpContext.js, loads./McpContext.jsonce with a cached dynamicimport(), constructsMcpContext, and synchronously callscontext.setRoots(this.#combinedRoots())in the same microtask before storingthis.#context.#updateRoots()recordsthis.#lastClientRoots = result.rootsand callsthis.#context?.setRoots(this.#combinedRoots())if the context exists.#applyConfigLocked()commitsnextConfigtothis.#configand callssetRoots()/onConfigChanged(previousArgs)only whenthis.#contexthas already been created; while idle, a laterMcpContext.create()reads the committedthis.#configand combined roots directly.close()setsthis.#closed = trueand disposesthis.#contextif present; ifclose()occurs whileimport('./McpContext.js')is in flight,#ensureContext()disposes the newly created context and rejects withMCP server is closed.McpContextrejects,this.#contextPromiseis reset toundefinedso later tool calls retry.eslint.config.jsforbids value imports fromthird_party/browser.jsinsrc/index.ts,src/bin/**/*.ts, andsrc/daemon/**/*.ts, and forbids value imports from./McpContext.jsinsrc/index.ts.Modules now in both bundles
None new.
What still loads browser.js at startup
Nothing: a static walk of the built JS in
out/Default/gen/srcfrom bothbin/chrome-devtools-mcp-main.jsanddaemon/daemon.jsno longer reachesthird_party/browser.js.Test plan
tests/McpServer.test.ts:creates the context on the first tool call with the navigation limitapplies roots updates and config reloads received while idle when the first tool call creates the contextcreates a single context when two tool calls start concurrentlycloses without creating or disposing a context when no tool ranrejects tool calls after close without creating a contextdisposes a context created while the server is closing and rejects the callretries creating the context when a previous creation threwnpm run formatand fullnpm run test.