{"id":"add-atomic-chat-tool","name":"add-atomic-chat-tool","summary":"Atomic Chat MCPサーバーを追加し、コンテナエージェントがOpenAI互換APIを通じてAtomic Chatデスクトップアプリが提供するローカルモデルを呼び出せるようにします。","body":"# Add Atomic Chat Integration\n\nThis skill adds a stdio-based MCP server that exposes models running in the local [Atomic Chat](https://github.com/AtomicBot-ai/Atomic-Chat) desktop app as tools for the container agent. Claude remains the orchestrator but can offload work to local models served by Atomic Chat on `http://127.0.0.1:1337/v1` (OpenAI-compatible).\n\nTools exposed:\n- `atomic_chat_list_models` — list models currently available in Atomic Chat (`GET /v1/models`)\n- `atomic_chat_generate` — send a prompt to a specified model and return the response (`POST /v1/chat/completions`)\n\nModel management (download, delete) is done through the **Atomic Chat desktop UI** — the app is a fork of Jan and manages its own model library.\n\nThe skill ships the MCP server source (and its test) in this folder and copies them into the agent-runner tree at install time, then registers the server in `index.ts` and forwards host env vars in `container-runner.ts`. Registering the server is enough to expose its tools — the agent's allow-pattern (`mcp__atomic_chat__*`) is derived from the registered server name.\n\n## Phase 1: Pre-flight\n\n### Check if already applied\n\nCheck if `container/agent-runner/src/atomic-chat-mcp-stdio.ts` exists. If it does, skip to Phase 3 (Configure).\n\n### Check prerequisites\n\nVerify Atomic Chat is installed and its local API server is running. On the host:\n\n```bash\ncurl -s http://127.0.0.1:1337/v1/models | head\n```\n\nIf the request fails:\n\n1. Install Atomic Chat from the [latest release](https://github.com/AtomicBot-ai/Atomic-Chat/releases) (macOS only for now — `atomic-chat.dmg`).\n2. Open the app.\n3. Open **Settings → Local API Server** and make sure it's enabled on port `1337`.\n4. Go to the **Hub** (or **Models**) tab and download at least one model (e.g. Llama 3.2 3B, Qwen 2.5 Coder 7B).\n5. Load the model once by sending any message in Atomic Chat's UI to warm it up.\n\n## Phase 2: Apply Code Changes\n\n### Copy the skill's source and tests into both trees\n\nThis skill reaches into both the container (Bun) tree and the host (Node) tree, so its\nfiles go into both, alongside the integration points they cover.\n\n```bash\nS=.claude/skills/add-atomic-chat-tool\n# Container (Bun) tree — the MCP server and the registration wiring test\ncp $S/atomic-chat-mcp-stdio.ts        container/agent-runner/src/atomic-chat-mcp-stdio.ts\ncp $S/atomic-chat-registration.test.ts container/agent-runner/src/atomic-chat-registration.test.ts\n# Host (Node) tree — the env-forwarding helper and the wiring test\ncp $S/atomic-chat-env.ts              src/atomic-chat-env.ts\ncp $S/atomic-chat-wiring.test.ts      src/atomic-chat-wiring.test.ts\n```\n\n### Register the MCP server in the agent-runner\n\nEdit `container/agent-runner/src/index.ts`. Find the `mcpServers` object that currently looks like this:\n\n```ts\n  const mcpServers: Record<string, { command: string; args: string[]; env: Record<string, string> }> = {\n    nanoclaw: {\n      command: 'bun',\n      args: ['run', mcpServerPath],\n      env: {},\n    },\n  };\n```\n\nAdd an `atomic_chat` entry alongside `nanoclaw`:\n\n```ts\n  const mcpServers: Record<string, { command: string; args: string[]; env: Record<string, string> }> = {\n    nanoclaw: {\n      command: 'bun',\n      args: ['run', mcpServerPath],\n      env: {},\n    },\n    atomic_chat: {\n      command: 'bun',\n      args: ['run', path.join(__dirname, 'atomic-chat-mcp-stdio.ts')],\n      env: {\n        ...(process.env.ATOMIC_CHAT_HOST ? { ATOMIC_CHAT_HOST: process.env.ATOMIC_CHAT_HOST } : {}),\n        ...(process.env.ATOMIC_CHAT_API_KEY ? { ATOMIC_CHAT_API_KEY: process.env.ATOMIC_CHAT_API_KEY } : {}),\n      },\n    },\n  };\n```\n\n`atomic-chat-registration.test.ts` asserts this entry is present and points at the server module — the tool only appears to the agent if it is registered here.\n\n### Forward host env vars into the container\n\nThe env-forwarding logic lives in the copied `src/atomic-chat-env.ts` (`atomicChatEnv()`), so the reach-in into `composeSessionSpec` is a single spread.\n\nImport it in `src/container-runner.ts` (alongside the other local imports):\n\n```ts\nimport { atomicChatEnv } from './atomic-chat-env.js';\n```\n\nThen, in `composeSessionSpec`, find the `contributedEnv` literal and spread the helper at the end. The contributed lane — not the composed `env` literal — because `ATOMIC_CHAT_API_KEY` is credential-NAMED and the composed lane's key-name check would refuse the spawn; the contributed lane exempts the name and still refuses credential-shaped values:\n\n```ts\n  const contributedEnv: Record<string, string> = {\n    ...(contribution.env ?? {}),\n    ...(gateway.env ?? {}),\n    ...atomicChatEnv(),\n  };\n```\n\n`atomic-chat-wiring.test.ts` asserts this `...atomicChatEnv()` spread exists inside `composeSessionSpec`.\n\n### Surface `[ATOMIC]` log lines at info level\n\n> **Shared block.** This rewrites the driver's container-stderr logger, which other local-model tools (e.g. `add-ollama-tool` for `[OLLAMA]`) also edit to surface their own prefix. Touch only the `[ATOMIC]` branch and leave the rest of the block intact, so the edits coexist and removal restores it cleanly.\n\nContainer stderr now lands in the Docker driver: in `src/drivers/docker-driver.ts`, inside `DockerHandle.start()`, find the stderr handler:\n\n```ts\n    proc.onStderr((line) => {\n      log.debug(line, { container: this.name });\n      this.#stderrTail.push(line);\n      if (this.#stderrTail.length > 10) this.#stderrTail.shift();\n    });\n```\n\nReplace the `log.debug` line with a prefix branch (leave the stderr-tail lines intact — they feed the non-zero-exit warning):\n\n```ts\n    proc.onStderr((line) => {\n      if (line.includes('[ATOMIC]')) {\n        log.info(line, { container: this.name });\n      } else {\n        log.debug(line, { container: this.name });\n      }\n      this.#stderrTail.push(line);\n      if (this.#stderrTail.length > 10) this.#stderrTail.shift();\n    });\n```\n\n### Add env-var stubs to `.env.example`\n\nAppend to `.env.example`:\n\n```bash\n# Atomic Chat MCP tool (.claude/skills/add-atomic-chat-tool)\n# Override the host where Atomic Chat exposes its OpenAI-compatible API.\n# Default: http://host.docker.internal:1337 (with fallback to localhost)\n# ATOMIC_CHAT_HOST=http://host.docker.internal:1337\n\n# Optional API key. Leave unset for a local Atomic Chat install — it does not require auth.\n# ATOMIC_CHAT_API_KEY=\n```\n\n### Validate code changes\n\n```bash\npnpm run build\npnpm exec tsc -p container/agent-runner/tsconfig.json --noEmit\n# Host tree: composeSessionSpec wiring\npnpm exec vitest run src/atomic-chat-wiring.test.ts\n# Container tree: index.ts registration\n(cd container/agent-runner && bun test src/atomic-chat-registration.test.ts)\n./container/build.sh\n```\n\nAll must be clean before proceeding. The wiring and registration tests confirm the two\nintegration points — the `composeSessionSpec` spread and the `index.ts` registration — are\nactually in place; a failure means one drifted. (The MCP server's own request/response\nbehavior against Atomic Chat is the author's build-time concern, not part of these tests —\nverify it manually in Phase 4.)\n\n## Phase 3: Configure\n\n### Set Atomic Chat host (optional)\n\nBy default, the MCP server connects to `http://host.docker.internal:1337` (Docker Desktop) with a fallback to `localhost`. To use a custom host, add to `.env`:\n\n```bash\nATOMIC_CHAT_HOST=http://your-atomic-chat-host:1337\n```\n\n### Set API key (optional)\n\nAtomic Chat does **not require authentication** when running locally — leave this unset. Only set it if you've put Atomic Chat behind a reverse proxy that enforces auth:\n\n```bash\nATOMIC_CHAT_API_KEY=sk-...\n```\n\n### Restart the service\n\nRun from your NanoClaw project root:\n\n```bash\nsource setup/lib/install-slug.sh\nlaunchctl kickstart -k gui/$(id -u)/$(launchd_label)  # macOS\n# Linux: systemctl --user restart $(systemd_unit)\n```\n\n## Phase 4: Verify\n\n### Test inference\n\nTell the user:\n\n> Send a message like: \"use atomic chat to tell me the capital of France\"\n>\n> The agent should use `atomic_chat_list_models` to find available models, then `atomic_chat_generate` to get a response.\n\n### Check logs if needed\n\n```bash\ntail -f logs/nanoclaw.log | grep -i atomic\n```\n\nLook for:\n- `[ATOMIC] Listing models...` — list request started\n- `[ATOMIC] Found N models` — models discovered\n- `[ATOMIC] >>> Generating with <model>` — generation started\n- `[ATOMIC] <<< Done: <model> | Xs | N tokens | M chars` — generation completed\n\n## Troubleshooting\n\n### Agent says \"Atomic Chat is not installed\" or tries to run a CLI\n\nThe agent is looking for a CLI that doesn't exist instead of using the MCP tools. This means:\n1. The MCP server wasn't copied — check `container/agent-runner/src/atomic-chat-mcp-stdio.ts` exists\n2. The MCP server wasn't registered — check `container/agent-runner/src/index.ts` has the `atomic_chat` entry in `mcpServers` (the allow-pattern is derived from this, so registration is the only thing to check)\n3. The container wasn't rebuilt — run `./container/build.sh`\n\n### \"Failed to connect to Atomic Chat\"\n\n1. Verify the host API is reachable: `curl http://127.0.0.1:1337/v1/models`\n2. Confirm the Local API Server is enabled in Atomic Chat's settings\n3. Check Docker can reach the host: `docker run --rm curlimages/curl curl -s http://host.docker.internal:1337/v1/models`\n4. If using a custom host, check `ATOMIC_CHAT_HOST` in `.env`\n\n### `model not found` / 404 on generate\n\nThe model ID passed to `atomic_chat_generate` must exactly match one of the IDs returned by `atomic_chat_list_models`. Ask the agent to list models first, then pick one from that list.\n\n### Slow first response\n\nAtomic Chat lazy-loads models into memory on first use. The initial call may take longer while the model warms up. Subsequent calls against the same model are fast.\n\n### Agent doesn't use Atomic Chat tools\n\nThe agent may not know about the tools. Try being explicit: \"use the atomic_chat_generate tool with llama3.2-3b-instruct to answer: ...\"\n\n### Context window or output size issues\n\nAtomic Chat respects each model's native context length. If you hit limits, pass `max_tokens` explicitly when calling `atomic_chat_generate`, or switch to a model with a larger context window in the Atomic Chat UI.","author":"@nanocoai","ownerProfile":null,"authorContacts":null,"sourceUrl":"https://github.com/nanocoai/nanoclaw/tree/main/.claude/skills/add-atomic-chat-tool","license":"MIT","category":"testing","lang":"en","tokens":2553,"stars":0,"calls30d":1,"claimed":false,"visibility":"public","origin":"crawler","version":"0.1.0","createdAt":"2026-08-22","updatedAt":"2026-08-22","files":[{"path":"atomic-chat-env.ts","size":1037,"sha256":"0a9ec003e072fc7bfea851ef24f6100d4d5b89eb57431ac929c8f9b6a623e16c"},{"path":"atomic-chat-mcp-stdio.ts","size":7011,"sha256":"a4870329df90e5c0a74c369179d9d6cdb28a457dd527ebce5ab79abea6247201"},{"path":"atomic-chat-registration.test.ts","size":2328,"sha256":"230470a4a9f2c65eac66d2d34d0dc8412c75f19bc9fc6c4c7ea9cec329e2579b"},{"path":"atomic-chat-wiring.test.ts","size":2173,"sha256":"37f77031723c10f5bb7fc294e2f2b26441dc5c65d0c6d8a46153887e9c2f714e"},{"path":"REMOVE.md","size":1597,"sha256":"568ba5bfc69bcb7a879773bc1752717591764da0fbc929264af4251e0e81430c"}],"requires":{"mcp":["atomic_chat"],"tools":[]},"safety":{"flags":[{"code":"net.endpoints","kind":"exfiltration","excerpt":"host.docker.internal","message":"bundled scripts reach 1 external host(s)","severity":"warn"}],"scannedAt":"2026-08-22","hasScripts":true,"networkEndpoints":["host.docker.internal"]}}