Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,29 @@ This detects your OS and writes to the correct Claude Desktop config file:

Restart Claude Desktop after installing.

### Codex (CLI / IDE)

```bash
npx @aibtc/mcp-server@latest --install --codex
```

This writes the AIBTC MCP server to Codex's shared config at `~/.codex/config.toml`.
Restart Codex, then run `codex mcp list` or use `/mcp` in the Codex TUI to verify
that `aibtc` is enabled.

### Testnet Mode

Add `--testnet` to either command:
Add `--testnet` to any install command:

```bash
# Claude Code on testnet
npx @aibtc/mcp-server@latest --install --testnet

# Claude Desktop on testnet
npx @aibtc/mcp-server@latest --install --desktop --testnet

# Codex on testnet
npx @aibtc/mcp-server@latest --install --codex --testnet
```

> **Why npx?** Using `npx @aibtc/mcp-server@latest` ensures you always get the newest version automatically. Global installs (`npm install -g`) won't auto-update.
Expand Down Expand Up @@ -97,6 +110,17 @@ If you prefer to configure manually, add the following to your config file.

> **Note:** Claude Desktop requires the `-y` flag in args so npx doesn't prompt for confirmation.

**Codex** (`~/.codex/config.toml`):

```toml
[mcp_servers.aibtc]
command = "npx"
args = ["-y", "@aibtc/mcp-server@latest"]

[mcp_servers.aibtc.env]
NETWORK = "mainnet"
```

## Giving Claude a Wallet

When you first use @aibtc/mcp-server, Claude doesn't have a wallet. Here's the smooth onboarding flow:
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"keywords": [
"claude",
"claude-code",
"codex",
"mcp",
"model-context-protocol",
"x402",
Expand Down
100 changes: 98 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const require = createRequire(import.meta.url);
const packageJson = require("../package.json");

// =============================================================================
// AUTO-INSTALL FOR CLAUDE CODE AND CLAUDE DESKTOP
// AUTO-INSTALL FOR CLAUDE CODE, CLAUDE DESKTOP, AND CODEX
// =============================================================================

function getClaudeDesktopConfigPath(): string {
Expand All @@ -34,6 +34,61 @@ function getClaudeDesktopConfigPath(): string {
}
}

function getCodexConfigPath(): string {
return path.join(os.homedir(), ".codex", "config.toml");
}

function escapeTomlString(value: string): string {
return value.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
}

function getTomlTableHeader(line: string): string | null {
const match = line.trim().match(/^(\[\[?[^\]]+\]\]?)(?:\s*#.*)?$/);
return match?.[1] ?? null;
Comment on lines +45 to +47

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve array-of-table sections when rewriting Codex TOML

getTomlTableHeader only matches single-bracket headers like [foo], so while removeTomlTable is skipping the old mcp_servers.aibtc block it does not recognize valid TOML array headers such as [[projects]] as a table boundary. In a config where [[...]] appears after [mcp_servers.aibtc], the installer will keep deleting lines until a later single-bracket table (or EOF), which can silently drop unrelated Codex settings.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 30ac179: getTomlTableHeader now treats both [table] and [[array_table]] headers as table boundaries while still matching optional trailing comments. Re-ran npm run build and verified reinstalling over a config with [[projects]] preserves both array-table sections while replacing only the AIBTC tables.

}

function removeTomlTable(content: string, tableName: string): string {
const target = `[${tableName}]`;
const lines = content.split(/\r?\n/);
const kept: string[] = [];
let skipping = false;

for (const line of lines) {
const tableHeader = getTomlTableHeader(line);

if (tableHeader === target) {
skipping = true;
continue;
}

if (skipping && tableHeader) {
skipping = false;
}

if (!skipping) {
kept.push(line);
}
}

return kept.join("\n").trimEnd();
}

function upsertCodexConfig(content: string, network: string): string {
let next = removeTomlTable(content, "mcp_servers.aibtc");
next = removeTomlTable(next, "mcp_servers.aibtc.env");

const block = [
"[mcp_servers.aibtc]",
'command = "npx"',
'args = ["-y", "@aibtc/mcp-server@latest"]',
"",
"[mcp_servers.aibtc.env]",
`NETWORK = "${escapeTomlString(network)}"`,
].join("\n");

return `${next}${next ? "\n\n" : ""}${block}\n`;
}

async function readJsonConfig(filePath: string): Promise<Record<string, unknown>> {
try {
const content = await fs.readFile(filePath, "utf8");
Expand All @@ -51,6 +106,23 @@ async function writeJsonConfig(filePath: string, config: Record<string, unknown>
await fs.writeFile(filePath, JSON.stringify(config, null, 2));
}

async function readTextConfig(filePath: string): Promise<string> {
try {
return await fs.readFile(filePath, "utf8");
} catch (err: unknown) {
if ((err as NodeJS.ErrnoException).code === "ENOENT") {
return "";
}
throw err;
}
}

async function writeTextConfig(filePath: string, content: string): Promise<void> {
const dir = path.dirname(filePath);
await fs.mkdir(dir, { recursive: true });
await fs.writeFile(filePath, content);
}

async function installToClaudeCode(): Promise<void> {
const claudeConfigPath = path.join(os.homedir(), ".claude.json");
const network = process.argv.includes("--testnet") ? "testnet" : "mainnet";
Expand Down Expand Up @@ -121,6 +193,29 @@ async function installToClaudeDesktop(): Promise<void> {
}
}

async function installToCodex(): Promise<void> {
const configPath = getCodexConfigPath();
const network = process.argv.includes("--testnet") ? "testnet" : "mainnet";

console.log("🔧 Installing @aibtc/mcp-server to Codex...\n");

const config = await readTextConfig(configPath);
await writeTextConfig(configPath, upsertCodexConfig(config, network));

console.log("✅ Successfully installed!\n");
console.log(` Config: ${configPath}`);
console.log(` Network: ${network}`);
console.log("\n📋 Next steps:");
console.log(" 1. Restart Codex or reload MCP configuration");
console.log(" 2. Run `codex mcp list` or use `/mcp` in the Codex TUI to verify `aibtc` is enabled");
console.log(" 3. Ask Codex: \"What's your wallet address?\"");
console.log(" 4. Codex will guide you through wallet setup\n");

if (network === "testnet") {
console.log("💡 Tip: Get testnet STX at https://explorer.hiro.so/sandbox/faucet?chain=testnet\n");
}
}

// =============================================================================
// YIELD HUNTER DAEMON
// =============================================================================
Expand Down Expand Up @@ -151,7 +246,8 @@ if (process.argv[2] === "yield-hunter") {
// Check for --install flag
else if (process.argv.includes("--install") || process.argv.includes("install")) {
const isDesktop = process.argv.includes("--desktop");
const installFn = isDesktop ? installToClaudeDesktop : installToClaudeCode;
const isCodex = process.argv.includes("--codex");
const installFn = isCodex ? installToCodex : isDesktop ? installToClaudeDesktop : installToClaudeCode;
installFn()
.then(() => process.exit(0))
.catch((error) => {
Expand Down
Loading