Skip to content

fix(portal): XJ-03 OAuth bridge derives redirect_uri with host 'localhost' instead of '127.0.0.1' #146

Closed

Description

@KooshaPari

Summary

The XJ-03 Spotify Portal OAuth bridge (portal-bridge MCP server) fails to
complete the OAuth authorization-code flow because the derived redirect_uri
uses the wrong host.

mcp-remote derives the loopback redirect URI from the host it is told to
bind. When the bridge is launched with localhost, the derived redirect URI is
http://localhost:<port>/oauth/callback. The Portal authorization server
requires the loopback host to be the literal address 127.0.0.1, and rejects
localhost outright.

Observed behavior (live, against the production AS)

GET http://127.0.0.1:47290/oauth/callback   -> HTTP 302   (accepted)
GET http://localhost:47290/oauth/callback   -> HTTP 400   (rejected)

Root cause

Two independent defects combined:

  1. Wrong redirect host. The bridge was configured to bind localhost,
    producing a redirect_uri whose host is localhost instead of
    127.0.0.1. The AS rejects it, so the authorization-code exchange never
    completes.

  2. Wrong argument order. The corrected mcp-remote invocation was not
    being used — the bridge config passed its flags in an order that made
    mcp-remote parse the host argument as something else, silently falling
    back to localhost.

Fix

Correct the portal-bridge entry so that it:

  • passes the argument order mcp-remote actually expects,
  • binds explicitly to 127.0.0.1,
  • keeps [email protected], http-only, and the production CIMD / resource,
  • uses timeout_secs: 120.

The canonical config lives at ~/.kcode/mcp.json; scripts/portal-mcp-bridge.json
in this repo mirrors the same vector.

Acceptance criteria

  • The bridge reaches the MCP server through the exact canonical config.
  • initialize returns the backstage server at the expected version.
  • tools/list returns the full tool inventory.
  • auth.who-am-i returns the operator identity.
  • The derived callback port matches the CIMD registration.
  • No consent gate is triggered on a warm cache (cached token is reused).
  • The regression test for the stub CIMD server asserts the corrected
    argument order rather than a condition that passes vacuously.

Additional finding: refresh-token rotation

The Portal authorization server rotates refresh tokens. A refresh returns a
new access/refresh pair and immediately invalidates the token it consumed:

  • refresh with RT-A -> HTTP 200
  • reuse RT-A -> HTTP 400 invalid_grant
  • refresh with RT-B -> HTTP 200

A manual refresh whose result is not persisted therefore leaves a dead refresh
token on disk, and every subsequent launch fails with invalid_grant.

To make this safe, scripts/refresh-portal-token.py refreshes and persists the
rotated token pair atomically (temp file + os.replace), sets mode 0600,
never prints token material, and supports --check for a read-only report.

References

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions