Skip to main content

mtg — Morphological Type Guards

Part of the Mizan stack — the Arabic-first reliability scale for AI agents.

A JSON Schema extension for multilingual tool-call arguments.

Installed as mtg-guards on PyPI, imported as mtg: pip install mtg-guards then import mtg.

License: Apache 2.0 Status: v0.1.0 advisory Python 3.10+

Tool-call parameters today are typed as string. Too weak for multilingual agents. MTG adds linguistic constraints on the span, validates pre-call and post-call, and emits structured violation receipts.

Why

Run a Gulf Arabic instruction through a frontier model and watch half the tool arguments come back transliterated — الرياض becomes Riyadh, أبي أحجز collapses to MSA. There is no type today that says "this parameter must stay in Arabic, in Gulf register, with preserved morphology." MTG is that type.

MTG is agent-framework-agnostic. Hermes is one adapter among several (OpenAI, Anthropic, Hermes-Function-Calling). The primitive generalizes to any morphologically rich language — Hebrew, Amharic, Tigrinya, Turkish, Persian.

Status

v0.1.0 — advisory mode only. Violations are logged; calls are never blocked. reconciled and enforced modes are defined in the spec but not enabled in the reference implementation. Data first, policy later.

Install

# From source (not yet on PyPI)
git clone https://github.com/Moshe-ship/mtg.git
cd mtg
pip install -e .

# With optional CAMeL Tools morphology backend
pip install -e ".[morph]"

Quickstart

import json
from mtg.adapters.openai import guard_tool

tool = json.load(open("examples/book_service.json"))
wrapped = guard_tool(tool)

# Validate an inbound call
result = wrapped.validate_call({
    "name": "book_service",
    "arguments": {
        "intent_phrase": "أبي أحجز",      # Gulf Arabic
        "service_id": "svc-42",
    },
})
for violation in result.violations:
    print(violation.code, violation.severity, violation.message)

Architecture

Tool definition (JSON Schema + x-mtg extension)
         │
         ▼
    Adapter  (openai / anthropic / hermes_fc)
         │
         ▼
    Pipeline  (pre-call + post-call)
         │
         ├─▶ Script backend      (Unicode range checks, pure-Python)
         ├─▶ Translit backend    (Arabizi heuristics)
         ├─▶ Dialect backend     (CAMeL DID if available; keyword fallback)
         ├─▶ Morph backend       (CAMeL Tools if available; fallback otherwise)
         └─▶ Canonical transforms (lemma / root+pattern / normalized)
         │
         ▼
    Receipt  (hash-chained, ToolProof-compatible)

The x-mtg extension

Any JSON Schema string property can carry an x-mtg block:

{
  "type": "string",
  "x-mtg": {
    "slot_type": "inflected_request_form",
    "script": "ar",
    "dialect_expected": "gulf",
    "dialect_enforcement": "preserve",
    "transliteration_allowed": false,
    "morphologically_productive": true,
    "canonicalization": "root_pattern",
    "mode": "advisory",
    "post_call_contract": ["script_match", "dialect_preserve"]
  }
}

Full spec: spec/taxonomy.md · spec/violations.md · spec/resolution.md · mtg/mtg.schema.json

Violation taxonomy

Code Phase Severity
SCRIPT_VIOLATION pre high
TRANSLITERATION_VIOLATION pre high
DIALECT_DRIFT pre medium
DIALECT_FLATTEN post medium
MORPH_CANONICALIZATION_FAILURE pre low
MORPH_AMBIGUITY pre low
BACKEND_DISAGREEMENT pre info
SURFACE_CORRUPTION_POST_CALL post high
ROOT_DRIFT post medium
FREE_TEXT_OVERFLOW pre medium
CANONICALIZATION_REQUIRED pre high

CLI

# Validate a tool's x-mtg annotations
mtg check-schema examples/book_service.json

# Validate an inbound call against a tool
mtg validate examples/book_service.json examples/sample_call.json

# Verify a receipt chain
mtg receipt-verify ~/.mtg/chain.ndjson

Programmatic evaluation (mtg.eval)

The evaluation harness runs MTG guards against an arabic-agent-eval-style JSONL dataset and aggregates per-dialect violation rates. Use it to measure MTG's ship-ready violation frequency before committing to a benchmark number.

