Repository navigation
feat(cli): agentmemory connect — automate native-plugin install for 8 agents #402
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import { existsSync, mkdirSync, writeFileSync } from "node:fs"; | ||
| import { homedir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import * as p from "@clack/prompts"; | ||
| import type { ConnectAdapter, ConnectOptions, ConnectResult } from "./types.js"; | ||
| import { | ||
| AGENTMEMORY_MCP_BLOCK, | ||
| backupFile, | ||
| logAlreadyWired, | ||
| logBackup, | ||
| logInstalled, | ||
| readJsonSafe, | ||
| writeJsonAtomic, | ||
| } from "./util.js"; | ||
|
|
||
| const CLAUDE_DIR = join(homedir(), ".claude"); | ||
| const CLAUDE_JSON = join(homedir(), ".claude.json"); | ||
|
|
||
| type ClaudeMcpEntry = typeof AGENTMEMORY_MCP_BLOCK; | ||
| type ClaudeConfig = { | ||
| mcpServers?: Record<string, ClaudeMcpEntry>; | ||
| [key: string]: unknown; | ||
| }; | ||
|
|
||
| function entryMatches(entry: unknown): boolean { | ||
| if (!entry || typeof entry !== "object") return false; | ||
| const e = entry as Record<string, unknown>; | ||
| if (e["command"] !== "npx") return false; | ||
| const args = Array.isArray(e["args"]) ? (e["args"] as string[]) : []; | ||
| return args.includes("@agentmemory/mcp"); | ||
| } | ||
|
|
||
| export const adapter: ConnectAdapter = { | ||
| name: "claude-code", | ||
| displayName: "Claude Code", | ||
| docs: "https://github.com/rohitg00/agentmemory#claude-code-one-block-paste-it", | ||
|
|
||
| detect(): boolean { | ||
| return existsSync(CLAUDE_DIR); | ||
| }, | ||
|
|
||
| async install(opts: ConnectOptions): Promise<ConnectResult> { | ||
| const existing = readJsonSafe<ClaudeConfig>(CLAUDE_JSON); | ||
| const next: ClaudeConfig = existing ? { ...existing } : {}; | ||
| const servers: Record<string, ClaudeMcpEntry> = { | ||
| ...((next.mcpServers as Record<string, ClaudeMcpEntry>) ?? {}), | ||
| }; | ||
|
|
||
| const alreadyHas = entryMatches(servers["agentmemory"]); | ||
| if (alreadyHas && !opts.force) { | ||
| logAlreadyWired("Claude Code", CLAUDE_JSON); | ||
| return { kind: "already-wired", mutatedPath: CLAUDE_JSON }; | ||
| } | ||
|
|
||
| if (opts.dryRun) { | ||
| p.log.info( | ||
| `[dry-run] Would ${alreadyHas ? "overwrite" : "add"} mcpServers.agentmemory in ${CLAUDE_JSON}`, | ||
| ); | ||
| return { kind: "installed", mutatedPath: CLAUDE_JSON }; | ||
| } | ||
|
|
||
| let backupPath: string | undefined; | ||
| if (existsSync(CLAUDE_JSON)) { | ||
| backupPath = backupFile(CLAUDE_JSON, "claude-code"); | ||
| logBackup(backupPath); | ||
| } else { | ||
| mkdirSync(CLAUDE_DIR, { recursive: true }); | ||
| writeFileSync(CLAUDE_JSON, "{}\n", "utf-8"); | ||
| } | ||
|
|
||
| servers["agentmemory"] = AGENTMEMORY_MCP_BLOCK; | ||
| next.mcpServers = servers; | ||
| writeJsonAtomic(CLAUDE_JSON, next); | ||
|
|
||
| const verify = readJsonSafe<ClaudeConfig>(CLAUDE_JSON); | ||
| if (!entryMatches(verify?.mcpServers?.["agentmemory"])) { | ||
| p.log.error( | ||
| `Verification failed: ${CLAUDE_JSON} did not contain mcpServers.agentmemory after write.`, | ||
| ); | ||
| return { kind: "skipped", reason: "verification-failed" }; | ||
| } | ||
|
|
||
| logInstalled("Claude Code", CLAUDE_JSON); | ||
| p.log.info( | ||
| "Restart Claude Code (or run `/mcp` inside a session) to pick up the new server.", | ||
| ); | ||
| return { kind: "installed", mutatedPath: CLAUDE_JSON, backupPath }; | ||
| }, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; | ||
| import { homedir } from "node:os"; | ||
| import { join, dirname } from "node:path"; | ||
| import * as p from "@clack/prompts"; | ||
| import type { ConnectAdapter, ConnectOptions, ConnectResult } from "./types.js"; | ||
| import { | ||
| backupFile, | ||
| logAlreadyWired, | ||
| logBackup, | ||
| logInstalled, | ||
| } from "./util.js"; | ||
|
|
||
| const CODEX_DIR = join(homedir(), ".codex"); | ||
| const CODEX_TOML = join(CODEX_DIR, "config.toml"); | ||
|
|
||
| const TOML_BLOCK = `[mcp_servers.agentmemory] | ||
| command = "npx" | ||
| args = ["-y", "@agentmemory/mcp"] | ||
|
|
||
| [mcp_servers.agentmemory.env] | ||
| AGENTMEMORY_URL = "http://localhost:3111" | ||
| `; | ||
|
|
||
| const SECTION_HEADER = "[mcp_servers.agentmemory]"; | ||
|
|
||
| function isWiredText(toml: string): boolean { | ||
| return toml.includes(SECTION_HEADER); | ||
| } | ||
|
|
||
| function stripExistingBlock(toml: string): string { | ||
| const lines = toml.split(/\r?\n/); | ||
| const out: string[] = []; | ||
| let skipping = false; | ||
| for (const line of lines) { | ||
| const trimmed = line.trim(); | ||
| if ( | ||
| trimmed === SECTION_HEADER || | ||
| trimmed === "[mcp_servers.agentmemory.env]" | ||
| ) { | ||
| skipping = true; | ||
| continue; | ||
| } | ||
| if ( | ||
| skipping && | ||
| trimmed.startsWith("[") && | ||
| trimmed !== "[mcp_servers.agentmemory.env]" | ||
| ) { | ||
| skipping = false; | ||
| } | ||
| if (!skipping) out.push(line); | ||
| } | ||
| return out.join("\n").replace(/\n{3,}$/, "\n\n").trimEnd() + "\n"; | ||
| } | ||
|
|
||
| export const adapter: ConnectAdapter = { | ||
| name: "codex", | ||
| displayName: "Codex CLI", | ||
| docs: "https://github.com/rohitg00/agentmemory#codex-cli-codex-plugin-platform", | ||
|
|
||
| detect(): boolean { | ||
| return existsSync(CODEX_DIR); | ||
| }, | ||
|
|
||
| async install(opts: ConnectOptions): Promise<ConnectResult> { | ||
| const exists = existsSync(CODEX_TOML); | ||
| const current = exists ? readFileSync(CODEX_TOML, "utf-8") : ""; | ||
| const wired = isWiredText(current); | ||
|
|
||
| if (wired && !opts.force) { | ||
| logAlreadyWired("Codex CLI", CODEX_TOML); | ||
| return { kind: "already-wired", mutatedPath: CODEX_TOML }; | ||
| } | ||
|
|
||
| if (opts.dryRun) { | ||
| p.log.info( | ||
| `[dry-run] Would ${wired ? "rewrite" : "append"} [mcp_servers.agentmemory] in ${CODEX_TOML}`, | ||
| ); | ||
| return { kind: "installed", mutatedPath: CODEX_TOML }; | ||
| } | ||
|
|
||
| let backupPath: string | undefined; | ||
| if (exists) { | ||
| backupPath = backupFile(CODEX_TOML, "codex", "toml"); | ||
| logBackup(backupPath); | ||
| } else { | ||
| mkdirSync(dirname(CODEX_TOML), { recursive: true }); | ||
| } | ||
|
|
||
| const cleaned = wired ? stripExistingBlock(current) : current; | ||
| const joiner = cleaned.length === 0 || cleaned.endsWith("\n") ? "" : "\n"; | ||
| const next = `${cleaned}${joiner}${cleaned.length > 0 ? "\n" : ""}${TOML_BLOCK}`; | ||
| writeFileSync(CODEX_TOML, next, "utf-8"); | ||
|
|
||
| const verify = readFileSync(CODEX_TOML, "utf-8"); | ||
| if (!isWiredText(verify)) { | ||
| p.log.error( | ||
| `Verification failed: ${CODEX_TOML} did not contain ${SECTION_HEADER} after write.`, | ||
| ); | ||
| return { kind: "skipped", reason: "verification-failed" }; | ||
| } | ||
|
|
||
| logInstalled("Codex CLI", CODEX_TOML); | ||
| p.log.info( | ||
| "Codex picks up MCP servers on next launch. For the deeper plugin install, run: codex plugin marketplace add rohitg00/agentmemory && codex plugin install agentmemory", | ||
| ); | ||
| return { | ||
| kind: "installed", | ||
| mutatedPath: CODEX_TOML, | ||
| ...(backupPath !== undefined && { backupPath }), | ||
| }; | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { homedir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import { createJsonMcpAdapter } from "./json-mcp-adapter.js"; | ||
|
|
||
| export const adapter = createJsonMcpAdapter({ | ||
| name: "cursor", | ||
| displayName: "Cursor", | ||
| detectDir: join(homedir(), ".cursor"), | ||
| configPath: join(homedir(), ".cursor", "mcp.json"), | ||
| docs: "https://github.com/rohitg00/agentmemory#other-agents", | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { homedir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import { createJsonMcpAdapter } from "./json-mcp-adapter.js"; | ||
|
|
||
| export const adapter = createJsonMcpAdapter({ | ||
| name: "gemini-cli", | ||
| displayName: "Gemini CLI", | ||
| detectDir: join(homedir(), ".gemini"), | ||
| configPath: join(homedir(), ".gemini", "settings.json"), | ||
| docs: "https://github.com/rohitg00/agentmemory#other-agents", | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { existsSync } from "node:fs"; | ||
| import { homedir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import * as p from "@clack/prompts"; | ||
| import type { ConnectAdapter, ConnectOptions, ConnectResult } from "./types.js"; | ||
|
|
||
| const HERMES_DIR = join(homedir(), ".hermes"); | ||
| const HERMES_CONFIG = join(HERMES_DIR, "config.yaml"); | ||
| const DOCS = "https://github.com/rohitg00/agentmemory/tree/main/integrations/hermes"; | ||
|
|
||
| export const adapter: ConnectAdapter = { | ||
| name: "hermes", | ||
| displayName: "Hermes Agent", | ||
| docs: DOCS, | ||
|
|
||
| detect(): boolean { | ||
| return existsSync(HERMES_DIR); | ||
| }, | ||
|
|
||
| async install(_opts: ConnectOptions): Promise<ConnectResult> { | ||
| p.log.warn( | ||
| "Hermes uses YAML config. Automated merge isn't implemented yet — manual install required.", | ||
| ); | ||
| p.note( | ||
| [ | ||
| `Add to ${HERMES_CONFIG}:`, | ||
| "", | ||
| " mcp_servers:", | ||
| " agentmemory:", | ||
| " command: npx", | ||
| ' args: ["-y", "@agentmemory/mcp"]', | ||
| "", | ||
| " memory:", | ||
| " provider: agentmemory", | ||
| "", | ||
| `Full guide: ${DOCS}`, | ||
| ].join("\n"), | ||
| "Hermes manual install", | ||
| ); | ||
| return { | ||
| kind: "stub", | ||
| reason: "yaml-merge-not-implemented", | ||
| }; | ||
| }, | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove unnecessary intermediate file write.
Line 68 writes an empty JSON object
{}toCLAUDE_JSON, but line 73 immediately overwrites it viawriteJsonAtomic. The intermediate write serves no purpose—only the directory creation on line 67 is needed before the atomic write.♻️ Proposed fix
} else { mkdirSync(CLAUDE_DIR, { recursive: true }); - writeFileSync(CLAUDE_JSON, "{}\n", "utf-8"); }📝 Committable suggestion
🤖 Prompt for AI Agents