-
Notifications
You must be signed in to change notification settings - Fork 2.5k
feat: add paginated list decorators for prompts, resources, and tools #1286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we have a some docs with examples? not sure if we have good docs for the lowlevel api but some usage examples would be great (if not just to help with reviewing).
Otherwise this looks good to me. happy with the separate functions to avoid breaking changes. keen to know how you think we should approach this in the high level interface.
91fa584
to
9d72703
Compare
Updated the docs, added example code, and included testing in the PR desription |
66ee42f
to
83d4890
Compare
70435e5
to
5d823c8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Backward compatibility: Modifying the existing decorators to require a cursor parameter in the callback would break existing uses of these decorators.
You can make it optional, can't you?
47c3f19
to
2b2e80c
Compare
0532ec3
to
7a394ee
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exciting changes! Just some concerns about the accepts_request logic
from typing import Any | ||
|
||
|
||
def accepts_request(func: Callable[..., Any]) -> bool: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think maybe the function we want is def accepts_single_positional_arg(func, arg_type)
? (or can_be_called_with_single_positional
to allow defaults)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've renamed the function, but didn't include an arg_type
because it's likely users will pass in an untyped handler which would then cause validation errors if we're checking for type hints.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a handler w/ an untyped single pos param (or untyped *args
) is perfectly fine, but if that single pos param does have a type it's a low hanging fruit to check it.
f0fedc4
to
a7476d8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
w.r.t. the disagreement on runtime type checking the positional arg i'm personally okay with either approach, I think being maximally permissive (as long as the thing is technically correct) is a good devx. happy to be wrong, but keen to see this merged
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks Max!
5. If the first parameter is typed with something incompatible -> (False, True) - old style, deprecate | ||
6. If the first parameter is untyped but accepts positional args -> (True, True) - pass request, deprecate | ||
""" | ||
can_accept_arg = accepts_single_positional_arg(func) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you inline & simplify this function's / file logic? It is now calling inspect.signature up to twice (from 3 places). Something like this maybe?
def function_accepts_single_param(func: Callable[..., Any], param_type: type) -> bool:
sig = inspect.signature(func)
if not sig.parameters:
# TODO: warn about old style without params
return False
elif len(sig.parameters) != 1:
raise ValueError(f"Function must have exactly one parameter of type {type} or none (legacy).")
param = next(iter(sig.parameters.values()))
if param.kind not in (inspect.Parameter.VAR_POSITIONAL, inspect.Parameter.VAR_KEYWORD):
# TODO: warn about exotic param type
return False
if param.annotation == inspect.Parameter.empty or param.annotation == Any:
# TODO: warn about untyped, depending on decision
return False # or True if allow untyped
return param.annotation == param_type
(not sure about swallowing the signature exceptions, removed it / better to fail loudly unless you know of legit cases that would throw?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this code has been removed now
if param_type is Any: | ||
return True | ||
|
||
# Exact match |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we really only need to handle this case / can be prescriptive re/ signature of functions we allow (we don't need to allow all of valid python).
pass | ||
|
||
|
||
@pytest.mark.parametrize( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you also test def bad_pos_arg_type(request: str)
and def good_untyped_pos_arg(request)
?
return False | ||
|
||
# Check for Any type | ||
if param_type is Any: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Kludex what's your take on allowing untyped def foo(request)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think any Python package should implement logic around positional arguments - modern packages rely on proper typing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm fair enough.
Alright so just so we can all be on the same page:
def foo()
,def foo(thing: int = 1)
- no exception, pass no request object, warn as deprecated
def foo(req: ListPromptsRequest)
,def foo(req: ListPromptsRequest, thing: int = 1)
- no exception, will pass through the request
def foo(thing: int = 1, req: ListPromptsRequest | None = None)
- no exception, pass no request object, warn as deprecated?
def foo(req)
,def foo(req: Any)
,def foo(req: Generic[T])
,def foo(req: str)
- no exception on registration, pass no request object, warn as deprected (even though it will fail at runtime)
def foo(thing: int, req: ListPromptsRequest)
- do not raise exception, attempt to pass request at runtime (even though it will fail at runtime)
Note: No raising custom exception at registration time
Not sure if I'm missing anything else
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Had a call with @Kludex, we've agreed on the above (now updated) 5 examples for me to now implement and then get a re-review.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've update the code to match the following except for adding the deprecation warnings.
I initially implemented it with deprecation (as you can see from the commented out code), but this infected the rest of the code base pretty thoroughly and caused a lot of issues with failed unit tests (from deprecation warnings), and the requirement of adding a lot of boilerplate code that generally isn't needed.
e.g. if you had:
@server.list_tools()
def handle_list_tools() -> list[Tool]:
return [ <my one tool> ]
Then to avoid the deprecation warning you'd need to change it to:
@server.list_tools()
def handle_list_tools(_: ListToolsRequest) -> ListToolsResult:
return ListToolsResult(tools=[ <my one tool> ])
I feel this is too much of a "breaking" change and would prefer to move the implementation/discussion around it into a separate PR rather than this one so we can get pagination in now.
Add list_prompts_paginated, list_resources_paginated, and list_tools_paginated decorators to support cursor-based pagination for listing endpoints. These decorators: - Accept a cursor parameter (can be None for first page) - Return the respective ListResult type directly - Maintain backward compatibility with existing non-paginated decorators - Update tool cache for list_tools_paginated Also includes simplified unit tests that verify cursor passthrough. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
Apply automatic formatting from ruff to ensure code meets project standards: - Remove trailing whitespace - Adjust line breaks for consistency - Format function arguments according to line length limits
- Create mcp_simple_pagination example server demonstrating all three paginated endpoints - Add pagination snippets for both server and client implementations - Update README to use snippet-source pattern for pagination examples - Move mutually exclusive note to blockquote format for better visibility - Complete example shows tools, resources, and prompts pagination with different page sizes
2baeaff
to
d154e5c
Compare
d154e5c
to
cbb0e37
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks Max!
# Can't inspect signature or resolve type hints, assume no request parameter (deprecated) | ||
return lambda _: func(), True | ||
|
||
# Check for positional-only parameter typed as request_type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
won't this try and call def foo(x: str, req: param_name)
as foo(req)
? Also if you wanna handle defaults def foo(x=10, *, req: param_name)
should work, and so should def foo(req: param_name, x=10)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the key is to always inject by name (i.e. you won't have req in a positional-only situation, as def foo(x=1, req: param_name)
is invalid w/o default for req).
Claude suggested something like this instead:
import inspect
import warnings
from collections.abc import Callable
from typing import Any, get_type_hints
def issue_deprecation_warning(func: Callable[..., Any], request_type: type) -> None:
"""
Issue a deprecation warning for handlers that don't use the new request parameter style.
"""
func_name = getattr(func, "__name__", str(func))
warnings.warn(
f"Handler '{func_name}' should accept a '{request_type.__name__}' parameter. "
"Support for handlers without this parameter will be removed in a future version.",
DeprecationWarning,
stacklevel=4,
)
def create_call_wrapper(func: Callable[..., Any], request_type: type) -> tuple[Callable[[Any], Any], bool]:
"""
Create a wrapper function that knows how to call func with the request object.
Returns a tuple of (wrapper_func, should_deprecate):
- wrapper_func: A function that takes the request and calls func appropriately
- should_deprecate: True if a deprecation warning should be issued
Supports patterns like:
- def handler(req: RequestType) # inject req
- def handler(*, req: RequestType) # inject req
- def handler(req: RequestType, x=10) # inject req, x has default
- def handler(x=10, *, req: RequestType) # inject req, x has default
- def handler() # deprecated, no injection
"""
try:
sig = inspect.signature(func)
type_hints = get_type_hints(func)
except (ValueError, TypeError, NameError):
# Can't inspect signature or resolve type hints, assume no request parameter (deprecated)
return lambda _: func(), True
# Find the request parameter
request_param_name = None
for param_name, param in sig.parameters.items():
param_type = type_hints.get(param_name)
if param_type == request_type:
# Found a parameter with the request type
if param.default is inspect.Parameter.empty:
# It's required - this is our injection target
request_param_name = param_name
break
# Has a default - treat as deprecated old style
return lambda _: func(), True
if request_param_name is None:
# No request parameter found - deprecated
return lambda _: func(), True
# Validate all other parameters have defaults
for param_name, param in sig.parameters.items():
if param_name != request_param_name:
if param.default is inspect.Parameter.empty and param.kind not in (
inspect.Parameter.VAR_POSITIONAL,
inspect.Parameter.VAR_KEYWORD,
):
raise TypeError(
f"Handler '{getattr(func, '__name__', str(func))}' has required parameter '{param_name}' "
f"but only '{request_param_name}' should be required"
)
# Create wrapper that injects only the request parameter
def wrapper(req: Any) -> Any:
return func(**{request_param_name: req})
return wrapper, False
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will intentionally fail later if you pass in func(thing: int, req: ListPromptsRequest)
. After some discussion with @Kludex this was the most "pythonic" common pattern.
Essentially, we're not going to raise any exceptions on registration, only at run-time. If you pass in a function with the wrong number of arguments, then that's on you. The type hinting is very explicit to saying your handler must have the correct type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See this comment for the list of functionality/rules #1286 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(i.e. you won't have req in a positional-only situation, as def foo(x=1, req: param_name) is invalid w/o default for req).
You can have foo(req: ListPromptsRequest, /)
which is positional only, no keywords. So foo(req=request)
will fail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, thanks for your patience, Max! Could you update the PR to mention the return type semantic change for annotated handlers? (we might wanna generalize this to all the other handlers as a follow up for consistency)
Updated! |
Dismissing to unblock merging - all comments addressed as far as I can tell.
* Add regression test for stateless request memory cleanup (modelcontextprotocol#1140) * Implement RFC9728 - Support WWW-Authenticate header by MCP client (modelcontextprotocol#1071) * Add streamable HTTP starlette example to Python SDK docs (modelcontextprotocol#1111) * fix markdown error in README in main (modelcontextprotocol#1147) * README - replace code snippets with examples - add lowlevel to snippets (modelcontextprotocol#1150) * README - replace code snippets with examples - streamable http (modelcontextprotocol#1155) * chore: don't allow users to create issues outside the templates (modelcontextprotocol#1163) * Tests(cli): Add coverage for helper functions (modelcontextprotocol#635) * Docs: Update CallToolResult parsing in README (modelcontextprotocol#812) Co-authored-by: Felix Weinberger <fweinberger@anthropic.com> * docs: add pre-commit install guide on CONTRIBUTING.md (modelcontextprotocol#995) Co-authored-by: Felix Weinberger <fweinberger@anthropic.com> * fix flaky fix-test_streamablehttp_client_resumption test (modelcontextprotocol#1166) * README - replace code snippets with examples -- auth examples (modelcontextprotocol#1164) * Support falling back to OIDC metadata for auth (modelcontextprotocol#1061) * Add CODEOWNERS file for sdk (modelcontextprotocol#1169) * fix flaky test test_88_random_error (modelcontextprotocol#1171) * Make sure `RequestId` is not coerced as `int` (modelcontextprotocol#1178) * Fix: Replace threading.Lock with anyio.Lock for Ray deployment compatibility (modelcontextprotocol#1151) * fix: fix OAuth flow request object handling (modelcontextprotocol#1174) * update codeowners group (modelcontextprotocol#1191) * fix: perform auth server metadata discovery fallbacks on any 4xx (modelcontextprotocol#1193) * server: skip duplicate response on CancelledError (modelcontextprotocol#1153) Co-authored-by: ihrpr <inna@anthropic.com> * Unpack settings in FastMCP (modelcontextprotocol#1198) * chore: Remove unused prompt_manager.py file (modelcontextprotocol#1229) Co-authored-by: Tapan Chugh <tapanc@cs.washington.edu> * Improved supported for ProtectedResourceMetadata (modelcontextprotocol#1235) Co-authored-by: Paul Carleton <paulcarletonjr@gmail.com> * chore: Remove unused variable notification_options (modelcontextprotocol#1238) * Improve README around the Context object (modelcontextprotocol#1203) * fix: allow to pass `list[str]` to `token_endpoint_auth_signing_alg_values_supported` (modelcontextprotocol#1226) * Remove strict validation on `response_modes_supported` member of `OAuthMetadata` (modelcontextprotocol#1243) * Add pyright strict mode on the whole project (modelcontextprotocol#1254) * Consistent casing for default headers Accept and Content-Type (modelcontextprotocol#1263) * Update dependencies and fix type issues (modelcontextprotocol#1268) Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com> * fix: prevent async generator cleanup errors in StreamableHTTP transport (modelcontextprotocol#1271) Co-authored-by: David Soria Parra <167242713+dsp-ant@users.noreply.github.com> * chore: uncomment .idea/ in .gitignore (modelcontextprotocol#1287) Co-authored-by: Claude <noreply@anthropic.com> * docs: clarify streamable_http_path configuration when mounting servers (modelcontextprotocol#1172) * feat: Add CORS configuration for browser-based MCP clients (modelcontextprotocol#1059) Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com> Co-authored-by: Felix Weinberger <fweinberger@anthropic.com> * Added Audio to FastMCP (modelcontextprotocol#1130) * fix: avoid uncessary retries in OAuth authenticated requests (modelcontextprotocol#1206) Co-authored-by: Felix Weinberger <fweinberger@anthropic.com> * Add PATHEXT to default STDIO env vars in windows (modelcontextprotocol#1256) * fix: error too many values to unpack (expected 2) (modelcontextprotocol#1279) Signed-off-by: San Nguyen <vinhsannguyen91@gmail.com> Co-authored-by: Felix Weinberger <fweinberger@anthropic.com> Co-authored-by: Felix Weinberger <3823880+felixweinberger@users.noreply.github.com> * SDK Parity: Avoid Parsing Server Response for non-JsonRPCMessage Requests (modelcontextprotocol#1290) * types: Setting default value for method: Literal (modelcontextprotocol#1292) * changes structured temperature to not deadly (modelcontextprotocol#1328) * Update simple-resource example to use non-deprecated read_resource return type (modelcontextprotocol#1331) Co-authored-by: Claude <noreply@anthropic.com> * docs: Update README to include link to API docs for modelcontextprotocol#1329 (modelcontextprotocol#1330) * Allow ping requests before initialization (modelcontextprotocol#1312) * Python lint: Ruff rules for pylint and code complexity (modelcontextprotocol#525) * Fix context injection for resources and prompts (modelcontextprotocol#1336) * fix(fastmcp): propagate mimeType in resource template list (modelcontextprotocol#1186) Co-authored-by: Felix Weinberger <3823880+felixweinberger@users.noreply.github.com> * fix: allow elicitations accepted without content (modelcontextprotocol#1285) Co-authored-by: Olivier Schiavo <olivier.schiavo@wengo.com> * Use --frozen in pre-commit config (modelcontextprotocol#1375) * Return HTTP 403 for invalid Origin headers (modelcontextprotocol#1353) * Add test for ProtectedResourceMetadataParsing (modelcontextprotocol#1236) Co-authored-by: Paul Carleton <paulcarletonjr@gmail.com> Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com> Co-authored-by: Felix Weinberger <3823880+felixweinberger@users.noreply.github.com> * Fastmcp logging progress example (modelcontextprotocol#1270) Co-authored-by: Felix Weinberger <3823880+felixweinberger@users.noreply.github.com> * feat: add paginated list decorators for prompts, resources, and tools (modelcontextprotocol#1286) Co-authored-by: Claude <noreply@anthropic.com> * Remove "unconditionally" from conditional description (modelcontextprotocol#1289) * Use streamable-http consistently in examples (modelcontextprotocol#1389) * feat: Add SDK support for SEP-1034 default values in elicitation schemas (modelcontextprotocol#1337) Co-authored-by: Tapan Chugh <tapanc@cs.washington.edu> Co-authored-by: Felix Weinberger <3823880+felixweinberger@users.noreply.github.com> * Implementation of SEP 973 - Additional metadata + icons support (modelcontextprotocol#1357) * Merge upstream/main with custom filtering --------- Signed-off-by: San Nguyen <vinhsannguyen91@gmail.com> Co-authored-by: Felix Weinberger <3823880+felixweinberger@users.noreply.github.com> Co-authored-by: yurikunash <143175350+yurikunash@users.noreply.github.com> Co-authored-by: Pamela Fox <pamela.fox@gmail.com> Co-authored-by: Inna Harper <inna.hrpr@gmail.com> Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com> Co-authored-by: Ian Davenport <49379192+davenpi@users.noreply.github.com> Co-authored-by: Dagang Wei <functicons@gmail.com> Co-authored-by: Felix Weinberger <fweinberger@anthropic.com> Co-authored-by: Stanley Law <stanleylkal@gmail.com> Co-authored-by: Luca Chang <131398524+LucaButBoring@users.noreply.github.com> Co-authored-by: leweng <leweng@nvidia.com> Co-authored-by: Clare Liguori <liguori@amazon.com> Co-authored-by: lukacf <luka@peltarion.com> Co-authored-by: ihrpr <inna@anthropic.com> Co-authored-by: Tapan Chugh <chugh.tapan@gmail.com> Co-authored-by: Tapan Chugh <tapanc@cs.washington.edu> Co-authored-by: Yann Jouanin <4557670+yannj-fr@users.noreply.github.com> Co-authored-by: Paul Carleton <paulcarletonjr@gmail.com> Co-authored-by: Sreenath Somarajapuram <somarajapuram@gmail.com> Co-authored-by: Omer Korner <omerkorner@gmail.com> Co-authored-by: joesavage-silabs <159480754+joesavage-silabs@users.noreply.github.com> Co-authored-by: Gregory L <gregory.linford@mistral.ai> Co-authored-by: David Soria Parra <167242713+dsp-ant@users.noreply.github.com> Co-authored-by: Moustapha Ebnou <155577789+mous222@users.noreply.github.com> Co-authored-by: Max Isbey <224885523+maxisbey@users.noreply.github.com> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Jerome <jerome@anthropic.com> Co-authored-by: xavier <84836280+dragonier23@users.noreply.github.com> Co-authored-by: keurcien <keurcien.luu@gmail.com> Co-authored-by: Tim Esler <tim.esler@gmail.com> Co-authored-by: San Nguyen <22189661+sandangel@users.noreply.github.com> Co-authored-by: Justin Wang <89049861+justin-yi-wang@users.noreply.github.com> Co-authored-by: jess <jessachandler@gmail.com> Co-authored-by: Peter Alexander <pja@anthropic.com> Co-authored-by: Reid Geyer <12072650+reidg44@users.noreply.github.com> Co-authored-by: Eleftheria Stein-Kousathana <eleftheria.kousathana@gmail.com> Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: pchoudhury22 <pchoudhury22@apple.com> Co-authored-by: owengo <owengo@users.noreply.github.com> Co-authored-by: Olivier Schiavo <olivier.schiavo@wengo.com> Co-authored-by: Steve Billings <billings.steve@gmail.com> Co-authored-by: Mike Salvatore <mike.s.salvatore@gmail.com>
Summary
This PR adds pagination support for listing prompts, resources, and tools using cursor-based pagination as defined in the MCP spec.
The lowlevel Server decorators
list_resources
,list_prompts
, andlist_tools
now handle two types of handlers:Callable[[], Awaitable[list[<resource, prompt, or tool>]]]
Callable[[List<___>Request], Awaitable[List<___>Result]]
List<___>Request
object depending on the decorator which the handle can now optionally handle.List<___>Response
which the handler can specify the cursor for the next page.How It Works
The decorators now automatically detect whether a callback accepts a cursor parameter:
Changes
list_prompts()
,list_resources()
, andlist_tools()
decorators to support both signaturesTest plan
🤖 Generated with Claude Code
👨 Below generated by the human, Max Isbey
Manual testing
I spun up the MCP inspector and ran the example pagination server in both stdio mode and SSE mode. Below is some testing evidence using the SSE mode:
Clicking "List More Resources" correctly lists all 30 resources:
Note: this works for prompts and tools as well
Requesting a resource works:
Running the pagination client example code works listing all 30 resources: