import { z } from "zod";
import { type InferSchema } from "xmcp";
import { headers } from "xmcp/headers";
function getApiKey(headerlist: Record<string, unknown>) {
const xApiKey = headerlist["x-api-key"];
if (typeof xApiKey === "string" && xApiKey.trim()) return xApiKey.trim();
const authorization = headerlist["Authorization"];
if (typeof authorization === "string" && authorization.trim()) {
return authorization.trim().replace(/^Bearer\s+/i, "");
}
return null;
}
export const schema = {
query: z.string().describe("The query to search for"),
};
export const metadata = {
name: "search",
description: "Search the web for information",
annotations: {
title: "Search the web for information",
readOnlyHint: true,
destructiveHint: false,
idempotentHint: true,
},
};
export default async function search({ query }: InferSchema<typeof schema>) {
const headerlist = headers();
const apiKey = getApiKey(headerlist);
if (!apiKey) {
return {
content: [
{ type: "text", text: "Missing API key. Provide request header `x-api-key` (recommended) or `Authorization: Bearer <key>`." }
],
};
}
const response = await fetch(`https://api.scira.ai/api/search`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": apiKey as string,
},
body: JSON.stringify({
messages: [
{ role: "user", content: query }
]
})
})
if (!response.ok) {
return {
content: [
{ type: "text", text: `Error: ${response.status} ${response.statusText}` }
],
};
}
const data = await response.json();
return {
content: [
{ type: "text", text: `${data.text}\n\nSources:\n${Array.isArray(data.sources) ? data.sources.join("\n") : ""}` }
],
};
}