Skip to main content

Reference implementation of the MESA specification: a semantic safety and coordination layer for AI-operated smart environments.

Project description

mesa-core

CI PyPI

mesa-core decides whether an AI agent should be allowed to act on a smart-home device, and how cautious it should be about it.

When an assistant tries to turn on a light, unlock the front door, or change the thermostat, mesa-core answers three questions: is this allowed, should the user confirm it first, and why. It ships with safe defaults (a light is fine to control automatically; unlike a lock) that you refine for each home.

It is the reference implementation of the MESA specification. It has no runtime dependencies and never talks to Home Assistant directly: you hand it device state through callback functions, and it hands you decisions.

pip install mesa-core

Quick look

from mesa_core import MesaEnforcer, ProfileStore
from mesa_core.backends import MemoryBackend

enforcer = MesaEnforcer(ProfileStore(backend=MemoryBackend()))

# An AI agent wants to unlock the front door. Should it?
result = enforcer.evaluate("lock.front_door", "lock.unlock")

result.allowed   # False
result.reason    # "Entity is prohibited by policy: lock.front_door"

How decisions work

  • Every device has a control mode: act freely, ask the user first, read-only, or never act.
  • Devices you have not configured fall back to safe defaults: lights act freely, locks and alarm panels are off-limits, everything else asks first.
  • Rules can be set for a single device, an area, or a whole device type, and are combined with a bias toward caution: the more restrictive rule wins, and anything that cannot be checked blocks rather than allows.
  • You can add finer limits (cap the speaker volume after 10pm) and time-based rules (no blinds before sunrise).

Asking the user to confirm

When a device is set to confirm first, evaluate returns a challenge instead of a yes. Show the action to the user, then call again with the token from the approved challenge.

result = enforcer.evaluate("cover.garage", "cover.open_cover")
if result.confirmation_challenge:
    # present result.confirmation_challenge to the user; once approved,
    # build a confirmation_token from it and re-submit the same call:
    result = enforcer.evaluate(
        "cover.garage", "cover.open_cover",
        confirmation_token=approved_token,
    )

Reading a device's profile

ProfileStore resolves the effective profile after inheritance and defaults. Use a file backend to persist profiles under your HA config:

from mesa_core import ProfileStore
from mesa_core.backends import JsonFileBackend

store = ProfileStore(backend=JsonFileBackend("/config/mesa/"))
profile = store.get_effective("light.living_room_ceiling")
profile.operational_boundaries.control_mode   # ControlMode.AUTONOMOUS

Exposing it to an AI agent (MCP)

Register MESA's tools into an MCP server so the agent can look up profiles and caller context for itself.

from mesa_core.mcp import register_mesa_tools

register_mesa_tools(store, adapter="fastmcp", server=app)

Adapters ship for FastMCP and the MCP Python SDK (pip install "mesa-core[fastmcp]" or "mesa-core[mcp]"). Any other framework can implement a small registration protocol.

Passing lease_manager=LeaseManager(store) also registers the advisory coordination tools (mesa_request_lease, mesa_release_lease), which let cooperating agents signal short-lived intent to each other. They are signals, not locks; see Enrichment Section 21 before relying on them.

Loading profiles shipped by integrations

An HA integration can ship a mesa_profile.json describing its own devices. Load it with:

from mesa_core import import_from_integration

profile = import_from_integration("/config/custom_components/my_integration")
if profile is not None:
    store.set_integration_profile(profile.entity_id, profile)

Documentation

Status

mesa-core v1.1 is ready for use: profile storage and inheritance, enforcement with confirmation, the MCP retrieval tools, privacy controls, and the advisory lease protocol are all implemented. Multi-agent lease preemption is planned for v2.

git clone https://github.com/sfox38/mesa-core
cd mesa-core
pip install -e ".[dev]"
pytest tests/ -v       # test suite
ruff check . && mypy   # lint and type check

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mesa_core-1.1.0.tar.gz (167.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mesa_core-1.1.0-py3-none-any.whl (67.6 kB view details)

Uploaded Python 3

File details

Details for the file mesa_core-1.1.0.tar.gz.

File metadata

  • Download URL: mesa_core-1.1.0.tar.gz
  • Upload date:
  • Size: 167.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for mesa_core-1.1.0.tar.gz
Algorithm Hash digest
SHA256 720c1bdff1fca13afbfceda0eebb084733aad19d0a9fc0d4cf8289674c68ece6
MD5 a2a5cb34fe224e8edd5b45c5896fa82f
BLAKE2b-256 4c4e31d94d98be8c4ddcf786d928241093a3282b2208930f63aa707cac9a5958

See more details on using hashes here.

File details

Details for the file mesa_core-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: mesa_core-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 67.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for mesa_core-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bc9cbb94566c981d3cc15cb6f96109235ec99d28d787318cd37361069f235512
MD5 1ce37d6fc8af29e601452887b1e85ba8
BLAKE2b-256 531598cbadd0d53f8aad1ebe9dd8b5ed3d1ed732e048bc96b578b8573e2438bb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page