Skip to main content

MolTrust trust verification middleware for LangChain 1.x

Project description

moltrust-langchain

Add trust verification to LangChain 1.x agents in one line.

moltrust-langchain plugs into LangChain 1.x's AgentMiddleware hook system to check MolTrust trust scores before an agent reasons and before it calls out to other agents/tools. Below-threshold agents are blocked (or warned/logged).

Install

pip install moltrust-langchain

Usage

from langchain.agents import create_agent
from moltrust_langchain import MolTrustMiddleware

agent = create_agent(
    model="anthropic:claude-sonnet-4-6",
    tools=[...],
    middleware=[MolTrustMiddleware(min_score=60)],
)

This example matches the real langchain.agents.create_agent signature (middleware: Sequence[AgentMiddleware] = ()), verified against langchain 1.3.11.

Usage tiers

Tier 1 — no account required (default):

MolTrustMiddleware(min_score=60)          # keyless; reads scores rate-limited

Tier 2 — free account (higher limits):

MolTrustMiddleware(min_score=60, api_key="mt_...")   # or MOLTRUST_API_KEY env

The API key is sent as the X-API-Key header only when present.

What it does

MolTrustMiddleware is a langchain.agents.middleware.AgentMiddleware. It uses two hook points (verified against langchain 1.3.11):

  • before_model(state, runtime) — checks this agent's own DID (agent_did=...) before it reasons. A blocked check raises TrustCheckFailed (halts the agent) when action="block".
  • wrap_tool_call(request, handler) — checks the target of a tool / A2A call (DID from the tool-call args under did_key, or agent_did_map by tool name). A blocked call returns an error ToolMessage (the agent can react) instead of raising.

min_score is on the 0–100 MolTrust trust score (trust_score from GET /skill/trust-score/{did}) — the behavioral score, not a 0–5 rating. Scores are recomputable: verify the on-chain solvency component of any agent yourself at

https://api.moltrust.ch/credits/solvency/{did}

Options

MolTrustMiddleware(
    min_score=60,               # 0-100 trust score threshold
    action="block",             # "block" | "warn" | "log"
    agent_did="did:moltrust:…",  # this agent's DID (before_model self-check)
    agent_did_map={"call_agent": "did:moltrust:…"},  # tool → target DID
    did_key="did",              # DID key in tool-call args / runtime context
    pass_without_did=True,       # allow calls with no resolvable DID
    check_model=True, check_tools=True,
)

Difference from Microsoft AGT

Microsoft's Agent Governance Toolkit enforces policieswhat an agent may do (scope, sandboxing, zero-trust identity). MolTrust verifies behavioral trustis this agent trustworthy, based on verifiable history. Complementary, not competing: AGT bounds authority; MolTrust decides who has earned it.

Honest limitations (0.1.0)

  • LangChain agents have no MolTrust DID by default — supply agent_did (for the self-check) and/or a did in tool-call args or agent_did_map (for targets).
  • A withheld score (registered agent, too few endorsements → API returns trust_score: null) is treated like an unregistered agent: fail-closed in block, fail-open in warn/log.
  • On a registry/transport error the middleware fails open (logs, allows) so a MolTrust hiccup never breaks your agent.
  • Synchronous hooks only. For fully async agents (ainvoke/astream), async abefore_model/awrap_tool_call are a follow-up.

License

MIT © CryptoKRI GmbH — see LICENSE.

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

moltrust_langchain-0.1.2.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

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

moltrust_langchain-0.1.2-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file moltrust_langchain-0.1.2.tar.gz.

File metadata

  • Download URL: moltrust_langchain-0.1.2.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for moltrust_langchain-0.1.2.tar.gz
Algorithm Hash digest
SHA256 fd651182da2b7abbdf069e6831e2edde8efc9d1380172f24f36b37966c0705da
MD5 7597c8583cacc1d381e4beb65273afd4
BLAKE2b-256 5ec9a3145206169d8b5e5f39907abce010123716f81aabaa345b2dc133f25999

See more details on using hashes here.

File details

Details for the file moltrust_langchain-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for moltrust_langchain-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 90b08fc65d5b1162f72a46fb0dd14dbddea2978be287608ff9720d40f22475cf
MD5 ffb45a9e63aa8bbe2cb0ae6a4fbe259e
BLAKE2b-256 ab74e1f7357211fb65caa99847e2fd17094318cea6e9cdc01d2b5d34e624ef1f

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