feat: Add output schema support to MCP tools (#153)
This commit is contained in:
parent
4c3e5e61c7
commit
c0938a19bd
2 changed files with 605 additions and 55 deletions
|
|
@ -47,7 +47,8 @@ final class ArrayLoader implements LoaderInterface
|
|||
* description: ?string,
|
||||
* annotations: ?ToolAnnotations,
|
||||
* icons: ?Icon[],
|
||||
* meta: ?array<string, mixed>
|
||||
* meta: ?array<string, mixed>,
|
||||
* outputSchema: ?array<string, mixed>
|
||||
* }[] $tools
|
||||
* @param array{
|
||||
* handler: Handler,
|
||||
|
|
@ -117,6 +118,7 @@ final class ArrayLoader implements LoaderInterface
|
|||
annotations: $data['annotations'] ?? null,
|
||||
icons: $data['icons'] ?? null,
|
||||
meta: $data['meta'] ?? null,
|
||||
outputSchema: $data['outputSchema'] ?? null,
|
||||
);
|
||||
$registry->registerTool($tool, $data['handler'], true);
|
||||
|
||||
|
|
|
|||
|
|
@ -111,4 +111,33 @@ class ToolReference extends ElementReference
|
|||
|
||||
return [new TextContent($jsonResult)];
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts structured content from a tool result using the output schema.
|
||||
*
|
||||
* @param mixed $toolExecutionResult the raw value returned by the tool's PHP method
|
||||
*
|
||||
* @return array<string, mixed>|null the structured content, or null if not extractable
|
||||
*
|
||||
* @throws \JsonException if JSON encoding fails for non-Content array/object results
|
||||
*/
|
||||
public function extractStructuredContent(mixed $toolExecutionResult): ?array
|
||||
{
|
||||
if (\is_array($toolExecutionResult)) {
|
||||
return $toolExecutionResult;
|
||||
}
|
||||
|
||||
if (\is_object($toolExecutionResult) && !($toolExecutionResult instanceof Content)) {
|
||||
$jsonResult = json_encode(
|
||||
$toolExecutionResult,
|
||||
\JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE | \JSON_THROW_ON_ERROR | \JSON_INVALID_UTF8_SUBSTITUTE
|
||||
);
|
||||
|
||||
return json_decode(
|
||||
$jsonResult, true, 512, \JSON_THROW_ON_ERROR
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue