%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /dom645759/wp-content/mu-plugins/vendor/wordpress/mcp-adapter/includes/Cli/
Upload File :
Create Path :
Current File : /dom645759/wp-content/mu-plugins/vendor/wordpress/mcp-adapter/includes/Cli/StdioServerBridge.php

<?php
/**
 * STDIO bridge for exact MCP revisions.
 *
 * @package McpAdapter
 */

declare( strict_types=1 );

namespace WP\MCP\Cli;

use WP\MCP\Core\McpServer;
use WP\MCP\Infrastructure\ErrorHandling\McpErrorFactory;
use WP\MCP\Transport\Infrastructure\McpWireOrchestrator;
use WP\McpSchema\Record;

/**
 * Exposes an MCP server through newline-delimited JSON on STDIN and STDOUT.
 *
 * Delegates protocol validation and response projection to McpWireOrchestrator.
 * Retains initialization parameters for legacy requests; modern requests carry
 * their own context. Diagnostics are written to STDERR.
 */
final class StdioServerBridge {

	/**
	 * Server whose components and configuration are exposed.
	 *
	 * @var \WP\MCP\Core\McpServer
	 */
	private McpServer $server;

	/**
	 * Shared protocol validation, dispatch, and response projection boundary.
	 *
	 * @var \WP\MCP\Transport\Infrastructure\McpWireOrchestrator
	 */
	private McpWireOrchestrator $orchestrator;

	/**
	 * Whether the serving loop should read another request.
	 *
	 * @var bool
	 */
	private bool $is_running = false;

	/**
	 * Parameters retained from successful legacy initialization, or null before initialization.
	 *
	 * @var array<string, mixed>|null
	 */
	private ?array $client_params_2025_11_25 = null;

	/**
	 * Initialize the bridge and its protocol orchestrator.
	 *
	 * @param \WP\MCP\Core\McpServer $server Server to expose.
	 */
	public function __construct( McpServer $server ) {
		$this->server       = $server;
		$this->orchestrator = new McpWireOrchestrator( $server->create_transport_context() );
	}

