-
Notifications
You must be signed in to change notification settings - Fork 100
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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', | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Can't the schema be a list? |
||||||
| * 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(); | ||||||
| } | ||||||
| } | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I advise against validating this here we should leave that to the user discretion, it'll make the sdk more portable and subject to specification changes.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was following the validation pattern we've for the inputSchema setup above. Also, if we don't add validation for outputSchema, how can we have a structured output format? Users might use a structure we don't support, don't you think?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I'm actually not a huge fan of the above I think it's the responsability of another abstraction to do this, and its also part of a bigger discussion around json schema and validation (I suggested already that these should not be coded into the php-sdk as they're quite complicated subjects). |
||||||
|
|
||||||
| 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; | ||||||
| } | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,15 +62,22 @@ public function handle(Request $request, SessionInterface $session): Response|Er | |
|
|
||
| $arguments['_session'] = $session; | ||
|
|
||
| $result = $this->referenceHandler->handle($reference, $arguments); | ||
| $rawResult = $this->referenceHandler->handle($reference, $arguments); | ||
|
|
||
| if (!$result instanceof CallToolResult) { | ||
| $result = new CallToolResult($reference->formatResult($result)); | ||
| $structuredContent = null; | ||
| if (null !== $reference->tool->outputSchema && !$rawResult instanceof CallToolResult) { | ||
| $structuredContent = $reference->extractStructuredContent($rawResult); | ||
| } | ||
|
|
||
| $result = $rawResult; | ||
| if (!$rawResult instanceof CallToolResult) { | ||
| $result = new CallToolResult($reference->formatResult($rawResult), structuredContent: $structuredContent); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd try not to change this part of the code, I understand we want to comply with the spec that says: If possible I think it should be handled in the My personal opinion would be to throw if the handler doesn't return a structured content.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, we're trying to comply with what the spec says here. I also think it reads better having it here otherwise, @chr-hertel feels otherwise |
||
| } | ||
|
|
||
| $this->logger->debug('Tool executed successfully', [ | ||
| 'name' => $toolName, | ||
| 'result_type' => \gettype($result), | ||
| 'structured_content' => $structuredContent, | ||
| ]); | ||
|
|
||
| return new Response($request->getId(), $result); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,9 @@ | ||
| { | ||
| "content": [ | ||
| { | ||
| "type": "text", | ||
| "text": "{\n \"result\": 50,\n \"operation\": \"10 multiply 5\",\n \"precision\": 2,\n \"within_bounds\": true\n}" | ||
| } | ||
| ], | ||
| "isError": false | ||
| "content": [ | ||
| { | ||
| "type": "text", | ||
| "text": "{\n \"result\": 50,\n \"operation\": \"10 multiply 5\",\n \"precision\": 2,\n \"within_bounds\": true\n}" | ||
| } | ||
| ], | ||
| "isError": false | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should probably not change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just indentation, no code change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know but why as we didn't change anything to the json serialization this is weird to me and pollutes the diff |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,9 @@ | ||
| { | ||
| "content": [ | ||
| { | ||
| "type": "text", | ||
| "text": "{\n \"original\": \"Hello World Test\",\n \"formatted\": \"HELLO WORLD TEST\",\n \"length\": 16,\n \"format_applied\": \"uppercase\"\n}" | ||
| } | ||
| ], | ||
| "isError": false | ||
| "content": [ | ||
| { | ||
| "type": "text", | ||
| "text": "{\n \"original\": \"Hello World Test\",\n \"formatted\": \"HELLO WORLD TEST\",\n \"length\": 16,\n \"format_applied\": \"uppercase\"\n}" | ||
| } | ||
| ], | ||
| "isError": false | ||
| } | ||
bigdevlarry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,9 @@ | ||
| { | ||
| "content": [ | ||
| { | ||
| "type": "text", | ||
| "text": "{\n \"success\": true,\n \"config\": {\n \"app\": {\n \"name\": \"TestApp\",\n \"env\": \"development\",\n \"debug\": true,\n \"url\": \"https://example.com\",\n \"port\": 8080\n },\n \"generated_at\": \"2025-01-01T00:00:00+00:00\",\n \"version\": \"1.0.0\",\n \"features\": {\n \"logging\": true,\n \"caching\": false,\n \"analytics\": false,\n \"rate_limiting\": false\n }\n },\n \"validation\": {\n \"app_name_valid\": true,\n \"url_valid\": true,\n \"port_in_range\": true\n }\n}" | ||
| } | ||
| ], | ||
| "isError": false | ||
| "content": [ | ||
| { | ||
| "type": "text", | ||
| "text": "{\n \"success\": true,\n \"config\": {\n \"app\": {\n \"name\": \"TestApp\",\n \"env\": \"development\",\n \"debug\": true,\n \"url\": \"https://example.com\",\n \"port\": 8080\n },\n \"generated_at\": \"2025-01-01T00:00:00+00:00\",\n \"version\": \"1.0.0\",\n \"features\": {\n \"logging\": true,\n \"caching\": false,\n \"analytics\": false,\n \"rate_limiting\": false\n }\n },\n \"validation\": {\n \"app_name_valid\": true,\n \"url_valid\": true,\n \"port_in_range\": true\n }\n}" | ||
| } | ||
| ], | ||
| "isError": false | ||
bigdevlarry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,9 @@ | ||
| { | ||
| "content": [ | ||
| { | ||
| "type": "text", | ||
| "text": "{\n \"original_count\": 4,\n \"processed_count\": 4,\n \"action\": \"sort\",\n \"original\": [\n \"apple\",\n \"banana\",\n \"cherry\",\n \"date\"\n ],\n \"processed\": [\n \"apple\",\n \"banana\",\n \"cherry\",\n \"date\"\n ],\n \"stats\": {\n \"average_length\": 5.25,\n \"shortest\": 4,\n \"longest\": 6\n }\n}" | ||
| } | ||
| ], | ||
| "isError": false | ||
| "content": [ | ||
| { | ||
| "type": "text", | ||
| "text": "{\n \"original_count\": 4,\n \"processed_count\": 4,\n \"action\": \"sort\",\n \"original\": [\n \"apple\",\n \"banana\",\n \"cherry\",\n \"date\"\n ],\n \"processed\": [\n \"apple\",\n \"banana\",\n \"cherry\",\n \"date\"\n ],\n \"stats\": {\n \"average_length\": 5.25,\n \"shortest\": 4,\n \"longest\": 6\n }\n}" | ||
| } | ||
| ], | ||
| "isError": false | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't we leave that to implementations of the handler? I'm not sure we should alter the content at any point in the sdk.