Skip to content

Conversation

@mattpodwysocki
Copy link
Contributor

Overview

This PR contains a task-based implementation of the optimization tool using the MCP Tasks API (experimental, SDK 1.25.1+).

Status: DRAFT - Not ready to merge until MCP clients add task support.

Why Task-based?

The Optimization API can take 5-10 seconds to complete, which blocks the agent during execution. A task-based approach allows:

  • Non-blocking execution (agent can do other work while optimization runs)
  • Background polling without blocking the client
  • Better UX for long-running operations

Implementation

  • Uses registerToolTask from MCP SDK experimental tasks API
  • createTask: Submits job to Mapbox API, returns immediately with task ID
  • Background polling function polls for results asynchronously
  • getTask: Returns current task status
  • getTaskResult: Returns final optimization results when complete

Current Blocker: No Client Support

Tested with:

  • ❌ MCP Inspector: Task support not available
  • ❌ Claude Desktop/CLI: Error -32601 "Tool optimization_tool requires task augmentation (taskSupport: 'required')"
  • ❌ Goose: Falls back to other tools

When to Merge

This PR can be merged when:

  1. At least one major MCP client (Claude Desktop, Inspector, or Goose) adds task support
  2. The task implementation can be tested end-to-end
  3. Documentation is updated to reflect async usage patterns

Files Changed

  • src/tools/optimization-tool/OptimizationTask.ts - New task-based implementation
  • src/index.ts - Task store, capabilities, and registration
  • src/tools/BaseTool.ts - Type assertion workarounds for SDK issue #985
  • src/prompts/OptimizeDeliveriesPrompt.ts - Updated for task-based execution

Related

🤖 Generated with Claude Code

mattpodwysocki and others added 3 commits January 5, 2026 22:28
Implements two new Mapbox Navigation API tools:

- Map Matching Tool (map_matching_tool): Snaps GPS traces to roads
  - Supports 2-100 coordinates with optional timestamps and radiuses
  - Returns confidence scores, matched geometry, and annotations
  - Handles driving, cycling, walking, and driving-traffic profiles

- Optimization Tool (optimization_tool): Solves vehicle routing problems
  - Supports up to 1000 coordinates
  - Simplified mode: auto-generates vehicle and services
  - Advanced mode: custom vehicles, services, shipments, time windows
  - Async polling mechanism (POST + GET)

Both tools include:
- Complete input/output schemas with Zod validation
- Comprehensive unit tests (19 tests total)
- Proper annotations (readOnlyHint, openWorldHint, etc.)

All 422 tests passing.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
This commit converts the OptimizationTool from a synchronous blocking tool
to an async task-based tool using the experimental MCP Tasks API.

**Problem:** The Optimization API v2 is long-running (up to 10 seconds with
polling), blocking the agent from doing other work while waiting for results.

**Solution:** Implemented task-based execution using MCP SDK 1.25.1's
experimental tasks API:

- **src/tools/optimization-tool/OptimizationTask.ts** (NEW): Task-based
  implementation with three handlers:
  - createTask: Submits optimization job to Mapbox API, returns immediately
    without blocking
  - getTask: Returns current task status (pending, working, completed, failed)
  - getTaskResult: Returns final optimization results when complete
  - Background polling runs asynchronously with proper error handling

- **src/index.ts**: Added task support to server
  - Created InMemoryTaskStore and InMemoryTaskMessageQueue
  - Updated server capabilities to include tasks: { requests: { tools: { call: {} } } }
  - Registered OptimizationTask instead of synchronous OptimizationTool
  - Fixed "Type instantiation is excessively deep" error in GetPromptRequestSchema
    handler with type assertions (workaround for MCP SDK 1.25.1 issue #985)

- **src/tools/BaseTool.ts**: Fixed TypeScript compilation error
  - Added type assertions to break complex type inference chains
  - Workaround for "Type instantiation is excessively deep" errors with
    MCP SDK 1.25.1 generic types

**Benefits:**
- Non-blocking execution - returns task ID immediately
- Agent can do other work while optimization runs in background
- Progress updates via task status messages
- Proper error handling and timeout management
- All existing tests pass (422 tests)
- Zero TypeScript compilation errors

**Testing:**
- Server starts successfully with task support
- All 422 existing tests pass
- Build completes without errors

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
- Upgrade @modelcontextprotocol/sdk from 1.21.1 to 1.25.1
- Remove old SDK 1.21.1 patch
- Apply SDK 1.25.1 patch with icons support and output validation fixes
- Update version to 0.8.1 to match main branch
- All 422 tests passing

This brings the task-based optimization branch up to date with the latest
SDK and includes the necessary patches for proper operation.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
Copy link

@prisma-cloud-devsecops prisma-cloud-devsecops bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prisma Cloud has found errors in this PR ⬇️

"dependencies": {
"@mcp-ui/server": "^5.13.1",
"@modelcontextprotocol/sdk": "^1.21.1",
"@modelcontextprotocol/sdk": "^1.25.1",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@modelcontextprotocol/sdk 1.25.1 / package.json

Total vulnerabilities: 1

Critical: 0 High: 1 Medium: 0 Low: 0
Vulnerability IDSeverityCVSSFixed inStatus
CVE-2026-0621 HIGH HIGH - - Open

@mattpodwysocki
Copy link
Contributor Author

Closing this PR

This PR is superseded by #94 (offline_tools branch).

What's in #94 now:

  1. Optimization V1 Tool ():

    • Uses V1 synchronous GET API
    • 2-12 coordinates, simple Traveling Salesman Problem
    • Works immediately in all MCP clients ✅
  2. Optimization V2 Tool ():

    • Task-based async implementation (from this PR)
    • V2 API with advanced features (time windows, capacity, breaks, etc.)
    • Code present in OptimizationV2Tool.ts but not registered
    • Ready to enable with one line when V2 API becomes public

Why V1 instead of V2?

The V2 API endpoint (/optimized-trips/v2) requires early access/beta flag and is currently returning 403 errors. The V1 API (/optimized-trips/v1/{profile}/{coordinates}) is publicly available and works now.

When will V2 be enabled?

Once the V2 API becomes publicly available, we can enable it by uncommenting one line in the toolRegistry. The code is ready to go!

Closing this PR as the work is preserved and ready for future activation.

🤖 Generated with Claude Code

@mattpodwysocki
Copy link
Contributor Author

Superseded by #94

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