feat(mcp): Add environment variable expansion support for MCP server configurations #535
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.
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