Skip to content

Commit 796a8ed

Browse files
Merge pull request #6 from Automattic/bugfixes
Bugfixes
2 parents ec65f10 + 19e7fa0 commit 796a8ed

File tree

3 files changed

+74
-6
lines changed

3 files changed

+74
-6
lines changed

includes/Core/RegisterMcpTool.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,12 @@ private function get_args_from_rest_api(): void {
8181
);
8282

8383
foreach ( $rest_api['args'] as $arg_name => $arg_schema ) {
84+
$type = $arg_schema['type'];
85+
if ( is_array( $type ) ) {
86+
$type = reset( $type );
87+
}
8488
$input_schema['properties'][ $arg_name ] = array(
85-
'type' => $arg_schema['type'],
89+
'type' => $type,
8690
'description' => $arg_schema['description'],
8791
);
8892

@@ -120,6 +124,12 @@ private function get_args_from_rest_api(): void {
120124
}
121125
}
122126

127+
// Apply modifications if provided in rest_alias['modifications'] .
128+
if ( isset( $this->args['rest_alias']['inputSchemaReplacements'] ) ) {
129+
$modifications = $this->args['rest_alias']['inputSchemaReplacements'];
130+
$input_schema = $this->apply_modifications( $input_schema, $modifications );
131+
}
132+
123133
// Update the args with the converted schema.
124134
$this->args['inputSchema'] = $input_schema;
125135
$this->args['callback'] = $rest_api['callback'];
@@ -268,4 +278,36 @@ private function validate_input_schema(): void {
268278
}
269279
}
270280
}
281+
282+
/**
283+
* Recursively remove all null values from an array.
284+
*
285+
* @param array $array The array to clean.
286+
* @return array The cleaned array.
287+
*/
288+
private function remove_null_recursive( array $array ): array {
289+
foreach ( $array as $key => &$value ) {
290+
if ( is_array( $value ) ) {
291+
$value = $this->remove_null_recursive( $value );
292+
} elseif ( is_null( $value ) ) {
293+
unset( $array[ $key ] );
294+
}
295+
}
296+
unset( $value ); // break reference.
297+
return $array;
298+
}
299+
300+
/**
301+
* Apply modifications to the input schema.
302+
*
303+
* @param array $input_schema The input schema.
304+
* @param array $modifications The modifications to apply.
305+
* @return array The modified input schema.
306+
*/
307+
private function apply_modifications( array $input_schema, array $modifications ): array {
308+
309+
$modifications = array_replace_recursive( $input_schema, $modifications );
310+
311+
return $this->remove_null_recursive( $modifications );
312+
}
271313
}

includes/Tools/McpPostsTools.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,28 @@ public function register_tools(): void {
5151
'description' => 'Add a new WordPress post',
5252
'type' => 'create',
5353
'rest_alias' => array(
54-
'route' => '/wp/v2/posts',
55-
'method' => 'POST',
54+
'route' => '/wp/v2/posts',
55+
'method' => 'POST',
56+
'inputSchemaReplacements' => array( // this will replace the defined elements in the default input schema with the new ones.
57+
'properties' => array(
58+
'title' => array(
59+
'type' => 'string',
60+
),
61+
'content' => array(
62+
'type' => 'string',
63+
'description' => 'The content of the post in a valid Guttenberg block format',
64+
),
65+
'excerpt' => array(
66+
'type' => 'string',
67+
),
68+
),
69+
'required' => array(
70+
'title',
71+
'content',
72+
),
73+
),
5674
),
57-
)
75+
),
5876
);
5977

6078
new RegisterMcpTool(

includes/Tools/McpUsersTools.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,16 @@ public function register_tools(): void {
2727
'description' => 'Search and filter WordPress users with pagination',
2828
'type' => 'read',
2929
'rest_alias' => array(
30-
'route' => '/wp/v2/users',
31-
'method' => 'GET',
30+
'route' => '/wp/v2/users',
31+
'method' => 'GET',
32+
'inputSchemaReplacements' => array(
33+
'properties' => array(
34+
'has_published_posts' => array(
35+
'items' => null, // this will remove the array from the schema.
36+
'default' => false,
37+
),
38+
),
39+
),
3240
),
3341
)
3442
);

0 commit comments

Comments
 (0)