Skip to content

Commit a00b20a

Browse files
feat: add cursor pagination support to all client list methods (#718)
1 parent b8f7b02 commit a00b20a

File tree

2 files changed

+154
-4
lines changed

2 files changed

+154
-4
lines changed

src/mcp/client/session.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,23 +201,29 @@ async def set_logging_level(self, level: types.LoggingLevel) -> types.EmptyResul
201201
types.EmptyResult,
202202
)
203203

204-
async def list_resources(self) -> types.ListResourcesResult:
204+
async def list_resources(
205+
self, cursor: str | None = None
206+
) -> types.ListResourcesResult:
205207
"""Send a resources/list request."""
206208
return await self.send_request(
207209
types.ClientRequest(
208210
types.ListResourcesRequest(
209211
method="resources/list",
212+
cursor=cursor,
210213
)
211214
),
212215
types.ListResourcesResult,
213216
)
214217

215-
async def list_resource_templates(self) -> types.ListResourceTemplatesResult:
218+
async def list_resource_templates(
219+
self, cursor: str | None = None
220+
) -> types.ListResourceTemplatesResult:
216221
"""Send a resources/templates/list request."""
217222
return await self.send_request(
218223
types.ClientRequest(
219224
types.ListResourceTemplatesRequest(
220225
method="resources/templates/list",
226+
cursor=cursor,
221227
)
222228
),
223229
types.ListResourceTemplatesResult,
@@ -278,12 +284,13 @@ async def call_tool(
278284
request_read_timeout_seconds=read_timeout_seconds,
279285
)
280286

281-
async def list_prompts(self) -> types.ListPromptsResult:
287+
async def list_prompts(self, cursor: str | None = None) -> types.ListPromptsResult:
282288
"""Send a prompts/list request."""
283289
return await self.send_request(
284290
types.ClientRequest(
285291
types.ListPromptsRequest(
286292
method="prompts/list",
293+
cursor=cursor,
287294
)
288295
),
289296
types.ListPromptsResult,
@@ -322,12 +329,13 @@ async def complete(
322329
types.CompleteResult,
323330
)
324331

325-
async def list_tools(self) -> types.ListToolsResult:
332+
async def list_tools(self, cursor: str | None = None) -> types.ListToolsResult:
326333
"""Send a tools/list request."""
327334
return await self.send_request(
328335
types.ClientRequest(
329336
types.ListToolsRequest(
330337
method="tools/list",
338+
cursor=cursor,
331339
)
332340
),
333341
types.ListToolsResult,
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import pytest
2+
3+
from mcp.server.fastmcp import FastMCP
4+
from mcp.shared.memory import (
5+
create_connected_server_and_client_session as create_session,
6+
)
7+
8+
# Mark the whole module for async tests
9+
pytestmark = pytest.mark.anyio
10+
11+
12+
async def test_list_tools_cursor_parameter():
13+
"""Test that the cursor parameter is accepted for list_tools.
14+
15+
Note: FastMCP doesn't currently implement pagination, so this test
16+
only verifies that the cursor parameter is accepted by the client.
17+
"""
18+
server = FastMCP("test")
19+
20+
# Create a couple of test tools
21+
@server.tool(name="test_tool_1")
22+
async def test_tool_1() -> str:
23+
"""First test tool"""
24+
return "Result 1"
25+
26+
@server.tool(name="test_tool_2")
27+
async def test_tool_2() -> str:
28+
"""Second test tool"""
29+
return "Result 2"
30+
31+
async with create_session(server._mcp_server) as client_session:
32+
# Test without cursor parameter (omitted)
33+
result1 = await client_session.list_tools()
34+
assert len(result1.tools) == 2
35+
36+
# Test with cursor=None
37+
result2 = await client_session.list_tools(cursor=None)
38+
assert len(result2.tools) == 2
39+
40+
# Test with cursor as string
41+
result3 = await client_session.list_tools(cursor="some_cursor_value")
42+
assert len(result3.tools) == 2
43+
44+
# Test with empty string cursor
45+
result4 = await client_session.list_tools(cursor="")
46+
assert len(result4.tools) == 2
47+
48+
49+
async def test_list_resources_cursor_parameter():
50+
"""Test that the cursor parameter is accepted for list_resources.
51+
52+
Note: FastMCP doesn't currently implement pagination, so this test
53+
only verifies that the cursor parameter is accepted by the client.
54+
"""
55+
server = FastMCP("test")
56+
57+
# Create a test resource
58+
@server.resource("resource://test/data")
59+
async def test_resource() -> str:
60+
"""Test resource"""
61+
return "Test data"
62+
63+
async with create_session(server._mcp_server) as client_session:
64+
# Test without cursor parameter (omitted)
65+
result1 = await client_session.list_resources()
66+
assert len(result1.resources) >= 1
67+
68+
# Test with cursor=None
69+
result2 = await client_session.list_resources(cursor=None)
70+
assert len(result2.resources) >= 1
71+
72+
# Test with cursor as string
73+
result3 = await client_session.list_resources(cursor="some_cursor")
74+
assert len(result3.resources) >= 1
75+
76+
# Test with empty string cursor
77+
result4 = await client_session.list_resources(cursor="")
78+
assert len(result4.resources) >= 1
79+
80+
81+
async def test_list_prompts_cursor_parameter():
82+
"""Test that the cursor parameter is accepted for list_prompts.
83+
84+
Note: FastMCP doesn't currently implement pagination, so this test
85+
only verifies that the cursor parameter is accepted by the client.
86+
"""
87+
server = FastMCP("test")
88+
89+
# Create a test prompt
90+
@server.prompt()
91+
async def test_prompt(name: str) -> str:
92+
"""Test prompt"""
93+
return f"Hello, {name}!"
94+
95+
async with create_session(server._mcp_server) as client_session:
96+
# Test without cursor parameter (omitted)
97+
result1 = await client_session.list_prompts()
98+
assert len(result1.prompts) >= 1
99+
100+
# Test with cursor=None
101+
result2 = await client_session.list_prompts(cursor=None)
102+
assert len(result2.prompts) >= 1
103+
104+
# Test with cursor as string
105+
result3 = await client_session.list_prompts(cursor="some_cursor")
106+
assert len(result3.prompts) >= 1
107+
108+
# Test with empty string cursor
109+
result4 = await client_session.list_prompts(cursor="")
110+
assert len(result4.prompts) >= 1
111+
112+
113+
async def test_list_resource_templates_cursor_parameter():
114+
"""Test that the cursor parameter is accepted for list_resource_templates.
115+
116+
Note: FastMCP doesn't currently implement pagination, so this test
117+
only verifies that the cursor parameter is accepted by the client.
118+
"""
119+
server = FastMCP("test")
120+
121+
# Create a test resource template
122+
@server.resource("resource://test/{name}")
123+
async def test_template(name: str) -> str:
124+
"""Test resource template"""
125+
return f"Data for {name}"
126+
127+
async with create_session(server._mcp_server) as client_session:
128+
# Test without cursor parameter (omitted)
129+
result1 = await client_session.list_resource_templates()
130+
assert len(result1.resourceTemplates) >= 1
131+
132+
# Test with cursor=None
133+
result2 = await client_session.list_resource_templates(cursor=None)
134+
assert len(result2.resourceTemplates) >= 1
135+
136+
# Test with cursor as string
137+
result3 = await client_session.list_resource_templates(cursor="some_cursor")
138+
assert len(result3.resourceTemplates) >= 1
139+
140+
# Test with empty string cursor
141+
result4 = await client_session.list_resource_templates(cursor="")
142+
assert len(result4.resourceTemplates) >= 1

0 commit comments

Comments
 (0)