Skip to main content
Glama
SonionMap MCP Server

SonionMap MCP Server

README.md
# SonionMaper

SonionMaper is a local-first tool for **manually mapping the internet**, with a focus on `.onion` links. Each **link** (called a `node` internally) represents a website and stores a name, URL, notes, status, free-choice color, and tags. Legacy `category` metadata is still accepted by headless tools and layout. You can also create **docs** written in Markdown with images, add standalone image nodes and whiteboards for drawings or formulas, and connect them with edges to build an interactive graph.

The project has two layers that share the same data model:

- **Web UI** for people, persisted in `localStorage`.
- **Headless layer** for scripts and AI agents: MCP server, REST API, and CLI, persisted in `~/.sonionmap/db.json`.

The UI performs a **pull sync** from the API on demand: press **↻** to merge maps created by an agent. A fresh installation starts with no maps.

## Features

- Create, edit, move, and delete links, docs, image nodes, whiteboards, and edges.
- Any color can be assigned to a link; colors are visual only and do not imply a category or meaning.
- Markdown docs with GFM tables, links, task lists, and inline images stored in the map database; the doc card shows the document title, a text preview and its first image.
- Standalone image nodes that can be opened in the foreground to inspect the full image.
- Whiteboard cards with an infinite pan/zoom canvas for handwritten formulas and sketches (any color, last 5 colors, stroke width, eraser, undo, clear, fit).
- Connect docs, images and whiteboards to links or each other, and open them with a click on the canvas.
- Statuses, notes, tags, legacy categories, and full-text search across links and docs.
- Multiple maps with JSON import/export.
- Overlap-free graph layouts: `auto`, `force`, `categories`, `grid`, and `radial`.
- MCP server with 32 `sonionmap_*` tools.
- Local REST API with optional token authentication and SSE change events.
- JSON CLI output designed for agents.
- Atomic JSON storage with cross-process locking.

## Requirements

- Node.js 20 or newer (tested with Node 24).
- npm.

## Quick start

```bash
npm install
npm run dev:all
```

Open `http://localhost:5173`. `dev:all` starts both the UI and the REST API at `http://127.0.0.1:4321`.

To run only the UI:

```bash
npm run dev
```

Without the API, the UI still works with `localStorage`, but it will not see maps created by agents.

## Using the UI

- **Double-click** the canvas to create a link.
- Select a link to edit it in the right sidebar and choose any color.
- Click **New Doc** to create a Markdown document; click a doc card on the canvas to edit it.
- Click **New Image** to add a standalone image node; click it to open the full image in the foreground.
- Click **New Whiteboard** to add a board; hold Space or use the hand tool to pan, scroll or use +/- to zoom, and pick any pen color.
- Drag the colored handle on a card to connect links, docs, images and whiteboards.
- Use **Auto layout** to redistribute the graph and **↻** to sync server maps.
- The maximize button fits the camera to the entire graph.
- Export and import maps from the top bar.

## Using SonionMaper from AI agents

The recommended option is the MCP server. Start it over stdio:

```bash
npm run mcp
# or
./bin/sonionmap-mcp
```

Example client configuration:

```json
{
  "mcpServers": {
    "sonionmap": {
      "command": "bash",
      "args": ["/absolute/path/to/SonionMap/bin/sonionmap-mcp"]
    }
  }
}
```

The CLI is also available:

```bash
./bin/sonionmap help
./bin/sonionmap maps
./bin/sonionmap create-map "My onion map"
./bin/sonionmap add-node <mapId> --name "Ahmia" --url http://xxxx.onion --category search
./bin/sonionmap add-document <mapId> --title "Field notes" --file notes.md
./bin/sonionmap add-image <mapId> screenshot.png
./bin/sonionmap layout <mapId> --strategy auto --spacing 70
./bin/sonionmap export <mapId> --out map.json
```

Or use the REST API:

```bash
npm run server
curl http://127.0.0.1:4321/api/maps
```

Full headless documentation: [`server/README.md`](server/README.md).

## Graph layout

The layout engine lives in `src/utils/forceLayout.ts` and is shared by the UI, API, MCP server, and CLI.

| Strategy | Description |
|---|---|
| `auto` | Recommended: `force` when the map has edges, `categories` otherwise. |
| `force` | Spring simulation with repulsion, link springs, and collision separation. |
| `categories` | Grid grouped by legacy category metadata. |
| `grid` | Simple alphabetical grid. |
| `radial` | Places the most connected node at the center. |

Every strategy removes overlaps and normalizes coordinates. Technical details: [`docs/layout.md`](docs/layout.md).

## Persistence and sync

- **UI:** `localStorage` keys `sonion_maps_v2` and `sonion_active_map_id_v2`.
- A fresh install starts empty; no sample or starter maps are bundled.
- **Agents:** `~/.sonionmap/db.json`, configurable with `SONIONMAP_DB`.
- Sync is **server → UI only (pull)** and runs only when you press **↻**. Changes made in the UI are not sent to the server.
- To move UI data to the server, export from the UI and import with `./bin/sonionmap import file.json`.

### Resetting local data

Both layers start empty on a fresh install and are never re-seeded. To discard your local maps:

- **Agents:** delete `~/.sonionmap/db.json` (or the file set in `SONIONMAP_DB`). It is recreated empty on the next command.
- **UI:** remove the `sonion_maps_v2`, `sonion_active_map_id_v2`, and `sonionmap:whiteboard-recent-colors` keys from the site's `localStorage`, then reload.

## Verification

```bash
npm run check                                  # web build + server typecheck
./node_modules/.bin/tsx server/smoke-test.ts   # MCP smoke test
```

The smoke test covers MCP tool registration, CRUD, documents and images, search, batch rollback, `organize_map`, layout inside `batch_apply`, and safe deletion.

## Project structure

```text
src/              React UI
server/core/      schemas, JSON store, and domain operations
server/api.ts     REST API + SSE
server/mcp.ts     MCP server
server/cli.ts     CLI
bin/              CLI and MCP executables
docs/             technical documentation
scripts/          development scripts
```

## Documentation

- [`server/README.md`](server/README.md) — MCP, REST API, and CLI reference.
- [`docs/layout.md`](docs/layout.md) — automatic graph layout engine.

## Security

- The API listens on `127.0.0.1` by default.
- If you expose it beyond localhost, set `SONIONMAP_TOKEN`.
- Do not invent `.onion` addresses: only store links that are provided or verified.
- Destructive operations require explicit confirmation.

## Development

- Validate server input with Zod.
- Run `npm run check` after every change.
- Do not commit automatically; create commits only when explicitly requested.