semantic-tool-router 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (26) hide show
  1. semantic_tool_router-0.1.0/LICENSE +22 -0
  2. semantic_tool_router-0.1.0/PKG-INFO +170 -0
  3. semantic_tool_router-0.1.0/README.md +148 -0
  4. semantic_tool_router-0.1.0/pyproject.toml +32 -0
  5. semantic_tool_router-0.1.0/setup.cfg +4 -0
  6. semantic_tool_router-0.1.0/src/semantic_tool_router/__init__.py +31 -0
  7. semantic_tool_router-0.1.0/src/semantic_tool_router/__main__.py +5 -0
  8. semantic_tool_router-0.1.0/src/semantic_tool_router/cli.py +318 -0
  9. semantic_tool_router-0.1.0/src/semantic_tool_router/embeddings.py +112 -0
  10. semantic_tool_router-0.1.0/src/semantic_tool_router/evaluation.py +120 -0
  11. semantic_tool_router-0.1.0/src/semantic_tool_router/live_benchmark.py +221 -0
  12. semantic_tool_router-0.1.0/src/semantic_tool_router/mcp.py +227 -0
  13. semantic_tool_router-0.1.0/src/semantic_tool_router/models.py +57 -0
  14. semantic_tool_router-0.1.0/src/semantic_tool_router/registry.py +29 -0
  15. semantic_tool_router-0.1.0/src/semantic_tool_router/router.py +65 -0
  16. semantic_tool_router-0.1.0/src/semantic_tool_router.egg-info/PKG-INFO +170 -0
  17. semantic_tool_router-0.1.0/src/semantic_tool_router.egg-info/SOURCES.txt +24 -0
  18. semantic_tool_router-0.1.0/src/semantic_tool_router.egg-info/dependency_links.txt +1 -0
  19. semantic_tool_router-0.1.0/src/semantic_tool_router.egg-info/entry_points.txt +2 -0
  20. semantic_tool_router-0.1.0/src/semantic_tool_router.egg-info/requires.txt +6 -0
  21. semantic_tool_router-0.1.0/src/semantic_tool_router.egg-info/top_level.txt +1 -0
  22. semantic_tool_router-0.1.0/tests/test_embeddings.py +91 -0
  23. semantic_tool_router-0.1.0/tests/test_evaluation.py +46 -0
  24. semantic_tool_router-0.1.0/tests/test_live_benchmark.py +61 -0
  25. semantic_tool_router-0.1.0/tests/test_mcp.py +42 -0
  26. semantic_tool_router-0.1.0/tests/test_router.py +79 -0
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Semantic Tool Router Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,170 @@
1
+ Metadata-Version: 2.4
2
+ Name: semantic-tool-router
3
+ Version: 0.1.0
4
+ Summary: Runtime semantic discovery for agent tools.
5
+ Author: Semantic Tool Router Contributors
6
+ License: MIT
7
+ Keywords: agents,tools,retrieval,mcp,semantic-search
8
+ Classifier: Development Status :: 3 - Alpha
9
+ Classifier: Intended Audience :: Developers
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Programming Language :: Python :: 3.10
12
+ Classifier: Programming Language :: Python :: 3.11
13
+ Classifier: Programming Language :: Python :: 3.12
14
+ Requires-Python: >=3.10
15
+ Description-Content-Type: text/markdown
16
+ License-File: LICENSE
17
+ Provides-Extra: sentence-transformers
18
+ Requires-Dist: sentence-transformers>=2.2.0; extra == "sentence-transformers"
19
+ Provides-Extra: openai
20
+ Requires-Dist: openai>=1.0.0; extra == "openai"
21
+ Dynamic: license-file
22
+
23
+ # Semantic Tool Router
24
+
25
+ [![PyPI Version](https://img.shields.io/badge/pypi-v0.1.0--alpha-blue)](https://pypi.org/)
26
+ [![License](https://img.shields.io/badge/license-MIT-green)](LICENSE)
27
+ [![Python Support](https://img.shields.io/badge/python-3.10%20%7C%203.11%20%7C%203.12-blue)](pyproject.toml)
28
+ [![CI Build](https://img.shields.io/badge/build-passing-brightgreen)](.github/workflows/ci.yml)
29
+
30
+ > **Dynamic runtime tool discovery and retrieval-augmented routing for AI agents.**
31
+
32
+ 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.
33
+
34
+ ---
35
+
36
+ ## How It Works
37
+
38
+ ```mermaid
39
+ graph LR
40
+ Query[Task Query] --> Router(Tool Router)
41
+ Registry[Tool Registry] --> Router
42
+ Router --> Filters{Filters}
43
+ Filters --> LLM[LLM Context]
44
+ ```
45
+
46
+ 1. **Tool Indexing:** Tool descriptions, schemas, tags, examples, and permissions are compiled into search strings and vectorized.
47
+ 2. **Semantic Matching:** The user query is embedded and compared against the indexed tools using cosine similarity.
48
+ 3. **Metadata Filtering:** Results are filtered by permission layers (e.g. read-only vs destructive commands) or specific tags.
49
+ 4. **Context Injection:** Only the top $k$ relevant tool schemas are injected into the LLM system prompt, preserving context tokens.
50
+
51
+ ---
52
+
53
+ ## Features
54
+
55
+ * ⚡ **Zero-Dependency Hashing Baseline:** Comes with a local token-hashing vectorizer (`HashingEmbeddingProvider`) that runs instantly without external APIs or PyTorch downloads.
56
+ * 🔌 **First-Class MCP Client:** Connects to live Stdio MCP servers, imports schemas automatically, and executes selected tools under expectation guards.
57
+ * 🏷️ **Metadata-Aware Filtering:** Apply rigid tag filters or restrict tools based on security permissions (`read`, `write`, `execute`, `destructive`, `network`).
58
+ * 📈 **Evaluation Suite:** Measure retrieval metrics (`hit_rate@k`, `top_1_accuracy`, `MRR`, `context_tokens_saved`) against reproducible benchmark files.
59
+ * 🧠 **Swappable Embedders:** Easily swap the hashing provider for local Hugging Face `SentenceTransformers` or cloud APIs (`OpenAI`).
60
+
61
+ ---
62
+
63
+ ## Installation
64
+
65
+ Install the core package (includes standard hashing retriever):
66
+
67
+ ```bash
68
+ pip install -e .
69
+ ```
70
+
71
+ For advanced semantic embeddings, install the optional package extras:
72
+
73
+ ```bash
74
+ # To run local models via SentenceTransformers
75
+ pip install -e .[sentence-transformers]
76
+
77
+ # To use OpenAI's hosted embedding models
78
+ pip install -e .[openai]
79
+ ```
80
+
81
+ ---
82
+
83
+ ## Quick Start
84
+
85
+ ### 1. Basic Tool Discovery
86
+ Query a local JSON registry of tool specs:
87
+
88
+ ```bash
89
+ python -m semantic_tool_router discover "read the project README file" --registry examples/tools.json
90
+ ```
91
+
92
+ Or choose a specific embedding model:
93
+
94
+ ```bash
95
+ python -m semantic_tool_router discover "generate a mock logo" \
96
+ --registry examples/tools.json \
97
+ --embedder sentence-transformers \
98
+ --embedding-model all-MiniLM-L6-v2
99
+ ```
100
+
101
+ ### 2. Live MCP Routing
102
+ 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:
103
+
104
+ ```powershell
105
+ python -m semantic_tool_router mcp-discover \
106
+ "read the first lines of the project README" \
107
+ --top-k 3 \
108
+ --allow-permission read \
109
+ --expect-tool read_text_file \
110
+ --call-argument "path=README.md" \
111
+ --call-argument "head=8" \
112
+ --server npx -y @modelcontextprotocol/server-filesystem .
113
+ ```
114
+
115
+ ---
116
+
117
+ ## Integrations
118
+
119
+ Use the router as a preprocessing step inside standard orchestrator loops to save prompt tokens:
120
+
121
+ * **LangChain Agent Integration:** See the [langchain_integration.py](examples/langchain_integration.py) template.
122
+ * **LlamaIndex Agent Integration:** See the [llamaindex_integration.py](examples/llamaindex_integration.py) template.
123
+
124
+ ---
125
+
126
+ ## Benchmarking & Evaluation
127
+
128
+ Evaluate your router configuration on fixture datasets:
129
+
130
+ ```bash
131
+ python -m semantic_tool_router benchmark \
132
+ --registry examples/tools.json \
133
+ --tasks benchmarks/tasks.json \
134
+ --top-k 3
135
+ ```
136
+
137
+ To run the reproducible baseline benchmark suite across four official live MCP reference servers (Filesystem, Memory, Sequential Thinking, and Everything):
138
+
139
+ ```bash
140
+ python -m semantic_tool_router mcp-benchmark \
141
+ --suite benchmarks/live_mcp_suite.json \
142
+ --workspace . \
143
+ --markdown-output benchmarks/results/live_mcp_baseline.md
144
+ ```
145
+
146
+ ---
147
+
148
+ ## Testing
149
+
150
+ Run unit tests locally across mock registry and MCP environments:
151
+
152
+ ```bash
153
+ python -m unittest discover -s tests
154
+ ```
155
+
156
+ ---
157
+
158
+ ## Contributing & Development
159
+
160
+ Contributions are welcome! Please run tests and benchmarking commands to verify that metrics remain high before submitting pull requests.
161
+
162
+ 1. Fork the repo and clone locally.
163
+ 2. Setup tests: `python -m pip install -e .[sentence-transformers,openai]`
164
+ 3. Ensure CI checks pass: `python -m unittest discover -s tests`
165
+
166
+ ---
167
+
168
+ ## License
169
+
170
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
@@ -0,0 +1,148 @@
1
+ # Semantic Tool Router
2
+
3
+ [![PyPI Version](https://img.shields.io/badge/pypi-v0.1.0--alpha-blue)](https://pypi.org/)
4
+ [![License](https://img.shields.io/badge/license-MIT-green)](LICENSE)
5
+ [![Python Support](https://img.shields.io/badge/python-3.10%20%7C%203.11%20%7C%203.12-blue)](pyproject.toml)
6
+ [![CI Build](https://img.shields.io/badge/build-passing-brightgreen)](.github/workflows/ci.yml)
7
+
8
+ > **Dynamic runtime tool discovery and retrieval-augmented routing for AI agents.**
9
+
10
+ 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.
11
+
12
+ ---
13
+
14
+ ## How It Works
15
+
16
+ ```mermaid
17
+ graph LR
18
+ Query[Task Query] --> Router(Tool Router)
19
+ Registry[Tool Registry] --> Router
20
+ Router --> Filters{Filters}
21
+ Filters --> LLM[LLM Context]
22
+ ```
23
+
24
+ 1. **Tool Indexing:** Tool descriptions, schemas, tags, examples, and permissions are compiled into search strings and vectorized.
25
+ 2. **Semantic Matching:** The user query is embedded and compared against the indexed tools using cosine similarity.
26
+ 3. **Metadata Filtering:** Results are filtered by permission layers (e.g. read-only vs destructive commands) or specific tags.
27
+ 4. **Context Injection:** Only the top $k$ relevant tool schemas are injected into the LLM system prompt, preserving context tokens.
28
+
29
+ ---
30
+
31
+ ## Features
32
+
33
+ * ⚡ **Zero-Dependency Hashing Baseline:** Comes with a local token-hashing vectorizer (`HashingEmbeddingProvider`) that runs instantly without external APIs or PyTorch downloads.
34
+ * 🔌 **First-Class MCP Client:** Connects to live Stdio MCP servers, imports schemas automatically, and executes selected tools under expectation guards.
35
+ * 🏷️ **Metadata-Aware Filtering:** Apply rigid tag filters or restrict tools based on security permissions (`read`, `write`, `execute`, `destructive`, `network`).
36
+ * 📈 **Evaluation Suite:** Measure retrieval metrics (`hit_rate@k`, `top_1_accuracy`, `MRR`, `context_tokens_saved`) against reproducible benchmark files.
37
+ * 🧠 **Swappable Embedders:** Easily swap the hashing provider for local Hugging Face `SentenceTransformers` or cloud APIs (`OpenAI`).
38
+
39
+ ---
40
+
41
+ ## Installation
42
+
43
+ Install the core package (includes standard hashing retriever):
44
+
45
+ ```bash
46
+ pip install -e .
47
+ ```
48
+
49
+ For advanced semantic embeddings, install the optional package extras:
50
+
51
+ ```bash
52
+ # To run local models via SentenceTransformers
53
+ pip install -e .[sentence-transformers]
54
+
55
+ # To use OpenAI's hosted embedding models
56
+ pip install -e .[openai]
57
+ ```
58
+
59
+ ---
60
+
61
+ ## Quick Start
62
+
63
+ ### 1. Basic Tool Discovery
64
+ Query a local JSON registry of tool specs:
65
+
66
+ ```bash
67
+ python -m semantic_tool_router discover "read the project README file" --registry examples/tools.json
68
+ ```
69
+
70
+ Or choose a specific embedding model:
71
+
72
+ ```bash
73
+ python -m semantic_tool_router discover "generate a mock logo" \
74
+ --registry examples/tools.json \
75
+ --embedder sentence-transformers \
76
+ --embedding-model all-MiniLM-L6-v2
77
+ ```
78
+
79
+ ### 2. Live MCP Routing
80
+ 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:
81
+
82
+ ```powershell
83
+ python -m semantic_tool_router mcp-discover \
84
+ "read the first lines of the project README" \
85
+ --top-k 3 \
86
+ --allow-permission read \
87
+ --expect-tool read_text_file \
88
+ --call-argument "path=README.md" \
89
+ --call-argument "head=8" \
90
+ --server npx -y @modelcontextprotocol/server-filesystem .
91
+ ```
92
+
93
+ ---
94
+
95
+ ## Integrations
96
+
97
+ Use the router as a preprocessing step inside standard orchestrator loops to save prompt tokens:
98
+
99
+ * **LangChain Agent Integration:** See the [langchain_integration.py](examples/langchain_integration.py) template.
100
+ * **LlamaIndex Agent Integration:** See the [llamaindex_integration.py](examples/llamaindex_integration.py) template.
101
+
102
+ ---
103
+
104
+ ## Benchmarking & Evaluation
105
+
106
+ Evaluate your router configuration on fixture datasets:
107
+
108
+ ```bash
109
+ python -m semantic_tool_router benchmark \
110
+ --registry examples/tools.json \
111
+ --tasks benchmarks/tasks.json \
112
+ --top-k 3
113
+ ```
114
+
115
+ To run the reproducible baseline benchmark suite across four official live MCP reference servers (Filesystem, Memory, Sequential Thinking, and Everything):
116
+
117
+ ```bash
118
+ python -m semantic_tool_router mcp-benchmark \
119
+ --suite benchmarks/live_mcp_suite.json \
120
+ --workspace . \
121
+ --markdown-output benchmarks/results/live_mcp_baseline.md
122
+ ```
123
+
124
+ ---
125
+
126
+ ## Testing
127
+
128
+ Run unit tests locally across mock registry and MCP environments:
129
+
130
+ ```bash
131
+ python -m unittest discover -s tests
132
+ ```
133
+
134
+ ---
135
+
136
+ ## Contributing & Development
137
+
138
+ Contributions are welcome! Please run tests and benchmarking commands to verify that metrics remain high before submitting pull requests.
139
+
140
+ 1. Fork the repo and clone locally.
141
+ 2. Setup tests: `python -m pip install -e .[sentence-transformers,openai]`
142
+ 3. Ensure CI checks pass: `python -m unittest discover -s tests`
143
+
144
+ ---
145
+
146
+ ## License
147
+
148
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
@@ -0,0 +1,32 @@
1
+ [build-system]
2
+ requires = ["setuptools>=68"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "semantic-tool-router"
7
+ version = "0.1.0"
8
+ description = "Runtime semantic discovery for agent tools."
9
+ readme = "README.md"
10
+ requires-python = ">=3.10"
11
+ authors = [{ name = "Semantic Tool Router Contributors" }]
12
+ license = { text = "MIT" }
13
+ keywords = ["agents", "tools", "retrieval", "mcp", "semantic-search"]
14
+ classifiers = [
15
+ "Development Status :: 3 - Alpha",
16
+ "Intended Audience :: Developers",
17
+ "Programming Language :: Python :: 3",
18
+ "Programming Language :: Python :: 3.10",
19
+ "Programming Language :: Python :: 3.11",
20
+ "Programming Language :: Python :: 3.12",
21
+ ]
22
+
23
+ [project.optional-dependencies]
24
+ sentence-transformers = ["sentence-transformers>=2.2.0"]
25
+ openai = ["openai>=1.0.0"]
26
+
27
+ [project.scripts]
28
+ semantic-tool-router = "semantic_tool_router.cli:main"
29
+
30
+ [tool.setuptools.packages.find]
31
+ where = ["src"]
32
+
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,31 @@
1
+ from semantic_tool_router.embeddings import (
2
+ HashingEmbeddingProvider,
3
+ OpenAIEmbeddingProvider,
4
+ SentenceTransformerEmbeddingProvider,
5
+ )
6
+ from semantic_tool_router.evaluation import (
7
+ BenchmarkReport,
8
+ BenchmarkTask,
9
+ TaskEvaluation,
10
+ evaluate,
11
+ )
12
+ from semantic_tool_router.models import DiscoveryResult, ToolSpec
13
+ from semantic_tool_router.mcp import McpServerSnapshot, StdioMcpClient
14
+ from semantic_tool_router.registry import ToolRegistry
15
+ from semantic_tool_router.router import ToolRouter
16
+
17
+ __all__ = [
18
+ "BenchmarkReport",
19
+ "BenchmarkTask",
20
+ "DiscoveryResult",
21
+ "HashingEmbeddingProvider",
22
+ "McpServerSnapshot",
23
+ "OpenAIEmbeddingProvider",
24
+ "SentenceTransformerEmbeddingProvider",
25
+ "StdioMcpClient",
26
+ "TaskEvaluation",
27
+ "ToolRegistry",
28
+ "ToolRouter",
29
+ "ToolSpec",
30
+ "evaluate",
31
+ ]
@@ -0,0 +1,5 @@
1
+ from semantic_tool_router.cli import main
2
+
3
+ if __name__ == "__main__":
4
+ raise SystemExit(main())
5
+