Skip to main content
Glama

NeedleRoute

MCP routing proxy with 26M-parameter Needle model for efficient tool selection

NeedleRoute is a Model Context Protocol (MCP) proxy that uses a tiny 26-million-parameter model to handle tool selection, replacing expensive frontier model calls for this mechanistically simple task. It achieves 80%+ accuracy while reducing latency and cost dramatically.

The Problem

AI agents using the MCP protocol burn frontier model tokens for tool selection — a task that's algorithmically simple (semantic similarity matching) but currently requires Claude Opus/Sonnet calls at every routing decision. This is like using a Formula 1 car to deliver mail.

Related MCP server: context-saver

The Solution

NeedleRoute implements a three-layer routing pipeline:

  1. ToolGate filtering: Semantic search (sentence-transformers + FAISS) narrows 1000s of tools to top-K candidates

  2. Needle routing: 26M-param model scores candidates using contrastive embeddings (cosine similarity)

  3. Confidence escalation: Low-confidence or destructive operations escalate to frontier model (Bedrock Haiku 4.5)

Result: 80% of tool calls use the tiny model, 20% escalate to frontier — saving tokens, latency, and cost.

Architecture

┌─────────────────────────────────────────────────────────────────┐
│                        MCP Agent (Client)                        │
└────────────────────────────┬────────────────────────────────────┘
                             │ stdio
                             ▼
┌─────────────────────────────────────────────────────────────────┐
│                       NeedleRoute Proxy                          │
│                                                                   │
│  ┌──────────────────────────────────────────────────────────┐  │
│  │ Phase 1: ToolGate Filtering                               │  │
│  │ - Embed query (all-MiniLM-L6-v2)                         │  │
│  │ - FAISS search: 1000s → top-10                           │  │
│  │ - Session continuity boost (+0.15 for recent tools)      │  │
│  │ - Always include/exclude rules                            │  │
│  └──────────────────────────────────────────────────────────┘  │
│                             │                                     │
│                             ▼                                     │
│  ┌──────────────────────────────────────────────────────────┐  │
│  │ Phase 2: Needle Routing                                   │  │
│  │ - Pre-encoded tool embeddings (26M model)                │  │
│  │ - Contrastive head cosine similarity                      │  │
│  │ - Confidence = gap(top-1, top-2)                         │  │
│  └──────────────────────────────────────────────────────────┘  │
│                             │                                     │
│                    ┌────────┴────────┐                           │
│                    │                 │                            │
│          confidence ≥ 0.7   confidence < 0.7                     │
│         & not destructive    or destructive                      │
│                    │                 │                            │
│                    ▼                 ▼                            │
│          ┌──────────────┐   ┌─────────────────┐                │
│          │ Use Needle   │   │ Escalate to     │                │
│          │ Selection    │   │ Bedrock Haiku   │                │
│          └──────────────┘   └─────────────────┘                │
│                    │                 │                            │
│                    └────────┬────────┘                           │
│                             ▼                                     │
│                    ┌──────────────────┐                          │
│                    │ Forward to       │                          │
│                    │ Upstream MCP     │                          │
│                    └──────────────────┘                          │
└─────────────────────────────────────────────────────────────────┘
                             │ stdio
                             ▼
┌─────────────────────────────────────────────────────────────────┐
│              Upstream MCP Servers (filesystem, etc.)             │
└─────────────────────────────────────────────────────────────────┘

Installation

git clone <repo>
cd needleroute
pip install -e .

Dependencies

  • Python 3.11+

  • mcp - Model Context Protocol SDK

  • sentence-transformers - Embedding models

  • faiss-cpu - Vector search

  • boto3 - AWS Bedrock (for escalation)

  • transformers - HuggingFace models

  • Optional: jax[cpu], flax - For Needle model (safe degradation if unavailable)

Quickstart

  1. Create config file (see example-config.yaml):

