modelcontextprotocol / modelcontextprotocol/php-sdk

[Client] Inject Bearer token into HttpTransport requests + handle 401 re-auth

Open
#316 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

auth Client enhancement improves spec compliance
Dominant language
PHP
Stars
1.6k
Forks
173
Avg merge
2d 49m
Merged PRs (30d)
23

Description

Context

Once an access token is acquired (via any OAuth grant), every outbound JSON-RPC request on HttpTransport must carry Authorization: Bearer <token>. On a 401 Unauthorized mid-session, the transport must trigger a re-auth attempt and retry the failed request exactly once.

Scope

  • src/Client/Transport/HttpTransport.php:
    • Read current token from TokenStorageInterface keyed by endpoint URL.
    • Add Authorization: Bearer <token> header on send() when a token is present.
    • On 401, parse WWW-Authenticate header, invoke auth coordinator, retry once.
  • Introduce Mcp\Client\Auth\AuthCoordinator to orchestrate discovery → registration → token acquisition. (Implementation details land in dependent issues.)

Conformance scenarios unblocked

All auth/* scenarios depend on this wiring.

Dependencies

Blocked by: #315 (TokenStorageInterface).

Acceptance

  • Unit tests for header injection + 401 retry behavior.
  • Documented extension point for AuthCoordinator.

cc @soyuka

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/Client/Transport/HttpTransport.php and review the TokenStorageInterface dependency from #315. Trace send() and the authentication coordinator entry point, then add unit coverage for Bearer header injection and one-time 401 re-auth retry. Done means requests use the current endpoint token, 401 responses trigger one retry, and the AuthCoordinator extension point is documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
api, authentication, backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.