Skip to content

Commit e1c96cf

Browse files
committed
Added basic StructuredContent
1 parent 345b94d commit e1c96cf

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

src/Capability/Tool/ToolCaller.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Mcp\Schema\Content\AudioContent;
1919
use Mcp\Schema\Content\EmbeddedResource;
2020
use Mcp\Schema\Content\ImageContent;
21+
use Mcp\Schema\Content\StructuredContent;
2122
use Mcp\Schema\Content\TextContent;
2223
use Mcp\Schema\Request\CallToolRequest;
2324
use Mcp\Schema\Result\CallToolResult;
@@ -59,7 +60,16 @@ public function call(CallToolRequest $request): CallToolResult
5960

6061
try {
6162
$result = $this->referenceHandler->handle($toolReference, $arguments);
62-
/** @var TextContent[]|ImageContent[]|EmbeddedResource[]|AudioContent[] $formattedResult */
63+
64+
if ($result instanceof CallToolResult) {
65+
$this->logger->debug('Tool executed successfully', [
66+
'name' => $toolName,
67+
'result_type' => \gettype($result),
68+
]);
69+
70+
return $result;
71+
}
72+
/** @var array<int, TextContent|ImageContent|EmbeddedResource|AudioContent|StructuredContent> $formattedResult */
6373
$formattedResult = $toolReference->formatResult($result);
6474

6575
$this->logger->debug('Tool executed successfully', [
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the official PHP MCP SDK.
5+
*
6+
* A collaboration between Symfony and the PHP Foundation.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Mcp\Schema\Content;
13+
14+
class StructuredContent extends Content
15+
{
16+
public function __construct(
17+
private array $data = [],
18+
) {
19+
parent::__construct('structured');
20+
}
21+
22+
public function jsonSerialize(): mixed
23+
{
24+
return json_encode($this->data);
25+
}
26+
}

src/Schema/Result/CallToolResult.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Mcp\Schema\Content\Content;
1717
use Mcp\Schema\Content\EmbeddedResource;
1818
use Mcp\Schema\Content\ImageContent;
19+
use Mcp\Schema\Content\StructuredContent;
1920
use Mcp\Schema\Content\TextContent;
2021
use Mcp\Schema\JsonRpc\Response;
2122
use Mcp\Schema\JsonRpc\ResultInterface;
@@ -44,7 +45,7 @@ class CallToolResult implements ResultInterface
4445
/**
4546
* Create a new CallToolResult.
4647
*
47-
* @param array<TextContent|ImageContent|AudioContent|EmbeddedResource> $content The content of the tool result
48+
* @param array<TextContent|ImageContent|AudioContent|EmbeddedResource|StructuredContent> $content The content of the tool result
4849
* @param bool $isError Whether the tool execution resulted in an error. If not set, this is assumed to be false (the call was successful).
4950
*/
5051
public function __construct(
@@ -107,7 +108,7 @@ public static function fromArray(array $data): self
107108

108109
/**
109110
* @return array{
110-
* content: array<TextContent|ImageContent|AudioContent|EmbeddedResource>,
111+
* content: array<TextContent|ImageContent|AudioContent|EmbeddedResource|StructuredContent>,
111112
* isError: bool,
112113
* }
113114
*/

0 commit comments

Comments
 (0)