@calypsohq/elephant-mcp-server 1.4.0 → 1.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -12
- package/dist/ElephantMcpServer.d.ts +45 -0
- package/dist/ElephantMcpServer.js +3 -26
- package/dist/auth/ApiKeyStrategy.d.ts +14 -0
- package/dist/auth/AuthChallenge.d.ts +27 -0
- package/dist/auth/AuthStrategy.d.ts +14 -0
- package/dist/auth/BearerAuthenticator.d.ts +17 -0
- package/dist/auth/Credential.d.ts +23 -0
- package/dist/auth/GrantScopes.d.ts +16 -0
- package/dist/auth/JwtGrantStrategy.d.ts +30 -0
- package/dist/auth/ProtectedResource.d.ts +21 -0
- package/dist/auth/ProtectedResource.js +1 -0
- package/dist/auth/TokenCipher.d.ts +13 -0
- package/dist/calypso/AgentCatalog.d.ts +41 -0
- package/dist/calypso/AgentCatalogLoader.d.ts +12 -0
- package/dist/calypso/AgentChoice.d.ts +25 -0
- package/dist/calypso/AgentResponses.d.ts +47 -0
- package/dist/calypso/AnswerDepth.d.ts +30 -0
- package/dist/calypso/AnswerDiagnostics.d.ts +23 -0
- package/dist/calypso/AnswerSources.d.ts +36 -0
- package/dist/calypso/BatchManifest.d.ts +29 -0
- package/dist/calypso/BucketDestination.d.ts +17 -0
- package/dist/calypso/BucketEdits.d.ts +41 -0
- package/dist/calypso/BucketSources.d.ts +28 -0
- package/dist/calypso/CalypsoApi.d.ts +28 -0
- package/dist/calypso/CalypsoApiClient.d.ts +29 -0
- package/dist/calypso/CalypsoApiConfig.d.ts +15 -0
- package/dist/calypso/KnowledgeBatches.d.ts +25 -0
- package/dist/calypso/KnowledgeBuckets.d.ts +18 -0
- package/dist/calypso/KnowledgeFiles.d.ts +45 -0
- package/dist/calypso/KnowledgeInventory.d.ts +36 -0
- package/dist/calypso/KnowledgeSearch.d.ts +52 -0
- package/dist/calypso/KnowledgeWebsites.d.ts +39 -0
- package/dist/calypso/UploadContent.d.ts +22 -0
- package/dist/calypso/apiErrors.d.ts +14 -0
- package/dist/calypso/bucketList.d.ts +48 -0
- package/dist/calypso/coerce.d.ts +14 -0
- package/dist/calypso/knowledgeTypes.d.ts +64 -0
- package/dist/calypso/polling.d.ts +14 -0
- package/dist/calypso/uploadBytes.d.ts +8 -0
- package/dist/hatching/HatchingCopy.d.ts +11 -0
- package/dist/http/HostedServer.d.ts +8 -0
- package/dist/http/HttpConfig.d.ts +40 -0
- package/dist/http/HttpConfig.js +11 -2
- package/dist/http/HttpServer.d.ts +36 -0
- package/dist/http/KnowledgeInventoryCache.d.ts +21 -0
- package/dist/http/McpEndpoint.d.ts +39 -0
- package/dist/http/McpEndpoint.js +9 -56
- package/dist/http/ModelCatalogCache.d.ts +20 -0
- package/dist/http/ProtectedResourceMetadata.d.ts +19 -0
- package/dist/http/ProtectedResourceMetadata.js +1 -1
- package/dist/http/RateLimiter.d.ts +25 -0
- package/dist/http/StatelessMcpEndpoint.d.ts +40 -0
- package/dist/http/StatelessMcpEndpoint.js +69 -0
- package/dist/index.d.ts +2 -0
- package/dist/kit.d.ts +35 -0
- package/dist/kit.js +31 -0
- package/dist/notices/PostBudget.d.ts +15 -0
- package/dist/notices/SlackUsageNotices.d.ts +22 -0
- package/dist/notices/UsageMessage.d.ts +22 -0
- package/dist/notices/UsageNotices.d.ts +25 -0
- package/dist/presets/PresetInstructions.d.ts +20 -0
- package/dist/presets/ToolGroup.d.ts +30 -0
- package/dist/presets/ToolGroup.js +4 -0
- package/dist/presets/ToolPreset.d.ts +80 -0
- package/dist/presets/ToolPreset.js +6 -1
- package/dist/presets/ToolPresetRegistry.d.ts +18 -0
- package/dist/stdio/LocalPresets.d.ts +3 -0
- package/dist/stdio/StdioOptions.d.ts +16 -0
- package/dist/stdio.d.ts +2 -0
- package/dist/tools/AddFileTool.d.ts +91 -0
- package/dist/tools/AddFilesTool.d.ts +110 -0
- package/dist/tools/AddWebsiteTool.d.ts +42 -0
- package/dist/tools/AskTool.d.ts +57 -0
- package/dist/tools/CreateBucketTool.d.ts +44 -0
- package/dist/tools/ElephantTool.d.ts +12 -0
- package/dist/tools/ElephantTool.js +6 -36
- package/dist/tools/ListBucketsTool.d.ts +34 -0
- package/dist/tools/McpTool.d.ts +56 -0
- package/dist/tools/McpTool.js +40 -0
- package/dist/tools/MoveSourcesTool.d.ts +30 -0
- package/dist/tools/OpenSourceTool.d.ts +28 -0
- package/dist/tools/RenameBucketTool.d.ts +30 -0
- package/dist/tools/SearchTool.d.ts +36 -0
- package/dist/tools/ToolContext.d.ts +20 -0
- package/dist/tools/ToolLogger.d.ts +17 -0
- package/dist/tools/ToolRegistrar.d.ts +22 -0
- package/dist/tools/ToolRegistrar.js +33 -0
- package/dist/tools/schemaParts.d.ts +38 -0
- package/dist/views/BatchView.d.ts +35 -0
- package/dist/views/BucketView.d.ts +34 -0
- package/dist/views/ErrorView.d.ts +6 -0
- package/dist/views/MoveView.d.ts +20 -0
- package/dist/views/SearchView.d.ts +36 -0
- package/dist/views/SourceView.d.ts +47 -0
- package/dist/views/WebsiteView.d.ts +20 -0
- package/dist/views/viewFields.d.ts +11 -0
- package/package.json +9 -1
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { CalypsoApiConfig } from "../calypso/CalypsoApiConfig.js";
|
|
2
|
+
import { type KnowledgeInventorySummary } from "../calypso/KnowledgeInventory.js";
|
|
3
|
+
export type InventoryLoader = (config: CalypsoApiConfig) => Promise<KnowledgeInventorySummary>;
|
|
4
|
+
/**
|
|
5
|
+
* Per-key cache of the team's knowledge inventory for the hosted server, the
|
|
6
|
+
* twin of ModelCatalogCache: `initialize` needs the inventory to choose its
|
|
7
|
+
* instructions, and without this every request would cost a bucket list.
|
|
8
|
+
* Short TTL — a team that just added its first source should see the hatch
|
|
9
|
+
* fade within seconds. A failed load resolves to null (unknown) and is not
|
|
10
|
+
* cached. Keyed by a hash so raw keys never sit in a map.
|
|
11
|
+
*/
|
|
12
|
+
export declare class KnowledgeInventoryCache {
|
|
13
|
+
private readonly ttlMs;
|
|
14
|
+
private readonly maxEntries;
|
|
15
|
+
private readonly load;
|
|
16
|
+
private readonly now;
|
|
17
|
+
private readonly entries;
|
|
18
|
+
constructor(ttlMs?: number, maxEntries?: number, load?: InventoryLoader, now?: () => number);
|
|
19
|
+
get(config: CalypsoApiConfig): Promise<KnowledgeInventorySummary | null>;
|
|
20
|
+
private evictOverflow;
|
|
21
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
+
import type { BearerAuthenticator } from "../auth/BearerAuthenticator.js";
|
|
3
|
+
import type { Credential } from "../auth/Credential.js";
|
|
4
|
+
import { type PackageInfo } from "../ElephantMcpServer.js";
|
|
5
|
+
import type { UsageNotices } from "../notices/UsageNotices.js";
|
|
6
|
+
import type { ToolPreset } from "../presets/ToolPreset.js";
|
|
7
|
+
import type { KnowledgeInventoryCache } from "./KnowledgeInventoryCache.js";
|
|
8
|
+
import type { ModelCatalogCache } from "./ModelCatalogCache.js";
|
|
9
|
+
import type { RateLimiter } from "./RateLimiter.js";
|
|
10
|
+
import { StatelessMcpEndpoint } from "./StatelessMcpEndpoint.js";
|
|
11
|
+
export type { EndpointOutcome } from "./StatelessMcpEndpoint.js";
|
|
12
|
+
export type McpEndpointOptions = {
|
|
13
|
+
readonly preset: ToolPreset;
|
|
14
|
+
readonly authenticator: BearerAuthenticator;
|
|
15
|
+
readonly catalogs: ModelCatalogCache;
|
|
16
|
+
/**
|
|
17
|
+
* The team's knowledge inventory, read only for presets that hatch. Absent
|
|
18
|
+
* (the HTTP tests), the inventory is unknown and nothing hatches.
|
|
19
|
+
*/
|
|
20
|
+
readonly inventories?: KnowledgeInventoryCache;
|
|
21
|
+
/** Shared by every endpoint, so a caller's budget spans both plugins. */
|
|
22
|
+
readonly limiter: RateLimiter;
|
|
23
|
+
readonly apiBaseUrl: string;
|
|
24
|
+
readonly packageInfo: PackageInfo;
|
|
25
|
+
/** Told after each tool call; absent (the HTTP tests), nobody is told. */
|
|
26
|
+
readonly notices?: UsageNotices;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* One Elephant preset served over stateless Streamable HTTP.
|
|
30
|
+
*
|
|
31
|
+
* The request handling is `StatelessMcpEndpoint`'s. This class adds the one
|
|
32
|
+
* Elephant step: a server bound to the caller's API key, with their agents
|
|
33
|
+
* and, for a preset that hatches, what their team has saved.
|
|
34
|
+
*/
|
|
35
|
+
export declare class McpEndpoint extends StatelessMcpEndpoint<ToolPreset> {
|
|
36
|
+
private readonly options;
|
|
37
|
+
constructor(options: McpEndpointOptions);
|
|
38
|
+
protected serverFor(credential: Credential): Promise<McpServer>;
|
|
39
|
+
}
|
package/dist/http/McpEndpoint.js
CHANGED
|
@@ -1,36 +1,19 @@
|
|
|
1
|
-
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
2
|
-
import { AuthChallenge } from "../auth/AuthChallenge.js";
|
|
3
1
|
import { createElephantMcpServer, } from "../ElephantMcpServer.js";
|
|
2
|
+
import { StatelessMcpEndpoint } from "./StatelessMcpEndpoint.js";
|
|
4
3
|
/**
|
|
5
|
-
* One preset served over stateless Streamable HTTP.
|
|
4
|
+
* One Elephant preset served over stateless Streamable HTTP.
|
|
6
5
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
6
|
+
* The request handling is `StatelessMcpEndpoint`'s. This class adds the one
|
|
7
|
+
* Elephant step: a server bound to the caller's API key, with their agents
|
|
8
|
+
* and, for a preset that hatches, what their team has saved.
|
|
10
9
|
*/
|
|
11
|
-
export class McpEndpoint {
|
|
10
|
+
export class McpEndpoint extends StatelessMcpEndpoint {
|
|
12
11
|
options;
|
|
13
|
-
preset;
|
|
14
12
|
constructor(options) {
|
|
13
|
+
super(options);
|
|
15
14
|
this.options = options;
|
|
16
|
-
this.preset = options.preset;
|
|
17
15
|
}
|
|
18
|
-
|
|
19
|
-
async handle(req, res, body) {
|
|
20
|
-
const result = await this.options.authenticator.authenticate(req.headers.authorization, this.preset);
|
|
21
|
-
if (result instanceof AuthChallenge) {
|
|
22
|
-
McpEndpoint.refuse(res, result);
|
|
23
|
-
return { refused: result.logReason() };
|
|
24
|
-
}
|
|
25
|
-
const decision = this.options.limiter.take(result.principal);
|
|
26
|
-
if (!decision.allowed) {
|
|
27
|
-
McpEndpoint.throttle(res, decision.retryAfterSeconds);
|
|
28
|
-
return { principal: result.principal };
|
|
29
|
-
}
|
|
30
|
-
await this.serve(req, res, body, result);
|
|
31
|
-
return { principal: result.principal };
|
|
32
|
-
}
|
|
33
|
-
async serve(req, res, body, credential) {
|
|
16
|
+
async serverFor(credential) {
|
|
34
17
|
const api = {
|
|
35
18
|
apiKey: credential.apiKey,
|
|
36
19
|
apiBaseUrl: this.options.apiBaseUrl,
|
|
@@ -41,7 +24,7 @@ export class McpEndpoint {
|
|
|
41
24
|
? this.options.inventories.get(api)
|
|
42
25
|
: null,
|
|
43
26
|
]);
|
|
44
|
-
|
|
27
|
+
return createElephantMcpServer({
|
|
45
28
|
preset: this.preset,
|
|
46
29
|
api,
|
|
47
30
|
agents,
|
|
@@ -52,35 +35,5 @@ export class McpEndpoint {
|
|
|
52
35
|
grant: credential.grant,
|
|
53
36
|
notices: this.options.notices,
|
|
54
37
|
});
|
|
55
|
-
const transport = new StreamableHTTPServerTransport({
|
|
56
|
-
sessionIdGenerator: undefined,
|
|
57
|
-
});
|
|
58
|
-
res.on("close", () => {
|
|
59
|
-
void transport.close();
|
|
60
|
-
void server.close();
|
|
61
|
-
});
|
|
62
|
-
await server.connect(transport);
|
|
63
|
-
await transport.handleRequest(req, res, body);
|
|
64
|
-
}
|
|
65
|
-
static refuse(res, challenge) {
|
|
66
|
-
res.writeHead(challenge.status, {
|
|
67
|
-
"Content-Type": "application/json",
|
|
68
|
-
"WWW-Authenticate": challenge.header(),
|
|
69
|
-
});
|
|
70
|
-
res.end(JSON.stringify(challenge.body()));
|
|
71
|
-
}
|
|
72
|
-
static throttle(res, retryAfterSeconds) {
|
|
73
|
-
res.writeHead(429, {
|
|
74
|
-
"Content-Type": "application/json",
|
|
75
|
-
"Retry-After": String(retryAfterSeconds),
|
|
76
|
-
});
|
|
77
|
-
res.end(JSON.stringify({
|
|
78
|
-
jsonrpc: "2.0",
|
|
79
|
-
error: {
|
|
80
|
-
code: -32000,
|
|
81
|
-
message: `Too many requests. Try again in ${retryAfterSeconds} s.`,
|
|
82
|
-
},
|
|
83
|
-
id: null,
|
|
84
|
-
}));
|
|
85
38
|
}
|
|
86
39
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { AgentCatalogSnapshot } from "../calypso/AgentCatalog.js";
|
|
2
|
+
import type { CalypsoApiConfig } from "../calypso/CalypsoApiConfig.js";
|
|
3
|
+
export type CatalogLoader = (config: CalypsoApiConfig) => Promise<AgentCatalogSnapshot>;
|
|
4
|
+
/**
|
|
5
|
+
* Per-key cache of the team's agent catalog for the hosted server.
|
|
6
|
+
*
|
|
7
|
+
* The stateless HTTP server builds a fresh McpServer for every request, and
|
|
8
|
+
* each one needs the catalog; without this, every tools/list would cost an
|
|
9
|
+
* Calypso API round trip. Keyed by a hash so raw keys never sit in a map.
|
|
10
|
+
*/
|
|
11
|
+
export declare class ModelCatalogCache {
|
|
12
|
+
private readonly ttlMs;
|
|
13
|
+
private readonly maxEntries;
|
|
14
|
+
private readonly load;
|
|
15
|
+
private readonly now;
|
|
16
|
+
private readonly entries;
|
|
17
|
+
constructor(ttlMs?: number, maxEntries?: number, load?: CatalogLoader, now?: () => number);
|
|
18
|
+
get(config: CalypsoApiConfig): Promise<AgentCatalogSnapshot>;
|
|
19
|
+
private evictOverflow;
|
|
20
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ProtectedResource } from "../auth/ProtectedResource.js";
|
|
2
|
+
/**
|
|
3
|
+
* RFC 9728 protected-resource metadata, one document per OAuth preset.
|
|
4
|
+
*
|
|
5
|
+
* ChatGPT reads the URL from the 401 challenge, fetches this document, and
|
|
6
|
+
* learns which authorization server issues tokens for the preset. A preset
|
|
7
|
+
* missing here would 401 forever, because discovery would have nowhere to go.
|
|
8
|
+
*/
|
|
9
|
+
export declare class ProtectedResourceMetadata {
|
|
10
|
+
private readonly publicBaseUrl;
|
|
11
|
+
private readonly issuer;
|
|
12
|
+
constructor(publicBaseUrl: string, issuer: string);
|
|
13
|
+
/** Path this preset's document is served on. */
|
|
14
|
+
static pathFor(preset: ProtectedResource): string;
|
|
15
|
+
/** Whether the preset is OAuth-protected (and so has a document). */
|
|
16
|
+
static supports(preset: ProtectedResource): boolean;
|
|
17
|
+
urlFor(preset: ProtectedResource): string | undefined;
|
|
18
|
+
documentFor(preset: ProtectedResource): Record<string, unknown>;
|
|
19
|
+
}
|
|
@@ -34,7 +34,7 @@ export class ProtectedResourceMetadata {
|
|
|
34
34
|
scopes_supported: preset.scopes(),
|
|
35
35
|
bearer_methods_supported: ["header"],
|
|
36
36
|
resource_name: preset.displayName,
|
|
37
|
-
resource_documentation: DOCUMENTATION_URL,
|
|
37
|
+
resource_documentation: preset.documentationUrl ?? DOCUMENTATION_URL,
|
|
38
38
|
};
|
|
39
39
|
}
|
|
40
40
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export type RateLimitDecision = {
|
|
2
|
+
allowed: true;
|
|
3
|
+
} | {
|
|
4
|
+
allowed: false;
|
|
5
|
+
retryAfterSeconds: number;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Token-bucket limit per caller, held in memory.
|
|
9
|
+
*
|
|
10
|
+
* The Calypso API has its own limits and billing; this one exists so a single
|
|
11
|
+
* runaway client cannot occupy the hosted server itself. Each key gets
|
|
12
|
+
* `capacity` requests at once and refills evenly over a minute. State is
|
|
13
|
+
* per-process, which is the right scope for protecting a process.
|
|
14
|
+
*/
|
|
15
|
+
export declare class RateLimiter {
|
|
16
|
+
private readonly capacity;
|
|
17
|
+
private readonly maxKeys;
|
|
18
|
+
private readonly now;
|
|
19
|
+
private readonly buckets;
|
|
20
|
+
private readonly refillPerMs;
|
|
21
|
+
constructor(capacity: number, maxKeys?: number, now?: () => number);
|
|
22
|
+
take(key: string): RateLimitDecision;
|
|
23
|
+
private refilled;
|
|
24
|
+
private evictOverflow;
|
|
25
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
+
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
3
|
+
import type { BearerAuthenticator } from "../auth/BearerAuthenticator.js";
|
|
4
|
+
import type { Credential } from "../auth/Credential.js";
|
|
5
|
+
import type { ProtectedResource } from "../auth/ProtectedResource.js";
|
|
6
|
+
import type { RateLimiter } from "./RateLimiter.js";
|
|
7
|
+
export type StatelessMcpEndpointOptions<Resource extends ProtectedResource = ProtectedResource> = {
|
|
8
|
+
readonly preset: Resource;
|
|
9
|
+
readonly authenticator: BearerAuthenticator;
|
|
10
|
+
/** Shared by every endpoint of a server, so a caller's budget spans them. */
|
|
11
|
+
readonly limiter: RateLimiter;
|
|
12
|
+
};
|
|
13
|
+
/** What one request to an endpoint came to: a served principal, or a refusal. */
|
|
14
|
+
export type EndpointOutcome = {
|
|
15
|
+
readonly principal?: string;
|
|
16
|
+
/** Why authentication refused the request; see AuthChallenge.logReason. */
|
|
17
|
+
readonly refused?: string;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* One protected resource served over stateless Streamable HTTP.
|
|
21
|
+
*
|
|
22
|
+
* Every POST is authenticated, checked against the caller's request budget,
|
|
23
|
+
* then handled by a fresh MCP server built for that caller alone. Nothing
|
|
24
|
+
* survives the request, so any instance can serve any call. What the server
|
|
25
|
+
* holds is the subclass's to say: `serverFor` is the one step that differs
|
|
26
|
+
* between products.
|
|
27
|
+
*/
|
|
28
|
+
export declare abstract class StatelessMcpEndpoint<Resource extends ProtectedResource = ProtectedResource> {
|
|
29
|
+
readonly preset: Resource;
|
|
30
|
+
private readonly authenticator;
|
|
31
|
+
private readonly limiter;
|
|
32
|
+
constructor(options: StatelessMcpEndpointOptions<Resource>);
|
|
33
|
+
/** The MCP server for one authenticated caller, built for this request. */
|
|
34
|
+
protected abstract serverFor(credential: Credential): Promise<McpServer>;
|
|
35
|
+
/** Who was served, or why the request was refused. */
|
|
36
|
+
handle(req: IncomingMessage, res: ServerResponse, body: unknown): Promise<EndpointOutcome>;
|
|
37
|
+
private serve;
|
|
38
|
+
private static refuse;
|
|
39
|
+
private static throttle;
|
|
40
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
2
|
+
import { AuthChallenge } from "../auth/AuthChallenge.js";
|
|
3
|
+
/**
|
|
4
|
+
* One protected resource served over stateless Streamable HTTP.
|
|
5
|
+
*
|
|
6
|
+
* Every POST is authenticated, checked against the caller's request budget,
|
|
7
|
+
* then handled by a fresh MCP server built for that caller alone. Nothing
|
|
8
|
+
* survives the request, so any instance can serve any call. What the server
|
|
9
|
+
* holds is the subclass's to say: `serverFor` is the one step that differs
|
|
10
|
+
* between products.
|
|
11
|
+
*/
|
|
12
|
+
export class StatelessMcpEndpoint {
|
|
13
|
+
preset;
|
|
14
|
+
authenticator;
|
|
15
|
+
limiter;
|
|
16
|
+
constructor(options) {
|
|
17
|
+
this.preset = options.preset;
|
|
18
|
+
this.authenticator = options.authenticator;
|
|
19
|
+
this.limiter = options.limiter;
|
|
20
|
+
}
|
|
21
|
+
/** Who was served, or why the request was refused. */
|
|
22
|
+
async handle(req, res, body) {
|
|
23
|
+
const result = await this.authenticator.authenticate(req.headers.authorization, this.preset);
|
|
24
|
+
if (result instanceof AuthChallenge) {
|
|
25
|
+
StatelessMcpEndpoint.refuse(res, result);
|
|
26
|
+
return { refused: result.logReason() };
|
|
27
|
+
}
|
|
28
|
+
const decision = this.limiter.take(result.principal);
|
|
29
|
+
if (!decision.allowed) {
|
|
30
|
+
StatelessMcpEndpoint.throttle(res, decision.retryAfterSeconds);
|
|
31
|
+
return { principal: result.principal };
|
|
32
|
+
}
|
|
33
|
+
await this.serve(req, res, body, result);
|
|
34
|
+
return { principal: result.principal };
|
|
35
|
+
}
|
|
36
|
+
async serve(req, res, body, credential) {
|
|
37
|
+
const server = await this.serverFor(credential);
|
|
38
|
+
const transport = new StreamableHTTPServerTransport({
|
|
39
|
+
sessionIdGenerator: undefined,
|
|
40
|
+
});
|
|
41
|
+
res.on("close", () => {
|
|
42
|
+
void transport.close();
|
|
43
|
+
void server.close();
|
|
44
|
+
});
|
|
45
|
+
await server.connect(transport);
|
|
46
|
+
await transport.handleRequest(req, res, body);
|
|
47
|
+
}
|
|
48
|
+
static refuse(res, challenge) {
|
|
49
|
+
res.writeHead(challenge.status, {
|
|
50
|
+
"Content-Type": "application/json",
|
|
51
|
+
"WWW-Authenticate": challenge.header(),
|
|
52
|
+
});
|
|
53
|
+
res.end(JSON.stringify(challenge.body()));
|
|
54
|
+
}
|
|
55
|
+
static throttle(res, retryAfterSeconds) {
|
|
56
|
+
res.writeHead(429, {
|
|
57
|
+
"Content-Type": "application/json",
|
|
58
|
+
"Retry-After": String(retryAfterSeconds),
|
|
59
|
+
});
|
|
60
|
+
res.end(JSON.stringify({
|
|
61
|
+
jsonrpc: "2.0",
|
|
62
|
+
error: {
|
|
63
|
+
code: -32000,
|
|
64
|
+
message: `Too many requests. Try again in ${retryAfterSeconds} s.`,
|
|
65
|
+
},
|
|
66
|
+
id: null,
|
|
67
|
+
}));
|
|
68
|
+
}
|
|
69
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/kit.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The parts of this server that are not about Elephant.
|
|
3
|
+
*
|
|
4
|
+
* Bearer authentication against Calypso's authorization server, a stateless
|
|
5
|
+
* Streamable HTTP endpoint, the tool, group and preset base classes, and the
|
|
6
|
+
* small helpers they lean on. Another Calypso MCP server imports these from
|
|
7
|
+
* `@calypsohq/elephant-mcp-server/kit` and adds only its own tools.
|
|
8
|
+
*
|
|
9
|
+
* Nothing here knows about knowledge, buckets or agents; those stay in the
|
|
10
|
+
* Elephant server proper.
|
|
11
|
+
*/
|
|
12
|
+
export { ApiKeyStrategy } from "./auth/ApiKeyStrategy.js";
|
|
13
|
+
export { AuthChallenge } from "./auth/AuthChallenge.js";
|
|
14
|
+
export type { AuthStrategy } from "./auth/AuthStrategy.js";
|
|
15
|
+
export { BearerAuthenticator } from "./auth/BearerAuthenticator.js";
|
|
16
|
+
export type { Credential, GrantFacts } from "./auth/Credential.js";
|
|
17
|
+
export { GrantScopes } from "./auth/GrantScopes.js";
|
|
18
|
+
export { JwtGrantStrategy, type JwtGrantStrategyOptions, } from "./auth/JwtGrantStrategy.js";
|
|
19
|
+
export type { ProtectedResource } from "./auth/ProtectedResource.js";
|
|
20
|
+
export { TokenCipher } from "./auth/TokenCipher.js";
|
|
21
|
+
export { CalypsoApiClient, CalypsoApiError, type CalypsoRequest, } from "./calypso/CalypsoApiClient.js";
|
|
22
|
+
export type { CalypsoApiConfig } from "./calypso/CalypsoApiConfig.js";
|
|
23
|
+
export { asRecord, numberOrUndefined, trimmedString, } from "./calypso/coerce.js";
|
|
24
|
+
export { HttpConfig } from "./http/HttpConfig.js";
|
|
25
|
+
export { HttpServer, type HttpServerOptions } from "./http/HttpServer.js";
|
|
26
|
+
export { ProtectedResourceMetadata } from "./http/ProtectedResourceMetadata.js";
|
|
27
|
+
export { RateLimiter } from "./http/RateLimiter.js";
|
|
28
|
+
export { type EndpointOutcome, StatelessMcpEndpoint, type StatelessMcpEndpointOptions, } from "./http/StatelessMcpEndpoint.js";
|
|
29
|
+
export { ToolGroup } from "./presets/ToolGroup.js";
|
|
30
|
+
export { type InstructionComposer, type PresetTool, ToolPreset, type ToolPresetOptions, } from "./presets/ToolPreset.js";
|
|
31
|
+
export { type InputShape, McpTool, type ToolArgs, type ToolOutput, type ToolRuntime, } from "./tools/McpTool.js";
|
|
32
|
+
export { ToolLogger } from "./tools/ToolLogger.js";
|
|
33
|
+
export { type ToolCallReport, ToolRegistrar } from "./tools/ToolRegistrar.js";
|
|
34
|
+
export { ErrorView } from "./views/ErrorView.js";
|
|
35
|
+
export { plainText, present } from "./views/viewFields.js";
|
package/dist/kit.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The parts of this server that are not about Elephant.
|
|
3
|
+
*
|
|
4
|
+
* Bearer authentication against Calypso's authorization server, a stateless
|
|
5
|
+
* Streamable HTTP endpoint, the tool, group and preset base classes, and the
|
|
6
|
+
* small helpers they lean on. Another Calypso MCP server imports these from
|
|
7
|
+
* `@calypsohq/elephant-mcp-server/kit` and adds only its own tools.
|
|
8
|
+
*
|
|
9
|
+
* Nothing here knows about knowledge, buckets or agents; those stay in the
|
|
10
|
+
* Elephant server proper.
|
|
11
|
+
*/
|
|
12
|
+
export { ApiKeyStrategy } from "./auth/ApiKeyStrategy.js";
|
|
13
|
+
export { AuthChallenge } from "./auth/AuthChallenge.js";
|
|
14
|
+
export { BearerAuthenticator } from "./auth/BearerAuthenticator.js";
|
|
15
|
+
export { GrantScopes } from "./auth/GrantScopes.js";
|
|
16
|
+
export { JwtGrantStrategy, } from "./auth/JwtGrantStrategy.js";
|
|
17
|
+
export { TokenCipher } from "./auth/TokenCipher.js";
|
|
18
|
+
export { CalypsoApiClient, CalypsoApiError, } from "./calypso/CalypsoApiClient.js";
|
|
19
|
+
export { asRecord, numberOrUndefined, trimmedString, } from "./calypso/coerce.js";
|
|
20
|
+
export { HttpConfig } from "./http/HttpConfig.js";
|
|
21
|
+
export { HttpServer } from "./http/HttpServer.js";
|
|
22
|
+
export { ProtectedResourceMetadata } from "./http/ProtectedResourceMetadata.js";
|
|
23
|
+
export { RateLimiter } from "./http/RateLimiter.js";
|
|
24
|
+
export { StatelessMcpEndpoint, } from "./http/StatelessMcpEndpoint.js";
|
|
25
|
+
export { ToolGroup } from "./presets/ToolGroup.js";
|
|
26
|
+
export { ToolPreset, } from "./presets/ToolPreset.js";
|
|
27
|
+
export { McpTool, } from "./tools/McpTool.js";
|
|
28
|
+
export { ToolLogger } from "./tools/ToolLogger.js";
|
|
29
|
+
export { ToolRegistrar } from "./tools/ToolRegistrar.js";
|
|
30
|
+
export { ErrorView } from "./views/ErrorView.js";
|
|
31
|
+
export { plainText, present } from "./views/viewFields.js";
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How many notices one process may post per minute. Slack webhooks accept
|
|
3
|
+
* about one message a second; past the budget a notice is dropped and
|
|
4
|
+
* counted, and the next one that gets through says how many were lost.
|
|
5
|
+
*/
|
|
6
|
+
export declare class PostBudget {
|
|
7
|
+
private readonly perMinute;
|
|
8
|
+
private readonly clock;
|
|
9
|
+
private windowStart;
|
|
10
|
+
private posted;
|
|
11
|
+
private dropped;
|
|
12
|
+
constructor(perMinute: number, clock?: () => number);
|
|
13
|
+
/** Null when this post must be dropped; otherwise how many were dropped since the last one that got through. */
|
|
14
|
+
take(): number | null;
|
|
15
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { PostBudget } from "./PostBudget.js";
|
|
2
|
+
import { type ServerIdentity, type SlackMessage } from "./UsageMessage.js";
|
|
3
|
+
import type { ToolCall, UsageNotices } from "./UsageNotices.js";
|
|
4
|
+
export declare const USAGE_POSTS_PER_MINUTE = 60;
|
|
5
|
+
export declare const USAGE_POST_TIMEOUT_MS = 3000;
|
|
6
|
+
export type UsagePost = (webhookUrl: string, message: SlackMessage, timeoutMs: number) => Promise<unknown>;
|
|
7
|
+
/**
|
|
8
|
+
* Posts each noticed tool call to a Slack incoming webhook. The server is a
|
|
9
|
+
* long-lived process, so the post runs after the tool call has returned and
|
|
10
|
+
* nothing waits for it; a failure is one warning line in the server log.
|
|
11
|
+
*/
|
|
12
|
+
export declare class SlackUsageNotices implements UsageNotices {
|
|
13
|
+
private readonly webhookUrl;
|
|
14
|
+
private readonly server;
|
|
15
|
+
private readonly budget;
|
|
16
|
+
private readonly post;
|
|
17
|
+
private readonly warn;
|
|
18
|
+
constructor(webhookUrl: string, server: ServerIdentity, budget?: PostBudget, post?: UsagePost, warn?: (line: string) => void);
|
|
19
|
+
toolCalled(call: ToolCall): void;
|
|
20
|
+
/** Resolves once the post settled; tests await it, the server never does. */
|
|
21
|
+
send(call: ToolCall): Promise<void>;
|
|
22
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ToolCall } from "./UsageNotices.js";
|
|
2
|
+
/**
|
|
3
|
+
* The tools worth a line in the operator channel: the ones that carry a
|
|
4
|
+
* question or change knowledge. The two inventory tools clients call on
|
|
5
|
+
* their own (`elephant-list-buckets`, `elephant-open-source`) stay quiet.
|
|
6
|
+
*/
|
|
7
|
+
export declare const NOTICED_TOOLS: ReadonlySet<string>;
|
|
8
|
+
export type SlackMessage = {
|
|
9
|
+
text: string;
|
|
10
|
+
blocks: Record<string, unknown>[];
|
|
11
|
+
};
|
|
12
|
+
export type ServerIdentity = {
|
|
13
|
+
readonly version: string;
|
|
14
|
+
readonly publicBaseUrl: string;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Builds the Slack post for one call, or null when the tool is not noticed.
|
|
18
|
+
* Only the caller's own input and a status leave the server: nothing from
|
|
19
|
+
* the tool's result or from the Calypso API is included (the same rule the
|
|
20
|
+
* tools' views follow).
|
|
21
|
+
*/
|
|
22
|
+
export declare function buildUsageMessage(call: ToolCall, server: ServerIdentity, dropped?: number): SlackMessage | null;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { GrantFacts } from "../auth/Credential.js";
|
|
2
|
+
/** One finished tool call, as the operator channel hears about it. */
|
|
3
|
+
export type ToolCall = {
|
|
4
|
+
readonly tool: string;
|
|
5
|
+
/** The caller's own input, as the tool received it. */
|
|
6
|
+
readonly args: unknown;
|
|
7
|
+
readonly outcome: "ok" | "error";
|
|
8
|
+
readonly durationMs: number;
|
|
9
|
+
/** `grant:<gid>` or `key:sk-…abcd`. */
|
|
10
|
+
readonly principal: string;
|
|
11
|
+
readonly scopes: readonly string[];
|
|
12
|
+
readonly grant?: GrantFacts;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Told after every tool call on the hosted server. Fire-and-forget: a
|
|
16
|
+
* notice runs after the tool has its result and can neither delay nor fail
|
|
17
|
+
* it, so the method returns nothing to wait for.
|
|
18
|
+
*/
|
|
19
|
+
export interface UsageNotices {
|
|
20
|
+
toolCalled(call: ToolCall): void;
|
|
21
|
+
}
|
|
22
|
+
/** The local (stdio) server and a hosted server without a webhook. */
|
|
23
|
+
export declare class NoUsageNotices implements UsageNotices {
|
|
24
|
+
toolCalled(): void;
|
|
25
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { GrantScopes } from "../auth/GrantScopes.js";
|
|
2
|
+
import type { ToolGroup } from "./ToolGroup.js";
|
|
3
|
+
/**
|
|
4
|
+
* The session instructions for one caller: what the server is, then what this
|
|
5
|
+
* connection can do and what it cannot.
|
|
6
|
+
*
|
|
7
|
+
* Composed from the groups the caller holds, so a read-only session is never
|
|
8
|
+
* told it can save a file, and a session that can is told to confirm first.
|
|
9
|
+
* What it cannot do comes with the way to get it, because the model is the
|
|
10
|
+
* only one who can tell the user.
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Said to every caller, whatever they allowed. A model asked to delete tends
|
|
14
|
+
* to go looking for the thing first; looking is a tool call, and the honest
|
|
15
|
+
* answer needs none.
|
|
16
|
+
*/
|
|
17
|
+
export declare 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.";
|
|
18
|
+
export declare const PresetInstructions: {
|
|
19
|
+
compose(about: string, groups: readonly ToolGroup[], grant: GrantScopes): string;
|
|
20
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
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 declare class ToolGroup {
|
|
9
|
+
/** Answering and searching: never changes anything. */
|
|
10
|
+
static readonly read: ToolGroup;
|
|
11
|
+
/** Saving new sources and creating buckets. */
|
|
12
|
+
static readonly add: ToolGroup;
|
|
13
|
+
/** Renaming buckets and moving sources between them. Deletes nothing. */
|
|
14
|
+
static readonly organise: ToolGroup;
|
|
15
|
+
readonly id: string;
|
|
16
|
+
/** OAuth scope a grant must carry for this group's tools. */
|
|
17
|
+
readonly scope: string;
|
|
18
|
+
/** Completes "It can …" for a caller who has the group. */
|
|
19
|
+
readonly can: string;
|
|
20
|
+
/** Completes "It cannot …" for a caller who does not. */
|
|
21
|
+
readonly cannot: string;
|
|
22
|
+
/** A group for another server's tools; Elephant's own are the statics above. */
|
|
23
|
+
static define(options: {
|
|
24
|
+
id: string;
|
|
25
|
+
scope: string;
|
|
26
|
+
can: string;
|
|
27
|
+
cannot: string;
|
|
28
|
+
}): ToolGroup;
|
|
29
|
+
private constructor();
|
|
30
|
+
}
|
|
@@ -34,6 +34,10 @@ export class ToolGroup {
|
|
|
34
34
|
can;
|
|
35
35
|
/** Completes "It cannot …" for a caller who does not. */
|
|
36
36
|
cannot;
|
|
37
|
+
/** A group for another server's tools; Elephant's own are the statics above. */
|
|
38
|
+
static define(options) {
|
|
39
|
+
return new ToolGroup(options);
|
|
40
|
+
}
|
|
37
41
|
constructor(options) {
|
|
38
42
|
this.id = options.id;
|
|
39
43
|
this.scope = options.scope;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { GrantScopes } from "../auth/GrantScopes.js";
|
|
2
|
+
import type { ProtectedResource } from "../auth/ProtectedResource.js";
|
|
3
|
+
import type { ElephantTool } from "../tools/ElephantTool.js";
|
|
4
|
+
import type { ToolGroup } from "./ToolGroup.js";
|
|
5
|
+
/**
|
|
6
|
+
* Words one caller's session instructions: what the server is, then what the
|
|
7
|
+
* groups they hold let this connection do.
|
|
8
|
+
*/
|
|
9
|
+
export type InstructionComposer = (about: string, groups: readonly ToolGroup[], grant: GrantScopes) => string;
|
|
10
|
+
/** The least a preset asks of a tool: a name, and the group that unlocks it. */
|
|
11
|
+
export type PresetTool = {
|
|
12
|
+
readonly name: string;
|
|
13
|
+
readonly group: ToolGroup;
|
|
14
|
+
};
|
|
15
|
+
export type ToolPresetOptions<Tool extends PresetTool = ElephantTool> = {
|
|
16
|
+
/** Stable identifier, e.g. `elephant`. */
|
|
17
|
+
readonly id: string;
|
|
18
|
+
/** HTTP path the preset is served on, e.g. `/elephant/mcp`. */
|
|
19
|
+
readonly path: string;
|
|
20
|
+
/** Name returned in `initialize.serverInfo.name`. */
|
|
21
|
+
readonly serverName: string;
|
|
22
|
+
/** Human name shown on consent and in protected-resource metadata. */
|
|
23
|
+
readonly displayName: string;
|
|
24
|
+
/**
|
|
25
|
+
* What the server is, in a sentence or two. What a caller can and cannot
|
|
26
|
+
* do with it is added per caller; see `instructionsFor`.
|
|
27
|
+
*/
|
|
28
|
+
readonly instructions: string;
|
|
29
|
+
/**
|
|
30
|
+
* OAuth scope a bearer token must carry to connect at all. Further tools
|
|
31
|
+
* are unlocked by the scopes of their groups.
|
|
32
|
+
*/
|
|
33
|
+
readonly scope: string;
|
|
34
|
+
/** The tools this preset publishes, in `tools/list` order. */
|
|
35
|
+
readonly tools: readonly Tool[];
|
|
36
|
+
/** Where the product is documented; see ProtectedResource. */
|
|
37
|
+
readonly documentationUrl?: string;
|
|
38
|
+
/**
|
|
39
|
+
* How a caller's instructions are worded. Elephant's wording
|
|
40
|
+
* (`PresetInstructions`) unless another product brings its own.
|
|
41
|
+
*/
|
|
42
|
+
readonly composeInstructions?: InstructionComposer;
|
|
43
|
+
/**
|
|
44
|
+
* The preset answers questions, so it hatches: empty-team instructions,
|
|
45
|
+
* the `elephant-get-started` prompt, and the hatch card in place of an
|
|
46
|
+
* answer when there is nothing to answer from. Off for Builder.
|
|
47
|
+
*/
|
|
48
|
+
readonly hatches?: boolean;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* One published plugin surface: its endpoint, its scope, and its tools.
|
|
52
|
+
*
|
|
53
|
+
* The preset is the single answer to "what does this endpoint offer", read by
|
|
54
|
+
* `tools/list`, the protected-resource metadata and the smoke probe alike.
|
|
55
|
+
*/
|
|
56
|
+
export declare class ToolPreset<Tool extends PresetTool = ElephantTool> implements ProtectedResource {
|
|
57
|
+
readonly id: string;
|
|
58
|
+
readonly path: string;
|
|
59
|
+
readonly serverName: string;
|
|
60
|
+
readonly displayName: string;
|
|
61
|
+
readonly instructions: string;
|
|
62
|
+
readonly scope: string;
|
|
63
|
+
readonly tools: readonly Tool[];
|
|
64
|
+
readonly hatches: boolean;
|
|
65
|
+
readonly documentationUrl?: string;
|
|
66
|
+
private readonly composeInstructions;
|
|
67
|
+
constructor(options: ToolPresetOptions<Tool>);
|
|
68
|
+
toolNames(): string[];
|
|
69
|
+
/** The groups this preset's tools belong to, in first-seen order. */
|
|
70
|
+
groups(): ToolGroup[];
|
|
71
|
+
/** Every OAuth scope a grant for this preset may carry. */
|
|
72
|
+
scopes(): string[];
|
|
73
|
+
/** The tools one caller is offered: those of the groups they agreed to. */
|
|
74
|
+
toolsFor(grant: GrantScopes): Tool[];
|
|
75
|
+
/** `initialize.instructions` for one caller. */
|
|
76
|
+
instructionsFor(grant: GrantScopes): string;
|
|
77
|
+
/** Absolute resource URL (`aud`) for this preset under a public base URL. */
|
|
78
|
+
resourceUrl(publicBaseUrl: string): string;
|
|
79
|
+
private static assertUniqueNames;
|
|
80
|
+
}
|