IRL Gateway

io.github.horkos-labsv0.2.1更新于 Oct 6, 2026

An AI agent's trading mandate it can't break: checked before every order, reasoning sealed in IRL.

概览

AI 生成的概览

让 AI 代理通过受策略校验的网关下现货市价单,并封存每笔交易的理由、核对成交结果。

功能
IRL Gateway 位于 AI 代理与交易所账户之间。其 execute_trade 工具按授权、下单、绑定三步执行:订单先按代理的授权范围(是否有效、名义金额上限、允许的资产与场所)校验后才到达交易所;代理必须说明交易理由,该理由会被哈希并封存进防篡改的存证中;授权意图与实际成交会被比对,记为 MATCHED 或 DIVERGENT。其他工具可读取授权范围与本地急停开关状态、报价、余额、按 id 查询封存存证,以及本地近期交易日志。
适用场景
当助手需要具备交易能力,但只能在预先注册的授权范围内操作,并且你希望留下可验证的记录——它被允许做什么、它声称在做什么、实际执行了什么——时适用。默认使用模拟盘,因此无需交易所密钥即可评估。
运行要求
以 stdio 方式在本地运行,可从 PyPI 安装 irl-gateway,或用 uvx 直接运行。需要一个 IRL 服务器和已注册的代理:IRL_BASE_URL、IRL_API_TOKEN(bearer 密钥)、IRL_AGENT_ID 和 IRL_MODEL_HASH 为必填。GATEWAY_BROKER 选择 paper(默认)或 exchange;exchange 模式需要 EXCHANGE_API_KEY 和 EXCHANGE_API_SECRET。需要能访问 IRL 服务器和交易场所的网络。
安装前请注意
execute_trade 是唯一会动用资金的工具,在 exchange 模式下会下真实订单,此时必须提供 EXCHANGE_API_KEY 和 EXCHANGE_API_SECRET,应严格限制其权限,并在测试期间保持 EXCHANGE_TESTNET 开启。IRL_API_TOKEN 是 bearer 密钥。理由原文留在本地日志中,其哈希会发送给 IRL;日志、急停开关和模拟盘状态存放在 IRL_GATEWAY_HOME 下。版本为早期 0.1,仅支持现货市价单,不含任何策略。

安装

在 SourceWeft 中

  1. 打开 控制台中的 IRL Gateway,将其添加到工作区。
  2. 为需要使用其工具的对话启用该服务。

Desktop only,通过 STDIO。 STDIO 服务会启动本地进程,因此需要 SourceWeft 桌面宿主。

其他 MCP 客户端

参照 仓库 中的启动说明。

README

IRL Gateway

Give your AI agent a trading account it can't misuse, and a record of every decision it can't rewrite.

IRL Gateway is an MCP server that sits between an AI agent (Claude, ChatGPT, or your own) and an exchange account. Every order the agent places goes through the IRL Engine:

  1. Policy before execution. IRL checks the order against the agent's mandate (active status, notional cap, allowed assets and venues) before anything reaches the exchange. Out of mandate means no order.
  2. The rationale is sealed. The agent must say why it is trading. The gateway hashes that rationale together with the trade inputs and seals the hash into IRL's tamper-evident trace, anchored daily to Bitcoin. The plaintext stays in your local journal.
  3. Intent is reconciled with the fill. After the exchange fills the order, IRL compares what was authorized with what executed and records MATCHED or DIVERGENT.

When something goes wrong, you can prove what the agent was allowed to do, what it said it was doing, and what actually happened.

AI agent ── MCP ──> irl-gateway ──> IRL: authorize (policy + sealed rationale)                         │                         ├──────> exchange: market order (client id = sealed intent)                         │                         └──────> IRL: bind fill -> MATCHED / DIVERGENT

Tools

ToolWhat it does
execute_trade(symbol, side, rationale, quantity | notional)The only tool that moves money. Spot market order through authorize → place → bind. Returns filled, denied, blocked or failed, with the IRL trace_id and verdict.
get_policy()The agent's mandate as IRL enforces it, plus the local kill-switch state.
get_quote(symbol)Last price on the gateway's venue.
get_balances()Free balances (paper or exchange).
get_trace(trace_id)IRL's sealed record of one trade.
list_recent_trades(limit)Local journal: rationale, context hash, trace id and outcome per trade.

