store->get('toolkits.mcp_servers', []); foreach (is_array($rows) ? $rows : [] as $row) { if (is_array($row) && ($row['id'] ?? null) === $id) { $row['header_value'] = Secrets::resolve($row); try { return ServerConfig::fromSettings($row); } catch (\Throwable) { return null; } } } return null; } /** * One row per tool the server lists, in the order it lists them, and the names of the changed * ones recorded through Drift::set(), each once, which clears the marker when there are none. * * A failure to build the client or to list the server is McpUnavailable, whatever was thrown, * so a caller that shows it, as the Discover route does, has one class to catch and never * answers an error of its own. The catch is \Throwable because both halves can throw beyond * RuntimeException: the builder is whatever closure the factory was handed, and the default * one reaches McpServer's constructor and PinnedHttpClient's, each of which can throw * \InvalidArgumentException; PhpAgentsClient turns only RuntimeException into McpUnavailable, * and php-agents' McpException docblock names a TypeError and an Error its client can raise * besides. What the message says: * * - An McpUnavailable is handed on as it is. * - An AddressRefused's message is kept. It is the plugin's own sentence, written for the * administrator who typed the URL (AddressRefused's docblock): Egress's or AddressPin's, * naming at most the host and an address it refuses. * - Anything else gets the plugin's sentence and none of its own text, which is not the * plugin's and could quote the URL, its query string included. * * The two it builds keep what was thrown as `previous`. * * @return list * @throws McpUnavailable when the client cannot be built or the server cannot be listed; the drift marker is left as it was */ public function tools(#[\SensitiveParameter] ServerConfig $server): array { $rows = []; $changed = []; try { $listed = $this->clients->for($server)->listTools(); } catch (McpUnavailable $e) { throw $e; } catch (AddressRefused $e) { throw new McpUnavailable($e->getMessage(), 0, $e); } catch (\Throwable $e) { throw new McpUnavailable(__('The MCP client failed in a way this plugin does not recognise, so the server was not listed.', 'alpaca-bot'), 0, $e); } $counts = array_count_values(array_map(static fn(ToolDefinition $d): string => $d->name, $listed)); foreach ($listed as $definition) { $fingerprint = $definition->fingerprint(); $pinned = $server->approved[$definition->name] ?? null; $state = match ($pinned) { null => 'new', $fingerprint => 'approved', default => 'changed', }; $repeated = $counts[$definition->name] > 1; if ($state === 'changed' && !in_array($definition->name, $changed, true)) { $changed[] = $definition->name; } $rows[] = [ 'definition' => $definition, 'fingerprint' => $fingerprint, 'state' => $state, 'ticked' => !$repeated && ($state === 'approved' || ($state === 'new' && !$definition->destructive())), ]; } Drift::set($server->id, $changed); return $rows; } }