Basic logic to update Claude.app

This commit is contained in:
Justin Spahr-Summers 2024-11-18 14:37:37 +00:00
commit 4622992557
No known key found for this signature in database

View file

@ -7,9 +7,57 @@ import fs from "fs/promises";
import chalk from "chalk";
import ora from "ora";
import ejs from "ejs";
import os from "os";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
async function updateClaudeConfig(directory: string, name: string) {
let configDir;
switch (os.platform()) {
case "darwin":
configDir = path.join(
os.homedir(),
"Library",
"Application Support",
"Claude",
);
break;
case "win32":
configDir = path.join(process.env.APPDATA || "", "Claude");
break;
default:
return;
}
const configFile = path.join(configDir, "claude_desktop_config.json");
try {
const config = JSON.parse(await fs.readFile(configFile, "utf-8"));
if (!config.mcpServers) {
config.mcpServers = {};
}
if (!config.mcpServers[name]) {
config.mcpServers[name] = {
command: "node",
args: ["index.js"],
};
}
await fs.writeFile(configFile, JSON.stringify(config, null, 2));
console.log(
chalk.green(
"✓ Successfully added MCP server to Claude.app configuration",
),
);
} catch {
console.log(
chalk.yellow("Note: Could not update Claude.app configuration"),
);
}
}
async function createServer(directory: string, options: any = {}) {
const questions = [
{
@ -26,6 +74,13 @@ async function createServer(directory: string, options: any = {}) {
default: "A Model Context Protocol server",
when: !options.description,
},
{
type: "confirm",
name: "installForClaude",
message: "Would you like to install this server for Claude.app?",
default: true,
when: os.platform() === "darwin" || os.platform() === "win32",
},
];
const answers = await inquirer.prompt(questions);
@ -80,6 +135,10 @@ async function createServer(directory: string, options: any = {}) {
spinner.succeed(chalk.green("MCP server created successfully!"));
if (answers.installForClaude) {
await updateClaudeConfig(directory, config.name);
}
// Print next steps
console.log("\nNext steps:");
console.log(chalk.cyan(` cd ${directory}`));