Skip to main content
Glama
spences10

MCP Perplexity Search

chat_completion

Generate AI-powered chat responses using Perplexity's API for technical documentation, code review, security practices, and API documentation with structured formats and source references.

Instructions

Generate chat completions using the Perplexity API

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
messagesYes
prompt_templateNoPredefined prompt template to use for common use cases. Available templates: - technical_docs: Technical documentation with code examples and source references - security_practices: Security best practices and implementation guidelines with references - code_review: Code analysis focusing on best practices and improvements - api_docs: API documentation in structured JSON format with examples
custom_templateNoCustom prompt template. If provided, overrides prompt_template.
formatNoResponse format. Use json for structured data, markdown for formatted text with code blocks. Overrides template format if provided.text
include_sourcesNoInclude source URLs in the response. Overrides template setting if provided.
modelNoModel to use for completion. Note: llama-3.1 models will be deprecated after 2/22/2025sonar
temperatureNoControls randomness in the output. Higher values (e.g. 0.8) make the output more random, while lower values (e.g. 0.2) make it more focused and deterministic.
max_tokensNoThe maximum number of tokens to generate in the response. One token is roughly 4 characters for English text.

Implementation Reference

  • Request handler for CallToolRequestSchema that dispatches to chat_completion implementation, which calls Perplexity API with prompt templates and parameters.
    this.server.setRequestHandler( CallToolRequestSchema, async (request) => { if (request.params.name !== 'chat_completion') { throw new McpError( ErrorCode.MethodNotFound, `Unknown tool: ${request.params.name}`, ); } const { messages, prompt_template, custom_template, model = 'sonar', temperature = 0.7, max_tokens = 1024, format: user_format, include_sources: user_include_sources, } = request.params.arguments as { messages: Array<{ role: string; content: string }>; prompt_template?: PromptTemplate; custom_template?: CustomPromptTemplate; model?: string; temperature?: number; max_tokens?: number; format?: 'text' | 'markdown' | 'json'; include_sources?: boolean; }; // Apply template if provided (custom template takes precedence) const template = custom_template ?? (prompt_template ? PROMPT_TEMPLATES[prompt_template] : null); const format = user_format ?? template?.format ?? 'text'; const include_sources = user_include_sources ?? template?.include_sources ?? false; // Merge template system message with user messages if template is provided const final_messages = template ? [ { role: 'system', content: template.system }, ...messages, ] : messages; try { const controller = new AbortController(); const timeoutId = setTimeout( () => controller.abort(), 30000, ); // 30 second timeout try { const response = await fetch( 'https://api.perplexity.ai/chat/completions', { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${PERPLEXITY_API_KEY}`, }, body: JSON.stringify({ model, messages: final_messages, temperature, max_tokens, format, include_sources, }), signal: controller.signal, }, ); if (!response.ok) { const errorData = await response .json() .catch(() => ({})); throw new McpError( ErrorCode.InternalError, `Perplexity API error: ${response.statusText}${ errorData.error ? ` - ${errorData.error}` : '' }`, ); } const data: PerplexityResponse = await response.json(); if (!data.choices?.[0]?.message) { throw new McpError( ErrorCode.InternalError, 'Invalid response format from Perplexity API', ); } return { content: [ { type: format === 'json' ? 'json' : 'text', text: data.choices[0].message.content, }, ], }; } finally { clearTimeout(timeoutId); } } catch (error) { return { content: [ { type: 'text', text: `Error generating completion: ${ error instanceof Error ? error.message : String(error) }`, }, ], isError: true, }; } }, );
  • Input schema definition for the chat_completion tool, including messages, prompt templates, model parameters, etc.
    inputSchema: { type: 'object', properties: { messages: { type: 'array', items: { type: 'object', required: ['role', 'content'], properties: { role: { type: 'string', enum: ['system', 'user', 'assistant'], }, content: { type: 'string', }, }, }, }, prompt_template: { type: 'string', enum: Object.keys(PROMPT_TEMPLATES), description: 'Predefined prompt template to use for common use cases. Available templates:\n' + Object.entries(PROMPT_TEMPLATES) .map( ([key, value]) => `- ${key}: ${value.description}`, ) .join('\n'), }, custom_template: { type: 'object', description: 'Custom prompt template. If provided, overrides prompt_template.', properties: { system: { type: 'string', description: "System message that sets the assistant's role and behavior", }, format: { type: 'string', enum: ['text', 'markdown', 'json'], description: 'Response format', }, include_sources: { type: 'boolean', description: 'Whether to include source URLs in responses', }, }, required: ['system'], }, format: { type: 'string', enum: ['text', 'markdown', 'json'], description: 'Response format. Use json for structured data, markdown for formatted text with code blocks. Overrides template format if provided.', default: 'text', }, include_sources: { type: 'boolean', description: 'Include source URLs in the response. Overrides template setting if provided.', default: false, }, model: { type: 'string', enum: [ 'sonar-pro', 'sonar', 'llama-3.1-sonar-small-128k-online', 'llama-3.1-sonar-large-128k-online', 'llama-3.1-sonar-huge-128k-online', ], description: 'Model to use for completion. Note: llama-3.1 models will be deprecated after 2/22/2025', default: 'sonar', }, temperature: { type: 'number', minimum: 0, maximum: 1, default: 0.7, description: 'Controls randomness in the output. Higher values (e.g. 0.8) make the output more random, while lower values (e.g. 0.2) make it more focused and deterministic.', }, max_tokens: { type: 'number', minimum: 1, maximum: 4096, default: 1024, description: 'The maximum number of tokens to generate in the response. One token is roughly 4 characters for English text.', }, }, required: ['messages'], },
  • src/index.ts:65-169 (registration)
    Registration of the chat_completion tool in the ListToolsRequestSchema handler.
    { name: 'chat_completion', description: 'Generate chat completions using the Perplexity API', inputSchema: { type: 'object', properties: { messages: { type: 'array', items: { type: 'object', required: ['role', 'content'], properties: { role: { type: 'string', enum: ['system', 'user', 'assistant'], }, content: { type: 'string', }, }, }, }, prompt_template: { type: 'string', enum: Object.keys(PROMPT_TEMPLATES), description: 'Predefined prompt template to use for common use cases. Available templates:\n' + Object.entries(PROMPT_TEMPLATES) .map( ([key, value]) => `- ${key}: ${value.description}`, ) .join('\n'), }, custom_template: { type: 'object', description: 'Custom prompt template. If provided, overrides prompt_template.', properties: { system: { type: 'string', description: "System message that sets the assistant's role and behavior", }, format: { type: 'string', enum: ['text', 'markdown', 'json'], description: 'Response format', }, include_sources: { type: 'boolean', description: 'Whether to include source URLs in responses', }, }, required: ['system'], }, format: { type: 'string', enum: ['text', 'markdown', 'json'], description: 'Response format. Use json for structured data, markdown for formatted text with code blocks. Overrides template format if provided.', default: 'text', }, include_sources: { type: 'boolean', description: 'Include source URLs in the response. Overrides template setting if provided.', default: false, }, model: { type: 'string', enum: [ 'sonar-pro', 'sonar', 'llama-3.1-sonar-small-128k-online', 'llama-3.1-sonar-large-128k-online', 'llama-3.1-sonar-huge-128k-online', ], description: 'Model to use for completion. Note: llama-3.1 models will be deprecated after 2/22/2025', default: 'sonar', }, temperature: { type: 'number', minimum: 0, maximum: 1, default: 0.7, description: 'Controls randomness in the output. Higher values (e.g. 0.8) make the output more random, while lower values (e.g. 0.2) make it more focused and deterministic.', }, max_tokens: { type: 'number', minimum: 1, maximum: 4096, default: 1024, description: 'The maximum number of tokens to generate in the response. One token is roughly 4 characters for English text.', }, }, required: ['messages'], }, }, ],
  • Predefined prompt templates referenced in chat_completion schema and used in handler for system messages and format settings.
    export const PROMPT_TEMPLATES = { technical_docs: { system: 'You are a technical documentation assistant. Provide clear, accurate, and well-structured information with code examples where relevant.', format: 'markdown' as const, include_sources: true, description: 'Technical documentation with code examples and source references', }, security_practices: { system: 'You are a security expert. Provide detailed security best practices, implementation guidelines, and potential vulnerability mitigations.', format: 'markdown' as const, include_sources: true, description: 'Security best practices and implementation guidelines with references', }, code_review: { system: 'You are a code review assistant. Analyze code for best practices, potential issues, and suggest improvements.', format: 'markdown' as const, include_sources: false, description: 'Code analysis focusing on best practices and improvements', }, api_docs: { system: 'You are an API documentation assistant. Provide clear explanations of API endpoints, parameters, and usage examples.', format: 'json' as const, include_sources: true, description: 'API documentation in structured JSON format with examples', }, } as const;

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/spences10/mcp-perplexity-search'

If you have feedback or need assistance with the MCP directory API, please join our Discord server