Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 16, 2026

Fix HTTP Backend Error Propagation ✅

When an HTTP backend MCP server returns an error, the error is now properly propagated to the client through the gateway.

Problem Summary

HTTP backend errors (4xx, 5xx) were converted to generic "Internal error" messages, losing all original error details (status code, error body), making debugging difficult for clients.

Solution

Modified the HTTP request handling to:

  1. Return JSON-RPC error responses instead of Go errors when backends return non-200 status codes
  2. Preserve HTTP status code and error body in the JSON-RPC error response
  3. Check for error field in responses before attempting to unmarshal results

Files Changed

  • internal/mcp/connection.go: Modified sendHTTPRequest to convert HTTP errors to JSON-RPC error responses
  • internal/server/unified.go: Added error field checks before unmarshaling response results
  • test/integration/http_error_test.go: Updated test to verify error propagation in response field
  • internal/mcp/http_error_propagation_test.go: New comprehensive unit tests (4 test functions, 13+ test cases)
  • internal/server/http_error_propagation_test.go: New integration tests (4 test functions)

Test Coverage ✅

Unit Tests (internal/mcp/http_error_propagation_test.go):

  • ✅ Non-200 status codes (400, 401, 403, 404, 500, 502, 503)
  • ✅ JSON-RPC error responses with HTTP 200
  • ✅ Mixed content types (plain text, HTML, JSON)
  • ✅ Error detail preservation

Integration Tests (internal/server/http_error_propagation_test.go):

  • ✅ Unified server error propagation
  • ✅ Error field checking before unmarshal
  • ✅ Legacy proxy error forwarding
  • ✅ Complex error data preservation

Existing Integration Tests (all passing):

  • ✅ HTTP 500/503 server errors
  • ✅ HTTP 401/403/404 client errors
  • ✅ Connection errors (refused, dropped)
  • ✅ Malformed responses
  • ✅ Network partitions
  • ✅ Request-time failures
  • ✅ Intermittent failures with recovery

Code Quality ✅

  • ✅ make lint: passes
  • ✅ gofmt: all files properly formatted
  • ✅ golangci-lint: 0 issues
  • ✅ All tests passing

Example Impact

Before:

{"jsonrpc":"2.0","id":1,"error":{"code":-32603,"message":"Internal error"}}

After:

{
  "jsonrpc": "2.0",
  "id": 1,
  "error": {
    "code": -32603,
    "message": "HTTP 503: Service Unavailable",
    "data": "{\"error\":\"Backend database is down\"}"
  }
}

Notes

  • Pre-existing test failure in internal/sys/sys_test.go is unrelated to these changes
  • No breaking changes - existing clients continue to work (error is in standard JSON-RPC error field)
  • Both connection-time and request-time errors handled uniformly
Original prompt

This section details on the original issue you should resolve

<issue_title>HTTP error not propagated</issue_title>
<issue_description>See logs https://github.com/githubnext/gh-aw/actions/runs/20901262632/job/60047505268#step:43:1</issue_description>

<agent_instructions>When an http backend mcp server returns an error, the error needs to be propagated to the client through the gateway.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


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

- Modified sendHTTPRequest to return JSON-RPC error responses instead of Go errors when HTTP backends return non-200 status codes
- HTTP errors (4xx, 5xx) are now properly converted to JSON-RPC error responses with status code and error body preserved
- Updated unified server to check for error field in responses before attempting to unmarshal result
- Updated integration test to verify error is propagated in response field rather than as Go error
- Fixes issue where HTTP backend errors were converted to generic "Internal error" messages

Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix HTTP error propagation from backend to client Propagate HTTP backend errors to clients with full context Jan 16, 2026
Copilot AI requested a review from lpcox January 16, 2026 06:32
@lpcox
Copy link
Collaborator

lpcox commented Jan 16, 2026

@copilot please create new unit and integration tests for http error propagation. also please be sure to run golangci-lint at the end.

Created new test files:
- internal/mcp/http_error_propagation_test.go: 4 test functions covering non-200 status codes, JSON-RPC errors, mixed content types, and error detail preservation
- internal/server/http_error_propagation_test.go: 4 test functions covering unified server error propagation, error field checking, proxy forwarding, and data preservation

All tests pass and golangci-lint reports 0 issues.

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

Copilot AI commented Jan 16, 2026

@copilot please create new unit and integration tests for http error propagation. also please be sure to run golangci-lint at the end.

Done! Created comprehensive tests and ran golangci-lint:

New Test Files:

  • internal/mcp/http_error_propagation_test.go (4 test functions, 13+ scenarios)
  • internal/server/http_error_propagation_test.go (4 test functions)

Test Coverage:

  • Non-200 status codes (400, 401, 403, 404, 500, 502, 503)
  • JSON-RPC errors with HTTP 200
  • Mixed content types (text, HTML, JSON)
  • Error detail preservation
  • Unified server integration
  • Legacy proxy forwarding

Results:
✅ All tests pass
✅ golangci-lint: 0 issues

Commit: 9dc1bc8

@lpcox
Copy link
Collaborator

lpcox commented Jan 16, 2026

@copilot can you please fix this error

make lint
shell: /usr/bin/bash -e {0}
env:
GOTOOLCHAIN: local
Running linters...
Running gofmt check...
The following files are not formatted:
internal/mcp/http_error_propagation_test.go
internal/server/http_error_propagation_test.go
make: *** [Makefile:26: lint] Error 1

Run gofmt to fix formatting in:
- internal/mcp/http_error_propagation_test.go
- internal/server/http_error_propagation_test.go

make lint now passes successfully.

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

Copilot AI commented Jan 16, 2026

@copilot can you please fix this error

make lint
...

Fixed! Ran gofmt on both test files to correct formatting issues.

make lint now passes successfully

Commit: cfb4d23

@lpcox lpcox marked this pull request as ready for review January 16, 2026 06:46
@lpcox lpcox merged commit 87ced49 into main Jan 16, 2026
3 checks passed
@lpcox lpcox deleted the copilot/propagate-http-error-to-client branch January 16, 2026 06:46
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.

HTTP error not propagated

2 participants