Skip to content

Commit b609fc5

Browse files
feat: add initial access token support for OAuth 2.0 Dynamic Client Registration (RFC 7591)
- Add initial_access_token parameter to OAuthClientProvider constructor - Implement multi-level fallback for token resolution: 1. Explicit parameter (highest priority) 2. Provider method (initial_access_token()) 3. Environment variable (OAUTH_INITIAL_ACCESS_TOKEN) 4. No token (existing behavior) - Add Authorization Bearer header to registration requests when token available - Add comprehensive test coverage for all fallback scenarios - Update documentation with usage examples and configuration details - Maintain full backward compatibility with existing OAuth flows This enables clients to register with protected OAuth endpoints that require initial access tokens per RFC 7591 Dynamic Client Registration specification.
1 parent 6566c08 commit b609fc5

File tree

5 files changed

+777
-5
lines changed

5 files changed

+777
-5
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,6 +1452,8 @@ async def main():
14521452
storage=CustomTokenStorage(),
14531453
redirect_handler=lambda url: print(f"Visit: {url}"),
14541454
callback_handler=lambda: ("auth_code", None),
1455+
# Optional: Initial access token for RFC 7591 Dynamic Client Registration
1456+
initial_access_token="your-initial-access-token",
14551457
)
14561458

14571459
# Use with streamable HTTP client
@@ -1465,6 +1467,41 @@ async def main():
14651467

14661468
For a complete working example, see [`examples/clients/simple-auth-client/`](examples/clients/simple-auth-client/).
14671469

1470+
#### Initial Access Tokens
1471+
1472+
The SDK supports RFC 7591 Dynamic Client Registration with initial access tokens. This feature provides a multi-level fallback system for obtaining initial access tokens:
1473+
1474+
```python
1475+
# Method 1: Explicit parameter (highest priority)
1476+
oauth_auth = OAuthClientProvider(
1477+
server_url="https://api.example.com",
1478+
client_metadata=client_metadata,
1479+
storage=storage,
1480+
redirect_handler=redirect_handler,
1481+
callback_handler=callback_handler,
1482+
initial_access_token="your-token",
1483+
)
1484+
1485+
# Method 2: Provider method override
1486+
class CustomOAuthProvider(OAuthClientProvider):
1487+
async def initial_access_token(self) -> str | None:
1488+
# Custom logic to retrieve token
1489+
return await get_token_from_secure_store()
1490+
1491+
# Method 3: Environment variable fallback
1492+
# Set OAUTH_INITIAL_ACCESS_TOKEN environment variable
1493+
# The SDK will automatically use this if no other method provides a token
1494+
1495+
# Method 4: No token (default behavior)
1496+
# Client registration will proceed without initial access token
1497+
```
1498+
1499+
The fallback order is:
1500+
1. Explicit `initial_access_token` parameter
1501+
2. Provider's `initial_access_token()` method
1502+
3. `OAUTH_INITIAL_ACCESS_TOKEN` environment variable
1503+
4. No token (proceeds with standard registration)
1504+
14681505
### MCP Primitives
14691506

14701507
The MCP protocol defines three core primitives that servers can implement:

0 commit comments

Comments
 (0)