AI Hints
Home›MCP Tutorial›Protocol Details
MCP · Lesson 81 of 120

Pagination

Long lists.

⏱ 2 min read▶ Video at 1:12:34🎓 Free
Watch this lesson in the MCP courseWatch on YouTube ↗Full MCP course

A big server might have hundreds of resources. Instead of one huge reply, lists come in pages. Each page may include a next cursor. Pass it back, and you get the next page.

Pagination
From the video: Pagination
  • Long lists
  • Pages
  • Next cursor

Reading every page

We start with no cursor. Each loop asks for a page, keeps its tools, and takes the next cursor. When there's no cursor, we're done. Run it. Our small server fits on one page, so the loop runs once.

pages.pypython
import asyncio
from mcp import Client
from northwind import mcp

async def main():
    async with Client(mcp) as client:
        cursor, names, pages = None, [], 0
        while True:
            page = await client.list_tools(cursor=cursor)
            names += [t.name for t in page.tools]
            cursor, pages = page.next_cursor, pages + 1
            if not cursor:
                break
        print(pages, "page(s):", names)

asyncio.run(main())
Output
1 page(s): ['order_status', 'delivery_cost']
Also uses northwind.py
from mcp.server.mcpserver import MCPServer
from mcp.server.mcpserver.exceptions import ToolError

mcp = MCPServer("northwind")
ORDERS = {1042: "shipped", 1043: "packing", 1044: "delivered"}

@mcp.tool()
def order_status(order_id: int) -> str:
    """Look up the status of a Northwind order by its number."""
    if order_id not in ORDERS:
        raise ToolError(f"Order {order_id} not found")
    return ORDERS[order_id]

@mcp.tool()
def delivery_cost(order_total: float) -> str:
    """UK delivery cost for an order total in pounds."""
    return "free" if order_total >= 50 else "£4.99"

@mcp.resource("policy://returns")
def returns_policy() -> str:
    """Northwind's returns policy."""
    return "You can return any unused item within 30 days of delivery."

@mcp.prompt()
def support_reply(question: str) -> str:
    """Draft a polite reply to a customer."""
    return f"Write a short, polite reply to this Northwind customer: {question}"

if __name__ == "__main__":
    mcp.run()

Summary

Lists can come in pages. Keep asking with the next cursor until there isn't one. Next, caching hints.

Key takeaways
  • cursor
  • next_cursor
  • Loop until done
Next up: Caching Hints — ttl for lists