perplexity_search
Search the web using Perplexity AI to retrieve relevant information and answers for your queries.
Instructions
Search the web using Perplexity AI
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | The search query |
Implementation Reference
- src/index.ts:61-120 (handler)The handler function for the 'perplexity_search' tool. It checks for the API key, validates the query argument, makes a POST request to Perplexity AI's chat completions endpoint using the 'sonar-pro' model, and returns the text response.if (request.params.name === "perplexity_search") { const apiKey = process.env.PERPLEXITY_API_KEY; if (!apiKey) { throw new McpError( ErrorCode.InvalidRequest, "PERPLEXITY_API_KEY environment variable is not set" ); } const args = request.params.arguments; if (!args || typeof args.query !== "string") { throw new McpError( ErrorCode.InvalidParams, "Query parameter is required and must be a string" ); } try { const response = await fetch( "https://api.perplexity.ai/chat/completions", { method: "POST", headers: { Authorization: `Bearer ${apiKey}`, "Content-Type": "application/json" }, body: JSON.stringify({ model: "sonar-pro", messages: [ { role: "user", content: args.query } ] }) } ); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.json(); return { content: [ { type: "text", text: data.choices[0].message.content } ] }; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); throw new McpError( ErrorCode.InternalError, `Search failed: ${errorMessage}` ); } }
- src/index.ts:21-30 (schema)Input schema definition for the 'perplexity_search' tool, specifying a required 'query' string parameter.inputSchema: { type: "object", properties: { query: { type: "string", description: "The search query" } }, required: ["query"] }
- src/index.ts:18-31 (registration)Tool registration in server capabilities, defining name, description, and input schema for 'perplexity_search'.perplexity_search: { name: "perplexity_search", description: "Search the web using Perplexity AI", inputSchema: { type: "object", properties: { query: { type: "string", description: "The search query" } }, required: ["query"] } }
- src/index.ts:38-57 (registration)ListTools request handler that returns the registered 'perplexity_search' tool details.server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: [ { name: "perplexity_search", description: "Search the web using Perplexity AI", inputSchema: { type: "object", properties: { query: { type: "string", description: "The search query" } }, required: ["query"] } } ] }; });