|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace KLP\KlpMcpServer\Services\ToolService\Examples; |
| 4 | + |
| 5 | +use KLP\KlpMcpServer\Services\ToolService\Annotation\ToolAnnotation; |
| 6 | +use KLP\KlpMcpServer\Services\ToolService\BaseToolInterface; |
| 7 | +use KLP\KlpMcpServer\Services\ToolService\Result\TextToolResult; |
| 8 | +use KLP\KlpMcpServer\Services\ToolService\Result\ToolResultInterface; |
| 9 | +use KLP\KlpMcpServer\Services\ToolService\Schema\PropertyType; |
| 10 | +use KLP\KlpMcpServer\Services\ToolService\Schema\SchemaBuilder; |
| 11 | +use KLP\KlpMcpServer\Services\ToolService\Schema\SchemaProperty; |
| 12 | +use KLP\KlpMcpServer\Services\ToolService\Schema\StructuredSchema; |
| 13 | + |
| 14 | +/** |
| 15 | + * Example tool demonstrating how to create outputSchema with arrays of objects. |
| 16 | + * |
| 17 | + * This tool shows how to use the enhanced SchemaProperty and SchemaBuilder |
| 18 | + * to create complex JSON Schema structures that include arrays of objects. |
| 19 | + */ |
| 20 | +class SearchResultsTool implements BaseToolInterface |
| 21 | +{ |
| 22 | + public function getName(): string |
| 23 | + { |
| 24 | + return 'search_results'; |
| 25 | + } |
| 26 | + |
| 27 | + public function getDescription(): string |
| 28 | + { |
| 29 | + return 'Example tool that returns search results in a structured array format'; |
| 30 | + } |
| 31 | + |
| 32 | + public function getInputSchema(): StructuredSchema |
| 33 | + { |
| 34 | + return new StructuredSchema( |
| 35 | + new SchemaProperty( |
| 36 | + name: 'query', |
| 37 | + type: PropertyType::STRING, |
| 38 | + description: 'Search query to process', |
| 39 | + required: true |
| 40 | + ), |
| 41 | + new SchemaProperty( |
| 42 | + name: 'limit', |
| 43 | + type: PropertyType::INTEGER, |
| 44 | + description: 'Maximum number of results to return', |
| 45 | + default: '10' |
| 46 | + ) |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + public function getOutputSchema(): ?StructuredSchema |
| 51 | + { |
| 52 | + return new StructuredSchema( |
| 53 | + // Using SchemaBuilder for array of objects |
| 54 | + SchemaBuilder::arrayOfObjects( |
| 55 | + 'results', |
| 56 | + [ |
| 57 | + 'id' => ['type' => 'string'], |
| 58 | + 'title' => ['type' => 'string'], |
| 59 | + 'url' => ['type' => 'string'], |
| 60 | + 'snippet' => ['type' => 'string'], |
| 61 | + 'score' => ['type' => 'number'] |
| 62 | + ], |
| 63 | + ['id', 'title'], // id and title are required |
| 64 | + 'Array of search results' |
| 65 | + ), |
| 66 | + |
| 67 | + // Additional metadata object |
| 68 | + SchemaBuilder::nestedObject( |
| 69 | + 'metadata', |
| 70 | + [ |
| 71 | + 'totalResults' => ['type' => 'integer'], |
| 72 | + 'searchTime' => ['type' => 'number'], |
| 73 | + 'query' => ['type' => 'string'] |
| 74 | + ], |
| 75 | + ['totalResults', 'query'], |
| 76 | + 'Search metadata' |
| 77 | + ), |
| 78 | + |
| 79 | + // Array of suggestion strings |
| 80 | + SchemaBuilder::arrayOfPrimitives( |
| 81 | + 'suggestions', |
| 82 | + 'string', |
| 83 | + 'Alternative search suggestions' |
| 84 | + ) |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + public function execute(array $arguments): ToolResultInterface |
| 89 | + { |
| 90 | + $query = $arguments['query']; |
| 91 | + $limit = $arguments['limit'] ?? 10; |
| 92 | + |
| 93 | + // Simulate search results |
| 94 | + $results = []; |
| 95 | + for ($i = 1; $i <= min($limit, 5); $i++) { |
| 96 | + $results[] = [ |
| 97 | + 'id' => "result_$i", |
| 98 | + 'title' => "Search Result $i for: $query", |
| 99 | + 'url' => "https://example.com/result/$i", |
| 100 | + 'snippet' => "This is a snippet for result $i matching your search query.", |
| 101 | + 'score' => 1.0 - ($i * 0.1) |
| 102 | + ]; |
| 103 | + } |
| 104 | + |
| 105 | + $response = [ |
| 106 | + 'results' => $results, |
| 107 | + 'metadata' => [ |
| 108 | + 'totalResults' => count($results), |
| 109 | + 'searchTime' => 0.125, |
| 110 | + 'query' => $query |
| 111 | + ], |
| 112 | + 'suggestions' => [ |
| 113 | + "$query tips", |
| 114 | + "$query tutorial", |
| 115 | + "$query examples" |
| 116 | + ] |
| 117 | + ]; |
| 118 | + |
| 119 | + return new TextToolResult(json_encode($response, JSON_PRETTY_PRINT)); |
| 120 | + } |
| 121 | + |
| 122 | + public function getAnnotations(): ToolAnnotation |
| 123 | + { |
| 124 | + return new ToolAnnotation(); |
| 125 | + } |
| 126 | +} |
0 commit comments