feat: Add output schema support to MCP tools (#153)
This commit is contained in:
parent
4c3e5e61c7
commit
c0938a19bd
1 changed files with 605 additions and 55 deletions
|
|
@ -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