Metadata-Version: 2.5
Name: mudraid-sdk
Version: 1.1.0
Summary: Python SDK for MudraID — drop-in replacement for `requests` that adds transparent agent authentication and per-platform token routing.
Project-URL: Homepage, https://github.com/MudraID/mudraid-sdk-python
Project-URL: Documentation, https://docs.mudraid.ai
Project-URL: Repository, https://github.com/MudraID/mudraid-sdk-python
Project-URL: Issues, https://github.com/MudraID/mudraid-sdk-python/issues
Author: MudraID
License:                                  Apache License
                                   Version 2.0, January 2004
                                http://www.apache.org/licenses/
        
           TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
        
           1. Definitions.
        
              "License" shall mean the terms and conditions for use, reproduction,
              and distribution as defined by Sections 1 through 9 of this document.
        
              "Licensor" shall mean the copyright owner or entity authorized by
              the copyright owner that is granting the License.
        
              "Legal Entity" shall mean the union of the acting entity and all
              other entities that control, are controlled by, or are under common
              control with that entity. For the purposes of this definition,
              "control" means (i) the power, direct or indirect, to cause the
              direction or management of such entity, whether by contract or
              otherwise, or (ii) ownership of fifty percent (50%) or more of the
              outstanding shares, or (iii) beneficial ownership of such entity.
        
              "You" (or "Your") shall mean an individual or Legal Entity
              exercising permissions granted by this License.
        
              "Source" form shall mean the preferred form for making modifications,
              including but not limited to software source code, documentation
              source, and configuration files.
        
              "Object" form shall mean any form resulting from mechanical
              transformation or translation of a Source form, including but
              not limited to compiled object code, generated documentation,
              and conversions to other media types.
        
              "Work" shall mean the work of authorship, whether in Source or
              Object form, made available under the License, as indicated by a
              copyright notice that is included in or attached to the work
              (an example is provided in the Appendix below).
        
              "Derivative Works" shall mean any work, whether in Source or Object
              form, that is based on (or derived from) the Work and for which the
              editorial revisions, annotations, elaborations, or other modifications
              represent, as a whole, an original work of authorship. For the purposes
              of this License, Derivative Works shall not include works that remain
              separable from, or merely link (or bind by name) to the interfaces of,
              the Work and Derivative Works thereof.
        
              "Contribution" shall mean any work of authorship, including
              the original version of the Work and any modifications or additions
              to that Work or Derivative Works thereof, that is intentionally
              submitted to Licensor for inclusion in the Work by the copyright owner
              or by an individual or Legal Entity authorized to submit on behalf of
              the copyright owner. For the purposes of this definition, "submitted"
              means any form of electronic, verbal, or written communication sent
              to the Licensor or its representatives, including but not limited to
              communication on electronic mailing lists, source code control systems,
              and issue tracking systems that are managed by, or on behalf of, the
              Licensor for the purpose of discussing and improving the Work, but
              excluding communication that is conspicuously marked or otherwise
              designated in writing by the copyright owner as "Not a Contribution."
        
              "Contributor" shall mean Licensor and any individual or Legal Entity
              on behalf of whom a Contribution has been received by Licensor and
              subsequently incorporated within the Work.
        
           2. Grant of Copyright License. Subject to the terms and conditions of
              this License, each Contributor hereby grants to You a perpetual,
              worldwide, non-exclusive, no-charge, royalty-free, irrevocable
              copyright license to reproduce, prepare Derivative Works of,
              publicly display, publicly perform, sublicense, and distribute the
              Work and such Derivative Works in Source or Object form.
        
           3. Grant of Patent License. Subject to the terms and conditions of
              this License, each Contributor hereby grants to You a perpetual,
              worldwide, non-exclusive, no-charge, royalty-free, irrevocable
              (except as stated in this section) patent license to make, have made,
              use, offer to sell, sell, import, and otherwise transfer the Work,
              where such license applies only to those patent claims licensable
              by such Contributor that are necessarily infringed by their
              Contribution(s) alone or by combination of their Contribution(s)
              with the Work to which such Contribution(s) was submitted. If You
              institute patent litigation against any entity (including a
              cross-claim or counterclaim in a lawsuit) alleging that the Work
              or a Contribution incorporated within the Work constitutes direct
              or contributory patent infringement, then any patent licenses
              granted to You under this License for that Work shall terminate
              as of the date such litigation is filed.
        
           4. Redistribution. You may reproduce and distribute copies of the
              Work or Derivative Works thereof in any medium, with or without
              modifications, and in Source or Object form, provided that You
              meet the following conditions:
        
              (a) You must give any other recipients of the Work or
                  Derivative Works a copy of this License; and
        
              (b) You must cause any modified files to carry prominent notices
                  stating that You changed the files; and
        
              (c) You must retain, in the Source form of any Derivative Works
                  that You distribute, all copyright, patent, trademark, and
                  attribution notices from the Source form of the Work,
                  excluding those notices that do not pertain to any part of
                  the Derivative Works; and
        
              (d) If the Work includes a "NOTICE" text file as part of its
                  distribution, then any Derivative Works that You distribute must
                  include a readable copy of the attribution notices contained
                  within such NOTICE file, excluding those notices that do not
                  pertain to any part of the Derivative Works, in at least one
                  of the following places: within a NOTICE text file distributed
                  as part of the Derivative Works; within the Source form or
                  documentation, if provided along with the Derivative Works; or,
                  within a display generated by the Derivative Works, if and
                  wherever such third-party notices normally appear. The contents
                  of the NOTICE file are for informational purposes only and
                  do not modify the License. You may add Your own attribution
                  notices within Derivative Works that You distribute, alongside
                  or as an addendum to the NOTICE text from the Work, provided
                  that such additional attribution notices cannot be construed
                  as modifying the License.
        
              You may add Your own copyright statement to Your modifications and
              may provide additional or different license terms and conditions
              for use, reproduction, or distribution of Your modifications, or
              for any such Derivative Works as a whole, provided Your use,
              reproduction, and distribution of the Work otherwise complies with
              the conditions stated in this License.
        
           5. Submission of Contributions. Unless You explicitly state otherwise,
              any Contribution intentionally submitted for inclusion in the Work
              by You to the Licensor shall be under the terms and conditions of
              this License, without any additional terms or conditions.
              Notwithstanding the above, nothing herein shall supersede or modify
              the terms of any separate license agreement you may have executed
              with Licensor regarding such Contributions.
        
           6. Trademarks. This License does not grant permission to use the trade
              names, trademarks, service marks, or product names of the Licensor,
              except as required for describing the origin of the Work and
              reproducing the content of the NOTICE file.
        
           7. Disclaimer of Warranty. Unless required by applicable law or
              agreed to in writing, Licensor provides the Work (and each
              Contributor provides its Contributions) on an "AS IS" BASIS,
              WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
              implied, including, without limitation, any warranties or conditions
              of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
              PARTICULAR PURPOSE. You are solely responsible for determining the
              appropriateness of using or redistributing the Work and assume any
              risks associated with Your exercise of permissions under this License.
        
           8. Limitation of Liability. In no event and under no legal theory,
              whether in tort (including negligence), contract, or otherwise,
              unless required by applicable law (such as deliberate and grossly
              negligent acts) or agreed to in writing, shall any Contributor be
              liable to You for damages, including any direct, indirect, special,
              incidental, or consequential damages of any character arising as a
              result of this License or out of the use or inability to use the
              Work (including but not limited to damages for loss of goodwill,
              work stoppage, computer failure or malfunction, or any and all
              other commercial damages or losses), even if such Contributor
              has been advised of the possibility of such damages.
        
           9. Accepting Warranty or Support. While redistributing the Work or
              Derivative Works thereof, You may choose to offer, and charge a
              fee for, acceptance of support, warranty, indemnity, or other
              liability obligations and/or rights consistent with this License.
              However, in accepting such obligations, You may act only on Your
              own behalf and on Your sole responsibility, not on behalf of any
              other Contributor, and only if You agree to indemnify, defend,
              and hold each Contributor harmless for any liability incurred by,
              or claims asserted against, such Contributor by reason of your
              accepting any such warranty or support.
        
           END OF TERMS AND CONDITIONS
        
           APPENDIX: How to apply the Apache License to your work.
        
              To apply the Apache License to your work, attach the following
              boilerplate notice, with the fields enclosed by brackets "[]"
              replaced with your own identifying information. (Don't include
              the brackets!)  The text should be enclosed in the appropriate
              comment syntax for the file format. We also recommend that a
              file or class name and description of purpose be included on the
              same "printed page" as the copyright notice for easier
              identification within third-party archives.
        
           Copyright 2026 MudraID
        
           Licensed under the Apache License, Version 2.0 (the "License");
           you may not use this file except in compliance with the License.
           You may obtain a copy of the License at
        
               http://www.apache.org/licenses/LICENSE-2.0
        
           Unless required by applicable law or agreed to in writing, software
           distributed under the License is distributed on an "AS IS" BASIS,
           WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
           See the License for the specific language governing permissions and
           limitations under the License.
