@xevos117/mcp-zotero 2.0.2 → 2.0.3
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 +50 -8
- package/build/citation-injector/citation-formatter.js +66 -7
- package/build/citation-injector/document-prefs.js +121 -0
- package/build/citation-injector/injector.js +35 -11
- package/build/server.js +7 -4
- package/build/tools/add-items-by-doi.js +14 -6
- package/build/tools/add-items.js +5 -2
- package/build/tools/add-linked-url-attachment.js +6 -2
- package/build/tools/create-collection.js +6 -2
- package/build/tools/delete-collection.js +7 -3
- package/build/tools/delete-items.js +7 -3
- package/build/tools/find-and-attach-pdfs.js +17 -9
- package/build/tools/get-collection-items.js +8 -7
- package/build/tools/get-collections.js +2 -2
- package/build/tools/get-item-fulltext.js +27 -12
- package/build/tools/get-items-details.js +7 -3
- package/build/tools/get-user-id.js +1 -1
- package/build/tools/import-pdf-to-zotero.js +41 -7
- package/build/tools/index.js +11 -3
- package/build/tools/inject-citations.js +33 -6
- package/build/tools/search-library.js +7 -5
- package/build/utils/csl-to-zotero.js +39 -14
- package/build/utils/doi-resolver.js +10 -2
- package/build/utils/error-hints.js +179 -0
- package/build/utils/html-entities.js +55 -0
- package/build/utils/library-context.js +11 -11
- package/build/utils/pdf-uploader.js +2 -0
- package/build/utils/unpaywall.js +24 -5
- package/build/utils/zotero-keys.js +2 -1
- package/package.json +5 -1
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { formatJsonResult, formatErrorResponse } from "../utils/error-formatter.js";
|
|
3
|
+
import { isAbsolute } from "node:path";
|
|
3
4
|
import { injectCitations } from "../citation-injector/injector.js";
|
|
4
5
|
import { resolveLibrary, libraryArgsSchema } from "../utils/library-context.js";
|
|
5
6
|
export const toolConfig = {
|
|
@@ -7,13 +8,14 @@ export const toolConfig = {
|
|
|
7
8
|
description: `Replace <zcite> placeholder tags in a .docx file with native Zotero field codes that Zotero for Word can recognize and manage. The tool fetches item metadata from Zotero automatically — you only need to provide the .docx file.
|
|
8
9
|
|
|
9
10
|
WORKFLOW — how to create a Word document with live Zotero citations:
|
|
10
|
-
1. Collect item keys: use add_items_by_doi (or search_library for existing items)
|
|
11
|
+
1. Collect item keys: use add_items_by_doi or add_items (or search_library for existing items)
|
|
11
12
|
2. Generate .docx: create a Word document (e.g. with the "docx" npm package) with <zcite keys="ITEMKEY"/> placeholders where citations should appear. Each <zcite> MUST be in its own dedicated TextRun — do NOT mix it with surrounding text.
|
|
12
13
|
3. Call this tool with the .docx file path. It replaces every zcite tag with a Zotero field code and appends a bibliography.
|
|
13
14
|
4. Tell the user to open the file in Word with the Zotero plugin, click Zotero → Add/Edit Bibliography, then Zotero → Refresh.
|
|
14
15
|
|
|
15
16
|
CITATION STYLES — ask the user which style they want before generating:
|
|
16
17
|
- apa (default): author-year — (Smith, 2023)
|
|
18
|
+
- harvard / chicago: author-date — (Smith, 2023) / (Smith 2023)
|
|
17
19
|
- ieee / vancouver: numbered — [1], [2]
|
|
18
20
|
WARNING: for numbered styles every <zcite> MUST include a num="N" attribute with the sequential citation number. Without num, citations render as [?].
|
|
19
21
|
|
|
@@ -22,14 +24,14 @@ ZCITE TAG FORMAT:
|
|
|
22
24
|
Supported attributes (any order):
|
|
23
25
|
- keys (required): item key or comma-separated keys — "ABC12345" or "ABC12345,DEF67890"
|
|
24
26
|
- num: citation number for IEEE/Vancouver — "1" or "1,2" (required for numbered styles)
|
|
25
|
-
- locator: page reference — "pp. 12
|
|
26
|
-
- prefix: text before citation — "see
|
|
27
|
+
- locator: page reference — "12-15" (shown as "pp. 12–15"; other labels explicit, e.g. "chap. 3")
|
|
28
|
+
- prefix: text before citation — "see"
|
|
27
29
|
- suffix: text after citation — ", emphasis added"
|
|
28
30
|
|
|
29
31
|
OUTPUT:
|
|
30
|
-
A new .docx file (original filename with _cited suffix) with Zotero field codes and a ZOTERO_BIBL bibliography at the end. A cited key that is not in the library, or that is not
|
|
32
|
+
A new .docx file (original filename with _cited suffix) with Zotero field codes and a ZOTERO_BIBL bibliography at the end. A cited key that is not in the library, or that is not a Zotero key (8 characters from 23456789ABCDEFGHIJKLMNPQRSTUVWXYZ), is an error and no file is written.
|
|
31
33
|
|
|
32
|
-
|
|
34
|
+
WHERE THE FILE LIVES: this tool reads and writes files on the machine where this MCP server runs. If you create the .docx in a sandbox or cloud workspace that the server cannot see (for example code execution in Claude.ai or Claude Desktop, paths like /mnt/user-data/...), do not call this tool: use the zotero-mcp-integrations skill, which injects the citations inside the sandbox with metadata from get_items_details. If that skill is not installed, recommend it to the user: zotero-skill-mcp-integrations.zip from https://github.com/Xevos117/mcp-zotero/releases/latest.`,
|
|
33
35
|
inputSchema: {
|
|
34
36
|
file_path: z
|
|
35
37
|
.string()
|
|
@@ -43,15 +45,40 @@ NOTE: If the inject-citations skill is available, prefer the skill workflow (run
|
|
|
43
45
|
},
|
|
44
46
|
};
|
|
45
47
|
const InjectCitationsSchema = z.object(toolConfig.inputSchema);
|
|
48
|
+
const FILE_NOT_FOUND = "File not found on the machine where this MCP server runs. If the document is in a sandbox or cloud workspace " +
|
|
49
|
+
"(for example code execution in Claude.ai or Claude Desktop), this server cannot read it: use the " +
|
|
50
|
+
"zotero-mcp-integrations skill, which injects the citations inside the sandbox. If the skill is not installed, " +
|
|
51
|
+
"ask the user to add zotero-skill-mcp-integrations.zip from https://github.com/Xevos117/mcp-zotero/releases/latest.";
|
|
46
52
|
export async function handleInjectCitations(zoteroApi, userId, args) {
|
|
47
53
|
const { file_path, style, library_type, library_id } = InjectCitationsSchema.parse(args);
|
|
48
54
|
const { type: libraryType, id: libraryId } = resolveLibrary({ library_type, library_id }, userId);
|
|
49
55
|
if (!file_path.endsWith(".docx")) {
|
|
50
56
|
return formatErrorResponse("File must be a .docx file", {
|
|
51
57
|
file_path,
|
|
58
|
+
hint: "Only Word .docx files are supported and the name must end in lowercase .docx: convert .doc, .odt or .rtf to .docx first, or create the document as .docx with <zcite> placeholders.",
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
let result;
|
|
62
|
+
try {
|
|
63
|
+
result = await injectCitations(file_path, zoteroApi, libraryType, libraryId, style);
|
|
64
|
+
}
|
|
65
|
+
catch (err) {
|
|
66
|
+
// A path from a sandbox or cloud workspace (Claude.ai, Claude Desktop code execution) does not exist here:
|
|
67
|
+
// say so and point to the skill instead of a bare ENOENT.
|
|
68
|
+
const code = err.code;
|
|
69
|
+
if (code === "EACCES" || code === "EPERM" || code === "EROFS") {
|
|
70
|
+
return formatErrorResponse(`Cannot read the file or write the _cited copy next to it (${code}).`, {
|
|
71
|
+
file_path,
|
|
72
|
+
hint: "Copy the .docx to a folder this server can write to (e.g. the user's Documents) and call again with that path.",
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
if (code !== "ENOENT")
|
|
76
|
+
throw err;
|
|
77
|
+
return formatErrorResponse(FILE_NOT_FOUND, {
|
|
78
|
+
file_path,
|
|
79
|
+
...(isAbsolute(file_path) ? {} : { hint: "Pass an absolute path." }),
|
|
52
80
|
});
|
|
53
81
|
}
|
|
54
|
-
const result = await injectCitations(file_path, zoteroApi, libraryType, libraryId, style);
|
|
55
82
|
const responseObj = {
|
|
56
83
|
output_path: result.outputPath,
|
|
57
84
|
citations_found: result.found,
|
|
@@ -6,8 +6,8 @@ export const toolConfig = {
|
|
|
6
6
|
name: "search_library",
|
|
7
7
|
description: `Search your Zotero library or list items sorted by a field.
|
|
8
8
|
|
|
9
|
-
When 'query' is provided,
|
|
10
|
-
When 'query' is omitted, lists items sorted by the chosen field (default: dateAdded, descending)
|
|
9
|
+
When 'query' is provided, matches it against titles, creator names and years (not abstracts, tags or full text).
|
|
10
|
+
When 'query' is omitted, lists items sorted by the chosen field (default: dateAdded, descending).
|
|
11
11
|
|
|
12
12
|
Examples:
|
|
13
13
|
- Search: { "query": "deep learning" }
|
|
@@ -32,9 +32,11 @@ Use the returned item keys with get_items_details, get_item_fulltext, or inject_
|
|
|
32
32
|
.describe("Sort direction (default: desc)"),
|
|
33
33
|
limit: z
|
|
34
34
|
.number()
|
|
35
|
+
.int()
|
|
36
|
+
.min(1)
|
|
35
37
|
.optional()
|
|
36
38
|
.default(25)
|
|
37
|
-
.describe("Maximum number of items to return (default: 25
|
|
39
|
+
.describe("Maximum number of items to return, 1-100 (default: 25; above 100 returns 100)"),
|
|
38
40
|
...libraryArgsSchema,
|
|
39
41
|
},
|
|
40
42
|
};
|
|
@@ -59,11 +61,11 @@ export async function handleSearchLibrary(zoteroApi, userId, args) {
|
|
|
59
61
|
if (query) {
|
|
60
62
|
return formatEmptyResult("No results found", {
|
|
61
63
|
query,
|
|
62
|
-
suggestion: "Try
|
|
64
|
+
suggestion: "Nothing in this library matches the query in title, creator or year. Try fewer or different words (one author surname or a title keyword), or omit query to list recent items. For a group library pass library_type and library_id.",
|
|
63
65
|
});
|
|
64
66
|
}
|
|
65
67
|
return formatEmptyResult("No items found", {
|
|
66
|
-
suggestion: "Add some items
|
|
68
|
+
suggestion: "This library has no items yet. Add some with add_items_by_doi (items with a DOI) or add_items.",
|
|
67
69
|
});
|
|
68
70
|
}
|
|
69
71
|
const formatted = items.map((item) => ({
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ITEM_TYPE_FIELDS } from "./zotero-item-types.js";
|
|
1
|
+
import { ITEM_TYPE_CREATOR_TYPES, ITEM_TYPE_FIELDS } from "./zotero-item-types.js";
|
|
2
2
|
const CSL_TO_ZOTERO_TYPE = {
|
|
3
3
|
// Standard CSL types
|
|
4
4
|
"article": "journalArticle",
|
|
@@ -112,13 +112,25 @@ const CONTAINER_TITLE_FIELD = {
|
|
|
112
112
|
tvBroadcast: "programTitle",
|
|
113
113
|
podcast: "programTitle",
|
|
114
114
|
};
|
|
115
|
+
/**
|
|
116
|
+
* CSL name variables ⇄ Zotero creator types, as in Zotero's CSL mappings. Authors first: the
|
|
117
|
+
* order of the CSL arrays becomes the order of the Zotero creators.
|
|
118
|
+
*/
|
|
119
|
+
const CSL_NAME_CREATOR_TYPES = [
|
|
120
|
+
["author", "author"],
|
|
121
|
+
["editor", "editor"],
|
|
122
|
+
["container-author", "bookAuthor"],
|
|
123
|
+
["collection-editor", "seriesEditor"],
|
|
124
|
+
["translator", "translator"],
|
|
125
|
+
];
|
|
115
126
|
export function cslToZoteroItem(csl, options) {
|
|
116
127
|
const itemType = (CSL_TO_ZOTERO_TYPE[csl.type] ?? "journalArticle");
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
128
|
+
// Authors always (as before); other roles only where the item type has them, or the API rejects the item.
|
|
129
|
+
// A CSL `literal` (institution, consortium) is Zotero's single-field name
|
|
130
|
+
const validCreatorTypes = ITEM_TYPE_CREATOR_TYPES[itemType];
|
|
131
|
+
const creators = CSL_NAME_CREATOR_TYPES.flatMap(([variable, creatorType]) => creatorType !== "author" && !validCreatorTypes?.has(creatorType)
|
|
132
|
+
? []
|
|
133
|
+
: (csl[variable] ?? []).map((a) => a.literal && !a.family ? { name: a.literal, creatorType } : { firstName: a.given ?? "", lastName: a.family ?? "", creatorType }));
|
|
122
134
|
const dateParts = csl.issued?.["date-parts"]?.[0];
|
|
123
135
|
const date = dateParts ? dateParts.join("-") : csl.issued?.literal ?? csl.issued?.raw ?? "";
|
|
124
136
|
const collections = [];
|
|
@@ -168,13 +180,26 @@ export function cslToZoteroItem(csl, options) {
|
|
|
168
180
|
}
|
|
169
181
|
return result;
|
|
170
182
|
}
|
|
183
|
+
/** Zotero creator → CSL name: a single-field name (institution) is a CSL `literal`. */
|
|
184
|
+
function cslName(c) {
|
|
185
|
+
if (c.name && !c.lastName)
|
|
186
|
+
return { literal: c.name };
|
|
187
|
+
return { family: c.lastName ?? "", given: c.firstName ?? "" };
|
|
188
|
+
}
|
|
189
|
+
/** CSL title of the container, from the item type's own field (bookTitle, proceedingsTitle, ...) or publicationTitle. */
|
|
190
|
+
function containerTitle(item) {
|
|
191
|
+
const field = CONTAINER_TITLE_FIELD[item.itemType];
|
|
192
|
+
const fields = item;
|
|
193
|
+
const value = (field && fields[field]) || item.publicationTitle;
|
|
194
|
+
return typeof value === "string" && value ? value : undefined;
|
|
195
|
+
}
|
|
171
196
|
export function zoteroItemToCsl(item) {
|
|
172
|
-
const
|
|
173
|
-
|
|
174
|
-
.
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
}
|
|
197
|
+
const names = {};
|
|
198
|
+
for (const [variable, creatorType] of CSL_NAME_CREATOR_TYPES) {
|
|
199
|
+
const list = (item.creators ?? []).filter((c) => c.creatorType === creatorType).map(cslName);
|
|
200
|
+
if (list.length > 0)
|
|
201
|
+
names[variable] = list;
|
|
202
|
+
}
|
|
178
203
|
let issued;
|
|
179
204
|
if (item.date) {
|
|
180
205
|
const parts = item.date.split("-").map(Number).filter((n) => !isNaN(n));
|
|
@@ -187,10 +212,10 @@ export function zoteroItemToCsl(item) {
|
|
|
187
212
|
return {
|
|
188
213
|
type: ZOTERO_TO_CSL_TYPE[item.itemType ?? ""] ?? "article-journal",
|
|
189
214
|
title: text(item.title),
|
|
190
|
-
|
|
215
|
+
...names,
|
|
191
216
|
issued,
|
|
192
217
|
DOI: text(item.DOI),
|
|
193
|
-
"container-title":
|
|
218
|
+
"container-title": containerTitle(item),
|
|
194
219
|
volume: text(item.volume),
|
|
195
220
|
issue: text(item.issue),
|
|
196
221
|
page: text(item.pages),
|
|
@@ -2,6 +2,7 @@ import { mapWithConcurrency } from "./concurrency.js";
|
|
|
2
2
|
import { fetchWithRetry } from "./fetch-retry.js";
|
|
3
3
|
import { errorMessage } from "./error-formatter.js";
|
|
4
4
|
import { InvalidJsonResponseError, readJsonObject } from "./json-response.js";
|
|
5
|
+
import { decodeEntitiesDeep } from "./html-entities.js";
|
|
5
6
|
export async function resolveDoi(doi) {
|
|
6
7
|
const response = await fetchWithRetry(`https://doi.org/${encodeURIComponent(doi)}`, {
|
|
7
8
|
headers: {
|
|
@@ -10,10 +11,17 @@ export async function resolveDoi(doi) {
|
|
|
10
11
|
redirect: "follow",
|
|
11
12
|
}, { label: "DOI resolution" });
|
|
12
13
|
if (!response.ok) {
|
|
13
|
-
|
|
14
|
+
const why = response.status === 404
|
|
15
|
+
? " (DOI not registered or mistyped)"
|
|
16
|
+
: response.status === 429 || response.status >= 500
|
|
17
|
+
? " (doi.org or the registry is busy or failing: retry later)"
|
|
18
|
+
: "";
|
|
19
|
+
const status = [response.status, response.statusText].filter(Boolean).join(" ");
|
|
20
|
+
throw new Error(`DOI resolution failed for ${doi}: ${status}${why}`);
|
|
14
21
|
}
|
|
15
22
|
try {
|
|
16
|
-
|
|
23
|
+
// the registries send some strings HTML-escaped ("Computers & Operations Research"): store the characters
|
|
24
|
+
return decodeEntitiesDeep(await readJsonObject(response));
|
|
17
25
|
}
|
|
18
26
|
catch (err) {
|
|
19
27
|
// doi.org answers some unresolvable DOIs with an HTML page and status 200
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { isZoteroApiError } from "../types/zotero-types.js";
|
|
2
|
+
import { errorMessage } from "./error-formatter.js";
|
|
3
|
+
import { RequestTimeoutError } from "./timeouts.js";
|
|
4
|
+
/**
|
|
5
|
+
* Wording shared by the tool errors: each one says what happened, what the model can do next and,
|
|
6
|
+
* when the fix is on the user's side, what to tell the user. No secrets: status codes and hosts only.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Server settings as the user sees them: the variable name, and the title of the same setting in the Claude Desktop
|
|
10
|
+
* extension (user_config of manifest.json), which has no env block.
|
|
11
|
+
*/
|
|
12
|
+
export const SETTINGS = {
|
|
13
|
+
apiKey: { env: "ZOTERO_API_KEY", title: "Zotero API key" },
|
|
14
|
+
userId: { env: "ZOTERO_USER_ID", title: "Zotero user ID" },
|
|
15
|
+
libraryId: { env: "ZOTERO_LIBRARY_ID", title: "Group library ID" },
|
|
16
|
+
unpaywallEmail: { env: "UNPAYWALL_EMAIL", title: "Email for Unpaywall" },
|
|
17
|
+
unsafeOperations: { env: "UNSAFE_OPERATIONS", title: "Deletions allowed" },
|
|
18
|
+
};
|
|
19
|
+
/** Where the user changes these settings, for every "tell the user" hint: extension or MCP config, then a restart. */
|
|
20
|
+
export function whereToSet(...settings) {
|
|
21
|
+
const titles = settings.map((s) => `"${s.title}"`).join(" / ");
|
|
22
|
+
const envs = settings.map((s) => s.env).join(" / ");
|
|
23
|
+
return (`the server's settings: with the Claude Desktop extension, ${titles} in Settings → Extensions → MCP Zotero; ` +
|
|
24
|
+
`in other clients, ${envs} in the env block of this server in the MCP config (e.g. claude_desktop_config.json or .mcp.json). ` +
|
|
25
|
+
"Then restart the client");
|
|
26
|
+
}
|
|
27
|
+
export const API_KEYS_PAGE = "https://www.zotero.org/settings/keys";
|
|
28
|
+
/** Hint for an argument that must be an existing item key. */
|
|
29
|
+
export const ITEM_KEY_HINT = "Get valid item keys from search_library or get_collection_items (for a group library pass the same library_type and library_id); never guess keys.";
|
|
30
|
+
/** Hint for an argument that must be an existing collection key. */
|
|
31
|
+
export const COLLECTION_KEY_HINT = "Get valid collection keys from get_collections (for a group library pass the same library_type and library_id); never guess keys.";
|
|
32
|
+
export const MISSING_API_KEY = "ZOTERO_API_KEY is not set in the server's environment";
|
|
33
|
+
export const MISSING_API_KEY_HINT = `Tell the user to create a Zotero API key at ${API_KEYS_PAGE} and set it in ${whereToSet(SETTINGS.apiKey)}.`;
|
|
34
|
+
const ACCESS_HINT = "Retrying or using other tools will not help. Tell the user to check the API key at " +
|
|
35
|
+
`${API_KEYS_PAGE}: it needs "Allow library access", "Allow write access" for tools that add or delete, ` +
|
|
36
|
+
"and access to the group for a group library; ZOTERO_USER_ID must be the userID shown on that page. " +
|
|
37
|
+
`They fix it by updating the key / user ID in ${whereToSet(SETTINGS.apiKey, SETTINGS.userId)}.`;
|
|
38
|
+
const STATUS_NAMES = {
|
|
39
|
+
400: "Bad Request",
|
|
40
|
+
401: "Unauthorized",
|
|
41
|
+
403: "Forbidden",
|
|
42
|
+
404: "Not Found",
|
|
43
|
+
409: "Conflict",
|
|
44
|
+
412: "Precondition Failed",
|
|
45
|
+
413: "Request Entity Too Large",
|
|
46
|
+
429: "Too Many Requests",
|
|
47
|
+
500: "Internal Server Error",
|
|
48
|
+
502: "Bad Gateway",
|
|
49
|
+
503: "Service Unavailable",
|
|
50
|
+
504: "Gateway Timeout",
|
|
51
|
+
};
|
|
52
|
+
function statusLabel(status) {
|
|
53
|
+
return STATUS_NAMES[status] ? `HTTP ${status} ${STATUS_NAMES[status]}` : `HTTP ${status}`;
|
|
54
|
+
}
|
|
55
|
+
function retryWait(retryAfterS) {
|
|
56
|
+
return retryAfterS !== undefined ? `${retryAfterS} s` : "about a minute";
|
|
57
|
+
}
|
|
58
|
+
/** What a Zotero Web API status means for the model: the error text and the next step. */
|
|
59
|
+
export function zoteroStatusHint(status, retryAfterS) {
|
|
60
|
+
const label = statusLabel(status);
|
|
61
|
+
if (status === 401 || status === 403) {
|
|
62
|
+
return {
|
|
63
|
+
error: `Zotero refused access (${label}): the API key is invalid or revoked, cannot access this library, or lacks write access for this operation.`,
|
|
64
|
+
hint: ACCESS_HINT,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
if (status === 404) {
|
|
68
|
+
return {
|
|
69
|
+
error: `Zotero returned ${label}: the library, item or collection does not exist or this API key cannot see it.`,
|
|
70
|
+
hint: `Do not retry with the same item or collection key. ${ITEM_KEY_HINT} Collection keys come from get_collections. If every call fails like this, the configured library id is wrong: tell the user to check the user ID (the userID at ${API_KEYS_PAGE}) or group library ID in ${whereToSet(SETTINGS.userId, SETTINGS.libraryId)}.`,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
if (status === 409 || status === 412) {
|
|
74
|
+
return {
|
|
75
|
+
error: `The Zotero library changed or was locked during the request (${label}).`,
|
|
76
|
+
hint: "Retry the same call once.",
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
if (status === 429) {
|
|
80
|
+
return {
|
|
81
|
+
error: `Zotero rate limit reached (${label}).`,
|
|
82
|
+
hint: `Wait ${retryWait(retryAfterS)} and retry once; avoid parallel calls and send fewer items per call.`,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
if (status >= 500) {
|
|
86
|
+
return {
|
|
87
|
+
error: `The Zotero API is temporarily unavailable (${label}).`,
|
|
88
|
+
hint: `Wait ${retryWait(retryAfterS)} and retry once. If it keeps failing, tell the user that Zotero's servers seem to have problems and to try again later.`,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
if (status === 400 || status === 413) {
|
|
92
|
+
return {
|
|
93
|
+
error: `Zotero rejected the request (${label}).`,
|
|
94
|
+
hint: "Retrying unchanged will fail again: check the arguments (and details, if present), fix them and retry; for large batches send fewer items per call.",
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
return { error: `Zotero API error (${label}).`, hint: "Retry once; if it fails again, tell the user what failed." };
|
|
98
|
+
}
|
|
99
|
+
const NETWORK_ERROR = /fetch failed|socket hang up|ENOTFOUND|EAI_AGAIN|ECONNREFUSED|ECONNRESET|ETIMEDOUT|EHOSTUNREACH|ENETUNREACH/;
|
|
100
|
+
/**
|
|
101
|
+
* A failed connection (DNS, refused, reset), as thrown by fetch: a TypeError, the cause carrying the code. Only these
|
|
102
|
+
* markers count: a bug's TypeError that merely mentions a network is not reported as a retryable network error.
|
|
103
|
+
*/
|
|
104
|
+
function networkDetail(err) {
|
|
105
|
+
if (!(err instanceof TypeError))
|
|
106
|
+
return undefined;
|
|
107
|
+
const cause = err.cause;
|
|
108
|
+
const causeMessage = cause instanceof Error ? cause.message : undefined;
|
|
109
|
+
const text = causeMessage ? `${err.message} (${causeMessage})` : err.message;
|
|
110
|
+
return NETWORK_ERROR.test(text) ? text : undefined;
|
|
111
|
+
}
|
|
112
|
+
function retryAfterSeconds(response) {
|
|
113
|
+
const raw = response.headers?.get?.("Retry-After");
|
|
114
|
+
const seconds = raw ? Number(raw) : NaN;
|
|
115
|
+
return Number.isFinite(seconds) && seconds >= 0 ? seconds : undefined;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Short plain-text reason from a Zotero error body ("Write access denied"), never an HTML page. Library paths and
|
|
119
|
+
* long digit runs (user and group ids) are masked: the account behind the server must not be identifiable.
|
|
120
|
+
*/
|
|
121
|
+
function apiReason(err) {
|
|
122
|
+
const reason = err.reason;
|
|
123
|
+
if (typeof reason !== "string")
|
|
124
|
+
return undefined;
|
|
125
|
+
const text = reason
|
|
126
|
+
.trim()
|
|
127
|
+
.replace(/\b(users|groups)\/\d+/g, "$1/<id>")
|
|
128
|
+
.replace(/\b\d{5,}\b/g, "<id>");
|
|
129
|
+
if (!text || /^</.test(text))
|
|
130
|
+
return undefined;
|
|
131
|
+
return text.length > 300 ? `${text.slice(0, 300)}…` : text;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Error text and next step for a failure a tool did not handle itself: Zotero API errors, timeouts and
|
|
135
|
+
* network errors. Undefined for anything else (a bug), which keeps its own message.
|
|
136
|
+
*/
|
|
137
|
+
export function describeError(err) {
|
|
138
|
+
if (isZoteroApiError(err)) {
|
|
139
|
+
const status = err.response.status;
|
|
140
|
+
const retry_after_s = retryAfterSeconds(err.response);
|
|
141
|
+
const { error, hint } = zoteroStatusHint(status, retry_after_s);
|
|
142
|
+
const details = apiReason(err);
|
|
143
|
+
return {
|
|
144
|
+
error,
|
|
145
|
+
hint,
|
|
146
|
+
http_status: status,
|
|
147
|
+
...(retry_after_s !== undefined ? { retry_after_s } : {}),
|
|
148
|
+
...(details ? { details } : {}),
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
if (err instanceof RequestTimeoutError) {
|
|
152
|
+
return {
|
|
153
|
+
error: err.message,
|
|
154
|
+
hint: "Retry once. A write that timed out may still have been saved: check with search_library before repeating it, to avoid duplicates. " +
|
|
155
|
+
"If it keeps timing out, tell the user to check the network connection of the machine running this server.",
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
const network = networkDetail(err);
|
|
159
|
+
if (network) {
|
|
160
|
+
return {
|
|
161
|
+
error: `Network error: ${network}`,
|
|
162
|
+
hint: "Retry once. If it keeps failing, tell the user that the machine running this server cannot reach the service (internet connection, DNS, proxy or firewall).",
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
return undefined;
|
|
166
|
+
}
|
|
167
|
+
/** describeError, falling back to the raw message (with no hint) for unexpected errors. */
|
|
168
|
+
export function describeAnyError(err) {
|
|
169
|
+
return describeError(err) ?? { error: errorMessage(err) };
|
|
170
|
+
}
|
|
171
|
+
/** Hint for a deletion blocked by UNSAFE_OPERATIONS: tell the user how to enable it, never work around it. */
|
|
172
|
+
export function unsafeOperationsHint(target) {
|
|
173
|
+
const values = target === "items"
|
|
174
|
+
? "'items' (allows delete_items) or 'all' (also delete_collection)"
|
|
175
|
+
: "'all' (allows delete_collection and delete_items; 'items' allows only delete_items)";
|
|
176
|
+
return ("Do not try to get around this with other tools (moving to the trash, editing, removing from collections). " +
|
|
177
|
+
`Tell the user that deleting is disabled in the server configuration and that, if they want to allow it, they can set ${values} ` +
|
|
178
|
+
`in ${whereToSet(SETTINGS.unsafeOperations)}, and then ask again.`);
|
|
179
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTML entities in registry metadata. doi.org / Crossref return some strings HTML-escaped
|
|
3
|
+
* ("Computers & Operations Research"); Zotero stores what it is given, so the entity ends up in the library
|
|
4
|
+
* and in every bibliography. Decoding only turns entities into the characters they stand for: the text stays
|
|
5
|
+
* plain text (no HTML is parsed or built), and every output that needs escaping (the .docx XML, JSON field codes)
|
|
6
|
+
* escapes it again where it is written.
|
|
7
|
+
*/
|
|
8
|
+
const NAMED = {
|
|
9
|
+
amp: "&",
|
|
10
|
+
lt: "<",
|
|
11
|
+
gt: ">",
|
|
12
|
+
quot: '"',
|
|
13
|
+
apos: "'",
|
|
14
|
+
nbsp: " ",
|
|
15
|
+
ndash: "–",
|
|
16
|
+
mdash: "—",
|
|
17
|
+
lsquo: "‘",
|
|
18
|
+
rsquo: "’",
|
|
19
|
+
ldquo: "“",
|
|
20
|
+
rdquo: "”",
|
|
21
|
+
hellip: "…",
|
|
22
|
+
};
|
|
23
|
+
const ENTITY = /&(?:#(\d{1,7})|#[xX]([0-9a-fA-F]{1,6})|([a-zA-Z]+));/g;
|
|
24
|
+
function fromCodePoint(cp) {
|
|
25
|
+
// NUL, surrogates and out-of-range values are not characters: keep the entity as it is
|
|
26
|
+
if (cp === 0 || cp > 0x10ffff || (cp >= 0xd800 && cp <= 0xdfff))
|
|
27
|
+
return undefined;
|
|
28
|
+
return String.fromCodePoint(cp);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Decodes HTML entities in ONE pass: the output is never scanned again, so "&lt;" becomes the text "<",
|
|
32
|
+
* not "<". Unknown or invalid entities are left unchanged.
|
|
33
|
+
*/
|
|
34
|
+
export function decodeHtmlEntities(text) {
|
|
35
|
+
if (!text.includes("&"))
|
|
36
|
+
return text;
|
|
37
|
+
return text.replace(ENTITY, (whole, dec, hex, name) => {
|
|
38
|
+
if (dec !== undefined)
|
|
39
|
+
return fromCodePoint(Number.parseInt(dec, 10)) ?? whole;
|
|
40
|
+
if (hex !== undefined)
|
|
41
|
+
return fromCodePoint(Number.parseInt(hex, 16)) ?? whole;
|
|
42
|
+
return NAMED[name.toLowerCase()] ?? whole;
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
/** decodeHtmlEntities on every string inside a JSON value (keys untouched); other values are returned as they are. */
|
|
46
|
+
export function decodeEntitiesDeep(value) {
|
|
47
|
+
if (typeof value === "string")
|
|
48
|
+
return decodeHtmlEntities(value);
|
|
49
|
+
if (Array.isArray(value))
|
|
50
|
+
return value.map((v) => decodeEntitiesDeep(v));
|
|
51
|
+
if (value !== null && typeof value === "object") {
|
|
52
|
+
return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, decodeEntitiesDeep(v)]));
|
|
53
|
+
}
|
|
54
|
+
return value;
|
|
55
|
+
}
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
* the personal library (ZOTERO_USER_ID); a group needs an explicit id.
|
|
19
19
|
*/
|
|
20
20
|
import { z } from "zod";
|
|
21
|
+
import { SETTINGS, whereToSet } from "./error-hints.js";
|
|
21
22
|
/** Zotero user and group IDs are numeric; also keeps ids safe to interpolate into API URLs. */
|
|
22
23
|
const LIBRARY_ID_PATTERN = /^\d+$/;
|
|
23
24
|
export function getLibraryType() {
|
|
@@ -48,7 +49,7 @@ export function getDefaultLibrary() {
|
|
|
48
49
|
}
|
|
49
50
|
const id = getLibraryId();
|
|
50
51
|
if (!id) {
|
|
51
|
-
throw new Error("Missing ZOTERO_USER_ID or ZOTERO_LIBRARY_ID environment variable");
|
|
52
|
+
throw new Error("Missing ZOTERO_USER_ID or ZOTERO_LIBRARY_ID environment variable (the userID is shown at https://www.zotero.org/settings/keys)");
|
|
52
53
|
}
|
|
53
54
|
assertLibraryId(id, process.env.ZOTERO_LIBRARY_ID ? "ZOTERO_LIBRARY_ID" : "ZOTERO_USER_ID");
|
|
54
55
|
return { type, id };
|
|
@@ -58,16 +59,13 @@ export const libraryArgsSchema = {
|
|
|
58
59
|
library_type: z
|
|
59
60
|
.enum(["user", "group"])
|
|
60
61
|
.optional()
|
|
61
|
-
.describe("
|
|
62
|
-
"Defaults to ZOTERO_LIBRARY_TYPE env (or 'user'). " +
|
|
63
|
-
"Use to target a different library per call without restarting the server."),
|
|
62
|
+
.describe("Library for this call: 'user' (personal) or 'group'. Default: the library configured on the server."),
|
|
64
63
|
library_id: z
|
|
65
64
|
.string()
|
|
66
|
-
.regex(LIBRARY_ID_PATTERN, "library_id must be a numeric Zotero
|
|
65
|
+
.regex(LIBRARY_ID_PATTERN, "library_id must be a numeric Zotero user or group ID (a group's ID is the number in its zotero.org/groups/<id> URL)")
|
|
67
66
|
.optional()
|
|
68
|
-
.describe("
|
|
69
|
-
"
|
|
70
|
-
"Required when library_type is 'group' and the server is configured for a user library."),
|
|
67
|
+
.describe("Numeric user or group ID for this call (a group's ID is the number in its zotero.org/groups/<id> URL). " +
|
|
68
|
+
"Default: the configured library. Required with library_type 'group' when the server is configured for a user library."),
|
|
71
69
|
};
|
|
72
70
|
/**
|
|
73
71
|
* Resolve the library context for a single tool call. Per-call args win,
|
|
@@ -87,12 +85,14 @@ export function resolveLibrary(args, defaultLibraryId) {
|
|
|
87
85
|
}
|
|
88
86
|
else {
|
|
89
87
|
// The default id belongs to a user library: never reuse it as a group id
|
|
90
|
-
throw new Error("library_id is required when library_type is 'group' and the server is configured for a user library
|
|
88
|
+
throw new Error("library_id is required when library_type is 'group' and the server is configured for a user library: " +
|
|
89
|
+
"pass the numeric group ID (the number in the group's zotero.org/groups/<id> URL).");
|
|
91
90
|
}
|
|
92
91
|
}
|
|
93
92
|
if (!id) {
|
|
94
|
-
throw new Error("Zotero library ID is not set
|
|
95
|
-
"
|
|
93
|
+
throw new Error("Zotero library ID is not set: the server has no ZOTERO_USER_ID for the personal library. Pass library_id " +
|
|
94
|
+
"(the numeric user ID), or tell the user to set their userID (shown at https://www.zotero.org/settings/keys) in " +
|
|
95
|
+
`${whereToSet(SETTINGS.userId)}.`);
|
|
96
96
|
}
|
|
97
97
|
assertLibraryId(id, "library_id");
|
|
98
98
|
return { type, id };
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { InvalidJsonResponseError, readJsonObject } from "./json-response.js";
|
|
2
2
|
import { createHash } from "node:crypto";
|
|
3
|
+
import { isZoteroApiError } from "../types/zotero-types.js";
|
|
3
4
|
import { logger } from "./logger.js";
|
|
4
5
|
import { extractPdfText } from "./pdf-text-extractor.js";
|
|
5
6
|
import { putFulltext } from "./zotero-fulltext.js";
|
|
@@ -161,6 +162,7 @@ export async function downloadAndUploadPdf(zoteroApi, libraryType, userId, apiKe
|
|
|
161
162
|
error: {
|
|
162
163
|
code: "item_creation_failed",
|
|
163
164
|
message: `Failed to create attachment item: ${detail}`,
|
|
165
|
+
...(isZoteroApiError(err) ? { status: err.response.status } : {}),
|
|
164
166
|
networkDetail: detail,
|
|
165
167
|
},
|
|
166
168
|
};
|
package/build/utils/unpaywall.js
CHANGED
|
@@ -2,15 +2,30 @@ import { InvalidJsonResponseError, readJsonObject } from "./json-response.js";
|
|
|
2
2
|
import { fetchWithRetry } from "./fetch-retry.js";
|
|
3
3
|
import { logger } from "./logger.js";
|
|
4
4
|
import { errorMessage } from "./error-formatter.js";
|
|
5
|
+
import { SETTINGS, whereToSet } from "./error-hints.js";
|
|
6
|
+
/** Per-item reason when the lookup is disabled; tools add UNPAYWALL_EMAIL_HINT once at top level. */
|
|
7
|
+
export const UNPAYWALL_EMAIL_MISSING = "UNPAYWALL_EMAIL is not set or invalid in the server configuration: open-access PDF lookup skipped.";
|
|
8
|
+
export const UNPAYWALL_EMAIL_HINT = "No PDF lookup was done (the items themselves are fine). Tell the user that open-access PDF lookup needs UNPAYWALL_EMAIL " +
|
|
9
|
+
`(a valid email address of theirs, which Unpaywall requires with each request) in ${whereToSet(SETTINGS.unpaywallEmail)}, and then run find_and_attach_pdfs.`;
|
|
5
10
|
const SKIPPED_RESULT = {
|
|
6
11
|
found: false,
|
|
7
12
|
pdf_url: null,
|
|
8
13
|
source: null,
|
|
9
14
|
license: null,
|
|
10
15
|
oa_status: null,
|
|
11
|
-
warning:
|
|
16
|
+
warning: UNPAYWALL_EMAIL_MISSING,
|
|
12
17
|
};
|
|
13
|
-
|
|
18
|
+
/** Unpaywall unreachable or failing: not the same as "no open access copy", so say so. */
|
|
19
|
+
function unavailableResult(what) {
|
|
20
|
+
return {
|
|
21
|
+
found: false,
|
|
22
|
+
pdf_url: null,
|
|
23
|
+
source: null,
|
|
24
|
+
license: null,
|
|
25
|
+
oa_status: null,
|
|
26
|
+
warning: `Unpaywall lookup failed (${what}): this does not mean there is no open access PDF. Retry later with find_and_attach_pdfs.`,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
14
29
|
function isValidEmail(email) {
|
|
15
30
|
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
|
|
16
31
|
}
|
|
@@ -34,11 +49,15 @@ export async function lookupOaPdfWithFallbacks(doi) {
|
|
|
34
49
|
catch (err) {
|
|
35
50
|
const detail = errorMessage(err);
|
|
36
51
|
logger.error("Unpaywall API network error", { doi, error: detail });
|
|
37
|
-
return { primary:
|
|
52
|
+
return { primary: unavailableResult(detail), fallback_urls: [] };
|
|
38
53
|
}
|
|
39
54
|
if (!response.ok) {
|
|
40
55
|
logger.error("Unpaywall API error", { doi, status: response.status });
|
|
41
|
-
|
|
56
|
+
if (response.status === 404) {
|
|
57
|
+
// Unpaywall does not know the DOI (not a Crossref DOI, e.g. DataCite): nothing to look up
|
|
58
|
+
return { primary: { found: false, pdf_url: null, source: null, license: null, oa_status: null }, fallback_urls: [] };
|
|
59
|
+
}
|
|
60
|
+
return { primary: unavailableResult(`HTTP ${response.status}`), fallback_urls: [] };
|
|
42
61
|
}
|
|
43
62
|
let data;
|
|
44
63
|
try {
|
|
@@ -48,7 +67,7 @@ export async function lookupOaPdfWithFallbacks(doi) {
|
|
|
48
67
|
if (!(err instanceof InvalidJsonResponseError))
|
|
49
68
|
throw err;
|
|
50
69
|
logger.error("Unpaywall API returned an invalid response", { doi, error: err.message });
|
|
51
|
-
return { primary:
|
|
70
|
+
return { primary: unavailableResult(err.message), fallback_urls: [] };
|
|
52
71
|
}
|
|
53
72
|
const locations = data.is_oa
|
|
54
73
|
? [data.best_oa_location, ...(data.oa_locations ?? [])].filter((l) => l !== null && l !== undefined)
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
/** Zotero object keys (items, collections): 8 characters from this alphabet, never 0, 1, O or lowercase. */
|
|
3
3
|
export const ZOTERO_KEY = /^[23456789ABCDEFGHIJKLMNPQRSTUVWXYZ]{8}$/;
|
|
4
|
-
export const INVALID_KEY_MESSAGE = "Invalid Zotero key: expected 8 characters from 23456789ABCDEFGHIJKLMNPQRSTUVWXYZ"
|
|
4
|
+
export const INVALID_KEY_MESSAGE = "Invalid Zotero key: expected 8 characters from 23456789ABCDEFGHIJKLMNPQRSTUVWXYZ (e.g. ABCD2345). " +
|
|
5
|
+
"Use a key returned by search_library, get_collection_items, get_collections or the add_* tools; never guess keys";
|
|
5
6
|
/**
|
|
6
7
|
* Schema for every key argument of every tool. Keys end up in API URL paths (zotero-api-client does not
|
|
7
8
|
* encode path segments, and URL parsing resolves "..") and in itemKey filters (ignored by the API when
|
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xevos117/mcp-zotero",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.0.
|
|
4
|
+
"version": "2.0.3",
|
|
5
|
+
"mcpName": "io.github.Xevos117/mcp-zotero",
|
|
5
6
|
"description": "Unofficial MCP server for Zotero integration with citation management",
|
|
6
7
|
"repository": {
|
|
7
8
|
"type": "git",
|
|
@@ -26,6 +27,7 @@
|
|
|
26
27
|
"test:watch": "vitest",
|
|
27
28
|
"build": "tsc",
|
|
28
29
|
"smoke": "node scripts/smoke-stdio.mjs",
|
|
30
|
+
"bundle": "node scripts/build-mcpb.mjs",
|
|
29
31
|
"start": "node build/server.js",
|
|
30
32
|
"prepublishOnly": "npm test && npm run build",
|
|
31
33
|
"test:e2e": "npm run build && vitest run --config vitest.e2e.config.ts",
|
|
@@ -56,8 +58,10 @@
|
|
|
56
58
|
},
|
|
57
59
|
"devDependencies": {
|
|
58
60
|
"@anthropic-ai/claude-agent-sdk": "^0.3.296",
|
|
61
|
+
"@anthropic-ai/mcpb": "2.1.2",
|
|
59
62
|
"@types/node": "^26.6.4",
|
|
60
63
|
"ajv": "^8.20.0",
|
|
64
|
+
"ajv-formats": "^3.0.1",
|
|
61
65
|
"citeproc": "^2.4.63",
|
|
62
66
|
"typescript": "^7.0.2",
|
|
63
67
|
"vitest": "^5.0.3"
|