Skip to content

Commit 3aef606

Browse files
committed
Ability to set custom protocol version
1 parent 6100ffc commit 3aef606

File tree

7 files changed

+124
-4
lines changed

7 files changed

+124
-4
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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\Schema\Enum;
13+
14+
/**
15+
* Available protocol versions for MCP.
16+
*/
17+
enum ProtocolVersion: string
18+
{
19+
case V2024_11_05 = '2024-11-05';
20+
21+
case V2025_03_26 = '2025-03-26';
22+
23+
case V2025_06_18 = '2025-06-18';
24+
}

src/Schema/JsonRpc/MessageInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Mcp\Schema\JsonRpc;
1313

14+
use Mcp\Schema\Enum\ProtocolVersion;
15+
1416
/**
1517
* Refers to any valid JSON-RPC object that can be decoded off the wire, or encoded to be sent.
1618
*
@@ -19,5 +21,5 @@
1921
interface MessageInterface extends \JsonSerializable
2022
{
2123
public const JSONRPC_VERSION = '2.0';
22-
public const PROTOCOL_VERSION = '2025-06-18';
24+
public const PROTOCOL_VERSION = ProtocolVersion::V2025_06_18;
2325
}

src/Schema/Result/InitializeResult.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Mcp\Schema\Result;
1313

1414
use Mcp\Exception\InvalidArgumentException;
15+
use Mcp\Schema\Enum\ProtocolVersion;
1516
use Mcp\Schema\Implementation;
1617
use Mcp\Schema\JsonRpc\MessageInterface;
1718
use Mcp\Schema\JsonRpc\Response;
@@ -38,6 +39,7 @@ public function __construct(
3839
public readonly Implementation $serverInfo,
3940
public readonly ?string $instructions = null,
4041
public readonly ?array $_meta = null,
42+
public readonly ?ProtocolVersion $protocolVersion = null,
4143
) {
4244
}
4345

@@ -66,7 +68,8 @@ public static function fromArray(array $data): self
6668
ServerCapabilities::fromArray($data['capabilities']),
6769
Implementation::fromArray($data['serverInfo']),
6870
$data['instructions'] ?? null,
69-
$data['_meta'] ?? null
71+
$data['_meta'] ?? null,
72+
ProtocolVersion::tryFrom($data['protocolVersion']),
7073
);
7174
}
7275

@@ -81,8 +84,9 @@ public static function fromArray(array $data): self
8184
*/
8285
public function jsonSerialize(): array
8386
{
87+
$protocolVersion = $this->protocolVersion ?? MessageInterface::PROTOCOL_VERSION;
8488
$data = [
85-
'protocolVersion' => MessageInterface::PROTOCOL_VERSION,
89+
'protocolVersion' => $protocolVersion->value,
8690
'capabilities' => $this->capabilities,
8791
'serverInfo' => $this->serverInfo,
8892
];

src/Server/Builder.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Mcp\Exception\ConfigurationException;
2828
use Mcp\JsonRpc\MessageFactory;
2929
use Mcp\Schema\Annotations;
30+
use Mcp\Schema\Enum\ProtocolVersion;
3031
use Mcp\Schema\Implementation;
3132
use Mcp\Schema\Prompt;
3233
use Mcp\Schema\PromptArgument;
@@ -75,6 +76,8 @@ final class Builder
7576

7677
private ?string $instructions = null;
7778

79+
private ?ProtocolVersion $protocolVersion = null;
80+
7881
/**
7982
* @var array<int, RequestHandlerInterface>
8083
*/
@@ -293,6 +296,13 @@ public function setDiscovery(
293296
return $this;
294297
}
295298

299+
public function setProtocolVersion(?ProtocolVersion $protocolVersion): self
300+
{
301+
$this->protocolVersion = $protocolVersion;
302+
303+
return $this;
304+
}
305+
296306
/**
297307
* Manually registers a tool handler.
298308
*
@@ -391,7 +401,7 @@ public function build(): Server
391401
$messageFactory = MessageFactory::make();
392402

393403
$capabilities = $registry->getCapabilities();
394-
$configuration = new Configuration($this->serverInfo, $capabilities, $this->paginationLimit, $this->instructions);
404+
$configuration = new Configuration($this->serverInfo, $capabilities, $this->paginationLimit, $this->instructions, $this->protocolVersion);
395405
$referenceHandler = new ReferenceHandler($container);
396406

397407
$requestHandlers = array_merge($this->requestHandlers, [

src/Server/Configuration.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Mcp\Server;
1313

14+
use Mcp\Schema\Enum\ProtocolVersion;
1415
use Mcp\Schema\Implementation;
1516
use Mcp\Schema\ServerCapabilities;
1617

@@ -34,6 +35,7 @@ public function __construct(
3435
public readonly ServerCapabilities $capabilities,
3536
public readonly int $paginationLimit = 50,
3637
public readonly ?string $instructions = null,
38+
public readonly ?ProtocolVersion $protocolVersion = null,
3739
) {
3840
}
3941
}

src/Server/Handler/Request/InitializeHandler.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public function handle(Request $request, SessionInterface $session): Response
4747
$this->configuration->capabilities ?? new ServerCapabilities(),
4848
$this->configuration->serverInfo ?? new Implementation(),
4949
$this->configuration?->instructions,
50+
null,
51+
$this->configuration?->protocolVersion,
5052
),
5153
);
5254
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)