Skip to content

feat(mcp): Add environment variable expansion support for MCP server configurations #535

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

pexa-tday
Copy link

Summary

This PR adds support for environment variable expansion in MCP (Model Context Protocol) server configurations, allowing users to use ${VAR_NAME} syntax in command paths, arguments, and
environment variables.

Changes Made

Core Implementation

New module: crates/chat-cli/src/util/env_expansion.rs
• Implements expand_env_vars() function with regex-based ${VAR_NAME} pattern matching
• Provides specialized functions for expanding variables in commands, arguments, and environment maps
• Comprehensive error handling for missing environment variables

Integration

Modified: crates/chat-cli/src/cli/chat/tools/custom_tool.rs
• Integrated environment variable expansion into CustomToolClient::from_config()
• Expands variables in command paths, arguments, and environment variables before MCP client creation
• Added proper error propagation with descriptive error messages

Testing

Unit tests in env_expansion.rs covering:
• Simple and complex variable expansion scenarios
• Missing variable error handling
• HashMap and vector expansion utilities
Integration tests in custom_tool.rs covering:
• Full MCP configuration expansion workflow
• Mixed expansion scenarios (some vars expanded, some static)
• Error cases with missing variables
Additional test file: tests/mcp_env_expansion_test.rs with comprehensive mock testing

Use Cases

This feature enables users to:
• Store sensitive credentials in environment variables: "env": {"API_KEY": "${MY_API_KEY}"}
• Use dynamic paths: "command": "${PYTHON_PATH}"
• Configure server ports: "args": ["--port", "${SERVER_PORT}"]
• Maintain environment-specific configurations without hardcoding values

Example Configuration

json
{
"command": "${PYTHON_PATH}",
"args": ["-m", "server", "--port", "${SERVER_PORT}"],
"env": {
"API_KEY": "${MY_API_KEY}",
"CONFIG_PATH": "${HOME}/.config/myapp"
}
}

Error Handling

• Clear error messages when environment variables are missing
• Fails fast during MCP client initialization to prevent runtime issues
• Preserves original configuration values when expansion fails

Backward Compatibility

• Fully backward compatible - configurations without environment variables work unchanged
• No breaking changes to existing MCP server configurations
• Optional feature that only activates when ${VAR_NAME} patterns are detected

🤖 Assisted by Amazon Q Developer

…configurations

- Add env_expansion utility module with support for ${VAR} syntax
- Update CustomToolClient to expand environment variables in command, args, and env
- Support expansion in MCP server command, arguments, and environment variables
- Add comprehensive error handling for missing environment variables
- Add tests for environment variable expansion functionality

This enables dynamic MCP server configurations using system environment
variables, improving security by avoiding hardcoded credentials and
supporting environment-specific configurations.

🤖 Assisted by [Amazon Q Developer](https://aws.amazon.com/q/developer)
…nsion

The functionality is self-explanatory and doesn't need explicit comments
mentioning the feature in every location where it's used.

🤖 Assisted by [Amazon Q Developer](https://aws.amazon.com/q/developer)
@pexa-tday pexa-tday requested a review from a team as a code owner August 6, 2025 23:57
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.

1 participant