WP_REST_Server::READABLE, 'callback' => array( $this, 'plugins' ), 'permission_callback' => array( KeyBindingsController::class, 'allowed' ), ) ); $principal = array( 'type' => 'string', 'pattern' => '^[a-z0-9-]+:[a-z-]+(:[A-Za-z0-9_.-]+)?$', 'required' => true, ); register_rest_route( KeyBindingsController::NAMESPACE, '/mcp-bindings', array( array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'bind' ), 'permission_callback' => array( KeyBindingsController::class, 'allowed' ), 'args' => array( 'principal' => $principal, 'agent_id' => array( 'type' => 'integer', 'minimum' => 1, 'required' => true, ), ), ), array( 'methods' => WP_REST_Server::DELETABLE, 'callback' => array( $this, 'unbind' ), 'permission_callback' => array( KeyBindingsController::class, 'allowed' ), 'args' => array( 'principal' => $principal ), ), ) ); } /** * The supported plugins, their state, and their credentials. */ public function plugins(): WP_REST_Response { $bindings = PathDBindings::all(); $plugins = array(); foreach ( Adapters::all() as $adapter ) { $status = Adapters::status( $adapter ); $principals = array(); foreach ( Adapters::ABSENT === $status ? array() : $adapter->principals() as $principal ) { $agent = isset( $bindings[ $principal['id'] ] ) ? Api::agent( $bindings[ $principal['id'] ] ) : null; $principals[] = $principal + array( 'agent' => null === $agent ? null : array( 'id' => $agent['id'], 'name' => $agent['name'], 'status' => $agent['status'], ), ); } $plugins[] = array( 'id' => $adapter->id(), 'name' => $adapter->name(), 'status' => $status, 'version' => Adapters::version( $adapter ), 'tested' => $adapter->tested(), 'principals' => $principals, ); } return new WP_REST_Response( $plugins, 200 ); } /** * Bind a credential an active plugin lists. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function bind( $request ) { $principal = (string) $request->get_param( 'principal' ); $agent_id = (int) $request->get_param( 'agent_id' ); $listed = self::listed( $principal ); if ( null === $listed ) { return new WP_Error( 'adafwc_mcp_principal_not_found', __( 'No active MCP plugin lists that credential.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), array( 'status' => 404 ) ); } $agent = Api::agent( $agent_id ); if ( null === $agent ) { return new WP_Error( 'adafwc_agent_not_found', __( 'There is no agent with that id.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), array( 'status' => 404 ) ); } // The plugin runs this credential as an administrator: only an agent // an administrator confirmed runs as one (AF-25.15) may hold it. if ( true === $listed['runs_as_admin'] && true !== ( $agent['runs_as_admin'] ?? false ) ) { return new WP_Error( 'adafwc_mcp_runs_as_admin', __( 'This credential runs as an administrator. Bind it to an agent created on the Agents screen for that administrator, with "This agent runs as an administrator" confirmed - and know that such an agent can do anything an administrator can through tools that run code or edit files.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), array( 'status' => 400 ) ); } $bound = $this->bindings->agent_for( $principal ); if ( null !== $bound && $bound !== $agent_id ) { return new WP_Error( 'adafwc_mcp_already_bound', __( 'This credential is already bound to another agent. Unbind it first.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), array( 'status' => 409 ) ); } $this->bindings->bind( $principal, $agent_id ); return new WP_REST_Response( array( 'principal' => $principal, 'agent_id' => $agent_id, ), 200 ); } /** * Unbind a credential. * * @param WP_REST_Request $request Request. */ public function unbind( $request ): WP_REST_Response { $this->bindings->unbind( (string) $request->get_param( 'principal' ) ); return new WP_REST_Response( null, 204 ); } /** * A credential an active plugin lists, or null. * * @param string $principal Principal id. * @return array{id: string, label: string, runs_as_admin: bool|null}|null */ private static function listed( string $principal ): ?array { foreach ( Adapters::all() as $adapter ) { if ( Adapters::ABSENT === Adapters::status( $adapter ) ) { continue; } foreach ( $adapter->principals() as $listed ) { if ( $principal === $listed['id'] ) { return $listed; } } } return null; } }