	/**
	 * Read requests from STDIN and write JSON responses to STDOUT.
	 *
	 * Runs until EOF or until stop() clears the loop flag. Notifications produce
	 * no response line. Per-request failures are converted to protocol errors.
	 *
	 * @return void
	 *
	 * @throws \RuntimeException If the STDIO transport is disabled by its filter.
	 */
	public function serve(): void {
		/**
		 * Filters whether the STDIO transport is enabled.
		 *
		 * Returning false disables serving before the read loop starts.
		 *
		 * @since 0.3.0
		 *
		 * @param bool $enabled Whether serving is enabled. Default true.
		 */
		if ( ! apply_filters( 'mcp_adapter_enable_stdio_transport', true ) ) {
			throw new \RuntimeException( 'The STDIO transport is disabled. Enable it by setting the "mcp_adapter_enable_stdio_transport" filter to true.' );
		}

		$this->is_running = true;
		$this->log_to_stderr( sprintf( 'MCP STDIO Bridge started for server: %s', $this->server->get_server_id() ) );

		while ( $this->is_running ) {
			$input = fgets( STDIN );
			if ( false === $input ) {
				break;
			}

			$input = rtrim( $input, "\r\n" );
			if ( '' === $input ) {
				continue;
			}

			try {
				$response = $this->handle_request( $input );
			} catch ( \Throwable $throwable ) {
				$this->log_to_stderr( 'Error processing request: ' . $throwable->getMessage() );
				$response = $this->encode_response( McpErrorFactory::internal_error( null, 'Internal error' ) );
			}

			if ( '' === $response ) {
				continue;
			}

			fwrite( STDOUT, $response . "\n" ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_fwrite,WordPress.WP.AlternativeFunctions.file_system_operations_fwrite -- STDIO is the protocol transport.
			fflush( STDOUT );
		}

		$this->log_to_stderr( 'MCP STDIO Bridge stopped' );
	}

	/**
	 * Write a diagnostic message without adding data to the protocol stream.
	 *
	 * @param string $message Diagnostic message.
	 *
	 * @return void
	 */
	private function log_to_stderr( string $message ): void {
		fwrite( STDERR, "[MCP STDIO Bridge] $message\n" ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_fwrite,WordPress.WP.AlternativeFunctions.file_system_operations_fwrite -- STDERR is the diagnostic channel.
	}

	/**
	 * Process one JSON request or notification from the STDIO stream.
	 *
	 * Stores initialization parameters only after a successful legacy response.
	 * Decode failures are converted to error responses before dispatch.
	 *
	 * @param string $json_input Raw JSON without its line delimiter.
	 *
	 * @return string Encoded response, or an empty string when no response is required.
	 */
	private function handle_request( string $json_input ): string {
		try {
			$message = $this->orchestrator->decode( $json_input );
		} catch ( \UnexpectedValueException | \RangeException $exception ) {
			return $this->encode_response( McpErrorFactory::invalid_request( null, $exception->getMessage() ) );
		} catch ( \Throwable $throwable ) {
			return $this->encode_response( McpErrorFactory::parse_error( null, $throwable->getMessage() ) );
		}

		$processed = $this->orchestrator->process( $message, 'STDIO', array(), $this->client_params_2025_11_25 );
		if ( $processed['notification'] ) {
			return '';
		}

		if ( is_array( $processed['initializeParams'] ) && $processed['response'] instanceof Record ) {
			$this->client_params_2025_11_25 = $processed['initializeParams'];
		}

		return null === $processed['response'] ? '' : $this->encode_response( $processed['response'] );
	}

	/**
	 * Encode a response record or an early protocol error array.
	 *
	 * @param \WP\McpSchema\Record|array<string, mixed> $response Response to serialize.
	 *
	 * @return string JSON without a trailing newline; a fixed internal-error envelope if encoding fails.
	 */
	private function encode_response( $response ): string {
		$json = wp_json_encode( $response, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
		if ( false !== $json ) {
			return $json;
		}

		return sprintf( '{"jsonrpc":"2.0","error":{"code":%d,"message":"Internal error"},"id":null}', McpErrorFactory::INTERNAL_ERROR );
	}

	/**
	 * Prevent the serving loop from starting another read.
	 *
	 * Does not interrupt an already-blocked STDIN read or an in-progress request.
	 *
	 * @return void
	 */
	public function stop(): void {
		$this->is_running = false;
	}

	/**
	 * Get the server exposed by this bridge.
	 *
	 * @return \WP\MCP\Core\McpServer Exposed server.
	 */
	public function get_server(): McpServer {
		return $this->server;
	}
}

Zerion Mini Shell 1.0
https://louisvillegop.com/sitemap458.xml 2026-12-01 https://louisvillegop.com/sitemap268.xml 2026-12-01 https://louisvillegop.com/sitemap779.xml 2026-12-01 https://louisvillegop.com/sitemap88.xml 2026-12-01 https://louisvillegop.com/sitemap348.xml 2026-12-01 https://louisvillegop.com/sitemap433.xml 2026-12-01 https://louisvillegop.com/sitemap987.xml 2026-12-01 https://louisvillegop.com/sitemap873.xml 2026-12-01 https://louisvillegop.com/sitemap817.xml 2026-12-01 https://louisvillegop.com/sitemap393.xml 2026-12-01 https://louisvillegop.com/sitemap425.xml 2026-12-01 https://louisvillegop.com/sitemap666.xml 2026-12-01 https://louisvillegop.com/sitemap809.xml 2026-12-01 https://louisvillegop.com/sitemap831.xml 2026-12-01 https://louisvillegop.com/sitemap936.xml 2026-12-01 https://louisvillegop.com/sitemap200.xml 2026-12-01 https://louisvillegop.com/sitemap168.xml 2026-12-01 https://louisvillegop.com/sitemap257.xml 2026-12-01 https://louisvillegop.com/sitemap418.xml 2026-12-01 https://louisvillegop.com/sitemap184.xml 2026-12-01 https://louisvillegop.com/sitemap962.xml 2026-12-01 https://louisvillegop.com/sitemap114.xml 2026-12-01 https://louisvillegop.com/sitemap715.xml 2026-12-01 https://louisvillegop.com/sitemap432.xml 2026-12-01 https://louisvillegop.com/sitemap210.xml 2026-12-01 https://louisvillegop.com/sitemap753.xml 2026-12-01 https://louisvillegop.com/sitemap628.xml 2026-12-01 https://louisvillegop.com/sitemap887.xml 2026-12-01 https://louisvillegop.com/sitemap177.xml 2026-12-01 https://louisvillegop.com/sitemap494.xml 2026-12-01 https://louisvillegop.com/sitemap818.xml 2026-12-01 https://louisvillegop.com/sitemap473.xml 2026-12-01 https://louisvillegop.com/sitemap140.xml 2026-12-01 https://louisvillegop.com/sitemap427.xml 2026-12-01 https://louisvillegop.com/sitemap18.xml 2026-12-01 https://louisvillegop.com/sitemap691.xml 2026-12-01 https://louisvillegop.com/sitemap408.xml 2026-12-01 https://louisvillegop.com/sitemap607.xml 2026-12-01 https://louisvillegop.com/sitemap115.xml 2026-12-01 https://louisvillegop.com/sitemap321.xml 2026-12-01 https://louisvillegop.com/sitemap733.xml 2026-12-01 https://louisvillegop.com/sitemap203.xml 2026-12-01 https://louisvillegop.com/sitemap610.xml 2026-12-01 https://louisvillegop.com/sitemap532.xml 2026-12-01 https://louisvillegop.com/sitemap379.xml 2026-12-01 https://louisvillegop.com/sitemap394.xml 2026-12-01 https://louisvillegop.com/sitemap833.xml 2026-12-01 https://louisvillegop.com/sitemap119.xml 2026-12-01 https://louisvillegop.com/sitemap799.xml 2026-12-01 https://louisvillegop.com/sitemap665.xml 2026-12-01 https://louisvillegop.com/sitemap732.xml 2026-12-01 https://louisvillegop.com/sitemap371.xml 2026-12-01 https://louisvillegop.com/sitemap802.xml 2026-12-01 https://louisvillegop.com/sitemap8.xml 2026-12-01 https://louisvillegop.com/sitemap103.xml 2026-12-01 https://louisvillegop.com/sitemap306.xml 2026-12-01 https://louisvillegop.com/sitemap172.xml 2026-12-01 https://louisvillegop.com/sitemap208.xml 2026-12-01 https://louisvillegop.com/sitemap925.xml 2026-12-01 https://louisvillegop.com/sitemap235.xml 2026-12-01 https://louisvillegop.com/sitemap593.xml 2026-12-01 https://louisvillegop.com/sitemap878.xml 2026-12-01 https://louisvillegop.com/sitemap846.xml 2026-12-01 https://louisvillegop.com/sitemap752.xml 2026-12-01 https://louisvillegop.com/sitemap796.xml 2026-12-01 https://louisvillegop.com/sitemap616.xml 2026-12-01 https://louisvillegop.com/sitemap11.xml 2026-12-01 https://louisvillegop.com/sitemap445.xml 2026-12-01 https://louisvillegop.com/sitemap820.xml 2026-12-01 https://louisvillegop.com/sitemap869.xml 2026-12-01