#!/usr/bin/env node 'use strict'; const readline = require('readline'); const BASE = (process.env.ARKA_SEARCH_URL || 'https://search.arka-ai.ru').replace(/\/+$/, ''); const KEY = process.env.ARKA_API_KEY || process.env.ARKA_SEARCH_TOKEN || ''; const UA = process.env.ARKA_SEARCH_UA || 'arka-search-mcp/1.0'; const DEFAULT_PROTOCOL = '2025-06-18'; const TOOLS = [ { name: 'web_search', description: 'Search the web via Arka (self-hosted SearXNG). Returns ranked results with title, url and snippet.', inputSchema: { type: 'object', properties: { q: { type: 'string', description: 'Search query' }, language: { type: 'string', description: 'Language hint, e.g. ru, en' }, categories: { type: 'string', description: 'general, news, images, it, science, ...', }, pageno: { type: 'integer', description: '1-based page number' }, time_range: { type: 'string', description: 'day | week | month | year' }, }, required: ['q'], }, }, { name: 'web_fetch', description: 'Fetch a URL through Arka and return its readable content as Markdown (SSRF-guarded).', inputSchema: { type: 'object', properties: { url: { type: 'string', description: 'Absolute http(s) URL' }, }, required: ['url'], }, }, ]; function send(msg) { process.stdout.write(JSON.stringify(msg) + '\n'); } function reply(id, result) { send({ jsonrpc: '2.0', id, result }); } function replyError(id, code, message) { send({ jsonrpc: '2.0', id, error: { code, message } }); } async function callGateway(path, params) { if (!KEY) { throw new Error('ARKA_API_KEY is not set in the server environment'); } const url = new URL(BASE + path); for (const [k, v] of Object.entries(params)) { if (v !== undefined && v !== null && v !== '') url.searchParams.set(k, String(v)); } const res = await fetch(url, { headers: { 'x-api-key': KEY, accept: 'application/json', 'user-agent': UA, }, signal: AbortSignal.timeout(25000), }); const text = await res.text(); if (!res.ok) { throw new Error(`Arka search ${res.status}: ${text.slice(0, 400)}`); } return text; } async function callTool(name, args) { if (name === 'web_search') { return callGateway('/search', { q: args.q, language: args.language, categories: args.categories, pageno: args.pageno, time_range: args.time_range, format: 'json', }); } if (name === 'web_fetch') { return callGateway('/fetch', { url: args.url }); } throw new Error('Unknown tool: ' + name); } async function handle(msg) { const id = msg.id; const method = msg.method; if (method === 'initialize') { const requested = msg.params && msg.params.protocolVersion; reply(id, { protocolVersion: requested || DEFAULT_PROTOCOL, capabilities: { tools: { listChanged: false } }, serverInfo: { name: 'arka-search', version: '1.0.0' }, }); return; } if (typeof method === 'string' && method.startsWith('notifications/')) { return; } if (method === 'ping') { reply(id, {}); return; } if (method === 'tools/list') { reply(id, { tools: TOOLS }); return; } if (method === 'tools/call') { const name = msg.params && msg.params.name; const args = (msg.params && msg.params.arguments) || {}; try { const text = await callTool(name, args); reply(id, { content: [{ type: 'text', text }] }); } catch (err) { reply(id, { content: [{ type: 'text', text: String((err && err.message) || err) }], isError: true, }); } return; } if (id !== undefined && id !== null) { replyError(id, -32601, 'Method not found: ' + method); } } const rl = readline.createInterface({ input: process.stdin }); rl.on('line', (line) => { const trimmed = line.trim(); if (!trimmed) return; let msg; try { msg = JSON.parse(trimmed); } catch { return; } handle(msg).catch((err) => { if (msg && msg.id !== undefined && msg.id !== null) { replyError(msg.id, -32603, String((err && err.message) || err)); } }); });