| 1 |
<?php |
| 2 |
/** |
| 3 |
* MCP protocol version negotiation. |
| 4 |
* |
| 5 |
* @package McpAdapter |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace WP\MCP\Core; |
| 11 |
|
| 12 |
/** |
| 13 |
* Negotiates the MCP protocol version between client and server. |
| 14 |
* |
| 15 |
* If the client requests a supported version, the server echoes it back. |
| 16 |
* Otherwise the server falls back to the latest supported version. |
| 17 |
* |
| 18 |
* This is a Core layer class — no WordPress function calls. |
| 19 |
* |
| 20 |
* @since 0.5.0 |
| 21 |
*/ |
| 22 |
final class McpVersionNegotiator { |
| 23 |
|
| 24 |
/** |
| 25 |
* Protocol versions supported by this server, ordered newest-first. |
| 26 |
* |
| 27 |
* @var array<int, string> |
| 28 |
*/ |
| 29 |
// phpcs:ignore SlevomatCodingStandard.Classes.DisallowMultiConstantDefinition -- False positive: sniff mistakes array() commas for multi-const commas (only handles short syntax). |
| 30 |
public const SUPPORTED_PROTOCOL_VERSIONS = array( |
| 31 |
'2025-11-25', |
| 32 |
'2025-06-18', |
| 33 |
'2024-11-05', |
| 34 |
); |
| 35 |
|
| 36 |
/** |
| 37 |
* Negotiate the protocol version to use for a session. |
| 38 |
* |
| 39 |
* If the client-requested version is in the supported list it is echoed |
| 40 |
* back verbatim. Otherwise the latest supported version is returned. |
| 41 |
* |
| 42 |
* @since 0.5.0 |
| 43 |
* |
| 44 |
* @param string $client_version The protocol version requested by the client. |
| 45 |
* |
| 46 |
* @return string The negotiated protocol version. |
| 47 |
*/ |
| 48 |
public static function negotiate( string $client_version ): string { |
| 49 |
if ( in_array( $client_version, self::SUPPORTED_PROTOCOL_VERSIONS, true ) ) { |
| 50 |
return $client_version; |
| 51 |
} |
| 52 |
|
| 53 |
return self::SUPPORTED_PROTOCOL_VERSIONS[0]; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Check whether a given version string is supported. |
| 58 |
* |
| 59 |
* @since 0.5.0 |
| 60 |
* |
| 61 |
* @param string $version The protocol version to check. |
| 62 |
* |
| 63 |
* @return bool True when the version is in the supported list, false otherwise. |
| 64 |
*/ |
| 65 |
public static function is_supported( string $version ): bool { |
| 66 |
return in_array( $version, self::SUPPORTED_PROTOCOL_VERSIONS, true ); |
| 67 |
} |
| 68 |
} |
| 69 |
|