usbguard-mcp 0.1.0__tar.gz

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.
Files changed (75) hide show
  1. usbguard_mcp-0.1.0/.gitignore +12 -0
  2. usbguard_mcp-0.1.0/CHANGELOG.md +36 -0
  3. usbguard_mcp-0.1.0/LICENSE +21 -0
  4. usbguard_mcp-0.1.0/PKG-INFO +215 -0
  5. usbguard_mcp-0.1.0/README.md +186 -0
  6. usbguard_mcp-0.1.0/SECURITY.md +14 -0
  7. usbguard_mcp-0.1.0/THIRD_PARTY_NOTICES.md +42 -0
  8. usbguard_mcp-0.1.0/docs/architecture.md +32 -0
  9. usbguard_mcp-0.1.0/docs/assets/banner.png +0 -0
  10. usbguard_mcp-0.1.0/docs/configuration.md +28 -0
  11. usbguard_mcp-0.1.0/docs/development.md +30 -0
  12. usbguard_mcp-0.1.0/docs/installation.md +43 -0
  13. usbguard_mcp-0.1.0/docs/permissions.md +45 -0
  14. usbguard_mcp-0.1.0/docs/threat-model.md +29 -0
  15. usbguard_mcp-0.1.0/docs/tools.md +225 -0
  16. usbguard_mcp-0.1.0/docs/troubleshooting.md +20 -0
  17. usbguard_mcp-0.1.0/docs/validation.md +70 -0
  18. usbguard_mcp-0.1.0/examples/README.md +21 -0
  19. usbguard_mcp-0.1.0/examples/claude-code/README.md +12 -0
  20. usbguard_mcp-0.1.0/examples/claude-desktop/claude_desktop_config.json +8 -0
  21. usbguard_mcp-0.1.0/examples/codex/README.md +10 -0
  22. usbguard_mcp-0.1.0/examples/inspector/README.md +10 -0
  23. usbguard_mcp-0.1.0/examples/opencode/opencode.json +9 -0
  24. usbguard_mcp-0.1.0/examples/vscode/mcp.json +9 -0
  25. usbguard_mcp-0.1.0/pyproject.toml +74 -0
  26. usbguard_mcp-0.1.0/src/usbguard_mcp/__init__.py +5 -0
  27. usbguard_mcp-0.1.0/src/usbguard_mcp/__main__.py +3 -0
  28. usbguard_mcp-0.1.0/src/usbguard_mcp/backends/__init__.py +14 -0
  29. usbguard_mcp-0.1.0/src/usbguard_mcp/backends/base.py +13 -0
  30. usbguard_mcp-0.1.0/src/usbguard_mcp/backends/cli.py +108 -0
  31. usbguard_mcp-0.1.0/src/usbguard_mcp/backends/mock.py +65 -0
  32. usbguard_mcp-0.1.0/src/usbguard_mcp/cli.py +56 -0
  33. usbguard_mcp-0.1.0/src/usbguard_mcp/config.py +29 -0
  34. usbguard_mcp-0.1.0/src/usbguard_mcp/exceptions.py +23 -0
  35. usbguard_mcp-0.1.0/src/usbguard_mcp/models.py +101 -0
  36. usbguard_mcp-0.1.0/src/usbguard_mcp/parsers/__init__.py +0 -0
  37. usbguard_mcp-0.1.0/src/usbguard_mcp/parsers/events.py +70 -0
  38. usbguard_mcp-0.1.0/src/usbguard_mcp/parsers/rules.py +158 -0
  39. usbguard_mcp-0.1.0/src/usbguard_mcp/prompts.py +63 -0
  40. usbguard_mcp-0.1.0/src/usbguard_mcp/py.typed +0 -0
  41. usbguard_mcp-0.1.0/src/usbguard_mcp/resources.py +90 -0
  42. usbguard_mcp-0.1.0/src/usbguard_mcp/security/__init__.py +0 -0
  43. usbguard_mcp-0.1.0/src/usbguard_mcp/security/command_guard.py +40 -0
  44. usbguard_mcp-0.1.0/src/usbguard_mcp/security/redaction.py +69 -0
  45. usbguard_mcp-0.1.0/src/usbguard_mcp/security/validation.py +65 -0
  46. usbguard_mcp-0.1.0/src/usbguard_mcp/server.py +31 -0
  47. usbguard_mcp-0.1.0/src/usbguard_mcp/services/__init__.py +0 -0
  48. usbguard_mcp-0.1.0/src/usbguard_mcp/services/audit_service.py +400 -0
  49. usbguard_mcp-0.1.0/src/usbguard_mcp/services/context.py +28 -0
  50. usbguard_mcp-0.1.0/src/usbguard_mcp/services/device_service.py +269 -0
  51. usbguard_mcp-0.1.0/src/usbguard_mcp/services/event_service.py +40 -0
  52. usbguard_mcp-0.1.0/src/usbguard_mcp/services/policy_service.py +450 -0
  53. usbguard_mcp-0.1.0/src/usbguard_mcp/services/system_service.py +160 -0
  54. usbguard_mcp-0.1.0/src/usbguard_mcp/tools/__init__.py +0 -0
  55. usbguard_mcp-0.1.0/src/usbguard_mcp/tools/_common.py +72 -0
  56. usbguard_mcp-0.1.0/src/usbguard_mcp/tools/devices.py +190 -0
  57. usbguard_mcp-0.1.0/src/usbguard_mcp/tools/monitoring.py +89 -0
  58. usbguard_mcp-0.1.0/src/usbguard_mcp/tools/policies.py +146 -0
  59. usbguard_mcp-0.1.0/src/usbguard_mcp/tools/security.py +133 -0
  60. usbguard_mcp-0.1.0/src/usbguard_mcp/tools/system.py +58 -0
  61. usbguard_mcp-0.1.0/src/usbguard_mcp/utils/__init__.py +0 -0
  62. usbguard_mcp-0.1.0/src/usbguard_mcp/utils/platform.py +65 -0
  63. usbguard_mcp-0.1.0/tests/conftest.py +21 -0
  64. usbguard_mcp-0.1.0/tests/fixtures/list-devices-live-format.txt +4 -0
  65. usbguard_mcp-0.1.0/tests/integration/test_degraded.py +80 -0
  66. usbguard_mcp-0.1.0/tests/integration/test_mcp.py +215 -0
  67. usbguard_mcp-0.1.0/tests/security/test_command_guard.py +81 -0
  68. usbguard_mcp-0.1.0/tests/security/test_tools_security.py +237 -0
  69. usbguard_mcp-0.1.0/tests/unit/test_audit.py +127 -0
  70. usbguard_mcp-0.1.0/tests/unit/test_backends.py +147 -0
  71. usbguard_mcp-0.1.0/tests/unit/test_cli.py +77 -0
  72. usbguard_mcp-0.1.0/tests/unit/test_devices.py +96 -0
  73. usbguard_mcp-0.1.0/tests/unit/test_parsers.py +140 -0
  74. usbguard_mcp-0.1.0/tests/unit/test_policy.py +157 -0
  75. usbguard_mcp-0.1.0/tests/unit/test_support.py +135 -0