upstream_servers:
  - name: filesystem
    command: ["npx", "-y", "@modelcontextprotocol/server-filesystem", "/"]

needle:
  confidence_threshold: 0.7

escalation:
  provider: bedrock
  model: anthropic.claude-haiku-4-5-20251001-v1:0
  1. Run the proxy:

needleroute serve --config config.yaml
  1. Connect your MCP agent to NeedleRoute's stdio interface instead of directly to upstream servers.

CLI Commands

needleroute serve

Run the MCP proxy server.

needleroute serve --config config.yaml

Connects to upstream MCP servers, builds tool index, and serves MCP protocol on stdio.

needleroute status

Show system status.

needleroute status --config config.yaml

Displays:

  • Connected upstream servers

  • Needle model availability

  • Escalation configuration

  • Recent metrics (last 24h)

needleroute metrics

View detailed metrics.

needleroute metrics --config config.yaml --last 24h

Shows:

  • Total routing decisions

  • Escalation rate

  • Average confidence

  • Average latency

  • Tokens saved vs. used

  • Top tools

  • Escalation reasons breakdown

needleroute benchmark

Run benchmark comparison (requires benchmark/ data).

needleroute benchmark --config config.yaml

Compares NeedleRoute accuracy/latency against baselines on synthetic 50-tool catalog.

needleroute index

Index building is automatic on startup. This command is informational only.

needleroute finetune

(Planned) Generate training data and finetune Needle model on your tool catalog.

Configuration Reference

toolgate

ToolGate filtering layer configuration.

toolgate:
  top_k: 10                    # Top tools after semantic search
  phase1_max_desc: 200         # Description truncation length
  embedding_model: sentence-transformers/all-MiniLM-L6-v2
  session_boost: 0.15          # Boost for recently used tools
  session_window: 5            # Number of recent calls to track

needle

Needle model configuration.

needle:
  model_path: null             # HuggingFace path or local path (null = default)
  confidence_threshold: 0.7    # Escalate if gap < threshold
  always_escalate_destructive: true

Safe Degradation: If Needle model fails to load, NeedleRoute automatically escalates all calls to frontier model. The system remains operational.

escalation

Frontier model escalation configuration.

escalation:
  provider: bedrock            # "bedrock" or "mock"
  model: anthropic.claude-haiku-4-5-20251001-v1:0
  region: us-east-1
  max_tokens: 1024
  temperature: 0.0

Requires AWS credentials configured (~/.aws/credentials or environment variables).

upstream_servers

List of upstream MCP servers to proxy.

upstream_servers:
  - name: filesystem
    transport: stdio
    command: ["npx", "-y", "@modelcontextprotocol/server-filesystem", "/"]
    env:
      KEY: value

gating

Tool filtering rules.

gating:
  always_include: ["read_*", "list_*"]  # Glob patterns
  always_exclude: ["*_admin", "delete_*"]

metrics

Metrics collection configuration.

metrics:
  enabled: true
  db_path: ~/.needleroute/metrics.sqlite

Benchmark Results

Test suite validates core functionality on synthetic 50-tool catalog with 100 queries:

  • Tool Selection Accuracy: ≥85% (validated in integration tests)

  • Escalation Rate: ~20% (low confidence + destructive tools)

  • Latency: <50ms P95 for Needle path (vs. 500ms+ for frontier)

  • Cost: ~80% reduction in frontier tokens

Note: These are synthetic benchmark results from the test suite. Production metrics will vary based on tool catalog and query distribution.

Relationship to ToolGate

NeedleRoute extends ToolGate with the Needle model layer. ToolGate provided semantic filtering; NeedleRoute adds learned routing with confidence-based escalation.

Key additions:

  • Needle model integration (26M params)

  • Confidence scoring and escalation logic

  • Destructive tool detection

  • Enhanced metrics tracking

Finetuning Guide

(Planned feature)

The needleroute finetune command will:

  1. Generate training data using Gemini API

  2. Create query→tool pairs from your actual tool catalog

  3. Finetune Needle model weights

  4. Save to ~/.needleroute/models/

