mcpproxy-go icon indicating copy to clipboard operation
mcpproxy-go copied to clipboard

[Bug]: OAuth token exchange fails with 'Time.UnmarshalJSON: input is not a JSON string' when provider returns numeric expires_at

Open j0bro opened this issue 3 weeks ago • 1 comments

What happened?

When completing the OAuth 2.0 authorization code exchange against an OAuth provider whose token endpoint includes a numeric "expires_at" field (Unix epoch timestamp) or null in the token response alongside "expires_in", MCPProxy fails the token exchange with:

OAuth token exchange failed for <server>: failed to decode token response: Time.UnmarshalJSON: input is not a JSON string

Root cause

  1. github.com/mark3labs/mcp-go (v1.0.0, client/transport/oauth.go:78-91) defines transport.Token with:
    type Token struct {
        AccessToken  string    `json:"access_token"`
        TokenType    string    `json:"token_type"`
        RefreshToken string    `json:"refresh_token,omitempty"`
        ExpiresIn    int64     `json:"expires_in,omitempty"`
        Scope        string    `json:"scope,omitempty"`
        ExpiresAt    time.Time `json:"expires_at,omitzero"`
    }
    
  2. Both ProcessAuthorizationResponse (oauth.go:1153-1161) and refreshToken (oauth.go:311-319) call json.Unmarshal(body, &tokenResp) directly into Token before overwriting ExpiresAt from ExpiresIn:
    var tokenResp Token
    if err := json.Unmarshal(body, &tokenResp); err != nil {
        return fmt.Errorf("failed to decode token response: %w", err)
    }
    if tokenResp.ExpiresIn > 0 {
        tokenResp.ExpiresAt = time.Now().Add(time.Duration(tokenResp.ExpiresIn) * time.Second)
    }
    
  3. RFC 6749 §5.1 specifies expires_in (seconds), and several OAuth/OIDC servers (e.g. Better Auth OAuth2 endpoints) also include "expires_at" as a numeric Unix timestamp in the JSON response. Because time.Time.UnmarshalJSON requires a quoted RFC 3339 string, decoding fails immediately with Time.UnmarshalJSON: input is not a JSON string.

Suggested fix

OAuthTransportWrapper.RoundTrip (internal/oauth/transport_wrapper.go) already intercepts w.matchesToken(req) responses to normalize HTTP 201 Created → 200 OK for mcp-go. Normalizing the JSON token response body there—by stripping "expires_at" when it is not an RFC 3339 string (and optionally deriving "expires_in" from a numeric "expires_at" if "expires_in" is missing)—would fix both initial token exchange and token refresh without waiting on an upstream mcp-go change.

Expected behavior

OAuth token exchange and token refresh succeed when the authorization server returns a valid OAuth 2.0 token response containing a numeric or non-RFC3339 "expires_at" field.

Steps to reproduce

  1. Configure an HTTP MCP upstream server in mcpproxy that uses an OAuth 2.0 token endpoint returning a numeric "expires_at" field in its JSON token response (e.g., {"access_token":"...","token_type":"Bearer","expires_in":3600,"expires_at":1794307530}).
  2. Run mcpproxy auth login --server=<server> (or trigger sign-in from the tray/Web UI) and complete the browser consent redirect.
  3. Observe that the callback succeeds, ProcessAuthorizationResponse exchanges the code, and json.Unmarshal fails with failed to decode token response: Time.UnmarshalJSON: input is not a JSON string.

MCPProxy version

v0.73.0 (personal) darwin/arm64

Operating system

macOS

Relevant logs

INFO  | oauth.oauth-callback | ✅ OAuth callback parameters delivered to the waiting flow | {"server": "<redacted>", "bind_host": "127.0.0.1", "port": 62379, "path": "/oauth/callback"}
ERROR | core/connection_oauth.go:2030 | ❌ Failed to exchange authorization code | {"server": "<redacted>", "error": "failed to decode token response: Time.UnmarshalJSON: input is not a JSON string"}
WARN  | oauth/config.go:388 | Recording OAuth failure on server status | {"server": "<redacted>", "error": "OAuth token exchange failed for <redacted>: failed to decode token response: Time.UnmarshalJSON: input is not a JSON string"}

Pre-submission checklist

  • [x] I searched existing issues and did not find a duplicate
  • [x] I ran mcpproxy doctor and reviewed the output

j0bro avatar Nov 10 '26 09:11 j0bro

Thanks for the precise report and root cause, @j0bro. Fix is up in #1880: the OAuth transport wrapper now normalizes token responses before mcp-go decodes them, dropping any expires_at that is not an RFC 3339 string (numeric epoch, null, all-digit strings) and deriving expires_in from a numeric epoch when the provider omits it. It covers both the code exchange and the refresh, since both go through the same client. It will land in the next release.

Dumbris avatar Nov 11 '26 06:11 Dumbris