| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | /** |
| 6: | * This file is part of the Nexus MCP SDK package. |
| 7: | * |
| 8: | * (c) 2025 John Paul E. Balandan, CPA <paulbalandan@gmail.com> |
| 9: | * |
| 10: | * For the full copyright and license information, please view |
| 11: | * the LICENSE file that was distributed with this source code. |
| 12: | */ |
| 13: | |
| 14: | namespace Nexus\Mcp\Schema\Request; |
| 15: | |
| 16: | use Nexus\Mcp\Schema\Arrayable; |
| 17: | use Nexus\Mcp\Schema\Message\JsonRpcRequest; |
| 18: | use Nexus\Mcp\Schema\Message\RequestId; |
| 19: | use Nexus\Mcp\Schema\Sampling\ModelPreferences; |
| 20: | use Nexus\Mcp\Schema\Sampling\SamplingMessage; |
| 21: | |
| 22: | /** |
| 23: | * A request from the server to sample an LLM via the client. The client has full discretion over which model to select. |
| 24: | * The client should also inform the user before beginning sampling, to allow them to inspect the request |
| 25: | * (human in the loop) and decide whether to approve it. |
| 26: | * |
| 27: | * @extends JsonRpcRequest<array{ |
| 28: | * jsonrpc: '2.0', |
| 29: | * id: int|non-empty-string, |
| 30: | * method: 'sampling/createMessage', |
| 31: | * params: array{ |
| 32: | * _meta?: array<string, mixed>, |
| 33: | * messages: list<template-type<SamplingMessage, Arrayable, 'T'>>, |
| 34: | * maxTokens: int<0, max>, |
| 35: | * modelPreferences?: template-type<ModelPreferences, Arrayable, 'T'>, |
| 36: | * systemPrompt?: non-empty-string, |
| 37: | * includeContext?: 'allServers'|'none'|'thisServer', |
| 38: | * temperature?: float, |
| 39: | * stopSequences?: list<non-empty-string>, |
| 40: | * metadata?: array<string, mixed>, |
| 41: | * }, |
| 42: | * }> |
| 43: | */ |
| 44: | final readonly class CreateMessageRequest extends JsonRpcRequest implements ServerRequest |
| 45: | { |
| 46: | /** |
| 47: | * @param list<SamplingMessage> $messages The messages to be sent to the LLM for sampling. |
| 48: | * @param int<0, max> $maxTokens The maximum number of tokens to sample, as requested by the server. |
| 49: | * The client MAY choose to sample fewer tokens than requested. |
| 50: | * @param null|ModelPreferences $modelPreferences The server’s preferences for which model to select. |
| 51: | * @param null|non-empty-string $systemPrompt An optional system prompt the server wants to use for sampling. |
| 52: | * @param null|'allServers'|'none'|'thisServer' $includeContext A request to include context from one or more MCP servers |
| 53: | * (including the caller), to be attached to the prompt. |
| 54: | * @param null|float $temperature The temperature to use for sampling. |
| 55: | * @param null|list<non-empty-string> $stopSequences A list of sequences to stop sampling at. |
| 56: | * @param null|array<string, mixed> $metadata Optional metadata to pass through to the LLM provider. |
| 57: | * The format of this metadata is provider-specific. |
| 58: | * @param null|array<string, mixed> $meta Reserved by MCP to allow clients and servers to attach |
| 59: | * additional metadata to their interactions. |
| 60: | */ |
| 61: | public function __construct( |
| 62: | RequestId $id, |
| 63: | array $messages, |
| 64: | int $maxTokens, |
| 65: | ?ModelPreferences $modelPreferences = null, |
| 66: | ?string $systemPrompt = null, |
| 67: | ?string $includeContext = null, |
| 68: | ?float $temperature = null, |
| 69: | ?array $stopSequences = null, |
| 70: | ?array $metadata = null, |
| 71: | ?array $meta = null, |
| 72: | ) { |
| 73: | parent::__construct(self::JSON_RPC_VERSION, $id, 'sampling/createMessage', array_filter([ |
| 74: | '_meta' => $meta, |
| 75: | 'messages' => array_map( |
| 76: | static fn(SamplingMessage $message): array => $message->toArray(), |
| 77: | $messages, |
| 78: | ), |
| 79: | 'maxTokens' => $maxTokens, |
| 80: | 'modelPreferences' => $modelPreferences?->toArray(), |
| 81: | 'systemPrompt' => $systemPrompt, |
| 82: | 'includeContext' => $includeContext, |
| 83: | 'temperature' => $temperature, |
| 84: | 'stopSequences' => $stopSequences, |
| 85: | 'metadata' => $metadata, |
| 86: | ], static fn(mixed $value): bool => null !== $value)); |
| 87: | } |
| 88: | } |
| 89: |