perplexity_search
Search the web using Perplexity AI models to get context-aware answers with citations for research, learning, and information discovery.
Instructions
Search using Perplexity AI's models with context-aware responses and citations
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | ||
| model | No | Model to use (sonar-reasoning-pro, sonar-reasoning, sonar-pro, sonar) | sonar |
| count | No |
Implementation Reference
- src/perplexity/index.ts:41-67 (handler)The asynchronous handler function that executes the Perplexity search using the OpenAI client configured for Perplexity API. It logs the query, makes the chat completion request, formats the response, and handles errors gracefully.async ({ query, model, count }) => { try { logger.info(`Performing search with model ${model}: ${query}`); const response = await client.chat.completions.create({ model, messages: [{ role: "user", content: query }], max_tokens: count * 100 }); return { content: [{ type: "text", text: response.choices[0]?.message.content || "No results found" }] }; } catch (error) { logger.error("Search error:", error); return { content: [{ type: "text", text: `Error performing search: ${error instanceof Error ? error.message : String(error)}` }], isError: true }; } } );
- src/perplexity/index.ts:36-40 (schema)Zod schema defining the input parameters for the tool: query (required string), model (optional enum default 'sonar'), count (optional number 1-10 default 5).{ query: z.string().min(1), model: z.enum(MODELS).default("sonar").describe("Model to use (sonar-reasoning-pro, sonar-reasoning, sonar-pro, sonar)"), count: z.number().min(1).max(10).optional().default(5) },
- src/perplexity/index.ts:34-67 (registration)Registration of the 'perplexity_search' tool on the MCP server, including name, description, input schema, and handler function."perplexity_search", "Search using Perplexity AI's models with context-aware responses and citations", { query: z.string().min(1), model: z.enum(MODELS).default("sonar").describe("Model to use (sonar-reasoning-pro, sonar-reasoning, sonar-pro, sonar)"), count: z.number().min(1).max(10).optional().default(5) }, async ({ query, model, count }) => { try { logger.info(`Performing search with model ${model}: ${query}`); const response = await client.chat.completions.create({ model, messages: [{ role: "user", content: query }], max_tokens: count * 100 }); return { content: [{ type: "text", text: response.choices[0]?.message.content || "No results found" }] }; } catch (error) { logger.error("Search error:", error); return { content: [{ type: "text", text: `Error performing search: ${error instanceof Error ? error.message : String(error)}` }], isError: true }; } } );
- src/perplexity/index.ts:25-30 (helper)Const array defining the supported Perplexity models used in the schema's enum validation.const MODELS = [ "sonar-reasoning-pro", "sonar-reasoning", "sonar-pro", "sonar" ] as const;
- src/perplexity/index.ts:15-18 (helper)Initialization of the OpenAI client configured for Perplexity AI API, used by the handler.const client = new OpenAI({ apiKey: PERPLEXITY_API_KEY, baseURL: "https://api.perplexity.ai" });