@@ -0,0 +1,12 @@
1
+ .venv/
2
+ __pycache__/
3
+ *.pyc
4
+ .coverage*
5
+ htmlcov/
6
+ dist/
7
+ *.egg-info/
8
+ .env
9
+ .pytest_cache/
10
+ .mypy_cache/
11
+ .ruff_cache/
12
+ coverage.xml
@@ -0,0 +1,36 @@
1
+ # Changelog
2
+
3
+ All notable changes are documented here. The format follows
4
+ [Keep a Changelog](https://keepachangelog.com/) and the project uses
5
+ [Semantic Versioning](https://semver.org/).
6
+
7
+ ## [Unreleased]
8
+
9
+ ## [0.1.0] - 2026-12-06
10
+
11
+ Initial, read-only release.
12
+
13
+ ### Added
14
+ - 32 read-only MCP tools (system, devices, policy, security, monitoring), 6 resources, 5 prompts.
15
+ - STDIO transport; protocol on stdout, logs on stderr, no banner.
16
+ - `usbguard` CLI backend limited to `--version`, `list-devices`, `list-rules`, `watch`.
17
+ - Explicit synthetic mock backend with 7 scenarios (`--mock`).
18
+ - Policy analysis honouring first-match-wins ordering and USBGuard set operators.
19
+ - Deterministic, explainable heuristics with evidence, severity, confidence and limitations.
20
+ - Privacy-preserving redaction (serials, hashes, ports, host IDs, audit metadata) on by default.
21
+
22
+ ### Validated
23
+ - Real USBGuard 1.1.4 daemon, a real USB flash drive and Claude Code, Codex and OpenCode on one
24
+ Fedora KDE laptop; least-privilege IPC (`Devices=list,listen`, `Policy=list`) as a non-wheel user.
25
+ Details: docs/validation.md.
26
+
27
+ ### Fixed during validation
28
+ - Duplicate `with-interface` entries from the real daemon are deduplicated.
29
+ - Firmware-update/vendor-specific interfaces no longer make a device "composite".
30
+ - Event field renamed `collected_at` (USBGuard provides no event timestamps).
31
+ - `detect_identity_changes` skips redacted serial/hash fields (no guessing oracle).
32
+ - Unexpected-error text is logged only at DEBUG.
33
+
34
+ ### Known limitations
35
+ - Only Fedora and USBGuard 1.1.4 were exercised; other distributions/versions are untested.
36
+ - No native IPC backend, no historical journal access, no HTTP transport.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 0x5t4l1n
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,215 @@
1
+ Metadata-Version: 2.5
2
+ Name: usbguard-mcp
3
+ Version: 0.1.0
4
+ Summary: Intelligent USB security through the Model Context Protocol.
5
+ Project-URL: Homepage, https://github.com/0x5t4l1n/USBGuard-MCP
6
+ Project-URL: Documentation, https://github.com/0x5t4l1n/USBGuard-MCP/tree/main/docs
7
+ Project-URL: Changelog, https://github.com/0x5t4l1n/USBGuard-MCP/blob/main/CHANGELOG.md
8
+ Project-URL: Repository, https://github.com/0x5t4l1n/USBGuard-MCP
9
+ Project-URL: Issues, https://github.com/0x5t4l1n/USBGuard-MCP/issues
10
+ Author: 0x5t4l1n
11
+ License-Expression: MIT
12
+ License-File: LICENSE
13
+ Keywords: linux,mcp,model-context-protocol,security,usb,usbguard
14
+ Classifier: Development Status :: 3 - Alpha
15
+ Classifier: Environment :: Console
16
+ Classifier: Intended Audience :: System Administrators
17
+ Classifier: License :: OSI Approved :: MIT License
18
+ Classifier: Operating System :: POSIX :: Linux
19
+ Classifier: Programming Language :: Python :: 3
20
+ Classifier: Programming Language :: Python :: 3.11
21
+ Classifier: Programming Language :: Python :: 3.12
22
+ Classifier: Programming Language :: Python :: 3.13
23
+ Classifier: Topic :: Security
24
+ Classifier: Typing :: Typed
25
+ Requires-Python: >=3.11
26
+ Requires-Dist: fastmcp<5,>=4.2.7
27
+ Requires-Dist: pydantic<3,>=2.12
28
+ Description-Content-Type: text/markdown
29
+
30
+ <p align="center">
31
+ <img src="https://raw.githubusercontent.com/0x5t4l1n/USBGuard-MCP/main/docs/assets/banner.png" alt="USBGuard-MCP: secure, read-only USB security for AI agents" width="100%">
32
+ </p>
33
+
34
+ # USBGuard-MCP
35
+
36
+ Intelligent USB security through the Model Context Protocol.
37
+
38
+ [![CI](https://github.com/0x5t4l1n/USBGuard-MCP/actions/workflows/ci.yml/badge.svg)](https://github.com/0x5t4l1n/USBGuard-MCP/actions/workflows/ci.yml)
39
+ [![Security](https://github.com/0x5t4l1n/USBGuard-MCP/actions/workflows/security.yml/badge.svg)](https://github.com/0x5t4l1n/USBGuard-MCP/actions/workflows/security.yml)
40
+ [![PyPI](https://img.shields.io/pypi/v/usbguard-mcp)](https://pypi.org/project/usbguard-mcp/)
41
+ [![Python](https://img.shields.io/pypi/pyversions/usbguard-mcp)](https://pypi.org/project/usbguard-mcp/)
42
+ [![License: MIT](https://img.shields.io/github/license/0x5t4l1n/USBGuard-MCP)](LICENSE)
43
+
44
+ mcp-name: io.github.0x5t4l1n/usbguard-mcp
45
+
46
+ USBGuard-MCP is an **independent, read-only** [MCP](https://modelcontextprotocol.io/) server that
47
+ lets AI agents inspect [USBGuard](https://usbguard.github.io/): list devices, analyze the
48
+ authorization policy, observe authorization events, and produce evidence-based security findings.
49
+ It is not affiliated with or endorsed by the USBGuard project.
50
+
51
+ > **Status: 0.1.0, first release.** Validated against a running USBGuard 1.1.4 daemon, a real USB
52
+ > flash drive and three MCP clients on one Fedora KDE laptop. Other distributions and USBGuard
53
+ > versions are untested. Full record: [docs/validation.md](docs/validation.md).
54
+
55
+ ## Why
56
+
57
+ USBGuard answers "may this device be used?" but its CLI output is text meant for humans. An agent
58
+ asked to "audit my USB policy" would otherwise have to run raw shell commands. USBGuard-MCP gives it
59
+ a narrow, typed, read-only interface instead: no arbitrary command execution, no write access,
60
+ structured results, and findings that state their evidence and limits.
61
+
62
+ ## Key features
63
+
64
+ - **32 read-only tools**, 6 resources and 5 prompts ([reference](docs/tools.md)).
65
+ - **Read-only by construction**: the backend can only run `usbguard --version`, `list-devices`,
66
+ `list-rules` and `watch`. Nothing can authorize, block, reject, or edit policy.
67
+ - **Policy analysis that respects USBGuard semantics**: first-match-wins ordering, set operators
68
+ (`equals` is the default), wildcards, and conditions. It reports duplicates, shadowed or
69
+ conflicting rules and permissive rules only when set logic *proves* them, and states what it
70
+ cannot evaluate.
71
+ - **Explainable heuristics**: every finding has evidence, severity, confidence, limitations and
72
+ remediation. It never calls a device "malicious" and does not detect malware or firmware attacks.
73
+ - **Untrusted-metadata handling**: device names, serials and rule text are sanitized, kept out of
74
+ finding prose, and flagged as untrusted in every response.
75
+ - **Privacy by default**: serials, hashes, ports, host identifiers and audit metadata are redacted.
76
+ No telemetry; runs fully offline.
77
+ - **Explicit mock mode** with synthetic scenarios, so development needs no root or hardware.
78
+
79
+ ## Architecture
80
+
81
+ ```mermaid
82
+ flowchart TD
83
+ A[MCP client] -->|STDIO| B[FastMCP server]
84
+ B --> C[Tools / resources / prompts]
85
+ C --> D[Input validation]
86
+ D --> E[Services: devices, policy, audit, events]
87
+ E --> F[Redaction layer]
88
+ E --> G[Backend interface]
89
+ G --> H[CLI backend<br/>command guard + bounded subprocess]
90
+ G --> M[Mock backend<br/>explicit opt-in]
91
+ H --> I[usbguard CLI] --> J[usbguard daemon<br/>source of truth]
92
+ ```
93
+
94
+ Details: [docs/architecture.md](docs/architecture.md).
95
+
96
+ ## Security model
97
+
98
+ Read-only, least privilege, no `sudo`, no `shell=True`, absolute root-owned executable path, minimal
99
+ environment, timeouts, output and concurrency limits, redaction on by default. See
100
+ [docs/threat-model.md](docs/threat-model.md) and [docs/permissions.md](docs/permissions.md).
101
+
102
+ ## Supported platforms
103
+
104
+ Linux only. Fedora is the development platform; Fedora, RHEL, CentOS Stream, Rocky, AlmaLinux,
105
+ Ubuntu, Debian, Arch and openSUSE are recognized and receive install hints. *Recognized* does not
106
+ mean *tested*: only Fedora has been exercised, and only up to the point of an inactive daemon.
107
+ USBGuard versions differ between distributions; this was developed against USBGuard 1.1.4.
108
+
109
+ ## Requirements
110
+
111
+ Python 3.11+, USBGuard installed and its daemon running (not needed for `--mock`). USBGuard-MCP never
112
+ installs or configures USBGuard for you.
113
+
114
+ ## Installation
115
+
116
+ ```bash
117
+ uvx usbguard-mcp --version # run without installing (needs uv)
118
+ pip install usbguard-mcp # or install into an environment
119
+ usbguard-mcp --mock # try synthetic data, no USBGuard needed
120
+ ```
121
+
122
+ From a checkout: `uv sync && uv run usbguard-mcp`. See [docs/installation.md](docs/installation.md).
123
+
124
+ ## Quick start
125
+
126
+ 1. Try it on synthetic data: `uvx usbguard-mcp --mock` (an MCP client speaks to it over STDIO).
127
+ 2. For real data, ask an administrator for the minimal read-only ACL in
128
+ [docs/permissions.md](docs/permissions.md), then run `uvx usbguard-mcp` and call
129
+ `check_permissions`.
130
+
131
+ ## MCP client configuration
132
+
133
+ | Client | Status (live, real daemon, 2026-12-06) |
134
+ |---|---|
135
+ | Claude Code | **Tested**: tool call returned the real USBGuard version |
136
+ | Codex CLI | **Tested**: same |
137
+ | OpenCode | **Tested** with caveats: use a project-level `opencode.json`; small models may need the server named in the prompt |
138
+ | MCP Inspector (CLI) | **Tested**: `tools/list`, `tools/call` |
139
+ | Claude Desktop, VS Code | Config shape follows vendor docs; **untested** |
140
+
141
+ Examples live in [`examples/`](examples/). For instance, with a local checkout:
142
+
143
+ ```bash
144
+ claude mcp add --scope user --transport stdio usbguard -- uvx usbguard-mcp
145
+ codex mcp add usbguard -- uvx usbguard-mcp
146
+ ```
147
+
148
+ ## Tools
149
+
150
+ Full generated reference: [docs/tools.md](docs/tools.md).
151
+
152
+ | Group | Tools |
153
+ |---|---|
154
+ | System | `get_usbguard_version`, `get_system_status`, `check_permissions`, `get_server_info`, `health_check` |
155
+ | Devices | `list_usb_devices`, `list_allowed_devices`, `list_blocked_devices`, `list_rejected_devices`, `inspect_usb_device`, `search_usb_devices`, `get_device_statistics`, `classify_usb_device`, `compare_usb_devices` |
156
+ | Policy | `list_policy_rules`, `inspect_policy_rule`, `analyze_security_policy`, `detect_duplicate_rules`, `detect_permissive_rules`, `detect_policy_conflicts`, `recommend_policy_improvements`, `preview_policy_rule` |
157
+ | Security | `assess_usb_security`, `detect_suspicious_interfaces`, `detect_composite_devices`, `analyze_device_risk`, `detect_identity_changes`, `generate_security_report` |
158
+ | Monitoring | `collect_usb_events`, `get_recent_usb_events`, `get_event_statistics`, `analyze_usb_activity` |
159
+
160
+ Resources: `usbguard://system/status`, `usbguard://devices`, `usbguard://policies`,
161
+ `usbguard://events/recent`, `usbguard://security/summary`, `usbguard://documentation`.
162
+ Prompts: `audit_usb_security`, `investigate_usb_device`, `review_usb_policy`,
163
+ `analyze_recent_usb_activity`, `generate_usb_security_report`.
164
+
165
+ ## Example prompts
166
+
167
+ - "List all authorized USB devices."
168
+ - "Are there any blocked USB devices?"
169
+ - "Audit my current USB authorization policy."
170
+ - "Explain which devices expose multiple USB interfaces."
171
+ - "Review my USBGuard configuration for security weaknesses."
172
+ - "Generate a USB security assessment report."
173
+
174
+ Example (synthetic data) from `detect_policy_conflicts`:
175
+
176
+ ```json
177
+ {"code": "CONFLICTING_RULE", "severity": "medium", "confidence": "high",
178
+ "summary": "A later rule has a different target but is unreachable: an earlier unconditional rule always matches first.",
179
+ "evidence": {"earlier_rule_id": 1, "later_rule_id": 3, "earlier_target": "allow", "later_target": "block"},
180
+ "limitations": ["Rule conditions (if ...) are evaluated by the daemon at runtime, not here.", "..."],
181
+ "remediation": "Decide which target is intended, then fix the rule order or remove the unreachable rule."}
182
+ ```
183
+
184
+ ## Troubleshooting
185
+
186
+ See [docs/troubleshooting.md](docs/troubleshooting.md). Start with `check_permissions`.
187
+
188
+ ## Known limitations
189
+
190
+ - Validated on one laptop, one USBGuard version (1.1.4) and one distribution (Fedora); parsers are
191
+ strict and report `PARSE_ERROR` rather than guessing, so other versions may need fixes.
192
+ - Event timestamps are collection time, not occurrence time (USBGuard `watch` gives none); only
193
+ order is reliable. USBGuard device IDs change when a device is replugged.
194
+ - `ImplicitPolicyTarget` and other settings in `usbguard-daemon.conf` are root-only and not read.
195
+ - Rule conditions (`if ...`) are not evaluated. Policy analysis is sound but incomplete.
196
+ - Rejected/disconnected devices are only visible via the live list or events seen this session.
197
+ - Events come from a bounded in-memory buffer; there is no historical journal source.
198
+ - No native IPC backend, HTTP transport or write operations.
199
+
200
+ ## Roadmap
201
+
202
+ 0.x: more distributions and USBGuard versions, MCP Registry submission. Later,
203
+ each needing its own security review: native IPC backend, persistent local event storage,
204
+ authenticated localhost HTTP, carefully controlled administrative functions. None may weaken the
205
+ read-only default.
206
+
207
+ ## Contributing
208
+
209
+ See [CONTRIBUTING.md](CONTRIBUTING.md). Report vulnerabilities per [SECURITY.md](SECURITY.md).
210
+
211
+ ## License and acknowledgment
212
+
213
+ MIT, see [LICENSE](LICENSE) and [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md). Thanks to the
214
+ [USBGuard](https://github.com/USBGuard/usbguard) project (GPL-2.0-or-later). This project invokes
215
+ its CLI and copies none of its code.
@@ -0,0 +1,186 @@
1
+ <p align="center">
2
+ <img src="https://raw.githubusercontent.com/0x5t4l1n/USBGuard-MCP/main/docs/assets/banner.png" alt="USBGuard-MCP: secure, read-only USB security for AI agents" width="100%">
3
+ </p>
4
+
5
+ # USBGuard-MCP
6
+
7
+ Intelligent USB security through the Model Context Protocol.
8
+
9
+ [![CI](https://github.com/0x5t4l1n/USBGuard-MCP/actions/workflows/ci.yml/badge.svg)](https://github.com/0x5t4l1n/USBGuard-MCP/actions/workflows/ci.yml)
10
+ [![Security](https://github.com/0x5t4l1n/USBGuard-MCP/actions/workflows/security.yml/badge.svg)](https://github.com/0x5t4l1n/USBGuard-MCP/actions/workflows/security.yml)
11
+ [![PyPI](https://img.shields.io/pypi/v/usbguard-mcp)](https://pypi.org/project/usbguard-mcp/)
12
+ [![Python](https://img.shields.io/pypi/pyversions/usbguard-mcp)](https://pypi.org/project/usbguard-mcp/)
13
+ [![License: MIT](https://img.shields.io/github/license/0x5t4l1n/USBGuard-MCP)](LICENSE)
14
+
15
+ mcp-name: io.github.0x5t4l1n/usbguard-mcp
16
+
17
+ USBGuard-MCP is an **independent, read-only** [MCP](https://modelcontextprotocol.io/) server that
18
+ lets AI agents inspect [USBGuard](https://usbguard.github.io/): list devices, analyze the
19
+ authorization policy, observe authorization events, and produce evidence-based security findings.
20
+ It is not affiliated with or endorsed by the USBGuard project.
21
+
22
+ > **Status: 0.1.0, first release.** Validated against a running USBGuard 1.1.4 daemon, a real USB
23
+ > flash drive and three MCP clients on one Fedora KDE laptop. Other distributions and USBGuard
24
+ > versions are untested. Full record: [docs/validation.md](docs/validation.md).
25
+
26
+ ## Why
27
+
28
+ USBGuard answers "may this device be used?" but its CLI output is text meant for humans. An agent
29
+ asked to "audit my USB policy" would otherwise have to run raw shell commands. USBGuard-MCP gives it
30
+ a narrow, typed, read-only interface instead: no arbitrary command execution, no write access,
31
+ structured results, and findings that state their evidence and limits.
32
+
33
+ ## Key features
34
+
35
+ - **32 read-only tools**, 6 resources and 5 prompts ([reference](docs/tools.md)).
36
+ - **Read-only by construction**: the backend can only run `usbguard --version`, `list-devices`,
37
+ `list-rules` and `watch`. Nothing can authorize, block, reject, or edit policy.
38
+ - **Policy analysis that respects USBGuard semantics**: first-match-wins ordering, set operators
39
+ (`equals` is the default), wildcards, and conditions. It reports duplicates, shadowed or
40
+ conflicting rules and permissive rules only when set logic *proves* them, and states what it
41
+ cannot evaluate.
42
+ - **Explainable heuristics**: every finding has evidence, severity, confidence, limitations and
43
+ remediation. It never calls a device "malicious" and does not detect malware or firmware attacks.
44
+ - **Untrusted-metadata handling**: device names, serials and rule text are sanitized, kept out of
45
+ finding prose, and flagged as untrusted in every response.
46
+ - **Privacy by default**: serials, hashes, ports, host identifiers and audit metadata are redacted.
47
+ No telemetry; runs fully offline.
48
+ - **Explicit mock mode** with synthetic scenarios, so development needs no root or hardware.
49
+
50
+ ## Architecture
51
+
52
+ ```mermaid
53
+ flowchart TD
54
+ A[MCP client] -->|STDIO| B[FastMCP server]
55
+ B --> C[Tools / resources / prompts]
56
+ C --> D[Input validation]
57
+ D --> E[Services: devices, policy, audit, events]
58
+ E --> F[Redaction layer]
59
+ E --> G[Backend interface]
60
+ G --> H[CLI backend<br/>command guard + bounded subprocess]
61
+ G --> M[Mock backend<br/>explicit opt-in]
62
+ H --> I[usbguard CLI] --> J[usbguard daemon<br/>source of truth]
63
+ ```
64
+
65
+ Details: [docs/architecture.md](docs/architecture.md).
66
+
67
+ ## Security model
68
+
69
+ Read-only, least privilege, no `sudo`, no `shell=True`, absolute root-owned executable path, minimal
70
+ environment, timeouts, output and concurrency limits, redaction on by default. See
71
+ [docs/threat-model.md](docs/threat-model.md) and [docs/permissions.md](docs/permissions.md).
72
+
73
+ ## Supported platforms
74
+
75
+ Linux only. Fedora is the development platform; Fedora, RHEL, CentOS Stream, Rocky, AlmaLinux,
76
+ Ubuntu, Debian, Arch and openSUSE are recognized and receive install hints. *Recognized* does not
77
+ mean *tested*: only Fedora has been exercised, and only up to the point of an inactive daemon.
78
+ USBGuard versions differ between distributions; this was developed against USBGuard 1.1.4.
79
+
80
+ ## Requirements
81
+
82
+ Python 3.11+, USBGuard installed and its daemon running (not needed for `--mock`). USBGuard-MCP never
83
+ installs or configures USBGuard for you.
84
+
85
+ ## Installation
86
+
87
+ ```bash
88
+ uvx usbguard-mcp --version # run without installing (needs uv)
89
+ pip install usbguard-mcp # or install into an environment
90
+ usbguard-mcp --mock # try synthetic data, no USBGuard needed
91
+ ```
92
+
93
+ From a checkout: `uv sync && uv run usbguard-mcp`. See [docs/installation.md](docs/installation.md).
94
+
95
+ ## Quick start
96
+
97
+ 1. Try it on synthetic data: `uvx usbguard-mcp --mock` (an MCP client speaks to it over STDIO).
98
+ 2. For real data, ask an administrator for the minimal read-only ACL in
99
+ [docs/permissions.md](docs/permissions.md), then run `uvx usbguard-mcp` and call
100
+ `check_permissions`.
101
+
102
+ ## MCP client configuration
103
+
104
+ | Client | Status (live, real daemon, 2026-12-06) |
105
+ |---|---|
106
+ | Claude Code | **Tested**: tool call returned the real USBGuard version |
107
+ | Codex CLI | **Tested**: same |
108
+ | OpenCode | **Tested** with caveats: use a project-level `opencode.json`; small models may need the server named in the prompt |
109
+ | MCP Inspector (CLI) | **Tested**: `tools/list`, `tools/call` |
110
+ | Claude Desktop, VS Code | Config shape follows vendor docs; **untested** |
111
+
112
+ Examples live in [`examples/`](examples/). For instance, with a local checkout:
113
+
114
+ ```bash
115
+ claude mcp add --scope user --transport stdio usbguard -- uvx usbguard-mcp
116
+ codex mcp add usbguard -- uvx usbguard-mcp
117
+ ```
118
+
119
+ ## Tools
120
+
121
+ Full generated reference: [docs/tools.md](docs/tools.md).
122
+
123
+ | Group | Tools |
124
+ |---|---|
125
+ | System | `get_usbguard_version`, `get_system_status`, `check_permissions`, `get_server_info`, `health_check` |
126
+ | Devices | `list_usb_devices`, `list_allowed_devices`, `list_blocked_devices`, `list_rejected_devices`, `inspect_usb_device`, `search_usb_devices`, `get_device_statistics`, `classify_usb_device`, `compare_usb_devices` |
127
+ | Policy | `list_policy_rules`, `inspect_policy_rule`, `analyze_security_policy`, `detect_duplicate_rules`, `detect_permissive_rules`, `detect_policy_conflicts`, `recommend_policy_improvements`, `preview_policy_rule` |
128
+ | Security | `assess_usb_security`, `detect_suspicious_interfaces`, `detect_composite_devices`, `analyze_device_risk`, `detect_identity_changes`, `generate_security_report` |
129
+ | Monitoring | `collect_usb_events`, `get_recent_usb_events`, `get_event_statistics`, `analyze_usb_activity` |
130
+
131
+ Resources: `usbguard://system/status`, `usbguard://devices`, `usbguard://policies`,
132
+ `usbguard://events/recent`, `usbguard://security/summary`, `usbguard://documentation`.
133
+ Prompts: `audit_usb_security`, `investigate_usb_device`, `review_usb_policy`,
134
+ `analyze_recent_usb_activity`, `generate_usb_security_report`.
135
+
136
+ ## Example prompts
137
+
138
+ - "List all authorized USB devices."
139
+ - "Are there any blocked USB devices?"
140
+ - "Audit my current USB authorization policy."
141
+ - "Explain which devices expose multiple USB interfaces."
142
+ - "Review my USBGuard configuration for security weaknesses."
143
+ - "Generate a USB security assessment report."
144
+
145
+ Example (synthetic data) from `detect_policy_conflicts`:
146
+
147
+ ```json
148
+ {"code": "CONFLICTING_RULE", "severity": "medium", "confidence": "high",
149
+ "summary": "A later rule has a different target but is unreachable: an earlier unconditional rule always matches first.",
150
+ "evidence": {"earlier_rule_id": 1, "later_rule_id": 3, "earlier_target": "allow", "later_target": "block"},
151
+ "limitations": ["Rule conditions (if ...) are evaluated by the daemon at runtime, not here.", "..."],
152
+ "remediation": "Decide which target is intended, then fix the rule order or remove the unreachable rule."}
153
+ ```
154
+
155
+ ## Troubleshooting
156
+
157
+ See [docs/troubleshooting.md](docs/troubleshooting.md). Start with `check_permissions`.
158
+
159
+ ## Known limitations
160
+
161
+ - Validated on one laptop, one USBGuard version (1.1.4) and one distribution (Fedora); parsers are
162
+ strict and report `PARSE_ERROR` rather than guessing, so other versions may need fixes.
163
+ - Event timestamps are collection time, not occurrence time (USBGuard `watch` gives none); only
164
+ order is reliable. USBGuard device IDs change when a device is replugged.
165
+ - `ImplicitPolicyTarget` and other settings in `usbguard-daemon.conf` are root-only and not read.
166
+ - Rule conditions (`if ...`) are not evaluated. Policy analysis is sound but incomplete.
167
+ - Rejected/disconnected devices are only visible via the live list or events seen this session.
168
+ - Events come from a bounded in-memory buffer; there is no historical journal source.
169
+ - No native IPC backend, HTTP transport or write operations.
170
+
171
+ ## Roadmap
172
+
173
+ 0.x: more distributions and USBGuard versions, MCP Registry submission. Later,
174
+ each needing its own security review: native IPC backend, persistent local event storage,
175
+ authenticated localhost HTTP, carefully controlled administrative functions. None may weaken the
176
+ read-only default.
177
+
178
+ ## Contributing
179
+
180
+ See [CONTRIBUTING.md](CONTRIBUTING.md). Report vulnerabilities per [SECURITY.md](SECURITY.md).
181
+
182
+ ## License and acknowledgment
183
+
184
+ MIT, see [LICENSE](LICENSE) and [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md). Thanks to the
185
+ [USBGuard](https://github.com/USBGuard/usbguard) project (GPL-2.0-or-later). This project invokes
186
+ its CLI and copies none of its code.
@@ -0,0 +1,14 @@
1
+ # Security policy
2
+
3
+ ## Supported versions
4
+ Only the latest 0.x release receives security fixes.
5
+
6
+ ## Reporting a vulnerability
7
+ Please report privately through GitHub's *Private vulnerability reporting* on the repository
8
+ (Security tab → "Report a vulnerability"). Do not open public issues for vulnerabilities. Expect an
9
+ acknowledgement within a few days; this is a volunteer project and no SLA is offered.
10
+
11
+ ## Scope and guarantees
12
+ USBGuard-MCP 0.1.0 is **read-only by design**. Anything that lets a tool authorize, block, reject,
13
+ or change USBGuard devices/policy, run arbitrary commands, escalate privileges, or leak redacted
14
+ identifiers is in scope. See `docs/threat-model.md`.
@@ -0,0 +1,42 @@
1
+ # Third-party notices
2
+
3
+ USBGuard-MCP is MIT-licensed and independent of the USBGuard project. It is not affiliated with
4
+ or endorsed by USBGuard.
5
+
6
+ ## USBGuard (GPL-2.0-or-later)
7
+ USBGuard-MCP **does not copy, link or vendor any USBGuard source code**. It only executes the
8
+ separately installed `usbguard` command-line program as a subprocess and parses its text output.
9
+ The format knowledge comes from USBGuard's public documentation (`usbguard-rules.conf(5)`,
10
+ `usbguard-daemon.conf(5)`, `usbguard(1)`). USBGuard: <https://github.com/USBGuard/usbguard>.
11
+
12
+ ## Python dependencies: audit of 2026-12-06
13
+
14
+ Scope: the 63 packages in the locked **runtime** closure (`uv export --no-dev`, from `uv.lock`),
15
+ licence taken from each installed package's metadata. 61 were inspected; two are platform-specific
16
+ and were not installed on the audit host (`pywin32`, Windows only; `httpx2-jsfetch`, browser/WASM
17
+ only), so their licences are **not verified**.
18
+
19
+ | Licence (SPDX as declared) | Packages |
20
+ |---|---|
21
+ | MIT / MIT-0 | annotated-types, anyio, attrs, beartype, cachetools, cffi, docstring_parser, exceptiongroup, h11, jsonref, jsonschema, jsonschema-specifications, markdown-it-py, mcp, mcp-types, mdurl, openapi-pydantic, platformdirs, pydantic, pydantic-settings, pydantic_core, PyJWT, PyYAML, referencing, rich, rich-rst, rpds-py, truststore, typing-inspection, uncalled-for, watchfiles |
22
+ | Apache-2.0 | aiofile, caio, cyclopts, fastmcp, fastmcp-slim, jsonschema-path, opentelemetry-api, pathable, py-key-value-aio, python-multipart |
23
+ | BSD-2/3-Clause, BSD | click, httpcore2, httpx2, idna, joserfc, pycparser, Pygments, pyperclip, python-dotenv, sse-starlette, starlette, uvicorn, websockets |
24
+ | Dual: Apache-2.0 OR BSD-3-Clause / OR BSD-2-Clause | cryptography, packaging |
25
+ | ISC | dnspython, griffelib |
26
+ | PSF-2.0 | typing_extensions |
27
+ | Unlicense | email-validator |
28
+ | **MPL-2.0** (weak, file-level copyleft) | certifi |
29
+
30
+ Findings:
31
+ - **No GPL, LGPL or AGPL** package is in the runtime closure.
32
+ - **certifi (MPL-2.0)** is the only copyleft-style licence. It is used unmodified; if you ever bundle
33
+ it (container, PyInstaller, zipapp), keep its licence text and make its source available, and do
34
+ not modify its files without publishing those changes.
35
+ - Apache-2.0 packages require their licence and NOTICE files when redistributed in a bundle.
36
+ - **USBGuard-MCP's own wheel/sdist bundle none of these**; pip/uv installs them from PyPI, so
37
+ publishing the wheel creates no redistribution obligation beyond this project's MIT licence.
38
+ Obligations apply only if you ship a bundled artifact (then include each licence text).
39
+ - Dev-only tools (pytest, ruff, mypy, pip-audit, coverage) are not shipped and were not audited.
40
+ - Licence strings come from package metadata, which can be wrong or missing (markdown-it-py,
41
+ mdurl and exceptiongroup declare MIT only via classifiers). This is a metadata audit, not legal
42
+ advice; re-run it at every release because `uv.lock` changes.
@@ -0,0 +1,32 @@
1
+ # Architecture
2
+
3
+ ```text
4
+ MCP client ─STDIO─▶ FastMCP ─▶ tools/resources/prompts ─▶ validation ─▶ services ─▶ redaction
5
+ │
6
+ Backend (protocol)
7
+ ┌──────────────┴──────────────┐
8
+ CLI backend Mock backend
9
+ command_guard + bounded subprocess (explicit opt-in)
10
+ │
11
+ usbguard CLI ─▶ usbguard daemon (source of truth)
12
+ ```
13
+
14
+ | Layer | Modules | Responsibility |
15
+ |---|---|---|
16
+ | Entry | `cli.py`, `server.py` | STDIO only, stdout = protocol, stderr = logs |
17
+ | MCP surface | `tools/`, `resources.py`, `prompts.py` | Typed inputs, annotations, uniform envelope (`tools/_common.py`) |
18
+ | Validation | `security/validation.py` | Re-validates every user-controlled argument |
19
+ | Services | `services/` | Pure logic: device, policy, audit (heuristics), events, system diagnostics |
20
+ | Parsing | `parsers/` | USBGuard rule/device/event text → Pydantic models; strict, raises `PARSE_ERROR` |
21
+ | Redaction | `security/redaction.py` | Sanitizes untrusted text; hides identifiers by default |
22
+ | Backends | `backends/` | `Operation` enum is the complete command allow-list |
23
+ | Execution | `security/command_guard.py`, `backends/cli.py` | Path resolution/ownership checks; bounded async subprocess |
24
+
25
+ Design choices:
26
+ - Services are pure functions over parsed models, so they are testable without USBGuard.
27
+ - `parsers/rules.py` handles both `list-devices` and `list-rules` (USBGuard prints both as rules);
28
+ there is no separate device parser.
29
+ - The CLI backend is the only real backend. A native IPC backend is future work; it would need its
30
+ own security design.
31
+ - Errors are values: `IntegrationError(code)` becomes `{"success": false, "error": {...}}`.
32
+ Unexpected exceptions become `INTERNAL_ERROR` with details only in the stderr log.
@@ -0,0 +1,28 @@
1
+ # Configuration
2
+
3
+ All settings are optional environment variables (see `.env.example`); the CLI flag `--mock`
4
+ overrides the backend.
5
+
6
+ | Variable | Default | Meaning |
7
+ |---|---|---|
8
+ | `USBGUARD_MCP_BACKEND` | `cli` | `cli` = real USBGuard, `mock` = synthetic data. Never chosen automatically. |
9
+ | `USBGUARD_MCP_SCENARIO` | `normal` | Mock scenario: `normal`, `empty`, `malformed`, `disconnected`, `unavailable`, `denied`, `variations`. |
10
+ | `USBGUARD_MCP_REDACT_SERIALS` | `true` | Hide device serial numbers (and `serial` in rules). |
11
+ | `USBGUARD_MCP_REDACT_HASHES` | `true` | Hide device and parent hashes. |
12
+ | `USBGUARD_MCP_REDACT_PORTS` | `true` | Hide physical port (`via-port`). |
13
+ | `USBGUARD_MCP_REDACT_HOST_IDS` | `true` | Hide host-specific identifiers (connect type, parent hash). |
14
+ | `USBGUARD_MCP_REDACT_AUDIT` | `true` | Hide rule IDs in events, rule `label` and `if` conditions. |
15
+
16
+ Invalid values stop the server at startup with a clear message. Logs go to stderr
17
+ (`--log-level DEBUG|INFO|WARNING|ERROR`). Because redaction applies to output, secrets used for
18
+ comparison (serial/hash in `detect_identity_changes`) are compared internally and never echoed.
19
+
20
+ Vendor ID, product ID, USBGuard device ID and interface triples are **not** redacted: they are
21
+ needed for analysis and are not unique to one unit.
22
+
23
+ ## Mock scenarios
24
+
25
+ Every mock response carries `"synthetic": true` and a warning. Scenarios: authorized storage, blocked
26
+ device, keyboard, mouse, internal camera, Bluetooth, composite device, missing serial, unusual
27
+ combination (`normal`); `empty`; `malformed`; `disconnected` (a device disappears); `unavailable`
28
+ (daemon down); `denied` (no permission); `variations` (tab/CRLF formatting).
@@ -0,0 +1,30 @@
1
+ # Development
2
+
3
+ ```bash
4
+ uv sync
5
+ uv run ruff check . && uv run ruff format --check . && uv run mypy
6
+ uv run pytest --cov # fails below 90% coverage
7
+ uv run python scripts/gen_tools_doc.py
8
+ uv run python scripts/smoke_stdio.py .venv/bin/usbguard-mcp
9
+ uv run pip-audit
10
+ ```
11
+
12
+ The normal suite needs no USBGuard, root or hardware: parsing uses fixtures, subprocess behaviour
13
+ uses fake executables, MCP behaviour uses the in-memory and STDIO clients with the mock backend.
14
+
15
+ ## Optional hardware validation (not run by CI, not yet performed)
16
+
17
+ The `hardware` pytest marker is reserved for opt-in tests against the real daemon. Safe procedure:
18
+
19
+ 1. Have an administrator grant the read-only ACL from [permissions](permissions.md) to your user.
20
+ 2. Run `uv run usbguard-mcp`, call `check_permissions` and `health_check`.
21
+ 3. Compare `list_usb_devices` with `usbguard list-devices` by eye. Do not run any modifying command.
22
+ 4. For events: call `collect_usb_events` and plug **one spare pen drive** into a port you do not
23
+ depend on. Never test on the only keyboard or mouse; never change policy while testing.
24
+ 5. Report any `PARSE_ERROR` with redacted output.
25
+
26
+ ## Verification status
27
+ See the README table. Facts verified against upstream: rule language, default `equals` operator,
28
+ first-match-wins, IPC ACL privileges, `add-user` flags (man pages on Fedora, USBGuard 1.1.4), and
29
+ `watch` output (upstream `IPCSignalWatcher.cpp`). Not verified: behaviour on a live daemon,
30
+ other distributions, other USBGuard versions.