mcp-shield-proxy 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mcp_shield/__init__.py +4 -0
- mcp_shield/auditor.py +213 -0
- mcp_shield/cli.py +137 -0
- mcp_shield/config.py +41 -0
- mcp_shield/db.py +382 -0
- mcp_shield/deception.py +225 -0
- mcp_shield/engine.py +273 -0
- mcp_shield/policy.json +7 -0
- mcp_shield/proxy.py +299 -0
- mcp_shield/routes_b.py +118 -0
- mcp_shield/sandbox.py +107 -0
- mcp_shield/server.py +419 -0
- mcp_shield/shadow.py +230 -0
- mcp_shield/static/assets/index-BRgR9y6F.css +1 -0
- mcp_shield/static/assets/index-gDjSoeTZ.js +89 -0
- mcp_shield/static/benchmark_results.json +47 -0
- mcp_shield/static/index.html +14 -0
- mcp_shield_proxy-0.1.0.dist-info/METADATA +82 -0
- mcp_shield_proxy-0.1.0.dist-info/RECORD +22 -0
- mcp_shield_proxy-0.1.0.dist-info/WHEEL +4 -0
- mcp_shield_proxy-0.1.0.dist-info/entry_points.txt +3 -0
- mcp_shield_proxy-0.1.0.dist-info/licenses/LICENSE +21 -0
mcp_shield/__init__.py
ADDED
mcp_shield/auditor.py
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
"""Lethal Trifecta MCP configuration auditor and mcp-shield proxy wrapper."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import re
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
_UNTRUSTED_KEYWORDS: tuple[str, ...] = (
|
|
10
|
+
"github",
|
|
11
|
+
"gitlab",
|
|
12
|
+
"issue",
|
|
13
|
+
"jira",
|
|
14
|
+
"linear",
|
|
15
|
+
"slack",
|
|
16
|
+
"discord",
|
|
17
|
+
"reddit",
|
|
18
|
+
"email",
|
|
19
|
+
"gmail",
|
|
20
|
+
"outlook",
|
|
21
|
+
"web",
|
|
22
|
+
"fetch",
|
|
23
|
+
"puppeteer",
|
|
24
|
+
"playwright",
|
|
25
|
+
"browser",
|
|
26
|
+
"rss",
|
|
27
|
+
"confluence",
|
|
28
|
+
"search",
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
_PRIVATE_KEYWORDS: tuple[str, ...] = (
|
|
32
|
+
"filesystem",
|
|
33
|
+
"workspace",
|
|
34
|
+
"file",
|
|
35
|
+
"files",
|
|
36
|
+
"repo",
|
|
37
|
+
"git",
|
|
38
|
+
"memory",
|
|
39
|
+
"notion",
|
|
40
|
+
"postgres",
|
|
41
|
+
"postgresql",
|
|
42
|
+
"sqlite",
|
|
43
|
+
"mysql",
|
|
44
|
+
"mongo",
|
|
45
|
+
"mongodb",
|
|
46
|
+
"redis",
|
|
47
|
+
"db",
|
|
48
|
+
"database",
|
|
49
|
+
"env",
|
|
50
|
+
"secret",
|
|
51
|
+
"secrets",
|
|
52
|
+
"vault",
|
|
53
|
+
"1password",
|
|
54
|
+
"aws",
|
|
55
|
+
"gcp",
|
|
56
|
+
"azure",
|
|
57
|
+
"ssh",
|
|
58
|
+
"drive",
|
|
59
|
+
"gdrive",
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
_EGRESS_KEYWORDS: tuple[str, ...] = (
|
|
63
|
+
"shell",
|
|
64
|
+
"terminal",
|
|
65
|
+
"bash",
|
|
66
|
+
"exec",
|
|
67
|
+
"command",
|
|
68
|
+
"run",
|
|
69
|
+
"github",
|
|
70
|
+
"gitlab",
|
|
71
|
+
"slack",
|
|
72
|
+
"discord",
|
|
73
|
+
"email",
|
|
74
|
+
"gmail",
|
|
75
|
+
"smtp",
|
|
76
|
+
"http",
|
|
77
|
+
"fetch",
|
|
78
|
+
"curl",
|
|
79
|
+
"webhook",
|
|
80
|
+
"post",
|
|
81
|
+
"push",
|
|
82
|
+
"docker",
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _extract_servers(config_json: dict[str, Any]) -> tuple[str, dict[str, dict[str, Any]]]:
|
|
87
|
+
for key in ("mcpServers", "servers"):
|
|
88
|
+
candidate = config_json.get(key)
|
|
89
|
+
if isinstance(candidate, dict):
|
|
90
|
+
return (
|
|
91
|
+
key,
|
|
92
|
+
{str(k): v for k, v in candidate.items() if isinstance(v, dict)},
|
|
93
|
+
)
|
|
94
|
+
if all(isinstance(v, dict) and "command" in v for v in config_json.values()):
|
|
95
|
+
return ("mcpServers", {str(k): v for k, v in config_json.items()})
|
|
96
|
+
return ("mcpServers", {})
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _server_tokens(name: str, server_cfg: dict[str, Any]) -> set[str]:
|
|
100
|
+
parts: list[str] = [name]
|
|
101
|
+
command = server_cfg.get("command")
|
|
102
|
+
if isinstance(command, str):
|
|
103
|
+
parts.append(command)
|
|
104
|
+
args = server_cfg.get("args")
|
|
105
|
+
if isinstance(args, list):
|
|
106
|
+
parts.extend(str(item) for item in args)
|
|
107
|
+
env = server_cfg.get("env")
|
|
108
|
+
if isinstance(env, dict):
|
|
109
|
+
parts.extend(str(k) for k in env.keys())
|
|
110
|
+
return set(re.findall(r"[a-z0-9]+", " ".join(parts).lower()))
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _matches_any(tokens: set[str], keywords: tuple[str, ...]) -> bool:
|
|
114
|
+
return any(kw in tokens for kw in keywords)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def _build_wrapped_snippet(container_key: str, servers: dict[str, dict[str, Any]]) -> str:
|
|
118
|
+
wrapped_servers: dict[str, dict[str, Any]] = {}
|
|
119
|
+
for name, cfg in servers.items():
|
|
120
|
+
command = cfg.get("command")
|
|
121
|
+
args = cfg.get("args") if isinstance(cfg.get("args"), list) else []
|
|
122
|
+
|
|
123
|
+
if isinstance(command, str) and command.strip():
|
|
124
|
+
already_wrapped = (
|
|
125
|
+
command.strip() == "mcp-shield"
|
|
126
|
+
or any("shield_proxy.py" in str(item) for item in [command, *args])
|
|
127
|
+
)
|
|
128
|
+
if already_wrapped:
|
|
129
|
+
wrapped_servers[name] = dict(cfg)
|
|
130
|
+
else:
|
|
131
|
+
new_cfg: dict[str, Any] = {
|
|
132
|
+
"command": "mcp-shield",
|
|
133
|
+
"args": [
|
|
134
|
+
"proxy",
|
|
135
|
+
"--server-name",
|
|
136
|
+
name,
|
|
137
|
+
"--session-id",
|
|
138
|
+
"default",
|
|
139
|
+
"--",
|
|
140
|
+
command,
|
|
141
|
+
*[str(a) for a in args],
|
|
142
|
+
],
|
|
143
|
+
}
|
|
144
|
+
if isinstance(cfg.get("env"), dict):
|
|
145
|
+
new_cfg["env"] = cfg["env"]
|
|
146
|
+
if isinstance(cfg.get("cwd"), str):
|
|
147
|
+
new_cfg["cwd"] = cfg["cwd"]
|
|
148
|
+
wrapped_servers[name] = new_cfg
|
|
149
|
+
else:
|
|
150
|
+
wrapped_servers[name] = dict(cfg)
|
|
151
|
+
|
|
152
|
+
return json.dumps({container_key: wrapped_servers}, indent=2)
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def audit_config(config_json: dict[str, Any]) -> dict[str, Any]:
|
|
156
|
+
"""Analyze an MCP config for the Lethal Trifecta and return a wrapped snippet."""
|
|
157
|
+
container_key, servers = _extract_servers(config_json)
|
|
158
|
+
|
|
159
|
+
untrusted: list[str] = []
|
|
160
|
+
private: list[str] = []
|
|
161
|
+
egress: list[str] = []
|
|
162
|
+
|
|
163
|
+
for name, cfg in servers.items():
|
|
164
|
+
tokens = _server_tokens(name, cfg)
|
|
165
|
+
if _matches_any(tokens, _UNTRUSTED_KEYWORDS):
|
|
166
|
+
untrusted.append(name)
|
|
167
|
+
if _matches_any(tokens, _PRIVATE_KEYWORDS):
|
|
168
|
+
private.append(name)
|
|
169
|
+
if _matches_any(tokens, _EGRESS_KEYWORDS):
|
|
170
|
+
egress.append(name)
|
|
171
|
+
|
|
172
|
+
legs_present = sum(bool(leg) for leg in (untrusted, private, egress))
|
|
173
|
+
if legs_present == 3:
|
|
174
|
+
risk_level = "CRITICAL"
|
|
175
|
+
elif legs_present == 2:
|
|
176
|
+
risk_level = "MEDIUM"
|
|
177
|
+
else:
|
|
178
|
+
risk_level = "LOW"
|
|
179
|
+
|
|
180
|
+
risky_combos: list[str] = []
|
|
181
|
+
if untrusted and private and egress:
|
|
182
|
+
risky_combos.append(
|
|
183
|
+
f"Lethal Trifecta: Untrusted input ({', '.join(untrusted)}) + "
|
|
184
|
+
f"Private data ({', '.join(private)}) + "
|
|
185
|
+
f"External egress/exec ({', '.join(egress)}) enables indirect prompt injection exfiltration."
|
|
186
|
+
)
|
|
187
|
+
if untrusted and private:
|
|
188
|
+
risky_combos.append(
|
|
189
|
+
f"Data Exposure Pair: Untrusted input ({', '.join(untrusted)}) can steer the agent "
|
|
190
|
+
f"to read private data ({', '.join(private)})."
|
|
191
|
+
)
|
|
192
|
+
if private and egress:
|
|
193
|
+
risky_combos.append(
|
|
194
|
+
f"Exfiltration Pair: Private data ({', '.join(private)}) paired with external "
|
|
195
|
+
f"egress/exec ({', '.join(egress)}) risks silent secret leakage."
|
|
196
|
+
)
|
|
197
|
+
if untrusted and egress:
|
|
198
|
+
risky_combos.append(
|
|
199
|
+
f"Hijack Execution Pair: Untrusted input ({', '.join(untrusted)}) paired with "
|
|
200
|
+
f"external egress/exec ({', '.join(egress)}) risks unauthorized outbound actions or RCE."
|
|
201
|
+
)
|
|
202
|
+
|
|
203
|
+
return {
|
|
204
|
+
"risk_level": risk_level,
|
|
205
|
+
"legs": {
|
|
206
|
+
"untrusted": untrusted,
|
|
207
|
+
"private": private,
|
|
208
|
+
"egress": egress,
|
|
209
|
+
},
|
|
210
|
+
"risky_combos": risky_combos,
|
|
211
|
+
"wrapped_snippet": _build_wrapped_snippet(container_key, servers),
|
|
212
|
+
}
|
|
213
|
+
|
mcp_shield/cli.py
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"""CLI entrypoints for MCP-Shield (`mcp-start` and `mcp-shield`)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import json
|
|
7
|
+
import os
|
|
8
|
+
import sys
|
|
9
|
+
import threading
|
|
10
|
+
import webbrowser
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
|
|
13
|
+
import uvicorn
|
|
14
|
+
|
|
15
|
+
from mcp_shield import auditor, proxy
|
|
16
|
+
from mcp_shield.config import (
|
|
17
|
+
get_data_dir,
|
|
18
|
+
get_db_path,
|
|
19
|
+
get_policy_path,
|
|
20
|
+
get_project_dir,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def start_dashboard(argv: list[str] | None = None) -> None:
|
|
25
|
+
"""Entrypoint for `mcp-start` dashboard and control server."""
|
|
26
|
+
parser = argparse.ArgumentParser(
|
|
27
|
+
prog="mcp-start",
|
|
28
|
+
description="Start the MCP-Shield Control Server and Web Console",
|
|
29
|
+
)
|
|
30
|
+
parser.add_argument(
|
|
31
|
+
"--project-dir",
|
|
32
|
+
default=".",
|
|
33
|
+
help="Target workspace directory to protect (default: current directory)",
|
|
34
|
+
)
|
|
35
|
+
parser.add_argument("--port", type=int, default=8000, help="Server port (default: 8000)")
|
|
36
|
+
parser.add_argument("--host", default="127.0.0.1", help="Bind host (default: 127.0.0.1)")
|
|
37
|
+
parser.add_argument(
|
|
38
|
+
"--no-browser",
|
|
39
|
+
action="store_true",
|
|
40
|
+
help="Do not automatically open the web browser",
|
|
41
|
+
)
|
|
42
|
+
args = parser.parse_args(argv)
|
|
43
|
+
|
|
44
|
+
os.environ["MCP_SHIELD_PROJECT_DIR"] = str(Path(args.project_dir).resolve())
|
|
45
|
+
|
|
46
|
+
if not args.no_browser:
|
|
47
|
+
url = f"http://{args.host}:{args.port}"
|
|
48
|
+
timer = threading.Timer(1.2, webbrowser.open, args=[url])
|
|
49
|
+
timer.daemon = True
|
|
50
|
+
timer.start()
|
|
51
|
+
|
|
52
|
+
uvicorn.run("mcp_shield.server:app", host=args.host, port=args.port)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _cmd_init(config_path: str, output_path: str | None = None) -> int:
|
|
56
|
+
src = Path(config_path).resolve()
|
|
57
|
+
if not src.exists():
|
|
58
|
+
sys.stderr.write(f"Error: Config file not found: {src}\n")
|
|
59
|
+
return 1
|
|
60
|
+
|
|
61
|
+
try:
|
|
62
|
+
config_data = json.loads(src.read_text(encoding="utf-8"))
|
|
63
|
+
except json.JSONDecodeError as exc:
|
|
64
|
+
sys.stderr.write(f"Error: Invalid JSON in {src}: {exc}\n")
|
|
65
|
+
return 1
|
|
66
|
+
|
|
67
|
+
report = auditor.audit_config(config_data)
|
|
68
|
+
sys.stdout.write(f"MCP-Shield Risk Level: {report['risk_level']}\n")
|
|
69
|
+
for combo in report.get("risky_combos", []):
|
|
70
|
+
sys.stdout.write(f" - {combo}\n")
|
|
71
|
+
|
|
72
|
+
dest = (
|
|
73
|
+
Path(output_path).resolve()
|
|
74
|
+
if output_path
|
|
75
|
+
else src.with_name(f"{src.stem}.shield{src.suffix}")
|
|
76
|
+
)
|
|
77
|
+
dest.write_text(report["wrapped_snippet"] + "\n", encoding="utf-8")
|
|
78
|
+
sys.stdout.write(f"Wrapped MCP configuration written to: {dest}\n")
|
|
79
|
+
return 0
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def _cmd_status() -> int:
|
|
83
|
+
sys.stdout.write("MCP-Shield Configuration Status\n")
|
|
84
|
+
sys.stdout.write(f" project_dir : {get_project_dir()}\n")
|
|
85
|
+
sys.stdout.write(f" data_dir : {get_data_dir()}\n")
|
|
86
|
+
sys.stdout.write(f" db_path : {get_db_path()}\n")
|
|
87
|
+
sys.stdout.write(f" policy_path : {get_policy_path()}\n")
|
|
88
|
+
return 0
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def main(argv: list[str] | None = None) -> None:
|
|
92
|
+
"""Entrypoint for `mcp-shield` CLI (`proxy`, `init`, `status`)."""
|
|
93
|
+
parser = argparse.ArgumentParser(
|
|
94
|
+
prog="mcp-shield",
|
|
95
|
+
description="MCP-Shield CLI: runtime proxy, config auditor, and status",
|
|
96
|
+
)
|
|
97
|
+
subparsers = parser.add_subparsers(dest="subcommand", required=True)
|
|
98
|
+
|
|
99
|
+
proxy_parser = subparsers.add_parser(
|
|
100
|
+
"proxy",
|
|
101
|
+
help="Wrap an MCP stdio server through MCP-Shield",
|
|
102
|
+
)
|
|
103
|
+
proxy_parser.add_argument("--server-name", required=True, help="Target server name")
|
|
104
|
+
proxy_parser.add_argument("--session-id", required=True, help="Security session ID")
|
|
105
|
+
proxy_parser.add_argument(
|
|
106
|
+
"target_command",
|
|
107
|
+
nargs=argparse.REMAINDER,
|
|
108
|
+
help="Target MCP server command (prefix with --)",
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
init_parser = subparsers.add_parser(
|
|
112
|
+
"init",
|
|
113
|
+
help="Audit an agent MCP JSON config and generate a shielded config",
|
|
114
|
+
)
|
|
115
|
+
init_parser.add_argument("--config", required=True, help="Path to MCP config JSON file")
|
|
116
|
+
init_parser.add_argument(
|
|
117
|
+
"--output",
|
|
118
|
+
default=None,
|
|
119
|
+
help="Optional output path (defaults to <config>.shield.json)",
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
subparsers.add_parser(
|
|
123
|
+
"status",
|
|
124
|
+
help="Print resolved project_dir, data_dir, db_path, and policy_path",
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
args = parser.parse_args(argv)
|
|
128
|
+
|
|
129
|
+
if args.subcommand == "proxy":
|
|
130
|
+
proxy_argv = ["--server-name", args.server_name, "--session-id", args.session_id]
|
|
131
|
+
proxy_argv.extend(args.target_command)
|
|
132
|
+
proxy.main(proxy_argv)
|
|
133
|
+
elif args.subcommand == "init":
|
|
134
|
+
sys.exit(_cmd_init(args.config, args.output))
|
|
135
|
+
elif args.subcommand == "status":
|
|
136
|
+
sys.exit(_cmd_status())
|
|
137
|
+
|
mcp_shield/config.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"""Runtime path and environment configuration for MCP-Shield."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def get_project_dir() -> Path:
|
|
10
|
+
"""Return the active project directory (MCP_SHIELD_PROJECT_DIR or cwd)."""
|
|
11
|
+
env_val = os.getenv("MCP_SHIELD_PROJECT_DIR", "").strip()
|
|
12
|
+
if env_val:
|
|
13
|
+
return Path(env_val).resolve()
|
|
14
|
+
return Path.cwd().resolve()
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def get_data_dir() -> Path:
|
|
18
|
+
"""Return the persistent MCP-Shield data directory (~/.mcp-shield or MCP_SHIELD_DATA_DIR)."""
|
|
19
|
+
env_val = os.getenv("MCP_SHIELD_DATA_DIR", "").strip()
|
|
20
|
+
data_dir = Path(env_val).resolve() if env_val else (Path.home() / ".mcp-shield").resolve()
|
|
21
|
+
data_dir.mkdir(parents=True, exist_ok=True)
|
|
22
|
+
return data_dir
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def get_db_path() -> Path:
|
|
26
|
+
"""Return the SQLite database path inside the data directory."""
|
|
27
|
+
return get_data_dir() / "shield.db"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def get_shadows_dir() -> Path:
|
|
31
|
+
"""Return the shadow workspaces root directory inside the data directory."""
|
|
32
|
+
return get_data_dir() / "temp_shadows"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def get_policy_path() -> Path:
|
|
36
|
+
"""Return project-local .mcp-shield/policy.json if present, else bundled policy.json."""
|
|
37
|
+
project_policy = get_project_dir() / ".mcp-shield" / "policy.json"
|
|
38
|
+
if project_policy.exists():
|
|
39
|
+
return project_policy
|
|
40
|
+
return Path(__file__).resolve().parent / "policy.json"
|
|
41
|
+
|