-
Notifications
You must be signed in to change notification settings - Fork 56
Add structuredContent & outputSchema Support #83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
taylorotwell
merged 19 commits into
laravel:main
from
macbookandrew:feature/structured-content-response
Nov 25, 2025
+919
−7
Merged
Changes from 4 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
e382dc2
Add structuredContent responses
macbookandrew 6dda2a8
fix tests
macbookandrew eb776d0
add Response::structured()
macbookandrew fadef3f
add another test
macbookandrew 8b4d409
fix structure
macbookandrew 37931ac
Merge branch 'main' into feature/structured-content-response
macbookandrew ec25b03
Merge branch 'main' into feature/structured-content-response
pushpak1300 c059663
Refactor
pushpak1300 fea12ea
Fix Test
pushpak1300 aedb251
Add Tests
pushpak1300 37e942f
Add Tests
pushpak1300 b45e1f3
Add Tests
pushpak1300 1e0af0f
Merge branch 'main' into feature/structured-content-response
pushpak1300 3c3b9a9
Refactor this
pushpak1300 43c2c0b
Fix CI
pushpak1300 c7fabe1
Update output schema handling to check for 'properties' key
pushpak1300 b65410c
Fix Test
pushpak1300 0c0fa78
Fix Test
pushpak1300 66bbd6e
Merge branch 'main' into feature/structured-content-response
taylorotwell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Laravel\Mcp\Server\Content; | ||
|
|
||
| use Laravel\Mcp\Server\Contracts\Content; | ||
| use Laravel\Mcp\Server\Prompt; | ||
| use Laravel\Mcp\Server\Resource; | ||
| use Laravel\Mcp\Server\Tool; | ||
|
|
||
| class StructuredContent implements Content | ||
| { | ||
| /** | ||
| * @param array<string, mixed>|object $structuredContent | ||
| */ | ||
| public function __construct(protected array|object $structuredContent = []) | ||
| { | ||
| // | ||
| } | ||
|
|
||
| /** | ||
| * @return array<string, mixed> | ||
| */ | ||
| public function toTool(Tool $tool): array | ||
| { | ||
| return json_decode($this->toJsonString(), true); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<string, mixed> | ||
| */ | ||
| public function toPrompt(Prompt $prompt): array | ||
| { | ||
| return $this->toArray(); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<string, mixed> | ||
| */ | ||
| public function toResource(Resource $resource): array | ||
| { | ||
| return [ | ||
| 'json' => $this->toJsonString(), | ||
| 'uri' => $resource->uri(), | ||
| 'name' => $resource->name(), | ||
| 'title' => $resource->title(), | ||
| 'mimeType' => $resource->mimeType() === 'text/plain' | ||
| ? 'application/json' | ||
| : $resource->mimeType(), | ||
| ]; | ||
| } | ||
|
|
||
| public function __toString(): string | ||
| { | ||
| return $this->toJsonString(); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<string, mixed> | ||
| */ | ||
| public function toArray(): array | ||
| { | ||
| return [ | ||
| 'type' => 'text', | ||
| 'text' => $this->toJsonString(), | ||
| ]; | ||
| } | ||
|
|
||
| private function toJsonString(): string | ||
| { | ||
| return json_encode($this->structuredContent, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\Fixtures; | ||
|
|
||
| use Illuminate\JsonSchema\JsonSchema; | ||
| use Laravel\Mcp\Request; | ||
| use Laravel\Mcp\Response; | ||
| use Laravel\Mcp\Server\Tool; | ||
|
|
||
| class ReturnStructuredContentTool extends Tool | ||
| { | ||
| protected string $description = 'This tool returns structured content'; | ||
|
|
||
| public function handle(Request $request): array | ||
| { | ||
| $request->validate([ | ||
| 'name' => 'required|string', | ||
| 'age' => 'required|integer', | ||
| ]); | ||
|
|
||
| $name = $request->get('name'); | ||
| $age = $request->get('age'); | ||
|
|
||
| return [ | ||
| Response::structured(['name' => $name]), | ||
| Response::structured(['age' => $age]), | ||
| ]; | ||
| } | ||
|
|
||
| public function schema(JsonSchema $schema): array | ||
| { | ||
| return [ | ||
| 'name' => $schema->string() | ||
| ->description('The name of the person to greet') | ||
| ->required(), | ||
| 'age' => $schema->integer() | ||
| ->description('The age of the person') | ||
| ->required(), | ||
| ]; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| <?php | ||
|
|
||
| use Laravel\Mcp\Server\Content\StructuredContent; | ||
| use Laravel\Mcp\Server\Prompt; | ||
| use Laravel\Mcp\Server\Resource; | ||
| use Laravel\Mcp\Server\Tool; | ||
|
|
||
| it('encodes content to resource payload with metadata', function (): void { | ||
| $structuredContent = new StructuredContent(['name' => 'John', 'age' => 30]); | ||
| $resource = new class extends Resource | ||
| { | ||
| protected string $uri = 'file://readme.txt'; | ||
|
|
||
| protected string $name = 'readme'; | ||
|
|
||
| protected string $title = 'Readme File'; | ||
|
|
||
| protected string $mimeType = 'application/json'; | ||
| }; | ||
|
|
||
| $payload = $structuredContent->toResource($resource); | ||
|
|
||
| expect($payload)->toEqual([ | ||
| 'json' => json_encode(['name' => 'John', 'age' => 30], JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT), | ||
| 'uri' => 'file://readme.txt', | ||
| 'name' => 'readme', | ||
| 'title' => 'Readme File', | ||
| 'mimeType' => 'application/json', | ||
| ]); | ||
| }); | ||
|
|
||
| it('may be used in tools', function (): void { | ||
| $structuredContent = new StructuredContent(['name' => 'John', 'age' => 30]); | ||
|
|
||
| $payload = $structuredContent->toTool(new class extends Tool {}); | ||
|
|
||
| expect($payload)->toEqual([ | ||
| 'name' => 'John', | ||
| 'age' => 30, | ||
| ]); | ||
| }); | ||
|
|
||
| it('may be used in prompts', function (): void { | ||
| $structuredContent = new StructuredContent(['name' => 'John', 'age' => 30]); | ||
|
|
||
| $payload = $structuredContent->toPrompt(new class extends Prompt {}); | ||
|
|
||
| expect($payload)->toEqual([ | ||
| 'type' => 'text', | ||
| 'text' => json_encode(['name' => 'John', 'age' => 30], JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT), | ||
| ]); | ||
| }); | ||
|
|
||
| it('casts to string as raw text', function (): void { | ||
| $structuredContent = new StructuredContent(['name' => 'John', 'age' => 30]); | ||
|
|
||
| expect((string) $structuredContent)->toBe(json_encode(['name' => 'John', 'age' => 30], JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT)); | ||
| }); | ||
|
|
||
| it('converts to array with type and text', function (): void { | ||
| $structuredContent = new StructuredContent(['name' => 'John', 'age' => 30]); | ||
|
|
||
| expect($structuredContent->toArray())->toEqual([ | ||
| 'type' => 'text', | ||
| 'text' => json_encode(['name' => 'John', 'age' => 30], JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT), | ||
| ]); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.