
4q Tokenz
io.github.steveweltman由 GitHub 帳號 steveweltman 發布v1.14.1更新於 Oct 24, 2026
Intelligent MCP proxy that reduces token usage by exposing only mcp_search and mcp_call.
概覽
一個 MCP 代理,介於助理與其他 MCP 伺服器之間,只暴露搜尋、呼叫與結構三個工具以減少 token 用量。
- 可以做什麼
- 它透過 stdio 或 HTTP 連接一個或多個上游 MCP 伺服器,在啟動時探索其工具,並對用戶端隱藏完整目錄。助理只會看到三個工具:mcp_search 以 BM25 詞彙搜尋依自然語言尋找工具,mcp_call 依參照呼叫工具,mcp_schema 檢視工具細節。它也會整形與截斷工具輸出、回收閒置的上游連線,並在本地儀表板連接埠提供 Prometheus 指標。
- 適用情境
- 當連接許多 MCP 伺服器、其工具結構合計佔用大量上下文視窗時值得加入。它鎖定工具密集的工作區,讓助理透過搜尋找到工具,而不是看到每個結構。
- 執行需求
- 以 Node.js 18+ 本地程序執行,可從 npm 安裝(4q-tokenz)或從原始碼建置。需要至少一個上游 MCP 伺服器,透過設定檔或 MCP_PROXY_UPSTREAMS 環境變數設定。上游憑證(例如 GOOGLE_TOKEN_FILE 或 API 金鑰環境變數)會傳遞給這些伺服器。僅支援桌面端。
安裝
在 SourceWeft 中
- 開啟 儀表板中的 4q Tokenz,將其新增到工作區。
- 為需要使用其工具的對話啟用該服務。
Desktop only,透過 STDIO。 STDIO 服務會啟動本機處理程序,因此需要 SourceWeft 桌面主機。
其他 MCP 客戶端
參照 儲存庫 中的啟動說明。
README
MCP Proxy Gateway
A context-aware MCP proxy that reduces token usage by exposing only 3 tools (mcp_search, mcp_call, mcp_schema) to LLMs instead of the full catalog.
Why This Exists
When you connect multiple MCP servers to an LLM, every tool from every server is listed in the LLM's context window. For a typical workspace with 50-100 tools across multiple MCP servers, that's thousands of tokens of schema documentation on every request.
MCP Proxy Gateway sits between your LLM and your MCP servers, offering:
- JIT tool loading — tools from upstream servers are discovered once at startup, then tools are called on-demand. Clients never see the full catalog.
- Intelligent search — fast lexical (BM25) search to find the right tool for a query. Tool tokens are pre-computed at startup for zero-overhead per-query scoring.
- Token savings — LLMs only see 3 tool schemas (search, call, schema) instead of 50+. Typical savings: 20-40% per turn for tool-heavy workflows.
- Zero native dependencies — pure JavaScript, no native modules, no model downloads, no supply chain risk from ML packages.
Prerequisites
- Node.js 18+ with npm or pnpm
- One or more MCP servers to proxy (stdio or HTTP)
Installation
From Source
As a Dependency
Getting Started: Google Workspace Example
Here's a concrete walkthrough to connect Google Workspace (Gmail, Calendar, Drive) to your LLM through the proxy:
Step 1: Choose or Build an MCP Server for Google
You need an MCP server that wraps Google APIs. Options:
-
@antidrift/mcp-google (recommended) — A collection of MCP server implementations for Google Workspace (Gmail, Calendar, Drive, Docs, Sheets). Works out of the box with this proxy.
-
@modelcontextprotocol/server-gmail — Gmail-only, official MCP server
-
Build your own — See the MCP spec to wrap your own APIs
Step 2: Set Up Google OAuth
- Go to Google Cloud Console
- Create a new project or select an existing one
- Enable these APIs:
- Gmail API
- Google Calendar API
- Google Drive API
- Create an OAuth 2.0 credential (type: Desktop application)
- Download the credential JSON
- Run the Google MCP server once to generate
token.json: This opens a browser for you to authorize. Once done, it savestoken.jsonlocally.
Step 3: Configure the Proxy
Create ~/.config/4q-tokens/config.json:
Step 4: Start the Proxy
Step 5: Connect Your LLM
Configure your LLM to use http://127.0.0.1:9200/mcp as its MCP server. It will see:
mcp_search— find tools by natural languagemcp_call— invoke a toolmcp_schema— see tool details
Example query:
Configuration
Quick Start with Environment Variables
Config File (Recommended)
Create ~/.config/4q-tokens/config.json:
Then run:
The proxy will load the config from ~/.config/4q-tokens/config.json if it exists, otherwise fall back to the MCP_PROXY_UPSTREAMS environment variable.
Configuration Reference
Upstream Server Config
Proxy Options
Environment variable overrides:
Multi-Agent Lanes (Per-Agent Isolation)
When several agents share one proxy, each agent should only be able to see and call its own tools. 4Q-Tokenz does this with lanes. A lane is a named view of the tool registry that is served at its own URL, /mcp/<agentId>. An agent connected to its lane cannot discover or call tools from any other lane, even if it knows the ref.
Defining lanes
Add an agents block next to upstreams in ~/.config/4q-tokens/config.json (or set MCP_PROXY_AGENTS as JSON). Each lane lists the upstream providers it may use and, optionally, individual tools to deny:
Agent ids must match ^[a-z0-9_][a-z0-9_-]{0,63}$. The plain array form ("hope": ["google-hope"]) is still accepted and means allow-only.
Fail-closed behavior
- The unscoped route
/mcpis refused with 403. A client that is not bound to a lane never sees the whole tool pool. For local development only, setMCP_PROXY_ALLOW_UNSCOPED=1to re-enable it. - An unknown lane (
/mcp/nobody) returns 404. - A stdio instance that would become the primary (and so serve the whole pool to one client) refuses to start unless
MCP_PROXY_ALLOW_UNSCOPED=1. Run the proxy as a service instead (see Singleton and Bridge Mode). - A bridge refuses to start without a valid
MCP_PROXY_AGENT.
Singleton and Bridge Mode
One primary instance owns the upstream connections and listens on MCP_PROXY_SINGLETON_PORT (default 9200). Run it as a service with MCP_PROXY_SERVICE_MODE=1 (HTTP only, no stdio). Any additional instance launched as a stdio MCP server detects the live primary through the lock file and becomes a bridge. A bridge forwards the client's stdio traffic to the primary on its own lane:
The lock file lives in MCP_PROXY_LOCK_DIR, else $XDG_RUNTIME_DIR, else /run/user/<uid>, else the temp dir. Hosts that launch the proxy with a minimal environment (for example the OpenClaw gateway) do not set XDG_RUNTIME_DIR, which is why the /run/user/<uid> fallback exists. If an instance wins a lock but finds the port already served, it releases the lock and bridges instead of crashing.
Wiring agents in OpenClaw
OpenClaw has no per-agent MCP setting and does not tell a stdio server which agent launched it. Isolation therefore needs two layers:
- One MCP server entry per agent, each passing its own lane through the environment. Use names that start with a letter, because OpenClaw exposes MCP tools as
<serverName>__<tool>:
- Each agent denies the other agents' entries using glob patterns in its tool policy, so it does not see them at all (deny always wins in OpenClaw's tool policy):
The proxy lane is the enforcing boundary: whatever an agent does, a bridge bound to hope can only reach /mcp/hope. The OpenClaw deny list is defense in depth that keeps one agent from being handed another agent's entry.
Verifying isolation
- Proxy log: every session is logged with its lane, for example
HTTP session created: ... [hope]. There should be no[open]sessions and noRefused unscopedlines in normal operation. - From each agent, run
mcp_searchand confirm only that lane's providers appear. - From an agent, call a ref from another lane. It must return
Tool not found.
Running
Standalone (Stdio Transport)
The proxy connects via stdio to your LLM. Use it with Claude or other MCP clients.
As a Systemd User Service
The install script can set this up for you (see below), or manually:
- Create
~/.config/systemd/user/mcp-proxy.service:
- Enable and start:
- View logs:
Prometheus Metrics (Port 9100)
The dashboard exposes a Prometheus-compatible /metrics endpoint:
Metrics exposed:
Add a scrape job in your Prometheus/Alloy config:
HTTP Server (Port 9200)
The proxy always starts an HTTP transport on port 9200 by default. Set MCP_PROXY_SINGLETON_PORT to use a different port. This allows multiple clients to connect to a single proxy instance. Clients connect to a lane at /mcp/<agentId> (see Multi-Agent Lanes). The unscoped /mcp route is refused unless MCP_PROXY_ALLOW_UNSCOPED=1 is set.
Troubleshooting
Upstream MCP Server Won't Connect
Check the server logs in the dashboard (port 9100 by default) or daemon logs:
The proxy logs:
- Tool discovery on startup
- Connection failures with error messages
- Upstream stderr (piped from stdio servers)
Proxy Crashes or Freezes
The proxy has comprehensive error handling to gracefully degrade on upstream failures:
- If an upstream tool call fails, the error is logged and returned to the client
- If all upstreams fail at discovery, startup fails with
NO_UPSTREAMS
For unhandled errors, check:
Tool Returns No Data
When a tool returns null or malformed data, the output shaper handles it gracefully:
- Null results return
[] - Strings are wrapped as
{value: string} - CSV is auto-parsed if it looks like tabular data
- Raw binary content (images, files) is preserved via
_rawContent
If a tool response looks truncated, retry with detail=true in mcp_call to disable output shaping:
Security & Networking
The proxy binds to 127.0.0.1 only for security — it's not accessible from the network by default. To access remotely:
- Same machine: Connect locally on
127.0.0.1:9200 - Remote access: Use Tailscale, SSH forwarding, or a VPN tunnel
- Systemd service: Access is local by default; no firewall rule needed
- Tenant isolation: see Multi-Agent Lanes. Lanes are enforced by the proxy, not by the agents, and the unscoped route fails closed
Known Limitations
- Tests are focused, not exhaustive:
pnpm testruns regression tests for the singleton lock, bridge startup, lane isolation and deny lists (Node's built-in test runner, no extra dependencies). Upstream connector behavior is not covered - Lane enforcement depends on how clients connect: a bridge or client is only isolated if it is bound to a lane. The proxy refuses unbound clients, but it cannot stop a client you deliberately point at another agent's lane
Changelog
Unreleased (next: v1.23.0)
Per-agent isolation is now enforced rather than optional:
- Breaking: the unscoped
/mcproute is refused with 403 unlessMCP_PROXY_ALLOW_UNSCOPED=1. Clients must use/mcp/<agentId> - Breaking: a bridge reads
MCP_PROXY_AGENT, connects to its lane, and refuses to start without a valid agent id. A non-service stdio primary refuses to start unlessMCP_PROXY_ALLOW_UNSCOPED=1 - New per-lane
denylist (provider.toolor*.tool) in theagentsconfig, with a startup warning for entries that match no tool - README: new Multi-Agent Lanes section covering lane config, fail-closed behavior, singleton and bridge mode, and OpenClaw wiring
- Tests added for lane isolation, fail-closed paths and deny semantics
v1.22.3
@modelcontextprotocol/sdk1.31.0 to 1.32.1, clearing allpnpm audit --prodfindings (includingproxy-addrvia express)
v1.22.2
- Version bump so the build with the singleton lock fix is distinguishable
Singleton lock directory fix (between v1.22.1 and v1.22.2)
- Fixed: a proxy launched with a minimal environment (no
XDG_RUNTIME_DIR, for example by the OpenClaw gateway) looked for its lock in the temp dir, did not find the running primary, tried to become a second primary and crashed withEADDRINUSE, which the client saw asMCP error -32000: Connection closed. The lock directory now falls back to/run/user/<uid>, and an instance that wins a lock but finds the port served bridges instead of crashing - Added regression tests (
pnpm test)
v1.22.1
Security hardening from the pre-release assessment (OWASP Top 10 2025, OSV.dev, GCVE):
- Dashboard: removed
Access-Control-Allow-Origin: *from/api/status, and stopped returning tool-callargsin the audit log data — previously any website could read recent call arguments from127.0.0.1:9100 - New
src/host-guard.ts:/mcpand the dashboard reject requests whoseHostorOriginis not loopback (403), blocking DNS-rebinding and cross-site browser access - Singleton lock file now lives in
$XDG_RUNTIME_DIRwhen set (falls back to the temp dir) and is created with mode 0600 - OSV.dev: 0 advisories across all 93 installed packages;
pnpm auditclean
v1.22.0
- Major dependency upgrades:
zod3.25 → 4.6,typescript6.0 → 7.0,@types/node25 → 26 - zod 4 migration:
z.recordcalls now pass an explicit key schema (src/types.ts) - Bump
@modelcontextprotocol/sdkfrom^1.30.0to^1.31.0 - Tighten pnpm overrides:
hono >=4.13.5,fast-uri >=4.1.4,ip-address >=10.5.1; addqs >=6.16.0— clears 2 high and 7 moderate advisories (all transitive via the MCP SDK) - Verified clean against
pnpm audit
v1.21.2
- Fix incomplete v1.21.1 patch: the
honopnpm override (>=4.12.25) still resolved to a vulnerable 4.12.x release; tightened to>=4.12.34 - Add pnpm overrides for
@hono/node-server >=1.19.15,fast-uri >=3.1.5,ip-address >=10.3.1,body-parser >=2.3.0— all transitive via@modelcontextprotocol/sdk, resolves 3 high and 6 medium/low CVEs (DoS, ReDoS, SSRF-adjacent parsing bugs) - Bump
@modelcontextprotocol/sdkfrom^1.26.0to^1.30.0 - Verified clean against
pnpm auditand OSV.dev for all resolved dependency versions
v1.21.1
- Attempted hono dependency patch (
pnpm override: hono >=4.12.25) — override was too loose and did not actually clear the vulnerable range; fully resolved in v1.21.2 above
v1.21.0
detail=trueno longer truncates text fields in tool output
v1.20.0
- Per-agent lane isolation: URL routing at
/mcp/{agentId}, filtered tool registry views,MCP_PROXY_AGENTSconfig
v1.19.0
- Add Prometheus
/metricsendpoint on the dashboard port (9100 by default) - Exposes:
mcp_proxy_uptime_seconds,mcp_proxy_registered_tools,mcp_proxy_upstream_up,mcp_proxy_upstream_tools,mcp_proxy_calls_total,mcp_proxy_call_duration_ms_total,mcp_proxy_output_bytes_total - Counters persist for the lifetime of the process; scrape with Prometheus/Alloy and visualize in Grafana
v1.18.0
- Drop
@xenova/transformersentirely — eliminates the protobufjs/ONNX runtime supply chain - Switch to pure lexical (BM25) search; tool tokens pre-computed at startup, zero per-query overhead
- No change to mcp_search quality for English-language tool catalogs
- Removes ~90MB model download on first run and all native module requirements
v1.17.1
- Pre-compute tool token sets at registry build time; lexical scoring now reads the cache instead of re-tokenizing on every query
- Add LRU cache for query embeddings (embeddings removed entirely in v1.18.0)
v1.17.0
- Switch to lighter English-optimized embedding model, reducing cold-start download by ~380MB (embeddings removed entirely in v1.18.0)
v1.16.0
- Update
@modelcontextprotocol/sdkfrom~1.22.0to^1.26.0— resolves 3 high-severity CVEs: ReDoS, cross-client data leak, DNS rebinding - Add pnpm override:
protobufjs >=7.5.8— resolves critical arbitrary code execution and multiple high CVEs in@xenova/transformerstransitive dependency chain - Add pnpm override:
qs >=6.15.2— resolves moderate DoS vulnerability inexpresstransitive dependency
Attribution
MCP Proxy Gateway is a fork of @arvoretech/mcp-proxy, originally created by João Augusto and Árvore Educação.
Forked and extended with:
- Singleton mode for HTTP bridge
- Idle server reaping
- Comprehensive error handling
- Config file support
- Systemd integration
License
MIT. See LICENSE.
來源:README.md,提交 38fb5cd
工具
0版本歷史
1- v1.14.1最新Sep 16, 2026
評分
已滿足適用檢查項的 50%
該評分衡量此收錄條目的完整程度,依據註冊中心條目、端點與公開來源計算得出。它不是對伺服器本身的評價,也不表示伺服器安全或目前可用:可連通性是帶日期的檢查結果,評分也不包含安裝量或使用者評價。
- 近 30 天內可連通25 分
請保持端點能夠回應 MCP 握手;SourceWeft 每 30 天探測一次。
- 已宣告認證方式10 分
請在註冊中心條目中宣告憑證請求標頭,或保持端點能夠回應探測,以便判定其認證方式。
- 已列出工具15 分
請讓伺服器在無憑證時回應 tools/list,或發布可在沙箱中無憑證啟動的軟體包。
- 描述必需15 分
- 圖示10 分
- 文件15 分
- 授權條款10 分