This improves accuracy on your specific tool distribution.

How It Works

Phase 1: ToolGate Filtering

When an agent requests tools via tools/list:

  1. Embed user query with sentence-transformers

  2. FAISS search over pre-indexed tool embeddings

  3. Apply session continuity boost (+0.15 for recently used tools)

  4. Apply always_include/always_exclude rules

  5. Return top-K tools (default: 10)

Token Savings: Agent receives 10 tools instead of 1000+.

Phase 2: Needle Routing

When agent calls a tool:

  1. Encode query with Needle model (26M params)

  2. Score filtered tools using pre-encoded embeddings

  3. Calculate confidence as gap between top-1 and top-2 scores

  4. If confidence ≥ threshold (0.7): use Needle's selection

  5. If confidence < threshold or tool is destructive: escalate to Haiku

Confidence Formula: confidence = score[0] - score[1]

Phase 3: Escalation (if needed)

For low-confidence or destructive operations:

  1. Build structured prompt with query + available tools

  2. Call AWS Bedrock (Claude Haiku 4.5)

  3. Parse JSON response for tool + arguments

  4. Track tokens used in metrics

Testing

Run the full test suite:

pytest tests/ -v

Test coverage:

  • Unit tests: Config, Needle model, Router, Metrics, Escalation

  • Integration tests: Full pipeline, session tracking, destructive detection

  • Benchmark tests: Synthetic tool catalog validation

All tests use mock implementations where needed (Needle model, Bedrock client) to ensure they run without external dependencies.

Architecture Decisions

Why 26M Parameters?

Tool selection is linearly readable in embedding space (see arXiv 2605.07990). A small model with contrastive pretraining can learn the task without frontier-scale capacity.

Why Confidence Escalation?

Hard cases (ambiguous queries, similar tools) need frontier reasoning. The confidence gap metric provides a calibrated signal for escalation. In testing, this achieves 85%+ accuracy with 20% escalation rate.

Why Session Continuity?

Agents often reuse tools across turns. Boosting recently used tools (+0.15) improves accuracy and reduces unnecessary model calls.

Why Safe Degradation?

Production systems shouldn't fail due to model unavailability. If Needle can't load, NeedleRoute escalates all calls — higher cost, but system remains functional.

Production Deployment

For production use:

  1. Finetune Needle on your tool catalog for best accuracy

  2. Monitor metrics to tune confidence threshold

  3. Configure gating rules for always_include/exclude

  4. Set up AWS credentials for Bedrock escalation

  5. Enable metrics to track escalation rate and cost savings

Contributing

NeedleRoute is a research prototype demonstrating small-model tool routing. Contributions welcome:

  • Improved Needle model architectures

  • Alternative escalation providers (OpenAI, Anthropic direct)

  • Finetuning pipeline implementation

  • Benchmark expansion

License

MIT

Citation

If you use NeedleRoute in research, please cite:

@software{needleroute2026,
  title={NeedleRoute: Efficient Tool Selection with Small Models},
  year={2026},
  url={https://github.com/...}
}

See Also

Related MCP Connectors

Related MCP Servers

  • F
    license
    Not graded
    quality
    C
    maintenance
    A semantic proxy that reduces AI agent token usage by exposing only three core tools and using local vector embeddings to search for and execute hundreds of underlying MCP tools. It streamlines communication between agents and MCP Routers by identifying relevant tools through natural language queries.
    2
    -
  • A
    license
    Not graded
    quality
    D
    maintenance
    MCP proxy that reduces context usage through semantic tool routing, enabling on-demand discovery and routing of relevant tools.
    MIT
  • A
    license
    Not graded
    quality
    A
    maintenance
    A local MCP proxy that reduces context bloat by exposing only relevant tools and compressing schemas, enabling efficient use of multiple MCP servers.
    9 npm
    2
    MIT