#!/usr/bin/env bun
import { existsSync } from "node:fs";
import { homedir } from "node:os";
import { join } from "node:path";
const CLAUDE_DESKTOP_CONFIGS: Record<string, string> = {
darwin: join(homedir(), "Library", "Application Support", "Claude", "claude_desktop_config.json"),
win32: join(homedir(), "AppData", "Roaming", "Claude", "claude_desktop_config.json"),
linux: join(homedir(), ".config", "Claude", "claude_desktop_config.json"),
};
const CLAUDE_CODE_CONFIG = join(homedir(), ".claude", "settings.json");
const MCP_CONFIG = {
command: "bunx",
args: ["wiki-mcp"],
};
interface McpConfig {
mcpServers?: Record<string, { command: string; args: string[] }>;
[key: string]: unknown;
}
async function readJsonFile(path: string): Promise<McpConfig> {
try {
const file = Bun.file(path);
if (await file.exists()) {
return await file.json();
}
} catch {
// File doesn't exist or is invalid
}
return {};
}
async function writeJsonFile(path: string, data: McpConfig): Promise<void> {
await Bun.write(path, JSON.stringify(data, null, 2) + "\n");
}
async function installToConfig(configPath: string, name: string): Promise<boolean> {
const config = await readJsonFile(configPath);
if (!config.mcpServers) {
config.mcpServers = {};
}
if (config.mcpServers.wikipedia) {
console.log(` Already installed in ${name}`);
return false;
}
config.mcpServers.wikipedia = MCP_CONFIG;
// Ensure parent directory exists
const dir = configPath.substring(0, configPath.lastIndexOf("/"));
if (!existsSync(dir)) {
await Bun.$`mkdir -p ${dir}`;
}
await writeJsonFile(configPath, config);
console.log(` Installed to ${name}`);
return true;
}
async function install(): Promise<void> {
console.log("Installing wiki-mcp...\n");
let installed = false;
// Install to Claude Desktop
const desktopConfig = CLAUDE_DESKTOP_CONFIGS[process.platform];
if (desktopConfig) {
try {
const result = await installToConfig(desktopConfig, "Claude Desktop");
installed = installed || result;
} catch (err) {
console.log(` Could not install to Claude Desktop: ${err}`);
}
}
// Install to Claude Code
try {
const result = await installToConfig(CLAUDE_CODE_CONFIG, "Claude Code");
installed = installed || result;
} catch (err) {
console.log(` Could not install to Claude Code: ${err}`);
}
console.log("");
if (installed) {
console.log("Done! Restart Claude to use the Wikipedia tools.");
} else {
console.log("wiki-mcp is already installed.");
}
}
function showHelp(): void {
console.log(`wiki-mcp - MCP server for Wikipedia
Usage:
bunx wiki-mcp Start the MCP server
bunx wiki-mcp install Install to Claude Desktop and Claude Code
bunx wiki-mcp help Show this help message
Tools provided:
wiki_search Search Wikipedia articles
wiki_get_article Get full article content
wiki_get_summary Get article summary
wiki_random Get random articles
wiki_get_categories Get article categories
wiki_get_links Get internal links
wiki_get_images Get article images
wiki_get_languages Get language versions
wiki_get_references Get external references
`);
}
export async function runCli(args: string[]): Promise<boolean> {
const command = args[0];
switch (command) {
case "install":
await install();
return true;
case "help":
case "--help":
case "-h":
showHelp();
return true;
default:
return false;
}
}