HttpTransport.php
132 lines
| 1 | <?php |
| 2 | /** |
| 3 | * MCP HTTP Transport for WordPress - MCP 2025-06-18 Compliant |
| 4 | * |
| 5 | * This transport implements the MCP Streamable HTTP specification and can work |
| 6 | * both with and without the mcp-wordpress-remote proxy. |
| 7 | * |
| 8 | * @package McpAdapter |
| 9 | */ |
| 10 | |
| 11 | declare( strict_types=1 ); |
| 12 | |
| 13 | namespace WP\MCP\Transport; |
| 14 | |
| 15 | use WP\MCP\Transport\Contracts\McpRestTransportInterface; |
| 16 | use WP\MCP\Transport\Infrastructure\HttpRequestContext; |
| 17 | use WP\MCP\Transport\Infrastructure\HttpRequestHandler; |
| 18 | use WP\MCP\Transport\Infrastructure\McpTransportContext; |
| 19 | use WP\MCP\Transport\Infrastructure\McpTransportHelperTrait; |
| 20 | |
| 21 | /** |
| 22 | * MCP HTTP Transport - Unified transport for both proxy and direct clients |
| 23 | * |
| 24 | * Implements MCP 2025-06-18 Streamable HTTP specification |
| 25 | */ |
| 26 | class HttpTransport implements McpRestTransportInterface { |
| 27 | use McpTransportHelperTrait; |
| 28 | |
| 29 | /** |
| 30 | * The HTTP request handler. |
| 31 | * |
| 32 | * @var \WP\MCP\Transport\Infrastructure\HttpRequestHandler |
| 33 | */ |
| 34 | protected HttpRequestHandler $request_handler; |
| 35 | |
| 36 | /** |
| 37 | * Initialize the class and register routes |
| 38 | * |
| 39 | * @param \WP\MCP\Transport\Infrastructure\McpTransportContext $transport_context The transport context. |
| 40 | */ |
| 41 | public function __construct( McpTransportContext $transport_context ) { |
| 42 | $this->request_handler = new HttpRequestHandler( $transport_context ); |
| 43 | add_action( 'rest_api_init', array( $this, 'register_routes' ), 16 ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Register MCP HTTP routes |
| 48 | */ |
| 49 | public function register_routes(): void { |
| 50 | // Get server info from request handler's transport context |
| 51 | $server = $this->request_handler->transport_context->mcp_server; |
| 52 | |
| 53 | // Single endpoint for MCP communication (POST, GET for SSE, DELETE for session termination) |
| 54 | register_rest_route( |
| 55 | $server->get_server_route_namespace(), |
| 56 | $server->get_server_route(), |
| 57 | array( |
| 58 | 'methods' => array( 'POST', 'GET', 'DELETE' ), |
| 59 | 'callback' => array( $this, 'handle_request' ), |
| 60 | 'permission_callback' => array( $this, 'check_permission' ), |
| 61 | ) |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Check if the user has permission to access the MCP API |
| 67 | * |
| 68 | * @param \WP_REST_Request<array<string, mixed>> $request The request object. |
| 69 | * |
| 70 | * @return bool True if the user has permission, false otherwise. |
| 71 | */ |
| 72 | public function check_permission( \WP_REST_Request $request ) { |
| 73 | $context = new HttpRequestContext( $request ); |
| 74 | |
| 75 | // Check permission using callback or default |
| 76 | $transport_context = $this->request_handler->transport_context; |
| 77 | |
| 78 | if ( null !== $transport_context->transport_permission_callback ) { |
| 79 | try { |
| 80 | $result = call_user_func( $transport_context->transport_permission_callback, $context->request ); |
| 81 | |
| 82 | // Handle WP_Error returns |
| 83 | if ( ! is_wp_error( $result ) ) { |
| 84 | // Return boolean result directly |
| 85 | return $result; |
| 86 | } |
| 87 | |
| 88 | // Log the error and fall back to default permission |
| 89 | $this->request_handler->transport_context->error_handler->log( |
| 90 | 'Permission callback returned WP_Error: ' . $result->get_error_message(), |
| 91 | array( 'HttpTransport::check_permission' ) |
| 92 | ); |
| 93 | // Fall through to default permission check |
| 94 | } catch ( \Throwable $e ) { |
| 95 | // Log the error using the error handler, and fall back to default permission |
| 96 | $this->request_handler->transport_context->error_handler->log( 'Error in transport permission callback: ' . $e->getMessage(), array( 'HttpTransport::check_permission' ) ); |
| 97 | } |
| 98 | } |
| 99 | $user_capability = apply_filters( 'mcp_adapter_default_transport_permission_user_capability', 'read', $context ); |
| 100 | |
| 101 | // Validate that the filtered capability is a non-empty string |
| 102 | if ( ! is_string( $user_capability ) || empty( $user_capability ) ) { |
| 103 | $user_capability = 'read'; |
| 104 | } |
| 105 | |
| 106 | $user_has_capability = current_user_can( $user_capability ); // phpcs:ignore WordPress.WP.Capabilities.Undetermined -- Capability is filtered and defaults to 'read' |
| 107 | |
| 108 | if ( ! $user_has_capability ) { |
| 109 | $user_id = get_current_user_id(); |
| 110 | $this->request_handler->transport_context->error_handler->log( |
| 111 | sprintf( 'Permission denied for MCP API access. User ID %d does not have capability "%s"', $user_id, $user_capability ), |
| 112 | array( 'HttpTransport::check_permission' ) |
| 113 | ); |
| 114 | } |
| 115 | |
| 116 | return $user_has_capability; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Handle HTTP requests according to MCP 2025-06-18 specification |
| 121 | * |
| 122 | * @param \WP_REST_Request<array<string, mixed>> $request The request object. |
| 123 | * |
| 124 | * @return \WP_REST_Response |
| 125 | */ |
| 126 | public function handle_request( \WP_REST_Request $request ): \WP_REST_Response { |
| 127 | $context = new HttpRequestContext( $request ); |
| 128 | |
| 129 | return $this->request_handler->handle_request( $context ); |
| 130 | } |
| 131 | } |
| 132 |