wikiSummary.ts•1.37 kB
import { z } from "zod";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
export function registerWikiSummary(server: McpServer) {
server.tool(
"wiki-summary",
"Get a summary of a Wikipedia article in the specified language",
{
title: z.string().describe("Article title"),
lang: z.string().optional().describe("Language code, default en"),
},
async ({ title, lang }) => {
const language = lang || "en";
const url = `https://${language}.wikipedia.org/api/rest_v1/page/summary/${encodeURIComponent(title)}`;
const ctrl = new AbortController();
const id = setTimeout(() => ctrl.abort(), 8000);
try {
const res = await fetch(url, { signal: ctrl.signal });
const data = await res.json();
const result = {
title: data.title,
extract: data.extract,
url: data.content_urls?.desktop?.page,
};
return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
};
} catch (err: any) {
return {
content: [
{
type: "text",
text: JSON.stringify({ error: err.message }),
},
],
};
} finally {
clearTimeout(id);
}
}
);
}