From 77af7d78492ddd2a62fc230eeeb0549561a6e0fe Mon Sep 17 00:00:00 2001 From: Tome Pajkovski Date: Sun, 16 Mar 2025 17:54:24 +0100 Subject: [PATCH 1/2] adding the base image generation tool --- ai-command.php | 7 +++++++ src/ImageTools.php | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/ImageTools.php diff --git a/ai-command.php b/ai-command.php index 8e52402..a16c0cc 100644 --- a/ai-command.php +++ b/ai-command.php @@ -20,6 +20,13 @@ // TODO Register your tool here and add it to the collection + $image_tools = new ImageTools(); + + foreach($image_tools->get_tools() as $tool){ + $tools->add($tool); + } + + // WordPress REST calls $rest_tools = new MapRESTtoMCP(); diff --git a/src/ImageTools.php b/src/ImageTools.php new file mode 100644 index 0000000..2ad6a05 --- /dev/null +++ b/src/ImageTools.php @@ -0,0 +1,38 @@ +image_generation_tool() + ]; + } + + public function image_generation_tool() { + return new Tool( + [ + 'name' => 'generate_image', + 'description' => 'Generates an image', + 'inputSchema' => [ + 'type' => 'object', + 'properties' => [ + 'prompt' => [ + 'type' => 'string', + 'description' => 'The prompt for generating the image.', + ], + ], + 'required' => [ 'prompt' ], + ], + 'callable' => function ( $params ) use ( $client ) { + return $client->get_image_from_ai_service( $params['prompt'] ); + }, + ] + ); + } + + + +} \ No newline at end of file From 732ec2b2329ae2678434c12478deb11495a0b14c Mon Sep 17 00:00:00 2001 From: Tome Pajkovski Date: Sun, 16 Mar 2025 18:00:12 +0100 Subject: [PATCH 2/2] refactoring image generation,removing the saving of the image to a local file --- ai-command.php | 2 +- src/ImageTools.php | 11 +++++++++-- src/MCP/Client.php | 2 +- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/ai-command.php b/ai-command.php index 2af4a63..5c44d1c 100644 --- a/ai-command.php +++ b/ai-command.php @@ -23,7 +23,7 @@ // TODO Register your tool here and add it to the collection - $image_tools = new ImageTools(); + $image_tools = new ImageTools($client); foreach($image_tools->get_tools() as $tool){ $tools->add($tool); diff --git a/src/ImageTools.php b/src/ImageTools.php index 2ad6a05..d68dbe5 100644 --- a/src/ImageTools.php +++ b/src/ImageTools.php @@ -1,9 +1,15 @@ client = $client; + } public function get_tools(){ return [ @@ -26,8 +32,9 @@ public function image_generation_tool() { ], 'required' => [ 'prompt' ], ], - 'callable' => function ( $params ) use ( $client ) { - return $client->get_image_from_ai_service( $params['prompt'] ); + 'callable' => function ( $params ) { + + return $this->client->get_image_from_ai_service( $params['prompt'] ); }, ] ); diff --git a/src/MCP/Client.php b/src/MCP/Client.php index 2316bd0..a20fe9b 100644 --- a/src/MCP/Client.php +++ b/src/MCP/Client.php @@ -107,7 +107,7 @@ static function () { rename( $filename, $filename . '.' . $extension ); $filename .= '.' . $extension; - file_put_contents( $filename, $image_blob->get_binary_data() ); + // file_put_contents( $filename, $image_blob->get_binary_data() ); $image_url = $filename; $image_id = \WP_CLI\AiCommand\MediaManager::upload_to_media_library($image_url);