@@ -46,19 +46,19 @@ class AiCommand extends WP_CLI_Command {
4646 */
4747 public function __invoke ( $ args , $ assoc_args ) {
4848 $ server = new MCP \Server ();
49- $ client = new MCP \Client ($ server );
49+ $ client = new MCP \Client ( $ server );
5050
51- $ this ->register_tools ($ server , $ client );
51+ $ this ->register_tools ( $ server , $ client );
5252
53- $ this ->register_resources ($ server );
53+ $ this ->register_resources ( $ server );
5454
5555 $ result = $ client ->call_ai_service_with_prompt ( $ args [0 ] );
5656
5757 WP_CLI ::success ( $ result );
5858 }
5959
6060 // Register tools for AI processing
61- private function register_tools ($ server , $ client ) {
61+ private function register_tools ( $ server , $ client ) {
6262 $ server ->register_tool (
6363 [
6464 'name ' => 'list_tools ' ,
@@ -206,134 +206,141 @@ private function register_tools($server, $client) {
206206 ]
207207 );
208208
209+ // $server->register_tool(
210+ // [
211+ // 'name' => 'generate_image',
212+ // 'description' => 'Generates an image.',
213+ // 'inputSchema' => [
214+ // 'type' => 'object',
215+ // 'properties' => [
216+ // 'prompt' => [
217+ // 'type' => 'string',
218+ // 'description' => 'The prompt for generating the image.',
219+ // ],
220+ // ],
221+ // 'required' => [ 'prompt' ],
222+ // ],
223+ // 'callable' => function ( $params ) use ( $client ) {
224+ // return $client->get_image_from_ai_service( $params['prompt'] );
225+ // },
226+ // ]
227+ // );
228+
209229 $ server ->register_tool (
210230 [
211- 'name ' => 'generate_image ' ,
212- 'description ' => 'Generates an image . ' ,
231+ 'name ' => 'fetch_wp_community_events ' ,
232+ 'description ' => 'Fetches upcoming WordPress community events near a specified city or the user \' s current location. If no events are found in the exact location, nearby events within a specific radius will be considered . ' ,
213233 'inputSchema ' => [
214234 'type ' => 'object ' ,
215235 'properties ' => [
216- 'prompt ' => [
236+ 'location ' => [
217237 'type ' => 'string ' ,
218- 'description ' => 'The prompt for generating the image . ' ,
238+ 'description ' => 'City name or "near me" for auto-detected location. If no events are found in the exact location, the tool will also consider nearby events within a specified radius (default: 100 km) . ' ,
219239 ],
220240 ],
221- 'required ' => [ 'prompt ' ],
241+ 'required ' => [ 'location ' ], // We only require the location
222242 ],
223- 'callable ' => function ( $ params ) use ( $ client ) {
224- return $ client ->get_image_from_ai_service ( $ params ['prompt ' ] );
225- },
226- ]
227- );
243+ 'callable ' => function ( $ params ) {
244+ // Default user ID is 0
245+ $ user_id = 0 ;
246+
247+ // Get the location from the parameters (already supplied in the prompt)
248+ $ location_input = strtolower ( trim ( $ params ['location ' ] ) );
249+
250+ // Manually include the WP_Community_Events class if it's not loaded
251+ if ( ! class_exists ( 'WP_Community_Events ' ) ) {
252+ require_once ABSPATH . 'wp-admin/includes/class-wp-community-events.php ' ;
253+ }
254+
255+ // Determine location for the WP_Community_Events class
256+ $ location = null ;
257+ if ( $ location_input !== 'near me ' ) {
258+ // Provide city name (WP will resolve coordinates)
259+ $ location = [
260+ 'description ' => $ location_input ,
261+ ];
262+ }
228263
229- $ server ->register_tool (
230- [
231- 'name ' => 'fetch_wp_community_events ' ,
232- 'description ' => 'Fetches upcoming WordPress community events near a specified city or the user \'s current location. If no events are found in the exact location, nearby events within a specific radius will be considered. ' ,
233- 'inputSchema ' => [
234- 'type ' => 'object ' ,
235- 'properties ' => [
236- 'location ' => [
237- 'type ' => 'string ' ,
238- 'description ' => 'City name or "near me" for auto-detected location. If no events are found in the exact location, the tool will also consider nearby events within a specified radius (default: 100 km). ' ,
239- ],
240- ],
241- 'required ' => [ 'location ' ], // We only require the location
242- ],
243- 'callable ' => function ( $ params ) {
244- // Default user ID is 0
245- $ user_id = 0 ;
246-
247- // Get the location from the parameters (already supplied in the prompt)
248- $ location_input = strtolower ( trim ( $ params ['location ' ] ) );
249-
250- // Manually include the WP_Community_Events class if it's not loaded
251- if ( ! class_exists ( 'WP_Community_Events ' ) ) {
252- require_once ABSPATH . 'wp-admin/includes/class-wp-community-events.php ' ;
253- }
254-
255- // Determine location for the WP_Community_Events class
256- $ location = null ;
257- if ( $ location_input !== 'near me ' ) {
258- // Provide city name (WP will resolve coordinates)
259- $ location = [
260- 'description ' => $ location_input ,
261- ];
262- }
263-
264- // Instantiate WP_Community_Events with user ID (0) and optional location
265- $ events_instance = new WP_Community_Events ( $ user_id , $ location );
266-
267- // Get events from WP_Community_Events
268- $ events = $ events_instance ->get_events ($ location_input );
269-
270- // Check for WP_Error
271- if ( is_wp_error ( $ events ) ) {
272- return [ 'error ' => $ events ->get_error_message () ];
273- }
264+ // Instantiate WP_Community_Events with user ID (0) and optional location
265+ $ events_instance = new WP_Community_Events ( $ user_id , $ location );
266+
267+ // Get events from WP_Community_Events
268+ $ events = $ events_instance ->get_events ( $ location_input );
269+
270+ // Check for WP_Error
271+ if ( is_wp_error ( $ events ) ) {
272+ return [ 'error ' => $ events ->get_error_message () ];
273+ }
274274
275275 // If no events found
276- if ( empty ( $ events ['events ' ] ) ) {
277- return [ 'message ' => 'No events found near ' . ( $ location_input === 'near me ' ? 'your location ' : $ location_input ) ];
278- }
276+ if ( empty ( $ events ['events ' ] ) ) {
277+ return [ 'message ' => 'No events found near ' . ( $ location_input === 'near me ' ? 'your location ' : $ location_input ) ];
278+ }
279279
280280 // Format and return the events correctly
281- $ formatted_events = array_map ( function ( $ event ) {
282- // Log event details to ensure properties are accessible
283- error_log ( 'Event details: ' . print_r ( $ event , true ) );
281+ $ formatted_events = array_map (
282+ function ( $ event ) {
283+ // Log event details to ensure properties are accessible
284+ error_log ( 'Event details: ' . print_r ( $ event , true ) );
284285
285- // Initialize a formatted event string
286- $ formatted_event = '' ;
286+ // Initialize a formatted event string
287+ $ formatted_event = '' ;
287288
288- // Format event title
289- if ( isset ( $ event ['title ' ] ) ) {
289+ // Format event title
290+ if ( isset ( $ event ['title ' ] ) ) {
290291 $ formatted_event .= $ event ['title ' ] . "\n" ;
291- }
292+ }
292293
293- // Format the date nicely
294- $ formatted_event .= ' - Date: ' . ( isset ( $ event ['date ' ] ) ? date ( 'F j, Y g:i A ' , strtotime ( $ event ['date ' ] ) ) : 'No date available ' ) . "\n" ;
294+ // Format the date nicely
295+ $ formatted_event .= ' - Date: ' . ( isset ( $ event ['date ' ] ) ? date ( 'F j, Y g:i A ' , strtotime ( $ event ['date ' ] ) ) : 'No date available ' ) . "\n" ;
295296
296- // Format the location
297- if ( isset ( $ event ['location ' ]['location ' ] ) ) {
298- $ formatted_event .= ' - Location: ' . $ event ['location ' ]['location ' ] . "\n" ;
299- }
297+ // Format the location
298+ if ( isset ( $ event ['location ' ]['location ' ] ) ) {
299+ $ formatted_event .= ' - Location: ' . $ event ['location ' ]['location ' ] . "\n" ;
300+ }
300301
301- // Format the event URL
302- $ formatted_event .= isset ( $ event ['url ' ] ) ? ' - URL: ' . $ event ['url ' ] . "\n" : '' ;
302+ // Format the event URL
303+ $ formatted_event .= isset ( $ event ['url ' ] ) ? ' - URL: ' . $ event ['url ' ] . "\n" : '' ;
303304
304- return $ formatted_event ;
305- }, $ events ['events ' ] );
305+ return $ formatted_event ;
306+ },
307+ $ events ['events ' ]
308+ );
306309
307310 // Combine the formatted events into a single string
308- $ formatted_events_output = implode ("\n" , $ formatted_events );
311+ $ formatted_events_output = implode ( "\n" , $ formatted_events );
309312
310313 // Return the formatted events string
311314 return [
312- 'message ' => " OK. I found " . count ($ formatted_events ) . " WordPress events near " . ( $ location_input === 'near me ' ? 'your location ' : $ location_input ) . ": \n\n" . $ formatted_events_output
315+ 'message ' => ' OK. I found ' . count ( $ formatted_events ) . ' WordPress events near ' . ( $ location_input === 'near me ' ? 'your location ' : $ location_input ) . ": \n\n" . $ formatted_events_output,
313316 ];
314- },
317+ },
315318 ]
316319 );
317320 }
318321
319322 // Register resources for AI access
320- private function register_resources ($ server ) {
323+ private function register_resources ( $ server ) {
321324 // Register Users resource
322- $ server ->register_resource ([
325+ $ server ->register_resource (
326+ [
323327 'name ' => 'users ' ,
324328 'uri ' => 'data://users ' ,
325329 'description ' => 'List of users ' ,
326330 'mimeType ' => 'application/json ' ,
327331 'dataKey ' => 'users ' , // Data will be fetched from 'users'
328- ]);
332+ ]
333+ );
329334
330335 // Register Product Catalog resource
331- $ server ->register_resource ([
336+ $ server ->register_resource (
337+ [
332338 'name ' => 'product_catalog ' ,
333339 'uri ' => 'file://./products.json ' ,
334340 'description ' => 'Product catalog ' ,
335341 'mimeType ' => 'application/json ' ,
336342 'filePath ' => './products.json ' , // Data will be fetched from products.json
337- ]);
343+ ]
344+ );
338345 }
339346}
0 commit comments