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\Server\Exception;
15:
16: use Nexus\Mcp\Core\Exception\McpExceptionInterface;
17: use Nexus\Mcp\Server\Validation\SchemaViolation;
18:
19: /**
20: * Thrown when a tool's result violates its declared `outputSchema`, by non-conformance or by carrying
21: * no `structuredContent` at all.
22: */
23: final class ToolOutputValidationException extends \RuntimeException implements McpExceptionInterface
24: {
25: /**
26: * @param list<SchemaViolation> $errors Conformance failures, empty when the result carried no `structuredContent`
27: */
28: public function __construct(string $toolName, array $errors, ?\Throwable $previous = null)
29: {
30: parent::__construct(
31: [] === $errors
32: ? \sprintf('Tool "%s" declares an outputSchema but its result carries no structuredContent.', $toolName)
33: : \sprintf(
34: 'Tool "%s" returned structuredContent that does not conform to its outputSchema: %s',
35: $toolName,
36: implode(' ', array_map(static fn(SchemaViolation $violation): string => $violation->message, $errors)),
37: ),
38: previous: $previous,
39: );
40: }
41: }
42: