@calypsohq/elephant-mcp-server 1.3.3 → 1.5.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 +16 -0
- package/dist/ElephantMcpServer.d.ts +45 -0
- package/dist/ElephantMcpServer.js +14 -13
- 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/JwtGrantStrategy.js +11 -1
- 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/HostedServer.js +10 -0
- package/dist/http/HttpConfig.d.ts +40 -0
- package/dist/http/HttpConfig.js +15 -3
- 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 +11 -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/PostBudget.js +32 -0
- package/dist/notices/SlackUsageNotices.d.ts +22 -0
- package/dist/notices/SlackUsageNotices.js +57 -0
- package/dist/notices/UsageMessage.d.ts +22 -0
- package/dist/notices/UsageMessage.js +245 -0
- package/dist/notices/UsageNotices.d.ts +25 -0
- package/dist/notices/UsageNotices.js +4 -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,36 @@
|
|
|
1
|
+
import { type IncomingMessage, type Server, type ServerResponse } from "node:http";
|
|
2
|
+
import { ProtectedResourceMetadata } from "./ProtectedResourceMetadata.js";
|
|
3
|
+
import type { StatelessMcpEndpoint } from "./StatelessMcpEndpoint.js";
|
|
4
|
+
export declare const OPENAI_CHALLENGE_PATH = "/.well-known/openai-apps-challenge";
|
|
5
|
+
export type HttpServerOptions = {
|
|
6
|
+
endpoints: readonly StatelessMcpEndpoint[];
|
|
7
|
+
metadata: ProtectedResourceMetadata;
|
|
8
|
+
maxBodyBytes: number;
|
|
9
|
+
version: string;
|
|
10
|
+
/**
|
|
11
|
+
* The token OpenAI's plugin portal gave for this host, served as plain
|
|
12
|
+
* text at `/.well-known/openai-apps-challenge` to prove the domain is ours.
|
|
13
|
+
* Public by design; unset, the path answers 404.
|
|
14
|
+
*/
|
|
15
|
+
openaiChallengeToken?: string;
|
|
16
|
+
log?: (line: Record<string, unknown>) => void;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* The hosted Calypso MCP server: preset endpoints, their protected-resource
|
|
20
|
+
* metadata, and a health check, on plain node:http.
|
|
21
|
+
*
|
|
22
|
+
* Streamable HTTP is POST-only here: no SSE resumption and no server-side
|
|
23
|
+
* sessions, so GET and DELETE on an endpoint answer 405.
|
|
24
|
+
*/
|
|
25
|
+
export declare class HttpServer {
|
|
26
|
+
private readonly options;
|
|
27
|
+
private readonly endpointsByPath;
|
|
28
|
+
private readonly metadataByPath;
|
|
29
|
+
constructor(options: HttpServerOptions);
|
|
30
|
+
createNodeServer(): Server;
|
|
31
|
+
route(req: IncomingMessage, res: ServerResponse): Promise<void>;
|
|
32
|
+
private dispatch;
|
|
33
|
+
private readJson;
|
|
34
|
+
private json;
|
|
35
|
+
private fail;
|
|
36
|
+
}
|
|
@@ -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,
|
|
@@ -49,36 +32,8 @@ export class McpEndpoint {
|
|
|
49
32
|
packageInfo: this.options.packageInfo,
|
|
50
33
|
principal: credential.principal,
|
|
51
34
|
scopes: credential.scopes,
|
|
35
|
+
grant: credential.grant,
|
|
36
|
+
notices: this.options.notices,
|
|
52
37
|
});
|
|
53
|
-
const transport = new StreamableHTTPServerTransport({
|
|
54
|
-
sessionIdGenerator: undefined,
|
|
55
|
-
});
|
|
56
|
-
res.on("close", () => {
|
|
57
|
-
void transport.close();
|
|
58
|
-
void server.close();
|
|
59
|
-
});
|
|
60
|
-
await server.connect(transport);
|
|
61
|
-
await transport.handleRequest(req, res, body);
|
|
62
|
-
}
|
|
63
|
-
static refuse(res, challenge) {
|
|
64
|
-
res.writeHead(challenge.status, {
|
|
65
|
-
"Content-Type": "application/json",
|
|
66
|
-
"WWW-Authenticate": challenge.header(),
|
|
67
|
-
});
|
|
68
|
-
res.end(JSON.stringify(challenge.body()));
|
|
69
|
-
}
|
|
70
|
-
static throttle(res, retryAfterSeconds) {
|
|
71
|
-
res.writeHead(429, {
|
|
72
|
-
"Content-Type": "application/json",
|
|
73
|
-
"Retry-After": String(retryAfterSeconds),
|
|
74
|
-
});
|
|
75
|
-
res.end(JSON.stringify({
|
|
76
|
-
jsonrpc: "2.0",
|
|
77
|
-
error: {
|
|
78
|
-
code: -32000,
|
|
79
|
-
message: `Too many requests. Try again in ${retryAfterSeconds} s.`,
|
|
80
|
-
},
|
|
81
|
-
id: null,
|
|
82
|
-
}));
|
|
83
38
|
}
|
|
84
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,32 @@
|
|
|
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 class PostBudget {
|
|
7
|
+
perMinute;
|
|
8
|
+
clock;
|
|
9
|
+
windowStart = 0;
|
|
10
|
+
posted = 0;
|
|
11
|
+
dropped = 0;
|
|
12
|
+
constructor(perMinute, clock = () => Date.now()) {
|
|
13
|
+
this.perMinute = perMinute;
|
|
14
|
+
this.clock = clock;
|
|
15
|
+
}
|
|
16
|
+
/** Null when this post must be dropped; otherwise how many were dropped since the last one that got through. */
|
|
17
|
+
take() {
|
|
18
|
+
const now = this.clock();
|
|
19
|
+
if (now - this.windowStart >= 60_000) {
|
|
20
|
+
this.windowStart = now;
|
|
21
|
+
this.posted = 0;
|
|
22
|
+
}
|
|
23
|
+
if (this.posted >= this.perMinute) {
|
|
24
|
+
this.dropped += 1;
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
this.posted += 1;
|
|
28
|
+
const dropped = this.dropped;
|
|
29
|
+
this.dropped = 0;
|
|
30
|
+
return dropped;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -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,57 @@
|
|
|
1
|
+
import { PostBudget } from "./PostBudget.js";
|
|
2
|
+
import { buildUsageMessage, } from "./UsageMessage.js";
|
|
3
|
+
export const USAGE_POSTS_PER_MINUTE = 60;
|
|
4
|
+
export const USAGE_POST_TIMEOUT_MS = 3000;
|
|
5
|
+
const postWithFetch = async (webhookUrl, message, timeoutMs) => {
|
|
6
|
+
const response = await fetch(webhookUrl, {
|
|
7
|
+
method: "POST",
|
|
8
|
+
headers: { "Content-Type": "application/json" },
|
|
9
|
+
body: JSON.stringify(message),
|
|
10
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
11
|
+
});
|
|
12
|
+
if (!response.ok)
|
|
13
|
+
throw new Error(`Slack answered ${response.status}`);
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Posts each noticed tool call to a Slack incoming webhook. The server is a
|
|
17
|
+
* long-lived process, so the post runs after the tool call has returned and
|
|
18
|
+
* nothing waits for it; a failure is one warning line in the server log.
|
|
19
|
+
*/
|
|
20
|
+
export class SlackUsageNotices {
|
|
21
|
+
webhookUrl;
|
|
22
|
+
server;
|
|
23
|
+
budget;
|
|
24
|
+
post;
|
|
25
|
+
warn;
|
|
26
|
+
constructor(webhookUrl, server, budget = new PostBudget(USAGE_POSTS_PER_MINUTE), post = postWithFetch, warn = (line) => console.warn(line)) {
|
|
27
|
+
this.webhookUrl = webhookUrl;
|
|
28
|
+
this.server = server;
|
|
29
|
+
this.budget = budget;
|
|
30
|
+
this.post = post;
|
|
31
|
+
this.warn = warn;
|
|
32
|
+
}
|
|
33
|
+
toolCalled(call) {
|
|
34
|
+
void this.send(call);
|
|
35
|
+
}
|
|
36
|
+
/** Resolves once the post settled; tests await it, the server never does. */
|
|
37
|
+
async send(call) {
|
|
38
|
+
const dropped = this.budget.take();
|
|
39
|
+
if (dropped === null)
|
|
40
|
+
return;
|
|
41
|
+
const message = buildUsageMessage(call, this.server, dropped);
|
|
42
|
+
if (!message)
|
|
43
|
+
return;
|
|
44
|
+
try {
|
|
45
|
+
await this.post(this.webhookUrl, message, USAGE_POST_TIMEOUT_MS);
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
this.warn(JSON.stringify({
|
|
49
|
+
level: "warning",
|
|
50
|
+
message: "[usage-notices] slack post failed",
|
|
51
|
+
tool: call.tool,
|
|
52
|
+
principal: call.principal,
|
|
53
|
+
error: error instanceof Error ? error.message : String(error),
|
|
54
|
+
}));
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -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;
|