search
Search the web using Perplexity AI to find information, answer questions, and retrieve relevant results through Claude's interface.
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:93-153 (handler)MCP CallToolRequestSchema handler that implements the 'search' tool logic by validating arguments and calling Perplexity AI's chat completions API.this.server.setRequestHandler(CallToolRequestSchema, async (request) => { if (request.params.name !== 'search') { throw new McpError( ErrorCode.MethodNotFound, `Unknown tool: ${request.params.name}` ); } if (!isValidSearchArgs(request.params.arguments)) { throw new McpError( ErrorCode.InvalidParams, 'Invalid search arguments. Query must be a non-empty string.' ); } try { const response = await this.axiosInstance.post<SearchResponse>('/chat/completions', { model: MODEL, messages: [ { role: 'system', content: 'You are a helpful assistant that searches the web for accurate information.' }, { role: 'user', content: request.params.arguments.query } ] }); if (response.data.choices && response.data.choices.length > 0) { return { content: [ { type: 'text', text: response.data.choices[0].message.content, }, ], }; } else { throw new Error('No response content received'); } } catch (error) { if (axios.isAxiosError(error)) { const errorMessage = error.response?.data?.error?.message || error.response?.data?.detail || error.message; console.error('Full error:', JSON.stringify(error.response?.data, null, 2)); return { content: [ { type: 'text', text: `Perplexity API error: ${errorMessage}`, }, ], isError: true, }; } throw error; } });
- src/index.ts:74-91 (registration)Registration of the 'search' tool in the ListToolsRequestSchema handler, providing name, description, and input schema.this.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: [ { name: 'search', description: 'Search the web using Perplexity AI', inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'The search query', }, }, required: ['query'], }, }, ], }));
- src/index.ts:24-30 (schema)TypeScript interface defining the expected response structure from Perplexity AI API.interface SearchResponse { choices: [{ message: { content: string; }; }]; }
- src/index.ts:79-88 (schema)Input schema definition for the 'search' tool.inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'The search query', }, }, required: ['query'], },
- src/index.ts:32-36 (helper)Helper function to validate the arguments for the 'search' tool.const isValidSearchArgs = (args: any): args is { query: string } => typeof args === 'object' && args !== null && typeof args.query === 'string' && args.query.trim().length > 0;