|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +MCP Pagination Example |
| 4 | +
|
| 5 | +Demonstrates the pagination feature for tools/list, resources/list, and prompts/list |
| 6 | +per MCP 2025-11-25 specification. |
| 7 | +
|
| 8 | +Usage: |
| 9 | + python pagination_example.py |
| 10 | +""" |
| 11 | + |
| 12 | +from praisonai.mcp_server.registry import ( |
| 13 | + MCPToolRegistry, |
| 14 | + encode_cursor, |
| 15 | + decode_cursor, |
| 16 | + DEFAULT_PAGE_SIZE, |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +def demo_tool_pagination(): |
| 21 | + """Demonstrate tool pagination.""" |
| 22 | + print("\n" + "=" * 60) |
| 23 | + print("Tool Pagination Demo") |
| 24 | + print("=" * 60) |
| 25 | + |
| 26 | + # Create registry with many tools |
| 27 | + registry = MCPToolRegistry() |
| 28 | + for i in range(75): |
| 29 | + registry.register( |
| 30 | + name=f"example.tool_{i:03d}", |
| 31 | + handler=lambda: f"Tool {i} executed", |
| 32 | + description=f"Example tool number {i}", |
| 33 | + ) |
| 34 | + |
| 35 | + print(f"\nRegistered {len(registry.list_all())} tools") |
| 36 | + print(f"Default page size: {DEFAULT_PAGE_SIZE}") |
| 37 | + |
| 38 | + # First page |
| 39 | + print("\n--- First Page ---") |
| 40 | + tools, next_cursor = registry.list_paginated() |
| 41 | + print(f"Tools returned: {len(tools)}") |
| 42 | + print(f"First tool: {tools[0]['name']}") |
| 43 | + print(f"Last tool: {tools[-1]['name']}") |
| 44 | + print(f"Next cursor: {next_cursor}") |
| 45 | + |
| 46 | + # Second page |
| 47 | + print("\n--- Second Page (using cursor) ---") |
| 48 | + tools2, next_cursor2 = registry.list_paginated(cursor=next_cursor) |
| 49 | + print(f"Tools returned: {len(tools2)}") |
| 50 | + print(f"First tool: {tools2[0]['name']}") |
| 51 | + print(f"Last tool: {tools2[-1]['name']}") |
| 52 | + print(f"Next cursor: {next_cursor2}") |
| 53 | + |
| 54 | + # Custom page size |
| 55 | + print("\n--- Custom Page Size (10) ---") |
| 56 | + tools3, next_cursor3 = registry.list_paginated(page_size=10) |
| 57 | + print(f"Tools returned: {len(tools3)}") |
| 58 | + print(f"Next cursor: {next_cursor3}") |
| 59 | + |
| 60 | + |
| 61 | +def demo_cursor_encoding(): |
| 62 | + """Demonstrate cursor encoding/decoding.""" |
| 63 | + print("\n" + "=" * 60) |
| 64 | + print("Cursor Encoding Demo") |
| 65 | + print("=" * 60) |
| 66 | + |
| 67 | + # Simple offset |
| 68 | + cursor1 = encode_cursor(50) |
| 69 | + offset1, _ = decode_cursor(cursor1) |
| 70 | + print(f"\nOffset 50 -> Cursor: {cursor1}") |
| 71 | + print(f"Decoded back: offset={offset1}") |
| 72 | + |
| 73 | + # With snapshot hash |
| 74 | + cursor2 = encode_cursor(100, "abc123hash") |
| 75 | + offset2, snapshot = decode_cursor(cursor2) |
| 76 | + print(f"\nOffset 100 with snapshot -> Cursor: {cursor2}") |
| 77 | + print(f"Decoded back: offset={offset2}, snapshot={snapshot}") |
| 78 | + |
| 79 | + |
| 80 | +def demo_tool_search(): |
| 81 | + """Demonstrate tool search with pagination.""" |
| 82 | + print("\n" + "=" * 60) |
| 83 | + print("Tool Search Demo") |
| 84 | + print("=" * 60) |
| 85 | + |
| 86 | + registry = MCPToolRegistry() |
| 87 | + |
| 88 | + # Register tools with different categories |
| 89 | + from praisonai.mcp_server.registry import MCPToolDefinition |
| 90 | + |
| 91 | + tools_data = [ |
| 92 | + ("memory.show", "Show memory contents", "memory", True, False), |
| 93 | + ("memory.clear", "Clear memory", "memory", False, True), |
| 94 | + ("file.read", "Read a file", "file", True, False), |
| 95 | + ("file.write", "Write to a file", "file", False, True), |
| 96 | + ("file.delete", "Delete a file", "file", False, True), |
| 97 | + ("web.search", "Search the web", "web", True, False), |
| 98 | + ("web.fetch", "Fetch a URL", "web", True, False), |
| 99 | + ] |
| 100 | + |
| 101 | + for name, desc, category, read_only, destructive in tools_data: |
| 102 | + registry._tools[name] = MCPToolDefinition( |
| 103 | + name=name, |
| 104 | + description=desc, |
| 105 | + handler=lambda: None, |
| 106 | + input_schema={"type": "object"}, |
| 107 | + category=category, |
| 108 | + read_only_hint=read_only, |
| 109 | + destructive_hint=destructive, |
| 110 | + ) |
| 111 | + |
| 112 | + # Search by query |
| 113 | + print("\n--- Search: 'memory' ---") |
| 114 | + results, _, total = registry.search(query="memory") |
| 115 | + print(f"Found {total} tools:") |
| 116 | + for tool in results: |
| 117 | + print(f" - {tool['name']}") |
| 118 | + |
| 119 | + # Search by category |
| 120 | + print("\n--- Search: category='file' ---") |
| 121 | + results, _, total = registry.search(category="file") |
| 122 | + print(f"Found {total} tools:") |
| 123 | + for tool in results: |
| 124 | + print(f" - {tool['name']}") |
| 125 | + |
| 126 | + # Search read-only tools |
| 127 | + print("\n--- Search: read_only=True ---") |
| 128 | + results, _, total = registry.search(read_only=True) |
| 129 | + print(f"Found {total} read-only tools:") |
| 130 | + for tool in results: |
| 131 | + print(f" - {tool['name']}") |
| 132 | + |
| 133 | + |
| 134 | +def main(): |
| 135 | + """Run all demos.""" |
| 136 | + print("\n" + "#" * 60) |
| 137 | + print("# MCP Pagination & Search Examples") |
| 138 | + print("# MCP Protocol Version: 2025-11-25") |
| 139 | + print("#" * 60) |
| 140 | + |
| 141 | + demo_cursor_encoding() |
| 142 | + demo_tool_pagination() |
| 143 | + demo_tool_search() |
| 144 | + |
| 145 | + print("\n" + "=" * 60) |
| 146 | + print("All demos completed!") |
| 147 | + print("=" * 60 + "\n") |
| 148 | + |
| 149 | + |
| 150 | +if __name__ == "__main__": |
| 151 | + main() |
0 commit comments