onSampling in two cases:
- A legacy sessionful server sends a
sampling/createMessagerequest. - A sessionless server enters the temporary
input_requiredmulti-round-trip compatibility flow.
Handle a request
OnSamplingCallback receives the protocol parameters for
sampling/createMessage and must return a
SamplingCreateMessageResult. Setting the callback also advertises the
sampling capability where the negotiated protocol supports it.
Global and per-server handlers
SetonSampling at the top level of the MCPClient configuration to use it as
a global default. An onSampling callback on an individual server
configuration takes precedence over that default. The Node client also accepts
the global callback in its second constructor argument.
React
useMcp({ onSampling }) handles compatible requests directly with your
callback.
McpClientProvider instead installs a handler that queues each request on the
managed server:
server.pendingSamplingRequestscontains the requests awaiting a decision.server.approveSampling(requestId, result)resolves a request with a completeSamplingCreateMessageResult.server.rejectSampling(requestId, error?)rejects it.onSamplingRequeston the server configuration can notify your UI when a request is added to the queue.
Runnable example
The maintained sampling client example exercises both the legacy push request and the sessionlessinput_required compatibility flow.