ruflo icon indicating copy to clipboard operation
ruflo copied to clipboard

Feature request: MCP tool filtering prior to LLM with semantic search

Open qdrddr opened this issue 1 year ago β€’ 5 comments

The issue

Too many tools confuse even the SotA models even with 1M context window, creating context rot issue that getting even worse with multi-step tasks.

Proposal

Semantic Search tool pre-filtering before LLM shows improved quality and reduced confusion, reduced LLM token consumption.

With each new user request, using the most recent and a few last questions in a conversation pre-filter tools with semantic search of user question against the tools descriptions. This is basically rag for MCP tools. RAG build for MCP tool selection can apply all traditional & advanced RAG techniques like: Reranking, HyDE/ReWOO/ReAct, Step-Back Prompting, Reflection, CoT, Query Transformation (Expansion and sub-query), Rule-based retrieval, Hybrid Search, etc. Its basically just RAG and all the tricks applicable here and can yield a better result for MCP Tool selection.

Filter out most semantically relevant tools before LLM call. Then inject the limited pre-filtered tools to LLM. This ensures improved quality and reduced LLM token consumption.

Similar how it is already implemented in https://github.com/mcp-use/mcp-use

Suggested tech stacks:

LanceDB/ChrimaDB/Turso/DuckDB as an embedded (in-progress) Vector DB. Suggested embedding models is Mistral Codestral-Embed,

Reranking Qwen3-reranker (Deep infra) or https://pub.towardsai.net/run-mxbai-rerank-v2-with-infinity-4b73858cd644

Context:

A few recent papers supporting the idea of RAG and semantic search for MCP Tools: TURA: https://www.arxiv.org/pdf/2508.04604 ScaleMCP: Dynamic and Auto-Synchronizing MCP Tools https://arxiv.org/abs/2505.06416 RAG MCP https://arxiv.org/abs/2505.03275

The papers conducted experiments demonstrating that both RAG techniques decrease the number of consumed tokens while at the same time increasing task completeness score with Vector Search + Reranker. We basically improve quality and making it cheaper.

Experiments, including an MCP stress test, demonstrate RAG-MCP significantly cuts prompt tokens (e.g., by over 50%) and more than triples tool selection accuracy (43.13% vs 13.62% baseline)

qdrddr avatar Aug 17 '25 14:08 qdrddr

MCP-use implemented a tool semantic search mechanism https://github.com/mcp-use/mcp-use

qdrddr avatar Aug 19 '25 14:08 qdrddr

MCP-Agent with EmbeddingRouter for semantic tool search and filtering.

https://github.com/lastmile-ai/mcp-agent

qdrddr avatar Aug 22 '25 00:08 qdrddr

MCP-Universe: Benchmarking Large Language Models with Real-World Model Context Protocol Servers.

Key findings: β€œLong-Context Challenge

Token count increases rapidly with interaction steps, often leading to context overflow and degraded performance in multi-step tasks requiring extensive reasoning.”

This proves how MCP tool pre-filtering (prior LLM) is important, and as I was saying, especially manifests in multi-steps.

https://mcp-universe.github.io

qdrddr avatar Sep 02 '25 19:09 qdrddr

Nightly triage β€” 2026-09-10

Status: needs human input

Genuine, substantial feature request (semantic-search pre-filtering of the MCP tool list shown to the LLM, per-query) β€” not a duplicate, and not implemented. The repo has adjacent-but-distinct pieces that address the same underlying "too many tools" problem differently: a static --tools/CLAUDE_FLOW_MCP_TOOLS allowlist (config-time, not per-query/semantic, from #2726) and ADR-112's "improve tool descriptions instead of filtering" approach β€” plus general embeddings/HNSW infrastructure that exists but isn't wired to tool-list filtering. None of these do what's asked here. This needs product/design judgment (embedding model choice, where to hook into the request pipeline, latency budget per turn, auto vs. opt-in, and how it should interact with the two related efforts already in flight) rather than a same-night unattended implementation.


Generated by Claude Code

ruvnet avatar Sep 10 '26 05:09 ruvnet

Semantic pre-filter helps a lot, but we've also seen the failure mode where search returns the near-duplicate sibling (same verbs, slightly different nouns) and the model confidently picks the wrong one β€” filtering alone doesn't fix collision in the shortlist.

What has worked for us: measure confusion clusters on a labeled request set, then rename/disambiguate ("use this / not that") before shrinking to always-on core + deferred rest. Happy to share the confusion-score approach if useful: https://catalogslim-waitlist.vercel.app β€” permission-only.

slowe89 avatar Sep 11 '26 08:09 slowe89