-
Notifications
You must be signed in to change notification settings - Fork 104
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', | ||
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
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. suggestion: /**
* @param ToolData $data
*/
public static function fromArray(array $data): self
{
if (empty($data['name']) || !\is_string($data['name'])) {
throw new InvalidArgumentException('Invalid or missing "name" in Tool data.');
}
if (!isset($data['inputSchema']) || !\is_array($data['inputSchema'])) {
throw new InvalidArgumentException('Invalid or missing "inputSchema" in Tool data.');
}
if (!isset($data['inputSchema']['type']) || 'object' !== $data['inputSchema']['type']) {
throw new InvalidArgumentException('Tool inputSchema must be of type "object".');
}
$inputSchema = self::normalizeSchemaProperties($data['inputSchema']);
$outputSchema = null;
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".');
}
$outputSchema = self::normalizeSchemaProperties($data['outputSchema']);
}
return new self(
$data['name'],
$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,
$outputSchema
);
}
/**
* Normalize schema properties: convert empty properties array to stdClass
*
* @param array<string, mixed> $schema
*
* @return array{properties: object}&array<string, mixed> $schema
*/
private static function normalizeSchemaProperties(array $schema): array
{
if (isset($schema['properties']) && \is_array($schema['properties']) && empty($schema['properties'])) {
$schema['properties'] = new \stdClass();
}
return $schema;
}
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. @soyuka If we change the $data['inputSchema'] -> $inputSchema Parameter #2 $inputSchema of class Mcp\Schema\Tool constructor expects array{type: 'object', properties: array<string, mixed>, required: array|null}, array{prop Similar to the outputSchema as well .. The error is visible when you run |
||
|
|
||
| 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; | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.