Skip to content

perf: Avoid scanning installed packages when MCP SDK is not installed - #6865

Open
mangodxd wants to merge 1 commit into
getsentry:masterfrom
mangodxd:fix/gh-6769-lazy-package-version-mcp
Open

perf: Avoid scanning installed packages when MCP SDK is not installed#6865
mangodxd wants to merge 1 commit into
getsentry:masterfrom
mangodxd:fix/gh-6769-lazy-package-version-mcp

Conversation

@mangodxd

Copy link
Copy Markdown

WHY

When the MCP SDK is not installed, module-level package_version("mcp") at the top of sentry_sdk/integrations/mcp.py scans every installed distribution via _get_installed_modules(). This slows sentry_sdk.init() by ~0.2s in environments with hundreds of packages.

Issue #6769 reports that this regression was introduced by #6583, which added a module-level MCP_PACKAGE_VERSION = package_version("mcp") above the try/except ImportError guard.

HOW

Move MCP_PACKAGE_VERSION = package_version("mcp") from module-level (above the guard) to inside the try block, after the successful import mcp. When import mcp fails and DidNotEnable is raised, package_version() is never called — so environments without MCP skip the expensive distribution scan entirely.

…getsentry#6769)

When mcp SDK is not installed, module-level package_version('mcp') at
the top of sentry_sdk/integrations/mcp.py scans every installed
distribution, slowing sentry_sdk.init() by ~200ms in environments
with hundreds of packages.

Move the call below the 	ry/except ImportError guard so it only runs
when import mcp succeeds. Environments without mcp skip the scan
entirely.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@mangodxd
mangodxd requested a review from a team as a code owner July 23, 2026 11:08
@mangodxd
mangodxd force-pushed the fix/gh-6769-lazy-package-version-mcp branch from 01cf58a to 07564c8 Compare July 23, 2026 11:08

@Chenghao999 Chenghao999 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — moving package_version("mcp") inside the try block skips the import-time package scan whenever MCP is not installed, which is a nice small perf win for the common case.

I checked the safety of the move: MCP_PACKAGE_VERSION is still referenced at module level outside the try (the version-gated imports and _patch_* selection), but the module either fully loads (assignment runs) or aborts with DidNotEnable, so there is no path where it is read while undefined. Clean change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants