fix(mcp): pre-compile env-var regex and unify interpolation - #26240
Merged
Merged
Conversation
Remove redundant inner `import re` and regex recompilation on every call in _interpolate_env_vars. Add module-level _ENV_VAR_PATTERN compiled once. Replace the separate _interpolate_value() in mcp_config.py (which used \w+ and would silently fail on env vars containing hyphens or dots) with the shared _ENV_VAR_PATTERN from mcp_tool.py. Remove now-unused import re.
Contributor
🔎 Lint report:
|
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.
Salvage of #2719 by @amethystani onto current main.
Summary
Pre-compiles the
${VAR}regex once at module level intools/mcp_tool.pyand replaces the diverged\w+copy inhermes_cli/mcp_config.pywith the shared pattern. Fixes #2711 and #2712.Changes
tools/mcp_tool.py: add_ENV_VAR_PATTERN = re.compile(r"\$\{([^}]+)\}")near_CREDENTIAL_PATTERN;_interpolate_env_varsuses it.hermes_cli/mcp_config.py: delete_interpolate_value()(its\w+pattern silently dropped env vars with-or.), reuse_ENV_VAR_PATTERNat the single call site incmd_mcp_test. Keptimport re(still used by_ENV_VAR_NAME_REon current main).from tools.mcp_tool import ...to the first-party import group (PEP 8 grouping).Validation
${MY-VAR}/${my.var}now resolve correctly in the CLI test masking path (previously dropped silently).Original PR: #2719
Closes #2711
Closes #2712
Co-authored-by: amethystani 108541149+amethystani@users.noreply.github.com