Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

mcp-fence

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mcp-fence

The bidirectional firewall for MCP — scans inputs AND outputs, detects rug-pulls at runtime, zero config.

latest
Source
npmnpm
Version
1.0.2
Version published
Weekly downloads
10
-33.33%
Maintainers
1
Weekly downloads
 
Created
Source

mcp-fence

A security proxy for MCP servers — sits between your AI agent and MCP server, scanning both requests AND responses for prompt injection, secret leaks, and tool tampering.

npm version license node OWASP MCP

Why mcp-fence?

MCP servers return data that AI agents trust blindly. A compromised server can embed hidden instructions in its responses, leak secrets through tool outputs, or silently change what a tool does after you've approved it.

Most MCP security tools only scan the input side. mcp-fence scans both.

  • Bidirectional scanning — catches threats hiding in server responses, not just requests
  • Rug-pull detection — pins tool descriptions by hash. If a server silently changes what a tool does, mcp-fence flags it immediately
  • Zero config — works out of the box in monitor mode. Logs threats without blocking, so you never break a working setup

Who needs this?

You probably do if:

  • You connect to MCP servers you don't fully control
  • You let AI agents read files, call tools, or access secrets
  • You assume server responses are "just data"

Quick Start

1. Try it now

npx mcp-fence start -- npx @modelcontextprotocol/server-filesystem /tmp

That's it. mcp-fence sits between your client and server, scanning every message in real time. Default is monitor mode — logs findings without blocking, so nothing breaks.

mcp-fence proxy — real-time threat detection

2. Claude Desktop

Add mcp-fence as a wrapper in your claude_desktop_config.json:

{
  "mcpServers": {
    "filesystem": {
      "command": "npx",
      "args": [
        "mcp-fence",
        "start",
        "--mode", "enforce",
        "--",
        "npx", "@modelcontextprotocol/server-filesystem", "/tmp"
      ]
    }
  }
}

Your MCP server works exactly as before. mcp-fence just inspects traffic passing through.

3. SSE / Streamable HTTP

# Proxy a remote MCP server over SSE
mcp-fence start --transport sse --upstream http://localhost:8080 --port 3000

# Streamable HTTP with JWT authentication
MCP_FENCE_JWT_SECRET=my-secret mcp-fence start \
  --transport http --upstream http://localhost:8080 --port 3000

4. Standalone scan (no proxy needed)

# Scan a file
mcp-fence scan ./suspicious-prompt.txt

# Scan inline text
mcp-fence scan --text "ignore all previous instructions"

# Scan as a server response
mcp-fence scan ./response.json --direction response

scan — injection detection with remediation guidance

PII detection — Korean resident ID, phone number, credit card

Architecture

                        mcp-fence
                  ┌─────────────────────┐
[MCP Client] ──> │  1. Detection Engine │ ──> [MCP Server]
          stdio / │  2. Hash Pin Check   │ stdio / SSE /
          SSE /   │  3. Policy Engine    │ Streamable HTTP
          HTTP    │  4. Context Budget   │
[MCP Client] <── │  5. Audit Logger     │ <── [MCP Server]
                  └─────────────────────┘
                           │
                     [SQLite Audit DB]

Every message flows through the same pipeline:

  • Intercept — Proxy captures the JSON-RPC message (request or response)
  • Detect — Injection, secret, PII, and command-injection patterns
  • Pin check — For tools/list responses, flags any description or schema changes
  • Policy — Tool-level allow/deny rules, argument constraints, OPA decisions
  • Context budget — Response size limits (warn/truncate/block)
  • Audit — Every result logged to SQLite with HMAC integrity chain
  • Forward or block — Monitor mode passes everything; enforce mode rejects threats

Modules are decoupled: detection doesn't import policy, audit doesn't import detection. The proxy orchestrates through the ScanResult contract.

Features

Detection

CategoryPatternsExamples
Prompt injection13Instruction override, role hijacking, hidden instructions, multilingual injection (10 languages)
Command injection6Shell metacharacters, dangerous commands, sensitive file access
Data exfiltration6URL exfil, DNS exfil, encoded exfil
Secret detection24AWS, GitHub, Slack, Stripe, OpenAI, JWT, private keys, connection strings
PII detection7Email, phone, SSN, credit card, IPv4, Korean resident ID, Korean phone

Security infrastructure

FeatureDescription
Rug-pull detectionSHA-256 hash pinning of tool descriptions, persisted to SQLite
Server schema pinningTOFU-based pinning. Detects tool addition, removal, and schema drift
Context budgetConfigurable max response size with warn/truncate/block actions
Policy engineTool-level allow/deny with glob patterns and argument validation
OPA integrationExternal policy via Open Policy Agent with SSRF protection and fail-closed
Data flow policiesCross-server session-level tool call tracking (e.g. deny read_file → send_email)
JWT authenticationHS256, RS256, JWKS rotation for SSE/HTTP transports
Audit loggingSQLite with secret masking, HMAC-SHA256 tamper detection, auto-pruning
SARIF outputExport findings for GitHub Security tab integration
Remediation guidanceEvery finding includes actionable fix advice

Limitations

  • Detection is regex-based. Known patterns are caught, but novel injection via paraphrase or synonyms will pass through. ML-based semantic detection is planned for v1.x.
  • TOFU pinning trusts on first observation. If the first contact is already compromised, it won't be detected.
  • MCP09 (Supply Chain) is only partially covered — runtime behavior inspection catches post-compromise activity, but there's no package-level verification.
  • Large responses are scanned at the head and tail only. Content in the middle of very large messages may not be inspected.

Full threat model: THREAT_MODEL.md

OWASP MCP Top 10 Coverage

IDRiskv1.0How
MCP01Token/Secret ExposureYesSecret pattern detection + audit log masking
MCP02Tool PoisoningYesTool description hash pinning (rug-pull detection)
MCP03Excessive PermissionsYesPolicy engine with tool allow/deny + argument constraints
MCP04Command InjectionYesCommand injection patterns in detection engine
MCP05Insecure Data HandlingYesSecret masking, HMAC integrity chain, DB size limits
MCP06Insufficient LoggingYesSQLite audit log + SARIF export + HMAC tamper detection
MCP07Insufficient AuthYesJWT authentication (HS256/RS256/JWKS) for HTTP transports
MCP08Server SpoofingYesServer schema TOFU pinning (SRV-001/002/003)
MCP09Supply Chain CompromisePartialRuntime behavior inspection; no package-level verification
MCP10Context InjectionYesContext budget + bidirectional injection scanning

CVE coverage: Tested against 44 known MCP vulnerabilities (13 specific CVEs) across 86 attack scenarios with 86% detection rate. Remaining 14% are server-implementation flaws outside proxy scope.

Or install globally:

npm install -g mcp-fence

Configuration

Generate a config file:

mcp-fence init

This creates fence.config.yaml:

# Operation mode: "monitor" (log only) or "enforce" (block threats)
mode: monitor

log:
  level: info

detection:
  warnThreshold: 0.5
  blockThreshold: 0.8
  maxInputSize: 10240

policy:
  defaultAction: allow
  rules:
    - tool: "exec_cmd"
      action: deny
    - tool: "read_file"
      action: allow
      args:
        - name: path
          denyPattern: "^\\.env$|^/etc/"
    - tool: "write_*"
      action: deny

# jwt:
#   enabled: true
#   audience: "mcp-fence"
#   issuer: "my-auth-server"

# dataFlow:
#   enabled: true
#   rules:
#     - from: "read_file"
#       to: "send_email"
#       action: deny

# contextBudget:
#   enabled: true
#   maxResponseBytes: 102400
#   truncateAction: warn

Config priority: CLI flags > environment variables > YAML file > defaults.

VariableValuesDescription
MCP_FENCE_MODEmonitor, enforceOperation mode
MCP_FENCE_LOG_LEVELdebug, info, warn, errorLog verbosity
MCP_FENCE_JWT_SECRETstringShared secret for HS256 JWT

CLI Reference

start — Run the security proxy

mcp-fence start -- npx @modelcontextprotocol/server-filesystem /tmp
mcp-fence start --mode enforce --config ./fence.config.yaml -- node my-server.js
mcp-fence start --transport sse --upstream http://localhost:8080 --port 3000
FlagDefaultDescription
-c, --config <path>Config file path
-m, --mode <mode>monitormonitor or enforce
--log-level <level>infodebug, info, warn, error
-t, --transport <type>stdiostdio, sse, http
-p, --port <port>3000Listen port (SSE/HTTP)
-u, --upstream <url>Upstream server URL (SSE/HTTP)
--jwks-url <url>JWKS endpoint for RS256 key rotation

scan — Standalone threat scan

mcp-fence scan ./file.txt
mcp-fence scan --text "ignore all previous instructions"
mcp-fence scan ./response.json --direction response
mcp-fence scan ./file.txt --format sarif > results.sarif

logs — Query the audit trail

mcp-fence logs --since 1h --level warn
mcp-fence logs --format sarif > audit.sarif
mcp-fence logs --direction response --limit 50

audit logs table output

verify — Check audit log integrity

mcp-fence verify

verify chain integrity

init — Generate default config

mcp-fence init
mcp-fence init --output ./custom-config.yaml

status — Show config and capabilities

mcp-fence status

status — detection patterns and config

Programmatic Usage

mcp-fence exports its detection engine, policy engine, and audit logger as importable modules. See the source code in src/ for available APIs.

Roadmap

VersionFocusStatus
v0.1stdio proxy, bidirectional scanning, secret detection, hash pinning, policy engine, audit, CLIDone
v0.2Audit hardening (masking, HMAC, pruning, verify), Unicode normalizationDone
v0.3SSE + HTTP transport, JWT auth, OPA integration, data flow policiesDone
v0.4Server schema TOFU pinning, context budget, SQLite-persisted hash pinsDone
v1.0PII detection, remediation guidance, 9 security hardening fixesCurrent
v1.xML-based semantic detection, session-level multi-step analysisPlanned

Contributing

Contributions welcome. Please open an issue before submitting large changes.

git clone https://github.com/yjcho9317/mcp-fence.git
cd mcp-fence
npm install
npm test
npm run typecheck
npm run lint

Security vulnerabilities: please report via THREAT_MODEL.md. Do not open public issues for security bugs.

Security-critical modules (src/detection/, src/integrity/, src/policy/local.ts) require manual review on every PR. No exceptions.

License

MIT

Keywords

mcp

FAQs

Package last updated on 08 Apr 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts