Fix Docker HTTP Stream transport connection issues by adding MCP_HOST support#7
Conversation
Co-authored-by: ZephyrDeng <56784262+ZephyrDeng@users.noreply.github.com>
- Add MCP_HOST environment variable to configure server binding address - Default to 0.0.0.0 for httpStream transport to allow external access - Default to localhost for stdio transport (backward compatibility) - Update .env.example with MCP_HOST documentation - Add comprehensive tests for host configuration - Fix jest.resetModules issue in transport tests Co-authored-by: ZephyrDeng <56784262+ZephyrDeng@users.noreply.github.com>
- Add Docker Compose example with MCP_HOST=0.0.0.0 - Document MCP_HOST variable in environment configuration - Add Docker deployment guidance for external access - Include comprehensive Docker configuration examples Co-authored-by: ZephyrDeng <56784262+ZephyrDeng@users.noreply.github.com>
There was a problem hiding this comment.
Pull Request Overview
This PR fixes Docker HTTP Stream transport connection issues by adding MCP_HOST environment variable support. The server was previously binding to localhost only, making it inaccessible from outside Docker containers.
Key changes:
- Added
MCP_HOSTenvironment variable with smart defaults (0.0.0.0 for httpStream, localhost for stdio) - Enhanced server configuration to support host binding in HTTP Stream mode
- Added comprehensive documentation and examples for Docker deployments
Reviewed Changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/index.ts | Added MCP_HOST environment variable parsing and host configuration for FastMCP server |
| src/tests/transport.test.ts | Added test coverage for host configuration scenarios and transport-specific defaults |
| README.md | Enhanced documentation with Docker deployment examples and MCP_HOST usage guidance |
| .env.example | Added MCP_HOST configuration with Chinese comments explaining Docker usage |
|
|
||
| const port = parseInt(process.env.MCP_PORT || '3000'); | ||
| const endpoint = process.env.MCP_ENDPOINT || '/mcp'; | ||
| const transportType: string = 'httpStream'; |
There was a problem hiding this comment.
The transport type logic is duplicated across multiple test cases. Consider extracting this into a helper function or testing the actual implementation from src/index.ts to avoid duplicating the host resolution logic in tests.
|
|
||
| const port = parseInt(process.env.MCP_PORT || '3000'); | ||
| const endpoint = process.env.MCP_ENDPOINT || '/mcp'; | ||
| const transportType: string = 'httpStream'; |
There was a problem hiding this comment.
The transport type logic is duplicated across multiple test cases. Consider extracting this into a helper function or testing the actual implementation from src/index.ts to avoid duplicating the host resolution logic in tests.
|
|
||
| it('should default to 0.0.0.0 for httpStream transport', () => { | ||
| delete process.env.MCP_HOST; | ||
| const transportType: string = 'httpStream'; |
There was a problem hiding this comment.
The transport type logic is duplicated across multiple test cases. Consider extracting this into a helper function or testing the actual implementation from src/index.ts to avoid duplicating the host resolution logic in tests.
|
|
||
| it('should default to localhost for stdio transport', () => { | ||
| delete process.env.MCP_HOST; | ||
| const transportType: string = 'stdio'; |
There was a problem hiding this comment.
The transport type logic is duplicated across multiple test cases. Consider extracting this into a helper function or testing the actual implementation from src/index.ts to avoid duplicating the host resolution logic in tests.
Problem
When deploying the GitLab MCP server in Docker containers using HTTP Stream transport, external connections were failing with "Connection refused" errors. The issue occurred because the FastMCP server was binding to
localhost(127.0.0.1) by default, making it inaccessible from outside the container.Root Cause
The original Docker deployment scenario:
Would start the server successfully:
But connections from the host machine to
http://localhost:5690/mcpwould fail because the server was only listening on the container's localhost interface, not on all interfaces.Solution
Added support for the
MCP_HOSTenvironment variable to configure the server binding address:0.0.0.0(all interfaces) for Docker compatibilitylocalhost(preserves existing behavior)Changes
Enhanced server configuration in
src/index.ts:MCP_HOSTenvironment variable parsingUpdated documentation:
MCP_HOSTto.env.examplewith usage guidanceComprehensive testing:
Usage
Docker Deployment (Automatic)
Docker Deployment (Explicit)
Local Development
Verification
The server now properly binds to all interfaces in Docker environments:
And external connections work correctly:
http://localhost:5690/mcp- accessible from hosthttp://127.0.0.1:5690/mcp- accessible from hostFixes #6.
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
gitlab.example.combun test(dns block)If you need me to access, download, or install something from one of these locations, you can either:
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.