Enable servers to send sampling messages to clients (#101)

This commit is contained in:
Christopher Hertel 2025-11-01 09:02:24 +01:00 • committed by GitHub
commit fb3c1e65a6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 460 additions and 137 deletions

View file

@ -18,7 +18,7 @@ use Mcp\Schema\Enum\Role;
* Describes a message issued to or received from an LLM API during sampling.
*
* @phpstan-type SamplingMessageData = array{
* role: string,
* role: 'user'|'assistant',
* content: TextContent|ImageContent|AudioContent
* }
*

View file

@ -0,0 +1,19 @@
<?php
/*
* This file is part of the official PHP MCP SDK.
*
* A collaboration between Symfony and the PHP Foundation.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mcp\Schema\Enum;
enum SamplingContext: string
{
case NONE = 'none';
case THIS_SERVER = 'thisServer';
case ALL_SERVERS = 'allServers';
}

View file

@ -13,6 +13,7 @@ namespace Mcp\Schema\Request;
use Mcp\Exception\InvalidArgumentException;
use Mcp\Schema\Content\SamplingMessage;
use Mcp\Schema\Enum\SamplingContext;
use Mcp\Schema\JsonRpc\Request;
use Mcp\Schema\ModelPreferences;
@ -29,29 +30,33 @@ final class CreateSamplingMessageRequest extends Request
* @param SamplingMessage[] $messages the messages to send to the model
* @param int $maxTokens The maximum number of tokens to sample, as requested by the server.
* The client MAY choose to sample fewer tokens than requested.
* @param ModelPreferences|null $preferences The server's preferences for which model to select. The client MAY
* @param ?ModelPreferences $preferences The server's preferences for which model to select. The client MAY
* ignore these preferences.
* @param string|null $systemPrompt An optional system prompt the server wants to use for sampling. The
* @param ?string $systemPrompt An optional system prompt the server wants to use for sampling. The
* client MAY modify or omit this prompt.
* @param string|null $includeContext A request to include context from one or more MCP servers (including
* @param ?SamplingContext $includeContext 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"
* @param float|null $temperature The temperature to use for sampling. The client MAY ignore this request.
* @param string[]|null $stopSequences A list of sequences to stop sampling at. The client MAY ignore this request.
* @param ?array<string, mixed> $metadata Optional metadata to pass through to the LLM provider. The format of
* this metadata is provider-specific.
* Allowed values: "none", "thisServer", "allServers"
* @param ?float $temperature The temperature to use for sampling. The client MAY ignore this request.
* @param ?string[] $stopSequences A list of sequences to stop sampling at. The client MAY ignore this request.
* @param ?array<string, mixed> $metadata Optional metadata to pass through to the LLM provider. The format of
* this metadata is provider-specific.
*/
public function __construct(
public readonly array $messages,
public readonly int $maxTokens,
public readonly ?ModelPreferences $preferences = null,
public readonly ?string $systemPrompt = null,
public readonly ?string $includeContext = null,
public readonly ?SamplingContext $includeContext = null,
public readonly ?float $temperature = null,
public readonly ?array $stopSequences = null,
public readonly ?array $metadata = null,
) {
foreach ($this->messages as $message) {
if (!$message instanceof SamplingMessage) {
throw new InvalidArgumentException('Messages must be instance of SamplingMessage.');
}
}
}
public static function getMethod(): string
@ -114,7 +119,7 @@ final class CreateSamplingMessageRequest extends Request
}
if (null !== $this->includeContext) {
$params['includeContext'] = $this->includeContext;
$params['includeContext'] = $this->includeContext->value;
}
if (null !== $this->temperature) {