@calypsohq/elephant-mcp-server 1.1.1 → 1.3.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/README.md +9 -6
- package/dist/ElephantMcpServer.js +6 -3
- package/dist/auth/GrantScopes.js +25 -0
- package/dist/calypso/AgentChoice.js +40 -0
- package/dist/calypso/AgentResponses.js +12 -5
- package/dist/calypso/AnswerDiagnostics.js +49 -0
- package/dist/calypso/BucketEdits.js +35 -0
- package/dist/calypso/BucketSources.js +38 -0
- package/dist/calypso/CalypsoApi.js +6 -0
- package/dist/calypso/CalypsoApiClient.js +10 -1
- package/dist/http/McpEndpoint.js +1 -0
- package/dist/http/ProtectedResourceMetadata.js +1 -1
- package/dist/presets/PresetInstructions.js +35 -0
- package/dist/presets/ToolGroup.js +43 -0
- package/dist/presets/ToolPreset.js +19 -0
- package/dist/presets/ToolPresetRegistry.js +21 -6
- package/dist/stdio/LocalPresets.js +28 -13
- package/dist/stdio/StdioOptions.js +3 -2
- package/dist/tools/AddFileTool.js +2 -0
- package/dist/tools/AddFilesTool.js +2 -0
- package/dist/tools/AddWebsiteTool.js +2 -0
- package/dist/tools/AskTool.js +106 -14
- package/dist/tools/CreateBucketTool.js +3 -0
- package/dist/tools/ListBucketsTool.js +30 -3
- package/dist/tools/MoveSourcesTool.js +58 -0
- package/dist/tools/OpenSourceTool.js +2 -0
- package/dist/tools/RenameBucketTool.js +52 -0
- package/dist/tools/SearchTool.js +16 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -26,14 +26,14 @@ CALYPSO_API_KEY=sk-… npx -y @calypsohq/elephant-mcp-server [--preset all|eleph
|
|
|
26
26
|
| `CALYPSO_API_BASE_URL` | `https://api.calypso.so/v1` | Calypso API |
|
|
27
27
|
| `ELEPHANT_PRESET` | `all` | default for `--preset` |
|
|
28
28
|
|
|
29
|
-
`all` publishes
|
|
29
|
+
`all` publishes every tool under the `elephant` server name; `elephant` keeps the four reading tools only, as it always has locally. Both hatch, and neither is served over HTTP. Discovery and the knowledge inventory load once at startup. Nothing of ours writes to stdout, which belongs to the protocol; `--help` and `--version` answer on stderr.
|
|
30
30
|
|
|
31
31
|
## Endpoints
|
|
32
32
|
|
|
33
33
|
| Path | Purpose |
|
|
34
34
|
|---|---|
|
|
35
|
-
| `POST /elephant/mcp` | Elephant
|
|
36
|
-
| `POST /elephant/builder/mcp` | Elephant Builder (add
|
|
35
|
+
| `POST /elephant/mcp` | Elephant: every tool, offered by the scopes the caller's grant carries |
|
|
36
|
+
| `POST /elephant/builder/mcp` | Elephant Builder (the four add tools; kept for existing connections) |
|
|
37
37
|
| `GET /.well-known/oauth-protected-resource/elephant/mcp` | RFC 9728 metadata for Elephant |
|
|
38
38
|
| `GET /.well-known/oauth-protected-resource/elephant/builder/mcp` | RFC 9728 metadata for Elephant Builder |
|
|
39
39
|
| `GET /healthz` | `{ ok, version }` |
|
|
@@ -42,7 +42,7 @@ CALYPSO_API_KEY=sk-… npx -y @calypsohq/elephant-mcp-server [--preset all|eleph
|
|
|
42
42
|
|
|
43
43
|
## Authentication
|
|
44
44
|
|
|
45
|
-
- **OAuth access token** from `login.calypso.so`: ES256 JWT, `aud` = the endpoint URL, `scope` = `elephant:read`
|
|
45
|
+
- **OAuth access token** from `login.calypso.so`: ES256 JWT, `aud` = the endpoint URL, `scope` = the scopes the user allowed (`elephant:read`, and on `/elephant/mcp` optionally `elephant:write` and `elephant:organise`), and an `ek` claim holding the grant's Calypso API key sealed with AES-256-GCM (`base64url(iv) "." base64url(ciphertext‖tag)`). A token for one endpoint is refused on the other.
|
|
46
46
|
- **Calypso API key** (`Authorization: Bearer sk-…`), for testing and clients that cannot do OAuth. Header only, never in the URL.
|
|
47
47
|
|
|
48
48
|
An unauthenticated request gets `401` with `WWW-Authenticate: Bearer resource_metadata="…"`, which is how ChatGPT discovers sign-in.
|
|
@@ -71,9 +71,12 @@ src/
|
|
|
71
71
|
tools/ one class per tool, each owning its name, wording and inputs
|
|
72
72
|
ElephantTool.ts base class: call() turns failures into readable tool errors
|
|
73
73
|
AskTool, SearchTool, ListBucketsTool, OpenSourceTool (Elephant)
|
|
74
|
-
AddFileTool, AddFilesTool, AddWebsiteTool, CreateBucketTool (
|
|
74
|
+
AddFileTool, AddFilesTool, AddWebsiteTool, CreateBucketTool (add)
|
|
75
|
+
RenameBucketTool, MoveSourcesTool (organise)
|
|
75
76
|
ToolContext.ts, ToolLogger.ts, schemaParts.ts
|
|
76
|
-
presets/ ToolPreset + ToolPresetRegistry: each
|
|
77
|
+
presets/ ToolPreset + ToolPresetRegistry: each endpoint's path, scope and tools;
|
|
78
|
+
ToolGroup (read, add, organise) and PresetInstructions, which decide
|
|
79
|
+
what one caller is offered and told
|
|
77
80
|
calypso/ the Calypso API as one caller sees it (CalypsoApi groups the resources:
|
|
78
81
|
AgentResponses, KnowledgeSearch, KnowledgeBuckets, KnowledgeFiles,
|
|
79
82
|
KnowledgeBatches, KnowledgeWebsites) plus agent discovery
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
+
import { GrantScopes } from "./auth/GrantScopes.js";
|
|
2
3
|
import { AgentCatalog } from "./calypso/AgentCatalog.js";
|
|
3
4
|
import { CalypsoApi } from "./calypso/CalypsoApi.js";
|
|
4
5
|
import { KnowledgeInventory, } from "./calypso/KnowledgeInventory.js";
|
|
@@ -15,6 +16,8 @@ export const GET_STARTED_PROMPT = "elephant-get-started";
|
|
|
15
16
|
*/
|
|
16
17
|
export function createElephantMcpServer(options) {
|
|
17
18
|
const { preset, packageInfo } = options;
|
|
19
|
+
const grant = GrantScopes.of(options.scopes);
|
|
20
|
+
const instructions = preset.instructionsFor(grant);
|
|
18
21
|
const inventory = preset.hatches ? (options.inventory ?? null) : null;
|
|
19
22
|
const server = new McpServer({
|
|
20
23
|
name: preset.serverName,
|
|
@@ -23,8 +26,8 @@ export function createElephantMcpServer(options) {
|
|
|
23
26
|
}, {
|
|
24
27
|
capabilities: { logging: {} },
|
|
25
28
|
instructions: preset.hatches
|
|
26
|
-
? hatchingInstructions(
|
|
27
|
-
:
|
|
29
|
+
? hatchingInstructions(instructions, inventory)
|
|
30
|
+
: instructions,
|
|
28
31
|
});
|
|
29
32
|
const api = new CalypsoApi(options.api, `${packageInfo.name}/${packageInfo.version} (Node.js/${process.versions.node})`);
|
|
30
33
|
const context = {
|
|
@@ -33,7 +36,7 @@ export function createElephantMcpServer(options) {
|
|
|
33
36
|
inventory: new KnowledgeInventory(inventory, () => api.buckets.list()),
|
|
34
37
|
log: new ToolLogger(server, options.principal),
|
|
35
38
|
};
|
|
36
|
-
for (const tool of preset.
|
|
39
|
+
for (const tool of preset.toolsFor(grant))
|
|
37
40
|
register(server, tool, context);
|
|
38
41
|
if (preset.hatches)
|
|
39
42
|
registerGetStarted(server, context);
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What one caller agreed to, as the tool layer needs to ask it.
|
|
3
|
+
*
|
|
4
|
+
* An OAuth grant lists its scopes. A raw API key lists none: the Calypso API
|
|
5
|
+
* is the authority on what that key may do, so every tool is offered and a
|
|
6
|
+
* call the key cannot make fails with the API's own error.
|
|
7
|
+
*/
|
|
8
|
+
export class GrantScopes {
|
|
9
|
+
scopes;
|
|
10
|
+
constructor(scopes) {
|
|
11
|
+
this.scopes = scopes;
|
|
12
|
+
}
|
|
13
|
+
/** Everything: a local server, or a key whose limits the API enforces. */
|
|
14
|
+
static everything() {
|
|
15
|
+
return new GrantScopes(null);
|
|
16
|
+
}
|
|
17
|
+
static of(scopes) {
|
|
18
|
+
return scopes?.length
|
|
19
|
+
? new GrantScopes(new Set(scopes))
|
|
20
|
+
: GrantScopes.everything();
|
|
21
|
+
}
|
|
22
|
+
allows(group) {
|
|
23
|
+
return this.scopes === null || this.scopes.has(group.scope);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The agent a connection asks when the caller names none: the team's default
|
|
3
|
+
* agent reading every bucket the team has, decided per question by the API.
|
|
4
|
+
* A bucket created through Elephant is answerable from the next question on,
|
|
5
|
+
* which a stored agent (linked to at most five chosen buckets) cannot promise.
|
|
6
|
+
*/
|
|
7
|
+
export const CONNECTOR_AGENT_ID = "elephant-agent:mcp";
|
|
8
|
+
/**
|
|
9
|
+
* Which agent answers this call, and what to fall back to.
|
|
10
|
+
*
|
|
11
|
+
* A caller that names an agent gets exactly that one, checked against the
|
|
12
|
+
* catalog. A caller that names none gets the connector agent, with the
|
|
13
|
+
* catalog's default behind it for an API that does not know the connector
|
|
14
|
+
* agent yet, so the server can be deployed before or after the API.
|
|
15
|
+
*/
|
|
16
|
+
export class AgentChoice {
|
|
17
|
+
agentId;
|
|
18
|
+
fallbackId;
|
|
19
|
+
constructor(agentId, fallbackId) {
|
|
20
|
+
this.agentId = agentId;
|
|
21
|
+
this.fallbackId = fallbackId;
|
|
22
|
+
}
|
|
23
|
+
static of(catalog, requested) {
|
|
24
|
+
if (String(requested || "").trim()) {
|
|
25
|
+
return new AgentChoice(catalog.resolve(requested), null);
|
|
26
|
+
}
|
|
27
|
+
const fallback = catalog.defaultAgentId;
|
|
28
|
+
return new AgentChoice(CONNECTOR_AGENT_ID, fallback && fallback !== CONNECTOR_AGENT_ID ? fallback : null);
|
|
29
|
+
}
|
|
30
|
+
get isConnector() {
|
|
31
|
+
return this.agentId === CONNECTOR_AGENT_ID;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/** Whether an API error says the agent itself is unknown, not something in the request. */
|
|
35
|
+
export function isUnknownAgent(error) {
|
|
36
|
+
const status = error?.status;
|
|
37
|
+
const message = error instanceof Error ? error.message : String(error ?? "");
|
|
38
|
+
return (status === 404 &&
|
|
39
|
+
/model_not_found|agent_not_found|unknown (rag )?(agent|profile|model)|does not exist/i.test(message));
|
|
40
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
2
|
import OpenAI from "openai";
|
|
3
3
|
import { AnswerDepth } from "./AnswerDepth.js";
|
|
4
|
+
import { AnswerDiagnostics } from "./AnswerDiagnostics.js";
|
|
4
5
|
import { AnswerSources } from "./AnswerSources.js";
|
|
5
6
|
import { requireApiKey } from "./CalypsoApiConfig.js";
|
|
6
7
|
/**
|
|
@@ -61,6 +62,13 @@ export class AgentResponses {
|
|
|
61
62
|
};
|
|
62
63
|
}
|
|
63
64
|
static metadataFor(question) {
|
|
65
|
+
const aicore = {
|
|
66
|
+
// Named files are retrieved from, not pasted into the prompt whole.
|
|
67
|
+
...(question.fileIds?.length
|
|
68
|
+
? { file_input_strategy: "rag_policy" }
|
|
69
|
+
: {}),
|
|
70
|
+
...(question.buckets?.length ? { buckets: [...question.buckets] } : {}),
|
|
71
|
+
};
|
|
64
72
|
return {
|
|
65
73
|
tool: "mcp",
|
|
66
74
|
agent: question.agentId,
|
|
@@ -68,10 +76,7 @@ export class AgentResponses {
|
|
|
68
76
|
...(question.depth
|
|
69
77
|
? { response_policy: question.depth.responsePolicy }
|
|
70
78
|
: {}),
|
|
71
|
-
|
|
72
|
-
...(question.fileIds?.length
|
|
73
|
-
? { _aicore: { file_input_strategy: "rag_policy" } }
|
|
74
|
-
: {}),
|
|
79
|
+
...(Object.keys(aicore).length ? { _aicore: aicore } : {}),
|
|
75
80
|
};
|
|
76
81
|
}
|
|
77
82
|
/** Folds the event stream into the answer text, its sources and the response id. */
|
|
@@ -80,6 +85,7 @@ export class AgentResponses {
|
|
|
80
85
|
let responseId = null;
|
|
81
86
|
let sources = AnswerSources.none();
|
|
82
87
|
let depth;
|
|
88
|
+
let diagnostics = AnswerDiagnostics.none();
|
|
83
89
|
for await (const event of stream) {
|
|
84
90
|
if (event.type === "response.output_text.delta" &&
|
|
85
91
|
typeof event.delta === "string") {
|
|
@@ -97,8 +103,9 @@ export class AgentResponses {
|
|
|
97
103
|
}
|
|
98
104
|
sources = AnswerSources.fromResponse(event.response);
|
|
99
105
|
depth = AnswerDepth.used(event.response);
|
|
106
|
+
diagnostics = AnswerDiagnostics.fromResponse(event.response);
|
|
100
107
|
}
|
|
101
108
|
}
|
|
102
|
-
return { text, responseId, sources, depth };
|
|
109
|
+
return { text, responseId, sources, depth, diagnostics };
|
|
103
110
|
}
|
|
104
111
|
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/** Retrieval failures a caller can act on, in the words it should act on. */
|
|
2
|
+
const REASONS = {
|
|
3
|
+
attached_source_outside_scope: "A source in `fileIds` is in a bucket the agent named in `model` does not read. Ask again without `model` (Elephant then reads every bucket), or without `fileIds`.",
|
|
4
|
+
bucket_outside_agent_scope: "The agent named in `model` does not read one of the buckets in `buckets`. Ask again without `model` (Elephant then reads every bucket), or name only that agent's buckets.",
|
|
5
|
+
bucket_scope_unsupported: "The agent named in `model` does not read buckets, so it cannot be limited to some. Ask again without `buckets`, or without `model`.",
|
|
6
|
+
bucket_store_indexing_pending: "The buckets to answer from have nothing indexed yet. Files usually finish indexing within a minute; ask again shortly.",
|
|
7
|
+
attached_source_unavailable: "A source in `fileIds` is still being indexed. Ask again in a minute, or without `fileIds`.",
|
|
8
|
+
attached_source_lookup_failed: "Elephant could not check where a source in `fileIds` lives. Ask again.",
|
|
9
|
+
attached_sources_mixed_storage: "The sources in `fileIds` live in different places and could not be read together. Ask about them in separate questions.",
|
|
10
|
+
bucket_store_limit_exceeded: "The agent named in `model` reads more buckets than one question can. Ask again without `model`, or name up to five `buckets`.",
|
|
11
|
+
bucket_cover_unresolved: "Elephant could not work out which buckets hold the sources in `fileIds`. Ask again with fewer of them, or name the `buckets`.",
|
|
12
|
+
};
|
|
13
|
+
export class AnswerDiagnostics {
|
|
14
|
+
failureCode;
|
|
15
|
+
coverage;
|
|
16
|
+
constructor(failureCode, coverage) {
|
|
17
|
+
this.failureCode = failureCode;
|
|
18
|
+
this.coverage = coverage;
|
|
19
|
+
}
|
|
20
|
+
static none() {
|
|
21
|
+
return new AnswerDiagnostics(null, null);
|
|
22
|
+
}
|
|
23
|
+
static fromResponse(response) {
|
|
24
|
+
const aicore = record(record(record(response)?.metadata)?._aicore);
|
|
25
|
+
const code = typeof aicore?.answer_failure_code === "string"
|
|
26
|
+
? aicore.answer_failure_code.trim() || null
|
|
27
|
+
: null;
|
|
28
|
+
const raw = record(aicore?.bucket_coverage);
|
|
29
|
+
const unsearched = strings(raw?.unsearched);
|
|
30
|
+
const coverage = unsearched.length
|
|
31
|
+
? { searched: strings(raw?.searched), unsearched }
|
|
32
|
+
: null;
|
|
33
|
+
return new AnswerDiagnostics(code, coverage);
|
|
34
|
+
}
|
|
35
|
+
/** What to tell the caller instead of the answer text, when retrieval could not run. */
|
|
36
|
+
failureReason() {
|
|
37
|
+
return this.failureCode ? (REASONS[this.failureCode] ?? null) : null;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function record(value) {
|
|
41
|
+
return value && typeof value === "object"
|
|
42
|
+
? value
|
|
43
|
+
: null;
|
|
44
|
+
}
|
|
45
|
+
function strings(value) {
|
|
46
|
+
return Array.isArray(value)
|
|
47
|
+
? value.filter((item) => typeof item === "string" && !!item)
|
|
48
|
+
: [];
|
|
49
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reorganising what is already saved: a bucket's name and note, and which
|
|
3
|
+
* bucket a source is filed in. Nothing here adds or deletes a source.
|
|
4
|
+
*/
|
|
5
|
+
export class BucketEdits {
|
|
6
|
+
api;
|
|
7
|
+
constructor(api) {
|
|
8
|
+
this.api = api;
|
|
9
|
+
}
|
|
10
|
+
async rename(input) {
|
|
11
|
+
const body = await this.api.request(`/buckets/${encodeURIComponent(input.bucket.trim())}`, {
|
|
12
|
+
method: "PATCH",
|
|
13
|
+
body: {
|
|
14
|
+
...(input.name?.trim() ? { name: input.name.trim() } : {}),
|
|
15
|
+
...(typeof input.description === "string"
|
|
16
|
+
? { description: input.description.trim() }
|
|
17
|
+
: {}),
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
return body ?? {};
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Moves sources into `toBucket`. The API adds each one there before taking
|
|
24
|
+
* it out of `fromBucket`, so a source is never left in no bucket.
|
|
25
|
+
*/
|
|
26
|
+
moveSources(input) {
|
|
27
|
+
return this.api.request(`/buckets/${encodeURIComponent(input.toBucket.trim())}/sources:move`, {
|
|
28
|
+
method: "POST",
|
|
29
|
+
body: {
|
|
30
|
+
source_ids: input.sourceIds,
|
|
31
|
+
from_bucket: input.fromBucket.trim(),
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { asRecord, numberOrUndefined, trimmedString } from "./coerce.js";
|
|
2
|
+
/** The most sources named per bucket in one listing. */
|
|
3
|
+
export const SOURCES_PER_BUCKET = 50;
|
|
4
|
+
/**
|
|
5
|
+
* What is inside a bucket (`GET /buckets/{id}/sources`): the names behind the
|
|
6
|
+
* ids a bucket listing carries, so a file can be called by its name before
|
|
7
|
+
* any answer has cited it.
|
|
8
|
+
*/
|
|
9
|
+
export class BucketSources {
|
|
10
|
+
api;
|
|
11
|
+
constructor(api) {
|
|
12
|
+
this.api = api;
|
|
13
|
+
}
|
|
14
|
+
async firstPage(bucketId) {
|
|
15
|
+
const body = await this.api.request(`/buckets/${encodeURIComponent(bucketId)}/sources?limit=${SOURCES_PER_BUCKET}`, { method: "GET" });
|
|
16
|
+
const raw = asRecord(body) ?? {};
|
|
17
|
+
const sources = (Array.isArray(raw.sources) ? raw.sources : [])
|
|
18
|
+
.map(BucketSources.source)
|
|
19
|
+
.filter((source) => source !== null);
|
|
20
|
+
return {
|
|
21
|
+
sources,
|
|
22
|
+
total: numberOrUndefined(raw.total) ?? sources.length,
|
|
23
|
+
hasMore: raw.has_more === true,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
static source(value) {
|
|
27
|
+
const raw = asRecord(value);
|
|
28
|
+
const id = trimmedString(raw?.id);
|
|
29
|
+
if (!raw || !id)
|
|
30
|
+
return null;
|
|
31
|
+
return {
|
|
32
|
+
id,
|
|
33
|
+
name: trimmedString(raw.title) ?? id,
|
|
34
|
+
type: trimmedString(raw.type) ?? "file",
|
|
35
|
+
ready: raw.ready === true,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { AgentResponses } from "./AgentResponses.js";
|
|
2
|
+
import { BucketEdits } from "./BucketEdits.js";
|
|
3
|
+
import { BucketSources } from "./BucketSources.js";
|
|
2
4
|
import { CalypsoApiClient } from "./CalypsoApiClient.js";
|
|
3
5
|
import { KnowledgeBatches } from "./KnowledgeBatches.js";
|
|
4
6
|
import { KnowledgeBuckets } from "./KnowledgeBuckets.js";
|
|
@@ -16,6 +18,8 @@ export class CalypsoApi {
|
|
|
16
18
|
agent;
|
|
17
19
|
batches;
|
|
18
20
|
buckets;
|
|
21
|
+
bucketEdits;
|
|
22
|
+
bucketSources;
|
|
19
23
|
files;
|
|
20
24
|
search;
|
|
21
25
|
websites;
|
|
@@ -25,6 +29,8 @@ export class CalypsoApi {
|
|
|
25
29
|
this.agent = new AgentResponses(config, userAgent);
|
|
26
30
|
this.batches = new KnowledgeBatches(client);
|
|
27
31
|
this.buckets = new KnowledgeBuckets(client);
|
|
32
|
+
this.bucketEdits = new BucketEdits(client);
|
|
33
|
+
this.bucketSources = new BucketSources(client);
|
|
28
34
|
this.files = new KnowledgeFiles(client);
|
|
29
35
|
this.search = new KnowledgeSearch(client);
|
|
30
36
|
this.websites = new KnowledgeWebsites(client);
|
|
@@ -28,7 +28,7 @@ export class CalypsoApiClient {
|
|
|
28
28
|
});
|
|
29
29
|
const body = await readResponseBody(response);
|
|
30
30
|
if (!response.ok) {
|
|
31
|
-
throw new
|
|
31
|
+
throw new CalypsoApiError(response.status, formatApiError(response.status, body));
|
|
32
32
|
}
|
|
33
33
|
return body;
|
|
34
34
|
}
|
|
@@ -42,3 +42,12 @@ export class CalypsoApiClient {
|
|
|
42
42
|
};
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
|
+
/** A non-2xx answer from the Calypso API, with its status kept for callers that branch on it. */
|
|
46
|
+
export class CalypsoApiError extends Error {
|
|
47
|
+
status;
|
|
48
|
+
constructor(status, message) {
|
|
49
|
+
super(message);
|
|
50
|
+
this.status = status;
|
|
51
|
+
this.name = "CalypsoApiError";
|
|
52
|
+
}
|
|
53
|
+
}
|
package/dist/http/McpEndpoint.js
CHANGED
|
@@ -31,7 +31,7 @@ export class ProtectedResourceMetadata {
|
|
|
31
31
|
return {
|
|
32
32
|
resource: preset.resourceUrl(this.publicBaseUrl),
|
|
33
33
|
authorization_servers: [this.issuer],
|
|
34
|
-
scopes_supported: preset.
|
|
34
|
+
scopes_supported: preset.scopes(),
|
|
35
35
|
bearer_methods_supported: ["header"],
|
|
36
36
|
resource_name: preset.displayName,
|
|
37
37
|
resource_documentation: DOCUMENTATION_URL,
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The session instructions for one caller: what the server is, then what this
|
|
3
|
+
* connection can do and what it cannot.
|
|
4
|
+
*
|
|
5
|
+
* Composed from the groups the caller holds, so a read-only session is never
|
|
6
|
+
* told it can save a file, and a session that can is told to confirm first.
|
|
7
|
+
* What it cannot do comes with the way to get it, because the model is the
|
|
8
|
+
* only one who can tell the user.
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Said to every caller, whatever they allowed. A model asked to delete tends
|
|
12
|
+
* to go looking for the thing first; looking is a tool call, and the honest
|
|
13
|
+
* answer needs none.
|
|
14
|
+
*/
|
|
15
|
+
export const NEVER_DELETES = "Elephant never deletes. When the user asks to delete, remove, erase or forget a source or a bucket, do not call any tool, not even to look for it: say that Elephant cannot delete and that sources and buckets are removed in the Elephant console at https://elephant.calypso.so.";
|
|
16
|
+
export const PresetInstructions = {
|
|
17
|
+
compose(about, groups, grant) {
|
|
18
|
+
const held = groups.filter((group) => grant.allows(group));
|
|
19
|
+
const missing = groups.filter((group) => !grant.allows(group) && group.cannot);
|
|
20
|
+
const lines = [about];
|
|
21
|
+
for (const group of held) {
|
|
22
|
+
if (group.id !== "read")
|
|
23
|
+
lines.push(`It can ${group.can}.`);
|
|
24
|
+
}
|
|
25
|
+
if (missing.length > 0) {
|
|
26
|
+
const what = missing.map((group) => group.cannot).join(", or ");
|
|
27
|
+
lines.push(`This connection cannot ${what}: the user did not allow it when connecting. If they ask for that, say so and tell them to reconnect Elephant and allow it. Never claim to have done it.`);
|
|
28
|
+
}
|
|
29
|
+
if (held.length === 1 && missing.length === 0) {
|
|
30
|
+
lines.push("It only reads: it cannot add, change or delete knowledge.");
|
|
31
|
+
}
|
|
32
|
+
lines.push(NEVER_DELETES);
|
|
33
|
+
return lines.join(" ");
|
|
34
|
+
},
|
|
35
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a tool does to the caller's knowledge, and the consent that unlocks it.
|
|
3
|
+
*
|
|
4
|
+
* One endpoint offers every tool; a caller sees the groups they agreed to.
|
|
5
|
+
* A group carries its own words, so the session instructions say exactly what
|
|
6
|
+
* this connection can and cannot do instead of describing the whole product.
|
|
7
|
+
*/
|
|
8
|
+
export class ToolGroup {
|
|
9
|
+
/** Answering and searching: never changes anything. */
|
|
10
|
+
static read = new ToolGroup({
|
|
11
|
+
id: "read",
|
|
12
|
+
scope: "elephant:read",
|
|
13
|
+
can: "answer questions, search passages, list buckets and open a source",
|
|
14
|
+
cannot: "",
|
|
15
|
+
});
|
|
16
|
+
/** Saving new sources and creating buckets. */
|
|
17
|
+
static add = new ToolGroup({
|
|
18
|
+
id: "add",
|
|
19
|
+
scope: "elephant:write",
|
|
20
|
+
can: "save files, file links and web pages into buckets and create new buckets. Confirm the destination bucket with the user before adding anything",
|
|
21
|
+
cannot: "add files, websites or buckets",
|
|
22
|
+
});
|
|
23
|
+
/** Renaming buckets and moving sources between them. Deletes nothing. */
|
|
24
|
+
static organise = new ToolGroup({
|
|
25
|
+
id: "organise",
|
|
26
|
+
scope: "elephant:organise",
|
|
27
|
+
can: "rename buckets and move sources from one bucket to another. Say what will move and where before doing it",
|
|
28
|
+
cannot: "rename buckets or move sources",
|
|
29
|
+
});
|
|
30
|
+
id;
|
|
31
|
+
/** OAuth scope a grant must carry for this group's tools. */
|
|
32
|
+
scope;
|
|
33
|
+
/** Completes "It can …" for a caller who has the group. */
|
|
34
|
+
can;
|
|
35
|
+
/** Completes "It cannot …" for a caller who does not. */
|
|
36
|
+
cannot;
|
|
37
|
+
constructor(options) {
|
|
38
|
+
this.id = options.id;
|
|
39
|
+
this.scope = options.scope;
|
|
40
|
+
this.can = options.can;
|
|
41
|
+
this.cannot = options.cannot;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { PresetInstructions } from "./PresetInstructions.js";
|
|
1
2
|
/**
|
|
2
3
|
* One published plugin surface: its endpoint, its scope, and its tools.
|
|
3
4
|
*
|
|
@@ -27,6 +28,24 @@ export class ToolPreset {
|
|
|
27
28
|
toolNames() {
|
|
28
29
|
return this.tools.map((tool) => tool.name);
|
|
29
30
|
}
|
|
31
|
+
/** The groups this preset's tools belong to, in first-seen order. */
|
|
32
|
+
groups() {
|
|
33
|
+
return [...new Set(this.tools.map((tool) => tool.group))];
|
|
34
|
+
}
|
|
35
|
+
/** Every OAuth scope a grant for this preset may carry. */
|
|
36
|
+
scopes() {
|
|
37
|
+
if (!this.scope)
|
|
38
|
+
return [];
|
|
39
|
+
return [...new Set([this.scope, ...this.groups().map((g) => g.scope)])];
|
|
40
|
+
}
|
|
41
|
+
/** The tools one caller is offered: those of the groups they agreed to. */
|
|
42
|
+
toolsFor(grant) {
|
|
43
|
+
return this.tools.filter((tool) => grant.allows(tool.group));
|
|
44
|
+
}
|
|
45
|
+
/** `initialize.instructions` for one caller. */
|
|
46
|
+
instructionsFor(grant) {
|
|
47
|
+
return PresetInstructions.compose(this.instructions, this.groups(), grant);
|
|
48
|
+
}
|
|
30
49
|
/** Absolute resource URL (`aud`) for this preset under a public base URL. */
|
|
31
50
|
resourceUrl(publicBaseUrl) {
|
|
32
51
|
return new URL(this.path, publicBaseUrl.replace(/\/+$/, "")).toString();
|
|
@@ -4,20 +4,25 @@ import { AddWebsiteTool } from "../tools/AddWebsiteTool.js";
|
|
|
4
4
|
import { AskTool } from "../tools/AskTool.js";
|
|
5
5
|
import { CreateBucketTool } from "../tools/CreateBucketTool.js";
|
|
6
6
|
import { ListBucketsTool } from "../tools/ListBucketsTool.js";
|
|
7
|
+
import { MoveSourcesTool } from "../tools/MoveSourcesTool.js";
|
|
7
8
|
import { OpenSourceTool } from "../tools/OpenSourceTool.js";
|
|
9
|
+
import { RenameBucketTool } from "../tools/RenameBucketTool.js";
|
|
8
10
|
import { SearchTool } from "../tools/SearchTool.js";
|
|
11
|
+
import { ToolGroup } from "./ToolGroup.js";
|
|
9
12
|
import { ToolPreset } from "./ToolPreset.js";
|
|
10
|
-
/**
|
|
13
|
+
/**
|
|
14
|
+
* Elephant by Calypso: everything in one place. Asking and searching for
|
|
15
|
+
* every caller; saving and organising for those who allowed it.
|
|
16
|
+
*/
|
|
11
17
|
const elephant = new ToolPreset({
|
|
12
18
|
id: "elephant",
|
|
13
19
|
path: "/elephant/mcp",
|
|
14
20
|
serverName: "elephant",
|
|
15
21
|
displayName: "Elephant by Calypso",
|
|
16
|
-
scope:
|
|
22
|
+
scope: ToolGroup.read.scope,
|
|
17
23
|
instructions: [
|
|
18
24
|
"Elephant by Calypso answers from this team's Calypso knowledge — the documents, websites and images the team has added — and cites every source.",
|
|
19
25
|
"Answers and searches have a depth. Start shallow and go one step deeper only when a result is thin or says the knowledge does not cover something the user expects: `depth` on elephant-ask (`fast`, `balanced`, `deep`) and on elephant-search (`fast`, `thorough`, with `alsoSearchFor`).",
|
|
20
|
-
"It is read-only: it cannot add, change or delete knowledge. To add files or websites, use Elephant Builder by Calypso.",
|
|
21
26
|
"When the knowledge does not cover a question, say so instead of answering from general knowledge.",
|
|
22
27
|
].join(" "),
|
|
23
28
|
tools: [
|
|
@@ -25,20 +30,30 @@ const elephant = new ToolPreset({
|
|
|
25
30
|
new SearchTool(),
|
|
26
31
|
new ListBucketsTool(),
|
|
27
32
|
new OpenSourceTool(),
|
|
33
|
+
new AddFileTool(),
|
|
34
|
+
new AddFilesTool(),
|
|
35
|
+
new AddWebsiteTool(),
|
|
36
|
+
new CreateBucketTool(),
|
|
37
|
+
new RenameBucketTool(),
|
|
38
|
+
new MoveSourcesTool(),
|
|
28
39
|
],
|
|
29
40
|
hatches: true,
|
|
30
41
|
});
|
|
31
|
-
/**
|
|
42
|
+
/**
|
|
43
|
+
* Elephant Builder by Calypso — adds sources and buckets.
|
|
44
|
+
*
|
|
45
|
+
* Kept for the connections that already use it. Elephant now offers the same
|
|
46
|
+
* tools on its own endpoint, so nothing new is built on this one.
|
|
47
|
+
*/
|
|
32
48
|
const elephantBuilder = new ToolPreset({
|
|
33
49
|
id: "elephant-builder",
|
|
34
50
|
path: "/elephant/builder/mcp",
|
|
35
51
|
serverName: "elephant-builder",
|
|
36
52
|
displayName: "Elephant Builder by Calypso",
|
|
37
|
-
scope:
|
|
53
|
+
scope: ToolGroup.add.scope,
|
|
38
54
|
instructions: [
|
|
39
55
|
"Elephant Builder by Calypso adds files and websites to this team's Calypso knowledge buckets and creates new buckets.",
|
|
40
56
|
"It does not answer questions; use Elephant by Calypso to ask about the knowledge once it is added.",
|
|
41
|
-
"Confirm the destination bucket with the user before adding content.",
|
|
42
57
|
].join(" "),
|
|
43
58
|
tools: [
|
|
44
59
|
new AddFileTool(),
|
|
@@ -1,12 +1,13 @@
|
|
|
1
|
+
import { ToolGroup } from "../presets/ToolGroup.js";
|
|
1
2
|
import { ToolPreset } from "../presets/ToolPreset.js";
|
|
2
3
|
import { ToolPresetRegistry } from "../presets/ToolPresetRegistry.js";
|
|
3
4
|
/**
|
|
4
|
-
*
|
|
5
|
+
* Everything, for the key in the environment.
|
|
5
6
|
*
|
|
6
|
-
* The
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
7
|
+
* The same tools as the hosted Elephant endpoint, without OAuth: a local
|
|
8
|
+
* server runs under the key owner's own account, so there is no consent to
|
|
9
|
+
* narrow it and the Calypso API decides what the key may do. It is never
|
|
10
|
+
* served over HTTP: the registry does not list it.
|
|
10
11
|
*/
|
|
11
12
|
const all = new ToolPreset({
|
|
12
13
|
id: "all",
|
|
@@ -14,20 +15,34 @@ const all = new ToolPreset({
|
|
|
14
15
|
serverName: "elephant",
|
|
15
16
|
displayName: "Elephant by Calypso",
|
|
16
17
|
scope: "",
|
|
17
|
-
instructions:
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
18
|
+
instructions: ToolPresetRegistry.elephant.instructions,
|
|
19
|
+
tools: [...ToolPresetRegistry.elephant.tools],
|
|
20
|
+
hatches: true,
|
|
21
|
+
});
|
|
22
|
+
/**
|
|
23
|
+
* Reading only, as `--preset elephant` has always meant on a local server.
|
|
24
|
+
*
|
|
25
|
+
* The hosted Elephant endpoint grew the saving and organising tools, offered
|
|
26
|
+
* by consent. A local server has no consent step, so someone who pinned this
|
|
27
|
+
* preset to keep their assistant from changing anything must keep getting
|
|
28
|
+
* exactly that.
|
|
29
|
+
*/
|
|
30
|
+
const readOnly = new ToolPreset({
|
|
31
|
+
id: "elephant",
|
|
32
|
+
path: "/elephant/local/read",
|
|
33
|
+
serverName: "elephant",
|
|
34
|
+
displayName: "Elephant by Calypso",
|
|
35
|
+
scope: "",
|
|
36
|
+
instructions: ToolPresetRegistry.elephant.instructions,
|
|
37
|
+
tools: ToolPresetRegistry.elephant.tools.filter((tool) => tool.group === ToolGroup.read),
|
|
25
38
|
hatches: true,
|
|
26
39
|
});
|
|
27
40
|
/** The preset a local server runs, by the id given on the command line. */
|
|
28
41
|
export function localPreset(id) {
|
|
29
42
|
if (id === "all")
|
|
30
43
|
return all;
|
|
44
|
+
if (id === readOnly.id)
|
|
45
|
+
return readOnly;
|
|
31
46
|
const preset = ToolPresetRegistry.byId(id);
|
|
32
47
|
if (!preset) {
|
|
33
48
|
const known = ["all", ...ToolPresetRegistry.all().map((p) => p.id)];
|
|
@@ -10,8 +10,9 @@ Environment:
|
|
|
10
10
|
ELEPHANT_PRESET default for --preset (default all)
|
|
11
11
|
|
|
12
12
|
Options:
|
|
13
|
-
--preset <id> which tools to publish: elephant (read),
|
|
14
|
-
(
|
|
13
|
+
--preset <id> which tools to publish: elephant (read only),
|
|
14
|
+
elephant-builder (add only) or all (read, add and
|
|
15
|
+
organise; the default)
|
|
15
16
|
--version print the version and exit
|
|
16
17
|
--help print this help and exit
|
|
17
18
|
`;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { countBucketTargets } from "../calypso/BucketDestination.js";
|
|
3
|
+
import { ToolGroup } from "../presets/ToolGroup.js";
|
|
3
4
|
import { ElephantTool, } from "./ElephantTool.js";
|
|
4
5
|
import { bucketDestinationInputs, chatGptFileInput, idempotencyKeyInput, } from "./schemaParts.js";
|
|
5
6
|
const inputSchema = {
|
|
@@ -52,6 +53,7 @@ export class AddFileTool extends ElephantTool {
|
|
|
52
53
|
"Pass `waitForIndexing: true` when the user wants to ask about the file right away.",
|
|
53
54
|
].join("\n");
|
|
54
55
|
inputSchema = inputSchema;
|
|
56
|
+
group = ToolGroup.add;
|
|
55
57
|
fileParams = ["file"];
|
|
56
58
|
annotations = {
|
|
57
59
|
readOnlyHint: false,
|
|
@@ -2,6 +2,7 @@ import { randomUUID } from "node:crypto";
|
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
import { BATCH_MAX_FILES } from "../calypso/BatchManifest.js";
|
|
4
4
|
import { countBucketTargets } from "../calypso/BucketDestination.js";
|
|
5
|
+
import { ToolGroup } from "../presets/ToolGroup.js";
|
|
5
6
|
import { ElephantTool, } from "./ElephantTool.js";
|
|
6
7
|
import { bucketDestinationInputs } from "./schemaParts.js";
|
|
7
8
|
const itemSchema = z.object({
|
|
@@ -53,6 +54,7 @@ export class AddFilesTool extends ElephantTool {
|
|
|
53
54
|
"For a single file the user attached, use elephant-add-file instead.",
|
|
54
55
|
].join("\n");
|
|
55
56
|
inputSchema = inputSchema;
|
|
57
|
+
group = ToolGroup.add;
|
|
56
58
|
annotations = {
|
|
57
59
|
readOnlyHint: false,
|
|
58
60
|
destructiveHint: false,
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { countBucketTargets } from "../calypso/BucketDestination.js";
|
|
3
|
+
import { ToolGroup } from "../presets/ToolGroup.js";
|
|
3
4
|
import { ElephantTool, } from "./ElephantTool.js";
|
|
4
5
|
import { bucketDestinationInputs, idempotencyKeyInput } from "./schemaParts.js";
|
|
5
6
|
const inputSchema = {
|
|
@@ -33,6 +34,7 @@ export class AddWebsiteTool extends ElephantTool {
|
|
|
33
34
|
"Adding a page the team already has is refused as `website_url_exists`, unless the same `idempotencyKey` is sent again.",
|
|
34
35
|
].join("\n");
|
|
35
36
|
inputSchema = inputSchema;
|
|
37
|
+
group = ToolGroup.add;
|
|
36
38
|
annotations = {
|
|
37
39
|
readOnlyHint: false,
|
|
38
40
|
destructiveHint: false,
|
package/dist/tools/AskTool.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
-
import {
|
|
2
|
+
import { AgentChoice, isUnknownAgent } from "../calypso/AgentChoice.js";
|
|
3
|
+
import { AgentResponses, } from "../calypso/AgentResponses.js";
|
|
3
4
|
import { ANSWER_DEPTHS, AnswerDepth } from "../calypso/AnswerDepth.js";
|
|
4
5
|
import { compactStrings } from "../calypso/coerce.js";
|
|
5
6
|
import { emptyKnowledgeAnswer } from "../hatching/HatchingCopy.js";
|
|
7
|
+
import { ToolGroup } from "../presets/ToolGroup.js";
|
|
6
8
|
import { ElephantTool, } from "./ElephantTool.js";
|
|
7
9
|
const inputSchema = {
|
|
8
10
|
prompt: z
|
|
@@ -12,11 +14,17 @@ const inputSchema = {
|
|
|
12
14
|
fileIds: z
|
|
13
15
|
.array(z.string())
|
|
14
16
|
.optional()
|
|
15
|
-
.describe("Optional
|
|
17
|
+
.describe("Optional source ids (from elephant-list-buckets or a search result) that the answer must take into account. The answer may still draw on the agent's other sources; to read only certain buckets, use elephant-search with `buckets`."),
|
|
16
18
|
model: z
|
|
17
19
|
.string()
|
|
18
20
|
.optional()
|
|
19
|
-
.describe("Optional id of one of the team's Calypso agents. Omit to
|
|
21
|
+
.describe("Optional id of one of the team's Calypso agents, when the user names one. Omit it to answer from every bucket the user has, including ones created a moment ago."),
|
|
22
|
+
buckets: z
|
|
23
|
+
.array(z.string().min(1))
|
|
24
|
+
.min(1)
|
|
25
|
+
.max(5)
|
|
26
|
+
.optional()
|
|
27
|
+
.describe("Optional bucket ids or slugs (from elephant-list-buckets), up to 5, to answer only from those. Use it when the user names a folder or topic that matches a bucket; omit it otherwise and Elephant finds the right buckets itself."),
|
|
20
28
|
depth: z
|
|
21
29
|
.enum(ANSWER_DEPTHS)
|
|
22
30
|
.optional()
|
|
@@ -38,13 +46,15 @@ export class AskTool extends ElephantTool {
|
|
|
38
46
|
"Use it when the user asks about their own company, product, policies, documents or anything their team keeps in Calypso.",
|
|
39
47
|
"Always cite the sources the answer returns. If the answer says the knowledge does not cover the question, say so plainly instead of answering from general knowledge.",
|
|
40
48
|
"Each call stands alone: put everything needed to answer, including the subject of a follow-up, in the prompt.",
|
|
41
|
-
"Optional `model` picks one of the team's Calypso agents
|
|
49
|
+
"Without `model`, Elephant answers from every bucket the user has, including one created a moment ago. Optional `model` picks one of the team's Calypso agents instead, which reads only its own buckets. Optional `buckets` limits the answer to up to five named buckets.",
|
|
50
|
+
"When the user has more buckets than one answer reads, Elephant picks the ones with evidence and the reply names any it did not search; ask again with `buckets` to choose them.",
|
|
42
51
|
"Optional `depth` (`fast`, `balanced`, `deep`) sets how much work the answer gets. Choose it from the question, not from habit: `fast` for one stated fact, `balanced` for an explanation or summary, `deep` when several sources must be compared or the answer will be acted on. The reply ends by naming the depth it was answered at.",
|
|
43
52
|
"If an answer is thin, misses part of the question, or says the knowledge does not cover something the user expects to be there, ask once more one depth higher before telling the user it is not covered. Do not repeat a call at the same depth, and do not go above `deep`.",
|
|
44
53
|
"A team's plan may cap depth; when the reply says it was answered at a lower depth than asked, that is the most this team allows, so do not retry higher.",
|
|
45
54
|
"When the team has no knowledge yet, the answer says so and how to add a first source; relay it, do not answer from general knowledge.",
|
|
46
55
|
].join("\n");
|
|
47
56
|
inputSchema = inputSchema;
|
|
57
|
+
group = ToolGroup.read;
|
|
48
58
|
annotations = {
|
|
49
59
|
readOnlyHint: true,
|
|
50
60
|
destructiveHint: false,
|
|
@@ -61,23 +71,38 @@ export class AskTool extends ElephantTool {
|
|
|
61
71
|
});
|
|
62
72
|
return { text: emptyKnowledgeAnswer() };
|
|
63
73
|
}
|
|
64
|
-
const
|
|
74
|
+
const choice = AgentChoice.of(agents, args.model);
|
|
65
75
|
const asked = AnswerDepth.named(args.depth);
|
|
66
76
|
const fileIds = compactStrings(args.fileIds);
|
|
77
|
+
const buckets = compactStrings(args.buckets);
|
|
67
78
|
const conversationId = AgentResponses.newConversationId();
|
|
68
|
-
|
|
69
|
-
tool: this.name,
|
|
70
|
-
agent: agentId,
|
|
71
|
-
conversationId,
|
|
72
|
-
fileCount: fileIds?.length ?? 0,
|
|
73
|
-
});
|
|
74
|
-
const answer = await api.agent.ask({
|
|
75
|
-
agentId,
|
|
79
|
+
const question = {
|
|
76
80
|
prompt: args.prompt.trim(),
|
|
77
81
|
fileIds,
|
|
78
82
|
conversationId,
|
|
79
83
|
depth: asked,
|
|
84
|
+
buckets,
|
|
85
|
+
};
|
|
86
|
+
await log.event("info", "Asking a Calypso agent.", {
|
|
87
|
+
tool: this.name,
|
|
88
|
+
agent: choice.agentId,
|
|
89
|
+
conversationId,
|
|
90
|
+
fileCount: fileIds?.length ?? 0,
|
|
91
|
+
bucketCount: buckets?.length ?? 0,
|
|
80
92
|
});
|
|
93
|
+
const answer = await AskTool.askWithFallback(api, choice, question, log);
|
|
94
|
+
const agentId = answer.agentId;
|
|
95
|
+
// Retrieval could not run as asked: say why, in words the caller can
|
|
96
|
+
// act on, instead of relaying "not enough information".
|
|
97
|
+
const refusal = answer.diagnostics.failureReason();
|
|
98
|
+
if (refusal) {
|
|
99
|
+
await log.event("info", "Retrieval refused the request.", {
|
|
100
|
+
tool: this.name,
|
|
101
|
+
agent: agentId,
|
|
102
|
+
failureCode: answer.diagnostics.failureCode,
|
|
103
|
+
});
|
|
104
|
+
throw new Error(refusal);
|
|
105
|
+
}
|
|
81
106
|
await log.event("info", "Calypso agent answered.", {
|
|
82
107
|
tool: this.name,
|
|
83
108
|
agent: agentId,
|
|
@@ -95,10 +120,62 @@ export class AskTool extends ElephantTool {
|
|
|
95
120
|
const parts = [
|
|
96
121
|
answer.text,
|
|
97
122
|
sources,
|
|
123
|
+
await AskTool.coverageNote(api, answer),
|
|
98
124
|
AskTool.depthNote(asked, answer.depth),
|
|
99
125
|
];
|
|
100
126
|
return { text: parts.filter(Boolean).join("\n\n") };
|
|
101
127
|
}
|
|
128
|
+
/**
|
|
129
|
+
* The connector agent first; the catalog's default when this API does not
|
|
130
|
+
* know it yet. Only an "unknown agent" answer falls back: anything else is
|
|
131
|
+
* the real outcome of the question.
|
|
132
|
+
*/
|
|
133
|
+
static async askWithFallback(api, choice, question, log) {
|
|
134
|
+
try {
|
|
135
|
+
const answer = await api.agent.ask({
|
|
136
|
+
...question,
|
|
137
|
+
agentId: choice.agentId,
|
|
138
|
+
});
|
|
139
|
+
return { ...answer, agentId: choice.agentId };
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
if (!choice.fallbackId || !isUnknownAgent(error)) {
|
|
143
|
+
throw AskTool.readable(error, question.fileIds);
|
|
144
|
+
}
|
|
145
|
+
await log.event("warning", "Connector agent unknown; asked the default.", {
|
|
146
|
+
tool: "elephant-ask",
|
|
147
|
+
agent: choice.fallbackId,
|
|
148
|
+
});
|
|
149
|
+
const answer = await api.agent
|
|
150
|
+
.ask({ ...question, agentId: choice.fallbackId })
|
|
151
|
+
.catch((retryError) => {
|
|
152
|
+
throw AskTool.readable(retryError, question.fileIds);
|
|
153
|
+
});
|
|
154
|
+
return { ...answer, agentId: choice.fallbackId };
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* One line naming the buckets the answer did not read, when there were
|
|
159
|
+
* more than one answer reads. Names come from the bucket list; an id is
|
|
160
|
+
* shown when a name cannot be read.
|
|
161
|
+
*/
|
|
162
|
+
static async coverageNote(api, answer) {
|
|
163
|
+
const coverage = answer.diagnostics.coverage;
|
|
164
|
+
if (!coverage)
|
|
165
|
+
return "";
|
|
166
|
+
const names = new Map();
|
|
167
|
+
try {
|
|
168
|
+
for (const bucket of (await api.buckets.list()).buckets) {
|
|
169
|
+
names.set(bucket.id, bucket.name || bucket.slug || bucket.id);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
catch {
|
|
173
|
+
// Ids are still an honest answer.
|
|
174
|
+
}
|
|
175
|
+
const label = (id) => names.get(id) ?? id;
|
|
176
|
+
const total = coverage.searched.length + coverage.unsearched.length;
|
|
177
|
+
return `Searched ${coverage.searched.length} of ${total} buckets, the ones with the strongest match. Not searched: ${coverage.unsearched.map(label).join(", ")}. Ask again with \`buckets\` to choose.`;
|
|
178
|
+
}
|
|
102
179
|
/**
|
|
103
180
|
* One closing line saying the depth the answer got, so the caller knows
|
|
104
181
|
* what a retry one step higher would buy, and when a plan capped the ask.
|
|
@@ -106,11 +183,26 @@ export class AskTool extends ElephantTool {
|
|
|
106
183
|
static depthNote(asked, used) {
|
|
107
184
|
if (!used)
|
|
108
185
|
return "";
|
|
109
|
-
if (asked
|
|
186
|
+
if (!asked) {
|
|
187
|
+
// Said in the reply because a client may hold a tool list from before
|
|
188
|
+
// `depth` existed, and would otherwise report that it cannot be set.
|
|
189
|
+
return `Answered at depth: ${used.name} (the agent's default; pass \`depth\` as \`fast\`, \`balanced\` or \`deep\` to choose).`;
|
|
190
|
+
}
|
|
191
|
+
if (used.isShallowerThan(asked)) {
|
|
110
192
|
return `Answered at depth: ${used.name} (asked for ${asked.name}; this team's plan caps depth here, so a higher depth will not change the answer).`;
|
|
111
193
|
}
|
|
112
194
|
return `Answered at depth: ${used.name}.`;
|
|
113
195
|
}
|
|
196
|
+
/**
|
|
197
|
+
* A file id the API does not know arrives as a bare 404. Say what it means
|
|
198
|
+
* and what to do, since "404 status code (no body)" tells the caller nothing.
|
|
199
|
+
*/
|
|
200
|
+
static readable(error, fileIds) {
|
|
201
|
+
const status = error?.status;
|
|
202
|
+
if (status !== 404 || !fileIds?.length)
|
|
203
|
+
return error;
|
|
204
|
+
return new Error("One of the `fileIds` is not a source in this team's knowledge. Use ids from elephant-list-buckets, or ask again without `fileIds`.");
|
|
205
|
+
}
|
|
114
206
|
/** Whether the answer text already ends in its own list of sources. */
|
|
115
207
|
static namesItsSources(text) {
|
|
116
208
|
return /(^|\n)\s*(\*\*)?(sources|references|fuentes|referencias)(\*\*)?:\s*\n/i.test(text);
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
+
import { ToolGroup } from "../presets/ToolGroup.js";
|
|
2
3
|
import { ElephantTool, } from "./ElephantTool.js";
|
|
3
4
|
import { idempotencyKeyInput } from "./schemaParts.js";
|
|
4
5
|
const inputSchema = {
|
|
@@ -27,8 +28,10 @@ export class CreateBucketTool extends ElephantTool {
|
|
|
27
28
|
"Creates a new knowledge bucket (a folder of sources) for the team.",
|
|
28
29
|
"",
|
|
29
30
|
"Use it when the user wants to keep a new topic separate before adding files or websites.",
|
|
31
|
+
"A new bucket needs no other setup: elephant-ask and elephant-search answer from it as soon as its files finish indexing.",
|
|
30
32
|
].join("\n");
|
|
31
33
|
inputSchema = inputSchema;
|
|
34
|
+
group = ToolGroup.add;
|
|
32
35
|
outputSchema = outputSchema;
|
|
33
36
|
annotations = {
|
|
34
37
|
readOnlyHint: false,
|
|
@@ -1,21 +1,28 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
+
import { ToolGroup } from "../presets/ToolGroup.js";
|
|
2
3
|
import { ElephantTool, } from "./ElephantTool.js";
|
|
3
4
|
const inputSchema = {
|
|
4
5
|
includeArchived: z
|
|
5
6
|
.boolean()
|
|
6
7
|
.optional()
|
|
7
8
|
.describe("If true, also list archived buckets. Defaults to false."),
|
|
9
|
+
includeSources: z
|
|
10
|
+
.boolean()
|
|
11
|
+
.optional()
|
|
12
|
+
.describe("Name the sources in each bucket (id, name, type, ready). Defaults to true; pass false for counts only."),
|
|
8
13
|
};
|
|
9
14
|
/** `elephant-list-buckets`: what Elephant knows about, folder by folder. */
|
|
10
15
|
export class ListBucketsTool extends ElephantTool {
|
|
11
16
|
name = "elephant-list-buckets";
|
|
12
17
|
title = "List buckets";
|
|
13
18
|
description = [
|
|
14
|
-
"Lists the team's knowledge buckets — folders of sources Elephant remembers — with their names, slugs, file counts and
|
|
19
|
+
"Lists the team's knowledge buckets — folders of sources Elephant remembers — with their names, slugs, file counts and the sources in each (id, name, type, and whether it is ready to answer from).",
|
|
15
20
|
"",
|
|
16
|
-
"Use it to show the user what Elephant knows about,
|
|
21
|
+
"Use it to show the user what Elephant knows about, to pick buckets before elephant-search, or to find a source's id by its name.",
|
|
22
|
+
"Call a source by its name when you tell the user about it. `moreSources: true` means the bucket holds more than the ones named.",
|
|
17
23
|
].join("\n");
|
|
18
24
|
inputSchema = inputSchema;
|
|
25
|
+
group = ToolGroup.read;
|
|
19
26
|
annotations = {
|
|
20
27
|
readOnlyHint: true,
|
|
21
28
|
destructiveHint: false,
|
|
@@ -27,10 +34,30 @@ export class ListBucketsTool extends ElephantTool {
|
|
|
27
34
|
const list = await api.buckets.list({
|
|
28
35
|
includeArchived: args.includeArchived,
|
|
29
36
|
});
|
|
37
|
+
const buckets = args.includeSources === false
|
|
38
|
+
? list.buckets
|
|
39
|
+
: await Promise.all(list.buckets.map((bucket) => ListBucketsTool.named(api, bucket)));
|
|
30
40
|
await log.event("info", "Listed knowledge buckets.", {
|
|
31
41
|
tool: this.name,
|
|
32
42
|
bucketCount: list.buckets.length,
|
|
33
43
|
});
|
|
34
|
-
return { json: list };
|
|
44
|
+
return { json: { ...list, buckets } };
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* The bucket with its sources named. A bucket whose sources cannot be read
|
|
48
|
+
* is returned as it was: the names are a help, and a listing that fails
|
|
49
|
+
* because one bucket's names did not load would be worse than ids alone.
|
|
50
|
+
*/
|
|
51
|
+
static async named(api, bucket) {
|
|
52
|
+
if (bucket.knowledgeIds.length === 0 && bucket.fileIds.length === 0) {
|
|
53
|
+
return { ...bucket, sources: [] };
|
|
54
|
+
}
|
|
55
|
+
try {
|
|
56
|
+
const page = await api.bucketSources.firstPage(bucket.id);
|
|
57
|
+
return { ...bucket, sources: page.sources, moreSources: page.hasMore };
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
return bucket;
|
|
61
|
+
}
|
|
35
62
|
}
|
|
36
63
|
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { ToolGroup } from "../presets/ToolGroup.js";
|
|
3
|
+
import { ElephantTool, } from "./ElephantTool.js";
|
|
4
|
+
/** The API moves at most this many sources in one request. */
|
|
5
|
+
const MAX_SOURCES = 100;
|
|
6
|
+
const inputSchema = {
|
|
7
|
+
sourceIds: z
|
|
8
|
+
.array(z.string().min(1))
|
|
9
|
+
.min(1)
|
|
10
|
+
.max(MAX_SOURCES)
|
|
11
|
+
.describe("Ids of the sources to move, from elephant-list-buckets. Up to 100."),
|
|
12
|
+
fromBucket: z
|
|
13
|
+
.string()
|
|
14
|
+
.min(1)
|
|
15
|
+
.describe("The bucket the sources are in now: its id or slug."),
|
|
16
|
+
toBucket: z
|
|
17
|
+
.string()
|
|
18
|
+
.min(1)
|
|
19
|
+
.describe("The bucket the sources go to: its id or slug. It must exist."),
|
|
20
|
+
};
|
|
21
|
+
/** `elephant-move-sources`: the same sources, filed in another bucket. */
|
|
22
|
+
export class MoveSourcesTool extends ElephantTool {
|
|
23
|
+
name = "elephant-move-sources";
|
|
24
|
+
title = "Move sources";
|
|
25
|
+
description = [
|
|
26
|
+
"Moves sources from one bucket to another. Each source is added to the destination first and only then taken out of the bucket it came from, so it is never left without a bucket. Files are not deleted or uploaded again.",
|
|
27
|
+
"",
|
|
28
|
+
"Use it when the user wants to refile sources: split a bucket, tidy up, or put a file where it belongs.",
|
|
29
|
+
"Before calling, name the sources and both buckets to the user. The destination must already exist: create it with elephant-create-bucket if this connection can.",
|
|
30
|
+
"The result lists each source with what happened: `moved`, `not_in_source_bucket` (it was never in `fromBucket`), `not_found`, `not_added`, or `added_not_removed` (now in both buckets). Report anything that is not `moved`; do not say everything moved when it did not.",
|
|
31
|
+
"A moved source is searchable in its new bucket once indexing there finishes, usually within a minute.",
|
|
32
|
+
].join("\n");
|
|
33
|
+
inputSchema = inputSchema;
|
|
34
|
+
group = ToolGroup.organise;
|
|
35
|
+
annotations = {
|
|
36
|
+
readOnlyHint: false,
|
|
37
|
+
destructiveHint: false,
|
|
38
|
+
idempotentHint: true,
|
|
39
|
+
openWorldHint: false,
|
|
40
|
+
};
|
|
41
|
+
failure = "Moving the sources failed.";
|
|
42
|
+
async run(args, { api, log }) {
|
|
43
|
+
if (args.fromBucket.trim() === args.toBucket.trim()) {
|
|
44
|
+
throw new Error("`fromBucket` and `toBucket` are the same bucket.");
|
|
45
|
+
}
|
|
46
|
+
const result = await api.bucketEdits.moveSources({
|
|
47
|
+
sourceIds: [...new Set(args.sourceIds.map((id) => id.trim()))],
|
|
48
|
+
fromBucket: args.fromBucket,
|
|
49
|
+
toBucket: args.toBucket,
|
|
50
|
+
});
|
|
51
|
+
await log.event("notice", "Sources moved.", {
|
|
52
|
+
tool: this.name,
|
|
53
|
+
requested: result.requested,
|
|
54
|
+
moved: result.moved,
|
|
55
|
+
});
|
|
56
|
+
return { json: result };
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
+
import { ToolGroup } from "../presets/ToolGroup.js";
|
|
2
3
|
import { ElephantTool, } from "./ElephantTool.js";
|
|
3
4
|
const inputSchema = {
|
|
4
5
|
fileId: z
|
|
@@ -20,6 +21,7 @@ export class OpenSourceTool extends ElephantTool {
|
|
|
20
21
|
"Use it when the user asks about a specific source cited in an answer or listed by elephant-list-buckets.",
|
|
21
22
|
].join("\n");
|
|
22
23
|
inputSchema = inputSchema;
|
|
24
|
+
group = ToolGroup.read;
|
|
23
25
|
annotations = {
|
|
24
26
|
readOnlyHint: true,
|
|
25
27
|
destructiveHint: false,
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { ToolGroup } from "../presets/ToolGroup.js";
|
|
3
|
+
import { ElephantTool, } from "./ElephantTool.js";
|
|
4
|
+
const inputSchema = {
|
|
5
|
+
bucket: z
|
|
6
|
+
.string()
|
|
7
|
+
.min(1)
|
|
8
|
+
.describe("The bucket to change: its id or slug, from elephant-list-buckets."),
|
|
9
|
+
name: z
|
|
10
|
+
.string()
|
|
11
|
+
.min(1)
|
|
12
|
+
.max(120)
|
|
13
|
+
.optional()
|
|
14
|
+
.describe("The bucket's new name."),
|
|
15
|
+
description: z
|
|
16
|
+
.string()
|
|
17
|
+
.max(500)
|
|
18
|
+
.optional()
|
|
19
|
+
.describe("A new note on what the bucket holds."),
|
|
20
|
+
};
|
|
21
|
+
/** `elephant-rename-bucket`: a bucket's name and note, nothing in it. */
|
|
22
|
+
export class RenameBucketTool extends ElephantTool {
|
|
23
|
+
name = "elephant-rename-bucket";
|
|
24
|
+
title = "Rename a bucket";
|
|
25
|
+
description = [
|
|
26
|
+
"Changes a bucket's name, its description, or both. The sources in the bucket, their indexing and the bucket's slug stay as they are.",
|
|
27
|
+
"",
|
|
28
|
+
"Use it when the user wants a bucket called something else or wants to note what it holds.",
|
|
29
|
+
"Find the bucket with elephant-list-buckets first, and tell the user the old and the new name. Pass at least one of `name` and `description`.",
|
|
30
|
+
"It does not move, add or delete anything: to move sources use elephant-move-sources.",
|
|
31
|
+
].join("\n");
|
|
32
|
+
inputSchema = inputSchema;
|
|
33
|
+
group = ToolGroup.organise;
|
|
34
|
+
annotations = {
|
|
35
|
+
readOnlyHint: false,
|
|
36
|
+
destructiveHint: false,
|
|
37
|
+
idempotentHint: true,
|
|
38
|
+
openWorldHint: false,
|
|
39
|
+
};
|
|
40
|
+
failure = "Renaming the bucket failed.";
|
|
41
|
+
async run(args, { api, log }) {
|
|
42
|
+
if (!args.name?.trim() && typeof args.description !== "string") {
|
|
43
|
+
throw new Error("Pass a new `name`, a new `description`, or both.");
|
|
44
|
+
}
|
|
45
|
+
const bucket = await api.bucketEdits.rename(args);
|
|
46
|
+
await log.event("notice", "Bucket renamed.", {
|
|
47
|
+
tool: this.name,
|
|
48
|
+
bucketId: typeof bucket.id === "string" ? bucket.id : null,
|
|
49
|
+
});
|
|
50
|
+
return { json: bucket };
|
|
51
|
+
}
|
|
52
|
+
}
|
package/dist/tools/SearchTool.js
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
+
import { CONNECTOR_AGENT_ID, isUnknownAgent } from "../calypso/AgentChoice.js";
|
|
2
3
|
import { SEARCH_DEPTHS } from "../calypso/AnswerDepth.js";
|
|
3
4
|
import { MAX_EXTRA_QUERIES } from "../calypso/KnowledgeSearch.js";
|
|
4
5
|
import { emptyKnowledgeAnswer } from "../hatching/HatchingCopy.js";
|
|
6
|
+
import { ToolGroup } from "../presets/ToolGroup.js";
|
|
5
7
|
import { ElephantTool, } from "./ElephantTool.js";
|
|
6
8
|
const inputSchema = {
|
|
7
9
|
query: z.string().min(1).describe("What to look for."),
|
|
8
10
|
agent: z
|
|
9
11
|
.string()
|
|
10
12
|
.optional()
|
|
11
|
-
.describe("Optional id of one of the team's Calypso agents, to search only what that agent can see. Not together with buckets."),
|
|
13
|
+
.describe("Optional id of one of the team's Calypso agents, to search only what that agent can see. Omit it (and `buckets`) to search every bucket the user has. Not together with buckets."),
|
|
12
14
|
buckets: z
|
|
13
15
|
.array(z.string())
|
|
14
16
|
.max(5)
|
|
@@ -44,6 +46,7 @@ export class SearchTool extends ElephantTool {
|
|
|
44
46
|
"Pass `buckets` (ids or slugs from elephant-list-buckets, up to 5) to search specific buckets, or `agent` to search what one Calypso agent can see — not both.",
|
|
45
47
|
].join("\n");
|
|
46
48
|
inputSchema = inputSchema;
|
|
49
|
+
group = ToolGroup.read;
|
|
47
50
|
annotations = {
|
|
48
51
|
readOnlyHint: true,
|
|
49
52
|
destructiveHint: false,
|
|
@@ -63,7 +66,18 @@ export class SearchTool extends ElephantTool {
|
|
|
63
66
|
});
|
|
64
67
|
return { text: emptyKnowledgeAnswer() };
|
|
65
68
|
}
|
|
66
|
-
|
|
69
|
+
// Neither an agent nor buckets: every bucket the user has, through the
|
|
70
|
+
// connector agent. An API that does not know it yet searches the default.
|
|
71
|
+
const everywhere = !args.agent?.trim() && !args.buckets?.length;
|
|
72
|
+
const result = everywhere
|
|
73
|
+
? await api.search
|
|
74
|
+
.search({ ...args, agent: CONNECTOR_AGENT_ID })
|
|
75
|
+
.catch((error) => {
|
|
76
|
+
if (!isUnknownAgent(error))
|
|
77
|
+
throw error;
|
|
78
|
+
return api.search.search(args);
|
|
79
|
+
})
|
|
80
|
+
: await api.search.search(args);
|
|
67
81
|
await log.event("info", "Knowledge search completed.", {
|
|
68
82
|
tool: this.name,
|
|
69
83
|
strategy: result.strategy,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@calypsohq/elephant-mcp-server",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
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",
|