-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathtest_stateless_mode.py
More file actions
263 lines (228 loc) · 8.33 KB
/
test_stateless_mode.py
File metadata and controls
263 lines (228 loc) · 8.33 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
"""Tests for stateless HTTP mode limitations.
Stateless HTTP mode does not support server-to-client requests because there
is no persistent connection for bidirectional communication. These tests verify
that appropriate errors are raised when attempting to use unsupported features.
See: https://github.com/modelcontextprotocol/python-sdk/issues/1097
"""
import anyio
import pytest
import mcp.types as types
from mcp.server.models import InitializationOptions
from mcp.server.session import ServerSession
from mcp.shared.message import SessionMessage
from mcp.types import ServerCapabilities
def create_test_streams():
"""Create memory streams for testing."""
server_to_client_send, server_to_client_receive = anyio.create_memory_object_stream[SessionMessage](1)
client_to_server_send, client_to_server_receive = anyio.create_memory_object_stream[SessionMessage | Exception](1)
return (
server_to_client_send,
server_to_client_receive,
client_to_server_send,
client_to_server_receive,
)
def create_init_options():
"""Create default initialization options for testing."""
return InitializationOptions(
server_name="test",
server_version="0.1.0",
capabilities=ServerCapabilities(),
)
@pytest.mark.anyio
async def test_list_roots_fails_in_stateless_mode():
"""Test that list_roots raises RuntimeError in stateless mode."""
(
server_to_client_send,
server_to_client_receive,
client_to_server_send,
client_to_server_receive,
) = create_test_streams()
async with (
client_to_server_send,
client_to_server_receive,
server_to_client_send,
server_to_client_receive,
):
async with ServerSession(
client_to_server_receive,
server_to_client_send,
create_init_options(),
stateless=True,
) as session:
with pytest.raises(RuntimeError) as exc_info:
await session.list_roots()
assert "stateless HTTP mode" in str(exc_info.value)
assert "list_roots" in str(exc_info.value)
@pytest.mark.anyio
async def test_create_message_fails_in_stateless_mode():
"""Test that create_message raises RuntimeError in stateless mode."""
(
server_to_client_send,
server_to_client_receive,
client_to_server_send,
client_to_server_receive,
) = create_test_streams()
async with (
client_to_server_send,
client_to_server_receive,
server_to_client_send,
server_to_client_receive,
):
async with ServerSession(
client_to_server_receive,
server_to_client_send,
create_init_options(),
stateless=True,
) as session:
with pytest.raises(RuntimeError) as exc_info:
await session.create_message(
messages=[
types.SamplingMessage(
role="user",
content=types.TextContent(type="text", text="hello"),
)
],
max_tokens=100,
)
assert "stateless HTTP mode" in str(exc_info.value)
assert "sampling" in str(exc_info.value)
@pytest.mark.anyio
async def test_elicit_form_fails_in_stateless_mode():
"""Test that elicit_form raises RuntimeError in stateless mode."""
(
server_to_client_send,
server_to_client_receive,
client_to_server_send,
client_to_server_receive,
) = create_test_streams()
async with (
client_to_server_send,
client_to_server_receive,
server_to_client_send,
server_to_client_receive,
):
async with ServerSession(
client_to_server_receive,
server_to_client_send,
create_init_options(),
stateless=True,
) as session:
with pytest.raises(RuntimeError) as exc_info:
await session.elicit_form(
message="Please provide input",
requestedSchema={"type": "object", "properties": {}},
)
assert "stateless HTTP mode" in str(exc_info.value)
assert "elicitation" in str(exc_info.value)
@pytest.mark.anyio
async def test_elicit_url_fails_in_stateless_mode():
"""Test that elicit_url raises RuntimeError in stateless mode."""
(
server_to_client_send,
server_to_client_receive,
client_to_server_send,
client_to_server_receive,
) = create_test_streams()
async with (
client_to_server_send,
client_to_server_receive,
server_to_client_send,
server_to_client_receive,
):
async with ServerSession(
client_to_server_receive,
server_to_client_send,
create_init_options(),
stateless=True,
) as session:
with pytest.raises(RuntimeError) as exc_info:
await session.elicit_url(
message="Please authenticate",
url="https://example.com/auth",
elicitation_id="test-123",
)
assert "stateless HTTP mode" in str(exc_info.value)
assert "elicitation" in str(exc_info.value)
@pytest.mark.anyio
async def test_elicit_deprecated_fails_in_stateless_mode():
"""Test that the deprecated elicit method also fails in stateless mode."""
(
server_to_client_send,
server_to_client_receive,
client_to_server_send,
client_to_server_receive,
) = create_test_streams()
async with (
client_to_server_send,
client_to_server_receive,
server_to_client_send,
server_to_client_receive,
):
async with ServerSession(
client_to_server_receive,
server_to_client_send,
create_init_options(),
stateless=True,
) as session:
with pytest.raises(RuntimeError) as exc_info:
await session.elicit(
message="Please provide input",
requestedSchema={"type": "object", "properties": {}},
)
assert "stateless HTTP mode" in str(exc_info.value)
assert "elicitation" in str(exc_info.value)
@pytest.mark.anyio
async def test_require_stateful_mode_does_not_raise_in_stateful_mode():
"""Test that _require_stateful_mode does not raise in stateful mode."""
(
server_to_client_send,
server_to_client_receive,
client_to_server_send,
client_to_server_receive,
) = create_test_streams()
async with (
client_to_server_send,
client_to_server_receive,
server_to_client_send,
server_to_client_receive,
):
async with ServerSession(
client_to_server_receive,
server_to_client_send,
create_init_options(),
stateless=False, # Stateful mode
) as session:
# These should not raise - the check passes in stateful mode
session._require_stateful_mode("list_roots")
session._require_stateful_mode("sampling")
session._require_stateful_mode("elicitation")
@pytest.mark.anyio
async def test_stateless_error_message_is_actionable():
"""Test that the error message provides actionable guidance."""
(
server_to_client_send,
server_to_client_receive,
client_to_server_send,
client_to_server_receive,
) = create_test_streams()
async with (
client_to_server_send,
client_to_server_receive,
server_to_client_send,
server_to_client_receive,
):
async with ServerSession(
client_to_server_receive,
server_to_client_send,
create_init_options(),
stateless=True,
) as session:
with pytest.raises(RuntimeError) as exc_info:
await session.list_roots()
error_message = str(exc_info.value)
# Should mention it's stateless mode
assert "stateless HTTP mode" in error_message
# Should explain why it doesn't work
assert "server-to-client requests" in error_message
# Should tell user how to fix it
assert "stateless_http=False" in error_message