oauth2
OAuth2 Authentication implementation for HTTPX.
Implements authorization code flow with PKCE and automatic token refresh.
PKCEParameters
Bases: BaseModel
PKCE (Proof Key for Code Exchange) parameters.
Source code in src/mcp/client/auth/oauth2.py
56 57 58 59 60 61 62 63 64 65 66 67 68 | |
generate
classmethod
generate() -> PKCEParameters
Generate new PKCE parameters.
Source code in src/mcp/client/auth/oauth2.py
62 63 64 65 66 67 68 | |
TokenStorage
Bases: Protocol
Protocol for token storage implementations.
Source code in src/mcp/client/auth/oauth2.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
get_tokens
async
get_tokens() -> OAuthToken | None
Get stored tokens.
Source code in src/mcp/client/auth/oauth2.py
74 75 76 | |
set_tokens
async
set_tokens(tokens: OAuthToken) -> None
Store tokens.
Source code in src/mcp/client/auth/oauth2.py
78 79 80 | |
get_client_info
async
get_client_info() -> OAuthClientInformationFull | None
Get stored client information.
Source code in src/mcp/client/auth/oauth2.py
82 83 84 | |
set_client_info
async
set_client_info(
client_info: OAuthClientInformationFull,
) -> None
Store client information.
Source code in src/mcp/client/auth/oauth2.py
86 87 88 | |
OAuthContext
dataclass
OAuth flow context.
Source code in src/mcp/client/auth/oauth2.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | |
get_authorization_base_url
Extract base URL by removing path component.
Source code in src/mcp/client/auth/oauth2.py
119 120 121 122 | |
update_token_expiry
update_token_expiry(token: OAuthToken) -> None
Update token expiry time using shared util function.
Source code in src/mcp/client/auth/oauth2.py
124 125 126 | |
is_token_valid
is_token_valid() -> bool
Check if current token is valid.
Source code in src/mcp/client/auth/oauth2.py
128 129 130 131 132 133 134 | |
can_refresh_token
can_refresh_token() -> bool
Check if token can be refreshed.
Source code in src/mcp/client/auth/oauth2.py
136 137 138 | |
clear_tokens
clear_tokens() -> None
Clear current tokens.
Source code in src/mcp/client/auth/oauth2.py
140 141 142 143 | |
get_resource_url
get_resource_url() -> str
Get resource URL for RFC 8707.
Uses PRM resource if it's a valid parent, otherwise uses canonical server URL.
Source code in src/mcp/client/auth/oauth2.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
should_include_resource_param
Determine if the resource parameter should be included in OAuth requests.
Returns True if: - Protected resource metadata is available, OR - MCP-Protocol-Version header is 2025-06-18 or later
Source code in src/mcp/client/auth/oauth2.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | |
prepare_token_auth
prepare_token_auth(
data: dict[str, str],
headers: dict[str, str] | None = None,
) -> tuple[dict[str, str], dict[str, str]]
Prepare authentication for token requests.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, str]
|
The form data to send |
required |
headers
|
dict[str, str] | None
|
Optional headers dict to update |
None
|
Returns:
| Type | Description |
|---|---|
tuple[dict[str, str], dict[str, str]]
|
Tuple of (updated_data, updated_headers) |
Source code in src/mcp/client/auth/oauth2.py
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | |
OAuthClientProvider
Bases: Auth
OAuth2 authentication for httpx.
Handles OAuth flow with automatic client registration and token storage.
Source code in src/mcp/client/auth/oauth2.py
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 | |
__init__
__init__(
server_url: str,
client_metadata: OAuthClientMetadata,
storage: TokenStorage,
redirect_handler: (
Callable[[str], Awaitable[None]] | None
) = None,
callback_handler: (
Callable[[], Awaitable[tuple[str, str | None]]]
| None
) = None,
timeout: float = 300.0,
client_metadata_url: str | None = None,
validate_resource_url: (
Callable[[str, str | None], Awaitable[None]] | None
) = None,
)
Initialize OAuth2 authentication.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server_url
|
str
|
The MCP server URL. |
required |
client_metadata
|
OAuthClientMetadata
|
OAuth client metadata for registration. |
required |
storage
|
TokenStorage
|
Token storage implementation. |
required |
redirect_handler
|
Callable[[str], Awaitable[None]] | None
|
Handler for authorization redirects. |
None
|
callback_handler
|
Callable[[], Awaitable[tuple[str, str | None]]] | None
|
Handler for authorization callbacks. |
None
|
timeout
|
float
|
Timeout for the OAuth flow. |
300.0
|
client_metadata_url
|
str | None
|
URL-based client ID. When provided and the server advertises client_id_metadata_document_supported=True, this URL will be used as the client_id instead of performing dynamic client registration. Must be a valid HTTPS URL with a non-root pathname. |
None
|
validate_resource_url
|
Callable[[str, str | None], Awaitable[None]] | None
|
Optional callback to override resource URL validation. Called with (server_url, prm_resource) where prm_resource is the resource from Protected Resource Metadata (or None if not present). If not provided, default validation rejects mismatched resources per RFC 8707. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If client_metadata_url is provided but not a valid HTTPS URL with a non-root pathname. |
Source code in src/mcp/client/auth/oauth2.py
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | |
async_auth_flow
async
async_auth_flow(
request: Request,
) -> AsyncGenerator[Request, Response]
HTTPX auth flow integration.
Source code in src/mcp/client/auth/oauth2.py
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 | |