Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
php-sdk/src/Server/RequestContext.php at main · modelcontextprotocol/php-sdk · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
modelcontextprotocol
php-sdk
Repository navigation
Code
Issues
48
(48)
Pull requests
9
(9)
Actions
Projects
Security and quality
1
(1)
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
php-sdk
/
src
/
Server
/
RequestContext.php
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
170 lines (144 loc) · 5.42 KB
main
Breadcrumbs
php-sdk
/
src
/
Server
/
RequestContext.php
Copy path
Top
File metadata and controls
Code
Blame
170 lines (144 loc) · 5.42 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
<?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
\
Capability
\
Logger
\
ClientLogger
;
use
Mcp
\
Exception
\
LogicException
;
use
Mcp
\
Schema
\
ClientCapabilities
;
use
Mcp
\
Schema
\
Enum
\
ProtocolVersion
;
use
Mcp
\
Schema
\
JsonRpc
\
Request
;
use
Mcp
\
Server
\
Authorization
\
AccessToken
;
use
Mcp
\
Server
\
Session
\
SessionInterface
;
use
Mcp
\
Server
\
Stateless
\
InputContext
;
use
Mcp
\
Server
\
Stateless
\
RequestMeta
;
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. 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
;
}
}
You can’t perform that action at this time.