Skip to content

Commit a529f39

Browse files
committed
Fixes from PR review
1 parent 3adff13 commit a529f39

File tree

3 files changed

+33
-52
lines changed

3 files changed

+33
-52
lines changed

src/mcp/client/session.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,9 @@ async def list_resources(
209209
types.ClientRequest(
210210
types.ListResourcesRequest(
211211
method="resources/list",
212-
params=types.PaginatedRequestParams(cursor=cursor),
212+
params=types.PaginatedRequestParams(cursor=cursor)
213+
if cursor is not None
214+
else None,
213215
)
214216
),
215217
types.ListResourcesResult,
@@ -223,7 +225,9 @@ async def list_resource_templates(
223225
types.ClientRequest(
224226
types.ListResourceTemplatesRequest(
225227
method="resources/templates/list",
226-
params=types.PaginatedRequestParams(cursor=cursor),
228+
params=types.PaginatedRequestParams(cursor=cursor)
229+
if cursor is not None
230+
else None,
227231
)
228232
),
229233
types.ListResourceTemplatesResult,
@@ -295,7 +299,9 @@ async def list_prompts(self, cursor: str | None = None) -> types.ListPromptsResu
295299
types.ClientRequest(
296300
types.ListPromptsRequest(
297301
method="prompts/list",
298-
params=types.PaginatedRequestParams(cursor=cursor),
302+
params=types.PaginatedRequestParams(cursor=cursor)
303+
if cursor is not None
304+
else None,
299305
)
300306
),
301307
types.ListPromptsResult,
@@ -340,7 +346,9 @@ async def list_tools(self, cursor: str | None = None) -> types.ListToolsResult:
340346
types.ClientRequest(
341347
types.ListToolsRequest(
342348
method="tools/list",
343-
params=types.PaginatedRequestParams(cursor=cursor),
349+
params=types.PaginatedRequestParams(cursor=cursor)
350+
if cursor is not None
351+
else None,
344352
)
345353
),
346354
types.ListToolsResult,

src/mcp/types.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ class Request(BaseModel, Generic[RequestParamsT, MethodT]):
8787
model_config = ConfigDict(extra="allow")
8888

8989

90+
class PaginatedRequest(
91+
Request[PaginatedRequestParams | None, MethodT], Generic[MethodT]
92+
):
93+
"""Base class for paginated requests,
94+
matching the schema's PaginatedRequest interface."""
95+
96+
params: PaginatedRequestParams | None = None
97+
98+
9099
class Notification(BaseModel, Generic[NotificationParamsT, MethodT]):
91100
"""Base class for JSON-RPC notifications."""
92101

@@ -358,9 +367,7 @@ class ProgressNotification(
358367
params: ProgressNotificationParams
359368

360369

361-
class ListResourcesRequest(
362-
Request[PaginatedRequestParams | None, Literal["resources/list"]]
363-
):
370+
class ListResourcesRequest(PaginatedRequest[Literal["resources/list"]]):
364371
"""Sent from the client to request a list of resources the server has."""
365372

366373
method: Literal["resources/list"]
@@ -423,7 +430,7 @@ class ListResourcesResult(PaginatedResult):
423430

424431

425432
class ListResourceTemplatesRequest(
426-
Request[PaginatedRequestParams | None, Literal["resources/templates/list"]]
433+
PaginatedRequest[Literal["resources/templates/list"]]
427434
):
428435
"""Sent from the client to request a list of resource templates the server has."""
429436

@@ -570,9 +577,7 @@ class ResourceUpdatedNotification(
570577
params: ResourceUpdatedNotificationParams
571578

572579

573-
class ListPromptsRequest(
574-
Request[PaginatedRequestParams | None, Literal["prompts/list"]]
575-
):
580+
class ListPromptsRequest(PaginatedRequest[Literal["prompts/list"]]):
576581
"""Sent from the client to request a list of prompts and prompt templates."""
577582

578583
method: Literal["prompts/list"]
@@ -703,7 +708,7 @@ class PromptListChangedNotification(
703708
params: NotificationParams | None = None
704709

705710

706-
class ListToolsRequest(Request[PaginatedRequestParams | None, Literal["tools/list"]]):
711+
class ListToolsRequest(PaginatedRequest[Literal["tools/list"]]):
707712
"""Sent from the client to request a list of tools the server has."""
708713

709714
method: Literal["tools/list"]

tests/client/test_list_methods_cursor.py

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,15 @@ async def test_tool_2() -> str:
3535
_ = await client_session.list_tools()
3636
list_tools_requests = spies.get_client_requests(method="tools/list")
3737
assert len(list_tools_requests) == 1
38-
assert (
39-
list_tools_requests[0].params is None
40-
or "cursor" not in list_tools_requests[0].params
41-
or list_tools_requests[0].params["cursor"] is None
42-
)
38+
assert list_tools_requests[0].params is None
4339

4440
spies.clear()
4541

4642
# Test with cursor=None
4743
_ = await client_session.list_tools(cursor=None)
4844
list_tools_requests = spies.get_client_requests(method="tools/list")
4945
assert len(list_tools_requests) == 1
50-
assert (
51-
list_tools_requests[0].params is None
52-
or "cursor" not in list_tools_requests[0].params
53-
or list_tools_requests[0].params["cursor"] is None
54-
)
46+
assert list_tools_requests[0].params is None
5547

5648
spies.clear()
5749

@@ -93,23 +85,15 @@ async def test_resource() -> str:
9385
_ = await client_session.list_resources()
9486
list_resources_requests = spies.get_client_requests(method="resources/list")
9587
assert len(list_resources_requests) == 1
96-
assert (
97-
list_resources_requests[0].params is None
98-
or "cursor" not in list_resources_requests[0].params
99-
or list_resources_requests[0].params["cursor"] is None
100-
)
88+
assert list_resources_requests[0].params is None
10189

10290
spies.clear()
10391

10492
# Test with cursor=None
10593
_ = await client_session.list_resources(cursor=None)
10694
list_resources_requests = spies.get_client_requests(method="resources/list")
10795
assert len(list_resources_requests) == 1
108-
assert (
109-
list_resources_requests[0].params is None
110-
or "cursor" not in list_resources_requests[0].params
111-
or list_resources_requests[0].params["cursor"] is None
112-
)
96+
assert list_resources_requests[0].params is None
11397

11498
spies.clear()
11599

@@ -150,23 +134,15 @@ async def test_prompt(name: str) -> str:
150134
_ = await client_session.list_prompts()
151135
list_prompts_requests = spies.get_client_requests(method="prompts/list")
152136
assert len(list_prompts_requests) == 1
153-
assert (
154-
list_prompts_requests[0].params is None
155-
or "cursor" not in list_prompts_requests[0].params
156-
or list_prompts_requests[0].params["cursor"] is None
157-
)
137+
assert list_prompts_requests[0].params is None
158138

159139
spies.clear()
160140

161141
# Test with cursor=None
162142
_ = await client_session.list_prompts(cursor=None)
163143
list_prompts_requests = spies.get_client_requests(method="prompts/list")
164144
assert len(list_prompts_requests) == 1
165-
assert (
166-
list_prompts_requests[0].params is None
167-
or "cursor" not in list_prompts_requests[0].params
168-
or list_prompts_requests[0].params["cursor"] is None
169-
)
145+
assert list_prompts_requests[0].params is None
170146

171147
spies.clear()
172148

@@ -210,11 +186,7 @@ async def test_template(name: str) -> str:
210186
method="resources/templates/list"
211187
)
212188
assert len(list_templates_requests) == 1
213-
assert (
214-
list_templates_requests[0].params is None
215-
or "cursor" not in list_templates_requests[0].params
216-
or list_templates_requests[0].params["cursor"] is None
217-
)
189+
assert list_templates_requests[0].params is None
218190

219191
spies.clear()
220192

@@ -224,11 +196,7 @@ async def test_template(name: str) -> str:
224196
method="resources/templates/list"
225197
)
226198
assert len(list_templates_requests) == 1
227-
assert (
228-
list_templates_requests[0].params is None
229-
or "cursor" not in list_templates_requests[0].params
230-
or list_templates_requests[0].params["cursor"] is None
231-
)
199+
assert list_templates_requests[0].params is None
232200

233201
spies.clear()
234202

0 commit comments

Comments
 (0)