1: <?php
2:
3: declare(strict_types=1);
4:
5: /**
6: * This file is part of the Nexus MCP SDK package.
7: *
8: * (c) 2026 John Paul E. Balandan, CPA <paulbalandan@gmail.com>
9: *
10: * For the full copyright and license information, please view
11: * the LICENSE file that was distributed with this source code.
12: */
13:
14: namespace Nexus\Mcp\Server\Handler\Request;
15:
16: use Nexus\Mcp\Core\Handler\AbstractContext;
17: use Nexus\Mcp\Core\Handler\RequestHandlerInterface;
18: use Nexus\Mcp\Core\Schema\Implementation;
19: use Nexus\Mcp\Core\Schema\JsonRpc\JsonRpcRequest;
20: use Nexus\Mcp\Core\Schema\MetaObject;
21: use Nexus\Mcp\Core\Schema\ProtocolVersion;
22: use Nexus\Mcp\Core\Schema\Result\InitializeResult;
23: use Nexus\Mcp\Core\Schema\ServerCapabilities;
24: use Nexus\Mcp\Server\ServerContext;
25:
26: /**
27: * Handles the `initialize` request, returning the server's protocol version,
28: * capabilities, and identification info.
29: *
30: * @implements RequestHandlerInterface<'initialize', InitializeResult, ServerContext>
31: */
32: final readonly class InitializeRequestHandler implements RequestHandlerInterface
33: {
34: /**
35: * @param null|non-empty-string $instructions
36: */
37: public function __construct(
38: private Implementation $serverInfo,
39: private ServerCapabilities $capabilities,
40: private ?string $instructions = null,
41: private MetaObject $meta = new MetaObject(),
42: ) {
43: }
44:
45: #[\Override]
46: public function handle(JsonRpcRequest $request, AbstractContext $context): InitializeResult
47: {
48: // The server supports exactly one revision, so the spec's negotiation rule
49: // (echo the requested version when supported, otherwise return the latest
50: // supported) collapses to LATEST_VERSION for every request.
51: return new InitializeResult(
52: new ProtocolVersion(ProtocolVersion::LATEST_VERSION),
53: $this->capabilities,
54: $this->serverInfo,
55: $this->instructions,
56: $this->meta,
57: );
58: }
59: }
60: