`: * the Settings › Access row that gates the server is also the key `alpaca_bot/toolkits` sees it * under, so what the filter receives names the server the way the Access tab and its filter * (`alpaca_bot/capability/mcp/`) do. * * A row is skipped, before any credential is read, when: * - its id is not one Schema::isMcpId() admits. Store hands back what is stored, not what the * schema would make of it, and Access::hook() keeps two rows' filters apart only for ids that * rule admits; * - ServerConfig::fromSettings() cannot read it (an object where a string belongs, which only a * write round the schema can store). It is skipped for every user, rather than failing the * turn of everyone the registry is asked about; * - its prefix is one Schema::isMcpPrefix() refuses (the rule, or the reserved `ability`), or * one an earlier row already holds. Schema::sanitizeMcpServers() refuses both on the way in; * this is the same rule for a row written round it, so two servers' tools still cannot reach * the model under one name. Which row holds a prefix is decided before the checks below, so it * is the earlier row whoever asks and whatever that row approves; * - it approves nothing: there would be nothing to offer, and no reason to hold its header value; * - the user fails its `mcp.` row (Access::allows(), asked of `$userId` with user_can()). * That row defaults to `manage_options`. It is not in Access::defaults(), because there is one * per server; a row that list does not name reads as `manage_options`. * For every other row the header value is put back from Secrets (the row carries the mask), and * the toolkit is handed the ClientFactory, from which McpToolkit builds its client when its tools * are first asked for. This class builds no client and asks nothing of the network; the factory * the plugin constructs builds PhpAgentsClient, which reaches the server through Mcp\Egress. * * The toolkit is handed `$userId`, the id its row was asked for, as the user its calls are * announced as, so the check and the record cannot disagree about who is asking. Its drift goes * to Drift::set(), the marker Discovery also writes. * * Nothing is kept between calls: for() reads the servers when it is asked, through the Store the * rest of the request shares. * * @since 0.6.0 */ final class Toolkits { public function __construct(private Store $store, private Access $access, private ClientFactory $clients) {} /** @return array by `mcp.`, in the order the servers are stored */ public function for(int $userId): array { $rows = $this->store->get('toolkits.mcp_servers', []); $out = []; $held = []; foreach (is_array($rows) ? $rows : [] as $row) { if (!is_array($row) || !Schema::isMcpId($row['id'] ?? null)) { continue; } /** @var string $id isMcpId() passed it */ $id = $row['id']; try { $read = ServerConfig::fromSettings($row); } catch (\Throwable) { continue; } if (!Schema::isMcpPrefix($read->prefix) || in_array($read->prefix, $held, true)) { continue; } $held[] = $read->prefix; if ($read->approved === [] || !$this->access->allows($userId, Access::MCP_PREFIX . $id, $userId)) { continue; } $server = new ServerConfig($read->id, $read->url, $read->headerName, Secrets::resolve($row), $read->prefix, $read->timeout, $read->maxBytes, $read->approved); $out[Access::MCP_PREFIX . $id] = new McpToolkit($this->clients, $server, $userId, Drift::set(...)); } return $out; } }