Runtime semantic discovery for agent tools.
Project description
Semantic Tool Router
Dynamic runtime tool discovery and retrieval-augmented routing for AI agents.
Semantic Tool Router is a dependency-light library designed to manage the "Many-Tool" problem in LLM and Agentic workflows. Instead of exposing every available tool or Model Context Protocol (MCP) server schema to a model context window (which increases costs and degrades accuracy), it embeds tools based on their descriptions and dynamically retrieves a focused candidate set ($top-k$) for the current task.
How It Works
graph LR
Query[Task Query] --> Router(Tool Router)
Registry[Tool Registry] --> Router
Router --> Filters{Filters}
Filters --> LLM[LLM Context]
- Tool Indexing: Tool descriptions, schemas, tags, examples, and permissions are compiled into search strings and vectorized.
- Semantic Matching: The user query is embedded and compared against the indexed tools using cosine similarity.
- Metadata Filtering: Results are filtered by permission layers (e.g. read-only vs destructive commands) or specific tags.
- Context Injection: Only the top $k$ relevant tool schemas are injected into the LLM system prompt, preserving context tokens.
Features
- ⚡ Zero-Dependency Hashing Baseline: Comes with a local token-hashing vectorizer (
HashingEmbeddingProvider) that runs instantly without external APIs or PyTorch downloads. - 🔌 First-Class MCP Client: Connects to live Stdio MCP servers, imports schemas automatically, and executes selected tools under expectation guards.
- 🏷️ Metadata-Aware Filtering: Apply rigid tag filters or restrict tools based on security permissions (
read,write,execute,destructive,network). - 📈 Evaluation Suite: Measure retrieval metrics (
hit_rate@k,top_1_accuracy,MRR,context_tokens_saved) against reproducible benchmark files. - 🧠 Swappable Embedders: Easily swap the hashing provider for local Hugging Face
SentenceTransformersor cloud APIs (OpenAI).
Installation
Install the core package (includes standard hashing retriever):
pip install -e .
For advanced semantic embeddings, install the optional package extras:
# To run local models via SentenceTransformers
pip install -e .[sentence-transformers]
# To use OpenAI's hosted embedding models
pip install -e .[openai]
Quick Start
1. Basic Tool Discovery
Query a local JSON registry of tool specs:
python -m semantic_tool_router discover "read the project README file" --registry examples/tools.json
Or choose a specific embedding model:
python -m semantic_tool_router discover "generate a mock logo" \
--registry examples/tools.json \
--embedder sentence-transformers \
--embedding-model all-MiniLM-L6-v2
2. Live MCP Routing
Connect to a live filesystem MCP server, dynamically retrieve the top-3 candidate tools matching your task, and execute the selected tool with safety parameters:
python -m semantic_tool_router mcp-discover \
"read the first lines of the project README" \
--top-k 3 \
--allow-permission read \
--expect-tool read_text_file \
--call-argument "path=README.md" \
--call-argument "head=8" \
--server npx -y @modelcontextprotocol/server-filesystem .
Integrations
Use the router as a preprocessing step inside standard orchestrator loops to save prompt tokens:
- LangChain Agent Integration: See the langchain_integration.py template.
- LlamaIndex Agent Integration: See the llamaindex_integration.py template.
Benchmarking & Evaluation
Evaluate your router configuration on fixture datasets:
python -m semantic_tool_router benchmark \
--registry examples/tools.json \
--tasks benchmarks/tasks.json \
--top-k 3
To run the reproducible baseline benchmark suite across four official live MCP reference servers (Filesystem, Memory, Sequential Thinking, and Everything):
python -m semantic_tool_router mcp-benchmark \
--suite benchmarks/live_mcp_suite.json \
--workspace . \
--markdown-output benchmarks/results/live_mcp_baseline.md
Testing
Run unit tests locally across mock registry and MCP environments:
python -m unittest discover -s tests
Contributing & Development
Contributions are welcome! Please run tests and benchmarking commands to verify that metrics remain high before submitting pull requests.
- Fork the repo and clone locally.
- Setup tests:
python -m pip install -e .[sentence-transformers,openai] - Ensure CI checks pass:
python -m unittest discover -s tests
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file semantic_tool_router-0.1.0.tar.gz.
File metadata
- Download URL: semantic_tool_router-0.1.0.tar.gz
- Upload date:
- Size: 20.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
95eb13dbe104b21acef0002cc2707fe26f3707294d71f0400fbac1a79ab94d05
|
|
| MD5 |
8dd0a34e0eb914343632e76fd31a1671
|
|
| BLAKE2b-256 |
95df1856c11e3896db7d405b7dcd82dabe900df84118abdca27b677a7b0b90c2
|
File details
Details for the file semantic_tool_router-0.1.0-py3-none-any.whl.
File metadata
- Download URL: semantic_tool_router-0.1.0-py3-none-any.whl
- Upload date:
- Size: 18.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f981db71a2b655ac27a02cb0353bf13bb7613f5a3936951e80677f4becb13e2d
|
|
| MD5 |
424ab63e0c1a69718f54889707e96882
|
|
| BLAKE2b-256 |
858f858932f75679640f512888c2209b9715224fd58ca8b6b04521c5dbae6f88
|