@calypsohq/elephant-mcp-server 1.3.0 → 1.3.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 CHANGED
@@ -74,6 +74,10 @@ src/
74
74
  AddFileTool, AddFilesTool, AddWebsiteTool, CreateBucketTool (add)
75
75
  RenameBucketTool, MoveSourcesTool (organise)
76
76
  ToolContext.ts, ToolLogger.ts, schemaParts.ts
77
+ views/ what a tool answers: one view per kind of record (SourceView, BucketView,
78
+ BatchView, WebsiteView, SearchView, MoveView), each naming the fields it
79
+ returns. No API record is handed to a client as it is. ErrorView
80
+ clears a failure of ids and index paths
77
81
  presets/ ToolPreset + ToolPresetRegistry: each endpoint's path, scope and tools;
78
82
  ToolGroup (read, add, organise) and PresetInstructions, which decide
79
83
  what one caller is offered and told
@@ -89,6 +93,8 @@ Each request builds a fresh `ElephantMcpServer` bound to the caller's key, so to
89
93
 
90
94
  Uploads take a ChatGPT attachment (`file`, declared in `_meta["openai/fileParams"]`), a public `sourceUrl`, or `contentBase64`. An attachment's temporary download link is handed to Calypso to import, so the bytes never pass through this server. Nothing reads from the server's disk.
91
95
 
96
+ Tools answer with views, not API records. The Calypso API's records also carry what only Calypso needs: which key added a file, index store and operation names, task and request ids, the workspace id. A view names each field it returns, so none of that reaches a client, including fields the API adds later. Error messages keep their code and advice and lose the same things; the server's own log keeps them whole.
97
+
92
98
  Each caller (grant or API key) gets `ELEPHANT_REQUESTS_PER_MINUTE` requests per minute across both endpoints (default 120); over that, `429` with `Retry-After`.
93
99
 
94
100
  ## Commands
@@ -140,8 +140,7 @@ function statusOf(result) {
140
140
  }
141
141
  function describeIndexingTimeout(result) {
142
142
  const fileId = result.file.id ? ` file_id=${result.file.id}` : "";
143
- const taskId = result.task?.id ? ` task_id=${result.task.id}` : "";
144
143
  const status = statusOf(result);
145
144
  const statusText = status ? ` Last status: ${status}.` : "";
146
- return `Upload accepted but indexing did not start or finish before timeout.${statusText} It may still finish; check the file again later.${fileId}${taskId}`;
145
+ return `Upload accepted but indexing did not start or finish before timeout.${statusText} It may still finish; check the file again later.${fileId}`;
147
146
  }
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * Shapes of the Calypso knowledge API's answers.
3
3
  *
4
- * Every type is open (`[key: string]: unknown`): tools pass these through to
5
- * the client as JSON, so a field the API adds later reaches the model without
6
- * a change here.
4
+ * Every type is open (`[key: string]: unknown`) because the API adds fields
5
+ * over time. None of these is ever handed to a client as it is: each tool
6
+ * answers with a view (`src/views`) that names the fields it returns.
7
7
  */
8
8
  export {};
@@ -1,6 +1,7 @@
1
1
  import { z } from "zod";
2
2
  import { countBucketTargets } from "../calypso/BucketDestination.js";
3
3
  import { ToolGroup } from "../presets/ToolGroup.js";
4
+ import { SourceView } from "../views/SourceView.js";
4
5
  import { ElephantTool, } from "./ElephantTool.js";
5
6
  import { bucketDestinationInputs, chatGptFileInput, idempotencyKeyInput, } from "./schemaParts.js";
6
7
  const inputSchema = {
@@ -76,7 +77,7 @@ export class AddFileTool extends ElephantTool {
76
77
  fileId: result.file.id,
77
78
  status: result.file.status || result.task?.status || null,
78
79
  });
79
- return { json: result };
80
+ return { json: SourceView.added(result) };
80
81
  }
