From 28da1711c143ccf2a55cfc42f64bd24fbc341296 Mon Sep 17 00:00:00 2001 From: "sentry-junior[bot]" <264270552+sentry-junior[bot]@users.noreply.github.com> Date: Fri, 30 Oct 2026 23:35:34 +0000 Subject: [PATCH] perf(mcp): Load the server stack only when the stdio server starts 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 --- packages/mcp-server/src/index.ts | 39 +++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/packages/mcp-server/src/index.ts b/packages/mcp-server/src/index.ts index cd8a1d608..db7d1959d 100644 --- a/packages/mcp-server/src/index.ts +++ b/packages/mcp-server/src/index.ts @@ -22,25 +22,16 @@ import { pathToFileURL } from "node:url"; -import { - getAgentProvider, - getResolvedProviderType, - setAgentProvider, - setProviderBaseUrls, -} from "@sentry/mcp-core/internal/agents/provider-factory"; -import { buildServer } from "@sentry/mcp-core/server"; +// Keep static imports light: `--help`, `--version`, and argument errors must +// not load the tools, the AI SDK, or the Sentry SDK. The server stack is +// imported in runMcpServer() once the server is about to start. import { SKILLS } from "@sentry/mcp-core/skills"; -import { sentryBeforeSend } from "@sentry/mcp-core/telem/sentry"; import { LIB_VERSION } from "@sentry/mcp-core/version"; -import * as Sentry from "@sentry/node"; -import { resolveAccessToken } from "./auth/resolve-token"; -import { authCommand } from "./cli/commands/auth"; import { printCliLine } from "./cli/output"; import { merge, parseArgv, parseEnv } from "./cli/parse"; import { finalize } from "./cli/resolve"; import type { PartiallyResolvedConfig } from "./cli/types"; import { buildUsage } from "./cli/usage"; -import { startStdio } from "./transports/stdio"; const defaultPackageName = "@sentry/mcp-server"; const allSkills = Object.keys(SKILLS) as ReadonlyArray< @@ -95,6 +86,7 @@ export async function runMcpServer( new Error("Use `sentry auth` to manage credentials for `sentry mcp`."), ); } + const { authCommand } = await import("./cli/commands/auth"); await authCommand(rawArgs.slice(1)); return; } @@ -130,11 +122,32 @@ export async function runMcpServer( ...partialCfg, accessToken, })) - : resolveAccessToken(partialCfg) + : import("./auth/resolve-token").then(({ resolveAccessToken }) => + resolveAccessToken(partialCfg), + ) ).catch((err) => { die(err); }); + const [ + { + getAgentProvider, + getResolvedProviderType, + setAgentProvider, + setProviderBaseUrls, + }, + { buildServer }, + { sentryBeforeSend }, + Sentry, + { startStdio }, + ] = await Promise.all([ + import("@sentry/mcp-core/internal/agents/provider-factory"), + import("@sentry/mcp-core/server"), + import("@sentry/mcp-core/telem/sentry"), + import("@sentry/node"), + import("./transports/stdio"), + ]); + // Configure embedded agent provider if (cfg.agentProvider) { setAgentProvider(cfg.agentProvider);