You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/_client/authorization.md
+41-4Lines changed: 41 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -97,6 +97,8 @@ Optional keyword arguments:
97
97
-`scope`: Space-separated scopes to request when the server's `WWW-Authenticate` does not specify one.
98
98
-`authorization_request_validator`: Callable invoked with an `MCP::Client::OAuth::AuthorizationRequest` before any authorization request is built.
99
99
Returning a falsy value abandons the flow with `Flow::AuthorizationRefusedError`. See [Reviewing the authorization request](#reviewing-the-authorization-request).
100
+
-`http_client_customizer`: Callable invoked with the Faraday connection the SDK builds for the OAuth flow's own requests, after its defaults and before its origin guard.
101
+
See [Customizing the OAuth HTTP Client](#customizing-the-oauth-http-client).
100
102
-`storage`: Object responding to `tokens`, `save_tokens(t)`, `client_information`, `save_client_information(info)`. Defaults to `MCP::Client::OAuth::InMemoryStorage`,
101
103
which keeps credentials in process memory only. Persisted `client_information` is stamped with an `"issuer"` member binding it to the authorization server that
102
104
issued it (SEP-2352): when the server's authorization server changes, the SDK discards the stale registration and its tokens and re-registers automatically
@@ -219,7 +221,7 @@ Keyword arguments:
219
221
-`private_key`, `signing_algorithm`: Required with `private_key_jwt` - the key (a PEM string
220
222
or `OpenSSL::PKey::PKey`, never written to `storage`) signs the client assertion with `"ES256"`
221
223
or `"RS256"`; `client_secret` must not be set, because the private key is the credential.
222
-
-`scope`, `storage`, `authorization_request_validator`, `token_request_params`: Optional, same meaning as on `Provider`.
224
+
-`scope`, `storage`, `authorization_request_validator`, `token_request_params`, `http_client_customizer`: Optional, same meaning as on `Provider`.
223
225
Use `token_request_params` for a parameter the authorization server requires on the `client_credentials` grant, such as Auth0's `audience`.
224
226
225
227
### Cross-App Access (JWT Bearer) Grant
@@ -258,7 +260,39 @@ Keyword arguments:
258
260
-`assertion_provider`: Required. Callable invoked as `call(audience:, resource:)` and returning the ID-JAG assertion.
259
261
`audience` is the MCP authorization server's validated issuer identifier; `resource` is the canonical MCP server URL (RFC 8707).
260
262
Passing both through to `IDJAGTokenExchange.request` covers the common case.
261
-
-`scope`, `storage`, `authorization_request_validator`, `token_request_params`: Optional, same meaning as on `Provider`.
263
+
-`scope`, `storage`, `authorization_request_validator`, `token_request_params`, `http_client_customizer`: Optional, same meaning as on `Provider`.
264
+
265
+
### Customizing the OAuth HTTP Client
266
+
267
+
The requests the OAuth flow makes (Protected Resource Metadata discovery on the MCP server's origin, authorization server metadata discovery,
268
+
dynamic client registration, and every token request the flow sends, whether the first exchange, a refresh, or a step-up) go over a Faraday connection of their own,
269
+
not over the transport's connection: the transport's is bound to the MCP server URL and carries the `headers:` and the customizer block meant for that server.
270
+
To add middleware to the OAuth flow's connection, or to swap its adapter, pass `http_client_customizer:` to the provider:
The callable receives the `Faraday::Connection` after the SDK has applied its defaults and registered the middleware that records the requested URL,
281
+
and before the SDK registers its origin guard, the same position the transport's customizer block has on the MCP server connection.
282
+
It may be invoked more than once, and from more than one thread at a time, so keep it free of side effects and safe to run concurrently;
283
+
today it runs once per authorization attempt, but that is not a promise.
284
+
A few constraints follow from the checks described below:
285
+
286
+
- Do not add redirect-following middleware. Every destination check runs against the URL as written, so a request that middleware added by the customizer would send
287
+
to a different origin after the SDK has recorded the requested URL, whether by following a `3xx` or by rewriting the URL, is refused with `Flow::DestinationMismatchError`
288
+
before it reaches the adapter, as is a request that reaches the guard without that record. Middleware inserted ahead of the record with `builder.insert(0, ...)`
289
+
that rewrites the URL first or rebuilds the environment is outside the guard, as is following done inside an adapter, so leave both off.
290
+
- Leave `Accept-Encoding` unset. The response cap below is measured on decoded bytes, and claiming the header turns Net::HTTP's decoding off.
291
+
- Do not add Faraday's `raise_error` middleware. The flow reads statuses itself, both to tell an absent metadata document from a failed request
292
+
and to turn a token endpoint error into `Flow::InvalidGrantError`.
293
+
- With an adapter that does not stream through `on_data`, the response cap is applied once the body has been buffered rather than as it arrives.
294
+
- A middleware that records requests sees the client credentials on token requests (`Authorization: Basic`, `client_secret`, `client_assertion`), refresh tokens,
295
+
and the access tokens in token responses; redact them before they reach a log.
262
296
263
297
### Communication Security
264
298
@@ -290,8 +324,11 @@ The range check compares IP literals and does not resolve hostnames, so it canno
290
324
such as `https://vault.corp.internal/`. Resolving names here would not close that gap either, because the address the SDK looked up need not be the one
291
325
the HTTP client connects to a moment later. The same-origin rule is what protects the `resource_metadata` URL, which is the only one of these a server supplies directly.
292
326
293
-
If you replace the OAuth HTTP client through `MCP::Client::OAuth::Flow.new(http_client_factory:)`, do not add redirect-following middleware. Every check above runs against
294
-
the URL as written, so a connection that follows a `3xx` on its own would reach hosts these rules just refused.
327
+
On the connection the SDK builds, a middleware that would send a request to a different origin, by following a `3xx` or by rewriting the URL, is refused before the request
328
+
goes out (see [Customizing the OAuth HTTP Client](#customizing-the-oauth-http-client)). A connection supplied through `MCP::Client::OAuth::Flow.new(http_client_factory:)`
329
+
replaces that one, the provider's `http_client_customizer` and the guard included, so do not add redirect-following middleware to it: every check above runs against
330
+
the URL as written, and a connection that follows a `3xx` on its own would reach hosts these rules just refused.
331
+
A factory that wants to keep them can return `MCP::Client::OAuth::Flow.build_http_client(customizer)`, the connection the SDK builds for itself.
295
332
296
333
The SDK also bounds what those endpoints may return. A discovery, dynamic client registration, token, or token exchange response is refused once it passes 4 MiB,
297
334
measured as the body arrives rather than after it has been buffered, so a compressed body that expands past the limit is refused partway through the expansion.
Copy file name to clipboardExpand all lines: docs/_client/transports.md
+3Lines changed: 3 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -248,6 +248,9 @@ http_transport = MCP::Client::HTTP.new(url: "https://api.example.com/mcp") do |f
248
248
end
249
249
```
250
250
251
+
The block customizes only the connection to the MCP server. The connection the OAuth flow uses for its own requests is customized through
252
+
the provider's `http_client_customizer:` keyword instead; see [Customizing the OAuth HTTP Client](/client/authorization/#customizing-the-oauth-http-client).
253
+
251
254
{: .note }
252
255
> Answers to server-to-client requests (a pong, an elicitation result) are POSTed from inside
253
256
> the SSE streaming callback of another response, re-entering the connection on the same thread.
0 commit comments