Overview
MCP configuration can quietly expand what a Claude session can see. A project
.mcp.json, desktop config, or managed MCP policy can add servers with
credentials, remote endpoints, and filesystem paths. That is useful when
intentional, but risky when a generated edit saves literal tokens or hands a
filesystem server a broad root.
This PreToolUse hook runs before Claude Code writes MCP-related configuration.
It scans the proposed content locally and blocks three concrete privacy risks:
inline credential values in env or headers, URLs that carry credential query
parameters, and broad filesystem roots for filesystem MCP servers.
Features
- Watches
Write, Edit, and MultiEdit calls for .mcp.json,
managed-mcp.json, mcp.json, claude_desktop_config.json, and content
that contains an mcpServers object.
- Parses complete JSON when available, then falls back to text heuristics for
partial edits.
- Flags literal credential-like values in MCP
env or headers objects while
allowing environment-variable references such as ${MCP_TOKEN}.
- Flags credential-bearing remote URLs that include token-like query
parameters.
- Flags filesystem MCP server configs that expose broad roots such as
/,
~, /home, /Users, /root, or a Windows user root.
- Reports finding categories only, without echoing the secret value, URL, or
full config.
How It Works
Claude Code sends the pending tool call to the hook on stdin. The script
extracts the tool name, target path, and new text from the Write/Edit/MultiEdit
payload. It scans only MCP-looking targets: known MCP config filenames or
content containing an mcpServers key.
When the proposed text is valid JSON, the hook uses jq to inspect env,
headers, string URLs, and filesystem server arguments. For partial edits, it
uses conservative grep patterns so a changed line can still be caught before it
is saved. If any risky pattern is found, the hook exits 2, which blocks the
write and returns the reason to Claude Code.
Use Cases
- Stop project-scoped
.mcp.json changes from committing literal service
tokens.
- Review remote MCP server URLs before a bearer token, API key, or password is
embedded in a query string.
- Keep generated filesystem server configs from exposing an entire home
directory or root filesystem.
- Run the guard in advisory mode while a team inventories approved MCP servers
and credential handling.
Installation
- Create a user-owned hooks directory:
mkdir -p "$HOME/.claude/hooks"
- Create the hook file:
touch "$HOME/.claude/hooks/mcp-config-privacy-scanner.sh"
- Paste the script body into that file and make it executable:
chmod +x "$HOME/.claude/hooks/mcp-config-privacy-scanner.sh"
- Add the configuration below to
~/.claude/settings.json for a user hook.
If you instead store this hook inside a project's .claude/hooks directory,
keep the matching $CLAUDE_PROJECT_DIR/.claude/hooks/... command only in that
reviewed project's .claude/settings.json; do not copy a project-relative
command into user-level settings.
Hook Configuration
{
"hooks": {
"PreToolUse": [
{
"matcher": "Write|Edit|MultiEdit",
"hooks": [
{
"type": "command",
"command": "$HOME/.claude/hooks/mcp-config-privacy-scanner.sh"
}
]
}
]
}
}
Script
#!/usr/bin/env bash
# Paste the scriptBody from this entry into:
# ~/.claude/hooks/mcp-config-privacy-scanner.sh
Configuration Options
MCP_CONFIG_PRIVACY_MODE=advisory prints warnings but exits 0.
MCP_CONFIG_PRIVACY_ALLOWLIST is an extended grep pattern checked against the
target path and proposed content. Use it only for reviewed local exceptions.
Expected Behavior
- Allowed: MCP config that references credentials with environment-variable
expansion.
- Blocked: MCP config that places a literal credential-like value in an
env or headers object.
- Blocked: Remote MCP URL strings that include credential-style query
parameters.
- Blocked: Filesystem MCP server arguments that expose an entire home or
root directory.
Limitations
- The hook is a local pre-write scanner. It does not prove an MCP server is
safe, authenticated correctly, or least-privilege at runtime.
- Regex and JSON heuristics can miss obfuscated secrets or unusual config
layouts.
- Partial edits may not contain enough JSON context to identify every server
type.
- A project can still add risky MCP config through another editor unless the
same policy is enforced in review or CI.
Troubleshooting
The hook does not run
Confirm the file is executable and that the hook config is in the active Claude
Code settings file. Then run /hooks or the relevant Claude Code config
diagnostic command to confirm the PreToolUse hook is loaded.
A placeholder was blocked
Use environment-variable expansion for credential values or set advisory mode
while tuning the policy. Avoid realistic-looking token placeholders in shared
MCP config.
A reviewed filesystem server was blocked
Narrow the path to the minimum directory the MCP server needs. If a broad path
is truly required, document the reason and add a local allowlist pattern.
Duplicate Check
Checked content/hooks/ for MCP config, mcpServers, privacy scanner,
environment variable leak, secret scanner, and prompt injection. Existing
hook content includes a pre-write secret scanner and package/download guards,
but no existing hook focuses on MCP configuration privacy, credential-bearing
MCP URLs, and filesystem MCP root exposure before config writes.
Sources