-
Notifications
You must be signed in to change notification settings - Fork 54
Add structuredContent responses #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
Draft
macbookandrew
wants to merge
5
commits into
laravel:main
Choose a base branch
from
macbookandrew:feature/structured-content-response
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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.