next-plaid-cli 0.2.2

Semantic code search powered by ColBERT
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# mgrep Installation Mechanism Analysis

This report analyzes how mgrep implements automatic installation for various AI coding tools (Claude Code, OpenCode, Codex, Factory Droid) to help you implement similar functionality for `next-plaid-cli`.

## Overview

mgrep uses four different installation strategies depending on the target tool's extension mechanism:

| Tool | Strategy | Config Location |
|------|----------|-----------------|
| Claude Code | Plugin Marketplace CLI | Uses `claude plugin` CLI commands |
| OpenCode | Tool file + JSON config | `~/.config/opencode/` |
| Codex | MCP CLI + AGENTS.md | `~/.codex/AGENTS.md` |
| Factory Droid | Hooks + Skills + Settings | `~/.factory/` |

---

## 1. Claude Code Installation

**File:** `src/install/claude-code.ts`

### Mechanism

Claude Code uses a **plugin marketplace system**. The installation leverages the `claude` CLI commands:

```typescript
// Add plugin to marketplace
await execAsync("claude plugin marketplace add mixedbread-ai/mgrep", { shell, env: process.env });

// Install the plugin
await execAsync("claude plugin install mgrep", { shell, env: process.env });
```

### Uninstall

```typescript
await execAsync("claude plugin uninstall mgrep", { shell, env: process.env });
await execAsync("claude plugin marketplace remove mixedbread-ai/mgrep", { shell, env: process.env });
```

### Required Assets

mgrep ships with a `.claude-plugin/` directory containing:

**`marketplace.json`** - Plugin registry metadata:
```json
{
  "$schema": "https://anthropic.com/claude-code/marketplace.schema.json",
  "name": "Mixedbread-Grep",
  "owner": { "name": "Mixedbread", "email": "support@mixedbread.ai" },
  "plugins": [{
    "name": "mgrep",
    "source": "./plugins/mgrep",
    "description": "Search your local files using Mixedbread",
    "version": "0.0.0",
    "author": { "name": "Joel Dierkes" },
    "skills": ["./skills/mgrep"]
  }]
}
```

**`plugins/mgrep/.claude-plugin/plugin.json`** - Plugin definition:
```json
{
  "name": "mgrep",
  "description": "Search your local files using Mixedbread",
  "version": "0.0.0",
  "author": { "name": "Joel Dierkes" },
  "hooks": "./hooks/hook.json"
}
```

**`plugins/mgrep/hooks/hook.json`** - Session lifecycle hooks:
```json
{
  "hooks": {
    "SessionStart": [{
      "matcher": "startup|resume",
      "hooks": [{
        "type": "command",
        "command": "python3 ${CLAUDE_PLUGIN_ROOT}/hooks/mgrep_watch.py",
        "timeout": 10
      }]
    }],
    "SessionEnd": [{
      "hooks": [{
        "type": "command",
        "command": "python3 ${CLAUDE_PLUGIN_ROOT}/hooks/mgrep_watch_kill.py",
        "timeout": 10
      }]
    }]
  }
}
```

### Key Requirements for Claude Code

- Requires Claude Code version 2.0.36+
- Uses `${CLAUDE_PLUGIN_ROOT}` environment variable for paths
- Plugin assets must be in `dist/plugins/` after build
- Build script copies plugins: `"postbuild": "cp -r plugins dist/"`

---

## 2. OpenCode Installation

**File:** `src/install/opencode.ts`

### Mechanism

OpenCode uses a **dual approach**:
1. Write a tool definition file to `~/.config/opencode/tool/mgrep.ts`
2. Add MCP server config to `~/.config/opencode/opencode.json` (or `.jsonc`)

### Tool File

Creates `~/.config/opencode/tool/mgrep.ts` with embedded SKILL documentation:

