Metadata-Version: 2.4
Name: swarm-provenance-mcp
Version: 0.3.2
Summary: MCP server that lets AI agents store, sign, anchor and verify data provenance on Swarm.
Author: Datafund
License-Expression: MIT
Project-URL: Homepage, https://github.com/datafund/provenance
Project-URL: Repository, https://github.com/datafund/swarm_provenance_MCP
Project-URL: Issues, https://github.com/datafund/swarm_provenance_MCP/issues
Project-URL: Documentation, https://github.com/datafund/swarm_provenance_MCP#readme
Keywords: mcp,ai-agents,data-provenance,swarm,notary,verification,blockchain,decentralized-storage
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
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
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: mcp<2,>=1.26
Requires-Dist: requests<3,>=2.31.0
Requires-Dist: pydantic<3,>=2.0.0
Requires-Dist: pydantic-settings<3,>=2.0
Requires-Dist: python-dotenv<2,>=1.0.0
Requires-Dist: web3<8,>=7
Requires-Dist: eth-account<0.14,>=0.13
Requires-Dist: eth-utils<6,>=5
Provides-Extra: dev
Requires-Dist: pytest>=7.0.0; extra == "dev"
Requires-Dist: pytest-asyncio>=0.21.0; extra == "dev"
Requires-Dist: pytest-mock>=3.10.0; extra == "dev"
Requires-Dist: requests-mock>=1.10.0; extra == "dev"
Requires-Dist: psutil>=5.9.0; extra == "dev"
Requires-Dist: jsonschema>=4.0.0; extra == "dev"
Requires-Dist: black>=23.0.0; extra == "dev"
Requires-Dist: ruff>=0.1.0; extra == "dev"
Provides-Extra: test
Requires-Dist: pytest>=7.0.0; extra == "test"
Requires-Dist: pytest-asyncio>=0.21.0; extra == "test"
Requires-Dist: pytest-mock>=3.10.0; extra == "test"
Requires-Dist: requests-mock>=1.10.0; extra == "test"
Requires-Dist: psutil>=5.9.0; extra == "test"
Requires-Dist: jsonschema>=4.0.0; extra == "test"
Provides-Extra: security
Requires-Dist: safety>=2.0.0; extra == "security"
Requires-Dist: bandit>=1.7.0; extra == "security"
Dynamic: license-file

# Swarm Provenance MCP

[![Regression and Safety Tests](https://github.com/datafund/swarm_provenance_MCP/actions/workflows/regression_tests.yml/badge.svg)](https://github.com/datafund/swarm_provenance_MCP/actions/workflows/regression_tests.yml)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

> ⚠️ **ALPHA SOFTWARE - PROOF OF CONCEPT**
> This software is **alpha** and should be considered a **Proof of Concept**. Use for testing and experimentation only. Not recommended for production use.

> ⚠️ **DATA PERSISTENCE WARNING**
> Storage on Swarm is **rented storage** with limited time periods. The default configuration uses very short rental periods (approximately **1 day**). **Do not expect uploaded data to persist longer than the rental period.** Data will become unavailable when the postage stamp expires.

> ⚠️ **TESTNET NOTICE**
> On-chain provenance features use **Base Sepolia (testnet)** by default. Testnet tokens have no monetary value and testnet state may be reset at any time. Do not rely on testnet anchoring for production data integrity. No warranties of any kind are provided.

> **PAYMENTS**
> This server cannot pay x402 requests. It uses the gateway's free tier (`PAYMENT_MODE=free`, the default) or sends no payment header. For paid use, run a similar operation from start to end instead of this server, with one of these:
> - The [Swarm Provenance CLI](https://github.com/datafund/swarm_provenance_CLI): `swarm-prov-upload --x402`. See its [x402 setup guide](https://github.com/datafund/swarm_provenance_CLI/blob/main/docs/x402-setup.md).
> - The [TypeScript SDK](https://github.com/datafund/swarm_provenance_SDK): `@datafund/swarm-provenance`, `ProvenanceClient` with `gatewayUrl` and the `payment` option. Set the payment network to the gateway's network. That option's format differs between SDK releases, so follow the README of the installed version, as shown on [npm](https://www.npmjs.com/package/@datafund/swarm-provenance).
>
> The default gateway of the CLI and SDK charges real USDC on Base mainnet.

A Model Context Protocol (MCP) server for managing Swarm postage stamps and provenance data storage through a centralized FastAPI gateway. Enables AI agents to upload provenance data to the decentralized Swarm network for immutable storage and retrieve it by reference.

**For AI agents:** follow the skill in [`skills/swarm-provenance-mcp/SKILL.md`](skills/swarm-provenance-mcp/SKILL.md) and the safe defaults in [`AGENTS.md`](AGENTS.md).

## Overview

This MCP server provides tools for AI agents to interact with Swarm postage stamps and provenance data storage, including purchasing and extending stamps, uploading provenance data to Swarm for immutable decentralized storage, and downloading data from the network by reference. It acts as a bridge between AI agents and the `swarm_connect` FastAPI gateway.

## Provenance & Immutable Storage

This MCP server is specifically designed for provenance data use cases, leveraging Swarm's decentralized network to provide:

- **Immutable Records**: Swarm addresses content by its hash, so the data stored under a reference cannot be changed. `download_data` checks content against its reference only in the cases its [reference check](#download_data) lists
- **Decentralized Storage**: No single point of failure or central authority
- **Provenance Metadata**: Support for structured provenance records with creator, timestamp, and lineage information
- **Reference Check on Download**: `download_data` recomputes the Swarm chunk address of content up to 4 KB and reports the content as verified only when that address equals the reference. References from `/bzz` uploads name a manifest and are reported as not checked; see [datafund/swarm_connect#499](https://github.com/datafund/swarm_connect/issues/499)

## Features

- **Purchase Stamps**: Create new postage stamps with configurable duration and size
- **Stamp Status**: Get detailed information about specific stamps
- **List Stamps**: View all available postage stamps
- **Extend Stamps**: Extend the duration of existing stamps
- **Data Upload**: Upload data to Swarm network with stamp validation
- **Data Download**: Download data from Swarm network by reference
- **Provenance Storage**: Store data with provenance metadata as immutable records whose on-chain registration you can check with `verify_hash`
- **On-Chain Verification** (on by default): Check whether a data hash is registered and read its provenance record and lineage with `verify_hash`, `get_provenance`, `get_provenance_chain` and `lookup_by_storage_ref`. They need no wallet and no gas. `CHAIN_ENABLED=false` turns them off
- **Health Monitoring**: Check gateway and Swarm network connectivity
- **Chain Diagnostics**: Check RPC connectivity with `chain_health` (on by default) and the wallet balance with `chain_balance` (needs `CHAIN_ENABLED=true` and `PROVENANCE_WALLET_KEY`)
- **On-Chain Anchoring** (optional, needs `CHAIN_ENABLED=true` and `PROVENANCE_WALLET_KEY`): Register data hashes (the SHA-256 of the data) on-chain for immutable provenance records, linked to their Swarm reference
- **Transformation Lineage** (optional): Record data transformations with [`record_transform`](#record_transform-optional--requires-chain_enabledtrue-and-provenance_wallet_key) and follow the lineage of a hash with [`get_provenance_chain`](#get_provenance_chain-on-by-default-chain_enabledfalse-turns-it-off). For step-by-step workflows, see [Record a Transformation](swarm_provenance_mcp/SKILLS.md#b-record-a-transformation), [Multi-Step Pipeline](swarm_provenance_mcp/SKILLS.md#c-multi-step-pipeline) and [Transformation Chain](swarm_provenance_mcp/SKILLS.md#transformation-chain) in SKILLS.md
- **Merge Transformations** (optional): Record N-to-1 merge transformations combining multiple source hashes into one with [`record_merge_transform`](#record_merge_transform-optional--requires-chain_enabledtrue-and-provenance_wallet_key). For the workflow, see [Merge Multiple Sources](swarm_provenance_mcp/SKILLS.md#d-merge-multiple-sources) in SKILLS.md

## Installation

### Prerequisites

- Python 3.10 or higher (use `python3` command)
- Internet connection (uses public gateway by default)
- Optional: Self-hosted `swarm_connect` gateway service (see Gateway Options below)

### Network requirements

The server needs outbound HTTPS. In a sandbox with an egress allowlist, allow these hosts, or the tool calls fail with connection errors:

- The gateway: `provenance-gateway.datafund.io` by default, or the host in `SWARM_GATEWAY_URL`.
- The chain RPC, when a chain tool or `health_check` runs (the read-only chain tools are on by default; `CHAIN_ENABLED=false` turns them off). On `base-sepolia` (the default): the RPC `sepolia.base.org`, and the fallbacks the server tries after it, in order: `base-sepolia-rpc.publicnode.com`, `base-sepolia.drpc.org` and `base-sepolia.gateway.tenderly.co`. With `CHAIN_NAME=base`: `mainnet.base.org`, `base-rpc.publicnode.com` and `base.drpc.org`. `CHAIN_RPC_URL` replaces the primary RPC; set alone, it also drops the preset fallbacks, so the server calls only that host. `CHAIN_RPC_URLS` (comma-separated) replaces the fallbacks; set alone, the preset primary stays first. Set both, and the server calls `CHAIN_RPC_URL`, then the `CHAIN_RPC_URLS` hosts in order.
- `pypi.org`: `health_check` asks it whether a newer release exists. It waits at most 3 seconds in total (DNS, connect and the answer), then goes on without it; the answer is kept for a day, a failure for an hour. Only final releases are compared, so a development or release-candidate build gets no notice. `UPDATE_CHECK=false` turns this off. `uvx` also needs PyPI the first time it runs the package.

### From PyPI

Listed in the [MCP Registry](https://registry.modelcontextprotocol.io) as `io.github.datafund/swarm-provenance-mcp`. The registry checks the next line to confirm that this PyPI package belongs to that name:

mcp-name: io.github.datafund/swarm-provenance-mcp

The server is also listed on [Glama](https://glama.ai/mcp/servers/datafund/swarm_provenance_MCP). There you can browse its tools and quality score, and try it online with the [MCP Inspector](https://glama.ai/mcp/inspector) before you install it.

Run the server without installing it, with [uv](https://docs.astral.sh/uv/):
```bash
uvx swarm-provenance-mcp
```

Or install it:
```bash
pip install swarm-provenance-mcp
```

To use it in Claude Desktop, put this in `claude_desktop_config.json`:
```json
{
  "mcpServers": {
    "swarm-provenance": {
      "command": "uvx",
      "args": ["swarm-provenance-mcp"]
    }
  }
}
```

Settings go in `~/.config/swarm-provenance-mcp/.env`, as described under [Configuration](#configuration).

Maintainers: the release process is in [RELEASING.md](RELEASING.md).

### From source

1. Clone the repository:
```bash
git clone https://github.com/datafund/swarm_provenance_MCP.git
cd swarm_provenance_MCP
```

2. Create and activate a virtual environment:
```bash
python3 -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
```

3. Install the package:
```bash
pip install -e .
```

4. Configure environment variables:
```bash
mkdir -p ~/.config/swarm-provenance-mcp
cp .env.example ~/.config/swarm-provenance-mcp/.env
# Edit that file to configure your gateway URL and defaults
```

### Docker

Run the MCP server in a container with no local dependencies.

> **Registry**: The pre-built image is published to **GitHub Container Registry** only — always use the full `ghcr.io/` prefix. It is **not** available on Docker Hub.

**Quick start** (pre-built image):
```bash
docker pull ghcr.io/datafund/swarm-provenance-mcp:latest
docker run -i --rm ghcr.io/datafund/swarm-provenance-mcp
```

**Build from source:**
```bash
docker build -t swarm-provenance-mcp .
docker run -i --rm swarm-provenance-mcp
```

**With environment variables:**
```bash
docker run -i --rm \
  -e SWARM_GATEWAY_URL=https://provenance-gateway.datafund.io \
  -e DEFAULT_STAMP_DURATION_HOURS=25 \
  -e DEFAULT_STAMP_SIZE=small \
  swarm-provenance-mcp
```

**Docker Compose:**
```bash
docker compose build
docker compose run --rm swarm-provenance-mcp
```

Compose takes its variables from `./.env` next to `docker-compose.yml` (for `${VAR}`
substitution and as the container's `env_file`) and passes them to the server as
environment variables. That is Compose's own file, separate from the server's env file
described under [Configuration](#configuration): the container never sees
`~/.config/swarm-provenance-mcp/.env` on the host.

| Variable | Description | Default |
|----------|-------------|---------|
| `SWARM_GATEWAY_URL` | Gateway endpoint URL (`http://` or `https://`; plain http to a remote host logs a warning) | `https://provenance-gateway.datafund.io` |
| `DEFAULT_STAMP_DURATION_HOURS` | Default stamp duration in hours (min 24) | `25` |
| `DEFAULT_STAMP_SIZE` | Default stamp size: `small`, `medium`, or `large` | `small` |
| `PAYMENT_MODE` | Gateway payment tier: `free` (3 write req/min) or empty (no payment header); other values stop the server at start-up | `free` |

## Configuration

Settings come from environment variables, or from an env file. The env file is
**never** read from the working directory (whichever program launches the server
controls that). It is read from, in order:

1. the path in `SWARM_PROVENANCE_ENV_FILE`, if set. It must be an absolute path (`~/...` is fine); start-up fails if it is relative or the file does not exist;
2. otherwise `~/.config/swarm-provenance-mcp/.env` (or `$XDG_CONFIG_HOME/swarm-provenance-mcp/.env`), if it exists.

Real environment variables (for example an MCP client's `"env"` block) override the env file.
Unknown keys in the env file are ignored. If a `.env` in the working directory is found and
ignored, the server logs a warning at start-up.

- `SWARM_PROVENANCE_ENV_FILE`: Explicit absolute path of the env file to load (see above)

- `SWARM_GATEWAY_URL`: URL of the swarm_connect FastAPI gateway (default: `https://provenance-gateway.datafund.io`). Must be `http://` or `https://` with a host and, if given, a numeric port (0 to 65535), or the server does not start. Plain `http://` to a host other than this machine logs a warning at start-up and adds a `health_check` recommendation. The `X-Payment-Mode` header is not sent on a redirect to another origin
- `DEFAULT_STAMP_DURATION_HOURS`: Default stamp duration in hours, minimum 24 (default: `25`)
- `DEFAULT_STAMP_SIZE`: Default stamp size — `small`, `medium`, or `large` (default: `small`)
- `PAYMENT_MODE`: Gateway payment tier (default: `free`, rate limited to 3 write requests/minute). Set it to empty to send no payment header. Any other value stops the server at start-up, because this server cannot pay x402 requests

#### Chain Anchoring (Optional)

- `CHAIN_ENABLED`: Which chain tools are listed. Unset (the default): the read-only tools `chain_health`, `verify_hash`, `get_provenance`, `get_provenance_chain` and `lookup_by_storage_ref`, which need no wallet. `true`: also `chain_balance` and the write tools `anchor_hash`, `record_transform`, `record_merge_transform` and `set_storage_ref`, which need `PROVENANCE_WALLET_KEY`. `false`: no chain tools. A wallet key alone never turns on the write tools. An empty value counts as unset
- `CHAIN_NAME`: Blockchain network — `base-sepolia` (testnet), `base` (mainnet), or `localhost` (local hardhat, chain 31337) (default: `base-sepolia`)
- `PROVENANCE_WALLET_KEY`: Private key for chain transactions (hex, with or without 0x prefix). A key that is set but invalid is logged as an error at start-up; the server still starts with chain writes disabled, and the write tools, `chain_health` and `health_check` say why. Read-only chain tools keep working. Use each key in one server process at a time. Two server processes that sign with the same key, for example one started by a desktop client and one by a command-line tool, can pick the same nonce, and one of the transactions then fails
- `CHAIN_RPC_URL`: Custom RPC endpoint (uses chain preset if not set). For `base-sepolia` and `base` the endpoint must report the preset's chain ID, or the server refuses it; see [Safety behavior](#safety-behavior)
- `CHAIN_RPC_URLS`: Comma-separated fallback RPC URLs, tried in order after `CHAIN_RPC_URL`. The same chain ID check applies to each one; see [Safety behavior](#safety-behavior)
- `CHAIN_CONTRACT`: Custom DataProvenance contract address (uses chain preset if not set). `CHAIN_CONTRACT_ADDRESS` is accepted too; `CHAIN_CONTRACT` wins if both are set
- `CHAIN_EXPLORER_URL`: Custom block explorer URL (uses chain preset if not set)
- `CHAIN_GAS_LIMIT`: Explicit gas limit for chain transactions (skips estimation if set)
- `CHAIN_MAX_FEE_GWEI`: Fee cap per gas (`maxFeePerGas`). Fee fields are set explicitly and a write is refused when the current fee is above the cap (default: `0.5` on mainnet, `10` on testnets)
- `CHAIN_SESSION_BUDGET_ETH`: Worst-case spend allowed per server session; each transaction reserves `gas limit x max fee` plus the L1 data fee allowance, and is charged its actual cost (L2 gas plus the receipt's `l1Fee` on Base) once mined (default: `0.002` on mainnet, `0.05` on testnets)
- `CHAIN_L1_FEE_ALLOWANCE_ETH`: L1 data fee reserved per transaction on top of L2 gas (default: `0.000001`, about 100x the ~1e-8 ETH measured on Base mainnet on 2026-10-21; L2 gas there was 1e-6 to 3e-5 ETH per transaction)
- `CHAIN_MAX_TX_PER_SESSION`: Chain transactions allowed per server session (default: `20` on mainnet, `500` on testnets)
- `CHAIN_REQUIRE_CONFIRM`: Require a confirmed quote on gas-spending tools. Without `confirm: true` and a matching `quote_id` they send nothing and return a quote of the exact action (arguments, fee cap, remaining budget) with a `quote_id`: an HMAC over the tool and its validated arguments under a per-process key, single use, valid 10 minutes (default: mainnet only — any chain id other than 84532, 11155111, 31337 or 1337 counts as mainnet). See [Safety behavior](#safety-behavior)
- `MAX_STAMP_DURATION_HOURS`: Maximum `duration_hours` for `purchase_stamp` / `extend_stamp` (default: `8760`)
- `UPDATE_CHECK`: Let `health_check` ask `pypi.org` whether a newer release exists and, if so, add a line with `uvx swarm-provenance-mcp@latest` (default: `true`). The check waits at most 3 seconds in total (DNS, connect and the answer), keeps the answer for a day (an hour after a failure) and never fails `health_check`; when it cannot reach PyPI in time it says nothing. Only final releases are compared: a running development or release-candidate build gets no notice

By default the read-only chain tools are listed: `chain_health`, `verify_hash`, `get_provenance`, `get_provenance_chain` and `lookup_by_storage_ref`. They work without a wallet key. With `CHAIN_ENABLED=true`, `chain_balance` and the write tools `anchor_hash`, `record_transform`, `record_merge_transform` and `set_storage_ref` are listed too; they require `PROVENANCE_WALLET_KEY` with a funded wallet. Blockchain dependencies (web3, eth-account) are included in the default install.

#### Contract versions

The DataProvenance contract has four deployments on Base Sepolia. They are named v1 to v4, in deployment order, as in the [contract repository](https://github.com/datafund/ConsentsBasedDataProvenance). The `base-sepolia` preset uses v4.

| Version | Address | Lineage support |
|---------|---------|-----------------|
| v1 | `0x9a3c6F47B69211F05891CCb7aD33596290b9fE64` | `recordTransformation` only. It stores the description and links no records in contract state. The link is only in the `DataTransformed(originalDataHash, newDataHash, transformation)` event |
| v2 | `0xD4a724CD7f5C4458cD2d884C2af6f011aC3Af80a` | All five lineage functions |
| v3 | `0xb309dDe00819668ac74075813108659d4332D2cC` | All five lineage functions |
| v4 (current) | `0x3945aDfd5Df9ab2F5cB4Ca0eb3D4384CC3650322` | All five lineage functions |

The five lineage functions are `recordTransformation`, `recordMergeTransformation`, `getTransformationParents`, `getChildHashes` and `getTransformationLinks`.

Use contract v3 or v4 with this server. `set_storage_ref` needs contract v4. On contract v1 and v2, the tools that read a record do not work: `anchor_hash`, `verify_hash`, `get_provenance`, `get_provenance_chain`, `set_storage_ref` and `lookup_by_storage_ref` ([#296](https://github.com/datafund/swarm_provenance_MCP/issues/296)).

### Upgrading from a `.env` in the repository

Earlier versions read `.env` from the working directory, so the old setup was
`cp .env.example .env` in the repository plus `"cwd": "/path/to/swarm_provenance_MCP"`
in `claude_desktop_config.json`. That file is no longer read: after upgrading, the server
starts with the defaults (no wallet, `CHAIN_ENABLED` unset, so only the read-only chain tools) and only logs a warning.

- Move the file: `mkdir -p ~/.config/swarm-provenance-mcp && mv .env ~/.config/swarm-provenance-mcp/.env`
  (or under `$XDG_CONFIG_HOME` if you set it),
  or keep it where it is and set `SWARM_PROVENANCE_ENV_FILE` to its absolute path in the client's `"env"` block.
- The `"env"` block in `claude_desktop_config.json` is unaffected and still overrides the env file.
- `"cwd"` no longer has any effect on configuration. You can remove it.

### Gateway Options

#### Public Gateway (Recommended)
The MCP server uses the public gateway hosted by DataFund by default at `https://provenance-gateway.datafund.io`. This gateway provides:
- High availability and reliability
- No setup or maintenance required
- Direct access to the Swarm network
- Free to use for development and testing

#### Self-Hosted Gateway
You can also run your own gateway instance for:
- Custom configurations
- Private or isolated environments
- Local development

To use a self-hosted gateway:
1. Clone the gateway repository: `git clone https://github.com/datafund/swarm_connect`
2. Follow the setup instructions in that repository
3. Set `SWARM_GATEWAY_URL=http://localhost:8000` in your env file (`~/.config/swarm-provenance-mcp/.env`) or environment

## Usage

### Running the MCP Server

```bash
swarm-provenance-mcp
```

### Available Tools

#### `purchase_stamp`
Purchase a new postage stamp.

**Parameters:**
- `duration_hours` (int): Duration in hours, minimum 24 (optional, uses default if not provided)
- `size` (string): Stamp size — `"small"`, `"medium"`, or `"large"` (optional, uses default if not provided)
- `depth` (int): Explicit depth override, 17–22, bypasses size (optional, advanced use only)
- `label` (string): Optional label for the stamp, at most 100 characters. If you omit it, the server generates a label of the form `mcp-<hex>`. The result shows it as "Label requested", because the gateway's answer does not confirm it; `list_stamps` shows the label the gateway stored

**Example:**
```json
{
  "name": "purchase_stamp",
  "arguments": {
    "duration_hours": 48,
    "size": "small",
    "label": "my-test-stamp"
  }
}
```

#### `get_stamp_status`
Get detailed information about a specific stamp.

**Parameters:**
- `stamp_id` (string): The batch ID of the stamp

**Example:**
```json
{
  "name": "get_stamp_status",
  "arguments": {
    "stamp_id": "000de42079daebd58347bb38ce05bdc477701d93651d3bba318a9aee3fbd786a"
  }
}
```

#### `list_stamps`
List postage stamps. By default the result shows the 5 usable stamps with the most time left (by `batchTTL`, then by expiry) and a count line, for example `Showing 5 of 47 stamps (12 usable). Pass all=true for the full list.` With `all: true` it lists every stamp in the gateway's order. The hints and `_next` always use the full list. Expiry times are ISO 8601 UTC, for example `2026-12-02T09:11:00Z`; `get_stamp_status`, `check_stamp_health` and the `extend_stamp` result use the same format.

Each stamp's `Status` uses the same rule as `health_check`:
- `Usable`: usable, and its `accessMode` is `shared` or absent. Only these stamps work with `upload_data`.
- `Not usable by this server`: a `pool` stamp (gateway inventory), an `owned` stamp (registered to the wallet that paid for it) or another access mode. This server sends no wallet, so the gateway refuses its uploads.
- `Not usable yet`: a writable stamp that may still be propagating. Poll `check_stamp_health`; do not buy another.
- `Cannot become usable`: expired, about to expire, full, invalid depth, unreadable on the node, or not found on the node.
- `Cannot be used: the batch ID is not in the expected format`: a usable or pending stamp without a valid batch ID. An upload needs the ID, so the server counts it as an entry it cannot read.

`_next` is `upload_data` when a stamp is usable, `check_stamp_health` when a stamp is still propagating, and `purchase_stamp` otherwise. If the stamp list is unknown or incomplete (some entries are not in the expected format, or a usable or pending stamp has a batch ID that is not in the expected format), `_next` is `health_check`, never `purchase_stamp`. If no stamp is usable or pending and a stamp has an access mode the gateway does not document, `_next` is `check_stamp_health`, and the recommendation names that stamp's batch ID. If none of those stamps has a valid batch ID, no ID is named and the result does not recommend a purchase: `list_stamps` gives `_next: health_check` and `health_check` gives `_next: list_stamps`. If the same answer repeats, stop and report it to the gateway operator.

**Parameters:**
- `all` (boolean): List every stamp, not only the 5 usable stamps with the most time left (default: false)

**Example:**
```json
{
  "name": "list_stamps",
  "arguments": {"all": true}
}
```

#### `extend_stamp`
Extend an existing stamp with additional duration.

**Parameters:**
- `stamp_id` (string): The batch ID of the stamp to extend
- `duration_hours` (int): Additional duration in hours, minimum 24

**Example:**
```json
{
  "name": "extend_stamp",
  "arguments": {
    "stamp_id": "000de42079daebd58347bb38ce05bdc477701d93651d3bba318a9aee3fbd786a",
    "duration_hours": 48
  }
}
```

#### `upload_data`
Upload data to the Swarm network with stamp validation. Returns the Swarm reference of the data. For an unsigned upload, the data hash that the chain tools take is the SHA-256 of the uploaded bytes; this tool does not return it. For a notary-signed upload it is not: see `anchor_hash` (this server does not compute it).

**Parameters:**
- `data` (string): Data content to upload (max 4096 bytes)
- `stamp_id` (string): Batch ID of a usable stamp: one that `list_stamps` marks `Usable`, or, only if none is usable and the user approved, one from `purchase_stamp`
- `content_type` (string): MIME type of the content (optional, default: "application/json")
- `sign` (string): Signing method (optional). The only accepted value is `"notary"`; see **Signing with the notary** below. Omit it for an unsigned upload. Any other value returns a validation error

**Example:**
```json
{
  "name": "upload_data",
  "arguments": {
    "data": "{\"message\": \"Hello Swarm!\"}",
    "stamp_id": "000de42079daebd58347bb38ce05bdc477701d93651d3bba318a9aee3fbd786a",
    "content_type": "application/json"
  }
}
```

**Signing with the notary:** Set `sign` to `"notary"` to have the gateway sign the data at upload time. Call `get_notary_info` first to check that signing is available. With `sign: "notary"`, `data` must be a JSON object with a top-level `data` field, for example `{"data": {...}, "signatures": []}`. The `signatures` field is optional and must be an array. The gateway adds its signature to it. `sign: "notary"` adds these 400 refusals to the errors of any upload:
- 400 `INVALID_DOCUMENT_FORMAT` when `data` is not valid UTF-8 JSON, is not a JSON object with a `data` field, or has a `signatures` field that is not an array.
- 400 `JSON_TOO_DEEP` when objects and arrays nest deeper than the gateway's limit. The gateway measures the depth on the whole document, the `{"data": ...}` envelope included. The limit is 64 levels by default, and the gateway operator can change it.
- 400 `NOTARY_NOT_ENABLED` or 400 `NOTARY_NOT_CONFIGURED` when the notary is off.

A signing failure inside the gateway, for example an unusable notary key, returns 500, not 400.

The gateway stores the signed document as `application/json`, whatever `content_type` you send. The result reports the input size as "before signing", because the stored document is larger. The gateway's upload answer has no signature details, so the result says that a notary signature was requested and that the tool did not confirm it. To check the signature, read the `signatures` entry of the stored document (`download_data` returns it) and compare its `signer` with the address that `get_notary_info` returns.

#### `download_data`
Download data from the Swarm network using a Swarm reference. The result includes the SHA-256 of the bytes. For content stored by an unsigned `upload_data`, that is the data hash that the chain tools take; for a notary-signed upload it is the hash of the stored signed document, not the data hash (see `anchor_hash`).

**Parameters:**
- `reference` (string): Swarm reference of the data to download: 64 hexadecimal characters, or 128 for an encrypted reference. Chain tools take only 64-character hashes

**Result:** For text and JSON content, the result has two content blocks. Read both:
1. A summary with the size, the SHA-256 of the original bytes, the `Reference check:` line (below), and the `_next` hints.
2. The content, fenced as untrusted data. Control and bidirectional characters are escaped and the content is cut at about 16 KB, so this view is not byte-exact.

A client that reads only the first block gets the summary, not the payload. Binary (non-UTF-8) content returns a single block that gives the size and SHA-256 and does not include the bytes. See [Safety behavior](#safety-behavior).

**Reference check:** the result has one `Reference check:` line that says what was checked.
- `verified`: the content is at most 4096 bytes, and its single-chunk Swarm address (BMT hash) equals the 64-character reference. The bytes are the chunk that the reference names.
- `not checked`, with the reason. Nothing proves that the bytes belong to the reference:
  - The content's chunk address does not equal the reference. The reference is then probably a manifest. A `/bzz` reference names a manifest root, whose address differs from the content's own chunk address. A Bee 2.8.2 upload recorded for the tests shows this (`tests/fixtures/bee_chunks.json`). The gateway stores data through Bee's `/bzz` endpoint, so references from `upload_data` most likely name a manifest. Only that last point comes from reading the gateway source; it was not confirmed with a live upload through the gateway. This server cannot follow a manifest to its content. A raw chunk endpoint on the gateway that would allow this is tracked in [datafund/swarm_connect#499](https://github.com/datafund/swarm_connect/issues/499).
  - The content is larger than one chunk (4096 bytes).
  - The reference is encrypted (128 hexadecimal characters).

A `not checked` result is not a mismatch. It means the gateway's bytes are shown as received. The result also reports the SHA-256 of the bytes, which you can compare with a hash you trust.

**Example:**
```json
{
  "name": "download_data",
  "arguments": {
    "reference": "a1b2c3d4e5f6789abcdef0123456789abcdef0123456789abcdef0123456789a"
  }
}
```

#### `check_stamp_health`
Run a health check on a specific stamp. Returns whether uploads can proceed, plus any errors or warnings with actionable suggestions.

**Parameters:**
- `stamp_id` (string): The batch ID of the stamp to check

**Example:**
```json
{
  "name": "check_stamp_health",
  "arguments": {
    "stamp_id": "000de42079daebd58347bb38ce05bdc477701d93651d3bba318a9aee3fbd786a"
  }
}
```

#### `get_wallet_info`
Get the gateway node's wallet address and BZZ balance. Useful for checking if the node has sufficient funds. Note: this is a debugging/diagnostic tool and may be removed in future versions.

**Parameters:** None

**Example:**
```json
{
  "name": "get_wallet_info",
  "arguments": {}
}
```

#### `get_notary_info`
Check whether the notary signing service is enabled and available on the gateway.

**Parameters:** None

**Example:**
```json
{
  "name": "get_notary_info",
  "arguments": {}
}
```

#### `health_check`
Check gateway and Swarm network connectivity status. Returns an adaptive status including stamp availability, a `ready` flag indicating whether uploads can proceed, and recommendations for next steps. `ready` is `false` when the gateway's Bee node reports no network (for example no connected peers), even if usable stamps exist. The output includes a `Swarm network:` line with the Bee node state.

**Parameters:** None

**Example:**
```json
{
  "name": "health_check",
  "arguments": {}
}
```

#### `chain_balance` *(optional — requires `CHAIN_ENABLED=true` and `PROVENANCE_WALLET_KEY`)*
Check the on-chain wallet ETH balance used for provenance anchoring. Returns wallet address, balance, chain info, and actionable funding guidance when balance is low.

**Parameters:** None

**Example:**
```json
{
  "name": "chain_balance",
  "arguments": {}
}
```

#### `chain_health` *(on by default; `CHAIN_ENABLED=false` turns it off)*
Test blockchain RPC connectivity for on-chain provenance. Returns connection status, chain name, chain ID, latest block number, and RPC response time. Does not require a wallet key.

**Parameters:** None

**Example:**
```json
{
  "name": "chain_health",
  "arguments": {}
}
```

#### `anchor_hash` *(optional — requires `CHAIN_ENABLED=true` and `PROVENANCE_WALLET_KEY`)*
Register a data hash on the blockchain, creating an immutable provenance record with owner, timestamp, and data type. `data_hash` is the data hash: the SHA-256 of the data. `storage_ref` is the Swarm reference that `upload_data` returned; the two must differ. Example: `upload_data` returns a Swarm reference; for an unsigned upload, the data hash is the SHA-256 of the uploaded bytes (the `SHA-256 of the original bytes` line of `download_data`); then call `anchor_hash(data_hash=<data hash>, storage_ref=<Swarm reference>)`. For a notary-signed upload, the data hash is the SHA-256 of the `data` field as canonical JSON (Python `json.dumps(data, sort_keys=True, separators=(',', ':'))`), the value the notary signs; this server does not compute it, and the `download_data` line is then the hash of the stored signed document. Costs gas. If the hash is already registered to the expected owner (`owner`, or the wallet address), it returns the existing record without error. With `storage_ref`, this holds only when the record already links the same storage reference; otherwise it returns an error that shows the existing reference. If another address owns the record, it returns an error that shows both addresses, and it sends no transaction.

**Parameters:**
- `data_hash` (string, required): The data hash to anchor: the SHA-256 of the data, 64 hex characters. `swarm_hash`, its old name, is deprecated but still accepted
- `data_type` (string): Data type/category (default: `swarm-provenance`). Must not be empty or only whitespace; at most 64 characters and 64 UTF-8 bytes
- `owner` (string): Ethereum address to register as owner (defaults to wallet address; requires delegate authorization for other addresses). The wallet's own address and the zero address are refused; a mixed-case address must have a valid EIP-55 checksum
- `storage_ref` (string): Optional Swarm reference to link bidirectionally: the 64-character hex reference that `upload_data` returned. Enables reverse lookup via `lookup_by_storage_ref`. Works with or without `owner`. With `owner`, the server calls the `registerDataFor` overload that takes a storage reference. A reference equal to `data_hash` or all zeros is refused
- `confirm` (boolean): Set to `true`, together with `quote_id`, to send the transaction. See [Safety behavior](#safety-behavior)
- `quote_id` (string): The `quote_id` from the quote for exactly these arguments (single use, valid for 10 minutes)

**Example:**
```json
{
  "name": "anchor_hash",
  "arguments": {
    "data_hash": "a1b2c3d4e5f6789abcdef0123456789abcdef0123456789abcdef0123456789a",
    "data_type": "provenance-metadata"
  }
}
```

#### `verify_hash` *(on by default; `CHAIN_ENABLED=false` turns it off)*
Check whether a data hash (the SHA-256 of the data) is registered on the blockchain. Returns verified status with basic provenance info (owner, timestamp, data type) if found. A record that an older workflow or the Swarm Provenance CLI anchored with a Swarm reference as its key can be checked by passing that Swarm reference. Read-only — no gas or wallet key required.

**Parameters:**
- `data_hash` (string, required): The data hash to verify: the SHA-256 of the data, 64 hex characters. `swarm_hash`, its old name, is deprecated but still accepted
- `expected_owner` (string): Ethereum address the record must belong to. If the record belongs to another address, the result is an error that shows both addresses

**Example:**
```json
{
  "name": "verify_hash",
  "arguments": {
    "data_hash": "a1b2c3d4e5f6789abcdef0123456789abcdef0123456789abcdef0123456789a"
  }
}
```

#### `get_provenance` *(on by default; `CHAIN_ENABLED=false` turns it off)*
Retrieve the full on-chain provenance record for a data hash (the SHA-256 of the data). Returns owner, registration timestamp, data type, status, transformations, and accessors. A record that an older workflow or the Swarm Provenance CLI anchored with a Swarm reference as its key can be checked by passing that Swarm reference. Read-only — no gas or wallet key required.

**Parameters:**
- `data_hash` (string, required): The data hash to look up: the SHA-256 of the data, 64 hex characters. `swarm_hash`, its old name, is deprecated but still accepted

**Example:**
```json
{
  "name": "get_provenance",
  "arguments": {
    "data_hash": "a1b2c3d4e5f6789abcdef0123456789abcdef0123456789abcdef0123456789a"
  }
}
```

#### `record_transform` *(optional — requires `CHAIN_ENABLED=true` and `PROVENANCE_WALLET_KEY`)*
Record a data transformation on-chain, linking the original data to its transformed version. Creates a verifiable lineage trail. The original hash must already be anchored. Costs gas. If the same `(original → new)` pair is already recorded, returns the existing link without spending gas (idempotent).

**Parameters:**
- `original_hash` (string, required): The data hash of the original data: its SHA-256, 64 hex characters (must be already anchored)
- `new_hash` (string, required): The data hash of the transformed data: its SHA-256, 64 hex characters
- `description` (string): Description of the transformation (max 256 chars, e.g., "Anonymized PII")
- `restrict_original` (boolean): If true, set the original data status to RESTRICTED after recording the transformation (default: false). This sends two transactions, and a quote covers both
- `confirm` (boolean): Set to `true`, together with `quote_id`, to send the transaction. See [Safety behavior](#safety-behavior)
- `quote_id` (string): The `quote_id` from the quote for exactly these arguments (single use, valid for 10 minutes)

**Example:**
```json
{
  "name": "record_transform",
  "arguments": {
    "original_hash": "a1b2c3d4e5f6789abcdef0123456789abcdef0123456789abcdef0123456789a",
    "new_hash": "b2c3d4e5f6789abcdef0123456789abcdef0123456789abcdef0123456789abc",
    "description": "Filtered for region EU",
    "restrict_original": false
  }
}
```

#### `record_merge_transform` *(optional — requires `CHAIN_ENABLED=true` and `PROVENANCE_WALLET_KEY`)*
Record an N-to-1 merge transformation on-chain, combining multiple source hashes into a single new hash. All source hashes must be already anchored. Costs gas. Needs contract v2 or later; see [Contract versions](#contract-versions). The default Base Sepolia contract supports merges.

**Parameters:**
- `source_hashes` (array of strings, required): 2–50 data hashes of the sources to merge (the SHA-256 of each, 64 hex characters)
- `new_hash` (string, required): The data hash of the merged result: its SHA-256, 64 hex characters
- `description` (string): Description of the merge transformation (max 256 chars)
- `new_data_type` (string): Data type for the merged result (default: `"merged"`). Must not be empty or only whitespace; at most 64 characters and 64 UTF-8 bytes
- `confirm` (boolean): Set to `true`, together with `quote_id`, to send the transaction. See [Safety behavior](#safety-behavior)
- `quote_id` (string): The `quote_id` from the quote for exactly these arguments (single use, valid for 10 minutes)

**Example:**
```json
{
  "name": "record_merge_transform",
  "arguments": {
    "source_hashes": [
      "a1b2c3d4e5f6789abcdef0123456789abcdef0123456789abcdef0123456789a",
      "b2c3d4e5f6789abcdef0123456789abcdef0123456789abcdef0123456789abc"
    ],
    "new_hash": "c3d4e5f6789abcdef0123456789abcdef0123456789abcdef0123456789abcde",
    "description": "Merged EU and US datasets",
    "new_data_type": "merged-dataset"
  }
}
```

#### `get_provenance_chain` *(on by default; `CHAIN_ENABLED=false` turns it off)*
Follow the transformation lineage for a data hash (the SHA-256 of the data). The tool reads the links from the contract state. Shows how data evolved — from original to each derived version. Read-only — no gas or wallet key required. For the contract versions it works on, see [Contract versions](#contract-versions).

Each node in the output lists its parent hashes, and each transformation prints as an edge: `Edge: <parent_hash> -> <child_hash>: "<description>"`.

The tool returns the nodes it could read. When the lineage is partial, the output starts with an `INCOMPLETE` banner and `_next` is `get_provenance_chain`. The banner lists:
- each node whose record or links could not be read, with a fixed label and the exception class name;
- each node that `max_depth` stopped at, marked "not expanded" (this is not an error).

The banner ends with a `Retry:` line: call `get_provenance_chain` on the named hash, or with a larger `max_depth`. RPC transport errors are not reported this way: the server fails over to the next RPC endpoint, or returns a retryable error.

**Parameters:**
- `data_hash` (string, required): The data hash to trace lineage for: the SHA-256 of the data, 64 hex characters. `swarm_hash`, its old name, is deprecated but still accepted. A record that an older workflow or the Swarm Provenance CLI anchored with a Swarm reference as its key can be checked by passing that Swarm reference.
- `max_depth` (integer): Maximum depth to traverse (default: 10, range: 1–50)

**Example:**
```json
{
  "name": "get_provenance_chain",
  "arguments": {
    "data_hash": "a1b2c3d4e5f6789abcdef0123456789abcdef0123456789abcdef0123456789a",
    "max_depth": 10
  }
}
```

#### `set_storage_ref` *(optional — requires `CHAIN_ENABLED=true` and `PROVENANCE_WALLET_KEY`)*
Attach a Swarm reference to an existing on-chain record, keyed by its data hash. Set-once: cannot be changed after first write. Owner-only. Useful after `record_transform` to link the transformed data's Swarm storage location.

**Parameters:**
- `data_hash` (string, required): The data hash of the existing on-chain record: the SHA-256 of the data, 64 hex characters
- `storage_ref` (string, required): The Swarm reference to link: the 64-character hex reference that `upload_data` returned
- `confirm` (boolean): Set to `true`, together with `quote_id`, to send the transaction. See [Safety behavior](#safety-behavior)
- `quote_id` (string): The `quote_id` from the quote for exactly these arguments (single use, valid for 10 minutes)

**Example:**
```json
{
  "name": "set_storage_ref",
  "arguments": {
    "data_hash": "a1b2c3d4e5f6789abcdef0123456789abcdef0123456789abcdef0123456789a",
    "storage_ref": "b2c3d4e5f6789abcdef0123456789abcdef0123456789abcdef0123456789abc"
  }
}
```

#### `lookup_by_storage_ref` *(on by default; `CHAIN_ENABLED=false` turns it off)*
Reverse lookup: find the on-chain provenance record by its Swarm reference (the linked `storage_ref`). Returns the linked data hash and full provenance record if found. Read-only — no gas or wallet key required.

**Parameters:**
- `storage_ref` (string, required): The Swarm reference to look up, 64 hex characters

**Example:**
```json
{
  "name": "lookup_by_storage_ref",
  "arguments": {
    "storage_ref": "b2c3d4e5f6789abcdef0123456789abcdef0123456789abcdef0123456789abc"
  }
}
```

### Response Format

All tool responses include structured metadata to help agents chain operations efficiently:

#### Success Responses

Every successful response appends workflow hints:

```
_next: <recommended_tool>        # The logical next tool to call
_related: <tool1>, <tool2>       # Other relevant tools
```

Hints are contextual. For example, `list_stamps` suggests `_next: upload_data` when a stamp this server can write to is usable, `_next: check_stamp_health` when such a stamp is still propagating, and `_next: purchase_stamp` otherwise. Two cases never suggest a purchase: an unknown or incomplete stamp list gives `_next: health_check`, and a stamp with an access mode the gateway does not document gives `_next: check_stamp_health` and names its batch ID. If none of those stamps has a valid batch ID, no ID is named and the result does not recommend a purchase: `list_stamps` gives `_next: health_check` and `health_check` gives `_next: list_stamps`. If the same answer repeats, stop and report it to the gateway operator.

#### Error Responses

Error responses include structured recovery information:

```
retryable: true|false            # Whether retrying the same call may succeed
_next: <recovery_tool>           # Tool to call for recovery
```

- **retryable: true** — transient errors (timeouts, rate limits, 502/503/504). Wait and retry. An answer from the gateway that this server cannot read (not valid JSON, incomplete, or with a corrupt encoding) is also `retryable: true` for read tools.
- **retryable: false** — permanent errors (validation failures, unknown tools). Fix input and try a different approach.
- **Outcome unknown**: the request reached the gateway, but no usable answer came back, so the write may already have happened. These results are `retryable: false`. They cover these cases:
  - `purchase_stamp` and `extend_stamp`: an answer that cannot be read (not valid JSON, a number too long to convert, nested too deeply, a corrupt content encoding, or an incomplete body), a read timeout, a connection dropped after the request was sent, HTTP 500, 502 or 504, and an answer that is not an object. For `purchase_stamp`, also an answer without a valid batch ID. Do not repeat the write: follow `_next` (`list_stamps` or `get_stamp_status`) to check whether it happened.
  - `upload_data`: only an answer that cannot be read, or an answer without a valid reference. The reference is unknown. Uploading the same data again normally returns the same reference and uses stamp capacity again; for a signed upload (`sign: "notary"`), do not upload again, because it creates a second signed document.
- **Payment required (HTTP 402)** — the gateway has x402 payment enabled and refused a write (`purchase_stamp`, `upload_data`, `extend_stamp`). The error shows the gateway's payment details (amount, asset, network, recipient) and the options. The details come from the x402 v2 `PAYMENT-REQUIRED` header when the gateway sends one, else from the response body. When the gateway offers a version other than v1, the error also names the offered versions (for example `x402 versions offered: v1 and v2`). This server cannot pay x402 requests yet. The error names the paid alternative for a similar operation, run from start to end outside this server: for `purchase_stamp` and `upload_data`, the CLI (`swarm-prov-upload --gateway-url <gateway URL> --x402 upload --file <path>`, which buys a stamp and uploads) or the SDK (`@datafund/swarm-provenance`, `ProvenanceClient` with `gatewayUrl` set to `<gateway URL>` and the `payment` option; set the payment network to the gateway's network. That option's format differs between SDK releases, so follow the README of the installed version); for `extend_stamp`, the CLI (`swarm-prov-upload --gateway-url <gateway URL> --x402 stamps extend <stamp_id> --duration-hours <hours>`; the released CLI 0.12.0 cannot pay for an extension, so install the CLI from its development branch), because the SDK has no method that extends a stamp. Set the CLI's network and wallet as its [docs/x402-setup.md](https://github.com/datafund/swarm_provenance_CLI/blob/main/docs/x402-setup.md) says.

#### Adaptive Health Check

The `health_check` tool returns additional fields:

```
ready: true|false                # Whether the system is ready for uploads
_recommendations:                # Actionable suggestions (only when issues exist)
  - No stamps found — purchase one before uploading
_gateway: <url> (connected|unreachable)
```

The output also has a `Swarm network:` line: `connected (...)`, `DEGRADED (...)`, or `not reported by gateway`. When it is `DEGRADED`, `ready` is `false` even if stamps are usable, because uploads may not propagate.

`ready` is true only when the server has a usable stamp it can write to: a `shared` stamp, or one without an `accessMode` (older gateways). `pool` stamps (gateway inventory) and `owned` stamps (registered to the wallet that paid for them) are counted separately, because the gateway refuses writes from this server to them. A writable stamp that is not usable yet (still propagating) leads to `_next: check_stamp_health`, not to a purchase.

Three more cases never lead to a purchase:
- A degraded Swarm network with a usable stamp gives `_next: health_check`. Call it again after the gateway operator fixes the Bee node's network connection.
- An unknown or incomplete stamp list gives `_next: list_stamps`. If the same answer repeats, stop and report it to the gateway operator.
- An access mode the gateway does not document, with no usable or pending stamp, gives `_next: check_stamp_health`, and the recommendation names the stamp's batch ID. If none of those stamps has a valid batch ID, no ID is named and the result does not recommend a purchase: `list_stamps` gives `_next: health_check` and `health_check` gives `_next: list_stamps`. If the same answer repeats, stop and report it to the gateway operator.

### Safety behavior

These behaviors apply to the chain tools that are listed: the read-only ones by default, all of them with `CHAIN_ENABLED=true`. The spending rules apply to the write tools, which only `CHAIN_ENABLED=true` lists.

#### Confirmed quotes for spending tools

`anchor_hash`, `record_transform`, `record_merge_transform` and `set_storage_ref` accept `confirm` and `quote_id`. A confirmed quote is required on mainnet (any chain ID other than 84532, 11155111, 31337 or 1337), or on any chain when `CHAIN_REQUIRE_CONFIRM=true`. `CHAIN_REQUIRE_CONFIRM=false` turns it off. On the testnet default it is off. When confirmation is off, there is no quote step: `confirm` and `quote_id` are ignored and an ordinary call sends the transaction directly.

When confirmation is required:

1. Call the tool with its normal arguments. The server sends no transaction and returns a quote: the action, the number of transactions, the fee cap, the current fee, the remaining session budget, and a `quote_id`.
2. Get the user's approval.
3. Call the tool again with the same arguments, `confirm: true`, and the `quote_id`.

A `quote_id` is valid for 10 minutes, works once, and works only for the tool and arguments it was issued for. Hash and address values are compared without regard to letter case. It is valid only in the server process that issued it, so a restart invalidates it. If `confirm` is `true` but the `quote_id` is missing, expired, used or for different arguments, the server sends nothing and returns a new quote. This no-send behavior applies only when confirmation is required.

#### Untrusted content

Text that anyone can write is treated as data, not instructions: `download_data` content, stamp labels, and on-chain data types and transformation descriptions. Multi-line content, such as a download, is placed in a fenced block with a warning, escaped, and capped at 16 KB. Single-line fields are quoted and capped at 200 characters. Lines in this content that look like server hints are marked as quoted. On-chain lists (transformations, accessors) show at most 50 entries.

#### RPC address redaction

An RPC URL can carry an API key in its path or query. Chain tool results show the configured RPC URLs as scheme, host and port only. In server logs, RPC URLs in error text are cut the same way: credentials, path and query are removed and scheme, host and port are kept. Connection labels in log messages show the host only. The gateway URL appears in the start-up log and in `health_check` without credentials, query or fragment. Request errors never include the gateway URL's credentials, query or fragment. Faucet and explorer links are not changed.

#### Chain ID pinning

For `CHAIN_NAME=base-sepolia` and `CHAIN_NAME=base`, the chain ID is fixed by the preset and never taken from the RPC. At start-up the server checks the primary RPC. If it reports another chain, the server refuses to start. Your MCP client may then show only a generic message such as "server disconnected"; the reason is in the server's stderr log. Each endpoint, including each `CHAIN_RPC_URLS` fallback, is also checked before its first read, and a write checks the chain ID again before signing. If every fallback is on the wrong chain, the tool returns a chain ID mismatch error. Only `CHAIN_NAME=localhost` with a custom RPC detects the chain ID from the RPC. Fix `CHAIN_RPC_URL`, `CHAIN_RPC_URLS` or `CHAIN_NAME` so they match.

#### Contract check

Before the first write, the server checks that `CHAIN_CONTRACT` (or the preset address) holds DataProvenance contract code. If every endpoint that answers reports no code, the write tools are disabled and report the reason, `health_check` and `chain_health` report it too, and the server checks again before each write. Read-only tools are not disabled by this write check and can still be called. They use the same configured contract, so they can return their own RPC or contract-read errors. If no RPC answers in time, the check runs again before the first transaction.

### MCP Prompts

The server provides workflow prompts that agents can invoke via `prompts/list` and `prompts/get`. These give step-by-step instructions for common tasks:

| Prompt | Description | Arguments |
|--------|-------------|-----------|
| `provenance-upload` | Upload data to Swarm: health check, stamp selection, upload, download to confirm | `data` (required), `content_type` (optional) |
| `provenance-verify` | Download existing data by reference, inspect it, and report what the reference check covered | `reference` (required) |
| `stamp-management` | Review stamp inventory, diagnose issues, recommend actions | none |
| `provenance-chain-workflow` | End-to-end on-chain provenance: store, anchor, and optionally record a transformation | `data` (required), `transform_description` (optional) |

### MCP Resources

The server exposes on-demand knowledge resources that agents can load via `resources/list` and `resources/read`:

| Resource URI | MIME Type | Description |
|-------------|-----------|-------------|
| `provenance://skills` | `text/markdown` | Provenance skills guide — concepts, critical rules, workflows, diagrams, and error recovery |

## Docker

### Building

```bash
docker build -t swarm-provenance-mcp .
```

To tag with a specific version:

```bash
docker build --build-arg VERSION=0.3.2 -t swarm-provenance-mcp:0.3.2 .
```

### Running

The server communicates via stdio (no ports). Pass `-i` for interactive stdin:

```bash
docker run -i --rm swarm-provenance-mcp
```

Configure the gateway URL and other settings via environment variables:

```bash
docker run -i --rm \
  -e SWARM_GATEWAY_URL=https://provenance-gateway.datafund.io \
  -e DEFAULT_STAMP_SIZE=medium \
  swarm-provenance-mcp
```

### Claude Desktop with Docker

Add to your `claude_desktop_config.json`:

```json
{
  "mcpServers": {
    "swarm-provenance": {
      "command": "docker",
      "args": ["run", "-i", "--rm", "swarm-provenance-mcp"]
    }
  }
}
```

## Architecture

```
┌─────────────────┐    ┌─────────────────┐    ┌─────────────────┐
│   AI Agents     │◄──►│  MCP Server     │◄──►│ swarm_connect   │
│                 │    │                 │    │   Gateway       │
│ • Claude        │    │ • Tool handlers │    │                 │
│ • Other LLMs    │    │ • Gateway client│    │ • Purchase API  │
│ • Custom agents │    │ • Chain client  │    │ • Status API    │
└─────────────────┘    │ • Error handling│    │ • Extension API │
                       └────────┬────────┘    └─────────┬───────┘
                                │                       │
                       ┌────────▼────────┐    ┌─────────▼───────┐
                       │  Base Sepolia   │    │  Swarm Network  │
                       │  (DataProv.     │    │   (Bee Node)    │
                       │   Contract)     │    └─────────────────┘
                       └─────────────────┘
```

### Components

- **MCP Server**: Exposes tools via the Model Context Protocol
- **Gateway Client**: HTTP client for communicating with swarm_connect
- **Chain Client** (optional): On-chain provenance via DataProvenance smart contract on Base Sepolia
- **Configuration**: Environment-based settings management
- **Error Handling**: Comprehensive error handling and logging

## Claude Desktop Integration

### Setup Instructions

1. **Install Claude Desktop**: Download from [claude.ai](https://claude.ai/download)

2. **Clone and set up this repository**:
```bash
# Clone the repository
git clone https://github.com/datafund/swarm_provenance_MCP.git

# Navigate to the project directory
cd swarm_provenance_MCP

# Create and activate virtual environment
python3 -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install the package in development mode
pip install -e .

# Configure environment variables
mkdir -p ~/.config/swarm-provenance-mcp
cp .env.example ~/.config/swarm-provenance-mcp/.env
# Edit that file if you need to customize gateway URL or defaults
```

3. **Configure MCP Server**: Add to Claude Desktop's configuration file:

**macOS** (`~/Library/Application Support/Claude/claude_desktop_config.json`):
```json
{
  "mcpServers": {
    "swarm-provenance": {
      "command": "/path/to/swarm_provenance_MCP/run.sh"
    }
  }
}
```

**Linux**: the macOS path above does not exist on Linux. Use the same `command` in the configuration file of your MCP client.

**Windows** (`%APPDATA%\Claude\claude_desktop_config.json`):
```json
{
  "mcpServers": {
    "swarm-provenance": {
      "command": "C:\\path\\to\\swarm_provenance_MCP\\venv\\Scripts\\python.exe",
      "args": ["-m", "swarm_provenance_mcp.server"]
    }
  }
}
```

*Note: Replace `/path/to/swarm_provenance_MCP` with the actual path where you cloned the repository.*

The `run.sh` launcher (macOS/Linux) starts the server from the repo's venv, with the repo as working directory. The working directory does not affect configuration: settings come from the env file described above, and a `.env` in the repository is no longer read (see [Upgrading from a `.env` in the repository](#upgrading-from-a-env-in-the-repository); the server logs a warning if it finds one). If the venv stops working — typically after moving or renaming the repo directory — it rebuilds it automatically. Make it executable once with `chmod +x run.sh`.

The rebuild installs all dependencies and can take a few minutes, longer than an MCP client waits for the server on startup. If the first launch after a move times out, run `./run.sh < /dev/null` once in a terminal to finish the rebuild, then restart the client. The Windows config above does not use the launcher yet; after moving the repo on Windows, recreate the venv manually.

**Alternative (if package is installed)**: You can use `"command": "swarm-provenance-mcp"` instead after running `pip install -e .`

#### With on-chain provenance

The read-only chain tools are on without any setting. To enable blockchain anchoring (the write tools), add an `"env"` block to the config. You can use this instead of (or alongside) the env file at `~/.config/swarm-provenance-mcp/.env`:

```json
{
  "mcpServers": {
    "swarm-provenance": {
      "command": "/path/to/swarm_provenance_MCP/run.sh",
      "env": {
        "CHAIN_ENABLED": "true",
        "PROVENANCE_WALLET_KEY": "0x...your_private_key_here..."
      }
    }
  }
}
```

Read-only chain tools (`verify_hash`, `get_provenance`, `get_provenance_chain`, `lookup_by_storage_ref`, `chain_health`) are on by default and work without `PROVENANCE_WALLET_KEY`. Write tools (`anchor_hash`, `record_transform`, `record_merge_transform`, `set_storage_ref`) and `chain_balance` require a funded wallet — see [Chain Anchoring](#chain-anchoring-optional) for details.

#### Docker-based

Use Docker for a zero-install experience — no Python, venv, or pip required:

```json
{
  "mcpServers": {
    "swarm-provenance": {
      "command": "docker",
      "args": [
        "run", "-i", "--rm",
        "-e", "SWARM_GATEWAY_URL=https://provenance-gateway.datafund.io",
        "ghcr.io/datafund/swarm-provenance-mcp"
      ]
    }
  }
}
```

The read-only chain tools are on in the image too. To enable chain anchoring (the write tools) with Docker, add the chain env vars:

```json
{
  "mcpServers": {
    "swarm-provenance": {
      "command": "docker",
      "args": [
        "run", "-i", "--rm",
        "-e", "SWARM_GATEWAY_URL=https://provenance-gateway.datafund.io",
        "-e", "CHAIN_ENABLED=true",
        "-e", "PROVENANCE_WALLET_KEY=0x...your_private_key_here...",
        "ghcr.io/datafund/swarm-provenance-mcp"
      ]
    }
  }
}
```

> **Docker Desktop MCP Toolkit**: If you use Docker Desktop with MCP Toolkit support, the server can be discovered automatically from the Docker MCP catalog.

4. **Restart Claude Desktop** and verify the connection.

### Getting Started

After setup, try these prompts in Claude Desktop to verify everything works:

**Check connectivity:**
> "Run a health check on the Swarm gateway"

Expected: a status showing `healthy`, gateway URL, and response time. If chain is enabled, you'll also see RPC connectivity and wallet balance info.

**List stamps:**
> "List all available Swarm stamps"

Expected: the usable postage stamps with the most time left and a count line, or a message that no stamps exist yet (with a suggestion to purchase one).

**Full upload workflow:**
> "Upload the text 'Hello Swarm!' to the Swarm network"

Claude will walk through: finding a usable stamp (or, with your approval, purchasing one and waiting for it to propagate), uploading the data, and returning the Swarm reference.

**Verify on-chain (if chain enabled):**
> "Anchor the uploaded hash on-chain and verify it"

Claude will register the hash on the blockchain and confirm the provenance record.

### Troubleshooting Setup

If Claude Desktop doesn't show the Swarm tools:
1. Check the config file path is correct for your OS
2. Verify the `command` path points to `run.sh` in your clone (macOS/Linux), or to the Python executable inside your venv (Windows)
3. Check Claude Desktop logs: **Help > Show Logs** (look for MCP connection errors)
4. Test manually: run `./run.sh` (or `swarm-provenance-mcp`) in your terminal — it should start without errors and wait for MCP input. Launcher messages, including a venv rebuild, go to stderr

## Development

### Testing

```bash
# Install development dependencies
pip install -e .[dev]

# Run tests
pytest

# Run with coverage
pytest --cov=swarm_provenance_mcp
```

### Docker Testing

Requires Docker daemon to be running:

```bash
# Run all Docker tests (builds image, tests MCP protocol, tool calls)
pytest tests/test_docker.py -v -m docker

# Verify manually
docker build -t swarm-provenance-mcp .
docker run -i --rm swarm-provenance-mcp                           # starts, waits for MCP input
docker run -i --rm -e SWARM_GATEWAY_URL=https://provenance-gateway.datafund.io swarm-provenance-mcp  # env override
```

### Code Quality

```bash
# Format code
black swarm_provenance_mcp/

# Lint code
ruff check swarm_provenance_mcp/
```

### Editor settings

The repository commits no editor hooks, MCP servers or auto-approve settings. Shared editor settings take effect on every contributor's machine, so keep your own in per-developer configuration:

- `.claude/settings.local.json` for project hooks and permissions. It is gitignored, and so is any `.claude/*.local.json`.
- `~/.claude/settings.json` for settings that apply to all your projects.
- `claude mcp add --scope local` or `--scope user` for MCP servers, instead of a committed `.mcp.json`.

These files count as committed editor settings: `.claude/**/*.json` except `*.local.json`, `.mcp.json`, `.cursor/mcp.json` and `.cursor/hooks.json`. They must not contain:

- A `hooks`, `mcpServers`, `statusLine` or `apiKeyHelper` key. Each one runs a command or connects to an MCP server.
- A non-empty `permissions.allow`.
- An `enableAllProjectMcpServers` or `enabledMcpjsonServers` key.
- A `permissions.defaultMode` other than `"default"`.

`tests/test_editor_settings.py` enforces these rules.

## Dependencies

- **Core Dependencies**:
  - `mcp>=1.26,<2`: Model Context Protocol framework (the server is written for mcp 1.x)
  - `requests>=2.31.0,<3`: HTTP client for gateway communication
  - `pydantic>=2.0.0,<3` and `pydantic-settings>=2.0,<3`: Data validation and settings
  - `python-dotenv>=1.0.0,<2`: Environment configuration
  - `web3>=7,<8`: Ethereum blockchain interaction for on-chain provenance
  - `eth-account>=0.13,<0.14`: Wallet and transaction signing for chain anchoring
  - `eth-utils>=5,<6`: Hashing helpers (keccak) used directly by the chain module

  Upper bounds are deliberate. Raise one only together with a passing test run
  against the new version.

- **Development Dependencies**:
  - `pytest`: Testing framework
  - `pytest-asyncio`: Async testing support
  - `pytest-mock`: Mocking utilities
  - `black`: Code formatting
  - `ruff`: Linting

## Integration with AI Agents

This MCP server is designed to work with AI agents that support the Model Context Protocol. Agents can use the provided tools to:

1. **Manage stamp inventory**: Purchase and extend stamps as needed
2. **Monitor usage**: Check stamp status and utilization
3. **Optimize costs**: List stamps to find the most suitable ones for tasks
4. **Store provenance data**: Upload data with provenance metadata to immutable decentralized storage
5. **Retrieve records**: Download records from the Swarm network by reference. Content up to 4 KB is checked against a reference that names a single chunk; see the `download_data` reference check
6. **Data lifecycle management**: Handle complete provenance workflows from creation to retrieval and on-chain checks
7. **Track data lineage**: Record transformations and trace the full provenance chain of derived data
8. **Automate workflows**: Integrate stamp management and data storage into larger AI workflows

## Troubleshooting

### Common Issues

1. **Connection errors**: Ensure the swarm_connect gateway is running and accessible
2. **Authentication errors**: Check that the gateway doesn't require authentication
3. **Invalid stamp IDs**: Verify stamp IDs are valid batch IDs from the Swarm network
4. **Timeout errors**: Increase timeout values if operations are taking too long
5. **Chain: "wallet key not configured"**: Set `PROVENANCE_WALLET_KEY` in the MCP client's `"env"` block or in the env file (`~/.config/swarm-provenance-mcp/.env`, see [Configuration](#configuration)) for write operations (`anchor_hash`, `record_transform`, `set_storage_ref`). Read-only tools work without it. If the error says the key is not a valid private key, fix the value (64 hexadecimal characters, with or without 0x) and restart the server.
6. **Chain: "insufficient funds"**: Fund your wallet with testnet ETH (Base Sepolia faucet) or bridge ETH to Base mainnet. Run `chain_balance` for guidance.
7. **Chain: "already registered"**: The hash is already anchored on-chain. If the result is an error, another address owns the record. Registration is first-come, so to anchor a record you own, change the content, upload it again, and anchor the new hash. Use `get_provenance` to view the existing record.
8. **Chain: "transformation already recorded"**: The `(original → new)` link already exists on-chain. No gas spent — use `get_provenance_chain` to verify the lineage.
9. **Chain: "too few/many sources"**: `record_merge_transform` requires 2–50 source hashes. Adjust the `source_hashes` array accordingly.

### Logging

The server logs important events and errors. To increase logging verbosity:

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

## License

MIT License - see LICENSE file for details.
