MCP PHP SDK

CreateSamplingMessageRequest extends Request

FinalYes

A request from the server to sample an LLM via the client. The client has full discretion over which model to select.

since protocol revision 2026-07-28 (SEP-2577), earliest removal 2027-07-28. Integrate with an LLM provider's API directly instead.

The client should also inform the user before beginning sampling, to allow them to inspect the request (human in the loop) and decide whether to approve it.

Tags
author

Kyrian Obikwelu koshnawaza@gmail.com

Table of Contents

Properties

$includeContext  : SamplingContext|null
$maxTokens  : int
$messages  : array<string|int, mixed>
$metadata  : array<string|int, mixed>|null
$preferences  : ModelPreferences|null
$stopSequences  : array<string|int, mixed>|null
$systemPrompt  : string|null
$temperature  : float|null
$toolChoice  : ToolChoice|null
$tools  : array<string|int, mixed>|null
$id  : string|int
$meta  : array<string, mixed>|null

Methods

__construct()  : mixed
fromArray()  : static
getId()  : string|int
getMeta()  : array<string, mixed>|null
getMethod()  : string
jsonSerialize()  : RequestData
validateToolFlow()  : void
Assert the spec's tool-flow rules over the whole message list.
withId()  : static
withMeta()  : static
fromParams()  : static
getParams()  : ToolChoice}

Properties

$meta

protected array<string, mixed>|null $meta = null

Methods

__construct()

public __construct(array<string|int, SamplingMessage$messages, int $maxTokens[, ModelPreferences|null $preferences = null ][, string|null $systemPrompt = null ][, SamplingContext|null $includeContext = null ][, float|null $temperature = null ][, array<string|int, string>|null $stopSequences = null ][, array<string, mixed>|null $metadata = null ][, array<string|int, Tool>|null $tools = null ][, ToolChoice|null $toolChoice = null ]) : mixed
Parameters
$messages : array<string|int, SamplingMessage>

the messages to send to the model

$maxTokens : int

The maximum number of tokens to sample, as requested by the server. The client MAY choose to sample fewer tokens than requested.

$preferences : ModelPreferences|null = null

The server's preferences for which model to select. The client MAY ignore these preferences.

$systemPrompt : string|null = null

An optional system prompt the server wants to use for sampling. The client MAY modify or omit this prompt.

$includeContext : SamplingContext|null = null

A request to include context from one or more MCP servers (including the caller), to be attached to the prompt. The client MAY ignore this request. Allowed values: "none", "thisServer", "allServers" Values other than "none" are soft-deprecated and SHOULD only be sent when the client advertises the sampling.context capability.

$temperature : float|null = null

The temperature to use for sampling. The client MAY ignore this request.

$stopSequences : array<string|int, string>|null = null

A list of sequences to stop sampling at. The client MAY ignore this request.

$metadata : array<string, mixed>|null = null

Optional metadata to pass through to the LLM provider. The format of this metadata is provider-specific.

$tools : array<string|int, Tool>|null = null

tools that the model may use during generation

$toolChoice : ToolChoice|null = null

controls how the model uses tools

fromArray()

public static fromArray(RequestData $data) : static
Parameters
$data : RequestData
Return values
static

getId()

public getId() : string|int
Return values
string|int

getMeta()

public getMeta() : array<string, mixed>|null
Return values
array<string, mixed>|null

jsonSerialize()

public jsonSerialize() : RequestData
Return values
RequestData

validateToolFlow()

Assert the spec's tool-flow rules over the whole message list.

public validateToolFlow() : void

These are deliberately kept out of the hydration path: a violation is an "invalid params" condition the peer must be told about, not a parse failure that would leave the request unanswered. Call it from whatever boundary can report it — the request handler when receiving, the gateway when sending.

Tags
throws
InvalidArgumentException

on the first violation found

withId()

public withId(string|int $id) : static
Parameters
$id : string|int
Return values
static

withMeta()

public withMeta(array<string, mixed>|null $meta) : static
Parameters
$meta : array<string, mixed>|null
Return values
static

fromParams()

protected static fromParams(array<string|int, mixed>|null $params) : static
Parameters
$params : array<string|int, mixed>|null
Return values
static
On this page

Search results