mjguard 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.
@@ -0,0 +1,5 @@
1
+ .venv
2
+ .git
3
+ docs/media
4
+ dist
5
+ **/__pycache__
@@ -0,0 +1,17 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: github-actions
4
+ directory: /
5
+ schedule:
6
+ interval: monthly
7
+ - package-ecosystem: uv
8
+ directory: /
9
+ schedule:
10
+ interval: monthly
11
+ ignore:
12
+ # mcp 2.x changes the server API; upgrade by hand.
13
+ - dependency-name: "mcp"
14
+ update-types: ["version-update:semver-major"]
15
+ groups:
16
+ python:
17
+ patterns: ["*"]
@@ -0,0 +1,38 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+
8
+ permissions:
9
+ contents: read
10
+
11
+ jobs:
12
+ check:
13
+ runs-on: ${{ matrix.os }}
14
+ strategy:
15
+ fail-fast: false
16
+ matrix:
17
+ os: [ubuntu-latest]
18
+ python: ["3.11", "3.12", "3.13"]
19
+ include:
20
+ - os: macos-latest
21
+ python: "3.13"
22
+ steps:
23
+ - uses: actions/checkout@v7
24
+ - uses: astral-sh/setup-uv@v10.4.0
25
+ with:
26
+ python-version: ${{ matrix.python }}
27
+ enable-cache: true
28
+ - run: uv sync --locked
29
+ - name: Lint
30
+ run: |
31
+ uv run ruff check .
32
+ uv run ruff format --check .
33
+ - name: Type check (pyright strict)
34
+ run: uv run pyright
35
+ - name: Tests, including UR5e and Panda from MuJoCo Menagerie
36
+ run: uv run pytest -q --cov=mjguard --cov-report=term-missing --cov-fail-under=90
37
+ - name: Build
38
+ run: uv build
@@ -0,0 +1,41 @@
1
+ name: Release
2
+
3
+ # Runs only when a version tag (v*) is pushed. Publishing uses PyPI trusted publishing: no token is stored here.
4
+ # One-time setup on pypi.org: add a pending publisher for this repository, workflow release.yml, environment pypi.
5
+ on:
6
+ push:
7
+ tags: ["v*"]
8
+
9
+ permissions:
10
+ contents: read
11
+
12
+ jobs:
13
+ build:
14
+ runs-on: ubuntu-latest
15
+ steps:
16
+ - uses: actions/checkout@v7
17
+ - uses: astral-sh/setup-uv@v10.4.0
18
+ with:
19
+ python-version: "3.13"
20
+ - run: uv sync --locked
21
+ - run: uv run pytest -q
22
+ - name: Tag matches package version
23
+ run: test "${GITHUB_REF_NAME#v}" = "$(uv run python -c 'import mjguard; print(mjguard.__version__)')"
24
+ - run: uv build
25
+ - uses: actions/upload-artifact@v5
26
+ with:
27
+ name: dist
28
+ path: dist/
29
+
30
+ publish:
31
+ needs: build
32
+ runs-on: ubuntu-latest
33
+ environment: pypi
34
+ permissions:
35
+ id-token: write
36
+ steps:
37
+ - uses: actions/download-artifact@v5
38
+ with:
39
+ name: dist
40
+ path: dist/
41
+ - uses: pypa/gh-action-pypi-publish@release/v1
@@ -0,0 +1,15 @@
1
+ .venv/
2
+ __pycache__/
3
+ *.pyc
4
+ .pytest_cache/
5
+ .hypothesis/
6
+ .ruff_cache/
7
+ .coverage
8
+ htmlcov/
9
+ dist/
10
+ build/
11
+ traces/
12
+ *.jsonl
13
+ # private planning notes
14
+ /DESIGN.md
15
+ .claude/
@@ -0,0 +1,15 @@
1
+ # Changelog
2
+
3
+ ## 0.1.0 (2026-11-25)
4
+
5
+ - Guard for any MuJoCo-modelled arm from a small TOML config: joint limits with margin, MuJoCo contacts, floor
6
+ clearance of every moving collision geom (meshes included), optional workspace box, IK convergence check.
7
+ - Refusals with reason codes and a checked-safe suggestion that re-plans deterministically.
8
+ - Floor clearance has a hard limit and a planning margin (10 mm by default); the monitor halts on the measured arm,
9
+ during the motion and after the servos settle. Pinned by a physics test on UR5e and Panda.
10
+ - Executor: one motion at a time, software stop, runtime monitor on measured joints, fault refusals, trace.
11
+ - Suggestions carry the `tool_down` they were checked with.
12
+ - Config loading never writes to stdout (the Menagerie downloader prints), so stdio stays a clean protocol.
13
+ - MuJoCo simulator transport: physics with the model's own position servos, or kinematic.
14
+ - MCP server (`mjguard serve`) and config checker (`mjguard check`).
15
+ - Presets: UR5e and Franka Panda from MuJoCo Menagerie, downloaded on first use.
@@ -0,0 +1,13 @@
1
+ # Runs the guarded UR5e from MuJoCo Menagerie over stdio. Used by MCP directories to start and inspect the server.
2
+ FROM python:3.13-slim
3
+ ENV PIP_NO_CACHE_DIR=1 PYTHONUNBUFFERED=1
4
+ RUN apt-get update && apt-get install -y --no-install-recommends git && rm -rf /var/lib/apt/lists/*
5
+ RUN useradd --create-home guard
6
+ WORKDIR /app
7
+ COPY pyproject.toml README.md LICENSE ./
8
+ COPY src ./src
9
+ RUN pip install --no-compile .
10
+ USER guard
11
+ # Download the Menagerie model at build time, so the server starts in about a second.
12
+ RUN mjguard check ur5e > /dev/null
13
+ ENTRYPOINT ["mjguard", "serve", "ur5e"]
mjguard-0.1.0/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
mjguard-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,158 @@
1
+ Metadata-Version: 2.5
2
+ Name: mjguard
3
+ Version: 0.1.0
4
+ Summary: Let LLM agents drive any MuJoCo-modelled robot arm over MCP, with every motion checked in the model before it runs.
5
+ Project-URL: Repository, https://github.com/SamKo88927/mjguard
6
+ Author: Sam Ko
7
+ License-Expression: Apache-2.0
8
+ License-File: LICENSE
9
+ Keywords: agents,llm,mcp,mujoco,robot-arm,robotics,safety
10
+ Classifier: Development Status :: 3 - Alpha
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: Intended Audience :: Science/Research
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
15
+ Requires-Python: >=3.11
16
+ Requires-Dist: mcp<2,>=1.30
17
+ Requires-Dist: mujoco>=3.2
18
+ Requires-Dist: numpy>=1.26
19
+ Requires-Dist: pydantic>=2.7
20
+ Requires-Dist: robot-descriptions>=1.15
21
+ Provides-Extra: menagerie
22
+ Description-Content-Type: text/markdown
23
+
24
+ # mjguard
25
+
26
+ [![PyPI](https://img.shields.io/pypi/v/mjguard)](https://pypi.org/project/mjguard/)
27
+ [![CI](https://github.com/SamKo88927/mjguard/actions/workflows/ci.yml/badge.svg)](https://github.com/SamKo88927/mjguard/actions/workflows/ci.yml)
28
+ [![License](https://img.shields.io/badge/license-Apache--2.0-blue)](LICENSE)
29
+
30
+ <!-- mcp-name: io.github.SamKo88927/mjguard -->
31
+
32
+ Let an LLM agent drive **any MuJoCo-modelled robot arm** over [MCP](https://modelcontextprotocol.io), with every
33
+ motion checked in the model before it runs.
34
+
35
+ Language models refuse unsafe requests in chat, then carry them out when they hold a robot's tools: refusal falls
36
+ from 73% of text requests to 18-25% once the model controls the arm
37
+ (["Refuses in Chat, Complies with Arms"](https://arxiv.org/abs/2610.14043), Sun et al., 2026). mjguard puts the check below the model, where a prompt cannot
38
+ reach it: the agent asks for a pose, mjguard plans it, checks every waypoint against the robot's own MuJoCo model,
39
+ and either moves or returns a refusal the model can act on.
40
+
41
+ ```sh
42
+ uvx mjguard check panda # first run downloads MuJoCo Menagerie (about a minute)
43
+ uvx mjguard serve panda # MCP over stdio; or ur5e, or your own robot.toml
44
+ ```
45
+
46
+ <p align="center">
47
+ <img src="docs/media/demo.gif" alt="A simulated Franka Panda moves to a checked target; a move into the table and an out-of-reach move are refused with a suggested safe point, and the arm then follows the suggestion" width="576">
48
+ </p>
49
+
50
+ <sub>Franka Panda from MuJoCo Menagerie, driven through the same executor and guard the MCP server uses
51
+ (`scripts/record_demo.py`). A target into the table and one out of reach are refused; the refusal carries the
52
+ farthest checked-safe point, and the next call follows it.</sub>
53
+
54
+ ## What it checks
55
+
56
+ Every motion is planned into joint-space waypoints, and **every waypoint** is checked before anything moves:
57
+
58
+ | Check | Refusal code |
59
+ | --- | --- |
60
+ | Joint ranges from the model, minus a margin | `JOINT_LIMIT` |
61
+ | Contacts between moving links and the rest of the scene, from MuJoCo's own collision pipeline | `COLLISION`, `TABLE_COLLISION` |
62
+ | Distance from every moving collision geom (meshes included) and the tool point to the floor or table, with a planning margin above the hard limit | `TABLE_CLEARANCE` |
63
+ | An optional workspace box for the tool point | `WORKSPACE` |
64
+ | IK convergence: a miss beyond 1 mm or 1.5° is a refusal, never a "close enough" plan | `UNREACHABLE` |
65
+
66
+ While the motion runs, and again after the servos settle, a monitor checks what the robot **actually** did and
67
+ halts it (`ENVELOPE_BREACH`) if the measured arm crosses the hard limit or touches anything. Plans keep a margin
68
+ (10 mm by default) above that limit: under physics, the UR5e and Panda presets settle about 6 mm below their plan,
69
+ and a test pins that they stay above the hard limit.
70
+
71
+ ## Refusals a model can use
72
+
73
+ ```json
74
+ {
75
+ "ok": false,
76
+ "refusal": {
77
+ "code": "UNREACHABLE",
78
+ "message": "no joint solution reaches the target; the closest misses by 2032.0 mm and 40.7 deg",
79
+ "suggestion": {"x_mm": 890.7, "y_mm": 125.3, "z_mm": 277.5,
80
+ "note": "farthest checked-safe point on the straight line toward the target (19% of the way)"}
81
+ }
82
+ }
83
+ ```
84
+
85
+ The suggestion is the farthest point toward the target that passed every check, rounded the way a model will send
86
+ it back, and re-planned deterministically, so following it (with the `tool_down` it names) is accepted. A property test sends random targets and
87
+ follows every suggestion to keep it that way.
88
+
89
+ ## Your own robot
90
+
91
+ Write a small TOML file next to your MJCF:
92
+
93
+ ```toml
94
+ name = "my_arm"
95
+
96
+ [model]
97
+ mjcf = "scene.xml" # or "menagerie:<name>"
98
+ floor_geom = "floor" # a plane; clearance is measured against it
99
+ keyframe = "home" # start and rest pose
100
+
101
+ [arm]
102
+ joints = ["joint1", "joint2", "joint3", "joint4", "joint5", "joint6"]
103
+ tcp_site = "tool" # or tcp_body = "hand" with tcp_offset_m = [0, 0, 0.1]
104
+ tool_axis = "z" # the axis that points out of the tool
105
+ actuators = ["a1", "a2", "a3", "a4", "a5", "a6"] # position servos: simulate with physics; omit for kinematic
106
+
107
+ [limits]
108
+ min_clearance_mm = 20 # hard limit, enforced on the measured arm
109
+ planning_margin_mm = 10 # plans keep 30 mm
110
+ workspace_mm = { x = [-600, 600], y = [-600, 600] }
111
+ ```
112
+
113
+ ```sh
114
+ mjguard check robot.toml # loads it, checks the rest pose, prints the envelope
115
+ mjguard serve robot.toml # MCP over stdio
116
+ ```
117
+
118
+ Links whose moving joints all spin about a vertical axis (a turret on its base) cannot change height, so they are
119
+ exempt from the clearance check; they are still collision-checked.
120
+
121
+ ### Claude Code
122
+
123
+ ```sh
124
+ claude mcp add ur5e -- uvx mjguard serve ur5e
125
+ ```
126
+
127
+ ## Tools
128
+
129
+ `get_state`, `get_limits`, `move_to(x_mm, y_mm, z_mm, tool_down=true, speed)`, `move_joints({name: value})`,
130
+ `gripper(opening)` (when configured), `rest`, `stop`. Resources: `mjguard://limits`, `mjguard://trace/latest`.
131
+ Millimetres and degrees; z is the height above the floor plane.
132
+
133
+ ## Real hardware
134
+
135
+ mjguard drives a `Transport`: anything that can report joint positions and follow a list of checked waypoints. It
136
+ ships the MuJoCo simulator transport. A real arm needs a transport for its driver; the guard, the monitor and the
137
+ MCP surface stay the same. [roarm-mcp](https://github.com/SamKo88927/roarm-mcp) is the RoArm-M3 application this
138
+ library was extracted from.
139
+
140
+ `stop()` is a software stop. On real hardware a physical e-stop is the only real emergency stop.
141
+
142
+ ## Limits of the method
143
+
144
+ - The check is only as good as the model. Objects that are not in the MJCF scene are invisible to it.
145
+ - Joint-space straight lines between waypoints are checked at a fixed spacing (3° by default), not continuously.
146
+ - Dynamics (speed, torque, payload) are not checked; the envelope is geometric.
147
+
148
+ ## Development
149
+
150
+ ```sh
151
+ uv sync
152
+ uv run ruff check . && uv run ruff format --check . && uv run pyright && uv run pytest -q
153
+ ```
154
+
155
+ ## License
156
+
157
+ Apache-2.0. Robot models come from [MuJoCo Menagerie](https://github.com/google-deepmind/mujoco_menagerie) under
158
+ their own licenses and are downloaded at runtime, not redistributed.
@@ -0,0 +1,135 @@
1
+ # mjguard
2
+
3
+ [![PyPI](https://img.shields.io/pypi/v/mjguard)](https://pypi.org/project/mjguard/)
4
+ [![CI](https://github.com/SamKo88927/mjguard/actions/workflows/ci.yml/badge.svg)](https://github.com/SamKo88927/mjguard/actions/workflows/ci.yml)
5
+ [![License](https://img.shields.io/badge/license-Apache--2.0-blue)](LICENSE)
6
+
7
+ <!-- mcp-name: io.github.SamKo88927/mjguard -->
8
+
9
+ Let an LLM agent drive **any MuJoCo-modelled robot arm** over [MCP](https://modelcontextprotocol.io), with every
10
+ motion checked in the model before it runs.
11
+
12
+ Language models refuse unsafe requests in chat, then carry them out when they hold a robot's tools: refusal falls
13
+ from 73% of text requests to 18-25% once the model controls the arm
14
+ (["Refuses in Chat, Complies with Arms"](https://arxiv.org/abs/2610.14043), Sun et al., 2026). mjguard puts the check below the model, where a prompt cannot
15
+ reach it: the agent asks for a pose, mjguard plans it, checks every waypoint against the robot's own MuJoCo model,
16
+ and either moves or returns a refusal the model can act on.
17
+
18
+ ```sh
19
+ uvx mjguard check panda # first run downloads MuJoCo Menagerie (about a minute)
20
+ uvx mjguard serve panda # MCP over stdio; or ur5e, or your own robot.toml
21
+ ```
22
+
23
+ <p align="center">
24
+ <img src="docs/media/demo.gif" alt="A simulated Franka Panda moves to a checked target; a move into the table and an out-of-reach move are refused with a suggested safe point, and the arm then follows the suggestion" width="576">
25
+ </p>
26
+
27
+ <sub>Franka Panda from MuJoCo Menagerie, driven through the same executor and guard the MCP server uses
28
+ (`scripts/record_demo.py`). A target into the table and one out of reach are refused; the refusal carries the
29
+ farthest checked-safe point, and the next call follows it.</sub>
30
+
31
+ ## What it checks
32
+
33
+ Every motion is planned into joint-space waypoints, and **every waypoint** is checked before anything moves:
34
+
35
+ | Check | Refusal code |
36
+ | --- | --- |
37
+ | Joint ranges from the model, minus a margin | `JOINT_LIMIT` |
38
+ | Contacts between moving links and the rest of the scene, from MuJoCo's own collision pipeline | `COLLISION`, `TABLE_COLLISION` |
39
+ | Distance from every moving collision geom (meshes included) and the tool point to the floor or table, with a planning margin above the hard limit | `TABLE_CLEARANCE` |
40
+ | An optional workspace box for the tool point | `WORKSPACE` |
41
+ | IK convergence: a miss beyond 1 mm or 1.5° is a refusal, never a "close enough" plan | `UNREACHABLE` |
42
+
43
+ While the motion runs, and again after the servos settle, a monitor checks what the robot **actually** did and
44
+ halts it (`ENVELOPE_BREACH`) if the measured arm crosses the hard limit or touches anything. Plans keep a margin
45
+ (10 mm by default) above that limit: under physics, the UR5e and Panda presets settle about 6 mm below their plan,
46
+ and a test pins that they stay above the hard limit.
47
+
48
+ ## Refusals a model can use
49
+
50
+ ```json
51
+ {
52
+ "ok": false,
53
+ "refusal": {
54
+ "code": "UNREACHABLE",
55
+ "message": "no joint solution reaches the target; the closest misses by 2032.0 mm and 40.7 deg",
56
+ "suggestion": {"x_mm": 890.7, "y_mm": 125.3, "z_mm": 277.5,
57
+ "note": "farthest checked-safe point on the straight line toward the target (19% of the way)"}
58
+ }
59
+ }
60
+ ```
61
+
62
+ The suggestion is the farthest point toward the target that passed every check, rounded the way a model will send
63
+ it back, and re-planned deterministically, so following it (with the `tool_down` it names) is accepted. A property test sends random targets and
64
+ follows every suggestion to keep it that way.
65
+
66
+ ## Your own robot
67
+
68
+ Write a small TOML file next to your MJCF:
69
+
70
+ ```toml
71
+ name = "my_arm"
72
+
73
+ [model]
74
+ mjcf = "scene.xml" # or "menagerie:<name>"
75
+ floor_geom = "floor" # a plane; clearance is measured against it
76
+ keyframe = "home" # start and rest pose
77
+
78
+ [arm]
79
+ joints = ["joint1", "joint2", "joint3", "joint4", "joint5", "joint6"]
80
+ tcp_site = "tool" # or tcp_body = "hand" with tcp_offset_m = [0, 0, 0.1]
81
+ tool_axis = "z" # the axis that points out of the tool
82
+ actuators = ["a1", "a2", "a3", "a4", "a5", "a6"] # position servos: simulate with physics; omit for kinematic
83
+
84
+ [limits]
85
+ min_clearance_mm = 20 # hard limit, enforced on the measured arm
86
+ planning_margin_mm = 10 # plans keep 30 mm
87
+ workspace_mm = { x = [-600, 600], y = [-600, 600] }
88
+ ```
89
+
90
+ ```sh
91
+ mjguard check robot.toml # loads it, checks the rest pose, prints the envelope
92
+ mjguard serve robot.toml # MCP over stdio
93
+ ```
94
+
95
+ Links whose moving joints all spin about a vertical axis (a turret on its base) cannot change height, so they are
96
+ exempt from the clearance check; they are still collision-checked.
97
+
98
+ ### Claude Code
99
+
100
+ ```sh
101
+ claude mcp add ur5e -- uvx mjguard serve ur5e
102
+ ```
103
+
104
+ ## Tools
105
+
106
+ `get_state`, `get_limits`, `move_to(x_mm, y_mm, z_mm, tool_down=true, speed)`, `move_joints({name: value})`,
107
+ `gripper(opening)` (when configured), `rest`, `stop`. Resources: `mjguard://limits`, `mjguard://trace/latest`.
108
+ Millimetres and degrees; z is the height above the floor plane.
109
+
110
+ ## Real hardware
111
+
112
+ mjguard drives a `Transport`: anything that can report joint positions and follow a list of checked waypoints. It
113
+ ships the MuJoCo simulator transport. A real arm needs a transport for its driver; the guard, the monitor and the
114
+ MCP surface stay the same. [roarm-mcp](https://github.com/SamKo88927/roarm-mcp) is the RoArm-M3 application this
115
+ library was extracted from.
116
+
117
+ `stop()` is a software stop. On real hardware a physical e-stop is the only real emergency stop.
118
+
119
+ ## Limits of the method
120
+
121
+ - The check is only as good as the model. Objects that are not in the MJCF scene are invisible to it.
122
+ - Joint-space straight lines between waypoints are checked at a fixed spacing (3° by default), not continuously.
123
+ - Dynamics (speed, torque, payload) are not checked; the envelope is geometric.
124
+
125
+ ## Development
126
+
127
+ ```sh
128
+ uv sync
129
+ uv run ruff check . && uv run ruff format --check . && uv run pyright && uv run pytest -q
130
+ ```
131
+
132
+ ## License
133
+
134
+ Apache-2.0. Robot models come from [MuJoCo Menagerie](https://github.com/google-deepmind/mujoco_menagerie) under
135
+ their own licenses and are downloaded at runtime, not redistributed.
Binary file
@@ -0,0 +1,4 @@
1
+ {
2
+ "$schema": "https://glama.ai/mcp/schemas/server.json",
3
+ "maintainers": ["SamKo88927"]
4
+ }