yomeru-mcp 0.1.0 → 0.1.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 +1 -0
- package/dist/index.js +3 -1
- package/dist/resolve.js +39 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -49,6 +49,7 @@ All optional.
|
|
|
49
49
|
| `YOMERU_KINDLE_DIR` | A folder of Markdown notes with `kindle-sync:` front matter (as written by the Obsidian Kindle Highlights plugin). Used to say "you already own this". Read only; titles never leave your machine. |
|
|
50
50
|
| `YOMERU_JURISDICTION` | `JP` (default) or `US`. Decides when a Project Gutenberg book counts as free. |
|
|
51
51
|
| `YOMERU_CACHE_DIR` | Cache location. Default `~/.cache/yomeru-mcp`. |
|
|
52
|
+
| `YOMERU_DEADLINE_MS` | Time budget for one lookup, default `45000`. A source that has not answered by then is reported as `error` (unknown), never as not found, and keeps loading in the background so the next lookup is fast. |
|
|
52
53
|
|
|
53
54
|
Example with everything on:
|
|
54
55
|
|
package/dist/index.js
CHANGED
|
@@ -3,7 +3,7 @@ import { readFileSync } from "node:fs";
|
|
|
3
3
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
4
4
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
5
5
|
import { z } from "zod";
|
|
6
|
-
import { configFromEnv, findReadingOptions } from "./resolve.js";
|
|
6
|
+
import { configFromEnv, findReadingOptions, prefetchCatalogues } from "./resolve.js";
|
|
7
7
|
import { calilLibraries } from "./sources/calil.js";
|
|
8
8
|
const pkg = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
|
|
9
9
|
export function createServer() {
|
|
@@ -61,6 +61,8 @@ async function main() {
|
|
|
61
61
|
return;
|
|
62
62
|
}
|
|
63
63
|
await createServer().connect(new StdioServerTransport());
|
|
64
|
+
// Warm the catalogue caches so the first lookup rarely waits on a 6-20 MB download.
|
|
65
|
+
prefetchCatalogues();
|
|
64
66
|
}
|
|
65
67
|
main().catch((e) => {
|
|
66
68
|
console.error(e);
|
package/dist/resolve.js
CHANGED
|
@@ -7,18 +7,46 @@ import { GUTENBERG_ATTRIBUTION, loadGutenberg, matchGutenberg } from "./sources/
|
|
|
7
7
|
import { NDL_ATTRIBUTION, ndlByIsbn, ndlByTitle, ndlTranslations } from "./sources/ndl.js";
|
|
8
8
|
import { OPENLIBRARY_ATTRIBUTION, olByIsbn, olSearch } from "./sources/openlibrary.js";
|
|
9
9
|
import { loadOwned, matchOwned } from "./sources/owned.js";
|
|
10
|
+
export const DEFAULT_DEADLINE_MS = 45000;
|
|
11
|
+
/** Node timers overflow past 2^31-1 ms; ten minutes is already far beyond any MCP client's wait. */
|
|
12
|
+
export const MAX_DEADLINE_MS = 600000;
|
|
13
|
+
export function parseDeadline(raw) {
|
|
14
|
+
const n = Number(raw);
|
|
15
|
+
return Number.isFinite(n) && n > 0 ? Math.min(n, MAX_DEADLINE_MS) : undefined;
|
|
16
|
+
}
|
|
10
17
|
export function configFromEnv(env = process.env) {
|
|
11
18
|
return {
|
|
12
19
|
calilAppkey: env.CALIL_APPKEY || undefined,
|
|
13
20
|
librarySystemIds: (env.YOMERU_LIBRARIES ?? "").split(",").map((s) => s.trim()).filter(Boolean),
|
|
14
21
|
ownedDir: env.YOMERU_KINDLE_DIR || undefined,
|
|
15
22
|
jurisdiction: env.YOMERU_JURISDICTION === "US" ? "US" : "JP",
|
|
23
|
+
deadlineMs: parseDeadline(env.YOMERU_DEADLINE_MS),
|
|
16
24
|
};
|
|
17
25
|
}
|
|
18
26
|
const now = () => new Date().toISOString();
|
|
19
|
-
|
|
27
|
+
class DeadlineExceeded extends Error {
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Races a lookup against the request deadline. The lookup itself keeps running, so a
|
|
31
|
+
* slow catalogue download still lands in the cache and the next request is fast.
|
|
32
|
+
*/
|
|
33
|
+
function withDeadline(p, deadline, budgetMs) {
|
|
34
|
+
const left = deadline - Date.now();
|
|
35
|
+
const msg = `no answer within ${Math.round(budgetMs / 1000)}s; still loading in the background, ask again shortly`;
|
|
36
|
+
if (left <= 0) {
|
|
37
|
+
p.catch(() => undefined);
|
|
38
|
+
return Promise.reject(new DeadlineExceeded(msg));
|
|
39
|
+
}
|
|
40
|
+
let timer;
|
|
41
|
+
const late = new Promise((_, reject) => {
|
|
42
|
+
timer = setTimeout(() => reject(new DeadlineExceeded(msg)), left);
|
|
43
|
+
});
|
|
44
|
+
p.catch(() => undefined);
|
|
45
|
+
return Promise.race([p, late]).finally(() => clearTimeout(timer));
|
|
46
|
+
}
|
|
47
|
+
async function attemptRaw(source, fn, deadline = Infinity, budgetMs = 0) {
|
|
20
48
|
try {
|
|
21
|
-
return { ok: true, value: await fn() };
|
|
49
|
+
return { ok: true, value: await withDeadline(fn(), deadline, budgetMs) };
|
|
22
50
|
}
|
|
23
51
|
catch (e) {
|
|
24
52
|
const status = e instanceof RateLimited ? "rate_limited" : "error";
|
|
@@ -106,7 +134,16 @@ export function recommend(options, sources) {
|
|
|
106
134
|
reason += ` Not checked or unanswered: ${unknown.join(", ")}.`;
|
|
107
135
|
return { today, wait, reason };
|
|
108
136
|
}
|
|
137
|
+
/** Starts both catalogue downloads; they are cached for a week. Errors surface at lookup time. */
|
|
138
|
+
export function prefetchCatalogues() {
|
|
139
|
+
loadAozora().catch(() => undefined);
|
|
140
|
+
loadGutenberg().catch(() => undefined);
|
|
141
|
+
}
|
|
109
142
|
export async function findReadingOptions(q, cfg) {
|
|
143
|
+
const budgetMs = parseDeadline(String(cfg.deadlineMs)) ?? DEFAULT_DEADLINE_MS;
|
|
144
|
+
const deadline = Date.now() + budgetMs;
|
|
145
|
+
const attempt = (source, fn) => attemptRaw(source, fn, deadline, budgetMs);
|
|
146
|
+
prefetchCatalogues();
|
|
110
147
|
const ledger = new Ledger();
|
|
111
148
|
const notes = [];
|
|
112
149
|
const editions = [];
|
package/package.json
CHANGED