opencode-lens

opencode-lens

MCP server that enables inspecting, controlling, and safely automating live opencode TUI sessions.

Category
访问服务器

README

opencode-lens

opencode-lens is a local automation bridge for live opencode TUI sessions. It combines an opencode TUI plugin, a Unix-socket HTTP API, an MCP server, a Hermes skill, and optional watchdog scripts so local agents can inspect and safely control running opencode instances without screen scraping.

Recommended GitHub description:

opencode TUI bridge and MCP toolkit for inspecting, controlling, and safely automating live opencode sessions

What It Does

  • Discover active opencode TUI instances.
  • Read the visible TUI session and status.
  • Send prompts to a specific session in the background or switch the visible TUI session.
  • Run a single prompt on a specific model with per-message providerID + modelID.
  • Read recent messages, diffs, and todo state.
  • Bridge opencode interactive questions to Hermes users.
  • Approve or deny opencode permission prompts with explicit user intent.
  • Close accidentally opened TUI overlays such as the model selector.
  • Monitor instances for pending permissions, questions, and completion events.

Repository Layout

opencode-lens/
├── packages/
│   ├── plugin/          # opencode TUI plugin; exposes the lens Unix socket API
│   ├── mcp/             # stdio MCP server for Hermes and other MCP clients
│   └── shared/          # shared registry/types/discovery helpers
├── scripts/             # optional watchdog scripts
├── skill/               # Hermes/OpenClaw skill instructions
├── docs/                # HTTP API notes
├── package.json         # Bun workspace
├── tsconfig.json
├── bun.lock
└── LICENSE

Architecture

Hermes / MCP client
  └─ opencode-lens-mcp (stdio MCP server)
       ├─ discovers active lens registry files
       ├─ talks HTTP over per-user Unix sockets
       └─ exposes tools such as instances_list, tui_status, prompt_send

opencode TUI process
  └─ opencode-lens plugin
       ├─ writes an instance registry file
       ├─ serves HTTP over a Unix Domain Socket
       ├─ reads live TUI/session state through the opencode plugin API
       ├─ forwards selected operations through the opencode SDK
       └─ publishes status events for automation clients

The plugin does not open a TCP port. Registry files are written under:

~/.local/share/opencode-lens/instances/

Sockets prefer:

$XDG_RUNTIME_DIR/opencode-lens/

and fall back to:

~/.local/share/opencode-lens/sockets/

Requirements

  • Bun 1.3+
  • opencode with TUI plugin support
  • Hermes or another MCP client if you want MCP automation

Install From npm

There are two separate install surfaces:

  • opencode-lens is an opencode TUI plugin. opencode can install/cache it automatically from npm when it sees the package name in the TUI config.
  • opencode-lens-mcp is a stdio MCP server. Install or run it from the environment where Hermes starts MCP servers.

opencode TUI plugin

Add the npm package name to ~/.config/opencode/tui.json:

{
  "$schema": "https://opencode.ai/tui.json",
  "plugin": ["opencode-lens"]
}

Restart opencode. opencode will resolve and cache the npm plugin package for the TUI runtime.

You can also install the plugin manually, but this is usually unnecessary for normal opencode plugin usage:

npm install -g opencode-lens

Hermes MCP server

For Hermes, either run the MCP server through npx:

mcp_servers:
  opencode-lens:
    command: npx
    args:
      - -y
      - opencode-lens-mcp
    enabled: true
    timeout: 30
    connect_timeout: 10

or install it globally:

npm install -g opencode-lens-mcp

and configure Hermes with the installed binary:

mcp_servers:
  opencode-lens:
    command: opencode-lens-mcp
    enabled: true
    timeout: 30
    connect_timeout: 10

Restart Hermes after installing or updating the MCP package so the tool list is reloaded.

If you want both npm packages installed globally for local development, use:

npm install -g opencode-lens opencode-lens-mcp

The opencode TUI plugin still needs to be registered in tui.json even when globally installed:

{
  "$schema": "https://opencode.ai/tui.json",
  "plugin": ["opencode-lens"]
}

Verify npm Packages

Check the published package metadata:

npm view opencode-lens version dist.tarball --registry=https://registry.npmjs.org/
npm view opencode-lens-mcp version bin dist.tarball --registry=https://registry.npmjs.org/

Smoke-test in a clean temporary directory:

mkdir /tmp/opencode-lens-npm-verify
cd /tmp/opencode-lens-npm-verify
npm init -y
npm install opencode-lens opencode-lens-mcp --registry=https://registry.npmjs.org/

The plugin bundle depends on Bun runtime features used by opencode, so validate import with Bun rather than Node:

bun --eval 'const plugin = await import("opencode-lens"); console.log(Object.keys(plugin).join(","));'

Validate the MCP server by asking it for its tool list:

