Skip to content

Add large response handling with temporary resources#114

Open
mattpodwysocki wants to merge 15 commits intomainfrom
feat/large-response-resources
Open

Add large response handling with temporary resources#114
mattpodwysocki wants to merge 15 commits intomainfrom
feat/large-response-resources

Conversation

@mattpodwysocki
Copy link
Contributor

@mattpodwysocki mattpodwysocki commented Feb 16, 2026

Overview

Implements conditional resource creation for DirectionsTool responses that exceed 50KB to prevent context window overflow.

Fixes the issue where long-distance routes (e.g., Washington DC to New York) return massive JSON responses that exceed the LLM's context window.

Solution

Conditional Resource Creation:

  • Detects when response size > 50KB
  • Creates temporary in-memory resource with URI: mapbox://temp/directions-{id}
  • Returns summary (distance, duration) + lightweight structured content
  • Small responses (<50KB) work unchanged - zero overhead

Resource Management:

  • In-memory storage with 30-minute TTL
  • Automatic cleanup every 5 minutes
  • Full data available via MCP resource API
  • Session-appropriate lifetime

LLM Guidance:

  • Updated tool description to guide LLMs: use geometries="none" for route planning, geometries="geojson" only for visualization
  • Updated search-along-route prompt to request compact responses by default
  • Prevents unnecessary large geometry requests

Schema Compliance:

  • Returns lightweight structured content for large responses to satisfy output schema validation
  • Includes route summary data (distance, duration, leg summaries) without geometry
  • Eliminates validation warnings that caused "Tool execution failed" errors in Claude Desktop

Example Output

Small Response (<50KB):

{
  "routes": [...full route with geometry...],
  "waypoints": [...]
}

Large Response (>50KB):

Route found: 225.6 miles, 252 minutes

Waypoints: 2

⚠️ Full response (342KB) exceeds context limit.

Full geometry and details stored as temporary resource.
Resource URI: mapbox://temp/directions-abc123
TTL: 30 minutes

Use the MCP resource API to retrieve full details if needed.
Or ask to read the resource by its URI.

Changes

  • New: TemporaryResourceManager - In-memory resource storage with TTL
  • New: TemporaryDataResource - MCP resource handler for temporary data
  • Updated: DirectionsTool - Conditional resource creation for large responses
  • Updated: DirectionsTool - Description guides LLMs to use geometries="none" for planning
  • Updated: DirectionsTool - Returns schema-compliant structured content for large responses
  • Updated: SearchAlongRoutePrompt - Uses geometries="none" and linear interpolation for sampling
  • Updated: Resource registry - Registered temporary resource
  • Updated: CHANGELOG.md - Documented all changes

Testing

✅ Build passes with no TypeScript errors
✅ Small responses work unchanged
✅ Large responses create resources and return summaries with structured content
✅ Resources retrievable via MCP API
✅ Cleanup works correctly
✅ No validation warnings or errors in Claude Desktop

Impact

  • Performance: Prevents context overflow for long-distance routes
  • UX: No more "Tool execution failed" errors in Claude Desktop
  • Efficiency: LLMs now request compact responses by default, only requesting geometry when needed
  • Compatibility: Fully backward compatible - small responses unchanged

Future Work

This pattern can be extended to other tools that may return large datasets:

  • Search results with many POIs
  • Matrix API results
  • Other tools as needed

Co-Authored-By: Claude Sonnet 4.5 noreply@anthropic.com

mattpodwysocki and others added 9 commits January 12, 2026 16:18
Implements MCP server icons at the correct architectural level (server
initialization) instead of at the tool level. Adds both light and dark
theme variants of the Mapbox logo using base64-encoded SVG data URIs.

- Add mapbox-logo-black.svg for light theme backgrounds
- Add mapbox-logo-white.svg for dark theme backgrounds
- Update server initialization to include icons array with theme property
- Use 800x180 SVG logos embedded as base64 data URIs

This replaces the previous incorrect approach of adding icons to
individual tools, which was not aligned with the MCP specification.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Updates the MCP SDK from 1.25.1 to 1.25.2 and recreates the output
validation patch for the new version. The patch continues to convert
strict output schema validation errors to warnings, allowing tools
to gracefully handle schema mismatches.

Changes:
- Update @modelcontextprotocol/sdk from ^1.25.1 to ^1.25.2
- Recreate SDK patch for version 1.25.2
- Remove obsolete 1.25.1 patch file
- All 397 tests pass with new SDK version

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Implements conditional resource creation for DirectionsTool responses
that exceed 50KB to prevent context window overflow.

Features:
- New TemporaryResourceManager for in-memory resource storage
- Temporary resources with 30-minute TTL and automatic cleanup
- DirectionsTool detects large responses and creates resources
- Returns summary (distance, duration, resource URI) for large responses
- Full geometry available via MCP resource API: mapbox://temp/directions-{id}
- Small responses (<50KB) work unchanged

This addresses performance issues on long-distance routes (e.g., DC to NYC)
where detailed geometry can exceed context limits.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@mattpodwysocki mattpodwysocki requested a review from a team as a code owner February 16, 2026 23:26
mattpodwysocki and others added 4 commits February 16, 2026 18:41
Added guidance in tool description to help LLMs choose appropriate
geometry format:
- Use geometries='none' for route planning and distance calculations
- Only use geometries='geojson' for map visualization or detailed navigation

This prevents unnecessary large responses and avoids the appearance of
'retry' behavior when Claude requests full geometry then realizes it's
too large.

Improves UX by guiding the LLM to make the right choice on first call.
When DirectionsTool creates a temporary resource for large responses,
it now returns lightweight structured content containing:
- Route summary data (distance, duration, leg summaries)
- Waypoints
- Custom _resourceUri and _truncated fields

This satisfies the output schema validation without bloating the
context window with large geometry data.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Use cleaned route fields (leg_summaries, intersecting_admins, etc.)
instead of full legs array with required nested fields. Explicitly
remove geometry and legs from structured content to keep it minimal.

This prevents schema validation errors that were causing Claude
Desktop to show "Tool execution failed" even though the tool succeeded.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
The prompt was instructing Claude to "get the route geometry" which
caused it to request geometries="geojson" even for long routes. Now:
- Explicitly requests geometries="none" for route metadata
- Uses linear interpolation for sampling instead of extracting from geometry
- Simplified sampling strategy to 5-7 points for all route lengths

This aligns with the DirectionsTool optimization for large responses.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@mattpodwysocki
Copy link
Contributor Author

mattpodwysocki commented Feb 17, 2026

✅ Verification - Feature Working Perfectly

Tested with query: "find gas stations between washington dc and new york"

Observed Behavior

Step 1 - Route Planning:

Workflow Execution

  • DirectionsTool called with optimized parameters
  • Route: ~362km (225 miles), ~4.3 hours
  • Recognized as VERY LONG route → 6 sample points strategy
  • Uses linear interpolation (no geometry extraction needed!)

Step 2 - Search Results:

Results

  • Clean, well-formatted output
  • Gas stations organized by location along route:
    • Near Washington, DC (Mile 0-10)
    • Maryland — Baltimore Area (~Mile 40-50)
    • [continues through route]
  • Route summary: ~225 miles via I-95 / NJ Turnpike, ~4.3 hours

Key Improvements Verified

✅ No "Tool execution failed" errors
✅ Single DirectionsTool call (not multiple retries)
✅ Compact response (no context overflow)
✅ Linear interpolation working correctly
✅ Proper sampling strategy based on route length
✅ Fast execution with clean results


Result: Feature working as designed! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant