Skip to content

Conversation

@christso
Copy link
Contributor

@christso christso commented Dec 28, 2025

Summary

Some OAuth servers (e.g., GitHub) return error responses with HTTP 200 status instead of 4xx. This PR adds a check for the error field in the JSON response before attempting to parse it as tokens.

Problem

When GitHub's OAuth token endpoint returns an error like:

{
  "error": "incorrect_client_credentials",
  "error_description": "The client_id and/or client_secret passed are incorrect."
}

The SDK's executeTokenRequest() only checks response.ok (which is true for HTTP 200), then tries to parse the response as OAuthTokensSchema, resulting in a confusing Zod validation error:

[
  {"expected": "string", "path": ["access_token"], "message": "Invalid input: expected string, received undefined"},
  {"expected": "string", "path": ["token_type"], "message": "Invalid input: expected string, received undefined"}
]

Solution

Check for the error field in the JSON response before attempting to parse as tokens. This surfaces the actual OAuth error message to users:

The client_id and/or client_secret passed are incorrect.

Test plan

  • Verify existing OAuth tests pass
  • TypeScript type checking passes
  • Test with GitHub OAuth endpoint returning error with HTTP 200 (not required as we decided to pass responsibility to GitHub to fix their error code)
  • Test normal successful token exchange still works

Closes #1342

🤖 Generated with Claude Code

Some OAuth servers (e.g., GitHub) return error responses with HTTP 200
status instead of 4xx. The SDK now checks for an `error` field in the
JSON response before attempting to parse it as tokens.

This provides users with meaningful error messages like:
"The client_id and/or client_secret passed are incorrect."

Instead of confusing Zod validation errors about missing access_token.

Fixes modelcontextprotocol#1342

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@christso christso requested a review from a team as a code owner December 28, 2025 01:49
@changeset-bot
Copy link

changeset-bot bot commented Dec 28, 2025

🦋 Changeset detected

Latest commit: 4055465

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@modelcontextprotocol/client Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new
Copy link

pkg-pr-new bot commented Dec 28, 2025

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/client@1343

@modelcontextprotocol/server

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/server@1343

@modelcontextprotocol/express

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/express@1343

@modelcontextprotocol/hono

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/hono@1343

@modelcontextprotocol/node

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/node@1343

commit: 4055465

- Fix TypeScript error by properly typing json as unknown
- Add changeset for the patch release

Fixes modelcontextprotocol#1342

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@pcarleton
Copy link
Member

Thanks! One small suggestion - check for error only if token parsing fails, keeps the happy path clean. Pushing a commit with this approach.

@christso christso requested a review from pcarleton January 16, 2026 05:31
@christso
Copy link
Contributor Author

@pcarleton It's waiting for review for a while. Should we still merge this given it won't make any difference unless GitHub stops returning HTTP 200 for what should be an error?

@felixweinberger felixweinberger force-pushed the fix/oauth-error-response-handling branch from 59df40b to 9fe43c2 Compare January 26, 2026 14:08
Copy link
Contributor

@felixweinberger felixweinberger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this LGTM!
Pushed a small regression test for completeness on top of this branch.

@felixweinberger felixweinberger dismissed pcarleton’s stale review January 26, 2026 14:09

Dismissing review as comments have been addressed afaict

@felixweinberger felixweinberger force-pushed the fix/oauth-error-response-handling branch from 9fe43c2 to 1213c01 Compare January 26, 2026 15:05
Adds test case for issue modelcontextprotocol#1342 where OAuth servers like GitHub return
error responses with HTTP 200 instead of 4xx. Ensures the fix properly
surfaces the OAuth error message instead of a confusing Zod validation error.
@felixweinberger felixweinberger force-pushed the fix/oauth-error-response-handling branch from 1213c01 to d6c3f65 Compare January 26, 2026 15:09
@christso christso deleted the fix/oauth-error-response-handling branch January 26, 2026 21:25
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.

OAuth token exchange doesn't handle error responses returned as HTTP 200

3 participants