81
82
  /**
82
83
  * A ChatGPT attachment arrives as a temporary download link. It is handed
@@ -3,6 +3,7 @@ import { z } from "zod";
3
3
  import { BATCH_MAX_FILES } from "../calypso/BatchManifest.js";
4
4
  import { countBucketTargets } from "../calypso/BucketDestination.js";
5
5
  import { ToolGroup } from "../presets/ToolGroup.js";
6
+ import { BatchView } from "../views/BatchView.js";
6
7
  import { ElephantTool, } from "./ElephantTool.js";
7
8
  import { bucketDestinationInputs } from "./schemaParts.js";
8
9
  const itemSchema = z.object({
@@ -75,9 +76,9 @@ export class AddFilesTool extends ElephantTool {
75
76
  const batch = await api.batches.upload({ ...args, batchIdempotencyKey });
76
77
  await log.event("info", "Batch accepted.", {
77
78
  tool: this.name,
78
- batchId: batch.batch_id ?? null,
79
+ batchId: batch.id ?? null,
79
80
  status: batch.status ?? null,
80
81
  });
81
- return { json: batch };
82
+ return { json: BatchView.of(batch) };
82
83
  }
83
84
  }
@@ -1,6 +1,7 @@
1
1
  import { z } from "zod";
2
2
  import { countBucketTargets } from "../calypso/BucketDestination.js";
3
3
  import { ToolGroup } from "../presets/ToolGroup.js";
4
+ import { WebsiteView } from "../views/WebsiteView.js";
4
5
  import { ElephantTool, } from "./ElephantTool.js";
5
6
  import { bucketDestinationInputs, idempotencyKeyInput } from "./schemaParts.js";
6
7
  const inputSchema = {
@@ -53,6 +54,6 @@ export class AddWebsiteTool extends ElephantTool {
53
54
  knowledgeId: website.knowledge_id ?? null,
54
55
  status: website.ingestion_status ?? null,
55
56
  });
56
- return { json: website };
57
+ return { json: WebsiteView.of(website) };
57
58
  }
58
59
  }
@@ -1,5 +1,6 @@
1
1
  import { z } from "zod";
2
2
  import { ToolGroup } from "../presets/ToolGroup.js";
3
+ import { BucketView } from "../views/BucketView.js";
3
4
  import { ElephantTool, } from "./ElephantTool.js";
4
5
  import { idempotencyKeyInput } from "./schemaParts.js";
5
6
  const inputSchema = {
@@ -46,15 +47,16 @@ export class CreateBucketTool extends ElephantTool {
46
47
  tool: this.name,
47
48
  bucketId: typeof bucket.id === "string" ? bucket.id : null,
48
49
  });
50
+ const view = BucketView.of(bucket);
49
51
  return {
50
- json: bucket,
51
- structured: CreateBucketTool.declaredFields(bucket),
52
+ json: view,
53
+ structured: CreateBucketTool.declaredFields(view),
52
54
  };
53
55
  }
54
56
  /**
55
- * Structured output is checked strictly against `outputSchema`, but the
56
- * API adds fields over time. Only the declared fields go in the structured
57
- * part; the full payload stays in the text.
57
+ * Structured output is checked strictly against `outputSchema`: only the
58
+ * declared fields go in the structured part. The text carries the bucket's
59
+ * view, which also has its description.
58
60
  */
