Skip to content

fix(http-client): throw on non-2xx responses so MCP tools surface errors correctly#70

Open
fuleinist wants to merge 1 commit into
21st-dev:mainfrom
fuleinist:fix/http-client-check-status
Open

fix(http-client): throw on non-2xx responses so MCP tools surface errors correctly#70
fuleinist wants to merge 1 commit into
21st-dev:mainfrom
fuleinist:fix/http-client-check-status

Conversation

@fuleinist

Copy link
Copy Markdown

Summary

Fixes #66

Before: response.ok was never checked, so server errors (500, 503, etc.) returned as successful tool results with the error text as the output. Example: /api/refine-ui returning "Anthropic experiencing high load" as HTTP 200 would appear as a successful refinement to the MCP client — the client could not distinguish an outage from a real refinement.

After: createMethod now throws an Error with the HTTP status and server error body when response.ok is false. MCP clients receive a proper error they can surface to the user.

Changes

  • src/utils/http-client.ts — after fetch(), check response.ok. If false, read the response body as text and throw Error(HTTP {status} {statusText}: {errorText}). If the body can't be read, fall back to just the status text.
  • src/utils/http-client.test.ts — 3 new regression tests covering:
    • 500 with JSON error body → full error message in thrown Error
    • 503 with no body → status-only thrown Error
    • 401 with JSON error body → full error message in thrown Error

Test results

PASS src/utils/http-client.test.ts
  http-client
    √ should use production URL in production environment (1 ms)
  http-client error handling
    √ throws on non-2xx response with status and error message (25 ms)
    √ throws on non-2xx response when response has no body (2 ms)
    √ throws on 401 with the error body from server (2 ms)
Tests: 4 passed, 4 total

…ors correctly

Fixes 21st-dev#66

Before: response.ok was never checked, so server errors (500, 503, etc.)
returned as successful tool results with the error text as the output.
Example: /api/refine-ui returning 'Anthropic experiencing high load' as
HTTP 200 would appear as a successful refinement to the MCP client.

After: createMethod now throws an Error with the HTTP status and server
error body when response.ok is false. MCP clients receive a proper error
they can surface to the user.

Added 3 regression tests covering:
- 500 with JSON error body
- 503 with no body
- 401 with JSON error body
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.

refine-ui always returns "Anthropic experiencing high load" — instant canned 200, persisting 2+ days (not load)

1 participant