Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions src/AiCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
] );


}
}
12 changes: 6 additions & 6 deletions src/MCP/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 17 additions & 3 deletions src/MCP/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ) ) {
Expand All @@ -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 = [];
Expand Down
38 changes: 38 additions & 0 deletions src/MediaManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace WP_CLI\AiCommand;

class MediaManager {

public static function upload_to_media_library($media_path) {
// Get WordPress upload directory information
$upload_dir = wp_upload_dir();

// Get the file name from the path
$file_name = basename($media_path);

// Copy file to the upload directory
$new_file_path = $upload_dir['path'] . '/' . $file_name;
copy($media_path, $new_file_path);

// Prepare attachment data
$wp_filetype = wp_check_filetype($file_name, null);
$attachment = array(
'post_mime_type' => $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;

}
}