Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions src/Server/Request/ToolsCallHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function execute(string $method, ?array $params = null): array
}

try {
$json = json_encode($preparedResult, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE);
json_encode($preparedResult, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE);
} catch (JsonException $exception) {
throw new JsonRpcErrorException(
message: 'Failed to encode tool result as JSON: '.$exception->getMessage(),
Expand All @@ -91,14 +91,6 @@ public function execute(string $method, ?array $params = null): array
}

return [
'content' => [
[
'type' => 'text',
'text' => $json,
],
],
// Provide structuredContent alongside text per MCP 2025-06-18 guidance.
// @see https://modelcontextprotocol.io/specification/2025-06-18#structured-content
'structuredContent' => $preparedResult,
];
}
Expand Down
65 changes: 34 additions & 31 deletions src/Services/ToolService/ToolResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,37 @@
namespace OPGG\LaravelMcpServer\Services\ToolService;

use InvalidArgumentException;
use JsonException;

/**
* Value object describing a structured tool response.
*/
final class ToolResponse
{
/**
* @var array<int, array{type: string, text: string, source?: string}>
*/
private array $content;

/**
* @var array<string, mixed>
*/
private array $metadata;

/**
* @param array<int, array{type: string, text: string, source?: string}> $content
* @param array<string, mixed> $metadata
*/
private function __construct(private array $content, private array $metadata = [])
private bool $includeContent;

private function __construct(array $content, array $metadata = [], bool $includeContent = true)
{
if (array_key_exists('content', $metadata)) {
throw new InvalidArgumentException('Metadata must not contain a content key.');
}

$this->content = array_values($content);
$this->metadata = $metadata;
$this->includeContent = $includeContent && $this->content !== [];

foreach ($this->content as $index => $item) {
if (! is_array($item) || ! isset($item['type'], $item['text'])) {
Expand Down Expand Up @@ -60,41 +73,26 @@ public static function text(string $text, string $type = 'text', array $metadata
}

/**
* Create a ToolResponse that includes structured content alongside serialised text.
* Create a ToolResponse that includes structured content alongside optional serialised text.
*
* @param array<int, array{type: string, text: string, source?: string}>|null $content
* @param array<string, mixed> $metadata
*
* @throws JsonException
*/
public static function structured(array $structuredContent, ?array $content = null, array $metadata = []): self
{
$json = json_encode($structuredContent, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE);

$contentItems = $content !== null ? array_values($content) : [];

$hasSerialisedText = false;
foreach ($contentItems as $item) {
if (isset($item['type'], $item['text']) && $item['type'] === 'text' && $item['text'] === $json) {
$hasSerialisedText = true;
break;
}
}

if (! $hasSerialisedText) {
$contentItems[] = [
'type' => 'text',
'text' => $json,
];
}

return new self($contentItems, [
...$metadata,
// The MCP 2025-06-18 spec encourages servers to mirror structured payloads in the
// `structuredContent` field for reliable client parsing.
// @see https://modelcontextprotocol.io/specification/2025-06-18#structured-content
'structuredContent' => $structuredContent,
]);
return new self(
$contentItems,
[
...$metadata,
// The MCP 2025-06-18 spec encourages servers to mirror structured payloads in the
// `structuredContent` field for reliable client parsing.
// @see https://modelcontextprotocol.io/specification/2025-06-18#structured-content
'structuredContent' => $structuredContent,
],
$contentItems !== []
);
}

/**
Expand Down Expand Up @@ -124,9 +122,14 @@ public function metadata(): array
*/
public function toArray(): array
{
return [
$payload = [
...$this->metadata,
'content' => $this->content,
];

if ($this->includeContent) {
$payload['content'] = $this->content;
}

return $payload;
}
}
4 changes: 1 addition & 3 deletions tests/Http/StreamableHttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@
expect($data['result']['content'][0]['text'])
->toContain('HelloWorld `Tester` developer');

expect($data['result']['content'][1]['type'])->toBe('text');
$decoded = json_decode($data['result']['content'][1]['text'], true);
expect($decoded['name'])->toBe('Tester');
expect($data['result']['content'])->toHaveCount(1);
expect($data['result']['structuredContent']['message'])
->toContain('HelloWorld `Tester` developer');
});
Expand Down