1: <?php
2:
3: declare(strict_types=1);
4:
5: /**
6: * This file is part of the Nexus MCP SDK package.
7: *
8: * (c) 2025 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\Schema\Result;
15:
16: use Nexus\Mcp\Schema\Arrayable;
17: use Nexus\Mcp\Schema\Content\AudioContent;
18: use Nexus\Mcp\Schema\Content\ContentBlock;
19: use Nexus\Mcp\Schema\Content\EmbeddedResource;
20: use Nexus\Mcp\Schema\Content\ImageContent;
21: use Nexus\Mcp\Schema\Content\ResourceLink;
22: use Nexus\Mcp\Schema\Content\TextContent;
23:
24: /**
25: * The server's response to a tool call.
26: *
27: * @phpstan-type UnstructuredContent AudioContent|EmbeddedResource|ImageContent|ResourceLink|TextContent
28: *
29: * @extends Result<array{
30: * _meta?: array<string, mixed>,
31: * content: list<template-type<UnstructuredContent, Arrayable, 'T'>>,
32: * isError?: bool,
33: * structuredContent?: template-type<StructuredContent, Arrayable, 'T'>,
34: * }>
35: */
36: final readonly class CallToolResult extends Result implements ServerResult
37: {
38: /**
39: * @param list<UnstructuredContent> $content A list of content objects that represent the unstructured
40: * result of the tool call.
41: * @param null|bool $isError Whether the tool call ended in an error.
42: * @param null|StructuredContent $structuredContent An optional JSON object that represents the structured
43: * result of the tool call.
44: */
45: public function __construct(
46: public array $content,
47: public ?bool $isError = null,
48: public ?StructuredContent $structuredContent = null,
49: ?array $meta = null,
50: ) {
51: parent::__construct($meta);
52: }
53:
54: #[\Override]
55: public function toArray(): array
56: {
57: return array_filter([
58: '_meta' => $this->meta,
59: 'content' => array_map(
60: static fn(Arrayable&ContentBlock $block): array => $block->toArray(),
61: $this->content,
62: ),
63: 'isError' => $this->isError,
64: 'structuredContent' => $this->structuredContent?->toArray(),
65: ], static fn(mixed $value): bool => null !== $value);
66: }
67: }
68: