Skip to content

fix(mcp): pre-compile env-var regex and unify interpolation across mcp_tool and mcp_config - #2719

Closed
amethystani wants to merge 1 commit into
NousResearch:mainfrom
amethystani:fix/mcp-env-var-interpolation
Closed

amethystani wants to merge 1 commit into
NousResearch:mainfrom
amethystani:fix/mcp-env-var-interpolation

Conversation

@amethystani

Copy link
Copy Markdown
Contributor

Summary

Fixes two related code quality issues in MCP env-var interpolation:

Changes

tools/mcp_tool.py

  • Add module-level _ENV_VAR_PATTERN = re.compile(r"\$\{([^}]+)\}") after _CREDENTIAL_PATTERN
  • Remove redundant inner import re inside _interpolate_env_vars() (module-level import already exists)
  • Use pre-compiled pattern instead of re-compiling on every call

hermes_cli/mcp_config.py

  • Remove _interpolate_value() which used \w+ and would silently fail on env vars with hyphens or dots (e.g. MY-VAR, my.var)
  • Replace its single call site with the shared _ENV_VAR_PATTERN from mcp_tool.py
  • Remove now-unused import re

Impact

Pure code hygiene — no behavior change for standard [a-zA-Z0-9_] env var names. Fixes a subtle inconsistency where hermes mcp test could show unresolved auth headers for env vars with hyphens/dots that work correctly at runtime.

Closes #2711
Closes #2712

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.
@alt-glitch alt-glitch added type/refactor Code restructuring, no behavior change P3 Low — cosmetic, nice to have tool/mcp MCP client and OAuth comp/cli CLI entry point, hermes_cli/, setup wizard labels May 3, 2026
@alt-glitch

Copy link
Copy Markdown
Contributor

Note: #2728 (same refactor) was closed. This appears to be a replacement.

@teknium1

Copy link
Copy Markdown
Collaborator

Merged via #26240 — your commit was cherry-picked onto current main with your authorship preserved in git log. Thanks @amethystani!

#26240

@teknium1 teknium1 closed this May 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/cli CLI entry point, hermes_cli/, setup wizard P3 Low — cosmetic, nice to have tool/mcp MCP client and OAuth type/refactor Code restructuring, no behavior change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unify env-var interpolation between mcp_tool.py and mcp_config.py Pre-compile env-var interpolation regex in mcp_tool.py

3 participants