mcpField
T
Tool Gating MCP

Tool Gating MCP

本地服务
github.com/ajbmachonAI 与智能体673 周前更新

关于 Tool Gating MCP

Tool Gating MCP 是连接 MCP 客户端与多个 MCP 服务工具的代理。它通过语义发现和按需提供工具,在令牌预算内筛选客户端可用工具。

主要功能

  1. 01通过单一连接接入多个 MCP 服务
  2. 02语义搜索相关工具
  3. 03按需提供工具
  4. 04按令牌预算筛选工具
  5. 05将调用路由到后端服务

适用场景

  1. 01查找已注册服务中的相关工具
  2. 02减少 MCP 客户端加载的工具数量
  3. 03通过一个端点将客户端连接到多个后端服务

MCP 相关

它作为 MCP 服务和代理,管理与后端 MCP 服务的连接、发现其工具,并将工具调用路由至相应服务。

配置

本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。

暂未收录安装方式

我们还没有获取到这个服务的安装包或远程地址,请按项目 README 中的说明安装。

代码仓库

工具

还没有获取到工具列表

本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。

详细介绍

Tool Gating MCP

面向 Model Context Protocol(MCP)的智能代理/路由器,使 Claude Desktop 和其他 MCP 客户端能够动态发现并使用来自多个 MCP 服务的工具,同时保持单一连接入口。该系统通过智能选择与每项任务最相关的工具,防止上下文膨胀。

🎯 问题

像 Claude Desktop 这样的 MCP 客户端必须在启动时加载所有服务,并且无法在对话期间动态添加服务。使用多个 MCP 服务时:

  • Exa 服务:7 个搜索工具(网页、研究论文、Twitter、公司等)
  • Puppeteer:浏览器自动化工具
  • Context7:文档搜索工具
  • Desktop Commander:18 个以上的桌面自动化工具

直接加载所有服务会导致:

  • 🚨 上下文膨胀:100 多个工具占用大部分上下文窗口
  • 🔒 静态配置:无法在不重启 Claude 的情况下添加服务
  • 💸 成本增加:每次请求消耗更多 token
  • 🎯 工具选择不佳:AI 难以从过多选项中做出选择

💡 解决方案

Tool Gating MCP 充当智能代理,能够:

  1. 单一连接:Claude Desktop 只连接到 Tool Gating
  2. 后端管理:保持与多个 MCP 服务的连接
  3. 智能发现:使用语义搜索,在所有服务中查找相关工具
  4. 动态提供:在 token 预算内仅加载所需工具
  5. 透明路由:在适当的后端服务上执行工具

示例:无需配置 10 个包含 100 多个工具的 MCP 服务,只需配置 Tool Gating。然后动态发现并使用所需的 2–3 个工具。

🚀 功能

  • 代理架构:单个 MCP 服务,可将请求路由到多个后端服务
  • 动态工具发现:无需手动配置,即可查找所有服务中的工具
  • 语义搜索:通过自然语言查询查找合适的工具
  • 智能提供:在 token 预算内仅加载相关工具
  • 透明执行:将工具调用路由到适当的后端服务
  • 原生 MCP 服务:通过 mcp-proxy 与 Claude Desktop 直接集成
  • 跨服务智能:统一查看来自 Puppeteer、Exa、Context7 等的工具
  • Token 优化:与加载所有服务相比,上下文用量减少 90% 以上
  • 零配置:Claude Desktop 只需配置 Tool Gating

📋 前置条件

  • Python 3.12+
  • uv 包管理器

🔧 安装

  1. 克隆仓库:
git clone https://github.com/yourusername/tool-gating-mcp.git
cd tool-gating-mcp
  1. 创建并激活虚拟环境:
uv venv
source .venv/bin/activate  # On Unix/macOS
# .venv\Scripts\activate  # On Windows
  1. 安装依赖:
uv sync
  1. 以开发模式安装软件包:
uv pip install -e .

🏃 运行服务

作为 HTTP API 服务
# Start the server
tool-gating-mcp

# Or with uvicorn for development
uvicorn tool_gating_mcp.main:app --reload

服务将在 http://localhost:8000 上运行

API 文档地址:

  • Swagger UI:http://localhost:8000/docs
  • ReDoc:http://localhost:8000/redoc
  • MCP 端点:http://localhost:8000/mcp(SSE 传输)
