Skip to content

refactor(mcp): unify env-var interpolation and pre-compile regex - #2728

Closed
kagura-agent wants to merge 1 commit into
NousResearch:mainfrom
kagura-agent:fix/unify-env-var-interpolation
Closed

refactor(mcp): unify env-var interpolation and pre-compile regex#2728
kagura-agent wants to merge 1 commit into
NousResearch:mainfrom
kagura-agent:fix/unify-env-var-interpolation

Conversation

@kagura-agent

Copy link
Copy Markdown
Contributor

Summary

Two separate env-var interpolation functions existed with inconsistent regex patterns, causing hermes mcp test to potentially show different results than runtime.

Fixes #2711 and #2712.

Root Cause

File Regex Behavior
tools/mcp_tool.py \$\{([^}]+)\} Matches any chars inside ${...}
hermes_cli/mcp_config.py \$\{(\w+)\} Word chars only — silently fails on - or .

Changes

tools/mcp_tool.py (+3/-2):

  • Pre-compile regex as _ENV_VAR_PATTERN at module level
  • Remove redundant inner import re in _interpolate_env_vars()

hermes_cli/mcp_config.py (+2/-4):

  • Rewrite _interpolate_value() to reuse _ENV_VAR_PATTERN from mcp_tool
  • Remove now-unused import re

Testing

  • No behavioral change for common env var names ([a-zA-Z0-9_])
  • Fixes resolution for env vars with dots or hyphens (edge case)
  • Pre-compiled regex eliminates per-call compilation overhead

Two env-var interpolation functions existed with different regex patterns:
- mcp_tool.py used $\{([^}]+)\} (matches any chars — correct)
- mcp_config.py used $\{(\w+)\} (word chars only — fails on dots/hyphens)

This meant 'hermes mcp test' could show unresolved values for headers
that work correctly at runtime.

Changes:
- Pre-compile regex as _ENV_VAR_PATTERN at module level in mcp_tool.py
  (removes redundant inner 'import re' on every call)
- Rewrite mcp_config.py's _interpolate_value to reuse _ENV_VAR_PATTERN
- Remove unused 'import re' from mcp_config.py

Fixes NousResearch#2711, fixes NousResearch#2712
@kagura-agent

Copy link
Copy Markdown
Contributor Author

Hi team — checking in on this one. Let me know if there are any concerns or changes needed. Will close in a week if no response. Thanks!

@kagura-agent

Copy link
Copy Markdown
Contributor Author

Gentle ping — any thoughts on this refactor? Happy to adjust if needed. 🙏

@kagura-agent

Copy link
Copy Markdown
Contributor Author

Closing — no response after two pings over 9 days. Feel free to reopen or cherry-pick if this becomes relevant. Thanks! 🙏

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.

Pre-compile env-var interpolation regex in mcp_tool.py

1 participant