{"slug": "from-a-headless-linux-server-to-a-browser-room-a-terminal-cli-and-mcp-server-my", "title": "From a Headless Linux Server to a Browser Room: A Terminal CLI and MCP Server Bundled from My Web App's Code", "summary": "A developer built clipshare, a terminal CLI and MCP server that lets a headless Linux server join the same browser-based Clipboard Share rooms used for file transfer. The CLI imports crypto and folder-diff modules directly from the web app's frontend/src to prevent protocol drift, with esbuild bundling the entry point into a single clipshare.mjs and a two-line vue-shim re-exporting ref from @vue/reactivity to avoid bundling all of Vue. Room keys travel only through SPAKE2, and a systemd user unit keeps folder sync running after the terminal closes.", "body_md": "The goal was narrow: send and receive files from a headless Linux server, the kind you only reach over SSH.\n\nClipboard Share already has rooms on the web. A phone and a PC join the same room and swap files. So the question was simple: can a terminal join that same room? That became `clipshare`. A file uploaded from the server shows up in the browser room right away.\n\nI built it with Claude Code. I made the design calls and checked the results. The work started with PR #164 (feat/cli), with stages 1 to 3 landing on 2026-11-30 and npm plus MCP on 2026-12-01.\n\nThis was the part I cared about most. If I write separate crypto for the CLI, it will drift from the web version, and then the two sides can't open each other's files.\n\nSo the CLI imports modules straight from `frontend/src`. This is the top of `share.js`:\n\n``` js\nimport { diffFolder } from '../../frontend/src/utils/folderDiff.js'\nimport { isCopyOfOther } from '../../frontend/src/utils/folderDedupe.js'\nimport { makeFolderEp, makeFolderEr } from '../../frontend/src/utils/privateFolder.js'\nimport { ctPartCount } from '../../frontend/src/utils/lockCrypto.js'\nimport { scanDir, FOLDER_MAX_FILES } from './files.js'\nimport { uploadLockedFile, contentHash } from './transfer.js'\n```\n\nThe catch is browser globals. Anything touching `document` or `window` won't run in Node. So I split out the parts that don't need a browser: `lockedUploadCore.js`, `lockedStream.js`, `identityName.js`, `receivePath.js` and `folderDedupe.js`. The web app and the CLI use the same files.\n\nesbuild turns the entry point into one file, `clipshare.mjs`:\n\n```\nawait build({\n  entryPoints: [path.join(cli, 'src', 'cli.js')],\n  outfile: path.join(outDir, 'clipshare.mjs'),\n  bundle: true,\n  platform: 'node',\n  target: 'node18',\n  format: 'esm',\n  // codeJoin.js only uses vue's ref: alias vue to the reactivity module\n  alias: { vue: path.join(cli, 'src', 'vue-shim.js') },\n  nodePaths: [path.join(frontend, 'node_modules')],\n})\n```\n\nThe web `codeJoin.js` uses `ref` from Vue. I didn't want all of Vue in a CLI bundle, so `vue-shim.js` re-exports `ref` from `@vue/reactivity`. The file is two lines.\n\nNode 18 has no `globalThis.crypto`, so the entry file fills it from `node:crypto`'s `webcrypto` before anything else loads. The web code calls Web Crypto directly, so this keeps it unchanged.\n\nOn the web, you join a room by entering four pictures. A terminal has no pictures, so the same code is written as numbers, 00 to 23 per picture:\n\n```\ncurl -fsSL clipboardapp.org/c | sh -s 1704-2311\n```\n\nOne line installs the CLI and joins. You still have to press \"admit\" on the web side. The code is single-use and lasts 5 minutes.\n\n`codejoin.js` is thin. It reuses the web state machine through a small adapter:\n\n``` js\nimport { createCodeJoiner } from '../../frontend/src/services/codeJoin.js'\n\nexport function codeAdapter(session) {\n  return {\n    emit: (event, payload) => session.emitAck(event, payload),\n    on: (event, fn) => session.socket.on(event, fn),\n    off: (event, fn) => session.socket.off(event, fn)\n  }\n}\n```\n\nThe room key travels only through SPAKE2, so the server never sees it.\n\nI kept the folder rules the same as the web:\n\n`share ~/docs` uploads a folder and checks for changes every 2 seconds. Dotfiles are skipped, with a cap of 1000 files and depth 8.`receive ~/inbox` saves room files into the same folder structure. Name collisions become `name (1).ext`. Downloads go to a `.xxxx.clipshare.part` temp file and get renamed when done.`sync ~/docs` runs both in one process.\nClosing the terminal kills a folder share, so `clipshare service install` writes a user unit:\n\n```\nclipshare service install --room myroom --sync ~/docs\nclipshare service list | status | logs | uninstall\n```\n\nThe unit goes to `~/.config/systemd/user/clipshare-<name>.service`. At first I followed `XDG_CONFIG_HOME`, but if you only change it in your shell, systemctl can't find the unit. I pinned the path to `~/.config` (commit f6d9915).\n\nTo keep it running after logout you need `loginctl enable-linger $USER`. The CLI doesn't run that for you. Changing system settings behind your back felt wrong.\n\nWhen I tried to publish, npm rejected `clipshare` because it's too similar to the existing `clip-share`. The package is now `clipboardapp`, and the command is still `clipshare` (commit 0a16202):\n\n```\nnpx clipboardapp join-code 1234-5678\n```\n\nI also had to fix the MCP registry name. `mcpName` has to match the GitHub account's capitalization: `io.github.Kangchanghwan/clipshare` (PR #170).\n\n`clipshare mcp` is a stdio MCP server with JSON-RPC 2.0 written by hand, no SDK. It has five tools: `send_file`, `send_text`, `list_files`, `get_file` and `list_rooms`.\n\nThe tools don't have their own logic. They call the existing commands with `--json`:\n\n``` js\nexport function toArgv(name, a = {}) {\n  const room = str(a.room) ? ['--room', a.room] : []\n  if (name === 'send_file') { if (!str(a.path)) throw new Error('path is required'); return ['send', '--json', ...room, '--', a.path] }\n  if (name === 'send_text') { if (!str(a.text)) throw new Error('text is required'); return ['text', '--json', ...room, '--', a.text] }\n  if (name === 'list_files') return ['ls', '--json', ...room]\n  if (name === 'list_rooms') return ['rooms', '--json']\n  return null\n}\n```\n\nReusing the commands the CLI tests already cover means fewer new places to break.\n\nWith stdio MCP, stdout is for JSON-RPC only. One stray log line and the client can't parse the stream. So every log goes to stderr:\n\n```\nexport async function runMcp({ run, env = process.env, input = process.stdin, output = process.stdout, err = (s) => console.error(s) }) {\n  err(`clipshare mcp ${VERSION}: stdio server ready`)\n  await createMcpServer({ run, env, log: err }).serve(input, output)\n  return 0\n}\n```\n\nIf no room is saved, a tool call returns an error telling you to run `clipshare join-code` first.\n\nRegistering it in Claude Code is one line:\n\n```\nclaude mcp add clipshare -- npx -y clipboardapp mcp\n```\n\nNow I can tell Claude Code to put a server log into a room.\n\nOnly things I can confirm from the code and README:\n\n`--net` (same-network rooms) supports send, list and receive only.`cli/server.json` is a draft for the official MCP registry. I haven't submitted it.`~/.config/clipshare/rooms.json` (directory 700, file 600). If that machine is compromised, so are those rooms.\nThe CLI is part of Clipboard Share. I'd like feedback from anyone who tries it, including the \"feedback\" link at the bottom of the site.", "url": "https://wpnews.pro/news/from-a-headless-linux-server-to-a-browser-room-a-terminal-cli-and-mcp-server-my", "canonical_source": "https://dev.to/kangchanghwan/from-a-headless-linux-server-to-a-browser-room-a-terminal-cli-and-mcp-server-bundled-from-my-web-39ep", "published_at": "2026-12-02 23:05:43+00:00", "updated_at": "2026-12-02 23:17:27.163596+00:00", "lang": "en", "topics": ["ai-tools", "developer-tools", "ai-agents", "agent-protocols"], "entities": ["Clipboard Share", "clipshare", "Claude Code", "esbuild", "Vue", "@vue/reactivity", "SPAKE2", "npm"], "also_reported_by": [], "takeaway": "Sharing crypto and folder-diff modules between a web app and a Node CLI via esbuild bundling, a two-line vue-shim aliasing Vue to @vue/reactivity, and a globalThis.crypto polyfill from node:crypto keeps both sides protocol-compatible without bundling the full framework.", "recommendation": {"advice": "Import shared crypto and state-machine modules from the web app into the CLI rather than reimplementing them, and shim browser-only dependencies like Vue's ref so the bundle stays small and the two sides stay compatible.", "playbook": "agent-architecture", "playbook_url": "https://wpnews.pro/playbooks/agent-architecture"}, "alternates": {"html": "https://wpnews.pro/news/from-a-headless-linux-server-to-a-browser-room-a-terminal-cli-and-mcp-server-my", "markdown": "https://wpnews.pro/news/from-a-headless-linux-server-to-a-browser-room-a-terminal-cli-and-mcp-server-my.md", "text": "https://wpnews.pro/news/from-a-headless-linux-server-to-a-browser-room-a-terminal-cli-and-mcp-server-my.txt", "jsonld": "https://wpnews.pro/news/from-a-headless-linux-server-to-a-browser-room-a-terminal-cli-and-mcp-server-my.jsonld"}}