headerName !== '' && $server->headerValue !== '') { // Exactly the strings PHP turns into an int key: "123" and "-1", not "0123" or "-0". if ((string) (int) $server->headerName === $server->headerName) { /* translators: %s: the header name an administrator gave an MCP server, e.g. 123 */ throw new McpUnavailable(sprintf(__('This server was not contacted: its header name, %s, is a whole number, and the MCP client would send the header\'s value in the name\'s place. Give the header a name with a letter in it.', 'alpaca-bot'), $server->headerName)); } $headers[$server->headerName] = $server->headerValue; } // Named arguments: the library only ever adds trailing optional parameters, and // protocolVersion already sits between maxResponseBytes and maxResultBytes. $library = new McpServer( url: $server->url, headers: $headers, timeout: $server->timeout, maxResponseBytes: $server->maxBytes, ); return new self(new McpClient($library, $egress->client($server), $sessions)); } public function __construct(private McpClientInterface $client) {} public function listTools(): array { try { $listed = $this->client->listTools(); } catch (\RuntimeException $e) { throw self::unavailable($e); } return array_map( static fn(McpToolDefinition $d): ToolDefinition => new ToolDefinition($d->name, $d->description, $d->inputSchema, $d->annotations, $d->title), $listed, ); } public function callTool(string $name, array $arguments): ToolResult { try { return $this->client->callTool($name, $arguments); } catch (\RuntimeException $e) { throw self::unavailable($e); } } /** The plugin's sentence for `$e`'s class of failure (the class docblock says why none of `$e`'s text). */ private static function unavailable(\RuntimeException $e): McpUnavailable { $message = match (true) { /* translators: %d: an HTTP status, 401 or 403 */ $e instanceof McpAuthException => sprintf(__('The MCP server refused the credentials it was sent (HTTP %d).', 'alpaca-bot'), $e->status), /* translators: %d: an HTTP status, e.g. 302 */ $e instanceof McpRedirectException => sprintf(__('The MCP server answered with a redirect (HTTP %d), which is not followed.', 'alpaca-bot'), $e->status), $e instanceof McpTransportException => __('The MCP server could not be reached, or gave no usable HTTP answer: a network error, a timeout, an answer over the byte cap or an HTTP error status.', 'alpaca-bot'), $e instanceof McpUnsupportedVersionException => __('The MCP server speaks no protocol version this client does.', 'alpaca-bot'), /* translators: %d: a JSON-RPC error code, e.g. -32601 */ $e instanceof McpRpcException => sprintf(__('The MCP server answered with JSON-RPC error %d.', 'alpaca-bot'), $e->rpcCode), $e instanceof McpProtocolException => __('The MCP server answered, but not in a way this client can use.', 'alpaca-bot'), default => __('The MCP request failed.', 'alpaca-bot'), }; return new McpUnavailable($message, 0, $e); } }