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 an internal error occurred on the receiver.
22: *
23: * This error is returned when the receiver encounters an unexpected condition that prevents it
24: * from fulfilling the request.
25: *
26: * @extends Error<array{code: -32603, message: non-empty-string, data?: mixed}>
27: *
28: * @see https://modelcontextprotocol.io/specification/2026-07-28/schema#internalerror
29: */
30: final readonly class InternalError extends Error
31: {
32: public const string DEFAULT_MESSAGE = 'Internal error';
33:
34: /**
35: * @param non-empty-string $message
36: */
37: public function __construct(string $message, mixed $data = null)
38: {
39: parent::__construct(code: ProtocolErrorCode::InternalError, message: $message, data: $data);
40: }
41:
42: #[\Override]
43: public static function fromArray(array $data): static
44: {
45: $message = $data['message'] ?? self::DEFAULT_MESSAGE;
46: Assert::that($message)->isNonEmptyString('error "message" must be a non-empty string, {type} given.');
47:
48: return new self(message: $message, data: $data['data'] ?? null);
49: }
50:
51: #[\Override]
52: public function toArray(): array
53: {
54: $result = [
55: 'code' => ProtocolErrorCode::InternalError->value,
56: 'message' => $this->message,
57: ];
58:
59: $data = $this->data ?? [];
60:
61: if ([] !== $data) {
62: $result['data'] = $data;
63: }
64:
65: return $result;
66: }
67: }
68: