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/draft/schema#invalidparamserror
35: */
36: final readonly class InvalidParamsError extends Error
37: {
38: public const string DEFAULT_MESSAGE = 'Invalid params';
39:
40: public function __construct(string $message, mixed $data = null)
41: {
42: parent::__construct(code: ProtocolErrorCode::InvalidParams, message: $message, data: $data);
43: }
44:
45: #[\Override]
46: public static function fromArray(array $data): static
47: {
48: $message = $data['message'] ?? self::DEFAULT_MESSAGE;
49: Assert::that($message)->isString('error "message" must be a string, {type} given.');
50:
51: return new self(message: $message, data: $data['data'] ?? null);
52: }
53:
54: #[\Override]
55: public function toArray(): array
56: {
57: $result = [
58: 'code' => ProtocolErrorCode::InvalidParams->value,
59: 'message' => $this->message,
60: ];
61:
62: $data = $this->data ?? [];
63:
64: if ([] !== $data) {
65: $result['data'] = $data;
66: }
67:
68: return $result;
69: }
70: }
71: