Skip to content

Instantly share code, notes, and snippets.

@funwarioisii
Last active March 22, 2025 14:52
Show Gist options
  • Save funwarioisii/87d6f55ddcc6747d150753535e4041d4 to your computer and use it in GitHub Desktop.
Save funwarioisii/87d6f55ddcc6747d150753535e4041d4 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# frozen_string_literal: true
require "mcp"
require "json"
require "net/http"
name "perplexity-mcp"
version "0.1.0"
# Check for API key
api_key = ENV["PERPLEXITY_API_KEY"]
unless api_key
warn "Error: PERPLEXITY_API_KEY environment variable is required"
exit 1
end
# Get the model from environment variable or use "sonar" as default
model = ENV["PERPLEXITY_MODEL"] || "sonar"
# Log which model is being used (helpful for debug)
warn "Using Perplexity AI model: #{model}"
# List available models
available_models = {
"sonar-deep-research" => "128k context - Enhanced research capabilities",
"sonar-reasoning-pro" => "128k context - Advanced reasoning with professional focus",
"sonar-reasoning" => "128k context - Enhanced reasoning capabilities",
"sonar-pro" => "200k context - Professional grade model",
"sonar" => "128k context - Default model",
"r1-1776" => "128k context - Alternative architecture"
}
warn "Available Perplexity models (set with PERPLEXITY_MODEL environment variable):"
available_models.each do |model_name, description|
marker = model_name == model ? "→" : " "
warn " #{marker} #{model_name}: #{description}"
end
# Define a resource template for prompts
resource_template "prompt://perplexity_search_web/{query}/{recency}" do
name "perplexity_search_web"
description "Search the web using Perplexity AI and filter results by recency"
mime_type "text/plain"
call do |args|
query = args[:query]
recency = args[:recency] || "month"
<<~TEXT
Find recent information about: #{query}
Only include results from the last #{recency}
TEXT
end
end
# Define a search tool
tool "perplexity_search_web" do
description "Search the web using Perplexity AI with recency filtering"
argument :query, String, required: true, description: "The search query to find information about"
argument :recency, String, required: false, description: "Filter results by how recent they are. Options: 'day' (last 24h), 'week' (last 7 days), 'month' (last 30 days), 'year' (last 365 days). Defaults to 'month'."
call do |args|
query = args[:query]
recency = args[:recency] || "month"
# Call Perplexity API
uri = URI("https://api.perplexity.ai/chat/completions")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
payload = {
model: model,
messages: [
{ role: "system", content: "Be precise and concise." },
{ role: "user", content: query }
],
max_tokens: "512",
temperature: 0.2,
top_p: 0.9,
return_images: false,
return_related_questions: false,
search_recency_filter: recency,
top_k: 0,
stream: false,
presence_penalty: 0,
frequency_penalty: 1
}
request = Net::HTTP::Post.new(uri)
request["Authorization"] = "Bearer #{api_key}"
request["Content-Type"] = "application/json"
request.body = payload.to_json
response = http.request(request)
if response.is_a?(Net::HTTPSuccess)
data = JSON.parse(response.body)
data["choices"][0]["message"]["content"]
else
"Error: Failed to get response from Perplexity API (#{response.code})"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment