|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the official PHP MCP SDK. |
| 5 | + * |
| 6 | + * A collaboration between Symfony and the PHP Foundation. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Mcp\Example\Server\Conformance; |
| 13 | + |
| 14 | +use Mcp\Schema\Content\Content; |
| 15 | +use Mcp\Schema\Content\EmbeddedResource; |
| 16 | +use Mcp\Schema\Content\ImageContent; |
| 17 | +use Mcp\Schema\Content\PromptMessage; |
| 18 | +use Mcp\Schema\Content\TextContent; |
| 19 | +use Mcp\Schema\Content\TextResourceContents; |
| 20 | +use Mcp\Schema\Enum\Role; |
| 21 | +use Mcp\Server\Protocol; |
| 22 | +use Mcp\Server\RequestContext; |
| 23 | + |
| 24 | +final class Elements |
| 25 | +{ |
| 26 | + // Sample base64 encoded 1x1 red PNG pixel for testing |
| 27 | + public const TEST_IMAGE_BASE64 = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8DwHwAFBQIAX8jx0gAAAABJRU5ErkJggg=='; |
| 28 | + // Sample base64 encoded minimal WAV file for testing |
| 29 | + public const TEST_AUDIO_BASE64 = 'UklGRiYAAABXQVZFZm10IBAAAAABAAEAQB8AAAB9AAACABAAZGF0YQIAAAA='; |
| 30 | + |
| 31 | + /** |
| 32 | + * @return Content[] |
| 33 | + */ |
| 34 | + public function toolMultipleTypes(): array |
| 35 | + { |
| 36 | + return [ |
| 37 | + new TextContent('Multiple content types test:'), |
| 38 | + new ImageContent(self::TEST_IMAGE_BASE64, 'image/png'), |
| 39 | + EmbeddedResource::fromText( |
| 40 | + 'test://mixed-content-resource', |
| 41 | + '{ "test" = "data", "value" = 123 }', |
| 42 | + 'application/json', |
| 43 | + ), |
| 44 | + ]; |
| 45 | + } |
| 46 | + |
| 47 | + public function toolWithLogging(RequestContext $context): string |
| 48 | + { |
| 49 | + $logger = $context->getClientLogger(); |
| 50 | + |
| 51 | + $logger->info('Tool execution started'); |
| 52 | + $logger->info('Tool processing data'); |
| 53 | + $logger->info('Tool execution completed'); |
| 54 | + |
| 55 | + return 'Tool with logging executed successfully'; |
| 56 | + } |
| 57 | + |
| 58 | + public function toolWithProgress(RequestContext $context): ?string |
| 59 | + { |
| 60 | + $client = $context->getClientGateway(); |
| 61 | + |
| 62 | + $client->progress(0, 100, 'Completed step 0 of 100'); |
| 63 | + $client->progress(50, 100, 'Completed step 0 of 100'); |
| 64 | + $client->progress(100, 100, 'Completed step 0 of 100'); |
| 65 | + |
| 66 | + $meta = $context->getSession()->get(Protocol::SESSION_ACTIVE_REQUEST_META, []); |
| 67 | + |
| 68 | + return $meta['progressToken'] ?? null; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @param string $prompt The prompt to send to the LLM |
| 73 | + */ |
| 74 | + public function toolWithSampling(RequestContext $context, string $prompt): string |
| 75 | + { |
| 76 | + $result = $context->getClientGateway()->sample($prompt, 100); |
| 77 | + |
| 78 | + return \sprintf('LLM response: %s', $result->content instanceof TextContent ? trim((string) $result->content->text) : ''); |
| 79 | + } |
| 80 | + |
| 81 | + public function resourceTemplate(string $id): TextResourceContents |
| 82 | + { |
| 83 | + return new TextResourceContents( |
| 84 | + uri: 'test://template/{id}/data', |
| 85 | + mimeType: 'application/json', |
| 86 | + text: json_encode([ |
| 87 | + 'id' => $id, |
| 88 | + 'templateTest' => true, |
| 89 | + 'data' => \sprintf('Data for ID: %s', $id), |
| 90 | + ]), |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @param string $arg1 First test argument |
| 96 | + * @param string $arg2 Second test argument |
| 97 | + * |
| 98 | + * @return PromptMessage[] |
| 99 | + */ |
| 100 | + public function promptWithArguments(string $arg1, string $arg2): array |
| 101 | + { |
| 102 | + return [ |
| 103 | + new PromptMessage(Role::User, new TextContent(\sprintf('Prompt with arguments: arg1="%s", arg2="%s"', $arg1, $arg2))), |
| 104 | + ]; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @param string $resourceUri URI of the resource to embed |
| 109 | + * |
| 110 | + * @return PromptMessage[] |
| 111 | + */ |
| 112 | + public function promptWithEmbeddedResource(string $resourceUri): array |
| 113 | + { |
| 114 | + return [ |
| 115 | + new PromptMessage(Role::User, EmbeddedResource::fromText($resourceUri, 'Embedded resource content for testing.')), |
| 116 | + new PromptMessage(Role::User, new TextContent('Please process the embedded resource above.')), |
| 117 | + ]; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * @return PromptMessage[] |
| 122 | + */ |
| 123 | + public function promptWithImage(): array |
| 124 | + { |
| 125 | + return [ |
| 126 | + new PromptMessage(Role::User, new ImageContent(self::TEST_IMAGE_BASE64, 'image/png')), |
| 127 | + new PromptMessage(Role::User, new TextContent('Please analyze the image above.')), |
| 128 | + ]; |
| 129 | + } |
| 130 | +} |
0 commit comments