@mseep/mcp-omnisearch
Version:
MCP server for integrating Omnisearch with LLMs
68 lines (67 loc) • 2.8 kB
JavaScript
import { validate_api_key } from '../../../common/utils.js';
import { config } from '../../../config/env.js';
export class KagiFastGPTProvider {
constructor() {
this.name = 'kagi_fastgpt';
this.description = 'Quick AI-generated answers with citations, optimized for rapid response (900ms typical start time). Runs full search underneath for enriched answers.';
}
async search(params) {
const response = await this.get_answer(params.query);
const results = [];
// Add the main answer as first result
results.push({
title: 'Kagi FastGPT Response',
url: 'https://kagi.com/fastgpt',
snippet: response.data.output,
source_provider: this.name,
});
// Add references if available
if (response.data.references &&
response.data.references.length > 0) {
results.push(...response.data.references.map((ref) => ({
title: ref.title,
url: ref.url,
snippet: ref.snippet,
source_provider: this.name,
})));
}
// Filter out any results with missing required fields
const filtered_results = results.filter((result) => result.title && result.url && result.snippet);
// Respect the limit parameter
if (params.limit && params.limit > 0) {
return filtered_results.slice(0, params.limit);
}
return filtered_results;
}
async get_answer(query, options = {}) {
const api_key = validate_api_key(config.ai_response.kagi_fastgpt.api_key, this.name);
const default_options = {
cache: true,
web_search: true, // Currently only true is supported
};
const final_options = { ...default_options, ...options };
try {
const response = await fetch('https://kagi.com/api/v0/fastgpt', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bot ${api_key}`,
},
body: JSON.stringify({
query,
cache: final_options.cache,
web_search: final_options.web_search,
}),
});
if (!response.ok) {
const error_data = await response.json();
throw new Error(`Kagi FastGPT API error: ${error_data.error || response.statusText}`);
}
return await response.json();
}
catch (error) {
const error_message = error instanceof Error ? error.message : String(error);
throw new Error(`Failed to get Kagi FastGPT answer: ${error_message}`);
}
}
}