#!/usr/bin/env node
/**
* Installation script for Perplexity Search MCP Skill
* Configures Claude Code to use the MCP server and installs the skill definition
*/
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const projectRoot = path.dirname(__dirname);
const CLAUDE_DIR = path.join(process.env.HOME || "~", ".claude");
const MCP_CONFIG_PATH = path.join(CLAUDE_DIR, "mcp.json");
const SKILLS_DIR = path.join(CLAUDE_DIR, "skills", "perplexity");
const SAP_AI_PROXY_URL =
process.env.SAP_AI_PROXY_URL || "http://127.0.0.1:3030";
// Skill definition content
const SKILL_MD = `# Perplexity Web Search
Use Perplexity AI via SAP AI Core for real-time web search with citations.
## When to Use
- User explicitly invokes \`/perplexity <query>\`
- User needs real-time web information with source citations
- User asks about current events, recent news, or up-to-date data
- As an alternative to the built-in WebSearch tool
## Available Tools
### perplexity_web_search (Full-featured)
Comprehensive web search with all configuration options.
**Parameters:**
- \`query\` (required): The search query
- \`model\` (optional): \`sonar\` | \`sonar-pro\` | \`sonar-reasoning\`
- \`sonar\`: Fast real-time search
- \`sonar-pro\`: Enhanced reasoning with citations
- \`sonar-reasoning\`: Deep analysis and reasoning
- \`search_recency_filter\` (optional): \`day\` | \`week\` | \`month\` | \`year\`
- \`return_citations\` (optional): boolean, default \`true\`
### perplexity_quick_search (Simplified)
Quick search with default settings for fast queries.
**Parameters:**
- \`query\` (required): The quick search query
## Usage Examples
### Explicit invocation
\`\`\`
User: /perplexity What are the latest developments in AI?
\`\`\`
Use \`perplexity_web_search\` with the query.
### Current events
\`\`\`
User: What happened in tech news today?
\`\`\`
Use \`perplexity_web_search\` with \`search_recency_filter: "day"\`.
### Quick lookup
\`\`\`
User: Quick, what's the current Bitcoin price?
\`\`\`
Use \`perplexity_quick_search\` for fast results.
### Research with specific model
\`\`\`
User: I need a detailed analysis of climate change policies in 2026
\`\`\`
Use \`perplexity_web_search\` with \`model: "sonar-reasoning"\` for deep analysis.
## Response Format
Responses include:
1. Main answer content from Perplexity
2. Source citations (numbered list of URLs)
3. Search results with titles and snippets (when available)
## Notes
- Requires SAP AI Core proxy running (default: http://127.0.0.1:3030)
- Perplexity models must be deployed in SAP AI Core
- Citations are automatically included unless disabled
`;
function ensureDir(dir) {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
console.log(`Created directory: ${dir}`);
}
}
function updateMcpConfig() {
let config = { mcpServers: {} };
// Read existing config if it exists
if (fs.existsSync(MCP_CONFIG_PATH)) {
try {
const content = fs.readFileSync(MCP_CONFIG_PATH, "utf-8");
config = JSON.parse(content);
if (!config.mcpServers) {
config.mcpServers = {};
}
} catch (e) {
console.warn("Warning: Could not parse existing mcp.json, creating new");
}
}
// Add perplexity-search server
config.mcpServers["perplexity-search"] = {
command: "node",
args: [path.join(projectRoot, "dist", "index.js")],
env: {
SAP_AI_PROXY_URL: SAP_AI_PROXY_URL,
},
};
// Write config
ensureDir(CLAUDE_DIR);
fs.writeFileSync(MCP_CONFIG_PATH, JSON.stringify(config, null, 2));
console.log(`Updated MCP config: ${MCP_CONFIG_PATH}`);
}
function installSkill() {
ensureDir(SKILLS_DIR);
const skillPath = path.join(SKILLS_DIR, "SKILL.md");
fs.writeFileSync(skillPath, SKILL_MD);
console.log(`Installed skill: ${skillPath}`);
}
function main() {
console.log("Installing Perplexity Search MCP Skill...\n");
// Check if dist/index.js exists
const distPath = path.join(projectRoot, "dist", "index.js");
if (!fs.existsSync(distPath)) {
console.error("Error: dist/index.js not found. Run 'npm run build' first.");
process.exit(1);
}
updateMcpConfig();
installSkill();
console.log("\n✅ Installation complete!");
console.log("\nNext steps:");
console.log("1. Ensure sap-ai-proxy is running at " + SAP_AI_PROXY_URL);
console.log("2. Ensure Perplexity model (sonar-pro) is deployed in SAP AI Core");
console.log("3. Restart Claude Code to load the new MCP server");
console.log('4. Use "/perplexity <query>" to search the web');
}
main();