Behaviour the agent can rely on:

  • Fail closed. If IRL is unreachable or denies the intent, no order is sent.
  • Kill switch. Create the file ~/.irl-gateway/KILL and every trade is refused before IRL is even called. Delete it to resume.
  • No silent fills. If the exchange fills but the IRL bind fails, the result still reports the fill and flags it for reconciliation.

Quick start (paper trading)

You need an IRL server and an agent registered on it. Paper trading is the default: fills are simulated at live public Binance prices, and no exchange keys are needed.

bash
pip install irl-gateway      # or run it without installing: uvx irl-gateway

Register the agent once, with its mandate:

bash
curl -X POST "$IRL_BASE_URL/irl/agents" -H "Authorization: Bearer $IRL_API_TOKEN" \  -H "Content-Type: application/json" -d '{    "name": "my-claude-trader",    "model_hash_hex": "<sha256 of your agent config>",    "max_notional": 100,    "allowed_assets": ["BTC/USDT", "ETH/USDT"],    "allowed_venues": ["paper-binance"]  }'

Then add the gateway to your MCP client, for example Claude Code or Claude Desktop:

json
{  "mcpServers": {    "irl-gateway": {      "command": "uvx",      "args": ["irl-gateway"],      "env": {        "IRL_BASE_URL": "https://irl.example.com",        "IRL_API_TOKEN": "…",        "IRL_AGENT_ID": "<agent_id from registration>",        "IRL_MODEL_HASH": "<the same model_hash_hex>",        "AGENT_MODEL_ID": "claude-opus-5-5",        "PAPER_BALANCES": "USDT=1000"      }    }  }}

Ask the agent to check get_policy, then trade.

Configuration

VariableDefaultMeaning
IRL_BASE_URL, IRL_API_TOKENrequiredIRL server and bearer token
IRL_AGENT_ID, IRL_MODEL_HASHrequiredThe registered agent and its model hash
AGENT_MODEL_IDunspecified-modelModel name sealed into each trace (the agent can override it per trade)
AGENT_CONFIG_CHECKSUMnoneOptional checksum of the agent's configuration, sealed into each trace
IRL_L2_MODEoffregime if your IRL server requires Layer 2 regime binding
GATEWAY_BROKERpaperpaper or exchange
EXCHANGE_IDbinanceAny ccxt exchange id; also the price source for paper trading
EXCHANGE_API_KEY, EXCHANGE_API_SECRETRequired for exchange
EXCHANGE_TESTNETtrueUse the exchange's testnet
PAPER_BALANCESUSDT=1000Starting paper balances (used only until paper_state.json exists; the paper account then persists across restarts)
IRL_GATEWAY_HOME~/.irl-gatewayJournal (journal.jsonl), kill switch (KILL) and paper account (paper_state.json) location

The venue IRL sees is the exchange id (binance), or paper-<exchange> for paper trading, so a mandate can allow paper trading while denying the real account.

How the rationale is sealed

For each trade the gateway builds a context of the rationale, symbol, side, quantity, reference price, venue, model id and client order id. It hashes that context as canonical JSON (sorted keys, no whitespace) with SHA-256 and sends the hash to IRL as prompt_version = "ctx-sha256:<hex>", which IRL seals into the trace's reasoning_hash.

The journal stores the full context next to its hash, so anyone holding a journal line can recompute the hash and match it to the sealed trace. IRL itself never sees the rationale's text.

Development

bash
python -m venv .venv && .venv/bin/pip install -e ".[dev]"pytest --cov=irl_gatewayruff check src tests && black --check src tests && isort --check-only src tests && mypy src

Status

Early (0.1). Spot market orders only. Paper trading and ccxt exchanges are supported; Alpaca is next. Not investment advice, and no strategy is included: the gateway controls and records what your agent does, it does not decide.

MIT licensed.

来源:README.md,提交 84461bb

工具

0
工具元数据尚未被收录。

版本历史

1
  1. v0.2.1最新Oct 6, 2026