作为 MCP 服务(推荐)

Tool Gating MCP 现已成为原生 MCP 服务,可直接与 Claude Desktop 配合使用:

  1. 启动服务:

    tool-gating-mcp
    
  2. 安装 mcp-proxy(如果尚未安装):

    uv tool install mcp-proxy
    
  3. 添加到 Claude Desktop:

    编辑 ~/Library/Application Support/Claude/claude_desktop_config.json:

    {
      "mcpServers": {
        "tool-gating": {
          "command": "/Users/YOUR_USERNAME/.local/bin/mcp-proxy",
          "args": ["http://localhost:8000/mcp"]
        }
      }
    }
    

    注意:将 YOUR_USERNAME 替换为你的实际用户名。

  4. 重启 Claude Desktop

你将在 Claude 中看到 Tool Gating 提供的工具,包括:

  • discover_tools - 根据查询查找相关工具
  • provision_tools - 在 token 预算内选择工具
  • register_tool - 向注册表添加新工具
  • list_mcp_servers - 查看已注册的 MCP 服务
  • 以及更多!

详细说明请参阅 MCP Native Usage Guide。

🔍 API 端点

工具发现
POST /api/tools/discover

基于语义搜索发现相关工具。

请求:

{
  "query": "I need to perform calculations",
  "tags": ["math", "calculation"],
  "limit": 5
}

响应:

{
  "tools": [
    {
      "tool_id": "calculator",
      "name": "Calculator",
      "description": "Perform mathematical calculations",
      "score": 0.95,
      "matched_tags": ["math", "calculation"],
      "estimated_tokens": 50
    }
  ],
  "query_id": "uuid",
  "timestamp": "2024-01-01T00:00:00"
}
工具配置
POST /api/tools/provision

选择并格式化供大模型使用的工具,并强制执行令牌预算。

请求:

{
  "tool_ids": ["calculator", "web-search"],
  "max_tools": 3
}

响应:

{
  "tools": [
    {
      "name": "Calculator",
      "description": "Perform mathematical calculations",
      "parameters": { "type": "object", "properties": {...} },
      "token_count": 50
    }
  ],
  "metadata": {
    "total_tokens": 150,
    "gating_applied": true
  }
}

🔄 工作原理

  1. Claude Desktop 配置:仅配置 Tool Gating MCP

    {
      "mcpServers": {
        "tool-gating": {
          "command": "mcp-proxy",
          "args": ["http://localhost:8000/mcp"]
        }
      }
    }
    
  2. 后端服务连接:Tool Gating 连接到多个 MCP 服务

    Tool Gating → puppeteer (browser tools)
               → exa (search tools)
               → context7 (documentation)
               → filesystem (file operations)
    
  3. 自然语言发现:「我需要搜索研究论文」

    Claude → discover_tools → Semantic Search → Returns relevant tools
    
  4. 实时工具执行:按需加载工具

    execute_tool("exa_research_paper_search", {...}) → Validates → Loads → Executes
    

    无需配置!使用工具时会动态加载。

🎯 使用示例

运行演示
# Make sure the server is running first
tool-gating-mcp

# In another terminal, run the interactive demo
python demo.py
手动测试
# Test the server endpoints
python test_server.py
示例:查找数学工具
import httpx
import asyncio

async def find_math_tools():
    async with httpx.AsyncClient() as client:
        response = await client.post(
            "http://localhost:8000/api/tools/discover",
            json={
                "query": "I need to solve equations",
                "tags": ["math"],
                "limit": 3
            }
        )
        tools = response.json()["tools"]
        print(f"Found {len(tools)} relevant tools")
        for tool in tools:
            print(f"- {tool['name']}: {tool['score']:.3f}")

asyncio.run(find_math_tools())

🧪 测试

# Run all tests
pytest

# Run with coverage
pytest --cov=tool_gating_mcp

# Run specific test files
pytest tests/test_discovery_service.py -v

# Run integration tests
pytest tests/test_integration.py -v

