[Server] Handle malformed JSON-RPC input graceful (#412)

* Never let a throwable escape Protocol::processInput()

* Validate payload types in the schema hydration methods

* Stop leaking handler exception messages to the client
This commit is contained in:
Christopher Hertel 2026-08-14 22:34:01 +02:00 • committed by GitHub
commit c8ed07bba2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -87,6 +87,9 @@ final class CreateSamplingMessageRequest extends Request
$preferences = null;
if (isset($params['preferences'])) {
if (!\is_array($params['preferences'])) {
throw new InvalidArgumentException('Invalid "preferences" parameter for sampling/createMessage.');
}
$preferences = ModelPreferences::fromArray($params['preferences']);
}
@ -95,13 +98,37 @@ final class CreateSamplingMessageRequest extends Request
$includeContext = SamplingContext::tryFrom($params['includeContext']);
}
if (isset($params['systemPrompt']) && !\is_string($params['systemPrompt'])) {
throw new InvalidArgumentException('Invalid "systemPrompt" parameter for sampling/createMessage.');
}
if (isset($params['temperature']) && !\is_float($params['temperature']) && !\is_int($params['temperature'])) {
throw new InvalidArgumentException('Invalid "temperature" parameter for sampling/createMessage.');
}
if (isset($params['stopSequences'])) {
if (!\is_array($params['stopSequences'])) {
throw new InvalidArgumentException('Invalid "stopSequences" parameter for sampling/createMessage.');
}
foreach ($params['stopSequences'] as $stopSequence) {
if (!\is_string($stopSequence)) {
throw new InvalidArgumentException('Each entry in "stopSequences" must be a string for sampling/createMessage.');
}
}
}
if (isset($params['metadata']) && !\is_array($params['metadata'])) {
throw new InvalidArgumentException('Invalid "metadata" parameter for sampling/createMessage.');
}
return new self(
$messages,
$params['maxTokens'],
$preferences,
$params['systemPrompt'] ?? null,
$includeContext,
$params['temperature'] ?? null,
isset($params['temperature']) ? (float) $params['temperature'] : null,
$params['stopSequences'] ?? null,
$params['metadata'] ?? null,
);