Metadata-Version: 2.4
Name: helixid-sdk-py
Version: 5.0.0
Summary: Python SDK for HelixID -- decentralized agent identity via DID/VC.
Author: DgVerse LLP
License: Apache-2.0
Project-URL: Homepage, https://github.com/helixid/helix-sdk-py
Project-URL: Repository, https://github.com/helixid/helix-sdk-py
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: pynacl<2.0,>=1.5
Requires-Dist: cryptography>=41.0
Requires-Dist: requests>=2.31
Provides-Extra: dev
Requires-Dist: pytest>=7.4; extra == "dev"
Requires-Dist: pytest-cov>=4.1; extra == "dev"
Requires-Dist: langchain-core>=0.3; python_version >= "3.10" and extra == "dev"
Requires-Dist: crewai>=1.0; python_version >= "3.10" and extra == "dev"
Provides-Extra: mcp-middleware
Requires-Dist: mcp<3,>=2.3; python_version >= "3.10" and extra == "mcp-middleware"
Requires-Dist: jsonschema>=4.18; extra == "mcp-middleware"
Provides-Extra: redis
Requires-Dist: redis>=5.0; extra == "redis"
Provides-Extra: dynamo
Requires-Dist: boto3>=1.28; extra == "dynamo"
Provides-Extra: langchain
Requires-Dist: langchain-core>=0.3; python_version >= "3.10" and extra == "langchain"
Provides-Extra: crewai
Requires-Dist: crewai>=1.0; python_version >= "3.10" and extra == "crewai"
Provides-Extra: all
Requires-Dist: mcp<3,>=2.3; python_version >= "3.10" and extra == "all"
Requires-Dist: jsonschema>=4.18; extra == "all"
Requires-Dist: redis>=5.0; extra == "all"
Requires-Dist: boto3>=1.28; extra == "all"
Requires-Dist: langchain-core>=0.3; python_version >= "3.10" and extra == "all"
Requires-Dist: crewai>=1.0; python_version >= "3.10" and extra == "all"
Dynamic: license-file

# helixid-sdk-py

