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: * Returned when a server rejects a request because the values in the HTTP headers do not match the
22: * corresponding values in the request body, or because required headers are missing or malformed. For
23: * HTTP, the response status code MUST be `400 Bad Request`.
24: *
25: * @extends Error<array{code: -32020, message: non-empty-string, data?: mixed}>
26: *
27: * @see https://modelcontextprotocol.io/specification/draft/schema#headermismatcherror
28: */
29: final readonly class HeaderMismatchError extends Error
30: {
31: public const string DEFAULT_MESSAGE = 'Header mismatch';
32:
33: public function __construct(string $message = self::DEFAULT_MESSAGE)
34: {
35: parent::__construct(code: ProtocolErrorCode::HeaderMismatch, message: $message);
36: }
37:
38: #[\Override]
39: public static function fromArray(array $data): static
40: {
41: $message = $data['message'] ?? self::DEFAULT_MESSAGE;
42: Assert::that($message)->isString('error "message" must be a string, {type} given.');
43:
44: return new self(message: $message);
45: }
46:
47: #[\Override]
48: public function toArray(): array
49: {
50: return [
51: 'code' => ProtocolErrorCode::HeaderMismatch->value,
52: 'message' => $this->message,
53: ];
54: }
55: }
56: