File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed
Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -113,6 +113,37 @@ private function register_tools($server, $client) {
113113 ]
114114 );
115115
116+
117+
118+ // Register tool to retrieve last N posts in JSON format.
119+ $ server ->register_tool ([
120+ 'name ' => 'list_posts ' ,
121+ 'description ' => 'Retrieves the last N posts. ' ,
122+ 'inputSchema ' => [
123+ 'type ' => 'object ' ,
124+ 'properties ' => [
125+ 'count ' => [
126+ 'type ' => 'integer ' ,
127+ 'description ' => 'The number of posts to retrieve. ' ,
128+ ],
129+ ],
130+ 'required ' => ['count ' ],
131+ ],
132+ 'callable ' => function ($ params ) {
133+ $ query = new \WP_Query ([
134+ 'posts_per_page ' => $ params ['count ' ],
135+ 'post_status ' => 'publish ' ,
136+ ]);
137+ $ posts = [];
138+ while ($ query ->have_posts ()) {
139+ $ query ->the_post ();
140+ $ posts [] = ['title ' => get_the_title (), 'content ' => get_the_content ()];
141+ }
142+ wp_reset_postdata ();
143+ return $ posts ;
144+ },
145+ ]);
146+
116147 $ server ->register_tool (
117148 [
118149 'name ' => 'greet ' ,
You can’t perform that action at this time.
0 commit comments