From f4078e694906c0be20ab43533c24c391894c12d7 Mon Sep 17 00:00:00 2001 From: Joost de Valk Date: Fri, 21 Mar 2025 14:54:20 +0100 Subject: [PATCH 1/3] Make the prompt easier to deal with --- src/MCP/Client.php | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/MCP/Client.php b/src/MCP/Client.php index 192e4e9..35d4f9d 100644 --- a/src/MCP/Client.php +++ b/src/MCP/Client.php @@ -42,7 +42,7 @@ public function send_request( $method, $params = [] ) { } if ( isset( $response['error'] ) ) { - throw new Exception( 'JSON-RPC Error: ' . $response['error']['message'], $response['error']['code'] ); + throw new Exception( 'JSON-RPC Error: ' . $response['error']['message'] . PHP_EOL . print_r( $params, 1 ), $response['error']['code'] ); } return $response['result']; @@ -122,7 +122,7 @@ static function () { break; } - return $image_id || 'no image found'; + return ( isset( $image_id ) || 'no image found' ); } // See https://github.com/felixarntz/ai-services/blob/main/docs/Accessing-AI-Services-in-PHP.md for further processing. @@ -132,8 +132,20 @@ static function () { return $image_url; } - public function call_ai_service_with_prompt( string $prompt ) { + public function call_ai_service_with_prompt( string $input_prompt ) { + if ( empty( $input_prompt ) ) { + echo "How can I help you?" . PHP_EOL; + $prompt = \cli\prompt( '', false, '> ' ); + return $this->call_ai_service_with_prompt( $prompt ) ; + } + + if ( $input_prompt === 'exit' || $input_prompt === 'quit' || $input_prompt === 'q' ) { + return "Bye!"; + } + + $prompt = apply_filters( 'ai_command_prompt', $input_prompt ); \WP_CLI::debug( "Prompt: {$prompt}", 'mcp_server' ); + $parts = new Parts(); $parts->add_text_part( $prompt ); @@ -201,13 +213,13 @@ static function () { ->get_model( [ 'feature' => 'text-generation', - // 'model' => $model, - 'tools' => $tools, - 'capabilities' => [ - AI_Capability::MULTIMODAL_INPUT, - AI_Capability::TEXT_GENERATION, - AI_Capability::FUNCTION_CALLING, - ], + 'model' => 'gpt-4o-mini', + 'tools' => $tools, + 'capabilities' => [ + AI_Capability::MULTIMODAL_INPUT, + AI_Capability::TEXT_GENERATION, + AI_Capability::FUNCTION_CALLING, + ], // 'generationConfig' => Text_Generation_Config::from_array( // array( // 'responseModalities' => array( From 7275cbd64050a8aeb76084b092e6710d12cbed1c Mon Sep 17 00:00:00 2001 From: Joost de Valk Date: Fri, 21 Mar 2025 14:56:10 +0100 Subject: [PATCH 2/3] Remove debug --- src/MCP/Client.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/MCP/Client.php b/src/MCP/Client.php index 35d4f9d..182c5ce 100644 --- a/src/MCP/Client.php +++ b/src/MCP/Client.php @@ -42,7 +42,7 @@ public function send_request( $method, $params = [] ) { } if ( isset( $response['error'] ) ) { - throw new Exception( 'JSON-RPC Error: ' . $response['error']['message'] . PHP_EOL . print_r( $params, 1 ), $response['error']['code'] ); + throw new Exception( 'JSON-RPC Error: ' . $response['error']['message'], $response['error']['code'] ); } return $response['result']; @@ -212,8 +212,7 @@ static function () { $candidates = $service ->get_model( [ - 'feature' => 'text-generation', - 'model' => 'gpt-4o-mini', + 'feature' => 'text-generation', 'tools' => $tools, 'capabilities' => [ AI_Capability::MULTIMODAL_INPUT, From 4c4ba179fb66264a0e86c01ab5fc327e30056fa5 Mon Sep 17 00:00:00 2001 From: Joost de Valk Date: Fri, 21 Mar 2025 14:57:38 +0100 Subject: [PATCH 3/3] Revert changes to model line --- src/MCP/Client.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/MCP/Client.php b/src/MCP/Client.php index 182c5ce..c45892d 100644 --- a/src/MCP/Client.php +++ b/src/MCP/Client.php @@ -212,13 +212,14 @@ static function () { $candidates = $service ->get_model( [ - 'feature' => 'text-generation', - 'tools' => $tools, - 'capabilities' => [ - AI_Capability::MULTIMODAL_INPUT, - AI_Capability::TEXT_GENERATION, - AI_Capability::FUNCTION_CALLING, - ], + 'feature' => 'text-generation', + // 'model' => $model, + 'tools' => $tools, + 'capabilities' => [ + AI_Capability::MULTIMODAL_INPUT, + AI_Capability::TEXT_GENERATION, + AI_Capability::FUNCTION_CALLING, + ], // 'generationConfig' => Text_Generation_Config::from_array( // array( // 'responseModalities' => array(