```typescript
import { tool } from "@opencode-ai/plugin"

const SKILL = `
---
name: mgrep
description: A semantic grep-like search tool...
---
// Usage instructions...
`;

export default tool({
  description: SKILL,
  args: {
    q: tool.schema.string().describe("The semantic search query."),
    m: tool.schema.number().default(10).describe("Number of chunks to return."),
    a: tool.schema.boolean().default(false).describe("Generate answer."),
  },
  async execute(args) {
    const result = await Bun.$`mgrep search -m ${args.m} ${args.a ? '-a ' : ''}${args.q}`.text()
    return result.trim()
  },
})
```

### Config File Modification

Adds MCP entry to `opencode.json`:

```typescript
const configJson = parseConfigFile(configPath, configContent);
configJson.mcp = configJson.mcp || {};
configJson.mcp.mgrep = {
  type: "local",
  command: ["mgrep", "mcp"],
  enabled: true,
};
fs.writeFileSync(configPath, stringify(configJson, null, 2));
```

### Uses `comment-json` for Safe Parsing

Preserves comments in JSONC files:
```typescript
import { parse, stringify } from "comment-json";
```

---

## 3. Codex Installation

**File:** `src/install/codex.ts`

### Mechanism

Codex uses a **CLI command + AGENTS.md file**:
1. Register MCP server via `codex mcp add` command
2. Append skill documentation to `~/.codex/AGENTS.md`

### MCP Registration

```typescript
await execAsync("codex mcp add mgrep mgrep mcp", { shell, env: process.env });
```

### AGENTS.md Skill Injection

Appends a SKILL markdown block to `~/.codex/AGENTS.md`:

```typescript
const SKILL = `
---
name: mgrep
description: A semantic grep-like search tool...
---

## When to use this skill
Whenever you need to search your local files...

## How to use this skill
Use \`mgrep\` to search your local files...
`;

const destPath = path.join(os.homedir(), ".codex", "AGENTS.md");
// Check if skill already exists
if (!existingContent.includes(SKILL) && !existingContent.includes(skillTrimmed)) {
  fs.appendFileSync(destPath, SKILL);
}
```

### Uninstall

Removes skill content with repeated string replacement:
```typescript
while (updatedContent !== previousContent) {
  previousContent = updatedContent;
  updatedContent = updatedContent.replace(SKILL, "");
  updatedContent = updatedContent.replace(SKILL.trim(), "");
}
```

---

## 4. Factory Droid Installation

**File:** `src/install/droid.ts`

### Mechanism

Factory Droid is the most complex, using **hooks, skills, and settings**:
1. Copy Python hook scripts to `~/.factory/hooks/mgrep/`
2. Copy SKILL.md to `~/.factory/skills/mgrep/`
3. Modify `~/.factory/settings.json` with hook configuration

### Bundled Assets

mgrep ships plugin assets at `dist/plugins/mgrep/`:
```
plugins/mgrep/
├── .claude-plugin/plugin.json
├── hooks/
│   ├── hook.json
│   ├── mgrep_watch.py
│   └── mgrep_watch_kill.py
└── skills/mgrep/SKILL.md
```

### Hook Installation

Copies Python scripts and registers hooks in settings:

```typescript
const hookConfig: HooksConfig = {
  SessionStart: [{
    matcher: "startup|resume",
    hooks: [{
      type: "command",
      command: `python3 "${watchPy}"`,
      timeout: 10,
    }],
  }],
  SessionEnd: [{
    hooks: [{
      type: "command",
      command: `python3 "${killPy}"`,
      timeout: 10,
    }],
  }],
};
```

### Settings Modification

Updates `~/.factory/settings.json`:

```typescript
const settings = loadSettings(settingsPath);
settings.enableHooks = true;
settings.allowBackgroundProcesses = true;
settings.hooks = mergeHooks(settings.hooks, hookConfig);
saveSettings(settingsPath, settings);
```

### Smart Merge for Hooks

Prevents duplicates when merging:
```typescript
function mergeHooks(existingHooks, newHooks) {
  // Clone existing, check for duplicates by matcher + command
  const duplicate = current.some(item =>
    (item?.matcher ?? null) === matcher &&
    item?.hooks?.[0]?.command === command
  );
  if (!duplicate) current.push(entry);
}
```

---

## Implementation Recommendations for next-plaid-cli

### 1. Project Structure

```
src/
├── index.ts                    # CLI entry point with commander
├── install/
│   ├── claude-code.ts          # Claude Code installer
│   ├── opencode.ts             # OpenCode installer
│   ├── codex.ts                # Codex installer
│   └── droid.ts                # Factory Droid installer
└── lib/
    └── warning.ts              # Post-install warning message

