1: <?php
2:
3: declare(strict_types=1);
4:
5: /**
6: * This file is part of the Nexus MCP SDK package.
7: *
8: * (c) 2026 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\Core\Schema\Error;
15:
16: use Nexus\Assert\Assert;
17: use Nexus\Mcp\Core\Schema\Enum\ProtocolErrorCode;
18: use Nexus\Mcp\Core\Schema\Error;
19:
20: /**
21: * A JSON-RPC error indicating that the method parameters are invalid or malformed.
22: *
23: * In MCP, this error is returned in various contexts when request parameters fail validation:
24: *
25: * - **Tools**: Unknown tool name or invalid tool arguments
26: * - **Prompts**: Unknown prompt name or missing required arguments
27: * - **Pagination**: Invalid or expired cursor values
28: * - **Logging**: Invalid log level
29: * - **Elicitation**: Server requests an elicitation mode not declared in client capabilities
30: * - **Sampling**: Missing tool result or tool results mixed with other content
31: *
32: * @extends Error<array{code: -32602, message: non-empty-string, data?: mixed}>
33: *
34: * @see https://modelcontextprotocol.io/specification/2026-07-28/schema#invalidparamserror
35: */
36: final readonly class InvalidParamsError extends Error
37: {
38: public const string DEFAULT_MESSAGE = 'Invalid params';
39:
40: /**
41: * @param non-empty-string $message
42: */
43: public function __construct(string $message, mixed $data = null)
44: {
45: parent::__construct(code: ProtocolErrorCode::InvalidParams, message: $message, data: $data);
46: }
47:
48: #[\Override]
49: public static function fromArray(array $data): static
50: {
51: $message = $data['message'] ?? self::DEFAULT_MESSAGE;
52: Assert::that($message)->isNonEmptyString('error "message" must be a non-empty string, {type} given.');
53:
54: return new self(message: $message, data: $data['data'] ?? null);
55: }
56:
57: #[\Override]
58: public function toArray(): array
59: {
60: $result = [
61: 'code' => ProtocolErrorCode::InvalidParams->value,
62: 'message' => $this->message,
63: ];
64:
65: $data = $this->data ?? [];
66:
67: if ([] !== $data) {
68: $result['data'] = $data;
69: }
70:
71: return $result;
72: }
73: }
74: