wikinews-mcp 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -0
- package/dist/api.js +35 -0
- package/dist/gateway.js +85 -0
- package/dist/server.js +76 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -18,3 +18,23 @@ node dist/index.js
|
|
|
18
18
|
```
|
|
19
19
|
|
|
20
20
|
Data comes from the public Wikinews API.
|
|
21
|
+
|
|
22
|
+
<!-- paywall -->
|
|
23
|
+
## Premium tools
|
|
24
|
+
|
|
25
|
+
These tools need a licence key:
|
|
26
|
+
|
|
27
|
+
* `article`
|
|
28
|
+
* `browse_categories`
|
|
29
|
+
* `category_members`
|
|
30
|
+
|
|
31
|
+
The tool bodies run on our gateway, not inside this package, so the key is what buys access. Buy one at https://buy.polar.sh/polar_cl_mrO7rS5LZxpqM7oAFFEA10i9121J240U40IIj4JRZO4 and set it in your MCP client config:
|
|
32
|
+
|
|
33
|
+
```json
|
|
34
|
+
"env": { "MCP_LICENSE_KEY": "polar_..." }
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
The gateway checks the key against Polar and gives every key 1,000 free premium
|
|
38
|
+
calls a month before per-call billing starts. Every other tool on this server
|
|
39
|
+
stays free.
|
|
40
|
+
<!-- /paywall -->
|
package/dist/api.js
CHANGED
|
@@ -26,3 +26,38 @@ export async function search(args) {
|
|
|
26
26
|
.map((r, i) => `${i + 1}. ${r.title ?? ''}${r.timestamp ? ` | ${String(r.timestamp).slice(0, 10)}` : ''}\n ${(r.snippet ?? '').replace(/<[^>]*>/g, '')}`)
|
|
27
27
|
.join('\n');
|
|
28
28
|
}
|
|
29
|
+
// ---- gap4a4 tools:
|
|
30
|
+
async function gapQuery(params) {
|
|
31
|
+
const query = new URLSearchParams({ action: 'query', format: 'json', ...params });
|
|
32
|
+
const res = await fetch(`https://en.wikinews.org/w/api.php?${query}`, {
|
|
33
|
+
headers: { 'User-Agent': 'mrfentmen-wikinews-mcp/1.0', Accept: 'application/json' },
|
|
34
|
+
signal: AbortSignal.timeout(20000),
|
|
35
|
+
});
|
|
36
|
+
if (!res.ok)
|
|
37
|
+
throw new Error(`Wiki API returned ${res.status}`);
|
|
38
|
+
const data = await res.json();
|
|
39
|
+
if (data.error)
|
|
40
|
+
throw new Error(data.error.info ?? 'Wiki API error');
|
|
41
|
+
return data;
|
|
42
|
+
}
|
|
43
|
+
export async function pageHistory(args) {
|
|
44
|
+
const limit = Math.max(1, Math.min(Math.round(args.limit ?? 10), 30));
|
|
45
|
+
const title = args.title.trim();
|
|
46
|
+
if (!title)
|
|
47
|
+
throw new Error('Provide a page title.');
|
|
48
|
+
const data = await gapQuery({ prop: 'revisions', titles: title, rvprop: 'ids|timestamp|user|comment', rvlimit: String(limit) });
|
|
49
|
+
const page = Object.values(data.query?.pages ?? {})[0];
|
|
50
|
+
if (!page || page.missing !== undefined)
|
|
51
|
+
return 'Page not found.';
|
|
52
|
+
const rows = page.revisions ?? [];
|
|
53
|
+
return rows.length ? `${page.title}\n` + rows.slice(0, limit).map((r) => `${r.revid ?? 'not reported'} | ${r.timestamp ?? 'not reported'} | ${r.user ?? 'not reported'} | ${String(r.comment ?? 'not reported').slice(0, 300)}`).join('\n') : 'No revisions returned.';
|
|
54
|
+
}
|
|
55
|
+
export async function pageBacklinks(args) {
|
|
56
|
+
const limit = Math.max(1, Math.min(Math.round(args.limit ?? 10), 30));
|
|
57
|
+
const title = args.title.trim();
|
|
58
|
+
if (!title)
|
|
59
|
+
throw new Error('Provide a page title.');
|
|
60
|
+
const data = await gapQuery({ list: 'backlinks', bltitle: title, bllimit: String(limit) });
|
|
61
|
+
const rows = data.query?.backlinks ?? [];
|
|
62
|
+
return rows.length ? rows.slice(0, limit).map((r, i) => `${i + 1}. ${String(r.title ?? 'not reported').slice(0, 250)} | page ${r.pageid ?? 'not reported'} | namespace ${r.ns ?? 'not reported'}`).join('\n') : 'No backlinks returned.';
|
|
63
|
+
}
|
package/dist/gateway.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thin client for the premium gateway.
|
|
3
|
+
*
|
|
4
|
+
* The premium tools do not run in this package any more. Their bodies live on
|
|
5
|
+
* the gateway, which checks a Polar licence key before it does any work, so
|
|
6
|
+
* deleting anything here buys nothing: the answer only exists behind the gate.
|
|
7
|
+
*
|
|
8
|
+
* Set `MCP_LICENSE_KEY` to a Polar licence key. `MCP_GATEWAY_URL` overrides the
|
|
9
|
+
* endpoint, which is only useful for testing.
|
|
10
|
+
*/
|
|
11
|
+
const DEFAULT_GATEWAY_URL = 'https://mcp-gateway.contactae2000.workers.dev/mcp';
|
|
12
|
+
const DEFAULT_TIMEOUT_MS = 25000;
|
|
13
|
+
const USER_AGENT = 'mrfentmen-mcp/1.0';
|
|
14
|
+
/** Raised when a premium call cannot be completed. `message` is safe to show. */
|
|
15
|
+
export class GatewayError extends Error {
|
|
16
|
+
refused;
|
|
17
|
+
constructor(message, refused) {
|
|
18
|
+
super(message);
|
|
19
|
+
this.refused = refused;
|
|
20
|
+
this.name = 'GatewayError';
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export async function premiumTool(tool, args) {
|
|
24
|
+
const key = (process.env.MCP_LICENSE_KEY ?? '').trim();
|
|
25
|
+
if (!key) {
|
|
26
|
+
throw new GatewayError(`"${tool}" is a premium tool and no licence key is set. Set MCP_LICENSE_KEY in your MCP client config.`, true);
|
|
27
|
+
}
|
|
28
|
+
const url = (process.env.MCP_GATEWAY_URL ?? DEFAULT_GATEWAY_URL).trim();
|
|
29
|
+
const controller = new AbortController();
|
|
30
|
+
const timer = setTimeout(() => controller.abort(), DEFAULT_TIMEOUT_MS);
|
|
31
|
+
let response;
|
|
32
|
+
try {
|
|
33
|
+
response = await fetch(url, {
|
|
34
|
+
method: 'POST',
|
|
35
|
+
headers: {
|
|
36
|
+
'content-type': 'application/json',
|
|
37
|
+
accept: 'application/json',
|
|
38
|
+
'user-agent': USER_AGENT,
|
|
39
|
+
authorization: `Bearer ${key}`,
|
|
40
|
+
},
|
|
41
|
+
body: JSON.stringify({
|
|
42
|
+
jsonrpc: '2.0',
|
|
43
|
+
id: 1,
|
|
44
|
+
method: 'tools/call',
|
|
45
|
+
params: { name: tool, arguments: args },
|
|
46
|
+
}),
|
|
47
|
+
signal: controller.signal,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
throw new GatewayError(`"${tool}" could not reach the licence gateway. Check your connection and try again.`, false);
|
|
52
|
+
}
|
|
53
|
+
finally {
|
|
54
|
+
clearTimeout(timer);
|
|
55
|
+
}
|
|
56
|
+
if (!response.ok) {
|
|
57
|
+
throw new GatewayError(`"${tool}" could not reach the licence gateway (HTTP ${response.status}).`, false);
|
|
58
|
+
}
|
|
59
|
+
let body;
|
|
60
|
+
try {
|
|
61
|
+
body = (await response.json());
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
throw new GatewayError(`"${tool}" got an unreadable answer from the licence gateway.`, false);
|
|
65
|
+
}
|
|
66
|
+
if (body.error) {
|
|
67
|
+
throw new GatewayError(`"${tool}" was refused by the licence gateway: ${body.error.message ?? 'unknown error'}`, false);
|
|
68
|
+
}
|
|
69
|
+
const result = body.result ?? {};
|
|
70
|
+
const text = (result.content ?? [])
|
|
71
|
+
.map((part) => part.text ?? '')
|
|
72
|
+
.join('\n')
|
|
73
|
+
.trim();
|
|
74
|
+
if (result.isError) {
|
|
75
|
+
// The gateway marks a licence refusal structurally, so a missing or rejected
|
|
76
|
+
// key is told apart from a tool that failed after the gate let it through.
|
|
77
|
+
if (result.structuredContent?.licenceRefusal) {
|
|
78
|
+
throw new GatewayError(text || `"${tool}" needs a valid licence key.`, true);
|
|
79
|
+
}
|
|
80
|
+
throw new GatewayError(text || `"${tool}" failed on the licence gateway.`, false);
|
|
81
|
+
}
|
|
82
|
+
if (!text)
|
|
83
|
+
throw new GatewayError(`"${tool}" returned no data.`, false);
|
|
84
|
+
return text;
|
|
85
|
+
}
|
package/dist/server.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
2
|
import { z } from "zod";
|
|
3
|
-
import { search } from "./api.js";
|
|
3
|
+
import { search, pageHistory, pageBacklinks } from "./api.js";
|
|
4
|
+
import { premiumTool } from "./gateway.js";
|
|
4
5
|
const text = (value) => ({ content: [{ type: "text", text: value }] });
|
|
5
6
|
const textError = (t) => ({ content: [{ type: "text", text: t }], isError: true });
|
|
6
7
|
const READ_ONLY = { readOnlyHint: true, openWorldHint: true };
|
|
@@ -20,5 +21,79 @@ export function createServer() {
|
|
|
20
21
|
return textError(error(e));
|
|
21
22
|
}
|
|
22
23
|
});
|
|
24
|
+
server.registerTool("article", {
|
|
25
|
+
title: "Article",
|
|
26
|
+
description: "Full plain text of a Wikinews article, from a title found with search.",
|
|
27
|
+
inputSchema: z.object({ title: z.string().describe("Exact Wikinews article title.") }),
|
|
28
|
+
annotations: READ_ONLY,
|
|
29
|
+
}, async (args) => {
|
|
30
|
+
// ---- paywall: article ----
|
|
31
|
+
// The body runs on the gateway, behind the licence gate.
|
|
32
|
+
// ---- paywall: end ----
|
|
33
|
+
try {
|
|
34
|
+
return text(await premiumTool("mcp_wikinews_article", args));
|
|
35
|
+
}
|
|
36
|
+
catch (e) {
|
|
37
|
+
return textError(error(e));
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
server.registerTool("browse_categories", {
|
|
41
|
+
title: "Browse categories",
|
|
42
|
+
description: "List Wikinews categories whose name starts with a prefix, to find the beat a story belongs to.",
|
|
43
|
+
inputSchema: z.object({ prefix: z.string().describe("Category name prefix, e.g. \"Afri\"."), limit: z.number().int().min(1).max(50).default(20).describe("Max categories.") }),
|
|
44
|
+
annotations: READ_ONLY,
|
|
45
|
+
}, async (args) => {
|
|
46
|
+
// ---- paywall: browse_categories ----
|
|
47
|
+
// The body runs on the gateway, behind the licence gate.
|
|
48
|
+
// ---- paywall: end ----
|
|
49
|
+
try {
|
|
50
|
+
return text(await premiumTool("mcp_wikinews_browse_categories", args));
|
|
51
|
+
}
|
|
52
|
+
catch (e) {
|
|
53
|
+
return textError(error(e));
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
server.registerTool("category_members", {
|
|
57
|
+
title: "Category members",
|
|
58
|
+
description: "List pages and subcategories in an exact category.",
|
|
59
|
+
inputSchema: z.object({ category: z.string().describe("Category name, with or without Category: prefix."), limit: z.number().describe("Maximum results, 1-30.").optional() }),
|
|
60
|
+
annotations: READ_ONLY,
|
|
61
|
+
}, async (args) => {
|
|
62
|
+
// ---- paywall: category_members ----
|
|
63
|
+
// The body runs on the gateway, behind the licence gate.
|
|
64
|
+
// ---- paywall: end ----
|
|
65
|
+
try {
|
|
66
|
+
return text(await premiumTool("mcp_wikinews_category_members", args));
|
|
67
|
+
}
|
|
68
|
+
catch (e) {
|
|
69
|
+
return textError(error(e));
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
server.registerTool("page_history", {
|
|
73
|
+
title: "Page history",
|
|
74
|
+
description: "Inspect recent revisions of a page, including revision IDs, editors, timestamps and edit summaries.",
|
|
75
|
+
inputSchema: z.object({ title: z.string().describe("Exact page title."), limit: z.number().describe("Maximum results, 1-30.").optional() }),
|
|
76
|
+
annotations: READ_ONLY,
|
|
77
|
+
}, async (args) => {
|
|
78
|
+
try {
|
|
79
|
+
return text(await pageHistory(args));
|
|
80
|
+
}
|
|
81
|
+
catch (e) {
|
|
82
|
+
return textError(error(e));
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
server.registerTool("backlinks", {
|
|
86
|
+
title: "Page backlinks",
|
|
87
|
+
description: "Find pages that link to an exact wiki page.",
|
|
88
|
+
inputSchema: z.object({ title: z.string().describe("Exact target page title."), limit: z.number().describe("Maximum results, 1-30.").optional() }),
|
|
89
|
+
annotations: READ_ONLY,
|
|
90
|
+
}, async (args) => {
|
|
91
|
+
try {
|
|
92
|
+
return text(await pageBacklinks(args));
|
|
93
|
+
}
|
|
94
|
+
catch (e) {
|
|
95
|
+
return textError(error(e));
|
|
96
|
+
}
|
|
97
|
+
});
|
|
23
98
|
return server;
|
|
24
99
|
}
|
package/package.json
CHANGED