catalog = $catalog ?? new BlockCatalog(); } /** * Build the McpResource instance for the block-types resource. * * @return McpResource|WP_Error The built resource, or WP_Error when the * adapter rejects the configuration. * @since 1.2.0 */ public function build(): McpResource|WP_Error { return McpResource::fromArray( [ 'uri' => self::URI, 'name' => 'block-types', 'title' => __( 'Block types', 'albert-ai-butler' ), 'description' => __( 'The block types this user may use, with title, category and whether each block renders dynamically.', 'albert-ai-butler' ), 'mimeType' => 'application/json', 'handler' => [ $this, 'handler' ], 'permission' => [ $this, 'permission' ], 'annotations' => [ 'audience' => [ 'assistant' ], 'priority' => 0.5, ], ] ); } /** * Resource read handler — return the allowed-block catalog for the current user. * * Returns a single text resource content item whose body is the JSON-encoded * catalog (same data the `albert/list-block-types` tool returns). The adapter * normalises this array of content items into the resource read result. * * @return array Content items. * @since 1.2.0 */ public function handler(): array { $blocks = $this->catalog->catalog(); $payload = [ 'blocks' => $blocks, 'total' => count( $blocks ), ]; $text = wp_json_encode( $payload ); return [ [ 'uri' => self::URI, 'mimeType' => 'application/json', 'text' => is_string( $text ) ? $text : '{}', ], ]; } /** * Permission gate — mirror the tool's edit-content capability check. * * @return bool True when the current user may read the block-types resource. * @since 1.2.0 */ public function permission(): bool { return current_user_can( 'edit_posts' ); } }