Metadata-Version: 2.5
Name: iwym-mcp
Version: 0.2.1
Summary: MCP server for iwym catalog (assets/instruments/tokens) and radar opportunity queries
Author: cyetstar
License-Expression: MIT
Keywords: assets,iwym,mcp,radar
Requires-Python: >=3.11
Requires-Dist: httpx>=0.27.0
Requires-Dist: mcp>=1.9.0
Description-Content-Type: text/markdown

# iwym-mcp

stdio MCP server for iwym **catalog maintenance** (assets / instruments / asset_tokens) and **radar opportunity queries** (spreads / funding_opps). It wraps the operator HTTP API.

Does **not** place exchange orders and does **not** touch `auto_exec`.

| | |
|---|---|
| Package | `iwym-mcp` |
| Entry | `iwym-mcp` → `iwym_mcp.server:main` |
| Python | ≥ 3.11 |
| Cursor server key | `iwym` |

## Requirements

- [uv](https://docs.astral.sh/uv/) (recommended) or pip
- A reachable iwym API
- Operator token from **Settings → System → OPERATOR API**

| Env | Meaning |
|---|---|
| `IWYM_API_BASE_URL` | API origin only (e.g. `http://127.0.0.1:8000` or Tailscale `http://jp2:8000`). **No** `/api/v1` suffix |
| `IWYM_OPERATOR_TOKEN` | Bearer from Settings → System → OPERATOR API (Generate / Reset; shown once) |

Missing env vars → process exits 1 with a stderr hint. Cookie login is unchanged; this Bearer works in any `APP_ENV` and does not need CSRF. Do not use `AUTH_ALLOW_BEARER` JWTs for this MCP.

## Use (local checkout)

```bash
cd mcp/iwym
uv sync
export IWYM_API_BASE_URL=http://127.0.0.1:8000
export IWYM_OPERATOR_TOKEN=paste-from-settings-system-operator-api
uv run iwym-mcp   # stdio; waits for the MCP host
```

Tests:

```bash
cd mcp/iwym
uv sync
uv run python -m unittest discover -s tests -v
```

## Use (Cursor)

Point Cursor at this directory (dev) or at an installed `iwym-mcp` on `PATH` (after package install).

**From checkout:**

```json
{
  "mcpServers": {
    "iwym": {
      "command": "uv",
      "args": [
        "--directory",
        "/ABS/PATH/iwym-next/mcp/iwym",
        "run",
        "iwym-mcp"
      ],
      "env": {
        "IWYM_API_BASE_URL": "http://127.0.0.1:8000",
        "IWYM_OPERATOR_TOKEN": "paste-from-settings-system-operator-api"
      }
    }
  }
}
```

**After install** (wheel / `uv tool` / venv with `iwym-mcp` on `PATH`):

```json
{
  "mcpServers": {
    "iwym": {
      "command": "iwym-mcp",
      "env": {
        "IWYM_API_BASE_URL": "http://127.0.0.1:8000",
        "IWYM_OPERATOR_TOKEN": "paste-from-settings-system-operator-api"
      }
    }
  }
}
```

## Build (package)

Bump `version` in `pyproject.toml` when cutting a release (currently `0.2.1`).

```bash
cd mcp/iwym
uv sync
uv build
# → dist/iwym_mcp-<version>-py3-none-any.whl
# → dist/iwym_mcp-<version>.tar.gz
```

`dist/` is gitignored. Install a local wheel:

```bash
uv pip install dist/iwym_mcp-*.whl
# or into an isolated tool env:
uv tool install dist/iwym_mcp-*.whl
```

## Publish

Publish the artifacts from `uv build` to the index you use (public PyPI or a private index).

```bash
cd mcp/iwym
uv build
# Public PyPI (token via UV_PUBLISH_TOKEN or keyring):
uv publish
# Private index:
uv publish --index-url https://your-index.example/simple/ --token "$UV_PUBLISH_TOKEN"
```

Consumers then:

```bash
uv pip install iwym-mcp
# or: uv tool install iwym-mcp
iwym-mcp
```

## Tools

### Assets

| Tool | HTTP | Notes |
|------|------|--------|
| `list_assets` | `GET /api/v1/assets` | Flat array; includes disabled; each row has `tokens[]` and optional `disambiguation` |
| `get_asset` | `GET /api/v1/assets/{id}` | Includes `tokens[]` and `disambiguation` — no separate list-tokens tool |
| `catalog_diff` | `GET /api/v1/assets/catalog-diffs` | `venue` optional; `include_orphans` default false |
| `create_or_upsert_asset` | `PUT /api/v1/assets/{id}` | Tool default `enabled=false`; does not rewrite existing scalars, including `disambiguation` |
| `update_asset` | `PATCH /api/v1/assets/{id}` | kind / enabled / display_symbol / oracle / anchor / disambiguation |
| `set_enabled` | `PATCH /api/v1/assets/{id}` | Radar whitelist toggle only |
| `soft_disable_asset` | `DELETE /api/v1/assets/{id}` | `enabled=false`; keeps row |

`disambiguation` is an optional human note (nullable, not unique) for rows that share a ticker. `list_assets` and `get_asset` return it. `create_or_upsert_asset` writes it only when the row is created; `update_asset` changes it (`clear_disambiguation=true` stores null). It is not a pairing key, scan base, or catalog-link key. `display_symbol` stays the unique-match ticker.

Asset `kind`: `spot_crypto` · `stable` · `wrapped` · `leveraged_token` · `equity`.

### Instruments

| Tool | HTTP |
|------|------|
| `list_asset_instruments` | `GET /api/v1/assets/{id}/instruments` |
| `search_instruments` | `GET /api/v1/exchanges/instruments/search` |
| `link_instrument` | `PATCH /api/v1/exchanges/instruments/{id}/link` |
| `sync_instruments` | `POST /api/v1/exchanges/instruments/sync` (hits upstream; heavy) |

### Tokens

| Tool | HTTP |
|------|------|
| `create_asset_token` | `POST /api/v1/assets/{id}/tokens` |
| `update_asset_token` | `PATCH /api/v1/assets/{id}/tokens/{chain}/{address}` |
| `delete_asset_token` | `DELETE …` |
| `reassign_asset_token` | `POST /api/v1/assets/tokens/reassign` |
| `migrate_asset_token` | `POST /api/v1/assets/tokens/migrate` |
| `lookup_asset_tokens` | `POST /api/v1/assets/token-lookups` |

Fields: `chain_key` · `address` · `symbol` · `decimals` · `status` (`trading`\|`offline`) · `enabled`.

### Radar opportunities (read-only)

| Tool | HTTP |
|------|------|
| `list_spreads` | `GET /api/v1/radar/spreads` |
| `list_funding_opps` | `GET /api/v1/radar/funding-opps` |

Default `limit=100`, max `300`. Filters mirror Console Radar boards. No WebSocket board subscription.

## Suggested catalog loop

1. `catalog_diff(venue="binance")`.
2. For one `missing` entry: check aliases / leverage / equity before writing.
3. `create_or_upsert_asset(..., enabled=false)`.
4. Add chain contracts with `create_asset_token` when needed; link CEX rows via Sync or `link_instrument`.
5. `set_enabled` only when the asset should enter the Radar scan set.

## Security

- Never commit `IWYM_OPERATOR_TOKEN`.
- The token has operator powers (whitelist / instruments / radar reads); it is not an exchange trading key.
