utils
extract_field_from_www_auth
Extract field from WWW-Authenticate header.
Returns:
| Type | Description |
|---|---|
str | None
|
Field value if found in WWW-Authenticate header, None otherwise |
Source code in src/mcp/client/auth/utils.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
extract_scope_from_www_auth
extract_scope_from_www_auth(
response: Response,
) -> str | None
Extract scope parameter from WWW-Authenticate header as per RFC 6750.
Returns:
| Type | Description |
|---|---|
str | None
|
Scope string if found in WWW-Authenticate header, None otherwise |
Source code in src/mcp/client/auth/utils.py
40 41 42 43 44 45 46 | |
extract_resource_metadata_from_www_auth
extract_resource_metadata_from_www_auth(
response: Response,
) -> str | None
Extract protected resource metadata URL from WWW-Authenticate header as per RFC 9728.
Returns:
| Type | Description |
|---|---|
str | None
|
Resource metadata URL if found in WWW-Authenticate header, None otherwise |
Source code in src/mcp/client/auth/utils.py
49 50 51 52 53 54 55 56 57 58 | |
build_protected_resource_metadata_discovery_urls
build_protected_resource_metadata_discovery_urls(
www_auth_url: str | None, server_url: str
) -> list[str]
Build ordered list of URLs to try for protected resource metadata discovery.
Per SEP-985, the client MUST: 1. Try resource_metadata from WWW-Authenticate header (if present) 2. Fall back to path-based well-known URI: /.well-known/oauth-protected-resource/{path} 3. Fall back to root-based well-known URI: /.well-known/oauth-protected-resource
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
www_auth_url
|
str | None
|
Optional resource_metadata URL extracted from the WWW-Authenticate header |
required |
server_url
|
str
|
Server URL |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
Ordered list of URLs to try for discovery |
Source code in src/mcp/client/auth/utils.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | |
get_client_metadata_scopes
get_client_metadata_scopes(
www_authenticate_scope: str | None,
protected_resource_metadata: (
ProtectedResourceMetadata | None
),
authorization_server_metadata: (
OAuthMetadata | None
) = None,
client_grant_types: list[str] | None = None,
) -> str | None
Select effective scopes and augment for refresh token support.
Source code in src/mcp/client/auth/utils.py
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 | |
build_oauth_authorization_server_metadata_discovery_urls
build_oauth_authorization_server_metadata_discovery_urls(
auth_server_url: str | None, server_url: str
) -> list[str]
Generate an ordered list of URLs for authorization server metadata discovery.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
auth_server_url
|
str | None
|
OAuth Authorization Server Metadata URL if found, otherwise None |
required |
server_url
|
str
|
URL for the MCP server, used as a fallback if auth_server_url is None |
required |
Source code in src/mcp/client/auth/utils.py
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 | |
handle_protected_resource_response
async
handle_protected_resource_response(
response: Response,
) -> ProtectedResourceMetadata | None
Handle protected resource metadata discovery response.
Per SEP-985, supports fallback when discovery fails at one URL.
Returns:
| Type | Description |
|---|---|
ProtectedResourceMetadata | None
|
ProtectedResourceMetadata if successfully discovered, None if we should try next URL |
Source code in src/mcp/client/auth/utils.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | |
create_client_registration_request
create_client_registration_request(
auth_server_metadata: OAuthMetadata | None,
client_metadata: OAuthClientMetadata,
auth_base_url: str,
) -> Request
Build a client registration request.
Source code in src/mcp/client/auth/utils.py
218 219 220 221 222 223 224 225 226 227 228 229 230 | |
handle_registration_response
async
handle_registration_response(
response: Response,
) -> OAuthClientInformationFull
Handle registration response.
Source code in src/mcp/client/auth/utils.py
233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
is_valid_client_metadata_url
Validate that a URL is suitable for use as a client_id (CIMD).
The URL must be HTTPS with a non-root pathname.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str | None
|
The URL to validate |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the URL is a valid HTTPS URL with a non-root pathname |
Source code in src/mcp/client/auth/utils.py
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | |
should_use_client_metadata_url
should_use_client_metadata_url(
oauth_metadata: OAuthMetadata | None,
client_metadata_url: str | None,
) -> bool
Determine if URL-based client ID (CIMD) should be used instead of DCR.
URL-based client IDs should be used when: 1. The server advertises client_id_metadata_document_supported=True 2. The client has a valid client_metadata_url configured
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
oauth_metadata
|
OAuthMetadata | None
|
OAuth authorization server metadata |
required |
client_metadata_url
|
str | None
|
URL-based client ID (already validated) |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if CIMD should be used, False if DCR should be used |
Source code in src/mcp/client/auth/utils.py
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | |
create_client_info_from_metadata_url
create_client_info_from_metadata_url(
client_metadata_url: str,
redirect_uris: list[AnyUrl] | None = None,
) -> OAuthClientInformationFull
Create client information using a URL-based client ID (CIMD).
When using URL-based client IDs, the URL itself becomes the client_id and no client_secret is used (token_endpoint_auth_method="none").
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client_metadata_url
|
str
|
The URL to use as the client_id |
required |
redirect_uris
|
list[AnyUrl] | None
|
The redirect URIs from the client metadata (passed through for compatibility with OAuthClientInformationFull which inherits from OAuthClientMetadata) |
None
|
Returns:
| Type | Description |
|---|---|
OAuthClientInformationFull
|
OAuthClientInformationFull with the URL as client_id |
Source code in src/mcp/client/auth/utils.py
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | |
handle_token_response_scopes
async
handle_token_response_scopes(
response: Response,
) -> OAuthToken
Parse and validate a token response.
Parses token response JSON. Callers should check response.status_code before calling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
Response
|
HTTP response from token endpoint (status already checked by caller) |
required |
Returns:
| Type | Description |
|---|---|
OAuthToken
|
Validated OAuthToken model |
Raises:
| Type | Description |
|---|---|
OAuthTokenError
|
If response JSON is invalid |
Source code in src/mcp/client/auth/utils.py
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 | |