perf: Avoid scanning installed packages when MCP SDK is not installed#6865
Open
mangodxd wants to merge 1 commit into
Open
perf: Avoid scanning installed packages when MCP SDK is not installed#6865mangodxd wants to merge 1 commit into
mangodxd wants to merge 1 commit into
Conversation
…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 <noreply@anthropic.com>
mangodxd
force-pushed
the
fix/gh-6769-lazy-package-version-mcp
branch
from
July 23, 2026 11:08
01cf58a to
07564c8
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
WHY
When the MCP SDK is not installed, module-level
package_version("mcp")at the top ofsentry_sdk/integrations/mcp.pyscans every installed distribution via_get_installed_modules(). This slowssentry_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 thetry/except ImportErrorguard.HOW
Move
MCP_PACKAGE_VERSION = package_version("mcp")from module-level (above the guard) to inside thetryblock, after the successfulimport mcp. Whenimport mcpfails andDidNotEnableis raised,package_version()is never called — so environments without MCP skip the expensive distribution scan entirely.