Skip to content

Commit 8718072

Browse files
committed
Refactoring according to suggestions
1 parent 6100ffc commit 8718072

File tree

4 files changed

+59
-4
lines changed

4 files changed

+59
-4
lines changed

src/Schema/Result/CallToolResult.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Mcp\Schema\Content\EmbeddedResource;
1818
use Mcp\Schema\Content\ImageContent;
1919
use Mcp\Schema\Content\TextContent;
20-
use Mcp\Schema\JsonRpc\ResultInterface;
2120

2221
/**
2322
* The server's response to a tool call.
@@ -33,7 +32,7 @@
3332
*
3433
* @author Kyrian Obikwelu <[email protected]>
3534
*/
36-
class CallToolResult implements ResultInterface
35+
class CallToolResult implements CallToolResultInterface
3736
{
3837
/**
3938
* Create a new CallToolResult.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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\Result;
13+
14+
use Mcp\Schema\JsonRpc\ResultInterface;
15+
16+
interface CallToolResultInterface extends ResultInterface
17+
{
18+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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\Result;
13+
14+
class CallToolStructuredContentResult implements CallToolResultInterface
15+
{
16+
/**
17+
* Creates a new CallToolResult with structured content.
18+
*
19+
* @see https://modelcontextprotocol.io/specification/2025-06-18/server/tools#structured-content
20+
*
21+
* @param mixed[] $structuredContent JSON content for `structuredContent`
22+
* @param CallToolResultInterface $callToolResult Traditional result
23+
*/
24+
public function __construct(
25+
public readonly array $structuredContent,
26+
public readonly CallToolResultInterface $callToolResult,
27+
) {
28+
}
29+
30+
public function jsonSerialize(): mixed
31+
{
32+
return ['structuredContent' => $this->structuredContent] + $this->callToolResult->jsonSerialize();
33+
}
34+
}

src/Server/Handler/Request/CallToolHandler.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Mcp\Schema\JsonRpc\Response;
2222
use Mcp\Schema\Request\CallToolRequest;
2323
use Mcp\Schema\Result\CallToolResult;
24+
use Mcp\Schema\Result\CallToolResultInterface;
2425
use Mcp\Server\Session\SessionInterface;
2526
use Psr\Log\LoggerInterface;
2627
use Psr\Log\NullLogger;
@@ -59,14 +60,17 @@ public function handle(Request $request, SessionInterface $session): Response|Er
5960
}
6061

6162
$result = $this->referenceHandler->handle($reference, $arguments);
62-
$formatted = $reference->formatResult($result);
63+
64+
if (!$result instanceof CallToolResultInterface) {
65+
$result = new CallToolResult($reference->formatResult($result));
66+
}
6367

6468
$this->logger->debug('Tool executed successfully', [
6569
'name' => $toolName,
6670
'result_type' => \gettype($result),
6771
]);
6872

69-
return new Response($request->getId(), new CallToolResult($formatted));
73+
return new Response($request->getId(), $result);
7074
} catch (ToolNotFoundException $e) {
7175
$this->logger->error('Tool not found', ['name' => $toolName]);
7276

0 commit comments

Comments
 (0)