auth_utils
Utilities for OAuth 2.0 Resource Indicators (RFC 8707) and PKCE (RFC 7636).
resource_url_from_server_url
Convert server URL to canonical resource URL per RFC 8707.
RFC 8707 section 2 states that resource URIs "MUST NOT include a fragment component". Returns absolute URI with lowercase scheme/host for canonical form.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str | HttpUrl | AnyUrl
|
Server URL to convert |
required |
Returns:
| Type | Description |
|---|---|
str
|
Canonical resource URL string |
Source code in src/mcp/shared/auth_utils.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |
check_resource_allowed
Check if a requested resource URL matches a configured resource URL.
A requested resource matches if it has the same scheme, domain, port, and its path starts with the configured resource's path. This allows hierarchical matching where a token for a parent resource can be used for child resources.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
requested_resource
|
str
|
The resource URL being requested |
required |
configured_resource
|
str
|
The resource URL that has been configured |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the requested resource matches the configured resource |
Source code in src/mcp/shared/auth_utils.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | |
calculate_token_expiry
Calculate token expiry timestamp from expires_in seconds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expires_in
|
int | str | None
|
Seconds until token expiration (may be string from some servers) |
required |
Returns:
| Type | Description |
|---|---|
float | None
|
Unix timestamp when token expires, or None if no expiry specified |
Source code in src/mcp/shared/auth_utils.py
68 69 70 71 72 73 74 75 76 77 78 79 80 | |