hn-reddit-mcp 1.0.0 → 1.0.1
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 +19 -0
- package/dist/api.js +95 -0
- package/dist/license.js +79 -0
- package/dist/server.js +51 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -20,3 +20,22 @@ node dist/index.js
|
|
|
20
20
|
```
|
|
21
21
|
|
|
22
22
|
Uses the official Hacker News Firebase API and the public Reddit JSON endpoint.
|
|
23
|
+
|
|
24
|
+
<!-- paywall -->
|
|
25
|
+
## Premium tools
|
|
26
|
+
|
|
27
|
+
These tools need a license key:
|
|
28
|
+
|
|
29
|
+
* `hn_search`
|
|
30
|
+
* `reddit_search`
|
|
31
|
+
|
|
32
|
+
Buy a key at https://mcp-marketplace.io/server/io-github-mrfentmen-hn-reddit-mcp and set it in your MCP client config:
|
|
33
|
+
|
|
34
|
+
```json
|
|
35
|
+
"env": { "MCP_LICENSE_KEY": "mcp_live_..." }
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
The key is checked against MCP Marketplace, cached for 24 hours, and keeps
|
|
39
|
+
working offline once one check has succeeded. Every other tool on this server
|
|
40
|
+
stays free.
|
|
41
|
+
<!-- /paywall -->
|
package/dist/api.js
CHANGED
|
@@ -46,3 +46,98 @@ export async function redditTop(args) {
|
|
|
46
46
|
const posts = parseRss(await res.text()).slice(0, limit);
|
|
47
47
|
return posts.map((p, i) => `${i + 1}. ${p.title}\n u/${p.author || "unknown"} | r/${sub}\n ${p.link}`).join("\n\n") || "No posts found";
|
|
48
48
|
}
|
|
49
|
+
const hnTags = (v) => {
|
|
50
|
+
const value = String(v ?? "").trim().toLowerCase();
|
|
51
|
+
return ["story", "comment", "poll", "show_hn", "ask_hn", "front_page"].includes(value) ? value : "story";
|
|
52
|
+
};
|
|
53
|
+
const hnText = (html, max = 220) => String(html ?? "")
|
|
54
|
+
.replace(/<[^>]*>/g, " ")
|
|
55
|
+
.replace(/&#x([0-9a-f]+);/gi, (_, hex) => String.fromCodePoint(parseInt(hex, 16)))
|
|
56
|
+
.replace(/&#(\d+);/g, (_, dec) => String.fromCodePoint(Number(dec)))
|
|
57
|
+
.replace(/>/g, ">")
|
|
58
|
+
.replace(/</g, "<")
|
|
59
|
+
.replace(/"/g, '"')
|
|
60
|
+
.replace(/'/g, "'")
|
|
61
|
+
.replace(/&/g, "&")
|
|
62
|
+
.replace(/\s+/g, " ")
|
|
63
|
+
.trim()
|
|
64
|
+
.slice(0, max);
|
|
65
|
+
/** Reddit's Atom <name> already carries the /u/ prefix, so do not add it twice. */
|
|
66
|
+
const redditUser = (author) => {
|
|
67
|
+
const name = author.trim().replace(/^\/?u\//i, "");
|
|
68
|
+
return name ? `u/${name}` : "u/unknown";
|
|
69
|
+
};
|
|
70
|
+
export async function hnSearch(args) {
|
|
71
|
+
const query = (args.query ?? "").trim();
|
|
72
|
+
if (!query)
|
|
73
|
+
throw new SocialError("Provide search terms");
|
|
74
|
+
const tag = hnTags(args.tag);
|
|
75
|
+
const perPage = Math.max(1, Math.min(args.hitsPerPage ?? 10, 30));
|
|
76
|
+
const page = Math.max(0, args.page ?? 0);
|
|
77
|
+
const params = new URLSearchParams({ query, tags: tag, hitsPerPage: String(perPage), page: String(page) });
|
|
78
|
+
const minPoints = Math.max(0, Math.min(args.minPoints ?? 0, 100000));
|
|
79
|
+
if (minPoints > 0)
|
|
80
|
+
params.set("numericFilters", `points>${minPoints}`);
|
|
81
|
+
const d = await get(`https://hn.algolia.com/api/v1/search?${params.toString()}`);
|
|
82
|
+
const hits = d.hits ?? [];
|
|
83
|
+
if (!hits.length)
|
|
84
|
+
return `No Hacker News ${tag} entries match "${query}"${minPoints ? ` with more than ${minPoints} points` : ""}.`;
|
|
85
|
+
return [
|
|
86
|
+
`Hacker News ${tag} search for "${query}" - ${d.nbHits ?? 0} matches, page ${(d.page ?? 0) + 1} of ${d.nbPages ?? 1}`,
|
|
87
|
+
...hits.map((h, i) => [
|
|
88
|
+
`${i + 1}. ${h.title ?? (hnText(h.comment_text, 120) || "(untitled)")}`,
|
|
89
|
+
` ${h.points ?? 0} pts | ${h.num_comments ?? 0} comments | by ${h.author ?? "unknown"} | ${h.created_at ?? "date not reported"}`,
|
|
90
|
+
` https://news.ycombinator.com/item?id=${h.objectID ?? ""}${h.url ? ` -> ${h.url}` : ""}`,
|
|
91
|
+
].join("\n")),
|
|
92
|
+
].join("\n");
|
|
93
|
+
}
|
|
94
|
+
export async function hnComments(args) {
|
|
95
|
+
const id = Math.floor(args.id ?? 0);
|
|
96
|
+
if (!id)
|
|
97
|
+
throw new SocialError("Provide a Hacker News item id");
|
|
98
|
+
const maxDepth = Math.max(1, Math.min(args.depth ?? 2, 6));
|
|
99
|
+
const d = await get(`https://hn.algolia.com/api/v1/items/${id}`);
|
|
100
|
+
if (!d || d.title === undefined)
|
|
101
|
+
return `No Hacker News item ${id}.`;
|
|
102
|
+
const lines = [];
|
|
103
|
+
let shown = 0;
|
|
104
|
+
const walk = (nodes, level) => {
|
|
105
|
+
for (const node of nodes ?? []) {
|
|
106
|
+
if (shown >= 40)
|
|
107
|
+
return;
|
|
108
|
+
const text = hnText(node.text, 260);
|
|
109
|
+
if (text) {
|
|
110
|
+
lines.push(`${" ".repeat(level)}- ${node.author ?? "unknown"}: ${text}`);
|
|
111
|
+
shown++;
|
|
112
|
+
}
|
|
113
|
+
if (level + 1 < maxDepth)
|
|
114
|
+
walk(node.children ?? [], level + 1);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
walk(d.children ?? [], 1);
|
|
118
|
+
return [
|
|
119
|
+
`${d.title ?? `item ${id}`} (${d.points ?? 0} pts) by ${d.author ?? "unknown"}`,
|
|
120
|
+
d.url ?? `https://news.ycombinator.com/item?id=${id}`,
|
|
121
|
+
"",
|
|
122
|
+
lines.length ? `Comments (${shown} shown, depth ${maxDepth}):` : "No comment text at this depth.",
|
|
123
|
+
...lines,
|
|
124
|
+
].join("\n");
|
|
125
|
+
}
|
|
126
|
+
export async function redditSearch(args) {
|
|
127
|
+
const sub = (args.subreddit ?? "programming").trim().replace(/^\/?r\//i, "");
|
|
128
|
+
const query = (args.query ?? "").trim();
|
|
129
|
+
if (!sub)
|
|
130
|
+
throw new SocialError("Provide a subreddit name");
|
|
131
|
+
if (!query)
|
|
132
|
+
throw new SocialError("Provide search terms");
|
|
133
|
+
const limit = Math.max(1, Math.min(args.limit ?? 10, 25));
|
|
134
|
+
const url = `https://www.reddit.com/r/${encodeURIComponent(sub)}/search.rss?q=${encodeURIComponent(query)}&restrict_sr=1&sort=relevance&t=year`;
|
|
135
|
+
const res = await fetch(url, { headers: { "User-Agent": UA, Accept: "application/atom+xml" }, signal: AbortSignal.timeout(25000) });
|
|
136
|
+
if (!res.ok)
|
|
137
|
+
throw new SocialError(`Reddit error ${res.status}`);
|
|
138
|
+
const posts = parseRss(await res.text());
|
|
139
|
+
if (!posts.length)
|
|
140
|
+
return `No r/${sub} posts match "${query}".`;
|
|
141
|
+
return `r/${sub} posts matching "${query}" (${Math.min(posts.length, limit)} of ${posts.length} found):\n` +
|
|
142
|
+
posts.slice(0, limit).map((p, i) => `${i + 1}. ${hnText(p.title, 160)}\n ${redditUser(p.author)} | ${p.link}`).join("\n\n");
|
|
143
|
+
}
|
package/dist/license.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// Premium tools on this server need a license key bought from MCP Marketplace.
|
|
2
|
+
// Buyers put the key in their MCP client config as MCP_LICENSE_KEY. Every tool
|
|
3
|
+
// that is not listed in PREMIUM keeps working without a key.
|
|
4
|
+
const SLUG = "hn-reddit-mcp";
|
|
5
|
+
// Tools that need a key. Everything else is free.
|
|
6
|
+
const PREMIUM = new Set([
|
|
7
|
+
"hn_search",
|
|
8
|
+
"reddit_search",
|
|
9
|
+
]);
|
|
10
|
+
const BUY_URL = `https://mcp-marketplace.io/server/io-github-mrfentmen-${SLUG}`;
|
|
11
|
+
// The license check calls the marketplace's verify endpoint directly instead of
|
|
12
|
+
// using @mcp_marketplace/license. That SDK sends no `apikey` header, so every
|
|
13
|
+
// check it makes is rejected by Supabase before it reaches the function. The
|
|
14
|
+
// publishable key below is public by design - it ships in mcp-marketplace.io's
|
|
15
|
+
// own JavaScript and only ever reaches their licence check.
|
|
16
|
+
const DEFAULT_VERIFY_URL = "https://virupvwhtkpkjsiskckg.supabase.co/functions/v1/verify-key";
|
|
17
|
+
const PUBLISHABLE_KEY = "sb_publishable_BuqlW96Ke8C_zzJG-LQv1Q_YHi_r_4h";
|
|
18
|
+
const OK_CACHE_MS = 60 * 60 * 1000;
|
|
19
|
+
const BAD_CACHE_MS = 60 * 1000;
|
|
20
|
+
const seen = new Map();
|
|
21
|
+
const REASONS = {
|
|
22
|
+
missing_key: "no key is set",
|
|
23
|
+
invalid_format: "that key is not a valid MCP Marketplace key",
|
|
24
|
+
not_found: "that key was not found",
|
|
25
|
+
revoked: "that key has been revoked",
|
|
26
|
+
rotated: "that key was rotated, use your new one",
|
|
27
|
+
expired: "that key has expired, renew it",
|
|
28
|
+
rate_limited: "there were too many checks just now, try again shortly",
|
|
29
|
+
network_error: "the license server could not be reached",
|
|
30
|
+
};
|
|
31
|
+
function blocked(tool, reason) {
|
|
32
|
+
const why = REASONS[reason] ?? `the license server answered "${reason}"`;
|
|
33
|
+
return `"${tool}" needs a license key, but ${why}. ` +
|
|
34
|
+
`Set MCP_LICENSE_KEY in your MCP client config. Get a key: ${BUY_URL}`;
|
|
35
|
+
}
|
|
36
|
+
async function verify(key) {
|
|
37
|
+
const res = await fetch(process.env.MCP_LICENSE_VERIFY_URL || DEFAULT_VERIFY_URL, {
|
|
38
|
+
method: "POST",
|
|
39
|
+
headers: {
|
|
40
|
+
apikey: PUBLISHABLE_KEY,
|
|
41
|
+
Authorization: `Bearer ${PUBLISHABLE_KEY}`,
|
|
42
|
+
"Content-Type": "application/json",
|
|
43
|
+
},
|
|
44
|
+
body: JSON.stringify({ key, slug: SLUG }),
|
|
45
|
+
// an unusable key answers 400 with a JSON body, so read the body either way
|
|
46
|
+
signal: AbortSignal.timeout(15000),
|
|
47
|
+
});
|
|
48
|
+
const data = (await res.json());
|
|
49
|
+
if (typeof data?.valid !== "boolean")
|
|
50
|
+
return { valid: false, reason: "unexpected_response" };
|
|
51
|
+
return data;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Returns null when the caller may run the tool, or a short message telling
|
|
55
|
+
* them how to get a key.
|
|
56
|
+
*
|
|
57
|
+
* A good key is remembered for an hour, so a busy session does not hit the
|
|
58
|
+
* license server on every call and keeps working through a brief outage.
|
|
59
|
+
*/
|
|
60
|
+
export async function premiumRequired(tool) {
|
|
61
|
+
if (!PREMIUM.has(tool))
|
|
62
|
+
return null;
|
|
63
|
+
const key = process.env.MCP_LICENSE_KEY;
|
|
64
|
+
if (!key)
|
|
65
|
+
return blocked(tool, "missing_key");
|
|
66
|
+
const hit = seen.get(key);
|
|
67
|
+
if (hit && Date.now() - hit.at < (hit.valid ? OK_CACHE_MS : BAD_CACHE_MS)) {
|
|
68
|
+
return hit.valid ? null : blocked(tool, hit.reason ?? "invalid");
|
|
69
|
+
}
|
|
70
|
+
let result;
|
|
71
|
+
try {
|
|
72
|
+
result = await verify(key);
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
result = { valid: false, reason: "network_error" };
|
|
76
|
+
}
|
|
77
|
+
seen.set(key, { ...result, at: Date.now() });
|
|
78
|
+
return result.valid ? null : blocked(tool, result.reason ?? "invalid");
|
|
79
|
+
}
|
package/dist/server.js
CHANGED
|
@@ -2,7 +2,8 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
import { hnItem } from "./api.js";
|
|
4
4
|
import { hnTop } from "./api.js";
|
|
5
|
-
import { redditTop } from "./api.js";
|
|
5
|
+
import { redditTop, hnSearch, hnComments, redditSearch } from "./api.js";
|
|
6
|
+
import { premiumRequired } from "./license.js";
|
|
6
7
|
const text = (value) => ({ content: [{ type: "text", text: value }] });
|
|
7
8
|
const textError = (t) => ({ content: [{ type: "text", text: t }], isError: true });
|
|
8
9
|
const READ_ONLY = { readOnlyHint: true, openWorldHint: true };
|
|
@@ -48,5 +49,54 @@ export function createServer() {
|
|
|
48
49
|
return textError(error(e));
|
|
49
50
|
}
|
|
50
51
|
});
|
|
52
|
+
server.registerTool("hn_search", {
|
|
53
|
+
title: "HN search",
|
|
54
|
+
description: "Full-text search Hacker News through the Algolia index: match title, points, comment count, author and date, with an optional minimum score.",
|
|
55
|
+
inputSchema: z.object({ query: z.string().min(1).max(200), tag: z.string().default("story").describe("story, comment, poll, show_hn, ask_hn or front_page"), minPoints: z.number().int().min(0).default(0), hitsPerPage: z.number().int().min(1).max(30).default(10), page: z.number().int().min(0).max(50).default(0) }),
|
|
56
|
+
annotations: READ_ONLY,
|
|
57
|
+
}, async (args) => {
|
|
58
|
+
// ---- paywall: hn_search ----
|
|
59
|
+
const paywallMessage = await premiumRequired("hn_search");
|
|
60
|
+
if (paywallMessage)
|
|
61
|
+
return { content: [{ type: "text", text: paywallMessage }], isError: true };
|
|
62
|
+
// ---- paywall: end ----
|
|
63
|
+
try {
|
|
64
|
+
return text(await hnSearch(args));
|
|
65
|
+
}
|
|
66
|
+
catch (e) {
|
|
67
|
+
return textError(error(e));
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
server.registerTool("hn_comments", {
|
|
71
|
+
title: "HN comments",
|
|
72
|
+
description: "The comment tree under one Hacker News story, with each comment's author, indented by reply depth.",
|
|
73
|
+
inputSchema: z.object({ id: z.number().int().min(1), depth: z.number().int().min(1).max(6).default(2) }),
|
|
74
|
+
annotations: READ_ONLY,
|
|
75
|
+
}, async (args) => {
|
|
76
|
+
try {
|
|
77
|
+
return text(await hnComments(args));
|
|
78
|
+
}
|
|
79
|
+
catch (e) {
|
|
80
|
+
return textError(error(e));
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
server.registerTool("reddit_search", {
|
|
84
|
+
title: "Reddit search",
|
|
85
|
+
description: "Search one subreddit's posts by keyword over the last year via Reddit's Atom feed, with the poster and permalink.",
|
|
86
|
+
inputSchema: z.object({ subreddit: z.string().min(1).max(60), query: z.string().min(1).max(200), limit: z.number().int().min(1).max(25).default(10) }),
|
|
87
|
+
annotations: READ_ONLY,
|
|
88
|
+
}, async (args) => {
|
|
89
|
+
// ---- paywall: reddit_search ----
|
|
90
|
+
const paywallMessage = await premiumRequired("reddit_search");
|
|
91
|
+
if (paywallMessage)
|
|
92
|
+
return { content: [{ type: "text", text: paywallMessage }], isError: true };
|
|
93
|
+
// ---- paywall: end ----
|
|
94
|
+
try {
|
|
95
|
+
return text(await redditSearch(args));
|
|
96
|
+
}
|
|
97
|
+
catch (e) {
|
|
98
|
+
return textError(error(e));
|
|
99
|
+
}
|
|
100
|
+
});
|
|
51
101
|
return server;
|
|
52
102
|
}
|
package/package.json
CHANGED