Split formatting logic from Reference classes into dedicated formatter classes: - ToolResultFormatter: Formats tool execution results into Content items - PromptResultFormatter: Formats prompt results into PromptMessage items - ResourceResultFormatter: Formats resource results into ResourceContents items This refactor: - Eliminates 122 lines of code duplication between ResourceReference and ResourceTemplateReference (identical formatting logic) - Improves code organization by separating formatting concerns from capability reference management - Makes formatters independently testable and reusable - Reduces Reference classes by ~450 lines while maintaining all functionality Changes: - Create src/Capability/Formatter/* with three formatter classes - Update Reference classes to delegate to formatters instead of inline logic - Add comprehensive test coverage for all formatter classes - All existing tests pass; no behavioral changes
52 lines
1.5 KiB
PHP
52 lines
1.5 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of the official PHP MCP SDK.
|
|
*
|
|
* A collaboration between Symfony and the PHP Foundation.
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Mcp\Capability\Registry;
|
|
|
|
use Mcp\Capability\Formatter\PromptResultFormatter;
|
|
use Mcp\Schema\Content\PromptMessage;
|
|
use Mcp\Schema\Prompt;
|
|
|
|
/**
|
|
* @phpstan-import-type Handler from ElementReference
|
|
*
|
|
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
|
|
*/
|
|
class PromptReference extends ElementReference
|
|
{
|
|
/**
|
|
* @param Handler $handler
|
|
* @param array<string, class-string|object> $completionProviders
|
|
*/
|
|
public function __construct(
|
|
public readonly Prompt $prompt,
|
|
\Closure|array|string $handler,
|
|
bool $isManual = false,
|
|
public readonly array $completionProviders = [],
|
|
) {
|
|
parent::__construct($handler, $isManual);
|
|
}
|
|
|
|
/**
|
|
* Formats the raw result of a prompt generator into an array of MCP PromptMessages.
|
|
*
|
|
* @param mixed $promptGenerationResult expected: array of message structures
|
|
*
|
|
* @return PromptMessage[] array of PromptMessage objects
|
|
*
|
|
* @throws \RuntimeException if the result cannot be formatted
|
|
* @throws \JsonException if JSON encoding fails
|
|
*/
|
|
public function formatResult(mixed $promptGenerationResult): array
|
|
{
|
|
return (new PromptResultFormatter())->format($promptGenerationResult);
|
|
}
|
|
}
|