@daloyjs/core 1.0.0-beta.5 → 1.0.0-beta.7
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 +2 -1
- package/dist/adapters/bun.d.ts +13 -1
- package/dist/adapters/bun.js +8 -1
- package/dist/adapters/cloudflare.d.ts +7 -1
- package/dist/adapters/cloudflare.js +6 -1
- package/dist/adapters/deno.d.ts +11 -1
- package/dist/adapters/deno.js +8 -1
- package/dist/adapters/fastly.d.ts +12 -2
- package/dist/adapters/fastly.js +12 -2
- package/dist/adapters/lambda.d.ts +37 -1
- package/dist/adapters/lambda.js +6 -1
- package/dist/adapters/node.d.ts +12 -1
- package/dist/adapters/node.js +7 -1
- package/dist/adapters/vercel.d.ts +13 -1
- package/dist/adapters/vercel.js +12 -1
- package/dist/app.d.ts +67 -17
- package/dist/app.js +97 -31
- package/dist/banner.d.ts +6 -0
- package/dist/banner.js +6 -0
- package/dist/cli.d.ts +35 -0
- package/dist/cli.js +23 -1
- package/dist/combine.d.ts +8 -0
- package/dist/combine.js +8 -0
- package/dist/compression.d.ts +3 -0
- package/dist/compression.js +3 -0
- package/dist/config.d.ts +4 -0
- package/dist/config.js +4 -0
- package/dist/conn-info.d.ts +35 -2
- package/dist/conn-info.js +35 -2
- package/dist/contract.d.ts +2 -0
- package/dist/contract.js +2 -0
- package/dist/cookie.d.ts +12 -0
- package/dist/cookie.js +12 -0
- package/dist/dependency.d.ts +4 -0
- package/dist/dependency.js +3 -0
- package/dist/discriminator.d.ts +13 -0
- package/dist/discriminator.js +23 -1
- package/dist/docs.d.ts +78 -0
- package/dist/docs.js +21 -0
- package/dist/errors.d.ts +16 -0
- package/dist/errors.js +14 -0
- package/dist/etag.d.ts +2 -0
- package/dist/etag.js +2 -0
- package/dist/fetch-guard.d.ts +7 -0
- package/dist/fetch-guard.js +7 -0
- package/dist/fetch-resilience.d.ts +4 -0
- package/dist/fetch-resilience.js +4 -0
- package/dist/http-signatures.d.ts +32 -0
- package/dist/http-signatures.js +30 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/ip-restriction.d.ts +40 -3
- package/dist/ip-restriction.js +35 -3
- package/dist/jwk.d.ts +12 -1
- package/dist/jwk.js +6 -0
- package/dist/jwt.d.ts +14 -0
- package/dist/jwt.js +10 -0
- package/dist/load-shedding.d.ts +3 -0
- package/dist/load-shedding.js +3 -0
- package/dist/logger.d.ts +13 -0
- package/dist/logger.js +3 -0
- package/dist/mcp.d.ts +574 -0
- package/dist/mcp.js +623 -0
- package/dist/middleware.d.ts +68 -0
- package/dist/middleware.js +17 -0
- package/dist/mtls.d.ts +19 -2
- package/dist/mtls.js +12 -2
- package/dist/multipart.d.ts +42 -5
- package/dist/multipart.js +41 -5
- package/dist/openapi.d.ts +15 -9
- package/dist/openapi.js +6 -9
- package/dist/rate-limit-redis.d.ts +21 -2
- package/dist/rate-limit-redis.js +17 -2
- package/dist/safe-redirect.d.ts +6 -0
- package/dist/safe-redirect.js +6 -0
- package/dist/sbom.cdx.json +9 -9
- package/dist/sbom.spdx.json +5 -5
- package/dist/scheduler.d.ts +4 -0
- package/dist/schema.d.ts +25 -0
- package/dist/security-schemes.d.ts +50 -0
- package/dist/security-schemes.js +6 -0
- package/dist/security.d.ts +33 -0
- package/dist/security.js +28 -10
- package/dist/session.d.ts +34 -5
- package/dist/session.js +31 -5
- package/dist/streaming.d.ts +19 -0
- package/dist/streaming.js +16 -0
- package/dist/subdomains.d.ts +4 -0
- package/dist/subdomains.js +4 -0
- package/dist/time-claims.d.ts +22 -2
- package/dist/time-claims.js +6 -0
- package/dist/tracing.d.ts +12 -0
- package/dist/tracing.js +6 -0
- package/dist/types.d.ts +70 -1
- package/dist/waf.js +21 -1
- package/dist/webhook-delivery.d.ts +6 -0
- package/dist/webhook-delivery.js +5 -0
- package/dist/websocket.d.ts +137 -4
- package/dist/websocket.js +105 -4
- package/package.json +6 -1
package/dist/logger.js
CHANGED
|
@@ -123,6 +123,9 @@ function redactString(value, cfg) {
|
|
|
123
123
|
* `cfg.redactJwt` is on) with `cfg.censor`. Exported for direct use by
|
|
124
124
|
* custom logger implementations that want the same defaults.
|
|
125
125
|
*
|
|
126
|
+
* @param record - Log record to redact. Mutated in place (cycle-safe, depth-capped).
|
|
127
|
+
* @param cfg - Resolved redaction settings (key set, censor, JWT/credential toggles, max depth).
|
|
128
|
+
* @returns The same `record` object, for chaining.
|
|
126
129
|
* @since 0.15.0
|
|
127
130
|
*/
|
|
128
131
|
export function redactRecord(record, cfg) {
|
package/dist/mcp.d.ts
ADDED
|
@@ -0,0 +1,574 @@
|
|
|
1
|
+
import type { PathString, RouteDefinition } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Latest MCP protocol version DaloyJS negotiates by default.
|
|
4
|
+
*
|
|
5
|
+
* @see https://modelcontextprotocol.io/specification/2025-11-25
|
|
6
|
+
* @since 1.0.0
|
|
7
|
+
*/
|
|
8
|
+
export declare const MCP_PROTOCOL_VERSION = "2025-11-25";
|
|
9
|
+
/**
|
|
10
|
+
* Protocol revisions accepted by {@link createMcpHandler} unless the caller
|
|
11
|
+
* provides an explicit `protocolVersions` list.
|
|
12
|
+
*
|
|
13
|
+
* @since 1.0.0
|
|
14
|
+
*/
|
|
15
|
+
export declare const MCP_PROTOCOL_VERSIONS: readonly string[];
|
|
16
|
+
/**
|
|
17
|
+
* Default maximum accepted JSON-RPC request body for a DaloyJS MCP endpoint.
|
|
18
|
+
* The cap is intentionally small because MCP calls should carry parameters,
|
|
19
|
+
* not bulk uploads. Raise it per endpoint when a real tool needs larger input.
|
|
20
|
+
*
|
|
21
|
+
* @since 1.0.0
|
|
22
|
+
*/
|
|
23
|
+
export declare const MCP_DEFAULT_MAX_BODY_BYTES: number;
|
|
24
|
+
/**
|
|
25
|
+
* JSON value accepted in MCP schemas, structured results, and metadata.
|
|
26
|
+
*
|
|
27
|
+
* @since 1.0.0
|
|
28
|
+
*/
|
|
29
|
+
export type McpJsonValue = null | boolean | number | string | McpJsonValue[] | {
|
|
30
|
+
[key: string]: McpJsonValue;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* JSON object used for MCP tool arguments and structured payloads.
|
|
34
|
+
*
|
|
35
|
+
* @since 1.0.0
|
|
36
|
+
*/
|
|
37
|
+
export type McpJsonObject = {
|
|
38
|
+
[key: string]: McpJsonValue;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* JSON Schema fragment advertised to MCP clients for a tool or prompt
|
|
42
|
+
* argument object. DaloyJS does not bundle a schema validator here, so the
|
|
43
|
+
* schema is documentation and client-side guidance. Validate sensitive inputs
|
|
44
|
+
* inside your handler before touching databases, files, or remote services.
|
|
45
|
+
*
|
|
46
|
+
* @since 1.0.0
|
|
47
|
+
*/
|
|
48
|
+
export type McpJsonSchema = McpJsonObject;
|
|
49
|
+
/**
|
|
50
|
+
* JSON-RPC id type accepted by MCP requests.
|
|
51
|
+
*
|
|
52
|
+
* @since 1.0.0
|
|
53
|
+
*/
|
|
54
|
+
export type McpJsonRpcId = string | number | null;
|
|
55
|
+
/**
|
|
56
|
+
* Icon metadata clients may render next to a server, tool, resource, or
|
|
57
|
+
* prompt (MCP 2025-11-25, SEP-973).
|
|
58
|
+
*
|
|
59
|
+
* @since 1.0.0
|
|
60
|
+
*/
|
|
61
|
+
export interface McpIcon {
|
|
62
|
+
/** Icon URL. Prefer `https:` or `data:` URIs that clients can fetch safely. */
|
|
63
|
+
src: string;
|
|
64
|
+
/** Optional icon media type, e.g. `"image/png"`. */
|
|
65
|
+
mimeType?: string;
|
|
66
|
+
/** Optional pixel sizes the icon is available in, e.g. `["48x48"]`. */
|
|
67
|
+
sizes?: string[];
|
|
68
|
+
/** Optional theme the icon is designed for. */
|
|
69
|
+
theme?: "light" | "dark";
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Identity block returned from the MCP `initialize` handshake.
|
|
73
|
+
*
|
|
74
|
+
* @since 1.0.0
|
|
75
|
+
*/
|
|
76
|
+
export interface McpServerInfo {
|
|
77
|
+
/** Stable machine-readable server name, for example `"acme-inventory-mcp"`. */
|
|
78
|
+
name: string;
|
|
79
|
+
/** Optional human-readable display title for MCP clients. */
|
|
80
|
+
title?: string;
|
|
81
|
+
/** Server version surfaced to clients for debugging and compatibility. */
|
|
82
|
+
version: string;
|
|
83
|
+
/** Optional human-readable server description (MCP 2025-11-25). */
|
|
84
|
+
description?: string;
|
|
85
|
+
/** Optional homepage URL for this server (MCP 2025-11-25). */
|
|
86
|
+
websiteUrl?: string;
|
|
87
|
+
/** Optional icons clients may display for this server (MCP 2025-11-25). */
|
|
88
|
+
icons?: McpIcon[];
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Per-request context passed to tool, resource, and prompt handlers.
|
|
92
|
+
*
|
|
93
|
+
* @since 1.0.0
|
|
94
|
+
*/
|
|
95
|
+
export interface McpRequestContext {
|
|
96
|
+
/** The original HTTP request received by the DaloyJS route. */
|
|
97
|
+
request: Request;
|
|
98
|
+
/**
|
|
99
|
+
* Protocol version selected for this call. `initialize` negotiates it from
|
|
100
|
+
* `params.protocolVersion`; other calls take the `MCP-Protocol-Version`
|
|
101
|
+
* header, falling back to `2025-03-26` (the spec's assumption for
|
|
102
|
+
* headerless requests) when supported, otherwise the preferred version.
|
|
103
|
+
*/
|
|
104
|
+
protocolVersion: string;
|
|
105
|
+
/** JSON-RPC id for request/response correlation. */
|
|
106
|
+
id: McpJsonRpcId;
|
|
107
|
+
/** Raw MCP method name, such as `"tools/call"` or `"resources/read"`. */
|
|
108
|
+
method: string;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Text content block returned from an MCP tool, resource, or prompt.
|
|
112
|
+
*
|
|
113
|
+
* @since 1.0.0
|
|
114
|
+
*/
|
|
115
|
+
export interface McpTextContent {
|
|
116
|
+
/** Discriminator literal identifying this block as text. */
|
|
117
|
+
type: "text";
|
|
118
|
+
/** The plain-text payload of the block. */
|
|
119
|
+
text: string;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Image content block returned from an MCP tool.
|
|
123
|
+
*
|
|
124
|
+
* `data` is base64-encoded image bytes. Keep images small; for large assets,
|
|
125
|
+
* return a resource link or URL-bearing text instead.
|
|
126
|
+
*
|
|
127
|
+
* @since 1.0.0
|
|
128
|
+
*/
|
|
129
|
+
export interface McpImageContent {
|
|
130
|
+
/** Discriminator literal identifying this block as an image. */
|
|
131
|
+
type: "image";
|
|
132
|
+
/** Base64-encoded image bytes. */
|
|
133
|
+
data: string;
|
|
134
|
+
/** Image media type, e.g. `"image/png"`. */
|
|
135
|
+
mimeType: string;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Embedded resource content block returned from an MCP tool.
|
|
139
|
+
*
|
|
140
|
+
* @since 1.0.0
|
|
141
|
+
*/
|
|
142
|
+
export interface McpEmbeddedResourceContent {
|
|
143
|
+
/** Discriminator literal identifying this block as an embedded resource. */
|
|
144
|
+
type: "resource";
|
|
145
|
+
/** The embedded resource contents (uri plus text or base64 blob). */
|
|
146
|
+
resource: McpResourceContents;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Content block supported by the dependency-free MCP helper.
|
|
150
|
+
*
|
|
151
|
+
* @since 1.0.0
|
|
152
|
+
*/
|
|
153
|
+
export type McpContent = McpTextContent | McpImageContent | McpEmbeddedResourceContent;
|
|
154
|
+
/**
|
|
155
|
+
* Result returned by an MCP tool handler.
|
|
156
|
+
*
|
|
157
|
+
* `isError` marks caller-correctable tool failures, such as invalid input or a
|
|
158
|
+
* domain error. Unexpected thrown errors become JSON-RPC internal errors and
|
|
159
|
+
* are redacted in production.
|
|
160
|
+
*
|
|
161
|
+
* @since 1.0.0
|
|
162
|
+
*/
|
|
163
|
+
export interface McpToolResult {
|
|
164
|
+
/**
|
|
165
|
+
* Human or model-readable content blocks returned to the MCP client. When
|
|
166
|
+
* omitted, {@link createMcpHandler} backfills a text block serializing
|
|
167
|
+
* `structuredContent` so pre-2025-06-18 clients still receive output.
|
|
168
|
+
*/
|
|
169
|
+
content?: McpContent[];
|
|
170
|
+
/** Optional structured payload for clients that can consume typed output. */
|
|
171
|
+
structuredContent?: McpJsonObject;
|
|
172
|
+
/** Set to `true` for domain/tool errors the model may recover from. */
|
|
173
|
+
isError?: boolean;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Behavioral hints a tool can advertise to MCP clients. Hints are untrusted
|
|
177
|
+
* metadata for UX decisions (confirmation prompts, badges); clients must not
|
|
178
|
+
* rely on them for security decisions.
|
|
179
|
+
*
|
|
180
|
+
* @since 1.0.0
|
|
181
|
+
*/
|
|
182
|
+
export interface McpToolAnnotations {
|
|
183
|
+
/** Human-readable title for the tool. */
|
|
184
|
+
title?: string;
|
|
185
|
+
/** Hint that the tool does not modify its environment. */
|
|
186
|
+
readOnlyHint?: boolean;
|
|
187
|
+
/** Hint that the tool may perform destructive updates. */
|
|
188
|
+
destructiveHint?: boolean;
|
|
189
|
+
/** Hint that repeated calls with the same arguments have no extra effect. */
|
|
190
|
+
idempotentHint?: boolean;
|
|
191
|
+
/** Hint that the tool interacts with external entities. */
|
|
192
|
+
openWorldHint?: boolean;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Handler for a single MCP tool.
|
|
196
|
+
*
|
|
197
|
+
* @typeParam TArgs - Type expected in `params.arguments` for this tool.
|
|
198
|
+
* @param args - Tool arguments supplied by the MCP client. They are typed for
|
|
199
|
+
* developer experience but are still untrusted JSON at runtime.
|
|
200
|
+
* @param ctx - Request metadata and the original HTTP request.
|
|
201
|
+
* @returns Text shorthand or a full {@link McpToolResult}.
|
|
202
|
+
* @throws {McpToolError} for caller-correctable failures that should be
|
|
203
|
+
* returned as an MCP tool error result.
|
|
204
|
+
*
|
|
205
|
+
* @since 1.0.0
|
|
206
|
+
*/
|
|
207
|
+
export type McpToolHandler<TArgs extends Record<string, unknown> = Record<string, unknown>> = (args: TArgs, ctx: McpRequestContext) => string | McpToolResult | Promise<string | McpToolResult>;
|
|
208
|
+
/**
|
|
209
|
+
* Definition of a callable MCP tool.
|
|
210
|
+
*
|
|
211
|
+
* Tools are model-controlled in MCP: clients may let the language model decide
|
|
212
|
+
* when to call them. Treat every tool as a public API operation and enforce
|
|
213
|
+
* authentication, authorization, rate limits, and validation before side
|
|
214
|
+
* effects.
|
|
215
|
+
*
|
|
216
|
+
* @typeParam TArgs - Type expected by this tool's handler.
|
|
217
|
+
* @since 1.0.0
|
|
218
|
+
*/
|
|
219
|
+
export interface McpTool<TArgs extends Record<string, unknown> = Record<string, unknown>> {
|
|
220
|
+
/** Unique tool name within this MCP server. Prefer namespaced verbs. */
|
|
221
|
+
name: string;
|
|
222
|
+
/** Optional human-readable title displayed by clients. */
|
|
223
|
+
title?: string;
|
|
224
|
+
/** Clear description of when the model should use this tool. */
|
|
225
|
+
description: string;
|
|
226
|
+
/** JSON Schema for `params.arguments`. */
|
|
227
|
+
inputSchema: McpJsonSchema;
|
|
228
|
+
/**
|
|
229
|
+
* Optional JSON Schema describing `structuredContent` in tool results
|
|
230
|
+
* (MCP 2025-06-18). When set, handlers should return `structuredContent`
|
|
231
|
+
* matching it.
|
|
232
|
+
*/
|
|
233
|
+
outputSchema?: McpJsonSchema;
|
|
234
|
+
/** Optional behavioral hints for clients. */
|
|
235
|
+
annotations?: McpToolAnnotations;
|
|
236
|
+
/** Optional icons clients may display for this tool (MCP 2025-11-25). */
|
|
237
|
+
icons?: McpIcon[];
|
|
238
|
+
/** Execute the tool with untrusted JSON arguments. */
|
|
239
|
+
handler: McpToolHandler<TArgs>;
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Resource metadata returned from `resources/list`.
|
|
243
|
+
*
|
|
244
|
+
* @since 1.0.0
|
|
245
|
+
*/
|
|
246
|
+
export interface McpResource {
|
|
247
|
+
/** Unique resource URI, for example `"daloy://schema/inventory"`. */
|
|
248
|
+
uri: string;
|
|
249
|
+
/** Stable resource name. */
|
|
250
|
+
name: string;
|
|
251
|
+
/** Optional human-readable title. */
|
|
252
|
+
title?: string;
|
|
253
|
+
/** Optional description shown by clients. */
|
|
254
|
+
description?: string;
|
|
255
|
+
/** MIME type returned by `resources/read`, such as `"application/json"`. */
|
|
256
|
+
mimeType?: string;
|
|
257
|
+
/** Optional icons clients may display for this resource (MCP 2025-11-25). */
|
|
258
|
+
icons?: McpIcon[];
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Resource payload returned from `resources/read`.
|
|
262
|
+
*
|
|
263
|
+
* Use either `text` for UTF-8 content or `blob` for base64-encoded binary
|
|
264
|
+
* content. Set `mimeType` so clients know how to present the resource.
|
|
265
|
+
*
|
|
266
|
+
* @since 1.0.0
|
|
267
|
+
*/
|
|
268
|
+
export interface McpResourceContents {
|
|
269
|
+
/** URI of the resource being returned. */
|
|
270
|
+
uri: string;
|
|
271
|
+
/** MIME type of the returned content. */
|
|
272
|
+
mimeType?: string;
|
|
273
|
+
/** UTF-8 text content. */
|
|
274
|
+
text?: string;
|
|
275
|
+
/** Base64-encoded binary content. */
|
|
276
|
+
blob?: string;
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Definition of a readable MCP resource.
|
|
280
|
+
*
|
|
281
|
+
* Resources are application-controlled context. They are a good fit for
|
|
282
|
+
* schemas, read-only records, catalogs, runbooks, and other context a client
|
|
283
|
+
* can choose to include before a tool call.
|
|
284
|
+
*
|
|
285
|
+
* @since 1.0.0
|
|
286
|
+
*/
|
|
287
|
+
export interface McpResourceDefinition extends McpResource {
|
|
288
|
+
/**
|
|
289
|
+
* Read the resource contents for `resources/read`.
|
|
290
|
+
*
|
|
291
|
+
* @param ctx - Request metadata and the original HTTP request.
|
|
292
|
+
* @returns One or more content entries for this resource.
|
|
293
|
+
*/
|
|
294
|
+
read: (ctx: McpRequestContext) => McpResourceContents | McpResourceContents[] | Promise<McpResourceContents | McpResourceContents[]>;
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Resource template metadata returned from `resources/templates/list`.
|
|
298
|
+
*
|
|
299
|
+
* @since 1.0.0
|
|
300
|
+
*/
|
|
301
|
+
export interface McpResourceTemplate {
|
|
302
|
+
/**
|
|
303
|
+
* URI template for this resource family, e.g. `"daloy://records/{id}"`.
|
|
304
|
+
* DaloyJS supports simple `{name}` variables (RFC 6570 level 1); each
|
|
305
|
+
* variable matches one URI segment (no `/`).
|
|
306
|
+
*/
|
|
307
|
+
uriTemplate: string;
|
|
308
|
+
/** Stable template name. */
|
|
309
|
+
name: string;
|
|
310
|
+
/** Optional human-readable title. */
|
|
311
|
+
title?: string;
|
|
312
|
+
/** Optional description shown by clients. */
|
|
313
|
+
description?: string;
|
|
314
|
+
/** MIME type of resources produced by this template. */
|
|
315
|
+
mimeType?: string;
|
|
316
|
+
/** Optional icons clients may display for this template (MCP 2025-11-25). */
|
|
317
|
+
icons?: McpIcon[];
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Definition of a parameterized MCP resource template.
|
|
321
|
+
*
|
|
322
|
+
* Templates answer `resources/read` for URIs that match `uriTemplate` but are
|
|
323
|
+
* not listed as concrete resources. Template variables arrive as raw URI
|
|
324
|
+
* segment strings; validate them before touching databases or files.
|
|
325
|
+
*
|
|
326
|
+
* @since 1.0.0
|
|
327
|
+
*/
|
|
328
|
+
export interface McpResourceTemplateDefinition extends McpResourceTemplate {
|
|
329
|
+
/**
|
|
330
|
+
* Read a resource instantiated from this template for `resources/read`.
|
|
331
|
+
*
|
|
332
|
+
* @param uri - The full resource URI requested by the client.
|
|
333
|
+
* @param variables - Template variable values extracted from `uri`.
|
|
334
|
+
* @param ctx - Request metadata and the original HTTP request.
|
|
335
|
+
* @returns One or more content entries for this resource.
|
|
336
|
+
* @throws {McpToolError} for caller-correctable failures such as an unknown
|
|
337
|
+
* record id; these become JSON-RPC invalid-params errors.
|
|
338
|
+
*/
|
|
339
|
+
read: (uri: string, variables: Record<string, string>, ctx: McpRequestContext) => McpResourceContents | McpResourceContents[] | Promise<McpResourceContents | McpResourceContents[]>;
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* Argument metadata for an MCP prompt.
|
|
343
|
+
*
|
|
344
|
+
* @since 1.0.0
|
|
345
|
+
*/
|
|
346
|
+
export interface McpPromptArgument {
|
|
347
|
+
/** Argument name accepted by the prompt. */
|
|
348
|
+
name: string;
|
|
349
|
+
/** Optional description displayed by clients. */
|
|
350
|
+
description?: string;
|
|
351
|
+
/** Whether the argument is required. */
|
|
352
|
+
required?: boolean;
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* Prompt metadata returned from `prompts/list`.
|
|
356
|
+
*
|
|
357
|
+
* @since 1.0.0
|
|
358
|
+
*/
|
|
359
|
+
export interface McpPrompt {
|
|
360
|
+
/** Unique prompt name within this MCP server. */
|
|
361
|
+
name: string;
|
|
362
|
+
/** Optional human-readable title. */
|
|
363
|
+
title?: string;
|
|
364
|
+
/** Optional prompt description. */
|
|
365
|
+
description?: string;
|
|
366
|
+
/**
|
|
367
|
+
* Prompt arguments clients may supply to `prompts/get`. Arguments marked
|
|
368
|
+
* `required: true` are enforced by {@link createMcpHandler}: a `prompts/get`
|
|
369
|
+
* call missing one fails with a JSON-RPC invalid-params error.
|
|
370
|
+
*/
|
|
371
|
+
arguments?: McpPromptArgument[];
|
|
372
|
+
/** Optional icons clients may display for this prompt (MCP 2025-11-25). */
|
|
373
|
+
icons?: McpIcon[];
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* Message returned from `prompts/get`.
|
|
377
|
+
*
|
|
378
|
+
* @since 1.0.0
|
|
379
|
+
*/
|
|
380
|
+
export interface McpPromptMessage {
|
|
381
|
+
/** Role that should receive the prompt content. */
|
|
382
|
+
role: "user" | "assistant";
|
|
383
|
+
/** Prompt content block. */
|
|
384
|
+
content: McpTextContent | McpImageContent | McpEmbeddedResourceContent;
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Result returned by an MCP prompt handler.
|
|
388
|
+
*
|
|
389
|
+
* @since 1.0.0
|
|
390
|
+
*/
|
|
391
|
+
export interface McpPromptResult {
|
|
392
|
+
/** Optional description of the rendered prompt. */
|
|
393
|
+
description?: string;
|
|
394
|
+
/** Messages the client can inject into the model conversation. */
|
|
395
|
+
messages: McpPromptMessage[];
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Definition of a reusable MCP prompt.
|
|
399
|
+
*
|
|
400
|
+
* @since 1.0.0
|
|
401
|
+
*/
|
|
402
|
+
export interface McpPromptDefinition extends McpPrompt {
|
|
403
|
+
/**
|
|
404
|
+
* Render the prompt for `prompts/get`.
|
|
405
|
+
*
|
|
406
|
+
* @param args - Prompt arguments supplied by the MCP client.
|
|
407
|
+
* @param ctx - Request metadata and the original HTTP request.
|
|
408
|
+
* @returns Prompt messages.
|
|
409
|
+
*/
|
|
410
|
+
get: (args: Record<string, unknown>, ctx: McpRequestContext) => McpPromptResult | Promise<McpPromptResult>;
|
|
411
|
+
}
|
|
412
|
+
/**
|
|
413
|
+
* Caller-correctable MCP tool/resource/prompt error.
|
|
414
|
+
*
|
|
415
|
+
* Throw this when the model supplied bad arguments, referenced a missing
|
|
416
|
+
* domain object, or otherwise made a recoverable call. Tool errors become
|
|
417
|
+
* `{ isError: true }` tool results; resource and prompt errors become
|
|
418
|
+
* JSON-RPC invalid-params errors. Unexpected errors are treated as internal
|
|
419
|
+
* server failures and are redacted in production.
|
|
420
|
+
*
|
|
421
|
+
* @since 1.0.0
|
|
422
|
+
*/
|
|
423
|
+
export declare class McpToolError extends Error {
|
|
424
|
+
/**
|
|
425
|
+
* Create a recoverable MCP handler error.
|
|
426
|
+
*
|
|
427
|
+
* @param message - Safe, caller-visible explanation.
|
|
428
|
+
*/
|
|
429
|
+
constructor(message: string);
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* Options for {@link createMcpHandler}.
|
|
433
|
+
*
|
|
434
|
+
* @since 1.0.0
|
|
435
|
+
*/
|
|
436
|
+
export interface McpHandlerOptions {
|
|
437
|
+
/** Server identity returned from the `initialize` handshake. */
|
|
438
|
+
serverInfo: McpServerInfo;
|
|
439
|
+
/** Optional guidance returned from `initialize`. */
|
|
440
|
+
instructions?: string;
|
|
441
|
+
/** Callable tools exposed through `tools/list` and `tools/call`. */
|
|
442
|
+
tools?: readonly McpTool[];
|
|
443
|
+
/** Readable resources exposed through `resources/list` and `resources/read`. */
|
|
444
|
+
resources?: readonly McpResourceDefinition[];
|
|
445
|
+
/**
|
|
446
|
+
* Parameterized resource templates exposed through
|
|
447
|
+
* `resources/templates/list` and matched by `resources/read` when a URI is
|
|
448
|
+
* not a listed concrete resource.
|
|
449
|
+
*/
|
|
450
|
+
resourceTemplates?: readonly McpResourceTemplateDefinition[];
|
|
451
|
+
/** Reusable prompts exposed through `prompts/list` and `prompts/get`. */
|
|
452
|
+
prompts?: readonly McpPromptDefinition[];
|
|
453
|
+
/**
|
|
454
|
+
* Extra `Origin` header values allowed on MCP requests, e.g.
|
|
455
|
+
* `"https://app.example.com"` (or the literal `"null"` for opaque origins).
|
|
456
|
+
*
|
|
457
|
+
* The MCP Streamable HTTP spec requires servers to validate `Origin` to
|
|
458
|
+
* prevent DNS rebinding attacks. DaloyJS always allows requests without an
|
|
459
|
+
* `Origin` header (non-browser MCP clients), same-origin requests, and
|
|
460
|
+
* loopback origins (`localhost`, `*.localhost`, `127.0.0.1`, `[::1]`); every
|
|
461
|
+
* other origin is rejected with `403` unless listed here.
|
|
462
|
+
*/
|
|
463
|
+
allowedOrigins?: readonly string[];
|
|
464
|
+
/** Accepted MCP protocol versions. Defaults to {@link MCP_PROTOCOL_VERSIONS}. */
|
|
465
|
+
protocolVersions?: readonly string[];
|
|
466
|
+
/**
|
|
467
|
+
* Protocol version returned when the client asks for an unsupported version.
|
|
468
|
+
* Defaults to {@link MCP_PROTOCOL_VERSION}.
|
|
469
|
+
*/
|
|
470
|
+
preferredProtocolVersion?: string;
|
|
471
|
+
/** Maximum accepted JSON-RPC body size in bytes. Defaults to 256 KiB. */
|
|
472
|
+
maxBodyBytes?: number;
|
|
473
|
+
/**
|
|
474
|
+
* Extra headers added to every JSON response. Use this for endpoint-local
|
|
475
|
+
* cache, CORS, or deployment metadata. Authentication should usually live in
|
|
476
|
+
* DaloyJS middleware before the MCP route.
|
|
477
|
+
*/
|
|
478
|
+
headers?: Record<string, string>;
|
|
479
|
+
/**
|
|
480
|
+
* Include development error details in JSON-RPC internal errors. Defaults to
|
|
481
|
+
* `process.env.NODE_ENV !== "production"` when `process` exists.
|
|
482
|
+
*/
|
|
483
|
+
exposeInternalErrors?: boolean;
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* Fetch-compatible handler returned by {@link createMcpHandler}.
|
|
487
|
+
*
|
|
488
|
+
* @param request - Incoming HTTP request for the MCP endpoint.
|
|
489
|
+
* @returns A standard `Response` containing a JSON-RPC response, `202` for
|
|
490
|
+
* accepted notifications, or `405` for unsupported HTTP methods.
|
|
491
|
+
*
|
|
492
|
+
* @since 1.0.0
|
|
493
|
+
*/
|
|
494
|
+
export type McpHandler = (request: Request) => Promise<Response>;
|
|
495
|
+
/**
|
|
496
|
+
* Create a dependency-free MCP Streamable HTTP endpoint handler.
|
|
497
|
+
*
|
|
498
|
+
* The handler implements the server side of MCP over one HTTP endpoint:
|
|
499
|
+
* `initialize`, `ping`, `tools/list`, `tools/call`, `resources/list`,
|
|
500
|
+
* `resources/templates/list`, `resources/read`, `prompts/list`, and
|
|
501
|
+
* `prompts/get`. It accepts JSON-RPC requests over `POST`, acknowledges
|
|
502
|
+
* notifications with `202`, validates the `MCP-Protocol-Version` header,
|
|
503
|
+
* bounds request bodies, enforces required prompt arguments, and returns
|
|
504
|
+
* JSON-RPC errors for malformed input.
|
|
505
|
+
*
|
|
506
|
+
* Security: per the Streamable HTTP spec's DNS-rebinding guidance, every
|
|
507
|
+
* request bearing an `Origin` header is validated. Same-origin and loopback
|
|
508
|
+
* origins pass; anything else is rejected with `403` unless listed in
|
|
509
|
+
* {@link McpHandlerOptions.allowedOrigins}.
|
|
510
|
+
*
|
|
511
|
+
* It intentionally does not spawn stdio servers, manage OAuth metadata, keep
|
|
512
|
+
* durable sessions, or open server-initiated SSE streams. Use DaloyJS
|
|
513
|
+
* middleware for authentication and authorization, and run this on a dedicated
|
|
514
|
+
* Daloy app when your MCP server has a different trust boundary than your REST
|
|
515
|
+
* API.
|
|
516
|
+
*
|
|
517
|
+
* @param options - Server identity, capabilities, limits, and response headers.
|
|
518
|
+
* @returns A Fetch-compatible request handler suitable for {@link mcpRoutes}
|
|
519
|
+
* or for direct use in any web-standard runtime.
|
|
520
|
+
* @throws {TypeError} at construction for invalid serverInfo, protocol
|
|
521
|
+
* versions, body limits, duplicate names/URIs, malformed `allowedOrigins`
|
|
522
|
+
* entries, or unsupported URI template expressions.
|
|
523
|
+
*
|
|
524
|
+
* @example
|
|
525
|
+
* ```ts
|
|
526
|
+
* const mcp = createMcpHandler({
|
|
527
|
+
* serverInfo: { name: "inventory-mcp", version: "1.0.0" },
|
|
528
|
+
* tools: [
|
|
529
|
+
* {
|
|
530
|
+
* name: "inventory_lookup",
|
|
531
|
+
* description: "Look up inventory by SKU.",
|
|
532
|
+
* inputSchema: {
|
|
533
|
+
* type: "object",
|
|
534
|
+
* properties: { sku: { type: "string" } },
|
|
535
|
+
* required: ["sku"],
|
|
536
|
+
* additionalProperties: false,
|
|
537
|
+
* },
|
|
538
|
+
* handler: async ({ sku }) => `SKU ${sku} has 42 units.`,
|
|
539
|
+
* },
|
|
540
|
+
* ],
|
|
541
|
+
* });
|
|
542
|
+
* ```
|
|
543
|
+
*
|
|
544
|
+
* @since 1.0.0
|
|
545
|
+
*/
|
|
546
|
+
export declare function createMcpHandler(options: McpHandlerOptions): McpHandler;
|
|
547
|
+
/**
|
|
548
|
+
* Build the Daloy route definitions for a Streamable HTTP MCP endpoint.
|
|
549
|
+
*
|
|
550
|
+
* Register each returned route on the Daloy app that should host MCP. A
|
|
551
|
+
* separate app is often the cleanest production shape: the REST API can keep
|
|
552
|
+
* its public contract and auth policy, while the MCP server can use its own
|
|
553
|
+
* bearer token, rate limit, network allowlist, and tool set.
|
|
554
|
+
*
|
|
555
|
+
* @param path - Public MCP endpoint path, usually `"/mcp"`.
|
|
556
|
+
* @param handler - Handler returned by {@link createMcpHandler}.
|
|
557
|
+
* @returns Route definitions for `POST`, `GET`, and `OPTIONS` on the same
|
|
558
|
+
* path. `POST` is the actual MCP transport; `GET` gives a human-readable
|
|
559
|
+
* 405 hint because this helper does not open server-initiated SSE streams;
|
|
560
|
+
* `OPTIONS` supports preflight when CORS middleware is installed.
|
|
561
|
+
*
|
|
562
|
+
* @example
|
|
563
|
+
* ```ts
|
|
564
|
+
* const app = new App();
|
|
565
|
+
* const mcp = createMcpHandler({ serverInfo, tools });
|
|
566
|
+
*
|
|
567
|
+
* for (const route of mcpRoutes("/mcp", mcp)) {
|
|
568
|
+
* app.route(route);
|
|
569
|
+
* }
|
|
570
|
+
* ```
|
|
571
|
+
*
|
|
572
|
+
* @since 1.0.0
|
|
573
|
+
*/
|
|
574
|
+
export declare function mcpRoutes(path: PathString, handler: McpHandler): RouteDefinition<PathString, "GET" | "POST" | "OPTIONS">[];
|