22
33namespace WP_CLI \AiCommand ;
44
5+ use WP_CLI \AiCommand \RouteInformation ;
56use WP_CLI ;
67use WP_CLI \AiCommand \MCP \Server ;
78use WP_REST_Request ;
89
10+
911class MapRESTtoMCP {
1012
1113 public function __construct (
@@ -96,12 +98,20 @@ public function map_rest_to_mcp( Server $mcp_server ) {
9698 continue ; // This route is the block list.
9799 }
98100
99-
100101 foreach ( $ endpoint ['methods ' ] as $ method_name => $ enabled ) {
102+ $ information = new RouteInformation (
103+ $ route ,
104+ $ method_name ,
105+ $ endpoint ['callback ' ],
106+ );
107+
108+ if ( ! $ information ->is_wp_rest_controller () ) {
109+ continue ;
110+ }
101111
102112 $ tool = [
103- 'name ' => $ this -> generate_tool_name ( $ route , $ method_name ),
104- 'description ' => $ this ->generate_description ( $ route , $ method_name , $ endpoint ),
113+ 'name ' => $ information -> get_sanitized_route_name ( ),
114+ 'description ' => $ this ->generate_description ( $ information ),
105115 'inputSchema ' => $ this ->args_to_schema ( $ endpoint ['args ' ] ),
106116 'callable ' => function ( $ inputs ) use ( $ route , $ method_name , $ server ){
107117 return $ this ->rest_callable ( $ inputs , $ route , $ method_name , $ server );
@@ -113,6 +123,7 @@ public function map_rest_to_mcp( Server $mcp_server ) {
113123
114124 }
115125 }
126+
116127 }
117128
118129 protected function generate_tool_name ($ route , $ method_name ) {
@@ -130,36 +141,23 @@ protected function generate_tool_name($route, $method_name) {
130141 * Get a list of posts GET /wp/v2/posts
131142 * Get post with id GET /wp/v2/posts/(?P<id>[\d]+)
132143 */
133- protected function generate_description ( $ route , $ method_name , $ endpoint ) {
144+ protected function generate_description ( RouteInformation $ information ) : string {
134145
135- // TODO all validation + exception handling.
136- $ verb = array (
146+ $ verb = match ($ information ->get_method ()) {
137147 'GET ' => 'Get ' ,
138148 'POST ' => 'Create ' ,
139- 'PUT ' => 'Update ' ,
140- 'PATCH ' => 'Update ' ,
149+ 'PUT ' , 'PATCH ' => 'Update ' ,
141150 'DELETE ' => 'Delete ' ,
142- ) ;
151+ } ;
143152
144- $ controller = $ endpoint ['callback ' ][0 ];
145- if ( !isset ($ endpoint ['callback ' ]) || ! \is_object ($ endpoint ['callback ' ][0 ])) {
146- throw new \Exception ('Not an object: ' . $ route );
147- }
148- if (! \method_exists ($ endpoint ['callback ' ][0 ], 'get_public_item_schema ' )) {
149- throw new \Exception ('missing method: ' . $ route );
150- }
151-
152- $ schema = $ controller ->get_public_item_schema ();
153+ $ schema = $ information ->get_wp_rest_controller ()->get_public_item_schema ();
153154 $ title = $ schema ['title ' ];
154155
155- // is singular?
156- $ singular = 'a ' ;
157- if ( $ method_name === 'GET ' && ! \str_contains ( $ route , '(?P< ' )) {
158- $ singular = 'List of ' ;
159-
160- }
156+ $ determiner = $ information ->is_singular ()
157+ ? 'a '
158+ : 'list of ' ;
161159
162- return $ verb[ $ method_name ] . ' ' . $ singular . ' ' . $ title ;
160+ return $ verb . ' ' . $ determiner . ' ' . $ title ;
163161 }
164162
165163 protected function rest_callable ( $ inputs , $ route , $ method_name , $ server ) {
0 commit comments