|null $schema The JSON schema to transform. * @param string $wrapper_key Property name to use when wrapping non-object schemas. * * @return array Array containing 'schema', 'was_transformed' (bool), and 'wrapper_property' when transformed. */ public static function transform_to_object_schema( ?array $schema, string $wrapper_key = 'input' ): array { // Handle null or empty schema - return minimal object schema if ( empty( $schema ) ) { return array( 'schema' => array( 'type' => 'object', 'additionalProperties' => false, ), 'was_transformed' => false, // Empty schema wasn't really transformed 'wrapper_property' => null, ); } // If no type is specified, just return it as-is. It will fail MCP validation later because MCP requires an explicit type: "object" if ( ! isset( $schema['type'] ) ) { return array( 'schema' => $schema, 'was_transformed' => false, 'wrapper_property' => null, ); } // If already an object type, return as-is if ( 'object' === $schema['type'] ) { return array( 'schema' => $schema, 'was_transformed' => false, 'wrapper_property' => null, ); } // Transform flattened schema to object format return array( 'schema' => self::wrap_in_object( $schema, $wrapper_key ), 'was_transformed' => true, 'wrapper_property' => $wrapper_key, ); } /** * Wrap a flattened schema in an object structure. * * Creates an object schema with a single property (named by $wrapper_key) that * contains the original flattened schema. * * @param array $schema The flattened schema to wrap. * @param string $wrapper_key Property name to wrap the value under. * * @return array The wrapped object schema. */ private static function wrap_in_object( array $schema, string $wrapper_key ): array { return array( 'type' => 'object', 'properties' => array( $wrapper_key => $schema, ), 'required' => array( $wrapper_key ), ); } }