From 114acfe92b4e4ea8b32ef12c6a775dd6a79eb9ef Mon Sep 17 00:00:00 2001 From: Matt Date: Sat, 15 Mar 2025 17:56:59 +0100 Subject: [PATCH] retrieve last N posts in json format --- src/AiCommand.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/AiCommand.php b/src/AiCommand.php index 6687fcc..b8e80d6 100644 --- a/src/AiCommand.php +++ b/src/AiCommand.php @@ -83,6 +83,37 @@ private function register_tools($server, $client) { ] ); + + + // Register tool to retrieve last N posts in JSON format. + $server->register_tool([ + 'name' => 'list_posts', + 'description' => 'Retrieves the last N posts.', + 'inputSchema' => [ + 'type' => 'object', + 'properties' => [ + 'count' => [ + 'type' => 'integer', + 'description' => 'The number of posts to retrieve.', + ], + ], + 'required' => ['count'], + ], + 'callable' => function ($params) { + $query = new \WP_Query([ + 'posts_per_page' => $params['count'], + 'post_status' => 'publish', + ]); + $posts = []; + while ($query->have_posts()) { + $query->the_post(); + $posts[] = ['title' => get_the_title(), 'content' => get_the_content()]; + } + wp_reset_postdata(); + return $posts; + }, + ]); + $server->register_tool( [ 'name' => 'greet',