License-File: LICENSE
Keywords: agent,ai-agents,authentication,jwt,mudraid,trust-layer
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Security
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Python: >=3.10
Requires-Dist: python-dotenv<2,>=1.0
Requires-Dist: requests<3,>=2.31
Provides-Extra: dev
Requires-Dist: pytest-asyncio<2,>=1.0; extra == 'dev'
Requires-Dist: pytest-cov<8,>=7; extra == 'dev'
Requires-Dist: pytest<10,>=9.0.3; extra == 'dev'
Requires-Dist: responses<1,>=0.25; extra == 'dev'
Requires-Dist: tomli<3,>=2; (python_version < '3.11') and extra == 'dev'
Provides-Extra: v2
Requires-Dist: cryptography>=50.0.0; extra == 'v2'
Requires-Dist: pyjwt<3,>=2.13.0; extra == 'v2'
Description-Content-Type: text/markdown

# mudraid-sdk

Python SDK for [MudraID](https://mudraid.ai) — a trust layer for AI agents.

A **drop-in replacement for `requests`** that adds transparent agent
authentication via MudraID-issued short-lived JWTs. Every call your
agent makes is automatically signed, refreshed on expiry, and routed
to the right platform — with no decorators, no boilerplate, no
secrets in code.

```python
# Before
import requests
response = requests.get("https://api.skyscanner.com/flights")

# After
from mudraid import Agent
agent = Agent()  # auto-loads credentials from .env
response = agent.get("https://api.skyscanner.com/flights")
```

That's the entire integration diff.

### Which client to use

There are two authentication profiles, and the source recommends the newer one
for new integrations:

| | `Agent` (legacy) | `MachineAgent` (V2) |
|---|---|---|
| Authenticates with | an `api_key_id` + shared `secret` | a `private_key_jwt` client assertion (RFC 7523) — no shared secret on the wire |
| Scope of an issued token | the agent's **full** permitted set when none is named | exactly the scopes named; omitting them requests the **empty** set |
| Bound to a resource | no | yes, via an RFC 8707 resource indicator |
| Retry semantics | consequence-safe | consequence-safe (identical) |

`Agent` remains fully supported and is what the rest of this README shows,
because it is the shorter diff. **New integrations should prefer
`MachineAgent`** — a compromised platform cannot
replay a client assertion the way it can replay a shared secret, and an omitted
scope set cannot silently widen to everything.

The legacy profile will be deprecated with a runtime warning and a migration
window before any removal; `Agent.legacy()` names the choice explicitly at the
call site if you want it pinned.

---

## What the SDK actually does

For every outgoing request the SDK:

1. **Resolves** the URL's host to a MudraID `platform_id`
   (bootstrapped once at first use from `POST /auth/agents/me/platforms`).
2. **Mints** a short-lived JWT for that platform via `POST /auth/token`
   (cached in process memory until ~30 s before expiry).
3. **Attaches** `Authorization: Bearer <jwt>` to the request.
4. **Forwards** to `requests` and returns the response unmodified.

On 401 from the upstream platform, the SDK refreshes the JWT and retries
once **only when the retry is consequence-safe** — a GET, PUT, DELETE or
other idempotent method, or any request you have given an
`idempotency_key`. Token expiry in mid-flight is invisible to your code
for those. An unkeyed POST or PATCH is **not** replayed: the 401 is
returned to you, because a platform that performs the write and *then*
answers 401 would otherwise be charged, shipped or booked twice. See
[Retries never duplicate a consequential action](#retries-never-duplicate-a-consequential-action).

Two HTTP `Session` instances are held internally: one for MudraID,
one for outgoing platform calls. They never share connections — your
agent secret can never accidentally leak onto a platform-bound
request, by construction.

---

## Installation

```bash
pip install mudraid-sdk
```

Requires Python 3.10+.

> **1.1.0 is not on PyPI yet.** Install from source until the first
> upload lands:
> ```bash
> pip install -e sdks/mudraid-sdk-python  # from the repo root
> ```

---

## Quickstart

### 1. Register an agent on MudraID

Sign in to the MudraID portal (or call the API), create a new agent,
and grant it the platforms + scopes it needs. You'll receive **once**:

- `MUDRAID_API_KEY_ID` — public identifier (safe to log)
- `MUDRAID_SECRET` — private credential (never logged, never persisted)

### 2. Drop the credentials into your project's `.env`

```env
MUDRAID_API_KEY_ID=muid_kid_a3f8e9c1d2b4f5e6a7b8c9d0e1f2a3b4
MUDRAID_SECRET=muid_sk_...your-secret...
```

Add `.env` to your `.gitignore`. The SDK loads it automatically;
explicit OS-env values override `.env`; explicit kwargs override
both.

### 3. Replace `requests` with `Agent`

```python
from mudraid import Agent

agent = Agent()  # reads .env
response = agent.get("https://api.skyscanner.com/flights/search")
response.raise_for_status()
flights = response.json()
```

That's it. The SDK is now handling authentication for every call.

---

## Full request surface

`Agent` mirrors `requests.Session`:

```python
agent.get(url, **kwargs)        # GET
agent.head(url, **kwargs)       # HEAD
agent.options(url, **kwargs)    # OPTIONS

agent.post(url, idempotency_key=None, **kwargs)     # POST
agent.patch(url, idempotency_key=None, **kwargs)    # PATCH
agent.put(url, idempotency_key=None, **kwargs)      # PUT
agent.delete(url, idempotency_key=None, **kwargs)   # DELETE
```

All `requests` kwargs work: `params`, `json`, `data`, `headers`,
`timeout`, `stream`, `files`, etc. Anything you pass survives through
to the underlying `requests` call.

Returns a real `requests.Response` — the SDK is a thin auth shim, not
a client framework.

### Retries never duplicate a consequential action

The SDK retries in two places: after a platform `401` (refresh the token, replay)
and after a transport failure. Neither will replay a request whose replay could
duplicate a side effect.

| Method | Replayed on 401 / ambiguous failure? |
|---|---|
| `GET` `HEAD` `OPTIONS` `PUT` `DELETE` | Yes — replaying cannot duplicate an effect |
| `POST` `PATCH` **with** `idempotency_key` | Yes — the server collapses the duplicate |
| `POST` `PATCH` **without** a key | **No** |

Without a key, a `401` is returned to you unreplayed, and an ambiguous transport
failure raises `MudraIDExecutionUnknownError`. You are the only party that knows
whether the action is safe to repeat.

```python
# A payment that must not happen twice.
r = agent.post(f"{API}/payments", json={"amount": 5000},
               idempotency_key=f"pay-{order_id}")

# Without the key, a platform that mutates and THEN answers 401 hands you the
# 401 instead of silently charging the customer a second time.
r = agent.post(f"{API}/payments", json={"amount": 5000})
if r.status_code == 401:
    ...  # reconcile, then re-issue with a key
```

Use a key that is stable for the *action*, not for the attempt — a fresh UUID per
call deduplicates nothing. `MachineAgent` applies the identical rule with the
identical signature.

---

## Error handling

Every SDK-raised exception inherits from `MudraIDError`. Catch the
base class to handle anything SDK-originated:

```python
from mudraid import Agent, MudraIDError

agent = Agent()
try:
    response = agent.get("https://api.skyscanner.com/flights")
except MudraIDError as exc:
    log.warning("SDK rejected the call: %s", exc)
```

Or catch specific subclasses for precise behaviour:

| Exception | Raised when |
|---|---|
| `MudraIDConfigError` | `MUDRAID_API_KEY_ID` / `MUDRAID_SECRET` missing or empty |
| `MudraIDAuthError` | MudraID rejected the credentials (HTTP 401 from `/auth/token`) |
| `MudraIDRevokedError` | Authorisation denied (HTTP 403: agent inactive, no platform access, scope rejected) |
| `MudraIDNetworkError` | Could not reach MudraID, response was malformed, or an unmapped non-2xx (the message names the path and `base_url`) |
| `MudraIDRateLimitedError` | MudraID rate-limited the call (HTTP 429). Subclasses `MudraIDNetworkError`; carries `retry_after_seconds` |
| `MudraIDPlatformNotRegisteredError` | Caller URL's host isn't a platform this agent is registered with |

### Rate limiting

Control-plane calls pass through **layered** rate limiting. A 429 can come from
any of these, and they are not the same limit with the same remedy:

| Layer | Keyed by | What it means |
|---|---|---|
| Per-key budget | your `api_key_id` | Token minting, credential verification and the platform bootstrap draw on **one shared counter for that key** — spending it on any of the three spends it for all. Sized so a brute-force attempt against one key burns that key's budget. |
| Account fair use | your **account** | The plan's ceiling, or an abuse block. Belongs to the whole account, so throttling one agent may not clear it. |
| Gateway limit | caller **source** | The gateway meters the whole control-plane surface by where the request came from — so a busy neighbour behind the same egress address can spend it, and your key may be nowhere near its own budget. |

All three raise `MudraIDRateLimitedError`.

**How the SDK tells them apart.** identity-service labels its own refusals with
a machine-readable `error_code`, and the gateway stamps `RateLimit-*` headers on
what it meters. The body's label wins whenever it is present — the gateway adds
those headers to responses it merely *forwards*, so reading them first would
blame the gateway for every application-level refusal in the system.

**The SDK names a specific layer only when the response says which one refused.**
Otherwise the message says the layer is unknown and lists the candidates. That is
deliberate: a confident wrong attribution would send you to throttle one agent
over a limit that was never that agent's, which is worse than saying "slow down,
cause unclear". Slowing down is the correct first move whichever layer answered.

The SDK does **not** sleep and retry for you either: a wait of up to a minute
inside a library call is indistinguishable from a hang, and the bootstrap this
most often guards runs once at start-up. You get the server's own delay and
decide:

```python
from mudraid import Agent, MudraIDRateLimitedError
import time

try:
    response = agent.get("https://api.example.com/things")
except MudraIDRateLimitedError as exc:
    time.sleep(exc.retry_after_seconds or 60)
```

`retry_after_seconds` is `None` when the server sent no `Retry-After` header —
an absence, never a guessed number, so you can tell "wait this long" from
"wait, length unknown".

**A 429 is not a credential failure.** The credentials were never examined; the
call simply arrived too fast.

---

**Platform-side 4xx / 5xx responses are NOT raised** — they come back
as ordinary `Response` objects so your code can decide what to do.
The single exception is 401, and only when replaying is
consequence-safe: the SDK refreshes the token and retries once for
idempotent methods, or for any request carrying an `idempotency_key`.
An unkeyed POST or PATCH is surfaced to you unreplayed.

---

## Configuration reference

| Setting | Env var | Constructor kwarg | Default |
|---|---|---|---|
| Public API key id | `MUDRAID_API_KEY_ID` | `api_key_id=` | (required) |
| Secret | `MUDRAID_SECRET` | `secret=` | (required) |
| MudraID base URL | `MUDRAID_BASE_URL` | `base_url=` | `https://api.mudraid.ai` |

Precedence: **kwarg > OS env > `.env` file**.

Anything else (timeouts, cache TTLs) is internal and stable in v1.

### `MUDRAID_BASE_URL` must be `https://`

The token and bootstrap calls send `{"api_key_id", "secret"}` as a JSON **body** —
your agent's long-lived credential, in the clear, on every mint. Over `http://`
that is readable by every hop in between, and nothing later can recover from it:
the secret is spent the moment it is sent.

So a cleartext URL to a remote host raises `MudraIDConfigError` at construction,
before anything is transmitted. `http://` is permitted **only** for a loopback
host, which is the local-development case:

```python
Agent(base_url="http://localhost:8001")   # fine — never leaves the machine
Agent(base_url="http://api.mudraid.ai")   # MudraIDConfigError
```

### Control-plane calls do not follow redirects

`requests` follows redirects by default and **replays the body** on 307/308. It
strips a cross-host `Authorization` header; it has no equivalent for a body,
because in general it cannot know one is sensitive. This one is.

So the SDK sets `allow_redirects=False` on every call that carries a credential —
the two control-plane paths and the V2 token endpoint — and surfaces a 3xx as
`MudraIDNetworkError`. Neither path redirects on a MudraID deployment; in
practice a 3xx here means `MUDRAID_BASE_URL` resolves to a proxy, a login portal
or a vanity domain rather than the API. The `Location` value is deliberately not
echoed into the error message.

Calls to *your platforms* are unaffected — those go through a separate session
and follow redirects as `requests` normally would.

### `PyJWTSigner` signs asymmetrically or not at all

`private_key_jwt` exists so no shared secret crosses the wire. PyJWT will
nonetheless encode `alg=none` (unsigned) or `HS256` (symmetric — your "private
key" becomes a secret the server must also hold), either of which dissolves the
profile without changing anything visible at the call site. `PyJWTSigner`
therefore accepts only `RS*`, `PS*`, `ES*` and `EdDSA`, and raises `ValueError`
otherwise. Supply your own `AssertionSigner` if you genuinely need something
else — there the choice is yours and it is visible.

---

## Diagnostics

Turn on the SDK's structured logs to watch the trust loop in action:

```python
import logging
logging.getLogger("mudraid").setLevel(logging.DEBUG)
```

Module loggers:

- `mudraid.agent` — request lifecycle and 401 retries
- `mudraid.token_manager` — cache hits, mints, refreshes
- `mudraid.platform_resolver` — bootstrap, host→platform_id resolution
- `mudraid.http` — outbound MudraID requests
- `mudraid.env` — `.env` loading

**No log statement in the SDK ever emits a secret or a JWT** — locked
by anti-leak unit tests that sweep the entire DEBUG output for
sentinel credential strings.

---

## Try it locally

A runnable sample agent ships in [`examples/sample-agent/`](../../examples/sample-agent/).
It calls the sample platform (running the matching FastAPI middleware)
and walks through the trust loop step by step.

```bash
# From the repo root:
docker compose up -d                                       # backend
docker compose --profile samples up -d sample-platform     # platform

cd examples/sample-agent
cp .env.example .env                                       # add real creds
python -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt
pip install -e ../../sdks/mudraid-sdk-python

python main.py
```

Expected output is documented in [`examples/sample-agent/README.md`](../../examples/sample-agent/README.md)
— a four-section walk through the public route, scope-gated read,
scope-gated write (403 by design), and unregistered host
(`MudraIDPlatformNotRegisteredError` by design).

---

## How the SDK fits into the wider system

```
your agent code              your platform's server
─────────────────            ────────────────────────
agent.get(url) ────┐         ┌──── @app.get("/items")
                   ▼         ▲
              ┌─────────┐    │
              │  SDK    │    │   ┌────────────────┐
              │ bootstrap│   │   │ MudraIDMiddleware │
              │ token   │    │   │  in your app      │
              │ refresh │    │   └────────────────┘
              └─────────┘    │           ▲
                   │         │           │
                   ▼         │           │
              ┌─────────────────────────────┐
              │     MudraID backend          │
              │  identity / platforms / KMS  │
              └─────────────────────────────┘
```

The matching server-side package is
[`mudraid-middleware`](../mudraid-middleware-python/) for FastAPI /
Starlette platforms.

---

## License

Apache 2.0 — see [LICENSE](LICENSE).
