Skip to content

perf(mcp): Load the server stack only when the stdio server starts - #1764

Merged
BYK merged 1 commit into
mainfrom
perf/mcp-defer-server-stack
Oct 31, 2026
Merged

BYK merged 1 commit into
mainfrom
perf/mcp-defer-server-stack

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Part of #1761.

runMcpServer() in @sentry/mcp-server imported mcp-core's server and every tool, the AI SDK providers, @sentry/node, and the v1 stdio transport at module load. --help, --version, and argument errors paid for all of that before printing one line. Those imports now load only after the config and token resolve, right before the server starts. The auth subcommand and the device-code flow load only on the paths that use them.

Measured on Node 22, max RSS from process.resourceUsage().maxRSS in a -r preload, 3 runs each:

before after
sentry mcp --version (CLI npm bundle) 178 MB, ~450 ms 147 MB, ~310 ms
sentry mcp --help (CLI npm bundle) 178 MB, ~435 ms 148 MB, ~370 ms
sentry-mcp --version (standalone packages/mcp-server/dist) 146 MB, ~470 ms 47 MB, ~12 ms
sentry mcp idle: initialize + tools/list 180 MB 180 MB (unchanged)
CLI dist/index.cjs bytes (metafile) 7,869,567 7,870,930 (+1.3 KB of tsdown chunk glue)

The idle server still loads the same modules, so idle RSS doesn't change.

For #1731's restack: the same issue exists in packages/cli/src/lib/mcp.ts. There, startMcpServer() imports parseMcpInvocation from ./mcp/server.js, which statically imports mcp-core and @sentry/node. On a local copy of that branch, returning before that import for --version took sentry-mcp --version from 137 MB / 335 ms to 107 MB / 165 ms. Moving parseMcpInvocation into a module without those imports, next to args.ts and usage.ts, would carry this change over. This PR edits packages/mcp-server/src/index.ts, which #1731 deletes, so the restack can drop this file's changes.

Lazy-loading the tool modules themselves (option 3 in the issue) is left out on purpose: tools/list needs every tool's schema at startup. Stubbing the whole AI SDK out of the #1731 bundle, an upper bound for lazy-loading it, saved only about 3.7 MB of idle RSS and about 30 ms.

via BYK.

--

View Session in Sentry

runMcpServer() imported mcp-core's server and tools, the AI SDK
providers, @sentry/node, and the stdio transport at module load, so
--help, --version, and argument errors paid for all of them. Import
them after the config and token are resolved instead, and load the
auth command and device-code flow only on the paths that use them.

Part of #1761

Co-Authored-By: BYK <burak.kaya@sentry.io>
@vercel

vercel Bot commented Oct 30, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
sentry-local Skipped Skipped Oct 30, 2026 11:37pm UTC

Request Review

@BYK
BYK merged commit 15d4388 into main Oct 31, 2026
30 checks passed
@BYK
BYK deleted the perf/mcp-defer-server-stack branch October 31, 2026 14:10
sentry-junior Bot added a commit that referenced this pull request Oct 31, 2026
Brings in the squash-merged #1763 (Zod English locale), #1764 (lazy server
stack) and #1765 (lazy syntax highlighting), which this branch had merged
from their PR branches.

Conflicts:
- packages/mcp-server/src/index.ts: kept deleted. #1764's lazy loading is
  already applied to the unified entry (commands/mcp.ts -> lib/mcp.ts ->
  lib/mcp/server.ts).
- script/bundle.ts, script/build.ts: kept the zod/v3 stub plugin. bundle.ts
  keeps the shared script/lib/require-alias-plugin.ts (the
  @sentry/mcp-server bundle uses it too) instead of main's inline copy.
  main's filter also lists list-command.ts, but on this branch that file no
  longer uses _require (the route registry replaced it), so it stays out.
- formatters/highlight.ts, syntax-highlight-bundle.test.ts: identical to
  main apart from comments that point at the shared require-alias plugin.

This branch was previously deployed

1 inactive deployment
Preview — 28da1711 Deployed Oct 30, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: medium PR risk score: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant