-
Notifications
You must be signed in to change notification settings - Fork 333
feat: Dynamic Environment Variable Injection for STDIO MCP Servers #1162
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
Merged
Conversation
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
manavgup
added a commit
that referenced
this pull request
Oct 3, 2025
- Remove unused imports: signal, time, sys, logging, json, typing imports - Remove unused variables: endpoint_event_found, headers - Remove unused mock imports: AsyncMock, Mock, patch where not used - Fixes CI ruff linter failures for PR #1162
manavgup
added a commit
that referenced
this pull request
Oct 3, 2025
- Fix test_stop_after_start to expect _proc to be None after stop() - This matches the actual implementation behavior where process is cleaned up - Fixes pytest failures in CI for PR #1162
manavgup
added a commit
that referenced
this pull request
Oct 3, 2025
- Remove unused imports and variables to fix ruff linting errors - Fix test expectations for process cleanup after stop() - Improve E2E test robustness with timeout handling for SSE streaming - All tests now pass consistently Fixes CI failures for PR #1162
🔧 Recent Fixes (Latest Commit)Test Infrastructure Improvements
Test Results
|
…servers Implements dynamic environment variable injection in mcpgateway.translate to extract HTTP headers from incoming requests and inject them as environment variables when starting STDIO MCP processes. Core changes: - Enhanced StdIOEndpoint class with environment variable support - Added CLI arguments --enable-dynamic-env and --header-to-env - New translate_header_utils.py for secure header processing - Comprehensive test coverage (unit, integration, e2e) - Full backward compatibility maintained Enables multi-tenant support for enterprise MCP deployments where different users need different tokens passed to STDIO MCP servers. Closes #964 Signed-off-by: Manav Gupta <manavg@gmail.com>
- Remove unused imports and variables to fix ruff linting errors - Fix test expectations for process cleanup after stop() - Improve E2E test robustness with timeout handling for SSE streaming - All tests now pass consistently Fixes CI failures for PR #1162
- Fix req_id scope errors in main.py endpoints - Add missing returncode attributes to mock process classes - Enhance MockStdio class with required methods - Enable authentication settings for credential tests - Implement dynamic port allocation to prevent conflicts - Add proper MCP SSE flow with timeout handling - Fix hanging tests with timeout decorators - Update status code assertions to be flexible All tests now pass consistently without hanging. Signed-off-by: Manav Gupta <manavg@gmail.com>
- Move asyncio.sleep outside httpx.AsyncClient context - Prevent event loop closure during health check retries - Fix CI/CD failures while maintaining local test compatibility Signed-off-by: Manav Gupta <manavg@gmail.com>
Signed-off-by: Manav Gupta <manavg@gmail.com>
Signed-off-by: Manav Gupta <manavg@gmail.com>
Signed-off-by: Manav Gupta <manavg@gmail.com>
- Add is_running() public method to StdIOEndpoint to avoid accessing protected _proc attribute - Move parse_header_mappings import to top-level imports to avoid import-outside-toplevel warning - Apply code formatting with black to maintain consistency This improves code quality while maintaining all functionality. Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
Add comprehensive documentation for the new dynamic environment variable injection feature in mcpgateway.translate: - Added new section in Configuration Options table - Documented --enable-dynamic-env and --header-to-env flags - Added multi-tenant GitHub Enterprise example with per-user tokens - Included security features and validation details - Updated Features table to highlight dynamic environment injection This enables users to understand and implement multi-tenant deployments where different users need different credentials passed to MCP servers. Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
crivetimihai
approved these changes
Oct 4, 2025
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.
Overview
Implements dynamic environment variable injection in
mcpgateway.translate
to extract HTTP headers from incoming requests and inject them as environment variables when starting STDIO MCP processes. This enables multi-tenant support for enterprise MCP deployments.Closes #964
🎯 Problem Solved
The
mcpgateway.translate
module previously only supported static environment variables for STDIO MCP servers. This limitation prevented multi-tenant scenarios where different users need different tokens (e.g., GitHub Personal Access Tokens) passed to MCP servers.✨ Solution
Core Features
CLI Interface
🔧 Changes Made
New Files
mcpgateway/translate_header_utils.py
: Header processing utilities with security validationtests/unit/mcpgateway/test_translate_header_utils.py
: Unit tests for header utilitiestests/unit/mcpgateway/test_translate_stdio_endpoint.py
: Unit tests for StdIOEndpoint enhancementstests/integration/test_translate_dynamic_env.py
: Integration teststests/e2e/test_translate_dynamic_env_e2e.py
: End-to-end testsModified Files
mcpgateway/translate.py
: Enhanced StdIOEndpoint and FastAPI integration🔒 Security Features
🧪 Testing
Test Coverage
Test Results
📋 Manual Testing
Test 1: Basic Environment Injection
Test 2: Case-Insensitive Headers
Test 3: Partial Headers
🎯 Use Cases Enabled
GitHub Enterprise MCP
Multi-tenant GitHub Enterprise deployments where each user needs their own PAT:
Generic MCP Servers
Any STDIO MCP server requiring environment variables:
🔄 Backward Compatibility
📊 Performance Impact
🚀 Ready for Production
🔗 Related