chat_perplexity
Send messages to Perplexity AI to maintain conversations with full history context, either starting new chats or continuing existing ones.
Instructions
Maintains ongoing conversations with Perplexity AI. Creates new chats or continues existing ones with full history context.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| message | Yes | The message to send to Perplexity AI | |
| chat_id | No | Optional: ID of an existing chat to continue. If not provided, a new chat will be created. |
Implementation Reference
- index.js:185-216 (handler)Handler for 'chat_perplexity' tool: retrieves chat history, appends user message, calls Perplexity /chat/completions API using 'sonar-reasoning-pro' model, saves assistant response, returns chat_id and response.case "chat_perplexity": { const { message, chat_id = crypto.randomUUID() } = request.params.arguments; // Get chat history const history = yield this.getMessagesForChat(chat_id); // Add new user message const userMessage = { role: "user", content: message }; yield this.addMessageToChat(chat_id, userMessage); // Prepare messages array with history const messages = [...history, userMessage]; // Call Perplexity API const response = yield this.axiosInstance.post("/chat/completions", { model: "sonar-reasoning-pro", messages, }); // Save assistant's response const assistantMessage = { role: "assistant", content: response.data.choices[0].message.content, }; yield this.addMessageToChat(chat_id, assistantMessage); return { content: [ { type: "text", text: JSON.stringify({ chat_id, response: assistantMessage.content, }, null, 2), }, ], }; }
- index.js:88-105 (registration)Tool registration in ListToolsRequestSchema handler, including name, description, and input schema definition.{ name: "chat_perplexity", description: "Maintains ongoing conversations with Perplexity AI. Creates new chats or continues existing ones with full history context.", inputSchema: { type: "object", properties: { message: { type: "string", description: "The message to send to Perplexity AI", }, chat_id: { type: "string", description: "Optional: ID of an existing chat to continue. If not provided, a new chat will be created.", }, }, required: ["message"], }, },
- index.js:92-105 (schema)Input schema for chat_perplexity tool defining required 'message' and optional 'chat_id'.type: "object", properties: { message: { type: "string", description: "The message to send to Perplexity AI", }, chat_id: { type: "string", description: "Optional: ID of an existing chat to continue. If not provided, a new chat will be created.", }, }, required: ["message"], }, },
- index.js:74-82 (helper)Helper to append a message to a specific chat's history and persist to file.addMessageToChat(chatId, message) { return __awaiter(this, void 0, void 0, function* () { const history = yield this.getChatHistory(); if (!history[chatId]) { history[chatId] = []; } history[chatId].push(message); yield this.saveChatHistory(history); });
- index.js:48-61 (helper)Helper to load entire chat history from persistent JSON file, returns empty object if not exists.getChatHistory() { return __awaiter(this, void 0, void 0, function* () { try { const data = yield readFile(this.chatHistoryFile, "utf-8"); return JSON.parse(data); } catch (error) { if (error.code === "ENOENT") { // File does not exist, return empty history return {}; } throw error; } });