📊 架构详情

  1. 工具注册:使用元数据、标签和令牌估算值注册工具
  2. 语义搜索:使用句子转换器对用户查询进行嵌入
  3. 相关性评分:根据以下因素为工具评分:
    • 查询与工具嵌入之间的余弦相似度
    • 标签匹配(每匹配一个标签加 0.2 分)
  4. 实时加载:执行过程中按需验证并加载工具
  5. MCP 格式化:根据 MCP 协议格式化所选工具

🔧 配置

系统采用合理的默认值,也可以进行配置:

  • 最大令牌数:默认每个请求 2000 个令牌
  • 最大工具数:默认每个请求 10 个工具
  • 嵌入模型:all-MiniLM-L6-v2(384 维嵌入)

📖 文档

📁 项目结构

tool-gating-mcp/
├── src/
│   └── tool_gating_mcp/
│       ├── __init__.py
│       ├── main.py              # FastAPI application
│       ├── api/
│       │   ├── models.py        # Pydantic models
│       │   ├── tools.py         # Tool management endpoints
│       │   └── mcp.py           # MCP server endpoints
│       ├── models/
│       │   └── tool.py          # Domain models
│       └── services/
│           ├── discovery.py     # Semantic search
│           ├── gating.py        # Tool selection logic
│           └── repository.py    # Tool storage
├── tests/
│   ├── test_*.py               # Test files
│   └── test_integration.py     # Integration tests
├── demo.py                     # Interactive demo
├── test_server.py              # Manual testing script
└── pyproject.toml              # Project configuration

🔌 与 MCP 服务集成

注册 MCP 服务中的工具
# 1. Clear existing demo tools
DELETE /api/tools/clear

# 2. Register tools from your MCP servers
POST /api/tools/register
{
  "id": "exa_research_paper_search",
  "name": "research_paper_search",
  "description": "Search across 100M+ research papers with full text access",
  "tags": ["search", "research", "academic"],
  "estimated_tokens": 250,
  "server": "exa",
  "parameters": {
    "type": "object",
    "properties": {
      "query": {"type": "string"},
      "numResults": {"type": "number", "default": 5}
    },
    "required": ["query"]
  }
}
与大模型编排配合使用
  1. 大模型接收用户查询:「查找近期有关量子计算的论文」
  2. 编排器查询工具门控:
    POST /api/tools/discover
    {"query": "find research papers", "limit": 3}
    
  3. 系统返回相关工具:仅返回研究工具,不包含文件编辑器
  4. 编排器配置工具:
    POST /api/tools/provision
    {"tool_ids": ["exa_research_paper_search"], "max_tokens": 500}
    
  5. 大模型直接通过 MCP 服务执行:使用已配置的工具定义
AI 辅助服务注册

AI 助手可以自动添加新的 MCP 服务:

# User: "Add this Slack MCP server to tool gating"
# AI: Connects to server, discovers tools, and registers everything

POST /api/mcp/ai/register-server
{
  "server_name": "slack",
  "config": {
    "command": "npx",
    "args": ["@slack/mcp-server"],
    "env": {"SLACK_TOKEN": "xoxb-..."}
  },
  "tools": [
    // AI provides all discovered tools with metadata
  ]
}

# Result: Slack server + all tools registered and ready for use

🧑‍💻 开发

代码质量
# Format code
black .

# Run linter
ruff check . --fix

# Type checking
mypy .

# Run all checks
black . && ruff check . --fix && mypy . && pytest
添加新工具

可在 services/repository.py 中向代码库添加工具:

Tool(
    id="my-tool",
    name="My Tool",
    description="Description for semantic search",
    tags=["category", "function"],
    estimated_tokens=100,
    parameters={
        "type": "object",
        "properties": {...},
        "required": [...]
    }
)

🤝 参与贡献

  1. 复刻代码库
  2. 创建功能分支(git checkout -b feature/amazing-feature)
  3. 进行更改并添加测试
  4. 运行质量检查(black、ruff、mypy、pytest)
  5. 提交更改(git commit -m 'Add amazing feature')
  6. 推送到分支(git push origin feature/amazing-feature)
  7. 发起拉取请求

📄 许可证

本项目采用 MIT 许可证授权,详情请参阅 LICENSE 文件。

🙏 致谢

评论

支持 Markdown

第一次接触 MCP?

从协议是什么,到在客户端里装上第一个服务,这几条回答覆盖了大多数入门问题。

一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。