@ontos-ai/knowhere-mcp 0.2.0
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/LICENSE +21 -0
- package/README.md +202 -0
- package/dist/chunk-ZITSXYWR.mjs +319 -0
- package/dist/index.d.mts +17 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +350 -0
- package/dist/index.mjs +8 -0
- package/dist/stdio.d.mts +1 -0
- package/dist/stdio.d.ts +1 -0
- package/dist/stdio.js +943 -0
- package/dist/stdio.mjs +612 -0
- package/package.json +69 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
createKnowhereMcpServer: () => createKnowhereMcpServer,
|
|
34
|
+
runKnowhereMcpServer: () => runKnowhereMcpServer
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(index_exports);
|
|
37
|
+
var import_mcp = require("@modelcontextprotocol/sdk/server/mcp.js");
|
|
38
|
+
var import_stdio = require("@modelcontextprotocol/sdk/server/stdio.js");
|
|
39
|
+
var import_knowhere_sdk = require("@ontos-ai/knowhere-sdk");
|
|
40
|
+
var z = __toESM(require("zod/v4"));
|
|
41
|
+
var parsingParamsSchema = z.object({
|
|
42
|
+
model: z.enum(["base", "advanced"]).optional(),
|
|
43
|
+
ocrEnabled: z.boolean().optional(),
|
|
44
|
+
kbDir: z.string().optional(),
|
|
45
|
+
docType: z.enum(["auto", "pdf", "docx", "txt", "md"]).optional(),
|
|
46
|
+
smartTitleParse: z.boolean().optional(),
|
|
47
|
+
summaryImage: z.boolean().optional(),
|
|
48
|
+
summaryTable: z.boolean().optional(),
|
|
49
|
+
summaryTxt: z.boolean().optional(),
|
|
50
|
+
addFragDesc: z.string().optional()
|
|
51
|
+
}).optional();
|
|
52
|
+
var objectOutputSchema = {
|
|
53
|
+
result: z.record(z.string(), z.unknown())
|
|
54
|
+
};
|
|
55
|
+
async function createKnowhereMcpServer(options) {
|
|
56
|
+
const client = options?.client ?? new import_knowhere_sdk.Knowhere({
|
|
57
|
+
authTokenProvider: options?.authTokenProvider,
|
|
58
|
+
baseURL: options?.baseURL
|
|
59
|
+
});
|
|
60
|
+
const knowledge = options?.cacheDirectory === void 0 ? client.knowledge : client.knowledge.withCacheDirectory(options.cacheDirectory);
|
|
61
|
+
if (options?.recoverPendingJobsOnStart !== false) {
|
|
62
|
+
await knowledge.recoverPendingAsyncParseJobs();
|
|
63
|
+
}
|
|
64
|
+
const server = new import_mcp.McpServer({
|
|
65
|
+
name: "knowhere-local-knowledge",
|
|
66
|
+
version: import_knowhere_sdk.VERSION
|
|
67
|
+
});
|
|
68
|
+
const permission = options?.permission ?? "full_access";
|
|
69
|
+
const hasWritePermission = permission === "full_access";
|
|
70
|
+
if (hasWritePermission) {
|
|
71
|
+
server.registerTool(
|
|
72
|
+
"knowhere_parse_url",
|
|
73
|
+
{
|
|
74
|
+
description: "Blocking parse: submit a remote URL to Knowhere, wait for completion, then cache the parse result locally for outline/read/grep/search tools.",
|
|
75
|
+
inputSchema: {
|
|
76
|
+
url: z.string().url(),
|
|
77
|
+
namespace: z.string().optional(),
|
|
78
|
+
localDocumentId: z.string().optional(),
|
|
79
|
+
dataId: z.string().optional(),
|
|
80
|
+
parsingParams: parsingParamsSchema
|
|
81
|
+
},
|
|
82
|
+
outputSchema: objectOutputSchema
|
|
83
|
+
},
|
|
84
|
+
async (input) => createToolResult(
|
|
85
|
+
await knowledge.parse({
|
|
86
|
+
url: input.url,
|
|
87
|
+
namespace: input.namespace,
|
|
88
|
+
localDocumentId: input.localDocumentId,
|
|
89
|
+
dataId: input.dataId,
|
|
90
|
+
...toFlatParsingParams(input.parsingParams)
|
|
91
|
+
})
|
|
92
|
+
)
|
|
93
|
+
);
|
|
94
|
+
server.registerTool(
|
|
95
|
+
"knowhere_parse_file",
|
|
96
|
+
{
|
|
97
|
+
description: "Blocking parse: submit a local file path available to this MCP process, wait for completion, then cache the parse result locally.",
|
|
98
|
+
inputSchema: {
|
|
99
|
+
file: z.string().describe("Local file path available to this MCP server process."),
|
|
100
|
+
fileName: z.string().optional(),
|
|
101
|
+
namespace: z.string().optional(),
|
|
102
|
+
localDocumentId: z.string().optional(),
|
|
103
|
+
dataId: z.string().optional(),
|
|
104
|
+
parsingParams: parsingParamsSchema
|
|
105
|
+
},
|
|
106
|
+
outputSchema: objectOutputSchema
|
|
107
|
+
},
|
|
108
|
+
async (input) => createToolResult(
|
|
109
|
+
await knowledge.parse({
|
|
110
|
+
file: input.file,
|
|
111
|
+
fileName: input.fileName,
|
|
112
|
+
namespace: input.namespace,
|
|
113
|
+
localDocumentId: input.localDocumentId,
|
|
114
|
+
dataId: input.dataId,
|
|
115
|
+
...toFlatParsingParams(input.parsingParams)
|
|
116
|
+
})
|
|
117
|
+
)
|
|
118
|
+
);
|
|
119
|
+
server.registerTool(
|
|
120
|
+
"knowhere_async_parse_url",
|
|
121
|
+
{
|
|
122
|
+
description: "Start parsing a remote URL through Knowhere and return immediately with the parse job. Poll with knowhere_async_get_job_status; completed tracked jobs are cached locally automatically.",
|
|
123
|
+
inputSchema: {
|
|
124
|
+
url: z.string().url(),
|
|
125
|
+
namespace: z.string().optional(),
|
|
126
|
+
localDocumentId: z.string().optional(),
|
|
127
|
+
dataId: z.string().optional(),
|
|
128
|
+
parsingParams: parsingParamsSchema
|
|
129
|
+
},
|
|
130
|
+
outputSchema: objectOutputSchema
|
|
131
|
+
},
|
|
132
|
+
async (input) => createToolResult(
|
|
133
|
+
await knowledge.startParse({
|
|
134
|
+
url: input.url,
|
|
135
|
+
namespace: input.namespace,
|
|
136
|
+
localDocumentId: input.localDocumentId,
|
|
137
|
+
dataId: input.dataId,
|
|
138
|
+
...toFlatParsingParams(input.parsingParams)
|
|
139
|
+
})
|
|
140
|
+
)
|
|
141
|
+
);
|
|
142
|
+
server.registerTool(
|
|
143
|
+
"knowhere_async_parse_file",
|
|
144
|
+
{
|
|
145
|
+
description: "Start parsing a local file path available to this MCP process, upload it if needed, and return immediately with the parse job. Poll with knowhere_async_get_job_status; completed tracked jobs are cached locally automatically.",
|
|
146
|
+
inputSchema: {
|
|
147
|
+
file: z.string().describe("Local file path available to this MCP server process."),
|
|
148
|
+
fileName: z.string().optional(),
|
|
149
|
+
namespace: z.string().optional(),
|
|
150
|
+
localDocumentId: z.string().optional(),
|
|
151
|
+
dataId: z.string().optional(),
|
|
152
|
+
parsingParams: parsingParamsSchema
|
|
153
|
+
},
|
|
154
|
+
outputSchema: objectOutputSchema
|
|
155
|
+
},
|
|
156
|
+
async (input) => createToolResult(
|
|
157
|
+
await knowledge.startParse({
|
|
158
|
+
file: input.file,
|
|
159
|
+
fileName: input.fileName,
|
|
160
|
+
namespace: input.namespace,
|
|
161
|
+
localDocumentId: input.localDocumentId,
|
|
162
|
+
dataId: input.dataId,
|
|
163
|
+
...toFlatParsingParams(input.parsingParams)
|
|
164
|
+
})
|
|
165
|
+
)
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
server.registerTool(
|
|
169
|
+
"knowhere_async_get_job_status",
|
|
170
|
+
{
|
|
171
|
+
description: "Fetch the current status for a Knowhere parse job. If the job was started by an async parse tool and is done, this also caches the result locally for outline/read/grep/search.",
|
|
172
|
+
inputSchema: {
|
|
173
|
+
jobId: z.string()
|
|
174
|
+
},
|
|
175
|
+
outputSchema: objectOutputSchema
|
|
176
|
+
},
|
|
177
|
+
async (input) => createToolResult(await knowledge.getJobStatus(input.jobId))
|
|
178
|
+
);
|
|
179
|
+
server.registerTool(
|
|
180
|
+
"knowhere_async_cache_job_result",
|
|
181
|
+
{
|
|
182
|
+
description: "Manually load a completed Knowhere parse job result and cache it locally. Usually not needed for jobs started by async parse tools because knowhere_async_get_job_status auto-caches them when done.",
|
|
183
|
+
inputSchema: {
|
|
184
|
+
jobId: z.string(),
|
|
185
|
+
localDocumentId: z.string().optional(),
|
|
186
|
+
verifyChecksum: z.boolean().optional()
|
|
187
|
+
},
|
|
188
|
+
outputSchema: objectOutputSchema
|
|
189
|
+
},
|
|
190
|
+
async (input) => createToolResult(
|
|
191
|
+
await knowledge.cacheJobResult({
|
|
192
|
+
jobId: input.jobId,
|
|
193
|
+
localDocumentId: input.localDocumentId,
|
|
194
|
+
verifyChecksum: input.verifyChecksum
|
|
195
|
+
})
|
|
196
|
+
)
|
|
197
|
+
);
|
|
198
|
+
server.registerTool(
|
|
199
|
+
"knowhere_list_documents",
|
|
200
|
+
{
|
|
201
|
+
description: "List parse results cached locally by this SDK-backed MCP server.",
|
|
202
|
+
inputSchema: {},
|
|
203
|
+
outputSchema: objectOutputSchema
|
|
204
|
+
},
|
|
205
|
+
async () => createToolResult({ documents: await knowledge.listDocuments() })
|
|
206
|
+
);
|
|
207
|
+
if (hasWritePermission) {
|
|
208
|
+
server.registerTool(
|
|
209
|
+
"knowhere_delete_document",
|
|
210
|
+
{
|
|
211
|
+
description: "Archive, or soft-delete, a published Knowhere document through the Knowhere API. Provide documentId directly, or localDocumentId for a cached parse result that has a server documentId.",
|
|
212
|
+
inputSchema: {
|
|
213
|
+
documentId: z.string().optional(),
|
|
214
|
+
localDocumentId: z.string().optional()
|
|
215
|
+
},
|
|
216
|
+
outputSchema: objectOutputSchema
|
|
217
|
+
},
|
|
218
|
+
async (input) => createToolResult(await archiveDocument({ client, knowledge, params: input }))
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
server.registerTool(
|
|
222
|
+
"knowhere_get_document_outline",
|
|
223
|
+
{
|
|
224
|
+
description: "Return the local outline for a cached parsed document.",
|
|
225
|
+
inputSchema: {
|
|
226
|
+
localDocumentId: z.string()
|
|
227
|
+
},
|
|
228
|
+
outputSchema: objectOutputSchema
|
|
229
|
+
},
|
|
230
|
+
async (input) => createToolResult(await knowledge.getDocumentOutline(input.localDocumentId))
|
|
231
|
+
);
|
|
232
|
+
server.registerTool(
|
|
233
|
+
"knowhere_read_chunks",
|
|
234
|
+
{
|
|
235
|
+
description: "Read exact chunks from a cached local parse result.",
|
|
236
|
+
inputSchema: {
|
|
237
|
+
localDocumentId: z.string(),
|
|
238
|
+
sectionPath: z.string().optional(),
|
|
239
|
+
startChunk: z.number().int().positive().optional(),
|
|
240
|
+
endChunk: z.number().int().positive().optional(),
|
|
241
|
+
chunkId: z.string().optional(),
|
|
242
|
+
chunkType: z.enum(["text", "image", "table"]).optional(),
|
|
243
|
+
limit: z.number().int().positive().optional()
|
|
244
|
+
},
|
|
245
|
+
outputSchema: objectOutputSchema
|
|
246
|
+
},
|
|
247
|
+
async (input) => createToolResult(await knowledge.readChunks(input))
|
|
248
|
+
);
|
|
249
|
+
server.registerTool(
|
|
250
|
+
"knowhere_grep_chunks",
|
|
251
|
+
{
|
|
252
|
+
description: "Run grep-style literal or regex matching against cached local chunks.",
|
|
253
|
+
inputSchema: {
|
|
254
|
+
localDocumentId: z.string(),
|
|
255
|
+
pattern: z.string(),
|
|
256
|
+
isRegex: z.boolean().optional(),
|
|
257
|
+
isCaseSensitive: z.boolean().optional(),
|
|
258
|
+
maxResults: z.number().int().positive().optional(),
|
|
259
|
+
chunkType: z.enum(["text", "image", "table"]).optional(),
|
|
260
|
+
sectionPathPrefix: z.string().optional(),
|
|
261
|
+
contextChars: z.number().int().nonnegative().optional()
|
|
262
|
+
},
|
|
263
|
+
outputSchema: objectOutputSchema
|
|
264
|
+
},
|
|
265
|
+
async (input) => createToolResult(await knowledge.grepChunks(input))
|
|
266
|
+
);
|
|
267
|
+
server.registerTool(
|
|
268
|
+
"knowhere_search",
|
|
269
|
+
{
|
|
270
|
+
description: "Search published Knowhere documents with the Knowhere API retrieval query. localDocumentIds only map returned server document IDs back to local cache IDs when available.",
|
|
271
|
+
inputSchema: {
|
|
272
|
+
query: z.string(),
|
|
273
|
+
namespace: z.string().optional(),
|
|
274
|
+
topK: z.number().int().positive().optional(),
|
|
275
|
+
localDocumentIds: z.array(z.string()).optional(),
|
|
276
|
+
useAgentic: z.boolean().optional()
|
|
277
|
+
},
|
|
278
|
+
outputSchema: objectOutputSchema
|
|
279
|
+
},
|
|
280
|
+
async (input) => createToolResult(await knowledge.search(input))
|
|
281
|
+
);
|
|
282
|
+
return server;
|
|
283
|
+
}
|
|
284
|
+
async function runKnowhereMcpServer(options) {
|
|
285
|
+
const server = await createKnowhereMcpServer(options);
|
|
286
|
+
const transport = new import_stdio.StdioServerTransport();
|
|
287
|
+
await server.connect(transport);
|
|
288
|
+
}
|
|
289
|
+
function createToolResult(result) {
|
|
290
|
+
const structuredContent = { result };
|
|
291
|
+
return {
|
|
292
|
+
content: [{ type: "text", text: JSON.stringify(structuredContent, null, 2) }],
|
|
293
|
+
structuredContent
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
function toFlatParsingParams(parsingParams) {
|
|
297
|
+
if (!parsingParams) {
|
|
298
|
+
return {};
|
|
299
|
+
}
|
|
300
|
+
return {
|
|
301
|
+
model: parsingParams.model,
|
|
302
|
+
ocr: parsingParams.ocrEnabled,
|
|
303
|
+
docType: parsingParams.docType,
|
|
304
|
+
smartTitleParse: parsingParams.smartTitleParse,
|
|
305
|
+
summaryImage: parsingParams.summaryImage,
|
|
306
|
+
summaryTable: parsingParams.summaryTable,
|
|
307
|
+
summaryTxt: parsingParams.summaryTxt,
|
|
308
|
+
addFragDesc: parsingParams.addFragDesc,
|
|
309
|
+
kbDir: parsingParams.kbDir
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
async function archiveDocument(params) {
|
|
313
|
+
const archiveTarget = await resolveArchiveTarget(params.knowledge, params.params);
|
|
314
|
+
const document = await params.client.documents.archive(archiveTarget.documentId);
|
|
315
|
+
return {
|
|
316
|
+
document,
|
|
317
|
+
localDocumentId: archiveTarget.localDocumentId
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
async function resolveArchiveTarget(knowledge, params) {
|
|
321
|
+
if (params.documentId) {
|
|
322
|
+
return {
|
|
323
|
+
documentId: params.documentId,
|
|
324
|
+
localDocumentId: params.localDocumentId
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
if (!params.localDocumentId) {
|
|
328
|
+
throw new import_knowhere_sdk.ValidationError("documentId or localDocumentId is required");
|
|
329
|
+
}
|
|
330
|
+
const document = await findLocalDocument(knowledge, params.localDocumentId);
|
|
331
|
+
if (!document) {
|
|
332
|
+
throw new Error(`Local Knowhere document not found: ${params.localDocumentId}`);
|
|
333
|
+
}
|
|
334
|
+
if (!document.documentId) {
|
|
335
|
+
throw new Error(`Local Knowhere document has no server documentId: ${params.localDocumentId}`);
|
|
336
|
+
}
|
|
337
|
+
return {
|
|
338
|
+
documentId: document.documentId,
|
|
339
|
+
localDocumentId: document.localDocumentId
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
async function findLocalDocument(knowledge, localDocumentId) {
|
|
343
|
+
const documents = await knowledge.listDocuments();
|
|
344
|
+
return documents.find((document) => document.localDocumentId === localDocumentId);
|
|
345
|
+
}
|
|
346
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
347
|
+
0 && (module.exports = {
|
|
348
|
+
createKnowhereMcpServer,
|
|
349
|
+
runKnowhereMcpServer
|
|
350
|
+
});
|
package/dist/index.mjs
ADDED
package/dist/stdio.d.mts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
package/dist/stdio.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#!/usr/bin/env node
|