Skip to content

Fix: Add Mcp-Session-Id header for HTTP backend initialization#178

Merged
lpcox merged 6 commits intomainfrom
copilot/fix-mcp-session-id-error
Jan 12, 2026
Merged

Fix: Add Mcp-Session-Id header for HTTP backend initialization#178
lpcox merged 6 commits intomainfrom
copilot/fix-mcp-session-id-error

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 12, 2026

Fix MCP Session ID Handling for HTTP Backend Servers ✅

Problem

HTTP backend MCP servers were rejecting requests with error "Missing Mcp-Session-Id header" during initialization, preventing the gateway from starting.

Root Cause

When the gateway initializes and calls tools/list on HTTP backend servers to discover available tools, it used context.Background() which has no session ID. HTTP backends that require the Mcp-Session-Id header in all requests would fail with a 400 Bad Request error.

Solution

  • Modified registerToolsFromBackend in unified.go to create a context with session ID for HTTP backends
  • Use pattern "gateway-init-{serverID}" for initialization calls to clearly identify gateway-initiated requests
  • Session ID is extracted from context and added as Mcp-Session-Id header when sending HTTP requests
  • Fully backward compatible - backends that don't require the header simply ignore it

Changes Made

  • Identified the issue in registerToolsFromBackend (unified.go:183)
  • Fixed initialization code to include session ID in context
  • Created comprehensive test suite for HTTP backend session ID handling
  • Tested with mock HTTP backend that requires the header
  • Added safeinputs-specific integration test (TestSafeinputsHTTPBackend)
  • Fixed linting errors (staticcheck QF1003)
  • Ran all unit and integration tests - all pass ✓
  • Formatted code with gofmt ✓
  • Ran linter - no issues ✓
  • Built successfully ✓
  • Added comprehensive documentation
  • Updated .gitignore for test files
  • Verified with make agent-finished - all checks pass ✓

Tests Added

  1. TestHTTPBackendInitialization - Verifies session ID is sent during initialization
  2. TestHTTPBackendInitializationWithSessionIDRequirement - Simulates exact error scenario (safeinputs server)
  3. TestHTTPBackend_SessionIDPropagation - Verifies session ID propagation through tool calls
  4. TestSafeinputsHTTPBackend - Integration test that simulates safeinputs HTTP backend behavior with strict session ID enforcement

Documentation

  • Added docs/HTTP_BACKEND_SESSION_ID.md with complete explanation:
    • Problem statement and solution
    • Implementation details
    • Session ID flow diagrams (initialization and client requests)
    • Testing approach and examples
    • Debugging tips and troubleshooting

Verification ✅

  • ✓ All unit tests pass (100%)
  • ✓ All integration tests pass (100%)
  • ✓ SafeinputsHTTPBackend integration test passes
  • ✓ Linting passes (no warnings)
  • ✓ Build succeeds
  • ✓ Code formatted with gofmt
  • ✓ Documentation complete and comprehensive

Backward Compatibility

This change is fully backward compatible:

  • HTTP backends that don't require the header will ignore it
  • HTTP backends that require the header now work correctly
  • Stdio backends are unaffected (no HTTP headers)

Impact

This fix enables the gateway to work with HTTP MCP servers that strictly enforce the Mcp-Session-Id header requirement, such as the safeinputs server mentioned in the issue.


💡 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.

Copilot AI and others added 3 commits January 12, 2026 02:00
- HTTP backends now receive Mcp-Session-Id header during initialization
- Use "gateway-init-{serverID}" as session ID for initialization calls
- Add comprehensive tests for HTTP backend session ID handling
- All existing tests continue to pass

Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
- Add comprehensive documentation on session ID handling
- Explain initialization and client request flows
- Include troubleshooting and debugging tips
- Update .gitignore to exclude test configuration files

Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix error connecting to backend mcp server Fix: Add Mcp-Session-Id header for HTTP backend initialization Jan 12, 2026
Copilot AI requested a review from lpcox January 12, 2026 02:06
@lpcox
Copy link
Copy Markdown
Collaborator

lpcox commented Jan 12, 2026

@copilot the safeinputs source is availble below. please use it to test your solution

https://github.com/githubnext/gh-aw/blob/main/actions/setup/js/safe_inputs_mcp_server.cjs

…D enforcement

- Create TestSafeinputsHTTPBackend that simulates the exact error scenario
- Mock HTTP server strictly enforces Mcp-Session-Id header requirement
- Verifies gateway sends session ID during initialization
- Test confirms fix resolves "Missing Mcp-Session-Id header" error
- Validates session ID pattern: gateway-init-{serverID}

Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jan 12, 2026

@copilot the safeinputs source is availble below. please use it to test your solution

https://github.com/githubnext/gh-aw/blob/main/actions/setup/js/safe_inputs_mcp_server.cjs

Created integration test TestSafeinputsHTTPBackend that simulates safeinputs HTTP backend behavior with strict Mcp-Session-Id header enforcement. The test:

  • Creates mock HTTP server that rejects requests without the header (returns 400 with "Missing Mcp-Session-Id header" error)
  • Verifies gateway sends session ID during initialization (gateway-init-safeinputs)
  • Confirms tool registration succeeds: Registered 1 tools from safeinputs: [safeinputs___safe_echo]

Test passes ✅ (commit bac00c5)

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:

  • https://api.github.com/repos/githubnext/gh-aw/contents/actions/setup/js
    • Triggering command: /usr/bin/curl curl -sSL REDACTED (http block)

If you need me to access, download, or install something from one of these locations, you can either:

@lpcox
Copy link
Copy Markdown
Collaborator

lpcox commented Jan 12, 2026

@copilot can you fix these linting errors

Running [/home/runner/golangci-lint-2.7.2-linux-amd64/golangci-lint run --timeout=5m] in [/home/runner/work/gh-aw-mcpg/gh-aw-mcpg] ...
Error: internal/server/unified_http_backend_test.go:188:3: QF1003: could use tagged switch on req.Method (staticcheck)
if req.Method == "tools/list" {
^
1 issues:

  • staticcheck: 1

Error: issues found
Ran golangci-lint in 2031ms

- Replace if-else chain with switch statement on req.Method
- Addresses staticcheck linting error QF1003 in unified_http_backend_test.go

Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jan 12, 2026

@copilot can you fix these linting errors

Running [/home/runner/golangci-lint-2.7.2-linux-amd64/golangci-lint run --timeout=5m] in [/home/runner/work/gh-aw-mcpg/gh-aw-mcpg] ...
...

Fixed staticcheck QF1003 linting error by replacing if-else chain with tagged switch statement on req.Method. Linting now passes ✅ (commit a6cc8d1)

@lpcox lpcox marked this pull request as ready for review January 12, 2026 02:38
@lpcox lpcox merged commit 7e8f860 into main Jan 12, 2026
3 checks passed
@lpcox lpcox deleted the copilot/fix-mcp-session-id-error branch January 12, 2026 02:38
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.

2 participants