@happyvertical/smrt-app-mcp 0.60.17 → 0.61.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/AGENTS.md +12 -0
- package/README.md +40 -0
- package/agents/runtime-loading.md +21 -0
- package/dist/__smrt-register__.d.ts +2 -0
- package/dist/__smrt-register__.d.ts.map +1 -0
- package/dist/audit-store.d.ts +53 -0
- package/dist/audit-store.d.ts.map +1 -0
- package/dist/audit-store.js +149 -0
- package/dist/audit-store.js.map +1 -0
- package/dist/audit.d.ts +59 -0
- package/dist/audit.d.ts.map +1 -0
- package/dist/chunks/{protocol-BRQji5k_.js → defaults-i3kpRFpB.js} +190 -149
- package/dist/chunks/defaults-i3kpRFpB.js.map +1 -0
- package/dist/chunks/{errors-XG_zn1pP.js → errors-C704k8Mv.js} +3 -2
- package/dist/chunks/errors-C704k8Mv.js.map +1 -0
- package/dist/chunks/protocol-D_O2WaCO.js +153 -0
- package/dist/chunks/protocol-D_O2WaCO.js.map +1 -0
- package/dist/errors.d.ts +2 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/host-operations.js +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -3
- package/dist/index.js.map +1 -1
- package/dist/manifest.json +435 -3
- package/dist/server.d.ts +39 -0
- package/dist/server.d.ts.map +1 -1
- package/dist/smrt-knowledge.json +174 -8
- package/dist/smrt-knowledge.okf.md +17 -4
- package/dist/sveltekit.d.ts +1 -1
- package/dist/sveltekit.d.ts.map +1 -1
- package/dist/sveltekit.js +4 -3
- package/dist/sveltekit.js.map +1 -1
- package/dist/workflow-tools.d.ts +14 -0
- package/dist/workflow-tools.d.ts.map +1 -1
- package/package.json +14 -6
- package/dist/chunks/errors-XG_zn1pP.js.map +0 -1
- package/dist/chunks/protocol-BRQji5k_.js.map +0 -1
|
@@ -1,9 +1,45 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { i as MCP_TOOL_ACCESS_DENIED_CODE, l as authoredMcpToolError, n as MCP_CONFIRM_REQUIRED_CODE, o as MCP_TOOL_NOT_FOUND_CODE, s as McpAccessError, t as MCP_AUTHENTICATION_REQUIRED_CODE, u as safeMcpToolError } from "./errors-C704k8Mv.js";
|
|
2
2
|
import { McpTaskNotFoundError, McpTaskStore, SmrtJobCollection } from "@happyvertical/smrt-jobs";
|
|
3
3
|
import { MCPGenerator, MCP_STABLE_CATALOG_TTL_MS, assertMcpJsonSchemaSafety, mcpToolAnnotationsFor } from "@happyvertical/smrt-core/generators/mcp";
|
|
4
4
|
import { ObjectRegistry, isTenantScopedClassResolved } from "@happyvertical/smrt-core";
|
|
5
5
|
import { createHash } from "node:crypto";
|
|
6
|
-
|
|
6
|
+
//#region src/audit.ts
|
|
7
|
+
var MAX_ARGUMENT_KEYS = 32;
|
|
8
|
+
var MAX_KEY_LENGTH = 120;
|
|
9
|
+
function mcpArgumentKeyNames(args) {
|
|
10
|
+
if (!args || typeof args !== "object" || Array.isArray(args)) return [];
|
|
11
|
+
return Object.keys(args).map((key) => key.slice(0, MAX_KEY_LENGTH)).sort().slice(0, MAX_ARGUMENT_KEYS);
|
|
12
|
+
}
|
|
13
|
+
function mcpAuditRecordFromEvent(event) {
|
|
14
|
+
return Object.freeze({
|
|
15
|
+
principalId: event.principal?.id ?? null,
|
|
16
|
+
principalKind: event.principal?.kind ?? null,
|
|
17
|
+
tool: event.tool,
|
|
18
|
+
tenantId: event.tenantId,
|
|
19
|
+
outcome: event.outcome === "ok" ? "allowed" : event.outcome,
|
|
20
|
+
...event.error ? { errorCode: event.error.code } : {},
|
|
21
|
+
...event.identity ? {
|
|
22
|
+
identityIssuer: event.identity.issuer,
|
|
23
|
+
identitySubject: event.identity.subject,
|
|
24
|
+
...event.identity.clientId ? { identityClientId: event.identity.clientId } : {}
|
|
25
|
+
} : {},
|
|
26
|
+
argumentKeys: event.argumentKeys,
|
|
27
|
+
occurredAt: event.occurredAt
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
function createInMemoryMcpAuditSink() {
|
|
31
|
+
const records = [];
|
|
32
|
+
return {
|
|
33
|
+
records,
|
|
34
|
+
record(record) {
|
|
35
|
+
records.push(record);
|
|
36
|
+
},
|
|
37
|
+
list(query = {}) {
|
|
38
|
+
return records.filter((r) => (query.tenantId === void 0 || r.tenantId === query.tenantId) && (query.principalId === void 0 || r.principalId === query.principalId) && (query.tool === void 0 || r.tool === query.tool) && (query.outcome === void 0 || r.outcome === query.outcome)).sort((a, b) => b.occurredAt.getTime() - a.occurredAt.getTime()).slice(0, query.limit ?? 100);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
//#endregion
|
|
7
43
|
//#region src/workflow-tools.ts
|
|
8
44
|
var TOOL_NAME = /^[a-z][a-z0-9_]{0,63}$/;
|
|
9
45
|
function assertRecord(value, label) {
|
|
@@ -95,6 +131,8 @@ function createMcpWorkflowTool(definition) {
|
|
|
95
131
|
assertMcpJsonSchemaSafety(definition.outputSchema);
|
|
96
132
|
assertMcpToolSchemaRoot(definition.inputSchema, "Workflow tool input schema");
|
|
97
133
|
assertMcpToolSchemaRoot(definition.outputSchema, "Workflow tool output schema");
|
|
134
|
+
if (definition.requiredScopes !== void 0 && (!Array.isArray(definition.requiredScopes) || definition.requiredScopes.some((scope) => typeof scope !== "string" || !scope))) throw new TypeError("Workflow tool requiredScopes must be an array of non-empty strings.");
|
|
135
|
+
if (definition.requiresConfirm !== void 0 && typeof definition.requiresConfirm !== "boolean") throw new TypeError("Workflow tool requiresConfirm must be boolean.");
|
|
98
136
|
const metadata = cloneMetadata(definition.metadata);
|
|
99
137
|
if (definition.ui !== void 0) {
|
|
100
138
|
assertRecord(definition.ui, "Workflow tool ui");
|
|
@@ -123,6 +161,8 @@ function createMcpWorkflowTool(definition) {
|
|
|
123
161
|
...Object.keys(meta).length > 0 ? { _meta: meta } : {}
|
|
124
162
|
},
|
|
125
163
|
execute: definition.execute,
|
|
164
|
+
requiredScopes: Object.freeze([...definition.requiredScopes ?? []]),
|
|
165
|
+
requiresConfirm: definition.requiresConfirm === true,
|
|
126
166
|
...definition.mapError ? { mapError: definition.mapError } : {}
|
|
127
167
|
};
|
|
128
168
|
}
|
|
@@ -7130,6 +7170,26 @@ function configuredEffects(effects) {
|
|
|
7130
7170
|
if (!Array.isArray(effects) || effects.some((effect) => !MCP_TOOL_EFFECTS.includes(effect))) throw new TypeError("MCP effects must be an array of 'read', 'write' or 'destructive'.");
|
|
7131
7171
|
return new Set(effects);
|
|
7132
7172
|
}
|
|
7173
|
+
var DENIED_FAILURE_CODES = /* @__PURE__ */ new Set([
|
|
7174
|
+
"forbidden",
|
|
7175
|
+
MCP_CONFIRM_REQUIRED_CODE,
|
|
7176
|
+
MCP_TOOL_ACCESS_DENIED_CODE,
|
|
7177
|
+
MCP_AUTHENTICATION_REQUIRED_CODE,
|
|
7178
|
+
MCP_TOOL_NOT_FOUND_CODE
|
|
7179
|
+
]);
|
|
7180
|
+
function configuredScopeRules(toolScopes) {
|
|
7181
|
+
if (toolScopes === void 0) return [];
|
|
7182
|
+
if (!toolScopes || typeof toolScopes !== "object" || Array.isArray(toolScopes)) throw new TypeError("MCP toolScopes must be an object.");
|
|
7183
|
+
return Object.entries(toolScopes).map(([pattern, scopes]) => {
|
|
7184
|
+
if (!pattern || !Array.isArray(scopes) || scopes.some((scope) => typeof scope !== "string" || !scope)) throw new TypeError("MCP toolScopes maps a tool pattern to an array of non-empty scopes.");
|
|
7185
|
+
return [pattern, Object.freeze([...scopes])];
|
|
7186
|
+
});
|
|
7187
|
+
}
|
|
7188
|
+
function configuredConfirmPatterns(patterns) {
|
|
7189
|
+
if (patterns === void 0) return [];
|
|
7190
|
+
if (!Array.isArray(patterns) || patterns.some((pattern) => typeof pattern !== "string" || !pattern)) throw new TypeError("MCP confirmTools must be an array of tool patterns.");
|
|
7191
|
+
return Object.freeze([...patterns]);
|
|
7192
|
+
}
|
|
7133
7193
|
function sameNamedClasses(simpleName) {
|
|
7134
7194
|
const wanted = simpleName.toLowerCase();
|
|
7135
7195
|
const seen = /* @__PURE__ */ new Set();
|
|
@@ -7168,6 +7228,8 @@ function createMcpAppServer(options) {
|
|
|
7168
7228
|
if (workflowToolsByName.has(workflowTool.tool.name)) throw new Error(`Duplicate MCP workflow tool name: ${workflowTool.tool.name}`);
|
|
7169
7229
|
workflowToolsByName.set(workflowTool.tool.name, workflowTool);
|
|
7170
7230
|
}
|
|
7231
|
+
const scopeRules = configuredScopeRules(options.toolScopes);
|
|
7232
|
+
const confirmPatterns = configuredConfirmPatterns(options.confirmTools);
|
|
7171
7233
|
const allowedEffects = configuredEffects(options.effects);
|
|
7172
7234
|
const requestedToolListCacheHint = configuredToolListCacheHint(options.toolListCache);
|
|
7173
7235
|
const tasksEnabled = [...ObjectRegistry.getAllClasses()].some(([key, classInfo]) => {
|
|
@@ -7229,11 +7291,48 @@ function createMcpAppServer(options) {
|
|
|
7229
7291
|
names.add(workflowTool.tool.name);
|
|
7230
7292
|
}
|
|
7231
7293
|
const allTools = [...coreTools, ...workflowTools.map(({ tool }) => ({ tool }))];
|
|
7294
|
+
warnUnmatchedPatterns(allTools.map(({ tool }) => tool.name));
|
|
7232
7295
|
for (const { tool } of allTools) {
|
|
7233
7296
|
const ui = tool._meta?.ui;
|
|
7234
7297
|
if (ui?.resourceUri !== void 0 && (typeof ui.resourceUri !== "string" || !resources.has(ui.resourceUri))) throw new TypeError("MCP tool references an undeclared resource.");
|
|
7235
7298
|
}
|
|
7236
|
-
return allTools.
|
|
7299
|
+
return allTools.map((entry) => ({
|
|
7300
|
+
...entry,
|
|
7301
|
+
tool: withConfirmSchema(entry.tool)
|
|
7302
|
+
})).filter(({ tool }) => !applyEffects || !allowedEffects || allowedEffects.has(mcpToolEffect(tool))).sort((left, right) => compareMcpToolNames(left.tool.name, right.tool.name));
|
|
7303
|
+
}
|
|
7304
|
+
let patternsChecked = false;
|
|
7305
|
+
function warnUnmatchedPatterns(names) {
|
|
7306
|
+
if (patternsChecked) return;
|
|
7307
|
+
patternsChecked = true;
|
|
7308
|
+
const unmatched = [...scopeRules.map(([pattern]) => ["toolScopes", pattern]), ...confirmPatterns.map((pattern) => ["confirmTools", pattern])].filter(([, pattern]) => !names.some((name) => matchesToolPattern(name, pattern)));
|
|
7309
|
+
for (const [option, pattern] of unmatched) console.warn(`[smrt-app-mcp] ${option} pattern "${pattern}" matches no tool; it protects nothing.`);
|
|
7310
|
+
}
|
|
7311
|
+
function requiredScopesFor(tool) {
|
|
7312
|
+
const required = new Set(workflowToolsByName.get(tool.name)?.requiredScopes ?? []);
|
|
7313
|
+
for (const [pattern, scopes] of scopeRules) if (matchesToolPattern(tool.name, pattern)) for (const scope of scopes) required.add(scope);
|
|
7314
|
+
return [...required];
|
|
7315
|
+
}
|
|
7316
|
+
function needsConfirm(tool) {
|
|
7317
|
+
return workflowToolsByName.get(tool.name)?.requiresConfirm === true || confirmPatterns.some((pattern) => matchesToolPattern(tool.name, pattern));
|
|
7318
|
+
}
|
|
7319
|
+
function withConfirmSchema(tool) {
|
|
7320
|
+
if (!needsConfirm(tool)) return tool;
|
|
7321
|
+
const schema = tool.inputSchema;
|
|
7322
|
+
if (schema.properties && "confirm" in schema.properties) return tool;
|
|
7323
|
+
return {
|
|
7324
|
+
...tool,
|
|
7325
|
+
inputSchema: {
|
|
7326
|
+
...tool.inputSchema,
|
|
7327
|
+
properties: {
|
|
7328
|
+
...schema.properties ?? {},
|
|
7329
|
+
confirm: {
|
|
7330
|
+
type: "boolean",
|
|
7331
|
+
description: "Must be true to confirm this action; the call is refused without it."
|
|
7332
|
+
}
|
|
7333
|
+
}
|
|
7334
|
+
}
|
|
7335
|
+
};
|
|
7237
7336
|
}
|
|
7238
7337
|
function runWorkflowAssertions(tool, identity, args, principal) {
|
|
7239
7338
|
const keys = identity && identity.originalName !== tool.name ? [tool.name, identity.originalName] : [tool.name];
|
|
@@ -7247,6 +7346,11 @@ function createMcpAppServer(options) {
|
|
|
7247
7346
|
return isPublicMcpTool(tool, publicPatterns ?? []);
|
|
7248
7347
|
}
|
|
7249
7348
|
async function passesToolPolicy(tool, principal) {
|
|
7349
|
+
const scopes = requiredScopesFor(tool);
|
|
7350
|
+
if (scopes.length > 0) {
|
|
7351
|
+
const held = principal?.scopes;
|
|
7352
|
+
if (!Array.isArray(held) || !scopes.every((s) => held.includes(s))) return false;
|
|
7353
|
+
}
|
|
7250
7354
|
if (!toolPolicy) return true;
|
|
7251
7355
|
try {
|
|
7252
7356
|
return Boolean(await toolPolicy({
|
|
@@ -7311,7 +7415,7 @@ function createMcpAppServer(options) {
|
|
|
7311
7415
|
if (requestedToolListCacheHint.cacheScope !== "public") return requestedToolListCacheHint;
|
|
7312
7416
|
const tools = await catalogTools();
|
|
7313
7417
|
const publicPatterns = getPublicPatterns();
|
|
7314
|
-
return !toolPolicy && tools.every(({ tool, identity }) => isPublicMcpTool(tool, publicPatterns) && identity !== void 0 && !isTenantScopedTool(identity)) ? requestedToolListCacheHint : {
|
|
7418
|
+
return !toolPolicy && scopeRules.length === 0 && tools.every(({ tool, identity }) => requiredScopesFor(tool).length === 0 && isPublicMcpTool(tool, publicPatterns) && identity !== void 0 && !isTenantScopedTool(identity)) ? requestedToolListCacheHint : {
|
|
7315
7419
|
...requestedToolListCacheHint,
|
|
7316
7420
|
cacheScope: "private"
|
|
7317
7421
|
};
|
|
@@ -7327,11 +7431,18 @@ function createMcpAppServer(options) {
|
|
|
7327
7431
|
code: MCP_TOOL_ACCESS_DENIED_CODE,
|
|
7328
7432
|
retryable: false
|
|
7329
7433
|
});
|
|
7434
|
+
if (needsConfirm(tool) && args.confirm !== true) return {
|
|
7435
|
+
args,
|
|
7436
|
+
principal,
|
|
7437
|
+
tool,
|
|
7438
|
+
confirmRequired: true
|
|
7439
|
+
};
|
|
7330
7440
|
runWorkflowAssertions(tool, identity, args, principal);
|
|
7331
7441
|
return {
|
|
7332
7442
|
args,
|
|
7333
7443
|
principal,
|
|
7334
|
-
tool
|
|
7444
|
+
tool,
|
|
7445
|
+
confirmRequired: false
|
|
7335
7446
|
};
|
|
7336
7447
|
}
|
|
7337
7448
|
function snapshotPrincipal(principal) {
|
|
@@ -7351,19 +7462,50 @@ function createMcpAppServer(options) {
|
|
|
7351
7462
|
});
|
|
7352
7463
|
}
|
|
7353
7464
|
async function audit(input, principal, outcome, error) {
|
|
7354
|
-
if (!options.onToolCall) return;
|
|
7465
|
+
if (!options.onToolCall && !options.auditSink) return;
|
|
7466
|
+
const reportFailure = (hookError) => {
|
|
7467
|
+
try {
|
|
7468
|
+
options.onToolCallHookError?.(hookError);
|
|
7469
|
+
} catch {}
|
|
7470
|
+
};
|
|
7471
|
+
let event;
|
|
7355
7472
|
try {
|
|
7356
|
-
|
|
7357
|
-
|
|
7358
|
-
|
|
7473
|
+
const snapshot = snapshotPrincipal(principal);
|
|
7474
|
+
const tool = String(input.name).slice(0, 256);
|
|
7475
|
+
let tenant = snapshot?.tenantId;
|
|
7476
|
+
if (options.auditTenantId) try {
|
|
7477
|
+
tenant = options.auditTenantId({
|
|
7478
|
+
principal: snapshot,
|
|
7479
|
+
tool
|
|
7480
|
+
});
|
|
7481
|
+
} catch (tenantError) {
|
|
7482
|
+
reportFailure(tenantError);
|
|
7483
|
+
tenant = snapshot?.tenantId;
|
|
7484
|
+
}
|
|
7485
|
+
event = Object.freeze({
|
|
7486
|
+
tool,
|
|
7487
|
+
principal: snapshot,
|
|
7359
7488
|
identity: identityOf(principal),
|
|
7360
7489
|
outcome,
|
|
7361
|
-
...error ? { error: Object.freeze({ ...error }) } : {}
|
|
7362
|
-
|
|
7490
|
+
...error ? { error: Object.freeze({ ...error }) } : {},
|
|
7491
|
+
tenantId: typeof tenant === "string" && tenant ? tenant : null,
|
|
7492
|
+
argumentKeys: Object.freeze(mcpArgumentKeyNames(input.arguments)),
|
|
7493
|
+
occurredAt: /* @__PURE__ */ new Date()
|
|
7494
|
+
});
|
|
7495
|
+
} catch (buildError) {
|
|
7496
|
+
reportFailure(buildError);
|
|
7497
|
+
return;
|
|
7498
|
+
}
|
|
7499
|
+
if (options.onToolCall) try {
|
|
7500
|
+
await options.onToolCall(event);
|
|
7363
7501
|
} catch (hookError) {
|
|
7364
|
-
|
|
7365
|
-
|
|
7366
|
-
|
|
7502
|
+
reportFailure(hookError);
|
|
7503
|
+
}
|
|
7504
|
+
const skipPersist = !principal && outcome === "denied" && error?.code === "mcp_tool_not_found";
|
|
7505
|
+
if (options.auditSink && !skipPersist) try {
|
|
7506
|
+
await options.auditSink.record(mcpAuditRecordFromEvent(event));
|
|
7507
|
+
} catch (sinkError) {
|
|
7508
|
+
reportFailure(sinkError);
|
|
7367
7509
|
}
|
|
7368
7510
|
}
|
|
7369
7511
|
function deniedInfo(error) {
|
|
@@ -7382,16 +7524,41 @@ function createMcpAppServer(options) {
|
|
|
7382
7524
|
else await audit(input, principal, "error", safeMcpToolError(error));
|
|
7383
7525
|
throw error;
|
|
7384
7526
|
}
|
|
7527
|
+
if (authorized.confirmRequired) {
|
|
7528
|
+
const failure = {
|
|
7529
|
+
code: MCP_CONFIRM_REQUIRED_CODE,
|
|
7530
|
+
message: `Confirmation required: call ${String(input.name).slice(0, 64)} again with confirm: true.`
|
|
7531
|
+
};
|
|
7532
|
+
await audit(input, principal, "denied", failure);
|
|
7533
|
+
return {
|
|
7534
|
+
isError: true,
|
|
7535
|
+
content: [{
|
|
7536
|
+
type: "text",
|
|
7537
|
+
text: failure.message
|
|
7538
|
+
}],
|
|
7539
|
+
structuredContent: { error: failure }
|
|
7540
|
+
};
|
|
7541
|
+
}
|
|
7385
7542
|
let result;
|
|
7386
7543
|
try {
|
|
7544
|
+
const { tool } = authorized;
|
|
7545
|
+
if (needsConfirm(tool) && !workflowToolsByName.has(tool.name) && Object.hasOwn(authorized.args, "confirm")) {
|
|
7546
|
+
const { confirm: _confirm, ...rest } = authorized.args;
|
|
7547
|
+
authorized = {
|
|
7548
|
+
...authorized,
|
|
7549
|
+
args: rest
|
|
7550
|
+
};
|
|
7551
|
+
}
|
|
7387
7552
|
result = await run(authorized);
|
|
7388
7553
|
} catch (error) {
|
|
7389
7554
|
if (error instanceof McpAccessError) await audit(input, principal, "denied", deniedInfo(error));
|
|
7390
7555
|
else await audit(input, principal, "error", safeMcpToolError(error));
|
|
7391
7556
|
throw error;
|
|
7392
7557
|
}
|
|
7393
|
-
if (result.response.isError)
|
|
7394
|
-
|
|
7558
|
+
if (result.response.isError) {
|
|
7559
|
+
const failure = result.failure ?? failureOf(result.response);
|
|
7560
|
+
await audit(input, principal, DENIED_FAILURE_CODES.has(failure.code) ? "denied" : "error", failure);
|
|
7561
|
+
} else await audit(input, principal, "ok");
|
|
7395
7562
|
return result.response;
|
|
7396
7563
|
}
|
|
7397
7564
|
function failureOf(response) {
|
|
@@ -7493,7 +7660,11 @@ function createMcpAppServer(options) {
|
|
|
7493
7660
|
if (origin) for (const { tool, identity } of await catalogTools(false)) {
|
|
7494
7661
|
if (!identity || workflowToolsByName.has(tool.name)) continue;
|
|
7495
7662
|
if (!isTaskOrigin(identity, origin)) continue;
|
|
7496
|
-
if (
|
|
7663
|
+
if (allowedEffects && !allowedEffects.has(mcpToolEffect(tool))) continue;
|
|
7664
|
+
const scopes = requiredScopesFor(tool);
|
|
7665
|
+
const held = principal.scopes;
|
|
7666
|
+
if (scopes.length > 0 && (!Array.isArray(held) || !scopes.every((s) => held.includes(s)))) continue;
|
|
7667
|
+
return;
|
|
7497
7668
|
}
|
|
7498
7669
|
throw new McpAccessError(404, "Unknown MCP task.");
|
|
7499
7670
|
}
|
|
@@ -7608,136 +7779,6 @@ function createDefaultMcpAppServer(options) {
|
|
|
7608
7779
|
});
|
|
7609
7780
|
}
|
|
7610
7781
|
//#endregion
|
|
7611
|
-
|
|
7612
|
-
var MCP_TASKS_EXTENSION = "io.modelcontextprotocol/tasks";
|
|
7613
|
-
var DEFAULT_TOOL_LIST_CACHE_HINT = {
|
|
7614
|
-
ttlMs: 864e5,
|
|
7615
|
-
cacheScope: "private"
|
|
7616
|
-
};
|
|
7617
|
-
async function createMcpProtocolServerForRequest(appServer, options = {}) {
|
|
7618
|
-
const principal = options.principal ?? null;
|
|
7619
|
-
let extensions = {};
|
|
7620
|
-
if (options.extensions) try {
|
|
7621
|
-
const tools = await appServer.listTools({ principal });
|
|
7622
|
-
const projected = await options.extensions({
|
|
7623
|
-
principal,
|
|
7624
|
-
tools
|
|
7625
|
-
});
|
|
7626
|
-
assertExtensionMetadata(projected);
|
|
7627
|
-
if (Object.hasOwn(projected, "io.modelcontextprotocol/tasks")) throw new TypeError("Reserved extension.");
|
|
7628
|
-
extensions = structuredClone(projected);
|
|
7629
|
-
} catch {
|
|
7630
|
-
throw new ProtocolError(ProtocolErrorCode.InternalError, "Extension discovery unavailable.");
|
|
7631
|
-
}
|
|
7632
|
-
return protocolServer(appServer, { principal }, extensions);
|
|
7633
|
-
}
|
|
7634
|
-
async function resolvePrincipal(option, context) {
|
|
7635
|
-
if (typeof option === "function") return await option(context) ?? null;
|
|
7636
|
-
return option ?? null;
|
|
7637
|
-
}
|
|
7638
|
-
function createMcpProtocolServer(appServer, options = {}) {
|
|
7639
|
-
if ("extensions" in options) throw new TypeError("Extension discovery requires createMcpProtocolServerForRequest.");
|
|
7640
|
-
return protocolServer(appServer, options, {});
|
|
7641
|
-
}
|
|
7642
|
-
function protocolServer(appServer, options, extensions) {
|
|
7643
|
-
const tasksEnabled = appServer.tasksEnabled && typeof options.principal !== "function" && Boolean(options.principal?.id);
|
|
7644
|
-
const server = new Server(appServer.serverInfo, {
|
|
7645
|
-
capabilities: {
|
|
7646
|
-
tools: {},
|
|
7647
|
-
...appServer.listResources && appServer.readResource ? { resources: {} } : {},
|
|
7648
|
-
...tasksEnabled || Object.keys(extensions).length ? { extensions: {
|
|
7649
|
-
...extensions,
|
|
7650
|
-
...tasksEnabled ? { [MCP_TASKS_EXTENSION]: {} } : {}
|
|
7651
|
-
} } : {}
|
|
7652
|
-
},
|
|
7653
|
-
cacheHints: { "tools/list": DEFAULT_TOOL_LIST_CACHE_HINT }
|
|
7654
|
-
});
|
|
7655
|
-
server.setRequestHandler("tools/list", async (_request, context) => {
|
|
7656
|
-
const principal = await resolvePrincipal(options.principal, context);
|
|
7657
|
-
const cacheHint = await appServer.getToolsListCacheHint?.() ?? DEFAULT_TOOL_LIST_CACHE_HINT;
|
|
7658
|
-
return {
|
|
7659
|
-
tools: [...await appServer.listTools({ principal })].sort((left, right) => compareMcpToolNames(left.name, right.name)),
|
|
7660
|
-
...cacheHint
|
|
7661
|
-
};
|
|
7662
|
-
});
|
|
7663
|
-
if (appServer.listResources && appServer.readResource) {
|
|
7664
|
-
server.setRequestHandler("resources/list", async (_request, context) => ({
|
|
7665
|
-
resources: await appServer.listResources({ principal: await resolvePrincipal(options.principal, context) }),
|
|
7666
|
-
ttlMs: 0,
|
|
7667
|
-
cacheScope: "private"
|
|
7668
|
-
}));
|
|
7669
|
-
server.setRequestHandler("resources/read", async (request, context) => {
|
|
7670
|
-
try {
|
|
7671
|
-
return {
|
|
7672
|
-
contents: [await appServer.readResource({
|
|
7673
|
-
uri: request.params.uri,
|
|
7674
|
-
principal: await resolvePrincipal(options.principal, context)
|
|
7675
|
-
})],
|
|
7676
|
-
ttlMs: 0,
|
|
7677
|
-
cacheScope: "private"
|
|
7678
|
-
};
|
|
7679
|
-
} catch (error) {
|
|
7680
|
-
if (error instanceof McpAccessError) throw new ProtocolError(ProtocolErrorCode.InvalidParams, "MCP resource is not available.");
|
|
7681
|
-
throw error;
|
|
7682
|
-
}
|
|
7683
|
-
});
|
|
7684
|
-
}
|
|
7685
|
-
server.setRequestHandler("tools/call", async (request, context) => {
|
|
7686
|
-
try {
|
|
7687
|
-
return await appServer.callTool({
|
|
7688
|
-
name: request.params.name,
|
|
7689
|
-
arguments: request.params.arguments,
|
|
7690
|
-
principal: await resolvePrincipal(options.principal, context)
|
|
7691
|
-
});
|
|
7692
|
-
} catch (error) {
|
|
7693
|
-
if (error instanceof McpAccessError) {
|
|
7694
|
-
const { code, retryable } = error.metadata;
|
|
7695
|
-
throw new ProtocolError(error.status === 404 ? ProtocolErrorCode.InvalidParams : ProtocolErrorCode.InvalidRequest, error.message, {
|
|
7696
|
-
...typeof code === "string" ? { code } : {},
|
|
7697
|
-
...typeof retryable === "boolean" ? { retryable } : {}
|
|
7698
|
-
});
|
|
7699
|
-
}
|
|
7700
|
-
throw error;
|
|
7701
|
-
}
|
|
7702
|
-
});
|
|
7703
|
-
return server;
|
|
7704
|
-
}
|
|
7705
|
-
function assertExtensionMetadata(value) {
|
|
7706
|
-
let budget = 65536;
|
|
7707
|
-
const visit = (v, depth) => {
|
|
7708
|
-
if (--budget < 0 || depth > 16) throw new TypeError("Extension metadata exceeds limits.");
|
|
7709
|
-
if (v === null || typeof v === "boolean") return;
|
|
7710
|
-
if (typeof v === "number" && Number.isFinite(v)) return;
|
|
7711
|
-
if (typeof v === "string") {
|
|
7712
|
-
budget -= new TextEncoder().encode(v).length;
|
|
7713
|
-
if (budget < 0) throw new TypeError("Extension metadata exceeds limits.");
|
|
7714
|
-
return;
|
|
7715
|
-
}
|
|
7716
|
-
if (Array.isArray(v)) {
|
|
7717
|
-
if (v.length > 1024) throw new TypeError("Extension array exceeds limits.");
|
|
7718
|
-
for (const item of v) visit(item, depth + 1);
|
|
7719
|
-
return;
|
|
7720
|
-
}
|
|
7721
|
-
if (!v || typeof v !== "object" || ![Object.prototype, null].includes(Object.getPrototypeOf(v))) throw new TypeError("Expected plain extension object.");
|
|
7722
|
-
const names = Object.keys(v);
|
|
7723
|
-
if (names.length > 256) throw new TypeError("Extension object exceeds limits.");
|
|
7724
|
-
for (const name of names) {
|
|
7725
|
-
if ([
|
|
7726
|
-
"__proto__",
|
|
7727
|
-
"constructor",
|
|
7728
|
-
"prototype"
|
|
7729
|
-
].includes(name)) throw new TypeError("Unsafe extension key.");
|
|
7730
|
-
const descriptor = Object.getOwnPropertyDescriptor(v, name);
|
|
7731
|
-
if (!descriptor || !("value" in descriptor)) throw new TypeError("Expected inert extension metadata.");
|
|
7732
|
-
visit(name, depth + 1);
|
|
7733
|
-
visit(descriptor.value, depth + 1);
|
|
7734
|
-
}
|
|
7735
|
-
};
|
|
7736
|
-
visit(value, 0);
|
|
7737
|
-
if (!value || typeof value !== "object" || Array.isArray(value)) throw new TypeError("Expected extension map.");
|
|
7738
|
-
for (const [name, entry] of Object.entries(value)) if (!name.trim() || !entry || typeof entry !== "object" || Array.isArray(entry)) throw new TypeError("Expected named extension object.");
|
|
7739
|
-
}
|
|
7740
|
-
//#endregion
|
|
7741
|
-
export { MCP_APP_RESOURCE_MAX_BYTES as _, createDefaultMcpAppServer as a, createMcpWorkflowTool as b, createMcpAppServer as c, isAllowedCoreTool as d, isPublicMcpTool as f, mcpToolEffect as g, matchesToolPattern as h, DEFAULT_MCP_APP_SERVER_INFO as i, MCP_TOOL_EFFECTS as l, isReadOnlyToolName as m, createMcpProtocolServer as n, mcpAllowedClassNames as o, isPublicToolName as p, createMcpProtocolServerForRequest as r, mcpPrincipalScopePolicy as s, MCP_TASKS_EXTENSION as t, classNamePrefixes as u, MCP_APP_RESOURCE_MIME as v, prepareMcpAppResource as y };
|
|
7782
|
+
export { prepareMcpAppResource as _, createMcpAppServer as a, mcpArgumentKeyNames as b, compareMcpToolNames as c, isPublicToolName as d, isReadOnlyToolName as f, MCP_APP_RESOURCE_MIME as g, MCP_APP_RESOURCE_MAX_BYTES as h, mcpPrincipalScopePolicy as i, isAllowedCoreTool as l, mcpToolEffect as m, createDefaultMcpAppServer as n, MCP_TOOL_EFFECTS as o, matchesToolPattern as p, mcpAllowedClassNames as r, classNamePrefixes as s, DEFAULT_MCP_APP_SERVER_INFO as t, isPublicMcpTool as u, createMcpWorkflowTool as v, mcpAuditRecordFromEvent as x, createInMemoryMcpAuditSink as y };
|
|
7742
7783
|
|
|
7743
|
-
//# sourceMappingURL=
|
|
7784
|
+
//# sourceMappingURL=defaults-i3kpRFpB.js.map
|