diff --git a/src/AiCommand.php b/src/AiCommand.php index 72e4316..5d1753c 100644 --- a/src/AiCommand.php +++ b/src/AiCommand.php @@ -221,5 +221,73 @@ private function register_resources($server) { 'mimeType' => 'application/json', 'filePath' => './products.json', // Data will be fetched from products.json ]); + + $this->register_media_resources($server); } + + protected function register_media_resources( $server ) { + + $args = array( + 'post_type' => 'attachment', + 'post_status' => 'inherit', + 'posts_per_page' => - 1, + ); + + $media_items = get_posts( $args ); + + foreach ( $media_items as $media ) { + + $media_id = $media->ID; + $media_url = wp_get_attachment_url( $media_id ); + $media_type = get_post_mime_type( $media_id ); + $media_title = get_the_title( $media_id ); + + $server->register_resource( [ + 'name' => 'media_' . $media_id, + 'uri' => 'media://' . $media_id, + 'description' => $media_title, + 'mimeType' => $media_type, + 'callable' => function () use ( $media_id, $media_url, $media_type ) { + $data = [ + 'id' => $media_id, + 'url' => $media_url, + 'filepath' => get_attached_file( $media_id ), + 'alt' => get_post_meta( $media_id, '_wp_attachment_image_alt', true ), + 'mime_type' => $media_type, + 'metadata' => wp_get_attachment_metadata( $media_id ), + ]; + + return $data; + } + ] ); + } + + // Also register a media collection resource + $server->register_resource( [ + 'name' => 'media_collection', + 'uri' => 'data://media', + 'description' => 'Collection of all media items', + 'mimeType' => 'application/json', + 'callable' => function () { + + $args = array( + 'post_type' => 'attachment', + 'post_status' => 'inherit', + 'posts_per_page' => - 1, + 'fields' => 'ids', + ); + + $media_ids = get_posts( $args ); + $media_map = []; + + foreach ( $media_ids as $id ) { + $media_map[ $id ] = 'media://' . $id; + } + + return $media_map; + } + ] ); + + } +} \ No newline at end of file diff --git a/src/MCP/Client.php b/src/MCP/Client.php index 396430c..3d99b4d 100644 --- a/src/MCP/Client.php +++ b/src/MCP/Client.php @@ -188,17 +188,17 @@ static function () { \WP_CLI::debug( 'Making request...' . print_r( $contents, true ), 'ai' ); - if ( $service->get_service_slug() === 'openai' ) { - $model = 'gpt-4o'; - } else { - $model = 'gemini-2.0-flash-exp'; - } + // if ( $service->get_service_slug() === 'openai' ) { + // $model = 'gpt-4o'; + // } else { + // $model = 'gemini-2.0-flash-exp'; + // } $candidates = $service ->get_model( [ 'feature' => 'text-generation', - 'model' => $model, + // 'model' => $model, // 'tools' => $tools, 'capabilities' => [ AI_Capability::MULTIMODAL_INPUT, diff --git a/src/MCP/Server.php b/src/MCP/Server.php index 0bc3124..5881e3f 100644 --- a/src/MCP/Server.php +++ b/src/MCP/Server.php @@ -211,9 +211,13 @@ private function read_resource( $uri ) { ]; } - private function get_resource_data( $mcp_resource ) { - // Replace this with your actual logic to access the resource data - // based on the resource definition. + public function get_resource_data( $mcp_resource ) { + + // return the resource element based on the path + if(str_starts_with($mcp_resource, 'media://')) { + return $this->get_media_data($mcp_resource); + } + // Example: If the resource is a file, read the file contents. if ( isset( $mcp_resource['filePath'] ) ) { @@ -230,6 +234,16 @@ private function get_resource_data( $mcp_resource ) { throw new Exception( 'Unable to access resource data.' ); } + + private function get_media_data($mcp_resource) { + foreach($this->resources as $resource) { + if($resource['uri'] === $mcp_resource) { + $callback_response = $resource['callable'](); + return $callback_response; + } + } + } + // TODO: use a dedicated JSON schema validator library private function validate_input( $input, $schema ): array { $errors = []; diff --git a/src/MediaManager.php b/src/MediaManager.php new file mode 100644 index 0000000..e1998dd --- /dev/null +++ b/src/MediaManager.php @@ -0,0 +1,38 @@ + $wp_filetype['type'], + 'post_title' => sanitize_file_name($file_name), + 'post_content' => '', + 'post_status' => 'inherit' + ); + + // Insert the attachment + $attach_id = wp_insert_attachment($attachment, $new_file_path); + + // Generate attachment metadata + require_once(ABSPATH . 'wp-admin/includes/image.php'); + $attach_data = wp_generate_attachment_metadata($attach_id, $new_file_path); + wp_update_attachment_metadata($attach_id, $attach_data); + + return $attach_id; + + } +} \ No newline at end of file