|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Matomo - free/libre analytics platform |
| 5 | + * |
| 6 | + * @link https://matomo.org |
| 7 | + * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | + */ |
| 9 | + |
| 10 | +declare(strict_types=1); |
| 11 | + |
| 12 | +namespace Piwik\Plugins\McpServer; |
| 13 | + |
| 14 | +use Piwik\Piwik; |
| 15 | +use Piwik\Plugins\McpServer\Contracts\McpTool; |
| 16 | +use Piwik\Plugins\McpServer\McpTools\ApiCallCreate; |
| 17 | +use Piwik\Plugins\McpServer\McpTools\ApiCallDelete; |
| 18 | +use Piwik\Plugins\McpServer\McpTools\ApiCallFull; |
| 19 | +use Piwik\Plugins\McpServer\McpTools\ApiCallRead; |
| 20 | +use Piwik\Plugins\McpServer\McpTools\ApiCallUpdate; |
| 21 | +use Piwik\Plugins\McpServer\McpTools\ApiGet; |
| 22 | +use Piwik\Plugins\McpServer\McpTools\ApiList; |
| 23 | +use Piwik\Plugins\McpServer\McpTools\DimensionGet; |
| 24 | +use Piwik\Plugins\McpServer\McpTools\DimensionList; |
| 25 | +use Piwik\Plugins\McpServer\McpTools\GoalGet; |
| 26 | +use Piwik\Plugins\McpServer\McpTools\GoalList; |
| 27 | +use Piwik\Plugins\McpServer\McpTools\ReportList; |
| 28 | +use Piwik\Plugins\McpServer\McpTools\ReportMetadata; |
| 29 | +use Piwik\Plugins\McpServer\McpTools\ReportProcessed; |
| 30 | +use Piwik\Plugins\McpServer\McpTools\SegmentGet; |
| 31 | +use Piwik\Plugins\McpServer\McpTools\SegmentList; |
| 32 | +use Piwik\Plugins\McpServer\McpTools\SiteGet; |
| 33 | +use Piwik\Plugins\McpServer\McpTools\SiteList; |
| 34 | +use Piwik\Plugins\McpServer\McpTools\SiteSearch; |
| 35 | +use Psr\Container\ContainerInterface; |
| 36 | + |
| 37 | +/** |
| 38 | + * Resolves the set of MCP tools exposed by the server. |
| 39 | + * |
| 40 | + * Built-in tools shipped by the McpServer plugin are listed in |
| 41 | + * {@see McpToolsProvider::BUILTIN_TOOL_CLASSES}. Other plugins extend or |
| 42 | + * restrict the set by listening to the {@hook McpServer.addTools} and |
| 43 | + * {@hook McpServer.filterTools} events. |
| 44 | + */ |
| 45 | +class McpToolsProvider |
| 46 | +{ |
| 47 | + /** |
| 48 | + * Tools shipped by the McpServer plugin. Held as class-strings so the |
| 49 | + * container resolves them at runtime — see resolveBuiltinTools(). |
| 50 | + * |
| 51 | + * @var list<class-string<McpTool>> |
| 52 | + */ |
| 53 | + private const BUILTIN_TOOL_CLASSES = [ |
| 54 | + ApiCallCreate::class, |
| 55 | + ApiCallDelete::class, |
| 56 | + ApiCallFull::class, |
| 57 | + ApiCallRead::class, |
| 58 | + ApiCallUpdate::class, |
| 59 | + ApiGet::class, |
| 60 | + ApiList::class, |
| 61 | + DimensionGet::class, |
| 62 | + DimensionList::class, |
| 63 | + GoalGet::class, |
| 64 | + GoalList::class, |
| 65 | + ReportList::class, |
| 66 | + ReportMetadata::class, |
| 67 | + ReportProcessed::class, |
| 68 | + SegmentGet::class, |
| 69 | + SegmentList::class, |
| 70 | + SiteGet::class, |
| 71 | + SiteList::class, |
| 72 | + SiteSearch::class, |
| 73 | + ]; |
| 74 | + |
| 75 | + public function __construct(private ContainerInterface $container) |
| 76 | + { |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Return all MCP tools that should be considered for registration. |
| 81 | + * |
| 82 | + * The list begins with the McpServer built-in tools, lets other plugins |
| 83 | + * append their own via {@hook McpServer.addTools}, then lets plugins |
| 84 | + * restrict the result via {@hook McpServer.filterTools}. |
| 85 | + * |
| 86 | + * Each entry MUST be an instance of {@see McpTool}; entries that fail |
| 87 | + * this check trigger a LogicException so misconfigurations surface at |
| 88 | + * boot rather than at tool-call time. |
| 89 | + * |
| 90 | + * @return list<McpTool> |
| 91 | + */ |
| 92 | + public function getAllTools(): array |
| 93 | + { |
| 94 | + $tools = $this->resolveBuiltinTools(); |
| 95 | + |
| 96 | + /** |
| 97 | + * Triggered to add custom MCP tools that ship outside the McpServer |
| 98 | + * plugin. Tools added here are subsequently passed through the |
| 99 | + * {@hook McpServer.filterTools} event before being registered. |
| 100 | + * |
| 101 | + * **Example** |
| 102 | + * |
| 103 | + * public function addMcpTools(&$tools) |
| 104 | + * { |
| 105 | + * $tools[] = StaticContainer::get(MyCustomMcpTool::class); |
| 106 | + * } |
| 107 | + * |
| 108 | + * @param McpTool[] &$tools An array of MCP tool instances. |
| 109 | + */ |
| 110 | + Piwik::postEvent('McpServer.addTools', [&$tools]); |
| 111 | + |
| 112 | + /** |
| 113 | + * Triggered to filter or restrict the set of MCP tools that will be |
| 114 | + * registered with the server. Use this to hide tools conditionally |
| 115 | + * (for example based on plugin state or system settings). |
| 116 | + * |
| 117 | + * **Example** |
| 118 | + * |
| 119 | + * public function filterMcpTools(&$tools) |
| 120 | + * { |
| 121 | + * foreach ($tools as $index => $tool) { |
| 122 | + * if ($tool->getName() === 'matomo_site_list') { |
| 123 | + * unset($tools[$index]); |
| 124 | + * } |
| 125 | + * } |
| 126 | + * } |
| 127 | + * |
| 128 | + * @param McpTool[] &$tools An array of MCP tool instances. |
| 129 | + */ |
| 130 | + Piwik::postEvent('McpServer.filterTools', [&$tools]); |
| 131 | + |
| 132 | + // Listeners can mutate $tools to anything; check each entry at runtime |
| 133 | + // so a misconfigured plugin fails at boot rather than at tool-call time. |
| 134 | + /** @var array<int|string, mixed> $contributed */ |
| 135 | + $contributed = $tools; |
| 136 | + $result = []; |
| 137 | + foreach ($contributed as $tool) { |
| 138 | + if (!$tool instanceof McpTool) { |
| 139 | + throw new \LogicException(sprintf( |
| 140 | + 'McpServer tool list contained a non-McpTool entry: %s.', |
| 141 | + get_debug_type($tool), |
| 142 | + )); |
| 143 | + } |
| 144 | + $result[] = $tool; |
| 145 | + } |
| 146 | + |
| 147 | + return $result; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * @return list<McpTool> |
| 152 | + */ |
| 153 | + private function resolveBuiltinTools(): array |
| 154 | + { |
| 155 | + $tools = []; |
| 156 | + foreach (self::BUILTIN_TOOL_CLASSES as $toolClass) { |
| 157 | + $tool = $this->container->get($toolClass); |
| 158 | + if (!$tool instanceof McpTool) { |
| 159 | + throw new \LogicException(sprintf( |
| 160 | + '%s did not resolve to an McpTool instance.', |
| 161 | + $toolClass, |
| 162 | + )); |
| 163 | + } |
| 164 | + $tools[] = $tool; |
| 165 | + } |
| 166 | + |
| 167 | + return $tools; |
| 168 | + } |
| 169 | +} |
0 commit comments