plugins/plaid/
├── .claude-plugin/
│   └── plugin.json
├── hooks/
│   ├── hook.json
│   ├── plaid_watch.py          # Session start hook
│   └── plaid_watch_kill.py     # Session end hook
└── skills/plaid/
    └── SKILL.md                # Usage instructions for agents
```

### 2. Build Configuration

In `package.json`:
```json
{
  "bin": { "plaid": "dist/index.js" },
  "scripts": {
    "postbuild": "chmod +x dist/index.js && cp -r plugins dist/"
  },
  "files": ["dist", "README.md"]
}
```

### 3. Command Registration

Using `commander`:
```typescript
import { installClaudeCode, uninstallClaudeCode } from "./install/claude-code.js";
// ... other imports

program.addCommand(installClaudeCode);
program.addCommand(uninstallClaudeCode);
// ... add other install commands
```

### 4. Key Dependencies

```json
{
  "commander": "^14.0.0",      // CLI framework
  "comment-json": "^4.5.0",    // Preserve JSON comments
  "chalk": "^5.6.0"            // Colored output
}
```

### 5. Essential Patterns

1. **Always check authentication first:**
   ```typescript
   await ensureAuthenticated();
   ```

2. **Use shell detection for cross-platform:**
   ```typescript
   const shell = process.env.SHELL ||
     (process.platform === "win32" ? process.env.COMSPEC || "cmd.exe" : "/bin/sh");
   ```

3. **Print post-install warning:**
   ```typescript
   printInstallWarning("Claude Code", "plaid uninstall-claude-code");
   ```

4. **Idempotent writes:**
   ```typescript
   function writeFileIfChanged(filePath, content) {
     const existing = fs.existsSync(filePath) ? fs.readFileSync(filePath, "utf-8") : undefined;
     if (existing !== content) fs.writeFileSync(filePath, content);
   }
   ```

5. **Graceful error handling with helpful messages:**
   ```typescript
   catch (error) {
     console.error(`Error: ${error}`);
     console.error(`Do you have claude-code version 2.0.36 or higher installed?`);
   }
   ```

---

## SKILL.md Template

Create a compelling SKILL.md that tells agents when and how to use your tool:

```markdown
---
name: plaid
description: Semantic code search tool. ALWAYS use this instead of Grep for code searches.
license: Apache 2.0
---

## When to use this skill

Whenever you need to search code files. Use this instead of Grep.

## How to use this skill

Use `plaid` for semantic code search with natural language queries.

### Do

```bash
plaid "authentication flow" -k 10
plaid "error handling patterns" ./src
```

### Don't

```bash
plaid "function"  # Too vague
```

## Keywords
search, grep, code, semantic search
```

---

## Summary

| Aspect | Claude Code | OpenCode | Codex | Factory Droid |
|--------|-------------|----------|-------|---------------|
| Installation Method | CLI commands | File writes | CLI + file | File writes |
| Config Format | Plugin system | JSONC | MCP + MD | JSON + scripts |
| Hooks | JSON in plugin | N/A | N/A | Python scripts |
| Skills | SKILL.md | Embedded TS | AGENTS.md | SKILL.md |
| Requires Build | Yes (cp plugins) | No | No | Yes (cp plugins) |