Skip to content

Improve MCP server latency with session reuse, caching, and persistent player - #116

Closed
jdlugo wants to merge 3 commits into
Aran404:mainfrom
jdlugo:main
Closed

jdlugo wants to merge 3 commits into
Aran404:mainfrom
jdlugo:main

Conversation

@jdlugo

@jdlugo jdlugo commented Nov 29, 2026

Copy link
Copy Markdown

Summary

Profiles and optimizes the SpotAPI FastMCP server to resolve latency bottlenecks and LLM token bloat:

  1. Session Pooling & Keep-Alive (_SharedCatalogue):
    Reuses TCP/TLS connections across tool calls rather than creating a new Catalogue() on every tool call. Search network latency reduced by ~57% (1,461 ms → 621 ms).
  2. In-Memory LRU Cache (ResponseCache):
    Adds an LRU cache (256 items, 300s TTL) for catalogue resource lookups (get_album, get_track, etc.). Cached retrieval drops from 1,255 ms to 4.5 ms (~280x faster).
  3. Persistent Dealer WebSocket (_PlayerPool):
    Maintains persistent WebSocket connections for player actions (play, pause, next, volume) instead of reconnecting per call. Multi-action playback sequences speed up by ~10x (1,435 ms → 144 ms). Added --no-persistent-player CLI flag for one-shot isolation.
  4. Token Optimization (compact=True):
    Adds compact=True (enabled by default in search and now_playing) to strip redundant Spotify internal structures, cutting search response payloads by 96.2% (44.6 KB → 1.6 KB, saving ~10,700 tokens per search).

Testing

  • $N=30$ statistical benchmarking runs.
  • Full unit test suite passed: 709 tests (664 passed, 45 skipped, 0 failed).

…t player

- Add _SharedCatalogue to reuse HTTP keep-alive, tokens, and ResponseCache
- Add _PlayerPool for persistent Dealer WebSocket connection across player tools
- Support compact=True on search and now_playing to cut payload size by 96%
- Add --no-persistent-player flag to preserve one-shot per-command isolation

Aran404 commented Nov 29, 2026

Copy link
Copy Markdown
Owner

Thanks for the contribution! Could you fix the player connection-state check, tie pooled resources to the MCP lifespan, preserve all search categories in compact mode, add regression tests for pooling/reconnection/cleanup, and leave versioning and release dates unchanged?

@jdlugo jdlugo closed this Nov 29, 2026
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.

2 participants