-
Notifications
You must be signed in to change notification settings - Fork 103
feat: Add output schema support to MCP tools #153
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
Open
bigdevlarry
wants to merge
1
commit into
modelcontextprotocol:main
Choose a base branch
from
bigdevlarry:add-support-for-output-schema
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.
+598
−54
Open
Changes from all commits
Commits
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
Some comments aren't visible on the classic Files Changed page.
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
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
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
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 |
|---|---|---|
|
|
@@ -24,28 +24,37 @@ | |
| * properties: array<string, mixed>, | ||
| * required: string[]|null | ||
| * } | ||
| * @phpstan-type ToolOutputSchema array{ | ||
| * type: 'object', | ||
bigdevlarry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * properties?: array<string, mixed>, | ||
| * required?: string[]|null, | ||
| * additionalProperties?: bool|array<string, mixed>, | ||
| * description?: string | ||
| * } | ||
| * @phpstan-type ToolData array{ | ||
| * name: string, | ||
| * inputSchema: ToolInputSchema, | ||
| * description?: string|null, | ||
| * annotations?: ToolAnnotationsData, | ||
| * icons?: IconData[], | ||
| * _meta?: array<string, mixed> | ||
| * _meta?: array<string, mixed>, | ||
| * outputSchema?: ToolOutputSchema | ||
| * } | ||
| * | ||
| * @author Kyrian Obikwelu <[email protected]> | ||
| */ | ||
| class Tool implements \JsonSerializable | ||
| { | ||
| /** | ||
| * @param string $name the name of the tool | ||
| * @param ?string $description A human-readable description of the tool. | ||
| * This can be used by clients to improve the LLM's understanding of | ||
| * available tools. It can be thought of like a "hint" to the model. | ||
| * @param ToolInputSchema $inputSchema a JSON Schema object (as a PHP array) defining the expected 'arguments' for the tool | ||
| * @param ?ToolAnnotations $annotations optional additional tool information | ||
| * @param ?Icon[] $icons optional icons representing the tool | ||
| * @param ?array<string, mixed> $meta Optional metadata | ||
| * @param string $name the name of the tool | ||
| * @param ?string $description A human-readable description of the tool. | ||
| * This can be used by clients to improve the LLM's understanding of | ||
| * available tools. It can be thought of like a "hint" to the model. | ||
| * @param ToolInputSchema $inputSchema a JSON Schema object (as a PHP array) defining the expected 'arguments' for the tool | ||
| * @param ?ToolAnnotations $annotations optional additional tool information | ||
| * @param ?Icon[] $icons optional icons representing the tool | ||
| * @param ?array<string, mixed> $meta Optional metadata | ||
| * @param ToolOutputSchema|null $outputSchema optional JSON Schema object (as a PHP array) defining the expected output structure | ||
| */ | ||
| public function __construct( | ||
| public readonly string $name, | ||
|
|
@@ -54,6 +63,7 @@ public function __construct( | |
| public readonly ?ToolAnnotations $annotations, | ||
| public readonly ?array $icons = null, | ||
| public readonly ?array $meta = null, | ||
| public readonly ?array $outputSchema = null, | ||
| ) { | ||
| if (!isset($inputSchema['type']) || 'object' !== $inputSchema['type']) { | ||
| throw new InvalidArgumentException('Tool inputSchema must be a JSON Schema of type "object".'); | ||
|
|
@@ -78,13 +88,23 @@ public static function fromArray(array $data): self | |
| $data['inputSchema']['properties'] = new \stdClass(); | ||
| } | ||
|
|
||
| if (isset($data['outputSchema']) && \is_array($data['outputSchema'])) { | ||
| if (!isset($data['outputSchema']['type']) || 'object' !== $data['outputSchema']['type']) { | ||
| throw new InvalidArgumentException('Tool outputSchema must be of type "object".'); | ||
| } | ||
| if (isset($data['outputSchema']['properties']) && \is_array($data['outputSchema']['properties']) && empty($data['outputSchema']['properties'])) { | ||
| $data['outputSchema']['properties'] = new \stdClass(); | ||
| } | ||
| } | ||
bigdevlarry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return new self( | ||
| $data['name'], | ||
| $data['inputSchema'], | ||
| isset($data['description']) && \is_string($data['description']) ? $data['description'] : null, | ||
| isset($data['annotations']) && \is_array($data['annotations']) ? ToolAnnotations::fromArray($data['annotations']) : null, | ||
| isset($data['icons']) && \is_array($data['icons']) ? array_map(Icon::fromArray(...), $data['icons']) : null, | ||
| isset($data['_meta']) && \is_array($data['_meta']) ? $data['_meta'] : null | ||
| isset($data['_meta']) && \is_array($data['_meta']) ? $data['_meta'] : null, | ||
| isset($data['outputSchema']) && \is_array($data['outputSchema']) ? $data['outputSchema'] : null, | ||
| ); | ||
| } | ||
|
|
||
|
|
@@ -95,7 +115,8 @@ public static function fromArray(array $data): self | |
| * description?: string, | ||
| * annotations?: ToolAnnotations, | ||
| * icons?: Icon[], | ||
| * _meta?: array<string, mixed> | ||
| * _meta?: array<string, mixed>, | ||
| * outputSchema?: ToolOutputSchema | ||
| * } | ||
| */ | ||
| public function jsonSerialize(): array | ||
|
|
@@ -116,6 +137,9 @@ public function jsonSerialize(): array | |
| if (null !== $this->meta) { | ||
| $data['_meta'] = $this->meta; | ||
| } | ||
| if (null !== $this->outputSchema) { | ||
| $data['outputSchema'] = $this->outputSchema; | ||
| } | ||
|
|
||
| return $data; | ||
| } | ||
|
|
||
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
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 |
|---|---|---|
|
|
@@ -5,5 +5,20 @@ | |
| "text": "{\n \"success\": true,\n \"message\": \"Event \\\"Client Call\\\" scheduled successfully for \\\"2024-12-02\\\".\",\n \"event_details\": {\n \"title\": \"Client Call\",\n \"date\": \"2024-12-02\",\n \"type\": \"call\",\n \"time\": \"14:30\",\n \"priority\": \"Normal\",\n \"attendees\": [\n \"[email protected]\"\n ],\n \"invites_will_be_sent\": false\n }\n}" | ||
| } | ||
| ], | ||
| "structuredContent": { | ||
| "success": true, | ||
| "message": "Event \"Client Call\" scheduled successfully for \"2024-12-02\".", | ||
| "event_details": { | ||
| "title": "Client Call", | ||
| "date": "2024-12-02", | ||
| "type": "call", | ||
| "time": "14:30", | ||
| "priority": "Normal", | ||
| "attendees": [ | ||
| "[email protected]" | ||
| ], | ||
| "invites_will_be_sent": false | ||
| } | ||
| }, | ||
| "isError": false | ||
| } | ||
Oops, something went wrong.
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.