Initialize
7 months ago
Prompts
7 months ago
Resources
7 months ago
System
7 months ago
Tools
7 months ago
HandlerHelperTrait.php
7 months ago
HandlerHelperTrait.php
117 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Helper trait for MCP handlers. |
| 4 | * |
| 5 | * @package McpAdapter |
| 6 | */ |
| 7 | |
| 8 | declare( strict_types=1 ); |
| 9 | |
| 10 | namespace WP\MCP\Handlers; |
| 11 | |
| 12 | use WP\MCP\Infrastructure\ErrorHandling\McpErrorFactory; |
| 13 | |
| 14 | /** |
| 15 | * Provides common helper methods for MCP handlers. |
| 16 | */ |
| 17 | trait HandlerHelperTrait { |
| 18 | /** |
| 19 | * Extract parameters from a request message. |
| 20 | * |
| 21 | * Handles both direct params and nested params structure for backward compatibility. |
| 22 | * This normalizes the dual parameter patterns found throughout handlers. |
| 23 | * |
| 24 | * @param array $data Request data that may have params at root or nested. |
| 25 | * |
| 26 | * @return array Extracted parameters. |
| 27 | */ |
| 28 | protected function extract_params( array $data ): array { |
| 29 | return $data['params'] ?? $data; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Create a standardized error response. |
| 34 | * |
| 35 | * This helper ensures all error responses follow the same format and |
| 36 | * properly extract the error field from McpErrorFactory responses. |
| 37 | * |
| 38 | * @param int $code Error code. |
| 39 | * @param string $message Error message. |
| 40 | * @param int $request_id Request ID for JSON-RPC. |
| 41 | * |
| 42 | * @return array Error response array with 'error' key. |
| 43 | */ |
| 44 | protected function create_error_response( int $code, string $message, int $request_id = 0 ): array { |
| 45 | return array( |
| 46 | 'id' => $request_id, |
| 47 | 'error' => array( |
| 48 | 'code' => $code, |
| 49 | 'message' => $message, |
| 50 | ), |
| 51 | ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Extract error array from McpErrorFactory response. |
| 56 | * |
| 57 | * McpErrorFactory methods return ['error' => [...]] but handlers |
| 58 | * often need just the error array itself. |
| 59 | * |
| 60 | * @param array $factory_response Response from McpErrorFactory method. |
| 61 | * |
| 62 | * @return array Error array (without wrapping 'error' key). |
| 63 | */ |
| 64 | protected function extract_error( array $factory_response ): array { |
| 65 | return $factory_response['error'] ?? $factory_response; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Create missing parameter error response. |
| 70 | * |
| 71 | * @param string $param_name Missing parameter name. |
| 72 | * @param int $request_id Request ID for JSON-RPC. |
| 73 | * |
| 74 | * @return array Error response array. |
| 75 | */ |
| 76 | protected function missing_parameter_error( string $param_name, int $request_id = 0 ): array { |
| 77 | return array( 'error' => McpErrorFactory::missing_parameter( $request_id, $param_name )['error'] ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Create permission denied error response. |
| 82 | * |
| 83 | * @param string $denied_resource Resource that was denied. |
| 84 | * @param int $request_id Request ID for JSON-RPC. |
| 85 | * |
| 86 | * @return array Error response array. |
| 87 | */ |
| 88 | protected function permission_denied_error( string $denied_resource, int $request_id = 0 ): array { |
| 89 | return array( 'error' => McpErrorFactory::permission_denied( $request_id, 'Access denied for: ' . $denied_resource )['error'] ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Create internal error response. |
| 94 | * |
| 95 | * @param string $message Error message. |
| 96 | * @param int $request_id Request ID for JSON-RPC. |
| 97 | * |
| 98 | * @return array Error response array. |
| 99 | */ |
| 100 | protected function internal_error( string $message, int $request_id = 0 ): array { |
| 101 | return array( 'error' => McpErrorFactory::internal_error( $request_id, $message )['error'] ); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Create a standardized success response. |
| 106 | * |
| 107 | * @param mixed $data Response data. |
| 108 | * |
| 109 | * @return array Success response array. |
| 110 | */ |
| 111 | protected function create_success_response( $data ): array { |
| 112 | return array( |
| 113 | 'result' => $data, |
| 114 | ); |
| 115 | } |
| 116 | } |
| 117 |