Principia Robotica
World-first unified MCP gateway + Control Barrier Function (CBF-QP) safety engine for agentic robotics, enabling LLMs to control physical robots with mathematically proven safety.
README
<div align="center">
โก PRINCIPIA ROBOTICA
Lex Prima: Safety is not a feature. It is the law.
World-first unified MCP gateway + Control Barrier Function (CBF-QP) safety engine for agentic robotics.
Give Claude, GPT-4o, Gemini, and any LLM mathematically proven safe control over physical robots via ROS2.
Created: July 29, 2026 ยท Author: Abdullah Bin Zafar ยท UET Lahore, Pakistan
"What Newton's Principia was to classical mechanics, this is to the laws governing AI-controlled machines."
</div>
๐ Why This Exists (The Problem)
Every AI agent โ Claude, GPT-4o, Gemini โ can be told to drive a robot. But none of them can mathematically guarantee safety. They can crash, over-accelerate, collide, or violate physical limits. There was no open-source bridge between LLM intelligence and control-theoretic safety.
Until now.
๐ฌ What This Is (The World-First Innovation)
| Existing World | After Principia Robotica |
|---|---|
AI sends cmd_vel. Robot may crash. |
AI's command is intercepted by CBF-QP solver. Safe command issued. |
| No formal safety guarantee | โtโฅ0: h(x(t)) โฅ 0 โ mathematically proven forward invariance |
| Safety = hope + testing | Safety = formal theorem (Ames et al., proven 2017) |
| MCP server = passive sensor reader | MCP server = active safety enforcer |
| Pre-simulation not available | Certify trajectory BEFORE hardware moves |
๐ The Mathematics (The Core Law)
The CBF-QP Safety Filter solves this problem in real-time (< 1ms in Python):
$$u^* = \arg\min_{u \in \mathcal{U}} \frac{1}{2}|u - u_{\text{AI}}|^2$$
$$\text{subject to:} \quad L_f h(x) + L_g h(x) u \geq -\alpha(h(x))$$
Where:
- $u_{\text{AI}}$ = command from AI agent (Claude, GPT-4o, Gemini...)
- $h(x)$ = Control Barrier Function (encodes obstacle avoidance)
- $L_f h$, $L_g h$ = Lie derivatives along robot dynamics
- $\alpha: \mathbb{R} \to \mathbb{R}$ = class-K function (typically $\alpha(h) = \gamma h$)
Result: The robot receives the closest safe command to what the AI wanted. Formally proven optimal by KKT conditions.
๐๏ธ Architecture
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ AI Agent (Claude / GPT-4o / Gemini) โ
โ Tools: cbf_filter_velocity, โ
โ predict_safe_trajectory, โ
โ lyapunov_stability_check, ... โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ MCP stdio (JSON-RPC 2.0)
โโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ PRINCIPIA ROBOTICA MCP SERVER โ
โ โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโ โ
โ โ CBF-QP Solver โ โ Kinematic World โ โ Lyapunov โ โ
โ โ (CVXPY + OSQP) โ โ Model (1000Hz) โ โ Stability โ โ
โ โ < 1ms Python โ โ Trajectory Sim โ โ Analyzer โ โ
โ โโโโโโโโโโฌโโโโโโโโโโ โโโโโโโโโโฌโโโโโโโโโโ โโโโโโโฌโโโโโโโ โ
โ โโโโโโโโโโโโโโโโโโโโโโฌโโ โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ROS2 Bridge (optional)
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ROS2 Middleware โ /cmd_vel_raw โ [CBF] โ /cmd_vel โ
โ โ /odom (live state) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ PHYSICAL ROBOT (TurtleBot4 / Spot / Custom) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐จ Interactive Web Visualizer Dashboard (1-Second Demo)
Launch the 60 FPS live HTML5 Canvas + Web GL simulator in your browser with a single command:
python -m principia.server --ui
- Live Obstacle Field: Drag and drop circular obstacles in real-time.
- AI Command Interceptor: Adjust AI linear/angular velocity sliders to observe real-time CBF-QP minimal perturbation filtering.
- Multi-Mode Support: Unicycle differential drive, Quadrotor 2D altitude floor, Swarm fleet inter-robot collision check, and Dynamic moving obstacles.
- Visual Telemetry: Real-time $h(x)$ safety gauge, Lyapunov energy $V(x)$ plot, and predicted trajectory path.
๐ ๏ธ 1-Second Installation & Setup
git clone https://github.com/EngineerAbdullahBinZafar/principia-robotica
cd principia-robotica
pip install -e .
Verify everything works instantly:
python -m principia.server --doctor
โก Quickstart (3 Commands)
1. Run the MCP server:
python -m principia.server
2. Test the CBF filter directly in Python:
import numpy as np
from principia.cbf_engine import (
DifferentialDriveModel, CircularObstacleCBF, CBFQPSafetyFilter
)
robot = DifferentialDriveModel()
obstacle = CircularObstacleCBF(obstacle_x=2.0, obstacle_y=0.0, obstacle_radius=0.5)
safety_filter = CBFQPSafetyFilter(
robot=robot,
cbfs=[obstacle],
u_min=np.array([-0.5, -2.5]),
u_max=np.array([1.5, 2.5]),
)
# Robot at (1.2, 0) heading toward obstacle at (2.0, 0)
x = np.array([1.2, 0.0, 0.0])
u_ai = np.array([1.5, 0.0]) # AI wants full speed ahead
result = safety_filter.solve(x, u_ai)
print(f"AI proposed: v={u_ai[0]:.2f} m/s")
print(f"Safe command: v={result['u_safe'][0]:.2f} m/s")
print(f"Was modified: {result['was_modified']}")
print(f"Solve time: {result['solve_time_ms']:.2f} ms")
3. Add to your AI client (Claude Desktop / Cursor):
Add this to your claude_desktop_config.json:
{
"mcpServers": {
"principia-robotica": {
"command": "python",
"args": ["-m", "principia.server"],
"cwd": "/path/to/principia-robotica"
}
}
}
๐ง MCP Tools (13 Total)
| Tool | Type | Description |
|---|---|---|
cbf_filter_velocity |
๐ด WORLD-FIRST | Real-time CBF-QP safety filter for velocity commands |
predict_safe_trajectory |
๐ด WORLD-FIRST | Pre-simulate + certify trajectory before hardware moves |
lyapunov_stability_check |
๐ด WORLD-FIRST | Real-time Lyapunov stability verification |
cbf_quadrotor_altitude |
๐ด WORLD-FIRST | CBF altitude safety for quadrotors |
minimal_perturbation_proof |
๐ด WORLD-FIRST | Formal KKT proof of minimum-norm safety correction |
clf_cbf_qp_solver |
๐ด WORLD-FIRST | Unified Control Lyapunov + Control Barrier QP solver |
swarm_cbf_fleet_safety |
๐ด WORLD-FIRST | Multi-robot swarm inter-agent collision & barrier check |
dynamic_obstacle_cbf |
๐ด WORLD-FIRST | CBF safety filter for dynamic moving obstacles |
get_cbf_spatial_map |
๐ด WORLD-FIRST | ASCII radar safety map of surrounding obstacle space |
batch_cbf_filter |
๐ด WORLD-FIRST | Parallel batch CBF velocity filter for multi-robot fleets |
get_cbf_safety_report |
โ | Full safety audit of current robot state |
principia_status |
โ | Server status and capability report |
principia_benchmark |
โ | QP solver performance benchmark |
๐งฌ File Structure
principia-robotica/
โโโ principia/
โ โโโ __init__.py # Package metadata
โ โโโ server.py # MCP JSON-RPC 2.0 server (entry point)
โ โโโ cbf_engine.py # CBF-QP solver + robot models
โ โโโ world_model.py # Kinematic trajectory predictor + Lyapunov
โ โโโ tools.py # MCP tool handler functions
โ โโโ ros2_bridge.py # ROS2 live integration (optional)
โ โโโ theory_and_proofs.py # Complete mathematical derivations
โโโ tests/
โ โโโ test_principia.py # Full test suite
โโโ docs/
โ โโโ THEORY.md # LaTeX-formatted mathematical proofs
โโโ mcp_config.json # MCP client configuration template
โโโ pyproject.toml # Package metadata + build config
โโโ LICENSE # MIT + attribution clause
โโโ README.md # This file
๐ Performance Benchmarks
| Operation | Time | Frequency |
|---|---|---|
| CBF margin evaluation | < 0.01 ms | > 100,000 Hz |
| Early exit (safe command) | < 0.05 ms | > 20,000 Hz |
| CBF-QP solve (CVXPY/OSQP) | < 2 ms | > 500 Hz |
| Trajectory pre-simulation (3s horizon) | < 5 ms | > 200 Hz |
| Lyapunov check | < 0.1 ms | > 10,000 Hz |
Real-time control loops run at 50โ200 Hz. This engine operates at > 500 Hz. โ
๐ Mathematical References
-
Ames, A.D., Xu, X., Grizzle, J.W., Tabuada, P. (2017). Control barrier function based quadratic programs for safety critical systems. IEEE Transactions on Automatic Control, 62(8), 3861โ3876.
-
Ames, A.D., Coogan, S., Egerstedt, M., et al. (2019). Control barrier functions: Theory and applications. European Control Conference.
-
Zafar, A.B. (2026). Principia Robotica: World-first unified MCP+CBF-QP agentic safety engine. GitHub. https://github.com/EngineerAbdullahBinZafar/principia-robotica
๐ค Author
Abdullah Bin Zafar
UET Lahore, Pakistan
GitHub: @EngineerAbdullahBinZafar
If this helped you, please โญ the repo. It costs you nothing and means everything.
<div align="center">
ยฉ 2026 Abdullah Bin Zafar โ MIT License
All derivative works must attribute the original author.
</div>
Recommended Servers
playwright-mcp
A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.
Magic Component Platform (MCP)
An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.
Audiense Insights MCP Server
Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.
VeyraX MCP
Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.
graphlit-mcp-server
The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.
Kagi MCP Server
An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.
E2B
Using MCP to run code via e2b.
Neon Database
MCP server for interacting with Neon Management API and databases
Exa Search
A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.
Qdrant Server
This repository is an example of how to create a MCP server for Qdrant, a vector search engine.