59
61
  static declaredFields(bucket) {
60
62
  return Object.fromEntries(Object.keys(outputSchema)
@@ -1,3 +1,4 @@
1
+ import { ErrorView } from "../views/ErrorView.js";
1
2
  /**
2
3
  * One Elephant tool: its public name, wording and inputs, and what it does.
3
4
  *
@@ -18,11 +19,9 @@ export class ElephantTool {
18
19
  return ElephantTool.result(await this.run(args, context));
19
20
  }
20
21
  catch (error) {
21
- const message = error instanceof Error ? error.message : String(error);
22
- await context.log.event("error", `${this.name} failed.`, {
23
- tool: this.name,
24
- error: message,
25
- });
22
+ const detail = error instanceof Error ? error.message : String(error);
23
+ const message = ErrorView.of(detail);
24
+ await context.log.event("error", `${this.name} failed.`, { tool: this.name, error: message }, { detail });
26
25
  return {
27
26
  isError: true,
28
27
  content: [{ type: "text", text: `Error: ${this.failure} ${message}` }],
@@ -1,5 +1,6 @@
1
1
  import { z } from "zod";
2
2
  import { ToolGroup } from "../presets/ToolGroup.js";
3
+ import { BucketView } from "../views/BucketView.js";
3
4
  import { ElephantTool, } from "./ElephantTool.js";
4
5
  const inputSchema = {
5
6
  includeArchived: z
@@ -35,13 +36,13 @@ export class ListBucketsTool extends ElephantTool {
35
36
  includeArchived: args.includeArchived,
36
37
  });
37
38
  const buckets = args.includeSources === false
38
- ? list.buckets
39
+ ? list.buckets.map((bucket) => BucketView.listed(bucket))
39
40
  : await Promise.all(list.buckets.map((bucket) => ListBucketsTool.named(api, bucket)));
40
41
  await log.event("info", "Listed knowledge buckets.", {
41
42
  tool: this.name,
42
43
  bucketCount: list.buckets.length,
43
44
  });
44
- return { json: { ...list, buckets } };
45
+ return { json: { buckets } };
45
46
  }
46
47
  /**
47
48
  * The bucket with its sources named. A bucket whose sources cannot be read
@@ -50,14 +51,17 @@ export class ListBucketsTool extends ElephantTool {
50
51
  */
51
52
  static async named(api, bucket) {
52
53
  if (bucket.knowledgeIds.length === 0 && bucket.fileIds.length === 0) {
53
- return { ...bucket, sources: [] };
54
+ return BucketView.listed(bucket, { sources: [] });
54
55
  }
55
56
  try {
56
57
  const page = await api.bucketSources.firstPage(bucket.id);
57
- return { ...bucket, sources: page.sources, moreSources: page.hasMore };
58
+ return BucketView.listed(bucket, {
59
+ sources: page.sources,
60
+ moreSources: page.hasMore,
61
+ });
58
62
  }
59
63
  catch {
60
- return bucket;
64
+ return BucketView.listed(bucket);
61
65
  }
62
66
  }
63
67
  }
@@ -1,5 +1,6 @@
1
1
  import { z } from "zod";
2
2
  import { ToolGroup } from "../presets/ToolGroup.js";
3
+ import { MoveView } from "../views/MoveView.js";
3
4
  import { ElephantTool, } from "./ElephantTool.js";
4
5
  /** The API moves at most this many sources in one request. */
5
6
  const MAX_SOURCES = 100;
@@ -53,6 +54,6 @@ export class MoveSourcesTool extends ElephantTool {
53
54
  requested: result.requested,
54
55
  moved: result.moved,
55
56
  });
56
- return { json: result };
57
+ return { json: MoveView.of(result) };
57
58
  }
58
59
  }
@@ -1,5 +1,6 @@
1
1
  import { z } from "zod";
2
2
  import { ToolGroup } from "../presets/ToolGroup.js";
3
+ import { SourceView } from "../views/SourceView.js";
3
4
  import { ElephantTool, } from "./ElephantTool.js";