Python SDK for [HelixID](https://github.com/helixid/helixid) -- decentralized
agent identity via DID/VC standards (did:hedera, did:key, did:web), delegation,
and KYA-OS request proofs verified in-process.

This is the Python counterpart to
[`helix-sdk-js`](https://github.com/helixid/helix-sdk-js), following the same
**SDK-API-only** architecture: every
SDK, in every language, depends only on the HelixID API -- never on a shared
"core" package -- except for private-key operations that must stay local:
keygen, sign and canonical-hash, which issuers and Service Providers use to sign
with their own keys. Delegation-VC construction and status checks are API calls
made through `HelixClient`.

**Every call carries a request proof (KYA-OS story C-C).** An agent signs each
MCP request with its own Signer (`kyaos.with_request_proof()`): an
`org.kya-os/proof.v1` bound to that request's body and the service's DID, with
its delegation chain beside it in `params._meta`. A service verifies it
in-process (`helix_sdk.verifier`, `helix_mcp_middleware`). The Helix VP
(`sign_vp()`, `verify_vp()`, `VPBuilder`, `X-Helix-VP`) is gone, with no
backward compatibility.

## Documentation

Full documentation is at **[docs.helixid.dev](https://docs.helixid.dev)** — concepts,
guides, and reference. This README covers only what is specific to this repository.

| | |
|---|---|
| **Start here** | [Introduction](https://docs.helixid.dev/) |
| **Concepts** | [The Trust Stack](https://docs.helixid.dev/concepts/trust-stack) · [Two-Issuer Model](https://docs.helixid.dev/concepts/two-issuer-model) · [Delegation](https://docs.helixid.dev/concepts/delegation) · [Revocation](https://docs.helixid.dev/concepts/revocation) |
| **Get started** | [Quick Start](https://docs.helixid.dev/get-started/quick-start) · [Installation & Modes](https://docs.helixid.dev/get-started/installation-and-modes) |
| **Contributing** | [How to Contribute](https://docs.helixid.dev/contributing/how-to-contribute) · [`CONTRIBUTING.md`](CONTRIBUTING.md) |
| **Security** | [Reporting a Vulnerability](https://docs.helixid.dev/security/reporting-a-vulnerability) · [`SECURITY.md`](SECURITY.md) |

---

## Install

```bash
pip install helixid-sdk-py
```

A service that verifies calls also installs the gate:

```bash
pip install "helixid-sdk-py[mcp-middleware]"
```

Python `>=3.9` (the gate's `mcp` extra needs 3.10+). Or from source:

```bash
pip install -e .
```

## Quick example

An agent signs every MCP request it sends. The proof is bound to that
request's body and to the service's DID, and lives 60 seconds at most.

By default Helix holds the agent's key, so the agent asks Helix to sign
(`HelixCustodialSigner`). There is no private key on your side. Set two
variables, both from the console when you created the agent:
`HELIX_AGENT_DID` and `HELIX_API_KEY` (this agent's API key).

```python
import os, uuid, requests
from helix_sdk import HelixCustodialSigner, kyaos

# Reads HELIX_AGENT_DID and HELIX_API_KEY; signs at https://api.helixid.dev.
signer = HelixCustodialSigner()
signed = kyaos.with_request_proof(
    {"name": "search_books", "arguments": {"query": "dune"}},
    signer,
    audience=os.environ["TARGET_SERVICE"],
)
reply = requests.post(
    "https://your-server.example/mcp",
    json={"jsonrpc": "2.0", "id": uuid.uuid4().hex, **signed},
    headers={"accept": "application/json, text/event-stream"},
    timeout=30,
)
```

`TARGET_SERVICE` is the service's DID. Set `HELIX_API_URL` to sign at
another Helix deployment. Pass `chain=hops` to carry a delegation chain (root to leaf) beside the
proof.

### If you hold your own key

Create the agent with `helix init` (the key is generated on your machine and
only its public half is sent; this package installs the same `helix init`
command). It writes `signing-key.env` with two variables, `HELIX_AGENT_DID`
and `HELIX_SIGNING_KEY`; keep that file secret and load it where the agent
runs. Then sign with `EnvSigner`. Nothing else changes:

```python
import os
from helix_sdk import EnvSigner, kyaos

# Reads HELIX_AGENT_DID and HELIX_SIGNING_KEY, and finds the key's id in the agent's DID document.
signer = EnvSigner.from_env()
signed = kyaos.with_request_proof(call, signer, audience=os.environ["TARGET_SERVICE"])
```

### The service

The service side is one line, `HelixMcpGate` in `helix_mcp_middleware`. It
verifies each call in-process and sends audit by itself:

```python
from helix_mcp_middleware import HelixMcpGate, LocalVerifierOptions

app = HelixMcpGate(
    mcp_app,  # the MCP Streamable HTTP app
    resource="https://orders.example.com/mcp",
    verifier=LocalVerifierOptions(service_did="did:web:orders.example.com"),
    tool_scopes={"lookup_order": ["orders:read"]},
    tools={"lookup_order": {"minimum_level": "L2"}},  # default L3
)
```

`examples/request_proof_demo.py` runs the sign / verify / replay / tamper cycle offline.

### Agents written in code: the signing transport

With the official MCP client, connect through `helix_transport` and write
nothing else for HelixID:

```python
from mcp import Client
from helix_sdk import helix_transport

async with Client(helix_transport("https://orders.example/mcp")) as client:
    result = await client.call_tool("lookup_order", {"orderId": "o-1"})
```

`helix_transport(url)` is an MCP client transport, like the MCP SDK's own
`streamable_http_client(url)`, so it also works with a `ClientSession`:
`async with helix_transport(url) as (read, write), ClientSession(read, write) as session:`.

- The agent's identity comes from the environment: `HELIX_AGENT_DID` with
  `HELIX_SIGNING_KEY` or `HELIX_API_KEY`.
- The service's DID is read from its `/.well-known/mcp`, which
  `HelixMcpGate` serves. There is no `TARGET_SERVICE` to set.
- When the service says it signs its answers, each answer is checked. An
  answer whose signature does not hold is an error, never handed to your
  code.
- When the service needs a user's consent, the call returns a normal tool
  result asking the user to open the link and approve, then ask again. A
  chat agent's model shows it. The next call carries the grant.

It needs the `mcp` package (the `mcp-middleware` extra). Frameworks that only
take a URL cannot use it: they never hand over a transport.

### Acting for a user

When a service needs the user's consent it answers the call with a link to
Helix's consent page. The user chooses there how long to allow the agent
for, and signs one grant with their own key. `call_with_consent` runs that
whole loop: it checks the service's signature on the answer, hands you the
link, waits for the user, and sends the call again with the grant.

```python
import os, uuid, requests
from helix_sdk import kyaos

class Refused(Exception):
    def __init__(self, status: int) -> None:
        super().__init__(f"refused: {status}")
        self.status = status

def send(request):
    reply = requests.post(
        "https://your-server.example/mcp",
        json={"jsonrpc": "2.0", "id": uuid.uuid4().hex, **request},
        headers={"accept": "application/json, text/event-stream"},
        timeout=30,
    )
    if not reply.ok:
        raise Refused(reply.status_code)
    return reply.json()["result"]

result = kyaos.consent.call_with_consent(
    {"name": "place_order", "arguments": {"item": "tea"}},
    signer=signer,
    service_did=os.environ["TARGET_SERVICE"],
    send=send,
    resolve_did=resolve_did,  # resolves the service's DID document
    for_user="ada",           # your own label for the user: required once the service asks for consent
    on_authorization_required=lambda prompt: print(f"Open {prompt.authorization_url} to allow this"),
)
```

There is no chain file to create or keep. The grant is held in memory per
user and service, and filed at Helix under `for_user` when the agent picks it
up. After a restart the SDK reads it back from Helix, under that label, the
first time the service asks. Calls a service accepts without consent need no
`for_user`; once a service asks for user consent, a call without one raises
`ConsentFlowError` with reason `for_user_required`. A grant is never renewed: when it ends, or the user
withdraws it, the user is asked again. Pass `wait_for_user=False` to get a
result with `status == "authorization_required"` and the `authorization_url`
back at once; the next call picks the grant up after the user approved.

## Cross-language parity

The local-signing primitives (`keys.py`, `crypto/common.py`, `proof.py`) are
verified **byte-for-byte** against the same
`fixtures/golden-vectors/*.json` fixtures that `helix-sdk-js` and
`helix-core` assert against -- same private key, same payload, same
signature, same base58btc-encoded proof value, in every language. See
`tests/test_golden_vectors.py` and `fixtures/golden-vectors/README.md`.

If you regenerate the vectors in `helix-server` (`pnpm generate:golden-vectors`),
re-copy the three JSON files here too, the same way `helix-sdk-js` does.

## Local verifier (KYA-OS story F-2)

`helix_sdk.verifier.verify(proof, seams, options, chain=..., request=..., card=...)` verifies a KYA-OS request proof
(`org.kya-os/proof.v1`) and the delegation chain the request carries, in-process, from the presented artifacts and
what its seams answer (`resolve_did`, `resolve_did_keys`, `fetch_status_list`, `resolve_card`, `nonce_store`, `clock`,
`trusted_issuers`). It never reads a Helix database, and a missing seam fails closed with its reason code
(`helix_sdk.verifier.ReasonCodes`). It is the port of helix-sdk-js's `kyaos.verifier.verify`: same seams, same codes,
same results, which `tests/test_verifier.py` proves over the fixture set helix-core generates
(`fixtures/verifier/*.json`, read from the sibling checkout or `HELIX_CORE_FIXTURES_DIR`).

`helix_sdk.verifier.verify_request_proof(proof, request, seams, options)` checks the request proof alone in the exact
SPEC-ENTITY-CARD section 11.2 order (story C-C), stopping at the first broken binding: schema, `kid` of `did`, the key,
its RFC 7638 thumbprint among the DID's keys, `audience`, `requestHash`, the window (+/-5 s, at most 60 s), the nonce,
the detached JWS, the `cnf` fusion (`L3` with a token `cnf`, `L3-minus` without). `verify()` runs it first; with
`require_proof=False` a chain without a proof verifies at L2 at most, and the result names `agent_did` and
`proof_floor`. `tests/test_request_proof_fixtures.py` runs helix-core's `fixtures/kya-os-helix/request-proof.json`.

`python -m helix_sdk.conformance [vectors_dir]` runs the KYA-OS vector categories `signed-proof`, `delegation-chain`,
`status-list`, `did-key`, `did-web`, `card-proof` and `entity-card` through `helix_sdk.conformance.HelixConformanceAdapter`.

## Status lists and revocation (KYA-OS story L3-C)

`helix_sdk.verifier` reads status lists as SPEC.md §6.6 says, exactly as helix-sdk-js does:

- `read_bitstring_status_list` / `read_status_list_2021` verify the list's own proof (`eddsa-jcs-2022`, or the §6.2
  `Ed25519Signature2020`) by its issuer's `assertionMethod` key before decoding any bit; `create_status_list_fetcher`
  fetches through the host's (Safe)fetch. Unreachable, malformed, unsigned, badly signed, wrong-purpose and out-of-range
  lists read as revoked.
- `create_revocation_checker` (the `check_revocation` seam, fail-closed) applies the keeper rules and caches lists and
  verdicts in a `StatusCache` (`status_cache_ttl`, `high_privilege_scopes` at most 60 s, `max_stale_seconds`,
  `invalidate(credential_id)`); a cached read is `fresh=False`, which demotes a Card L3 request to L2. Its Redis keys and
  JSON match the JavaScript cache, so both can share one store. `check_chain_revocation` walks a chain root to leaf.
- `verify_revocation_signal` checks Helix's revocation SET (RFC 8417): a trusted issuer and its signature, one credential.
- The conformance `status-list` category runs through these production readers, both list formats.

`helix_mcp_middleware` uses them the way `@helixid/mcp-middleware` does:

- `create_local_verifier` reads every list through `create_status_list_fetcher` and a fail-closed revocation checker,
  and exposes `status_cache`. Options: `status_cache` (a `StatusCache`, or a dict of its options), `status_list_issuers`,
  `check_revocation`. The default fetch is `create_resolution_fetch`: https only, a host written as an IP must be public
  unicast, same-origin redirects, 5 s, 1 MiB. Pass your SafeFetch as `fetch` to pin the connection too.
- An agent card's own `revocation` entry is read too (SPEC-ENTITY-CARD section 10.3): a set bit refuses the call
  (`status_revoked`). The entry is the agent's identity credential's, which the agent's account issues, so the list must
  be signed by the `controller` the agent's DID document names (its account's DID), or by the agent's own DID when the
  document names no controller; any other signer refuses the call (`status_list_mismatch`). A HelixID-hosted agent needs
  no configuration here: its document names its account, and the account signs the list.
  (`helix_sdk.verifier.card_list_issuers` is that rule; a `status_list_issuers` binding you configure still decides first.)
- `RedisStatusCacheStore` shares the cache between instances (any client with redis-py's `get`/`set(px=)`/`delete`/`incr`).
- `RevocationSignalClient` / `create_revocation_signal_client(verifier, trusted_issuers, ...)` receive Helix's SETs:
  over SSE by default (one outbound connection on a background thread, reconnecting with backoff and `Last-Event-ID`),
  or pushed (RFC 8935) through `asgi_webhook()` or the framework-neutral `handle_push()` (202, or 400 `{err, description}`).
  A valid SET calls `status_cache.invalidate(credential_id)`; a bad or foreign one is ignored and reported through
  `on_signal`.

```python
from helix_mcp_middleware import RedisStatusCacheStore, create_local_verifier, create_revocation_signal_client

verifier = create_local_verifier(
    service_did="did:web:mcp.example.com",
    status_cache={"store": RedisStatusCacheStore(redis.Redis()), "status_cache_ttl": 300, "high_privilege_scopes": ["orders:admin"]},
)
signals = create_revocation_signal_client(
    verifier,
    ["did:web:api.helixid.example"],  # the Helix issuer whose SETs are honoured
    events_url="https://api.helixid.example/v1/events/revocations",
    on_signal=lambda report: audit(report.to_dict()),
)
signals.start()  # or mount signals.asgi_webhook() at the URL registered with Helix
```

## Packages in this repo

- **`helix_sdk`** -- core SDK (see Quick example above).
- **`helix_mcp_middleware`** -- Model Context Protocol integration, the same
  Phase 1 surface as `@helixid/mcp-middleware`:
  `helixid_mcp_middleware()` / `async_helixid_mcp_middleware()` gate every
  incoming tool call. A caller presents a KYA-OS request proof (MCP
  `_meta["org.kya-os/request-proof"]`, with its delegation chain at
  `_meta["org.kya-os/delegation-chain"]`), which the gate verifies in-process
  with `helix_sdk.verifier` (`verifier=LocalVerifierOptions(...)` or
  `create_local_verifier()`, story F-2; the Helix API is never asked), or an
  OAuth 2.1 bearer token. A bearer token sent with a proof is the proof's
  sender constraint (`cnf` fusion, L3). The Helix VP is no longer read.
  **The one-line HTTP gate, `HelixMcpGate`** (same names and options as
  helix-sdk-js's `helixMcpGate`), is an ASGI middleware in front of the MCP
  Streamable HTTP app (`mcp` 2.x `MCPServer.streamable_http_app(stateless_http=True,
  json_response=True)`):
  `app = HelixMcpGate(mcp_app, resource=..., verifier=..., tool_scopes=..., kyaos=...)`,
  or `app.add_middleware(HelixMcpGate, resource=..., ...)` on Starlette/FastAPI.
  It serves the RFC 9728 metadata and reads each POST to the MCP path. It
  passes every request through the gate and answers refusals with 401 / 403 / 400
  and `WWW-Authenticate` (503 with `Retry-After` when an authorization server is
  down). It runs the KYA-OS handshake, sessions, `needs_authorization` and the
  response proof, and runs the call in the caller's delegation context and
  audit call. Leave `audit` out and it sends audit by itself with
  `HELIX_API_KEY` (see [Audit](#audit-the-gate-sends-it-by-itself)); or pass
  settings, a trail of your own, or `False`. It builds the verifier's replay store from `KYAOS_NONCE_STORE`
  when you give `LocalVerifierOptions` without one. Then it replays the body to
  the app behind it. The handshake tool reads the agent its credential
  identified at `request.state.helix_mcp_authenticated_agent_did`
  (`AUTHENTICATED_AGENT_STATE_KEY`). The `[mcp-middleware]` extra installs
  `mcp>=2.3,<3`.
  Pass every JSON-RPC request through the gate (its `method`, or the
  `request` as received, plus `headers` and `clientIp`); it decides which
  need a caller. `DEFAULT_OPEN_METHODS` (`initialize`,
  `notifications/initialized`, `tools/list`) pass unchecked and unaudited,
  on purpose, so clients can see what the server offers; change them with
  `open_methods` (`[]` opens nothing). Every other method needs a verified
  caller: `resources/read` / `resources/subscribe` / `resources/unsubscribe`
  are checked against `resource_scopes` (by URI) and `prompts/get` against
  `prompt_scopes`, both deny by default (`RESOURCE_NOT_PERMITTED` /
  `PROMPT_NOT_PERMITTED`, 403); list methods and `ping` need the caller only.
  Every refusal is audited, including those of a caller the gate could not
  identify (no credential, a bad bearer, a proof naming no verified DID, a
  replay, `CREDENTIAL_CONFLICT`): `authMethod: 'none'`, no actor, with the
  method and target, `reasonCode`, `clientIp`, `userAgent` and, marked
  `claimedVerified: False`, `oauthIssuer` / `claimedDid`; in the typed trail
  `authorization.denied` with that reason code.
  `tool_scopes` maps each tool to the scopes it needs and denies unlisted
  tools; then `minimum_level` (default `L3`), `tools[name]["minimum_level"]`
  and `l3_minus_satisfies_l3` set the Card level each tool demands (story
  C-C; a call below it raises `LevelError`, 401 `insufficient_user_authentication`
  with `derivedLevel` / `requiredLevel`); every decision goes to `audit_sink`,
  which `HelixMcpGate` sets by itself. A failing sink never blocks a call: it
  is a warning on standard error (or `on_audit_error`'s to handle). The audit
  log holds typed producer events (KYA-OS AAP-1). A trail's
  `tool_audit_sink()` records each decision as typed events: a KYA-OS caller
  becomes `proof.verified` (verification code
  `KYAOS_PROOF_<level>`, `_CACHED` when a status was not read live; story C-C
  writes an L3 on an unfused proof as `L3_MINUS` and adds `_CNF_UNFUSED` for
  the `cnf` diagnostic) then `authorization.approved` / `authorization.denied`
  (reason `proof_level_insufficient` for a level refusal), a refused request
  proof `proof.rejected` by its first reason code, an OAuth caller the
  authorization event alone; each strictly validated, sequenced per source
  with `previousSourceEventDigest` and digested. The gate also records the
  lifecycle of each tool call (`session.established`, then
  `tool.call.started` / `completed` / `failed` through
  `audit_tool_handlers(server)`, or `tool.call.denied`, under one correlation
  id), as `examples/mcp_order_management` does. No event holds tool arguments, a
  response, a token or a credential; an OAuth subject is a keyed commitment; tool
  names only with `include_tool_names=True`. The typed events themselves
  (`AuditTrail`, the strict validator, digests, SPEC 7.8 references, the
  evidence vault) are `helix_sdk.audit`.
  `oauth=` takes one trusted issuer or a list: `OAuthIssuerOptions(issuer,
  audience)` is enough (keys come from the issuer's metadata), or use the
  presets `entra()`, `auth0()`, `okta()`, `keycloak()`; opaque tokens are
  checked by RFC 7662 introspection at the one issuer you run.
  `to_http_error_response()` and `build_protected_resource_metadata()` give
  the 401 / 403 challenges and the RFC 9728 document. A runnable server is in
  [`examples/mcp_order_management`](examples/mcp_order_management).
  Install with the `mcp-middleware` extra:
  `pip install "helixid-sdk-py[mcp-middleware]"`.
- **`helix_langchain`** -- LangChain integration:
  `helix_id_tool_wrapper(tool, signer=..., audience=..., chain=...)` runs the
  tool with the agent's Signer, and inside it `sign_current_request(request)`
  returns the MCP request with a fresh request proof (and the chain) in
  `params._meta`. Tool input is untouched. Also scope-based tool filtering.
  Install with `pip install helixid-sdk-py[langchain]`.
- **`helix_crewai`** -- CrewAI integration, following the same pattern as
  `helix_langchain`. There is no CrewAI counterpart in `helix-sdk-js` --
  this is Python-only. Install with `pip install helixid-sdk-py[crewai]`.

Install everything at once with `pip install helixid-sdk-py[all]`.

## Audit: the gate sends it by itself

Set `HELIX_API_KEY` (this service's API key, from the console) in the service's environment. There is nothing else to set up.

- Audit goes to `https://api.helixid.dev`, always.
- When `HELIX_API_URL` is set to a different address (a self-hosted Helix server), audit goes there too, with the same key. When both are the same address there is one logger.
- `DEFAULT_HELIX_API_URL` replaces `https://api.helixid.dev`, for local testing and development. It is used as written.
- At startup the gate asks each address whether this key may write audit for this service and prints `HelixID: audit OK`, or the exact reason it is not (`HELIX_API_KEY is not set`, the address could not be reached, the key was refused, the key is another service's). `gate.audit_ready` (a `concurrent.futures.Future`) holds the result, one `GateAuditCheck` per address; the check runs on a background thread and never delays or stops start-up.
- A write that fails is a warning on standard error, `HelixID: audit write failed: <reason>` (at most one line per 30 s). The tool call still runs.

Events go to the HelixID recorder (`POST /v1/audit/submissions`), which assigns sequence, epoch, predecessor and time and signs a receipt. The server never chooses any of those.

To change the defaults, pass `audit` settings to the gate (a dict or a `GateAuditSettings`: `include_tool_names`, `delivery`, `outbox_dir`, `source_id`, `timeout_seconds`, `startup_check`, `log`); pass `audit=False` for no audit, or an `McpAuditTrail` of your own to send it somewhere else:

```python
app = HelixMcpGate(mcp_app, resource=resource, verifier=verifier, tool_scopes=tool_scopes, audit={"include_tool_names": True})
audit_tool_handlers(server)  # the MCPServer, before its tools are registered: each tool records its own started / completed
```

| `audit["delivery"]` | What it means |
|---|---|
| `best-effort` (default) | Delivery is attempted before the call goes on; a failure is a warning and the call runs. |
| `required` | Fail closed. The intent's signed receipt is obtained before the handler runs; if it cannot be, the call answers `audit_delivery_failed` and the handler does not run. If the terminal append fails after the handler ran, the result is marked `_meta["org.kya-os/audit"] = {"status": "degraded", "reason": ...}` with `isError`. |
| `buffered` | Events go to a durable outbox (`audit["outbox_dir"]`) keyed by `(sourceId, eventId)` and are delivered in the background, in order per source; the outbox survives a restart. |

`create_gate_audit(service_did=...)` builds the same trail outside the gate (its `check()` prints the startup line), and
`create_helix_audit_delivery(base_url, api_key)` is one delivery to one address, for a trail you assemble yourself.
The tenant is the producer (the service DID) unless `tenant_ref` says otherwise; each tenant DID has its own ledger.
`audit_tool_handlers` takes the call explicitly, as the JS one does, or uses the tool call the gate opened in the
current context, since a Python server is usually built once. `run_audited_tool` / `async_run_audited_tool` wrap a
single handler. `HelixClient.get_audit_log(cursor=...)` reads the log one page at a time
(`{"items": [...], "nextCursor": ...}`). The order-management sample reads `AUDIT_DELIVERY` and `AUDIT_OUTBOX_DIR`.

## KYA-OS session, replay store and response proof (`helix_mcp_middleware`, story L2-A)

The Python twin of `@helixid/mcp-middleware`'s KYA-OS layer, with the same wire format, error codes and replay-store
keys, so JS and Python instances serve one DID side by side:

```python
from helix_sdk import kyaos
from helix_mcp_middleware import KyaOsOptions, create_kyaos

kya = create_kyaos(KyaOsOptions(
    signer=kyaos.EnvSigner("did:web:mcp.example.com", env_var="SERVICE_SIGNING_KEY"),  # the key stays in the environment
    public_origin="https://mcp.example.com",
))
# The handshake tool: await kya.handshake_tool_result(args). Every other tool call:
started = await kya.start({"name": name, "arguments": args, "_meta": meta}, authorize=lambda: gate.authorize(tool_call))
if started.outcome == "denied":
    ...  # to_http_error_response(started.error, metadata_url, proof=started.proof)
result = await started.finish(await handler(started.arguments))  # the result with _meta["org.kya-os/response-proof"]
```

The client side is `helix_sdk.kyaos` (`create_handshake_request()`, `verify_response_proof()`, which recomputes both
hashes against the server DID). The replay store is the one the environment names unless `nonce_store=` is passed
(`nonce_store_from_env()`):

| Variable | Values | Default |
|---|---|---|
| `KYAOS_NONCE_STORE` | `memory`, `redis`, `dynamo` | `memory` |
| `KYAOS_REDIS_URL` | `redis://` or `rediss://` URL; required for `redis` | |
| `KYAOS_NONCE_KEY_PREFIX` | Redis key prefix | `kyaos:nonce:` |
| `KYAOS_DYNAMO_TABLE` | table name; required for `dynamo` | |
| `KYAOS_DYNAMO_KEY_ATTRIBUTE` | partition key attribute (string) | `pk` |
| `KYAOS_DYNAMO_TTL_ATTRIBUTE` | the table's TTL attribute (Unix seconds) | `expiresAt` |
| `KYAOS_DYNAMO_REGION` | AWS region | boto3's own |
| `KYAOS_DYNAMO_ENDPOINT` | endpoint URL, for DynamoDB Local | boto3's own |

AWS credentials come from boto3's standard chain, never from these variables. The clients are optional extras,
imported only when chosen: `pip install 'helixid-sdk-py[redis]'` (redis-py, used through `redis.asyncio`) and
`pip install 'helixid-sdk-py[dynamo]'` (boto3, called from a worker thread). It fails closed: an unknown store, a
missing required variable or a missing package raises `NonceStoreConfigError` when the runtime is built, and nothing
falls back to memory. One startup line on the `helix_mcp_middleware.kyaos` logger names the store without secrets (a
Redis password is shown as `***`); memory with `ENV` or `NODE_ENV` set to `production` also warns that it covers one
process only.

## KYA-OS signed `needs_authorization` and resume (story L3-E)

Server side (`helix_mcp_middleware`): when a caller is identified but its delegation does not cover the tool's scopes, the gate answers with the SPEC §9.2 challenge instead of a plain 403: `{"error": "needs_authorization", "message", "authorizationUrl", "resumeToken", "expiresAt", "scopes", "display"?}`. Under the KYA-OS runtime it is the call's tool result, signed with `outcome: needs_authorization` and a `responseHash` over the whole result, so `authorizationUrl` is covered by the service signature.

```python
from helix_mcp_middleware import MCPMiddlewareOptions, NeedsAuthorizationOptions, create_needs_authorization

needs_authorization = create_needs_authorization(NeedsAuthorizationOptions(
    authorization_url=lambda c: f"https://consent.example/authorize?tool={c.tool}",
    ttl_seconds=600,              # resume-token lifetime
    # store: default follows KYAOS_NONCE_STORE (resume_token_store_from_env: Redis on KYAOS_REDIS_URL, else memory)
    authorizer=verify_delegation, # the seam stories F-2 and L3-H implement; default refuses every retry
    audit_sink=audit_sink,        # every challenge: AUTHZ_DENIED, reason "needs_authorization"
))
options = MCPMiddlewareOptions(..., needs_authorization=needs_authorization)
```

The resume token is opaque, bound to the agent, tool and scopes, single-use and gone at `expiresAt`; a retry carries it as the `_kyaos_resume` control argument and the new DelegationCredential as `_kyaos_delegation`. Completing consent alone never authorises the call. With `async_helixid_mcp_middleware` the store, authorizer and audit sink may be async. Client side (`helix_sdk.needs_authorization`): `handle_needs_authorization(response, service_did, resolve_did, request=...)` verifies the challenge's proof, recomputing both hashes, before it surfaces the URL; `retry_with_resume(resume_token, hop, call=..., send=...)` retries with the token and the credential.

## Forwarding delegation downstream (`helix_mcp_middleware`, story L3-D)

When a tool calls another service on the agent's behalf (SPEC.md §8), `with_delegation(request, signer)` wraps any `request(method, url, headers=..., **kw)` (`requests.request`, `httpx.Client().request`, `httpx.AsyncClient().request`) and sends, on every call, the chain the agent presented (`KYA-OS-Delegation-Credential`), a JWT this service signs for the target host (`mint_delegation_proof_jwt`: `iss` the service, `sub` the root hop's issuer, `aud` the host, 60 s, `jti`, `delegation_id`, `delegation_chain`, `scope`), `KYA-OS-Agent-DID` and `KYA-OS-Session-Id`; `advisory_headers=True` adds the advisory chain and scope headers. The context is `delegation_context_of(await gate.authorize(call))`, made current with `with run_with_delegation(context):`. `redelegate` appends the service's own hop, refused (`RedelegationError`) unless it attenuates the chain; `audit=trail.outbound_audit()` records `delegation.outbound_attached`.

The gate verifies inbound delegation headers with `verify_inbound_delegation_headers` (`inbound_delegation=InboundDelegationSettings(forwarders=..., audience=...)`, on by default; `forwarders` defaults to `"*"`, `[]` for none): the proof's signature, `aud`, window and `jti`; `sub` = the chain's root issuer; the forwarder bound to the chain; `KYA-OS-Agent-DID` = the agent the chain names; the chain itself by the local verifier, at most L2. Advisory headers never decide. Chain verdicts are cached by credential id (`VerificationCache`, no longer than the status cache, ≤ 60 s for high-privilege scopes, bypassed for L3 tools) and dropped at once by a revocation signal; `DelegationGraph` covers L3.14 to L3.19. Same names and reason codes as helix-sdk-js.

## The `/.well-known/mcp` discovery document (`helix_mcp_middleware`, story L1-D)

`create_well_known_mcp()` builds the KYA-OS discovery document from config, validates it against the vendored
`well-known-mcpi.json` schema at startup (raising `WellKnownMcpError` with the schema's complaints) and serves it over
ASGI; for the same config it is byte for byte the document `@helixid/mcp-middleware`'s `createWellKnownMcp()` serves.

```python
from helix_mcp_middleware import create_well_known_mcp

well_known = create_well_known_mcp(
    public_origin="https://mcp.example.com",   # the origin agents connect to; required, never the bind address
    server_did="did:web:mcp.example.com",       # did:web or did:key; required
    tools_endpoint="/mcp",                       # path at public_origin, or an absolute URL there
    kyaos=kya.describe(),                        # proofs, the handshake and the skew from the L2-A layer
    status_list_credential="https://mcp.example.com/credentials/status/1",  # optional
    capabilities={"delegation": False, "statusList": False, "crisp": False},  # each true only while that feature is on
)

async def app(scope, receive, send):
    if await well_known.handle(scope, receive, send):  # GET / HEAD /.well-known/mcp; False for anything else
        return
    ...
# any other host: well_known.respond() is (status, headers, body)
```

`HelixMcpGate` serves this document by itself whenever it knows its DID (`service_did`, or the verifier's): the
DID, the MCP endpoint (`resource`) and whether it signs its answers (from `kyaos`). `well_known=False` turns that off
(a host serving its own document, as `examples/mcp_order_management` does); a dict such as
`{"status_list_credential": ...}` sets the document's other members. `gate.well_known` is the document.

`enabled=False` answers 404. Validation needs `jsonschema` (in the `mcp-middleware` and `all` extras); the `uri`,
`uri-reference` and `email` formats are checked with ajv-formats' own expressions, so both languages refuse the same
documents with the same messages. Route `/.well-known/mcp` at your origin root to the middleware: helix-sdk-js's
`mcp-middleware/docs/well-known.md` has the nginx, ALB and API Gateway rules and the `public_origin` rule. The client
side is `helix_sdk.kyaos.fetch_well_known_mcp(origin)` / `read_well_known_mcp(document)`, which fill
`clockSkewSeconds` with 120 when absent and resolve relative endpoints, for `create_handshake_request()` timing and
`verify_response_proof(..., skew_seconds=document["clockSkewSeconds"])`.

## Examples

See `examples/`:

- `request_proof_demo.py` -- an agent signs one MCP call with
  `kyaos.with_request_proof()` and a service verifies it in-process in the
  section 11.2 order; a replay, another body and another service are refused.
  Runs offline:

```bash
python examples/request_proof_demo.py
```

## CLI

Platform-operator workflows (DID/wallet/status-list/VC lifecycle) are covered
by `helix-sdk-js`'s `cli` package -- the CLI has a single canonical
implementation rather than one per SDK language.

The one exception is custody: this package installs a `helix` command with
`helix init`, `helix activate` and `helix custody take` (`take` alone also
works; or `python -m helix_sdk.custody`). They do and print what the
helix-sdk-js commands of the same names do, with the same options, for
Python-only hosts. `init` and `custody take` end with what to do next: which
file is secret and the one line that loads it. `--json` prints the result as
JSON instead. They exit 0
when the command ran, an agent still waiting for its documents included, and
1 with the error on stderr. The same steps are functions: `helix_init`,
`activate_agent`, `change_to_self_custody`.

With own hosting you do not have to run `activate`: Helix re-checks an agent
waiting for its documents in the background and activates it once they are
live.

A self-custody agent's key is generated on its host and never sent. A
custodial agent signs through `HelixCustodialSigner`, which calls the Helix
API. Both are Signers, so `helix_sdk.verifier.sign_request_proof` and `sign_jws` work
the same with either. Custodial agents are labelled `custody: helix` and are
not counted in KYA-OS conformance claims; their proofs still verify.

## Testing

```bash
pip install -e ".[dev,all]"
pytest
```

`pytest` always runs with coverage (`[tool.pytest.ini_options]` in
`pyproject.toml`) and fails under 60% -- the measured baseline as of
2026-09-03, not a target. `helix-sdk-js`/`helix-api` enforce 90%
lines/statements, 85% branches, 90% functions; closing this gap (some
modules, like `did.py`, currently have no direct unit tests at all) is
real follow-up work, not something this tooling change did on its own.

- `tests/test_golden_vectors.py` -- cross-language crypto parity (no network).
- `tests/test_wallet.py` -- `AgentWallet` encrypted persistence, credential
  storage/queries, and a local-signing round trip. (`delegate()`
  and its mocked-HTTP tests were removed.)
- `tests/test_mcp_middleware.py` -- the middleware's Phase 1 cases (credentials,
  scopes, JWT / introspection / several issuers / discovery / presets, HTTP
  challenges, audit); `tests/test_mcp_order_management_example.py` runs the
  sample behind the MCP SDK's HTTP transport (Python 3.10+).
- `tests/test_framework_adapters.py` -- exercises `helix_mcp_middleware`,
  `helix_langchain`, and `helix_crewai` against the real installed
  `langchain-core` and `crewai` packages (not mocks of them), so a real
  interface drift in either framework would fail these tests. Skips
  gracefully on Python < 3.10, since both frameworks require it.

There is currently no automated **live** integration test against a real
`helix-api` instance in this repo's CI (see Known gaps) -- if you have a
running instance, you can exercise the real network path with the
delegation example above.

## Known gaps

- **No live network integration test yet.** The mocked tests above verify
  the SDK's logic faithfully, but nothing here has been run against a real,
  live `helix-api` process end-to-end. (The sandbox used to build this SDK
  could not start one -- `helix-api`'s `server.ts` imports the generated
  Prisma client unconditionally, even in SQLite storage mode, and `prisma
  generate` needs network access to `binaries.prisma.sh`, which was outside
  that environment's egress allowlist. This is a sandbox-only limitation,
  not a code issue, and shouldn't block real CI or local development where
  Prisma can actually run.) Running the two example scripts against a real
  instance is the recommended way to close this gap.
- **`HttpAdapter` is synchronous** (built on `requests`), unlike
  `helix-sdk-js`'s async `fetch`-based adapter. This was a deliberate v0
  choice -- most Python agent/automation call sites are synchronous -- not
  an oversight; an async variant can be added later without breaking this
  one.
- **No `did:hedera` package yet.** DID resolution generally is always an
  API call in this architecture, so
  this is really about offline Hedera anchoring specifically, which
  `helix-sdk-js`'s `cli` package supports via a separate `did-hedera`
  package this repo hasn't ported. Since the CLI itself is JS-only, this gap only matters if this SDK's
  own local-signing primitives grow a `did:hedera` creation path.
- **No `widget` package** (helix-sdk-js's browser-embeddable consent
  widget has no obvious Python equivalent and hasn't been attempted).
- **`HelixIDCallbackHandler` in `helix_langchain` is a no-op.** LangChain
  Python's `on_tool_start` callback is a notification hook, not an
  interceptor. Request signing happens in `helix_id_tool_wrapper()`, which
  hands the tool `sign_current_request()`; the callback handler is kept only
  for observability use. (The JS SDK has dropped its callback form,
  `HelixIDMiddleware`, entirely.)
- **Version numbering does not yet track `helix-sdk-js`'s version.** This SDK's version should track the JS
  SDK's; that mapping strategy hasn't been decided yet, so this starts at
  `0.1.0` independently.

## The HelixID ecosystem

| Repository | What it is |
|---|---|
| [helixid](https://github.com/helixid/helixid) | HelixID API — the issuer and verifier service |
| [helix-core](https://github.com/helixid/helix-core) | `@helixid/core` — crypto, schemas, resolver, verification primitives |
| [helix-sdk-js](https://github.com/helixid/helix-sdk-js) | JS/TS SDK, CLI, LangChain + MCP middleware, consent widget |
| **helix-sdk-py** — you are here | `helixid-sdk-py` — the Python SDK |
| [helix-console](https://github.com/helixid/helix-console) | Operator Console SPA |
| [helix-wiki](https://github.com/helixid/helix-wiki) | Source for [docs.helixid.dev](https://docs.helixid.dev) |

---

## License

Apache-2.0
