|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the official PHP MCP SDK. |
| 5 | + * |
| 6 | + * A collaboration between Symfony and the PHP Foundation. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Mcp\Tests\Unit\Server\Handler\Request; |
| 13 | + |
| 14 | +use Mcp\Schema\Enum\ProtocolVersion; |
| 15 | +use Mcp\Schema\Implementation; |
| 16 | +use Mcp\Schema\JsonRpc\MessageInterface; |
| 17 | +use Mcp\Schema\Request\InitializeRequest; |
| 18 | +use Mcp\Schema\Result\InitializeResult; |
| 19 | +use Mcp\Schema\ServerCapabilities; |
| 20 | +use Mcp\Server\Configuration; |
| 21 | +use Mcp\Server\Handler\Request\InitializeHandler; |
| 22 | +use Mcp\Server\Session\SessionInterface; |
| 23 | +use PHPUnit\Framework\Attributes\TestDox; |
| 24 | +use PHPUnit\Framework\TestCase; |
| 25 | + |
| 26 | +class InitializeHandlerTest extends TestCase |
| 27 | +{ |
| 28 | + #[TestDox('uses configuration protocol version when provided')] |
| 29 | + public function testHandleUsesConfigurationProtocolVersion(): void |
| 30 | + { |
| 31 | + $customProtocolVersion = ProtocolVersion::V2024_11_05; |
| 32 | + |
| 33 | + $configuration = new Configuration( |
| 34 | + serverInfo: new Implementation('server', '1.2.3'), |
| 35 | + capabilities: new ServerCapabilities(), |
| 36 | + protocolVersion: $customProtocolVersion, |
| 37 | + ); |
| 38 | + |
| 39 | + $handler = new InitializeHandler($configuration); |
| 40 | + |
| 41 | + $session = $this->createMock(SessionInterface::class); |
| 42 | + $session->expects($this->once()) |
| 43 | + ->method('set') |
| 44 | + ->with('client_info', [ |
| 45 | + 'name' => 'client-app', |
| 46 | + 'version' => '1.0.0', |
| 47 | + ]); |
| 48 | + |
| 49 | + $request = InitializeRequest::fromArray([ |
| 50 | + 'jsonrpc' => MessageInterface::JSONRPC_VERSION, |
| 51 | + 'id' => 'request-1', |
| 52 | + 'method' => InitializeRequest::getMethod(), |
| 53 | + 'params' => [ |
| 54 | + 'protocolVersion' => ProtocolVersion::V2024_11_05->value, |
| 55 | + 'capabilities' => [], |
| 56 | + 'clientInfo' => [ |
| 57 | + 'name' => 'client-app', |
| 58 | + 'version' => '1.0.0', |
| 59 | + ], |
| 60 | + ], |
| 61 | + ]); |
| 62 | + |
| 63 | + $response = $handler->handle($request, $session); |
| 64 | + |
| 65 | + $this->assertInstanceOf(InitializeResult::class, $response->result); |
| 66 | + |
| 67 | + /** @var InitializeResult $result */ |
| 68 | + $result = $response->result; |
| 69 | + |
| 70 | + $this->assertSame($customProtocolVersion, $result->protocolVersion); |
| 71 | + $this->assertSame( |
| 72 | + $customProtocolVersion->value, |
| 73 | + $result->jsonSerialize()['protocolVersion'] |
| 74 | + ); |
| 75 | + } |
| 76 | +} |
0 commit comments