printf '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"verify","version":"0.0.0"}}}\n{"jsonrpc":"2.0","method":"notifications/initialized","params":{}}\n{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}\n' | npx opencode-lens-mcp

Development

bun install
bun run typecheck
bun test
bun run build

Build standalone MCP binaries when needed:

bun run build:mcp:binaries

Install The TUI Plugin From Source

Build the plugin:

bun run --cwd packages/plugin build

Install the built plugin into opencode's TUI plugin directory:

mkdir -p ~/.config/opencode/plugins
cp packages/plugin/dist/index.js ~/.config/opencode/plugins/opencode-lens.js

Register it in ~/.config/opencode/tui.json:

{
  "$schema": "https://opencode.ai/tui.json",
  "plugin": ["./plugins/opencode-lens.js"]
}

Restart opencode after changing the plugin bundle. Already-running opencode TUI processes keep the old plugin code loaded.

Configure Hermes MCP

If you installed from npm, use command: opencode-lens-mcp as shown above. For source checkouts, build the MCP server:

bun run --cwd packages/mcp build

Example Hermes MCP configuration:

mcp_servers:
  opencode-lens:
    command: /path/to/bun
    args:
      - /path/to/opencode-lens/packages/mcp/dist/opencode-lens-mcp.js
    enabled: true
    timeout: 30
    connect_timeout: 10

Restart Hermes after changing MCP tools. MCP tools are registered when Hermes starts.

Install The Hermes Skill

Copy the skill into Hermes:

mkdir -p ~/.hermes/skills/opencode-lens
cp skill/SKILL.md ~/.hermes/skills/opencode-lens/SKILL.md
rm -f ~/.hermes/.skills_prompt_snapshot.json

The skill tells Hermes agents how to use the MCP tools safely. In particular, it requires concise status tables, explicit permission choices, per-message model selection, and safe handling of interactive questions.

Optional Watchdog Scripts

Copy the watchdog scripts if you want polling-based notifications:

mkdir -p ~/.hermes/scripts
cp scripts/opencode-watch.py ~/.hermes/scripts/opencode-watch.py
cp scripts/opencode-watch-loop.sh ~/.hermes/scripts/opencode-watch-loop.sh
chmod +x ~/.hermes/scripts/opencode-watch.py ~/.hermes/scripts/opencode-watch-loop.sh

Run one poll:

~/.hermes/scripts/opencode-watch.py

Run a simple polling loop:

~/.hermes/scripts/opencode-watch-loop.sh

Useful environment variables:

OPENCODE_LENS_SOCKET_DIR       defaults to $XDG_RUNTIME_DIR/opencode-lens or /run/user/$UID/opencode-lens
OPENCODE_WATCH_STATE_FILE      defaults to ~/.hermes/opencode-watch-state.json
OPENCODE_WATCH_LIST_FILE       defaults to ~/.hermes/opencode-watch-list.json
OPENCODE_WATCH_LOCK_FILE       defaults to ~/.hermes/opencode-watch.lock
OPENCODE_WATCH_SCRIPT          used by opencode-watch-loop.sh

MCP Tool Highlights

  • instances_list: list active opencode lens instances.
  • tui_status: read the visible TUI session/status for an instance.
  • prompt_send: send a prompt to a target session; supports per-message model parameters.
  • messages_read: read recent messages for a session.
  • tui_session_switch: switch the visible TUI session.
  • question_respond / question_reject: answer or dismiss opencode interactive questions.
  • permission_respond: reply to permission prompts with allow, always, or deny.
  • model_selector_close: close the currently open TUI overlay/dialog when the runtime exposes api.ui.dialog.clear().

Model Selection

opencode 1.17.x chooses the model per message. The reliable workflow is:

  1. Call models_list.
  2. Match the desired provider/model IDs.
  3. Send the prompt with prompt_send and providerID + modelID.

Do not rely on moving the TUI's visible model indicator. The plugin API does not provide a stable way to set that indicator to an arbitrary model by ID.

Permission And Question Safety

Permission replies are high-risk. Always show the user the requested operation and ask for one of three choices:

  • allow once
  • allow always
  • deny

Interactive questions are separate from permissions. They must be answered with question_respond; sending normal prompt text does not select an option.

Troubleshooting

  • No instances: confirm ~/.config/opencode/tui.json loads ./plugins/opencode-lens.js, then restart opencode.
  • MCP tool missing: rebuild packages/mcp, then restart Hermes.
  • Plugin behavior did not change: restart the target opencode TUI process.
  • Prompt returns 409 session_busy: inspect tui_status; the session may be running, waiting for a question, or waiting for permission.
  • Permission prompt does not appear: the target opencode permission rules may be configured as auto-allow instead of ask.
  • Status reads time out: the plugin caps slow opencode SDK calls and returns structured timeout errors instead of Bun's default 10-second server timeout.

License

MIT

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选