Skip to content

Support client-side sampling via MCP::Client#on_sampling#458

Merged
koic merged 1 commit into
modelcontextprotocol:mainfrom
koic:feature_sampling_client
Jul 15, 2026
Merged

Support client-side sampling via MCP::Client#on_sampling#458
koic merged 1 commit into
modelcontextprotocol:mainfrom
koic:feature_sampling_client

Conversation

@koic

@koic koic commented Jul 14, 2026

Copy link
Copy Markdown
Member

Motivation and Context

The MCP specification defines sampling/createMessage as a server-to-client request that lets a server ask the client to generate an LLM completion, so the server can leverage the client's model access without its own API keys. The Ruby SDK already supports the server side (sending the request) and, on the client side, the generic server-to-client request infrastructure introduced for elicitation (the standalone GET SSE listening stream and on_server_request dispatch). This adds the client-side sampling/createMessage handler on top of that infrastructure.

Changes

  • Add MCP::Client#on_sampling, a thin wrapper that registers a sampling/createMessage handler via transport.on_server_request, mirroring on_elicitation. The handler receives the request params and returns a CreateMessageResult-shaped Hash sent back as the JSON-RPC result.
  • Document client-side sampling in the README, including the sampling (and sampling.tools) capability declaration and the human-in-the-loop guidance from the specification.

How Has This Been Tested?

  • Unit tests for on_sampling covering handler registration on the transport and the error raised when the transport does not support server-to-client requests.
  • An HTTP transport test that dispatches a sampling/createMessage server request delivered on the SSE stream and returns a CreateMessageResult.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

Deprecation Note: SEP-2577 deprecates sampling (along with roots and logging) starting with protocol version 2026-07-28. The deprecation warnings are added in #406. Sampling remains fully supported under the current 2025-11-25 protocol version and stays available throughout the spec's deprecation window, so the client side is implemented here for parity with the server side and with the Python and TypeScript SDKs.

MCP::Client#on_sampling carries the same @deprecated annotation as the server-side senders annotated in #429, tailored to the receiving side: the handler exists to interoperate with servers that still send sampling requests during the deprecation window. The README section carries the same note.

Ref: https://modelcontextprotocol.io/specification/2025-11-25/client/sampling

## Motivation and Context

The MCP specification defines `sampling/createMessage` as a server-to-client request
that lets a server ask the client to generate an LLM completion, so the server can leverage
the client's model access without its own API keys.
The Ruby SDK already supports the server side (sending the request) and, on the client side,
the generic server-to-client request infrastructure introduced for elicitation
(the standalone GET SSE listening stream and `on_server_request` dispatch).
This adds the client-side `sampling/createMessage` handler on top of that infrastructure.

## Changes

- Add `MCP::Client#on_sampling`, a thin wrapper that registers a `sampling/createMessage` handler
  via `transport.on_server_request`, mirroring `on_elicitation`. The handler receives the request params
  and returns a `CreateMessageResult`-shaped Hash sent back as the JSON-RPC result.
- Document client-side sampling in the README, including the `sampling` (and `sampling.tools`)
  capability declaration and the human-in-the-loop guidance from the specification.

## How Has This Been Tested?

- Unit tests for `on_sampling` covering handler registration on the transport
  and the error raised when the transport does not support server-to-client requests.
- An HTTP transport test that dispatches a `sampling/createMessage` server request delivered on
  the SSE stream and returns a `CreateMessageResult`.

## Deprecation Note

SEP-2577 deprecates sampling (along with roots and logging) starting with protocol version `2026-07-28`.
The deprecation warnings are added in modelcontextprotocol#406.
Sampling remains fully supported under the current `2025-11-25` protocol version and stays available
throughout the spec's deprecation window, so the client side is implemented here for parity with
the server side and with the Python and TypeScript SDKs.

`MCP::Client#on_sampling` carries the same `@deprecated` annotation as the server-side senders
annotated in modelcontextprotocol#429, tailored to the receiving side: the handler exists to interoperate with servers
that still send sampling requests during the deprecation window. The README section carries
the same note.

Ref: https://modelcontextprotocol.io/specification/2025-11-25/client/sampling
@koic koic merged commit cc219a2 into modelcontextprotocol:main Jul 15, 2026
11 checks passed
@koic koic deleted the feature_sampling_client branch July 15, 2026 16:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants