ruby-sdk/lib/mcp/client/oauth
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Koichi ITO 02db089038 Bound OAuth response bodies in the client
## Motivation and Context

`MCP::Client::HTTP` caps a single message from the server at 4 MiB, but the OAuth flow talks to
authorization endpoints over its own Faraday connection, and that one had no cap. Discovery,
dynamic client registration, and token responses were buffered in full and handed to `JSON.parse`,
so a server that answered an OAuth request with an endless body grew the client's memory for as
long as it kept sending.

`Flow` and `IDJAGTokenExchange` each build their own connection, so both needed the bound.
The new `BoundedBody` counts bytes in a Faraday `on_data` callback and refuses the response once
it passes `MAX_RESPONSE_BYTES`, which matches the 4 MiB of `MCP::Client::HTTP::MAX_MESSAGE_BYTES`
and `MCP::Client::Stdio::MAX_LINE_BYTES`. It hands back the status paired with the bounded body
rather than the Faraday response, so no later caller can reach the unbounded `response.body`.

Those bytes are counted after decompression. The default Net::HTTP adapter negotiates `Accept-Encoding`
itself  and reads the body through `Net::HTTPResponse#inflater`, so a small compressed body that expands past
the cap is refused partway through the expansion. That holds only while the connection leaves
`Accept-Encoding` alone, which is now recorded in a comment on both `default_http_client` definitions.

An over-cap response is refused rather than truncated: a partial discovery or token document cannot be validated.
Protected resource metadata is the one place where refusing is not fatal, since any PRM discovery failure
already selects the legacy authorization path by design.

## How Has This Been Tested?

New unit tests in `test/mcp/client/oauth/bounded_body_test.rb` cover the streaming path,
the at-limit boundary, and the fallback for adapters that ignore `on_data`. One of them serves
a gzip body over a real socket rather than through WebMock, which returns a stubbed body verbatim
and never runs the inflater; it fails if the cap ever starts measuring compressed bytes. New tests in
`test/mcp/client/oauth/flow_test.rb` and `test/mcp/client/oauth/id_jag_token_exchange_test.rb` drive each of
the four request wrappers end to end with an over-cap body; all five fail without the cap in place.

## Breaking Changes

An OAuth endpoint response larger than 4 MiB is now rejected instead of parsed.
2026-08-16 03:29:15 +09:00
..
bounded_body.rb Bound OAuth response bodies in the client 2026-08-16 03:29:15 +09:00
client_credentials_provider.rb Support private_key_jwt Client Authentication for the client_credentials Grant 2026-06-28 20:59:37 +09:00
cross_app_access_provider.rb Support SEP-990 Cross-App Access via ID-JAG and the jwt-bearer Grant 2026-07-13 11:45:42 +09:00
discovery.rb Restrict OAuth Discovery URL Destinations 2026-08-13 14:54:27 +09:00
flow.rb Bound OAuth response bodies in the client 2026-08-16 03:29:15 +09:00
id_jag_token_exchange.rb Bound OAuth response bodies in the client 2026-08-16 03:29:15 +09:00
in_memory_storage.rb Bind Stored Client Credentials to the Authorization Server Issuer per SEP-2352 2026-07-05 01:12:08 +09:00
jwt_client_assertion.rb Support private_key_jwt Client Authentication for the client_credentials Grant 2026-06-28 20:59:37 +09:00
pkce.rb Add OAuth 2.1 client support for MCP authorization flow 2026-05-17 00:48:41 +09:00
provider.rb Merge pull request #439 from koic/issuer_bound_client_credentials 2026-07-12 02:16:12 +09:00
storage_backed_provider.rb Support OAuth client_credentials grant in OAuth client 2026-06-13 00:12:19 +09:00