|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * WP-CLI command for validating MCP tools |
| 4 | + * |
| 5 | + * @package McpForWoo |
| 6 | + * @subpackage CLI |
| 7 | + */ |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +namespace McpForWoo\CLI; |
| 12 | + |
| 13 | +use WP_CLI; |
| 14 | +use McpForWoo\Utils\ToolValidator; |
| 15 | +use McpForWoo\Utils\SchemaValidator; |
| 16 | +use McpForWoo\Core\RegisterMcpTool; |
| 17 | + |
| 18 | +/** |
| 19 | + * Validates MCP tools for compliance and functionality. |
| 20 | + */ |
| 21 | +class ValidateToolsCommand { |
| 22 | + |
| 23 | + /** |
| 24 | + * Validates all registered MCP tools |
| 25 | + * |
| 26 | + * ## OPTIONS |
| 27 | + * |
| 28 | + * [--level=<level>] |
| 29 | + * : Validation level: strict, extended, or permissive |
| 30 | + * --- |
| 31 | + * default: extended |
| 32 | + * options: |
| 33 | + * - strict |
| 34 | + * - extended |
| 35 | + * - permissive |
| 36 | + * --- |
| 37 | + * |
| 38 | + * [--format=<format>] |
| 39 | + * : Output format |
| 40 | + * --- |
| 41 | + * default: table |
| 42 | + * options: |
| 43 | + * - table |
| 44 | + * - json |
| 45 | + * - yaml |
| 46 | + * --- |
| 47 | + * |
| 48 | + * ## EXAMPLES |
| 49 | + * |
| 50 | + * wp woo-mcp validate-tools |
| 51 | + * wp woo-mcp validate-tools --level=strict |
| 52 | + * wp woo-mcp validate-tools --format=json |
| 53 | + * |
| 54 | + * @param array $args Command arguments. |
| 55 | + * @param array $assoc_args Associative arguments. |
| 56 | + */ |
| 57 | + public function __invoke( $args, $assoc_args ) { |
| 58 | + $level = $assoc_args['level'] ?? 'extended'; |
| 59 | + $format = $assoc_args['format'] ?? 'table'; |
| 60 | + |
| 61 | + if ( ! in_array( $level, array( 'strict', 'extended', 'permissive' ), true ) ) { |
| 62 | + WP_CLI::error( 'Invalid validation level. Use: strict, extended, or permissive' ); |
| 63 | + } |
| 64 | + |
| 65 | + WP_CLI::log( "Validating MCP tools with {$level} validation level..." ); |
| 66 | + |
| 67 | + $registered_tools = $this->get_registered_tools(); |
| 68 | + $validation_results = array(); |
| 69 | + $schema_validator = new SchemaValidator(); |
| 70 | + |
| 71 | + if ( empty( $registered_tools ) ) { |
| 72 | + WP_CLI::warning( 'No MCP tools found to validate.' ); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + $progress = WP_CLI\Utils\make_progress_bar( 'Validating tools', count( $registered_tools ) ); |
| 77 | + |
| 78 | + foreach ( $registered_tools as $tool_name => $tool_data ) { |
| 79 | + $progress->tick(); |
| 80 | + |
| 81 | + $tool_validator = new ToolValidator( array(), $level ); |
| 82 | + $validation_result = $tool_validator->validate( $tool_data ); |
| 83 | + |
| 84 | + $result_data = array( |
| 85 | + 'tool' => $tool_name, |
| 86 | + 'status' => is_wp_error( $validation_result ) ? 'failed' : 'passed', |
| 87 | + 'errors' => array(), |
| 88 | + ); |
| 89 | + |
| 90 | + if ( is_wp_error( $validation_result ) ) { |
| 91 | + $result_data['errors'] = $validation_result->get_error_data(); |
| 92 | + } |
| 93 | + |
| 94 | + // Additional schema validation |
| 95 | + $schema_result = $schema_validator->validate_mcp_tool( $tool_data ); |
| 96 | + if ( is_wp_error( $schema_result ) ) { |
| 97 | + $result_data['status'] = 'failed'; |
| 98 | + $result_data['errors'] = array_merge( |
| 99 | + $result_data['errors'], |
| 100 | + $schema_validator->get_validation_errors() |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + $validation_results[] = $result_data; |
| 105 | + } |
| 106 | + |
| 107 | + $progress->finish(); |
| 108 | + |
| 109 | + $this->output_results( $validation_results, $format ); |
| 110 | + $this->display_summary( $validation_results ); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Get all registered MCP tools |
| 115 | + * |
| 116 | + * @return array Registered tools data. |
| 117 | + */ |
| 118 | + private function get_registered_tools(): array { |
| 119 | + global $wp_mcp_tools; |
| 120 | + |
| 121 | + if ( empty( $wp_mcp_tools ) ) { |
| 122 | + // Try to get tools from RegisterMcpTool if available |
| 123 | + if ( class_exists( 'McpForWoo\Core\RegisterMcpTool' ) ) { |
| 124 | + $register_tool = new RegisterMcpTool(); |
| 125 | + // This would need a method to retrieve registered tools |
| 126 | + // For now, return empty array |
| 127 | + return array(); |
| 128 | + } |
| 129 | + return array(); |
| 130 | + } |
| 131 | + |
| 132 | + return $wp_mcp_tools; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Output validation results |
| 137 | + * |
| 138 | + * @param array $results Validation results. |
| 139 | + * @param string $format Output format. |
| 140 | + */ |
| 141 | + private function output_results( array $results, string $format ): void { |
| 142 | + switch ( $format ) { |
| 143 | + case 'json': |
| 144 | + WP_CLI::print_value( $results, array( 'format' => 'json' ) ); |
| 145 | + break; |
| 146 | + |
| 147 | + case 'yaml': |
| 148 | + WP_CLI::print_value( $results, array( 'format' => 'yaml' ) ); |
| 149 | + break; |
| 150 | + |
| 151 | + case 'table': |
| 152 | + default: |
| 153 | + $table_data = array(); |
| 154 | + foreach ( $results as $result ) { |
| 155 | + $table_data[] = array( |
| 156 | + 'Tool' => $result['tool'], |
| 157 | + 'Status' => $result['status'], |
| 158 | + 'Errors' => empty( $result['errors'] ) ? '-' : implode( '; ', array_slice( $result['errors'], 0, 2 ) ), |
| 159 | + ); |
| 160 | + } |
| 161 | + |
| 162 | + if ( ! empty( $table_data ) ) { |
| 163 | + WP_CLI\Utils\format_items( 'table', $table_data, array( 'Tool', 'Status', 'Errors' ) ); |
| 164 | + } |
| 165 | + break; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Display validation summary |
| 171 | + * |
| 172 | + * @param array $results Validation results. |
| 173 | + */ |
| 174 | + private function display_summary( array $results ): void { |
| 175 | + $total_tools = count( $results ); |
| 176 | + $passed_tools = count( array_filter( $results, function( $result ) { |
| 177 | + return 'passed' === $result['status']; |
| 178 | + } ) ); |
| 179 | + $failed_tools = $total_tools - $passed_tools; |
| 180 | + |
| 181 | + WP_CLI::log( '' ); |
| 182 | + WP_CLI::log( 'Validation Summary:' ); |
| 183 | + WP_CLI::log( "- Total tools: {$total_tools}" ); |
| 184 | + |
| 185 | + if ( $passed_tools > 0 ) { |
| 186 | + WP_CLI::success( "Passed: {$passed_tools}" ); |
| 187 | + } |
| 188 | + |
| 189 | + if ( $failed_tools > 0 ) { |
| 190 | + WP_CLI::error( "Failed: {$failed_tools}", false ); |
| 191 | + |
| 192 | + // Show detailed errors for failed tools |
| 193 | + WP_CLI::log( '' ); |
| 194 | + WP_CLI::log( 'Failed Tools Details:' ); |
| 195 | + foreach ( $results as $result ) { |
| 196 | + if ( 'failed' === $result['status'] ) { |
| 197 | + WP_CLI::log( "- {$result['tool']}:" ); |
| 198 | + foreach ( $result['errors'] as $error ) { |
| 199 | + WP_CLI::log( " • {$error}" ); |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + if ( 0 === $failed_tools ) { |
| 206 | + WP_CLI::success( 'All tools passed validation!' ); |
| 207 | + } |
| 208 | + } |
| 209 | +} |
0 commit comments