4
5
  const inputSchema = {
5
6
  fileId: z
@@ -16,7 +17,7 @@ export class OpenSourceTool extends ElephantTool {
16
17
  name = "elephant-open-source";
17
18
  title = "Open a source";
18
19
  description = [
19
- "Returns one source's details by file id: name, type, size and whether it is ready to answer from.",
20
+ "Returns one source's details by file id: name, type, size, the buckets it is in and whether it is ready to answer from.",
20
21
  "",
21
22
  "Use it when the user asks about a specific source cited in an answer or listed by elephant-list-buckets.",
22
23
  ].join("\n");
@@ -30,6 +31,7 @@ export class OpenSourceTool extends ElephantTool {
30
31
  };
31
32
  failure = "Opening the source failed.";
32
33
  async run(args, { api }) {
33
- return { json: await api.files.get(args.fileId, { verify: args.verify }) };
34
+ const file = await api.files.get(args.fileId, { verify: args.verify });
35
+ return { json: SourceView.of(file) };
34
36
  }
35
37
  }
@@ -1,5 +1,6 @@
1
1
  import { z } from "zod";
2
2
  import { ToolGroup } from "../presets/ToolGroup.js";
3
+ import { BucketView } from "../views/BucketView.js";
3
4
  import { ElephantTool, } from "./ElephantTool.js";
4
5
  const inputSchema = {
5
6
  bucket: z
@@ -33,7 +34,8 @@ export class RenameBucketTool extends ElephantTool {
33
34
  group = ToolGroup.organise;
34
35
  annotations = {
35
36
  readOnlyHint: false,
36
- destructiveHint: false,
37
+ // A rename overwrites the bucket's name and description.
38
+ destructiveHint: true,
37
39
  idempotentHint: true,
38
40
  openWorldHint: false,
39
41
  };
@@ -47,6 +49,6 @@ export class RenameBucketTool extends ElephantTool {
47
49
  tool: this.name,
48
50
  bucketId: typeof bucket.id === "string" ? bucket.id : null,
49
51
  });
50
- return { json: bucket };
52
+ return { json: BucketView.of(bucket) };
51
53
  }
52
54
  }
@@ -4,6 +4,7 @@ import { SEARCH_DEPTHS } from "../calypso/AnswerDepth.js";
4
4
  import { MAX_EXTRA_QUERIES } from "../calypso/KnowledgeSearch.js";
5
5
  import { emptyKnowledgeAnswer } from "../hatching/HatchingCopy.js";
6
6
  import { ToolGroup } from "../presets/ToolGroup.js";
7
+ import { SearchView } from "../views/SearchView.js";
7
8
  import { ElephantTool, } from "./ElephantTool.js";
8
9
  const inputSchema = {
9
10
  query: z.string().min(1).describe("What to look for."),
@@ -83,6 +84,6 @@ export class SearchTool extends ElephantTool {
83
84
  strategy: result.strategy,
84
85
  resultCount: result.results.length,
85
86
  });
86
- return { json: result };
87
+ return { json: SearchView.of(result) };
87
88
  }
88
89
  }
