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/tests/Integration/StatelessClientTest.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
View 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
/
tests
/
Integration
/
StatelessClientTest.php
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
172 lines (137 loc) · 5.96 KB
main
Breadcrumbs
php-sdk
/
tests
/
Integration
/
StatelessClientTest.php
Copy path
Top
File metadata and controls
Code
Blame
172 lines (137 loc) · 5.96 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
171
172
<?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
\
Tests
\
Integration
;
use
Mcp
\
Client
;
use
Mcp
\
Client
\
Handler
\
Request
\
RequestHandlerInterface
;
use
Mcp
\
Client
\
Transport
\
HttpTransport
;
use
Mcp
\
Schema
\
ClientCapabilities
;
use
Mcp
\
Schema
\
Content
\
TextContent
;
use
Mcp
\
Schema
\
Enum
\
ElicitAction
;
use
Mcp
\
Schema
\
Enum
\
ProtocolVersion
;
use
Mcp
\
Schema
\
JsonRpc
\
Request
;
use
Mcp
\
Schema
\
JsonRpc
\
Response
;
use
Mcp
\
Schema
\
Request
\
ElicitRequest
;
use
Mcp
\
Schema
\
Result
\
CallToolResult
;
use
Mcp
\
Schema
\
Result
\
ElicitResult
;
use
PHPUnit
\
Framework
\
Attributes
\
TestDox
;
use
PHPUnit
\
Framework
\
TestCase
;
use
Symfony
\
Component
\
Process
\
Process
;
/**
* The SDK's client against the SDK's server, both on 2026-07-28.
*
* {@see StatelessLifecycleTest} drives the same example with hand-built HTTP,
* which proves the server answers a conforming client. This proves the other
* half: that the client *is* one — no handshake, an envelope on every request,
* headers the server's own validator accepts, and a multi round-trip call
* completed without the caller doing anything special.
*/
class
StatelessClientTest
extends
TestCase
{
private
const
SERVER
=
__DIR__
.
'
/../../examples/server/stateless-lifecycle/server.php
'
;
private
Process
$
server
;
private
int
$
port
;
protected
function
setUp
():
void
{
$
this
->
port
=
8900
+ (
getmypid
() %
300
);
$
this
->
server
=
new
Process
([
'
php
'
,
'
-S
'
,
\sprintf
(
'
127.0.0.1:%d
'
,
$
this
->
port
),
self
::
SERVER
]);
$
this
->
server
->
start
();
$
deadline
=
microtime
(
true
) +
5
;
while
(
microtime
(
true
) <
$
deadline
) {
if
(@
fsockopen
(
'
127.0.0.1
'
,
$
this
->
port
,
$
errno
,
$
error
,
0.1
)) {
return
;
}
usleep
(
50_000
);
}
$
this
->
fail
(
\sprintf
(
'
The example server did not start: %s
'
,
$
this
->
server
->
getErrorOutput
()));
}
protected
function
tearDown
():
void
{
$
this
->
server
->
stop
();
}
#[TestDox(
'
connects without a handshake and learns who it is talking to
'
)]
public
function
testConnectWithoutHandshake
():
void
{
$
client
=
$
this
->
connect
();
$
this
->
assertTrue
(
$
client
->
isConnected
());
$
this
->
assertSame
(ProtocolVersion::
V2026_07_28
,
$
client
->
getProtocolVersion
());
$
this
->
assertSame
(
'
Stateless Lifecycle Demo
'
,
$
client
->
getServerInfo
()?->name);
$
this
->
assertTrue
(
$
client
->
getServerCapabilities
()?->tools);
$
client
->
disconnect
();
}
#[TestDox(
'
lists and calls a tool, which the server accepts on the first try
'
)]
public
function
testToolCall
():
void
{
$
client
=
$
this
->
connect
();
$
tools
=
$
client
->
listTools
();
$
this
->
assertContains
(
'
get_weather
'
,
array_map
(
static
fn
(
$
tool
) =>
$
tool
->
name
,
$
tools
->
tools
));
// A header the server disagreed with would come back as -32020, so a
// plain result is also the assertion that the mirroring was right.
$
result
=
$
client
->
callTool
(
'
get_weather
'
, [
'
city
'
=>
'
Munich
'
]);
$
this
->
assertStringContainsString
(
'
Munich
'
,
self
::
text
(
$
result
));
$
client
->
disconnect
();
}
#[TestDox(
'
completes a multi round-trip call by answering the server itself
'
)]
public
function
testMultiRoundTripIsTransparentToTheCaller
():
void
{
$
client
=
$
this
->
connect
(elicitation:
true
);
// One call from here; two on the wire. The server asks for a name, the
// client answers from its own handler and retries with the sealed
// `requestState`, and the caller only ever sees the finished result.
$
result
=
$
client
->
callTool
(
'
greet
'
, []);
$
this
->
assertStringContainsString
(
'
Hello, Ada!
'
,
self
::
text
(
$
result
));
$
client
->
disconnect
();
}
#[TestDox(
'
a capability the client never declared is refused up front, and costs only that call
'
)]
public
function
testUndeclaredCapabilityIsRefused
():
void
{
// No elicitation capability, and the envelope says so on every request,
// so the server refuses rather than asking for something that could
// never be answered.
$
client
=
$
this
->
connect
();
try
{
$
client
->
callTool
(
'
greet
'
, []);
$
this
->
fail
(
'
Expected the server to refuse the undeclared capability.
'
);
}
catch
(
\
Throwable
$
e
) {
$
this
->
assertStringContainsString
(
'
did not declare it can provide
'
,
$
e
->
getMessage
());
}
// The connection is stateless, so a failed call costs nothing.
$
this
->
assertStringContainsString
(
'
Munich
'
,
self
::
text
(
$
client
->
callTool
(
'
get_weather
'
, [
'
city
'
=>
'
Munich
'
])));
$
client
->
disconnect
();
}
private
static
function
text
(
CallToolResult
$
result
):
string
{
$
first
=
$
result
->
content
[
0
] ??
null
;
self
::
assertInstanceOf
(TextContent::class,
$
first
);
return
$
first
->
text
;
}
private
function
connect
(
bool
$
elicitation
=
false
):
Client
{
$
builder
= Client::
builder
()
->
setClientInfo
(
'
stateless-integration-client
'
,
'
1.0.0
'
)
->
setProtocolVersion
(ProtocolVersion::
V2026_07_28
)
->
setRequestTimeout
(
10
);
if
(
$
elicitation
) {
$
builder
->
setCapabilities
(
new
ClientCapabilities
(elicitation:
true
));
$
builder
->
addRequestHandler
(
new
class
implements
RequestHandlerInterface {
public
function
supports
(
Request
$
request
):
bool
{
return
$
request
instanceof
ElicitRequest;
}
public
function
handle
(
Request
$
request
):
Response
{
return
new
Response
(
$
request
->
getId
(),
new
ElicitResult
(ElicitAction::Accept, [
'
name
'
=>
'
Ada
'
]));
}
});
}
$
client
=
$
builder
->
build
();
$
client
->
connect
(
new
HttpTransport
(
\sprintf
(
'
http://127.0.0.1:%d/
'
,
$
this
->
port
)));
return
$
client
;
}
}
You can’t perform that action at this time.