How to Add a Fail-Closed Permission Gate to LM Studio and Ollama MCP Tools
Local 8B–32B models misfire tool arguments. Wrap LM Studio and Ollama MCP servers with a fail-closed proxy: argument rules, headless enforcement, zero-tunnel Telegram/Slack approvals and a hash-chained SQLite log.
Running tools against a local model is now an ordinary setup: LM Studio reads ~/.lmstudio/mcp.json, and Ollama-backed clients such as ollmcp, Cline, Continue and Claude Code (via ollama launch claude) speak MCP to the same servers you use with Claude Desktop. The model is cheaper and private. The tools are still real: they write files, open pull requests, query databases and move money.
This guide puts a fail-closed permission gate between any of those clients and their MCP servers. It is a small proxy that checks every tools/call against your rules before the server sees it. It works the same for an 8B model on a laptop as for a frontier model, because it wraps the MCP server, not the LLM.
If you only want the config, the MCP Firewall Config Generator builds it in your browser. The rest of this post explains what it does and why.
Why smaller local models need argument-level rules
Frontier models misfire tools too. Local 8B–32B models misfire them more often, and in predictable ways:
| Failure mode | What it looks like in a tools/call |
|---|---|
| Wrong path root | "path": "/etc/hosts" when the project is in /Users/you/projects |
| Traversal by string concatenation | "path": "/Users/you/projects/../.ssh/id_rsa" |
| Type drift | "amount": "250" (string) instead of 250 |
| Missing required argument | create_refund with no amount, which Stripe treats as a full refund |
| Read/write confusion | write_file with empty content when the user asked to read the file |
| Retry loops | the same create_issue five times because the result was not parsed |
None of these needs a malicious prompt. A quantized model with a 4K effective context window that has just read a long directory listing will do some of them by accident. Add prompt injection (a README, web page or issue body that tells the model what to do) and they become deliberate.
The fix that holds up is the one from network security: default deny, explicit allow, and checks on arguments, not just tool names.
Where LM Studio's confirmation stops
LM Studio asks before each tool call in the chat UI, and you can allow a tool for the rest of the session. That is useful, and it is the right default for interactive use. It is a UI feature, though, and it has three gaps:
- It has no argument policy. "Allow
write_file" allows every path. It cannot say "writes inside~/projects/scratchare fine, everything else asks me, and..is never allowed." - It only exists in the chat window. If a script, an agent framework or another app calls the model through
lms server startand runs the tools itself, no popup appears. The same applies to any Ollama-backed agent loop. - It needs you at the screen. A long-running local agent that pauses on a popup while you are away just sits there. Worse, someone gets used to clicking Allow.
A proxy at the MCP server boundary closes all three: rules run on the arguments, enforcement does not depend on which UI started the process, and approvals can reach your phone.
Architecture
LM Studio · ollmcp · Cline · Continue · Claude Code (ollama launch claude)
│ stdio JSON-RPC (tools/list, tools/call)
▼
agent-gateway-sdk mcp-stdio rules.json → ALLOW / REQUIRE_APPROVAL / BLOCK
│ held calls → dashboard · Telegram · Slack
│ every call → SHA-256 hash-chained SQLite row
▼ (only if allowed or approved)
the real MCP server (filesystem, GitHub, Postgres, Stripe, fetch, …)The client starts npx -y agent-gateway-sdk mcp-stdio … -- <your server command> instead of the server directly. Mont starts the real server as a child process and relays everything. Each tools/call is checked first: its rule key is MCP /api/mcp/tools/<tool name> and its arguments are visible to policies as body.*. A refused call returns a normal MCP tool result with isError: true and a sentence explaining why, so the model can recover instead of crashing the session.
Setup for LM Studio
1. Create a data directory outside any folder the model can write to. Mont keeps rules.json, settings and the audit database there. If the filesystem server can see that folder, the model could edit its own rules.
mkdir -p /Users/you/.mont2. Add the wrapped server. In LM Studio, open the Program tab in the right sidebar → Install → Edit mcp.json. The file lives at ~/.lmstudio/mcp.json:
{
"mcpServers": {
"filesystem-guarded": {
"command": "npx",
"args": [
"-y", "agent-gateway-sdk", "mcp-stdio",
"--data-dir", "/Users/you/.mont",
"--",
"npx", "-y", "@modelcontextprotocol/server-filesystem", "/Users/you/projects"
]
}
}
}Use absolute paths. MCP clients start servers without a shell, so ~ in args reaches the program as a literal ~. On Windows, use "command": "cmd" and put "/c", "npx", … at the front of args.
3. Write the rules to /Users/you/.mont/rules.json:
{
"MCP /api/mcp/tools/list_directory": { "mode": "ALLOW" },
"MCP /api/mcp/tools/search_files": { "mode": "ALLOW" },
"MCP /api/mcp/tools/read_text_file": {
"mode": "BLOCK",
"policies": [
{ "name": "no traversal", "when": [{ "field": "body.path", "op": "contains", "value": ".." }], "mode": "BLOCK" },
{ "name": "no secrets", "when": [{ "field": "body.path", "op": "regex", "value": "(^|/)(\\.env|\\.ssh|\\.aws)", "flags": "i" }], "mode": "BLOCK" },
{ "name": "inside projects", "when": [{ "field": "body.path", "op": "regex", "value": "^/Users/you/projects(?:/|$)" }], "mode": "ALLOW" }
]
},
"MCP /api/mcp/tools/write_file": {
"mode": "REQUIRE_APPROVAL",
"policies": [{ "name": "no traversal", "when": [{ "field": "body.path", "op": "contains", "value": ".." }], "mode": "BLOCK" }]
},
"MCP /api/mcp/tools/*delete*": { "mode": "BLOCK" }
}Read it the way Mont does. Tiers run top to bottom and the first match wins. If no tier matches, the rule's own mode applies. If no rule matches the tool, it is blocked. So read_text_file is denied unless the path is inside projects, contains no .. and is not a secret file. The (?:/|$) boundary stops /Users/you/projects-old from matching /Users/you/projects.
4. Restart the server in LM Studio and test it. The output below is from an end-to-end run of exactly this wrapper around @modelcontextprotocol/server-filesystem, with a 5-second approval timeout:
read notes.md OK "hello from notes"
read .env REFUSED Blocked by the Mont gateway: Tool "read_text_file" was blocked by a gateway policy.
write (approval, 5s timeout) REFUSED This tool call needed human approval and none was given in time (approval #1). It was not executed.The ready-made version of this setup (macOS/Linux and Windows mcp.json plus rules) is in examples/lm-studio-filesystem-guard.
Setup for Ollama
Ollama serves the model. MCP is handled by whichever client is driving it, and every MCP client accepts the same wrapped command.
ollmcp (a terminal MCP client for Ollama) reads a standard mcpServers file:
ollama pull qwen3:8b
ollmcp --servers-json servers.json --model qwen3:8bClaude Code on a local model. ollama launch claude starts Claude Code against Ollama. Register the wrapped server once:
claude mcp add notes-guarded -- npx -y agent-gateway-sdk mcp-stdio --data-dir /Users/you/.mont \
-- npx -y @modelcontextprotocol/server-filesystem /Users/you/notes
ollama launch claudeCline and Continue take the same mcpServers entry in their MCP settings. Open WebUI reaches MCP servers over HTTP, so put mcpo in front of the wrapped command, or run Mont as mcp-http --upstream <url> against an HTTP MCP server. For a custom Python or Node tool-calling loop, spawn the wrapped command as your stdio MCP server. Mont sees the calls the same way.
examples/ollama-mcp-telegram-approval has a one-command setup (npm run setup) that saves the Telegram settings and writes servers.generated.json with absolute paths for your machine.
Pick a model that handles tool calls well (Qwen3 8B/14B, Qwen2.5 14B+, Llama 3.1 8B, Mistral Small). The gate catches bad calls, but a model that makes fewer of them means fewer approvals.
Approvals when you are not at the screen
REQUIRE_APPROVAL holds the call open. The client sees a slow tool, not an error, until a human decides or --wait-seconds (default 300) runs out. You can decide in three places:
- the local dashboard at
http://127.0.0.1:3000/agent-admin/ - Telegram: inline Approve / Reject buttons. Mont long-polls the Bot API over an outbound connection.
- Slack: buttons via Socket Mode over an outbound WebSocket.
Neither phone channel needs a public URL, ngrok or an open port, so it works on a laptop behind NAT. The bot token lives in Mont's SQLite store, not in any MCP server's environment and not in the model's context, so a prompt-injected model cannot use your Telegram bot to send data out. The free Community edition runs one channel. Pro runs both and adds Edit & Approve: fix a hallucinated argument ("path": "/Users/you/projects/README.md" instead of /README.md) and approve in the same tap.
For the full Slack and Telegram setup, see how to add Slack & Telegram approvals to MCP servers in Claude Desktop and Cursor. The setup is the same for local clients.
Headless and API-server use
Mont's rules do not depend on any client UI, so the same rules.json enforces in places a desktop popup can't reach:
- an agent framework that drives LM Studio's local server (
lms server start) and runs MCP tools itself - an Ollama-backed cron job that summarises an inbox and files issues
- a CI job that lets a local model edit code in a scratch branch
Point several of these at the same --data-dir and they share one rule set, one approval queue and one audit trail. Spending budgets and duplicate-action protection are stored in SQLite, so the limits hold across processes.
What lands in the audit log
Every call, whether allowed, held, approved, rejected or blocked, is written to logs.db before it is forwarded. Each row's hash covers the previous row's hash plus the redacted request and response. An edit, deletion or truncation breaks the chain at that point. Secrets in arguments (API keys, tokens, card numbers) are scrubbed before they are stored. For a local setup this answers the question you will eventually ask: what did the model actually do last Tuesday?
Trade-offs
- The proxy only sees MCP. If the model also has a shell tool, it can bypass the filesystem server with
cat. Gate the shell tool (or don't expose one), and use an OS sandbox for blast radius. The proxy and the sandbox solve different problems; see why voluntary ask_human tools fail under prompt injection. - Tool names vary by server version. The reference filesystem server has added and renamed tools over time (
read_file→read_text_file, newappend_fileandcopy_file). Default deny makes that safe, because a new tool is blocked until you allow it. Check Audit Logs → unknown endpoints after an upgrade. - Regexes run on strings only. Array arguments such as
read_multiple_files'pathscan't be checked item by item, so give themREQUIRE_APPROVALinstead ofALLOW. - Approval fatigue is real. Allow reads and safe writes inside a scratch folder, and save approvals for the calls that matter.
Next step
Open the MCP Firewall Config Generator, choose LM Studio or Ollama / Cline, pick a preset or paste your existing mcpServers block, and copy the three files. The whole setup runs locally with no account, and Community is free.