from pathlib import Path
from mtg import GuardSpec
from mtg.eval import run_on_jsonl, ALL_ARMS

guard_map = {
    "intent_phrase": GuardSpec.from_dict({
        "slot_type": "inflected_request_form",
        "script": "ar",
        "dialect_expected": "gulf",
        "morphologically_productive": True,
        "mode": "advisory",
    }),
}

report = run_on_jsonl(Path("datasets/mtg_slots_v1.jsonl"), guard_map)
print(report.violation_counts)            # Counter({'DIALECT_DRIFT': 3, ...})
print(report.dialect_violation_rates)     # {'gulf': 0.0, 'egy': 0.33, ...}

# Experimental arms for the research program (spec/resolution.md)
for arm in ALL_ARMS:
    print(arm.name, arm.description)

Exports: run_on_jsonl, ItemReport, AggregateReport, Condition, ARM_A/B/C/D, ALL_ARMS. All are re-exported from mtg.eval.

Limitations (v0.1.0)

  • Advisory only. No enforcement. Don't use MTG as a security control — use it as a diagnostic.
  • CAMeL Tools is optional. Without it, morphological analysis falls back to surface-only with BACKEND_DISAGREEMENT info annotations. Install mtg-guards[morph] for full analysis.
  • Dialect classifier is keyword-based by default. Accuracy on short tool-call inputs is limited; receipts always include dialect_confidence so downstream consumers can filter low-confidence cases (<0.75).
  • Arabic-first. Other languages are supported in the schema (script: "he", "fa", etc.) but the reference validator is tuned for Arabic. Other-language backends are welcome PRs.
  • Not fine-tuned. MTG v0.1.0 is a prompt-time instrument. It does not modify models or training data.

Related projects

  • arabic-agent-eval — 51-item Arabic function-calling benchmark with dialect splits. The primary diagnostic substrate for MTG.
  • ToolProof — agent tool-call verification. MTG violations convert to ToolProof receipts via toolproof.mtg_bridge.from_mtg_violation.
  • artok — Arabic token cost calculator across 18 tokenizers. Provides the future-work basis for MTG compression claims.
  • NousResearch/Hermes-Function-Calling — target adapter landing spot upstream.
  • NousResearch/atropos — community environment for MTG-guarded RL rollouts.

Citation

@software{mtg_2026,
  title = {Morphological Type Guards: A JSON Schema extension for multilingual tool-call arguments},
  author = {Abumazin, Mousa},
  year = {2026},
  url = {https://github.com/Moshe-ship/mtg}
}

License

Apache-2.0. See LICENSE.

Metadata

Release files for mtg-guards 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mtg-guards 0.1.0
File Size Uploaded Provenance Actions
mtg_guards-0.1.0.tar.gz 78.6 kB With provenance Details for mtg_guards-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mtg-guards 0.1.0
File Interpreter ABI Platform Actions
mtg_guards-0.1.0-py3-none-any.whl Python 3 none any Details for mtg_guards-0.1.0-py3-none-any.whl

Total release size: 144.6 kB

Release files / mtg_guards-0.1.0.tar.gz

Download URL mtg_guards-0.1.0.tar.gz
Size 78.6 kB
Tags Source
SHA-256 checksum
How to use checksums
c3e84fea0d4f731aba4de7e2c1ae55788d3b0cfe046be53bb330cb469ab8cf98
BLAKE2b-256 checksum
How to use checksums
65e352394b7a625dd947a2c1bddae36801046e8fb470427a10e68217dfdde923
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 1, 2026.

Transparency log

Release files / mtg_guards-0.1.0-py3-none-any.whl

Download URL mtg_guards-0.1.0-py3-none-any.whl
Size 66.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
60437416fc9aa46ea7dd8f5c2108b3f3e76b69ee38869aad9d6fb5297d14c1f5
BLAKE2b-256 checksum
How to use checksums
94ba096edbe5c9c0559376e2303f91a3d1652c8321ec6b7f99eede45a2f576aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 1, 2026.

Transparency log

Security for mtg-guards 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for mtg_guards-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 1, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page