Repository navigation
Expand file tree
/
Copy pathRequestContext.php
More file actions
171 lines (145 loc) · 5.51 KB
/
Copy pathRequestContext.php
File metadata and controls
171 lines (145 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/*
* This file is part of the official PHP MCP SDK.
*
* A collaboration between Symfony and the PHP Foundation.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mcp\Server;
use Mcp\Schema\ClientCapabilities;
use Mcp\Schema\Enum\ProtocolVersion;
use Mcp\Schema\JsonRpc\Request;
use Mcp\Schema\RequestMeta;
use Mcp\Server\Authorization\AccessToken;
use Mcp\Server\Capability\Logger\ClientLogger;
use Mcp\Server\Exception\LogicException;
use Mcp\Server\Session\SessionInterface;
use Mcp\Server\Stateless\InputContext;
use Mcp\Server\Stateless\RequestStateCodec;
/**
* Context related to a single request. This includes information about the session and
* will build request-specific objects.
*
* This is a stateful object, it should not be reused between requests.
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
final class RequestContext
{
/**
* `_meta` key carrying the protocol revision of a single request, introduced
* with the modern era that replaced the `initialize` handshake.
*
* @see https://modelcontextprotocol.io/specification/2026-07-28/basic/versioning
*/
private const PROTOCOL_VERSION_META_KEY = 'io.modelcontextprotocol/protocolVersion';
private ?ClientGateway $clientGateway = null;
private ?ClientLogger $clientLogger = null;
public function __construct(
private readonly SessionInterface $session,
private readonly Request $request,
) {
}
public function getRequest(): Request
{
return $this->request;
}
public function getSession(): SessionInterface
{
return $this->session;
}
/**
* The protocol revision this request is served under.
*
* Modern revisions declare it per request in `_meta`, handshake ones negotiate
* it once and keep it on the session, which a server without sessions fills from
* the `MCP-Protocol-Version` header. Neither is guaranteed to be present — a
* transport may skip `initialize` entirely — so this falls back to the newest
* handshake revision, whose rules hold for every revision below it too.
*/
public function getProtocolVersion(): ProtocolVersion
{
$requested = $this->request->getMeta()[self::PROTOCOL_VERSION_META_KEY]
?? $this->session->get('protocol_version');
if (!\is_string($requested)) {
return ProtocolVersion::latestHandshake();
}
return ProtocolVersion::tryFrom($requested) ?? ProtocolVersion::latestHandshake();
}
/**
* The access token this request was authorized with, or null when the
* transport does not authorize requests — see
* {@see Transport\Http\Middleware\AuthorizationMiddleware}.
*/
public function getAccessToken(): ?AccessToken
{
$accessToken = $this->session->get(AccessToken::class);
return $accessToken instanceof AccessToken ? $accessToken : null;
}
public function getClientGateway(): ClientGateway
{
if (null == $this->clientGateway) {
$this->clientGateway = new ClientGateway($this->session, $this->request);
}
return $this->clientGateway;
}
/**
* What a multi round-trip retry carried back, or null on a first call —
* which is the signal to return an
* {@see \Mcp\Schema\Result\InputRequiredResult} instead of an answer.
*/
public function getInputContext(): ?InputContext
{
$context = $this->session->get(InputContext::class);
return $context instanceof InputContext ? $context : null;
}
/**
* What the client declared on this request. A server MUST NOT ask for
* input the client cannot supply. Null in the handshake era, where
* capabilities are connection state.
*/
public function getClientCapabilities(): ?ClientCapabilities
{
$meta = $this->session->get(RequestMeta::class);
return $meta instanceof RequestMeta ? $meta->clientCapabilities : null;
}
/**
* The W3C trace context this request carried, if any.
*
* Values are passed through exactly as they arrived — validating or
* regenerating them is the tracing library's job, not the protocol's. The
* SDK echoes them onto the notifications a handler emits so a span stays
* joined across the stream.
*
* @return array<string, string> keyed by `traceparent`, `tracestate`, `baggage`
*/
public function getTraceContext(): array
{
$meta = $this->session->get(RequestMeta::class);
return $meta instanceof RequestMeta ? $meta->traceContext : [];
}
/**
* Seals handler context into the string an
* {@see \Mcp\Schema\Result\InputRequiredResult} carries to the client.
* Signed, not encrypted: nothing secret belongs in the payload.
*
* @param array<string, mixed> $payload
*/
public function mintRequestState(array $payload): string
{
$codec = $this->session->get(RequestStateCodec::class);
if (!$codec instanceof RequestStateCodec) {
throw new LogicException('No requestState signing key is configured; call Builder::setRequestState() before returning state from a handler.');
}
return $codec->mint($payload);
}
public function getClientLogger(): ClientLogger
{
if (null === $this->clientLogger) {
$this->clientLogger = new ClientLogger($this->getClientGateway());
}
return $this->clientLogger;
}
}