⚠️ Add Pagination Support to Prevent Token Overflow #12

Open
opened 2025-10-10 09:17:52 +02:00 by jack · 0 comments
Owner

🚨 Critical Issue

Problem

Currently, tools that return large datasets can cause token overflow in the MCP protocol, leading to:

  • Truncated responses
  • Lost data
  • Poor user experience
  • Unusable results for large datasets

Affected Tools in This MCP

  • request - ⚠️ CRITICAL: Response body could be massive
  • get_json - JSON responses can be very large
  • post_json - Response data could overflow
  • Note: Should support passing pagination params to APIs

Example: Token Overflow Problem

Current Behavior ( Bad)

// get_json - API could return huge dataset!
const data = await getJson('https://api.example.com/all-users');
// Response: 50,000 users = Token overflow!

With Pagination ( Good)

// Paginated API request
const data = await getJson('https://api.example.com/users', {
  queryParams: { limit: 100, offset: 0 }
});
// Response: { items: [...100 users], pagination: { total: 50000, hasMore: true } }

💡 Proposed Solution

Standard Pagination Interface

interface PaginationParams {
  limit?: number;      // Max items to return (default: 100, max: 1000)
  offset?: number;     // Number of items to skip (default: 0)
}

interface PaginatedResponse<T> {
  items: T[];
  pagination: {
    total: number;       // Total count of items
    limit: number;       // Applied limit
    offset: number;      // Applied offset
    hasMore: boolean;    // Whether more items exist
    nextOffset?: number; // Offset for next page (null if no more)
  }
}

Implementation Example for request, get_json, list_profiles

// Add pagination parameters to tool schema
{
  name: "your_tool",
  inputSchema: {
    type: "object",
    properties: {
      // ... existing parameters
      limit: {
        type: "number",
        description: "Maximum items to return (default: 100, max: 1000)",
        minimum: 1,
        maximum: 1000,
        default: 100
      },
      offset: {
        type: "number",
        description: "Number of items to skip (default: 0)",
        minimum: 0,
        default: 0
      }
    }
  }
}

// In handler
const limit = Math.min(args.limit ?? 100, 1000);
const offset = args.offset ?? 0;

const allItems = await getAllItems();
const total = allItems.length;
const items = allItems.slice(offset, offset + limit);

return {
  items,
  pagination: {
    total,
    limit,
    offset,
    hasMore: offset + limit < total,
    nextOffset: offset + limit < total ? offset + limit : null
  }
};

🎯 Benefits

  • Prevent token overflow - Never exceed token limits
  • Faster responses - Smaller payloads = faster responses
  • Better UX - Users can request more if needed
  • Scalability - Handles datasets of any size
  • Efficient - Only process/transfer what's needed
  • Standard pattern - Familiar to all developers

📋 Implementation Checklist

  • Add limit and offset parameters to affected tools
  • Set sensible defaults (limit: 100, max: 1000)
  • Return pagination metadata in responses
  • Update tool descriptions to mention pagination
  • Add examples to README
  • Update CHANGELOG

🚦 Priority Level

🔴 CRITICAL - This directly affects:

  • System stability (prevents crashes)
  • User experience (complete data access)
  • Data integrity (no truncation)
  • Resource usage (efficient transfers)

🤖 Generated with Claude Code

## 🚨 Critical Issue ### Problem Currently, tools that return large datasets can cause **token overflow** in the MCP protocol, leading to: - ❌ Truncated responses - ❌ Lost data - ❌ Poor user experience - ❌ Unusable results for large datasets ### Affected Tools in This MCP - **`request`** - ⚠️ CRITICAL: Response body could be massive - **`get_json`** - JSON responses can be very large - **`post_json`** - Response data could overflow - Note: Should support passing pagination params to APIs ### Example: Token Overflow Problem #### Current Behavior (❌ Bad) ```typescript // get_json - API could return huge dataset! const data = await getJson('https://api.example.com/all-users'); // Response: 50,000 users = Token overflow! ``` #### With Pagination (✅ Good) ```typescript // Paginated API request const data = await getJson('https://api.example.com/users', { queryParams: { limit: 100, offset: 0 } }); // Response: { items: [...100 users], pagination: { total: 50000, hasMore: true } } ``` ## 💡 Proposed Solution ### Standard Pagination Interface ```typescript interface PaginationParams { limit?: number; // Max items to return (default: 100, max: 1000) offset?: number; // Number of items to skip (default: 0) } interface PaginatedResponse<T> { items: T[]; pagination: { total: number; // Total count of items limit: number; // Applied limit offset: number; // Applied offset hasMore: boolean; // Whether more items exist nextOffset?: number; // Offset for next page (null if no more) } } ``` ### Implementation Example for `request`, `get_json`, `list_profiles` ```typescript // Add pagination parameters to tool schema { name: "your_tool", inputSchema: { type: "object", properties: { // ... existing parameters limit: { type: "number", description: "Maximum items to return (default: 100, max: 1000)", minimum: 1, maximum: 1000, default: 100 }, offset: { type: "number", description: "Number of items to skip (default: 0)", minimum: 0, default: 0 } } } } // In handler const limit = Math.min(args.limit ?? 100, 1000); const offset = args.offset ?? 0; const allItems = await getAllItems(); const total = allItems.length; const items = allItems.slice(offset, offset + limit); return { items, pagination: { total, limit, offset, hasMore: offset + limit < total, nextOffset: offset + limit < total ? offset + limit : null } }; ``` ## 🎯 Benefits - ✅ **Prevent token overflow** - Never exceed token limits - ✅ **Faster responses** - Smaller payloads = faster responses - ✅ **Better UX** - Users can request more if needed - ✅ **Scalability** - Handles datasets of any size - ✅ **Efficient** - Only process/transfer what's needed - ✅ **Standard pattern** - Familiar to all developers ## 📋 Implementation Checklist - [ ] Add `limit` and `offset` parameters to affected tools - [ ] Set sensible defaults (limit: 100, max: 1000) - [ ] Return pagination metadata in responses - [ ] Update tool descriptions to mention pagination - [ ] Add examples to README - [ ] Update CHANGELOG ## 🚦 Priority Level **🔴 CRITICAL** - This directly affects: - System stability (prevents crashes) - User experience (complete data access) - Data integrity (no truncation) - Resource usage (efficient transfers) --- 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
customable-mcp/http-mcp#12
No description provided.