perplexity_search
Perform web searches using Perplexity AI to retrieve relevant results through an API interface. Input a query to access concise and accurate search outcomes for your needs.
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 logic for executing the 'perplexity_search' tool. It validates the API key and query, makes a POST request to Perplexity AI's chat completions endpoint, and returns the response content.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:18-32 (registration)Registration of the 'perplexity_search' tool in the server's capabilities, including name, description, and input schema.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:42-54 (schema)Tool schema returned by the ListTools handler, defining the input schema for '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)Registration of the ListTools request handler, which lists the available 'perplexity_search' tool.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"] } } ] }; });
- src/index.ts:60-123 (registration)Registration of the CallTool request handler, which dispatches to the 'perplexity_search' implementation.server.setRequestHandler(CallToolRequestSchema, async (request) => { 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}` ); } } throw new McpError(ErrorCode.MethodNotFound, "Tool not found"); });