@@ -1,5 +1,6 @@
1
1
  /**
2
- * Structured logs for one tool call, sent to the MCP client and to stdout.
2
+ * Structured logs for one tool call: sent to the MCP client, and errors to
3
+ * the server's own log with the caller's principal.
3
4
  *
4
5
  * Callers log identifiers, counts and statuses only. Prompts, file bytes and
5
6
  * API keys never reach a log line.
@@ -11,15 +12,25 @@ export class ToolLogger {
11
12
  this.server = server;
12
13
  this.principal = principal;
13
14
  }
14
- async event(level, message, data = {}) {
15
- const entry = { message, principal: this.principal, ...data };
16
- if (level === "error")
17
- console.error(JSON.stringify({ level, ...entry }));
15
+ async event(level, message, data = {},
16
+ /** Kept in the server's log only: what a client is not shown. */
17
+ serverOnly = {}) {
18
+ if (level === "error") {
19
+ console.error(JSON.stringify({
20
+ level,
21
+ message,
22
+ principal: this.principal,
23
+ ...data,
24
+ ...serverOnly,
25
+ }));
26
+ }
18
27
  try {
28
+ // The client is told what happened, not who the caller is to Calypso:
29
+ // the principal (a grant or key reference) stays in the server's log.
19
30
  await this.server?.server.sendLoggingMessage({
20
31
  level,
21
32
  logger: "elephant",
22
- data: entry,
33
+ data: { message, ...data },
23
34
  });
24
35
  }
25
36
  catch {
@@ -0,0 +1,47 @@
1
+ import { asRecord, numberOrUndefined, plainText, present, trimmedString, } from "./viewFields.js";
2
+ /**
3
+ * How a batch upload went, file by file. The API's answer also carries the
4
+ * upload sessions, task ids and index sync records; a view carries none.
5
+ */
6
+ export class BatchView {
7
+ batch;
8
+ constructor(batch) {
9
+ this.batch = batch;
10
+ }
11
+ static of(value) {
12
+ return new BatchView(asRecord(value) ?? {}).fields();
13
+ }
14
+ fields() {
15
+ const batch = this.batch;
16
+ return present({
17
+ id: trimmedString(batch.id) ?? trimmedString(batch.batch_id),
18
+ status: trimmedString(batch.status),
19
+ message: plainText(batch.message),
20
+ total: numberOrUndefined(batch.total),
21
+ accepted: numberOrUndefined(batch.accepted),
22
+ rejected: numberOrUndefined(batch.rejected),
23
+ queued: numberOrUndefined(batch.queued),
24
+ indexing: numberOrUndefined(batch.indexing),
25
+ active: numberOrUndefined(batch.active),
26
+ failed: numberOrUndefined(batch.failed),
27
+ files: (Array.isArray(batch.items) ? batch.items : []).map(BatchView.file),
28
+ });
29
+ }
30
+ static file(value) {
31
+ const item = asRecord(value) ?? {};
32
+ const file = asRecord(item.file);
33
+ const error = asRecord(item.error);
34
+ return present({
35
+ client_file_id: trimmedString(item.client_file_id) ?? trimmedString(item.clientFileId),
36
+ file_id: trimmedString(item.knowledgeId) ??
37
+ trimmedString(item.knowledge_id) ??
38
+ trimmedString(file?.id),
39
+ filename: trimmedString(item.filename) ?? trimmedString(file?.filename),
40
+ status: trimmedString(item.canonicalStatus) ?? trimmedString(item.status),
41
+ ready: typeof file?.ready === "boolean" ? file.ready : undefined,
42
+ error: error
43
+ ? (plainText(error.message) ?? plainText(error.code))
44
+ : plainText(item.error),
45
+ });
46
+ }
47
+ }
@@ -0,0 +1,35 @@
1
+ import { asRecord, numberOrUndefined, present, trimmedString, } from "./viewFields.js";
2
+ /**
3
+ * A bucket's name, slug and note. The API's record also names the workspace,
4
+ * who made the bucket and the index store behind it; a view carries none.
5
+ */
6
+ export class BucketView {
7
+ bucket;
8
+ constructor(bucket) {
9
+ this.bucket = bucket;
10
+ }
11
+ static of(value) {
12
+ return new BucketView(asRecord(value) ?? {}).fields();
13
+ }
14
+ fields() {
15
+ const bucket = this.bucket;
16
+ return present({
17
+ id: trimmedString(bucket.id) ?? "",
18
+ name: trimmedString(bucket.name),
19
+ slug: trimmedString(bucket.slug),
20
+ description: trimmedString(bucket.description),
21
+ status: trimmedString(bucket.status),
22
+ });
23
+ }
24
+ static listed(bucket, named) {
25
+ const ids = [...new Set([...bucket.knowledgeIds, ...bucket.fileIds])];
26
+ return present({
27
+ ...BucketView.of(bucket),
28
+ file_count: numberOrUndefined(bucket.filesCount),
29
+ source_count: numberOrUndefined(bucket.memberCount),
30
+ sources: named?.sources,
31
+ moreSources: named?.moreSources,
32
+ source_ids: named || ids.length === 0 ? undefined : ids,
33
+ });
34
+ }
35
+ }
@@ -0,0 +1,43 @@
1
+ /**
2
+ * A failure, as the client may read it.
3
+ *
4
+ * An error message is written for whoever has to act on it: a refusal code,
5
+ * what was wrong with the input, what to try next. The API's errors can also
6
+ * carry what only Calypso needs to trace a call, so a message is cleared of
7
+ * request, task, key and workspace ids and of index paths before it leaves.
8
+ * The server's own log keeps the message as it was.
9
+ */
10
+ const INTERNAL_KEYS = [
11
+ "request_id",
12
+ "task_id",
13
+ "api_key_id",
14
+ "team_id",
15
+ "trace_id",
16
+ "operation_name",
17
+ "document_name",
18
+ "store_name",
19
+ ].join("|");
20
+ /** `"task_id": "…"` inside the JSON details of an API error. */
21
+ const JSON_PAIR = new RegExp(`,?\\s*"(?:${INTERNAL_KEYS})"\\s*:\\s*(?:"[^"]*"|[^,}\\]]+)`, "g");
22
+ /** `task_id=…` in a sentence. */
23
+ const KEY_VALUE = new RegExp(`\\s*\\b(?:${INTERNAL_KEYS})=\\S+`, "g");
24
+ const INDEX_PATH = /fileSearchStores\/[\w\-/]+/g;
25
+ export class ErrorView {
26
+ message;
27
+ constructor(message) {
28
+ this.message = message;
29
+ }
30
+ static of(message) {
31
+ return new ErrorView(message).text();
32
+ }
33
+ text() {
34
+ return (this.message
35
+ .replace(JSON_PAIR, "")
36
+ .replace(KEY_VALUE, "")
37
+ // A pair removed from the front of an object leaves its comma behind.
38
+ .replace(/([{[])\s*,\s*/g, "$1")
39
+ .replace(/\s*details=\{\s*\}/g, "")
40
+ .replace(INDEX_PATH, "the index")
41
+ .trim());
42
+ }
43
+ }
@@ -0,0 +1,27 @@
1
+ import { asRecord, numberOrUndefined, plainText, present, trimmedString, } from "./viewFields.js";
2
+ export class MoveView {
3
+ move;
4
+ constructor(move) {
5
+ this.move = move;
6
+ }
7
+ static of(value) {
8
+ return new MoveView(asRecord(value) ?? {}).fields();
9
+ }
10
+ fields() {
11
+ const move = this.move;
12
+ return present({
13
+ bucket_id: trimmedString(move.bucket_id),
14
+ from_bucket_id: trimmedString(move.from_bucket_id),
15
+ moved: numberOrUndefined(move.moved),
16
+ requested: numberOrUndefined(move.requested),
17
+ sources: (Array.isArray(move.sources) ? move.sources : []).map((entry) => {
18
+ const source = asRecord(entry) ?? {};
19
+ return present({
20
+ id: trimmedString(source.id),
21
+ status: trimmedString(source.status),
22
+ detail: plainText(source.detail),
23
+ });
24
+ }),
25
+ });
26
+ }
27
+ }
@@ -0,0 +1,52 @@
1
+ import { asRecord, numberOrUndefined, present, trimmedString, } from "./viewFields.js";
2
+ /** A search depth as the API names it, to the name the tool takes. */
3
+ const DEPTH_NAMES = {
4
+ fast: "fast",
5
+ standard: "thorough",
6
+ };
7
+ /**
8
+ * The passages a search found and where each is from. The API's answer also
9
+ * says which agent and retrieval strategy ran, what it cost in tokens, and
10
+ * for each passage its index and storage references; a view carries none.
11
+ */
12
+ export class SearchView {
13
+ search;
14
+ constructor(search) {
15
+ this.search = search;
16
+ }
17
+ static of(value) {
18
+ return new SearchView(asRecord(value) ?? {}).fields();
19
+ }
20
+ fields() {
21
+ const search = this.search;
22
+ return present({
23
+ query: trimmedString(search.query),
24
+ depth: DEPTH_NAMES[trimmedString(search.depth) ?? ""],
25
+ results: (Array.isArray(search.results) ? search.results : []).map(SearchView.result),
26
+ });
27
+ }
28
+ static result(value) {
29
+ const result = asRecord(value) ?? {};
30
+ const attributes = asRecord(result.attributes) ?? {};
31
+ return present({
32
+ source_index: numberOrUndefined(result.source_index),
33
+ type: trimmedString(result.type),
34
+ title: trimmedString(result.title),
35
+ text: trimmedString(result.text),
36
+ quote: trimmedString(result.quote),
37
+ // The knowledge id is the one the other tools take.
38
+ file_id: trimmedString(attributes.knowledge_id) ??
39
+ SearchView.plainId(result.file_id),
40
+ filename: trimmedString(result.filename),
41
+ url: trimmedString(result.url),
42
+ page: numberOrUndefined(attributes.page_number),
43
+ location: trimmedString(attributes.locator_label) ??
44
+ trimmedString(attributes.page_label),
45
+ });
46
+ }
47
+ /** An id, never a path into the index. */
48
+ static plainId(value) {
49
+ const id = trimmedString(value);
50
+ return id && !id.includes("/") ? id : undefined;
51
+ }
52
+ }
@@ -0,0 +1,63 @@
1
+ import { asRecord, bucketIds, numberOrUndefined, plainText, present, trimmedString, } from "./viewFields.js";
2
+ const READY_STATUSES = new Set(["active", "indexed"]);
3
+ /**
4
+ * A file's details and readiness (`GET /knowledge/files/{id}` and what an
5
+ * upload answers). The API's record also says which key added the file and
6
+ * where it sits in the index; a view carries neither.
7
+ */
8
+ export class SourceView {
9
+ file;
10
+ constructor(file) {
11
+ this.file = file;
12
+ }
13
+ static of(value) {
14
+ return new SourceView(asRecord(value) ?? {}).fields();
15
+ }
16
+ fields() {
17
+ const file = this.file;
18
+ const status = trimmedString(file.status);
19
+ return present({
20
+ id: trimmedString(file.id) ?? "",
21
+ title: trimmedString(file.title),
22
+ filename: trimmedString(file.filename),
23
+ content_type: trimmedString(file.content_type),
24
+ size_bytes: numberOrUndefined(file.size_bytes),
25
+ status,
26
+ ready: typeof file.ready === "boolean"
27
+ ? file.ready
28
+ : READY_STATUSES.has(status ?? ""),
29
+ bucket_ids: bucketIds(asRecord(file.bucket_assignment)?.assigned),
30
+ created_at: trimmedString(file.created_at),
31
+ updated_at: trimmedString(file.updated_at),
32
+ failure: SourceView.failure(file.failure_details),
33
+ index_check: SourceView.indexCheck(file.gemini),
34
+ });
35
+ }
36
+ /** What `elephant-add-file` answers: the file, and its indexing status. */
37
+ static added(value) {
38
+ return present({
39
+ file: SourceView.of(value.file),
40
+ indexing_status: trimmedString(asRecord(value.task)?.status),
41
+ });
42
+ }
43
+ static failure(value) {
44
+ const details = asRecord(value);
45
+ if (!details)
46
+ return undefined;
47
+ return (plainText(details.message) ??
48
+ plainText(details.reason) ??
49
+ plainText(details.code));
50
+ }
51
+ /** Whether the index holds the source in every bucket it is filed in. */
52
+ static indexCheck(value) {
53
+ const check = asRecord(value);
54
+ if (!check)
55
+ return undefined;
56
+ const documents = (Array.isArray(check.documents) ? check.documents : []).map((document) => asRecord(document));
57
+ return present({
58
+ in_index: documents.length > 0 &&
59
+ documents.every((document) => document?.exists === true),
60
+ checked_at: trimmedString(check.verifiedAt),
61
+ });
62
+ }
63
+ }
@@ -0,0 +1,29 @@
1
+ import { asRecord, bucketIds, plainText, present, trimmedString, } from "./viewFields.js";
2
+ /** What saving a web page answers, without the workspace or request ids. */
3
+ export class WebsiteView {
4
+ website;
5
+ constructor(website) {
6
+ this.website = website;
7
+ }
8
+ static of(value) {
9
+ return new WebsiteView(asRecord(value) ?? {}).fields();
10
+ }
11
+ fields() {
12
+ const website = this.website;
13
+ const assignment = asRecord(website.bucket_assignment);
14
+ const tags = (Array.isArray(website.tags) ? website.tags : [])
15
+ .map((tag) => trimmedString(tag))
16
+ .filter((tag) => Boolean(tag));
17
+ return present({
18
+ id: trimmedString(website.knowledge_id) ?? trimmedString(website.id),
19
+ title: trimmedString(website.title),
20
+ url: trimmedString(website.url),
21
+ summary: plainText(website.summary),
22
+ tags: tags.length ? tags : undefined,
23
+ status: trimmedString(website.ingestion_status) ??
24
+ trimmedString(website.status),
25
+ bucket_ids: bucketIds(assignment?.assigned),
26
+ created_bucket_ids: bucketIds(assignment?.created),
27
+ });
28
+ }
29
+ }
@@ -0,0 +1,38 @@
1
+ import { asRecord, numberOrUndefined, trimmedString, } from "../calypso/coerce.js";
2
+ /**
3
+ * What every view is built from.
4
+ *
5
+ * A view names each field it returns. Nothing the Calypso API sends reaches a
6
+ * client because it happened to be in the payload: the API's records also
7
+ * carry what only Calypso needs (which key added a file, index store and
8
+ * operation names, task and request ids), and none of that is the user's to
9
+ * read or a model's to repeat.
10
+ */
11
+ /** Names of index stores, operations and storage objects: never shown. */
12
+ const INTERNAL_PATH = /fileSearchStores\/|\boperations\/|\bgs:\/\//;
13
+ const MAX_TEXT = 300;
14
+ /** The fields that are present, in the order given. */
15
+ export function present(fields) {
16
+ return Object.fromEntries(Object.entries(fields).filter(([, value]) => value !== undefined && value !== null));
17
+ }
18
+ /**
19
+ * A short explanation the API wrote for a person (why a file failed, why a
20
+ * source did not move). Dropped when it names an internal path.
21
+ */
22
+ export function plainText(value) {
23
+ const text = trimmedString(value);
24
+ if (!text || INTERNAL_PATH.test(text))
25
+ return undefined;
26
+ return text.length > MAX_TEXT ? `${text.slice(0, MAX_TEXT)}…` : text;
27
+ }
28
+ /** Bucket ids out of an assignment list, whose entries are ids or records. */
29
+ export function bucketIds(value) {
30
+ const ids = (Array.isArray(value) ? value : [])
31
+ .map((entry) => {
32
+ const record = asRecord(entry);
33
+ return trimmedString(record ? (record.id ?? record.bucket_id ?? record.slug) : entry);
34
+ })
35
+ .filter((id) => Boolean(id));
36
+ return ids.length ? [...new Set(ids)] : undefined;
37
+ }
38
+ export { asRecord, numberOrUndefined, trimmedString };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@calypsohq/elephant-mcp-server",
3
- "version": "1.3.0",
3
+ "version": "1.3.2",
4
4
  "description": "Elephant: PDF AI and docs Q&A as an MCP server. Chat with your PDFs, documents and web pages from Claude, ChatGPT and Cursor, with every answer cited.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",