@frontmcp/sdk 1.13.1 → 1.13.2
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/auth/auth-ui/auth-ui.render.d.ts +5 -2
- package/auth/auth-ui/auth-ui.render.d.ts.map +1 -1
- package/auth/auth-ui/index.d.ts +1 -1
- package/auth/auth-ui/index.d.ts.map +1 -1
- package/auth/flows/oauth.authorize.flow.d.ts +1 -0
- package/auth/flows/oauth.authorize.flow.d.ts.map +1 -1
- package/auth/flows/oauth.callback.flow.d.ts.map +1 -1
- package/auth/flows/oauth.register.flow.d.ts +18 -1
- package/auth/flows/oauth.register.flow.d.ts.map +1 -1
- package/auth/flows/well-known.prm.flow.d.ts +4 -0
- package/auth/flows/well-known.prm.flow.d.ts.map +1 -1
- package/auth/instances/instance.local-primary-auth.d.ts +4 -3
- package/auth/instances/instance.local-primary-auth.d.ts.map +1 -1
- package/auth/instances/instance.remote-primary-auth.d.ts.map +1 -1
- package/browser/auth/auth-ui/auth-ui.render.mjs +9 -8
- package/browser/auth/auth-ui/index.mjs +4 -2
- package/browser/auth/federated-steps.mjs +1 -1
- package/browser/auth/flows/oauth.authorize.flow.mjs +15 -8
- package/browser/auth/flows/oauth.callback.flow.mjs +10 -3
- package/browser/auth/flows/oauth.connect.flow.mjs +1 -1
- package/browser/auth/flows/oauth.provider-callback.flow.mjs +1 -1
- package/browser/auth/flows/oauth.register.flow.mjs +5 -1
- package/browser/auth/flows/well-known.prm.flow.mjs +22 -3
- package/browser/auth/instances/instance.local-primary-auth.mjs +20 -9
- package/browser/auth/instances/instance.remote-primary-auth.mjs +5 -0
- package/browser/common/decorators/http-route.decorator.mjs +9 -1
- package/browser/common/index.mjs +1 -1
- package/browser/common/metadata/index.mjs +1 -1
- package/browser/common/metadata/materialized.metadata.mjs +13 -0
- package/browser/common/metadata/resource.metadata.mjs +6 -5
- package/browser/common/tokens/resource.tokens.mjs +1 -0
- package/browser/common/types/options/http/schema.mjs +17 -1
- package/browser/common/types/options/transport/schema.mjs +11 -1
- package/browser/direct/direct-server.mjs +15 -11
- package/browser/errors/index.mjs +3 -1
- package/browser/errors/mcp.error.mjs +7 -0
- package/browser/errors/transport.errors.mjs +7 -1
- package/browser/event/event-scope.helper.mjs +4 -2
- package/browser/event/event-subscription-info.mjs +55 -0
- package/browser/event/event.registry.mjs +18 -0
- package/browser/event/flows/event-ended.flow.mjs +89 -0
- package/browser/event/flows/event-unsubscribe.flow.mjs +7 -1
- package/browser/event/poll/event-poll-leases.mjs +11 -0
- package/browser/event/stream/event-stream.mjs +4 -0
- package/browser/event/stream/event-streams.mjs +13 -0
- package/browser/event/subscription/event-subscription-termination.mjs +1 -0
- package/browser/event/subscription/event-subscriptions.mjs +18 -2
- package/browser/front-mcp/front-mcp.mjs +13 -0
- package/browser/http-route/flows/http.route.flow.mjs +71 -8
- package/browser/http-route/http-route.table.mjs +61 -15
- package/browser/http-route/people-session-store.mjs +47 -0
- package/browser/http-route/people-sign-in.routes.mjs +3 -2
- package/browser/http-route/sse-route.stream.mjs +19 -8
- package/browser/index.cjs +3011 -2017
- package/browser/index.mjs +24 -3
- package/browser/notification/notification.service.mjs +8 -1
- package/browser/resource/materialized/materialized-resource.context.mjs +3 -1
- package/browser/resource/materialized/materialized-resources.mjs +53 -27
- package/browser/resource/materialized/resource-store.mjs +269 -17
- package/browser/resource/resource.registry.mjs +31 -0
- package/browser/scope/http-scope.helper.mjs +2 -1
- package/browser/scope/scope.instance.mjs +2 -2
- package/browser/server/security/host-validation.mjs +4 -1
- package/browser/server/server.instance.mjs +22 -4
- package/browser/subsystems.mjs +21 -2
- package/browser/tool/flows/call-tool.flow.mjs +3 -3
- package/browser/tool/undeclared-arguments-message.mjs +10 -8
- package/browser/tool/undeclared-arguments.mjs +33 -0
- package/browser/transport/adapters/transport.sse.adapter.mjs +5 -2
- package/browser/transport/adapters/transport.streamable-http.adapter.mjs +3 -1
- package/browser/transport/flows/handle.mcp-20260728.flow.mjs +7 -2
- package/browser/transport/mcp-20260728/subscriptions.mjs +24 -4
- package/browser/transport/stream-keep-alive.mjs +70 -0
- package/browser/transport/transport.registry.mjs +9 -7
- package/browser/transport/web-standard-mcp.mjs +3 -1
- package/common/decorators/http-route.decorator.d.ts +2 -1
- package/common/decorators/http-route.decorator.d.ts.map +1 -1
- package/common/interfaces/http-route.interface.d.ts +9 -1
- package/common/interfaces/http-route.interface.d.ts.map +1 -1
- package/common/metadata/app.metadata.d.ts +8 -0
- package/common/metadata/app.metadata.d.ts.map +1 -1
- package/common/metadata/front-mcp.metadata.d.ts +197 -0
- package/common/metadata/front-mcp.metadata.d.ts.map +1 -1
- package/common/metadata/http-route.metadata.d.ts +11 -3
- package/common/metadata/http-route.metadata.d.ts.map +1 -1
- package/common/metadata/materialized.metadata.d.ts +21 -0
- package/common/metadata/materialized.metadata.d.ts.map +1 -1
- package/common/metadata/resource.metadata.d.ts +8 -1
- package/common/metadata/resource.metadata.d.ts.map +1 -1
- package/common/tokens/resource.tokens.d.ts +1 -0
- package/common/tokens/resource.tokens.d.ts.map +1 -1
- package/common/types/options/auth/index.d.ts +1 -1
- package/common/types/options/auth/index.d.ts.map +1 -1
- package/common/types/options/http/index.d.ts +1 -1
- package/common/types/options/http/index.d.ts.map +1 -1
- package/common/types/options/http/interfaces.d.ts +41 -1
- package/common/types/options/http/interfaces.d.ts.map +1 -1
- package/common/types/options/http/schema.d.ts +12 -0
- package/common/types/options/http/schema.d.ts.map +1 -1
- package/common/types/options/transport/index.d.ts +1 -1
- package/common/types/options/transport/index.d.ts.map +1 -1
- package/common/types/options/transport/interfaces.d.ts +24 -0
- package/common/types/options/transport/interfaces.d.ts.map +1 -1
- package/common/types/options/transport/schema.d.ts +3 -0
- package/common/types/options/transport/schema.d.ts.map +1 -1
- package/direct/direct-server.d.ts +4 -2
- package/direct/direct-server.d.ts.map +1 -1
- package/direct/direct.types.d.ts +7 -0
- package/direct/direct.types.d.ts.map +1 -1
- package/errors/index.d.ts +1 -1
- package/errors/index.d.ts.map +1 -1
- package/errors/mcp.error.d.ts +4 -0
- package/errors/mcp.error.d.ts.map +1 -1
- package/errors/transport.errors.d.ts +4 -0
- package/errors/transport.errors.d.ts.map +1 -1
- package/esm/index.mjs +42 -42
- package/event/event-scope.helper.d.ts.map +1 -1
- package/event/event-subscription-info.d.ts +72 -0
- package/event/event-subscription-info.d.ts.map +1 -0
- package/event/event.registry.d.ts +7 -0
- package/event/event.registry.d.ts.map +1 -1
- package/event/flows/event-ended.flow.d.ts +56 -0
- package/event/flows/event-ended.flow.d.ts.map +1 -0
- package/event/flows/event-unsubscribe.flow.d.ts +4 -1
- package/event/flows/event-unsubscribe.flow.d.ts.map +1 -1
- package/event/index.d.ts +3 -1
- package/event/index.d.ts.map +1 -1
- package/event/poll/event-poll-leases.d.ts +3 -0
- package/event/poll/event-poll-leases.d.ts.map +1 -1
- package/event/stream/event-stream.d.ts +2 -0
- package/event/stream/event-stream.d.ts.map +1 -1
- package/event/stream/event-streams.d.ts +3 -0
- package/event/stream/event-streams.d.ts.map +1 -1
- package/event/subscription/event-subscription-termination.d.ts +4 -1
- package/event/subscription/event-subscription-termination.d.ts.map +1 -1
- package/event/subscription/event-subscriptions.d.ts +19 -1
- package/event/subscription/event-subscriptions.d.ts.map +1 -1
- package/front-mcp/front-mcp.d.ts.map +1 -1
- package/http-route/flows/http.route.flow.d.ts +12 -4
- package/http-route/flows/http.route.flow.d.ts.map +1 -1
- package/http-route/http-route.table.d.ts +10 -3
- package/http-route/http-route.table.d.ts.map +1 -1
- package/http-route/people-session-store.d.ts +24 -0
- package/http-route/people-session-store.d.ts.map +1 -0
- package/http-route/people-sign-in.routes.d.ts +2 -2
- package/http-route/people-sign-in.routes.d.ts.map +1 -1
- package/http-route/sse-route.stream.d.ts +6 -1
- package/http-route/sse-route.stream.d.ts.map +1 -1
- package/index.d.ts +16 -8
- package/index.d.ts.map +1 -1
- package/index.js +43 -43
- package/notification/notification.service.d.ts.map +1 -1
- package/package.json +10 -10
- package/resource/materialized/index.d.ts +1 -1
- package/resource/materialized/index.d.ts.map +1 -1
- package/resource/materialized/materialized-resource.context.d.ts +1 -0
- package/resource/materialized/materialized-resource.context.d.ts.map +1 -1
- package/resource/materialized/materialized-resources.d.ts +11 -5
- package/resource/materialized/materialized-resources.d.ts.map +1 -1
- package/resource/materialized/resource-store.d.ts +31 -1
- package/resource/materialized/resource-store.d.ts.map +1 -1
- package/resource/resource.registry.d.ts +11 -0
- package/resource/resource.registry.d.ts.map +1 -1
- package/scope/http-scope.helper.d.ts +1 -1
- package/scope/http-scope.helper.d.ts.map +1 -1
- package/scope/scope.instance.d.ts.map +1 -1
- package/server/adapters/polyfills/browser-express-host.d.ts +3 -0
- package/server/adapters/polyfills/browser-express-host.d.ts.map +1 -1
- package/server/adapters/polyfills/node-express-host.d.ts +1 -0
- package/server/adapters/polyfills/node-express-host.d.ts.map +1 -1
- package/server/adapters/user-content.listener.d.ts +26 -0
- package/server/adapters/user-content.listener.d.ts.map +1 -0
- package/server/security/host-validation.d.ts.map +1 -1
- package/server/server.instance.d.ts +1 -0
- package/server/server.instance.d.ts.map +1 -1
- package/subsystems.d.ts +1 -1
- package/subsystems.d.ts.map +1 -1
- package/tool/undeclared-arguments-message.d.ts +8 -5
- package/tool/undeclared-arguments-message.d.ts.map +1 -1
- package/tool/undeclared-arguments.d.ts +2 -0
- package/tool/undeclared-arguments.d.ts.map +1 -1
- package/transport/adapters/base-sse-transport.d.ts +4 -0
- package/transport/adapters/base-sse-transport.d.ts.map +1 -1
- package/transport/adapters/polyfills/browser-sse-transport.d.ts +1 -0
- package/transport/adapters/polyfills/browser-sse-transport.d.ts.map +1 -1
- package/transport/adapters/streamable-http-transport.d.ts +2 -0
- package/transport/adapters/streamable-http-transport.d.ts.map +1 -1
- package/transport/adapters/transport.sse.adapter.d.ts.map +1 -1
- package/transport/adapters/transport.streamable-http.adapter.d.ts.map +1 -1
- package/transport/flows/handle.mcp-20260728.flow.d.ts.map +1 -1
- package/transport/mcp-20260728/subscriptions.d.ts +2 -0
- package/transport/mcp-20260728/subscriptions.d.ts.map +1 -1
- package/transport/stream-keep-alive.d.ts +13 -0
- package/transport/stream-keep-alive.d.ts.map +1 -0
- package/transport/transport.registry.d.ts +1 -1
- package/transport/transport.registry.d.ts.map +1 -1
- package/transport/web-standard-mcp.d.ts.map +1 -1
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// libs/sdk/src/event/poll/event-poll-leases.ts
|
|
2
2
|
import { getRuntimeContext } from "@frontmcp/utils";
|
|
3
|
+
import { heldSubscriptionInfoOf } from "../event-subscription-info.mjs";
|
|
3
4
|
var POLL_LEASE_MIN_MS = 3e5;
|
|
4
5
|
var POLL_LEASE_INTERVALS = 5;
|
|
5
6
|
var POLL_LEASE_SWEEP_INTERVAL_MS = 6e4;
|
|
@@ -37,6 +38,16 @@ var EventPollLeases = class {
|
|
|
37
38
|
has(subscriptionId) {
|
|
38
39
|
return this.leases.has(subscriptionId) || this.starting.has(subscriptionId);
|
|
39
40
|
}
|
|
41
|
+
/** The leases this process holds, of every event or of one, with when each lapses unless a poll renews it. */
|
|
42
|
+
list(eventName) {
|
|
43
|
+
const listed = [];
|
|
44
|
+
for (const lease of this.leases.values()) {
|
|
45
|
+
if (eventName !== void 0 && lease.subscription.name !== eventName) continue;
|
|
46
|
+
const held = heldSubscriptionInfoOf(lease.subscription, lease.principalKey, lease.arguments);
|
|
47
|
+
listed.push({ ...held, mode: "poll", expiresAt: lease.expiresAt });
|
|
48
|
+
}
|
|
49
|
+
return listed;
|
|
50
|
+
}
|
|
40
51
|
/** Renew the lease of a poll, or start it: a new lease calls the event's `onSubscribe` first. */
|
|
41
52
|
async renew(request) {
|
|
42
53
|
const { subscription, leaseMs } = request;
|
|
@@ -42,6 +42,10 @@ var EventStream = class {
|
|
|
42
42
|
get eventName() {
|
|
43
43
|
return this.options.event.name;
|
|
44
44
|
}
|
|
45
|
+
/** The stream's arguments, as the event's input schema parsed them. */
|
|
46
|
+
get arguments() {
|
|
47
|
+
return this.options.arguments;
|
|
48
|
+
}
|
|
45
49
|
/** The owner's key; undefined for an anonymous reader. */
|
|
46
50
|
get principalKey() {
|
|
47
51
|
return this.options.reader.principalKey;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// libs/sdk/src/event/stream/event-streams.ts
|
|
2
2
|
import { EventOccurrenceSchema } from "@frontmcp/protocol";
|
|
3
3
|
import { randomUUID } from "@frontmcp/utils";
|
|
4
|
+
import { heldSubscriptionInfoOf } from "../event-subscription-info.mjs";
|
|
4
5
|
var EVENT_RELAY_CHANNEL = "occurrences";
|
|
5
6
|
var MAX_OPEN_STREAMS = 1e4;
|
|
6
7
|
var EventStreams = class {
|
|
@@ -26,6 +27,18 @@ var EventStreams = class {
|
|
|
26
27
|
get size() {
|
|
27
28
|
return this.held.size;
|
|
28
29
|
}
|
|
30
|
+
/** The streams this process holds open or is opening, of every event or of one. */
|
|
31
|
+
list(eventName) {
|
|
32
|
+
const listed = [];
|
|
33
|
+
for (const stream of this.held) {
|
|
34
|
+
if (stream.isClosed || eventName !== void 0 && stream.eventName !== eventName) continue;
|
|
35
|
+
listed.push({
|
|
36
|
+
...heldSubscriptionInfoOf(stream.subscription, stream.principalKey, stream.arguments),
|
|
37
|
+
mode: "push"
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
return listed;
|
|
41
|
+
}
|
|
29
42
|
/** Listen for the occurrences other processes publish, when the storage can carry them. */
|
|
30
43
|
async start() {
|
|
31
44
|
const { storage } = this.options;
|
|
@@ -13,6 +13,7 @@ async function terminateEventSubscription(args) {
|
|
|
13
13
|
const removed = await subscriptions?.store.delete(subscription.id);
|
|
14
14
|
if (!removed || !subscriptions || !outbox) return;
|
|
15
15
|
await subscriptions.notifyUnsubscribed(removed, "terminated");
|
|
16
|
+
await subscriptions.notifyEnded({ record: removed, reason: "terminated", termination: error });
|
|
16
17
|
metrics?.count("terminated", removed.eventName);
|
|
17
18
|
logger.warn(
|
|
18
19
|
`Subscription ${removed.id} to event "${removed.eventName}" is terminated: ${error.message} (${String(error.data["reason"])})`
|
|
@@ -82,10 +82,26 @@ var EventSubscriptions = class {
|
|
|
82
82
|
this.logger.warn(`onUnsubscribe of event "${record.eventName}" failed for subscription ${record.id}`, error);
|
|
83
83
|
}
|
|
84
84
|
}
|
|
85
|
-
/**
|
|
85
|
+
/**
|
|
86
|
+
* Run the `events:ended` flow for a subscription this caller removed, after its event was told. Only the caller the
|
|
87
|
+
* store gave the removed subscription to calls it, so it runs once across every node. A failure there is logged,
|
|
88
|
+
* never thrown.
|
|
89
|
+
*/
|
|
90
|
+
async notifyEnded(ending) {
|
|
91
|
+
const { ended } = this.options;
|
|
92
|
+
if (!ended) return;
|
|
93
|
+
try {
|
|
94
|
+
await ended(ending);
|
|
95
|
+
} catch (error) {
|
|
96
|
+
this.logger.warn(`events:ended failed for subscription ${ending.record.id} (${ending.reason})`, error);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/** End one subscription whose lifetime is over, telling its event and `events:ended`, once across every node. */
|
|
86
100
|
async expire(id, now = Date.now()) {
|
|
87
101
|
const removed = await this.store.deleteExpired(id, now);
|
|
88
|
-
if (removed)
|
|
102
|
+
if (!removed) return;
|
|
103
|
+
await this.notifyUnsubscribed(removed, "expired");
|
|
104
|
+
await this.notifyEnded({ record: removed, reason: "expired" });
|
|
89
105
|
}
|
|
90
106
|
/** End every subscription whose lifetime is over. */
|
|
91
107
|
async sweep(now = Date.now()) {
|
|
@@ -69,6 +69,12 @@ async function parseCliConfig(options, { fileLog }) {
|
|
|
69
69
|
}
|
|
70
70
|
return parsedConfig;
|
|
71
71
|
}
|
|
72
|
+
function warnUserContentIgnored(log, http, entry) {
|
|
73
|
+
if (!http?.userContent) return;
|
|
74
|
+
log?.warn(
|
|
75
|
+
`http.userContent is ignored: ${entry} opens no listener of its own. Only a Node HTTP server (bootstrap, runUnixSocket) serves it.`
|
|
76
|
+
);
|
|
77
|
+
}
|
|
72
78
|
async function exitAfterDetachedRun(config, runId) {
|
|
73
79
|
const { runDetachedJob } = await import("../job/runner/detached-job.worker.mjs");
|
|
74
80
|
process.exit(await runDetachedJob(config, runId));
|
|
@@ -357,6 +363,7 @@ var FrontMcpInstance = class _FrontMcpInstance {
|
|
|
357
363
|
await frontMcp.wireMetricsService(server);
|
|
358
364
|
server.prepare();
|
|
359
365
|
await frontMcp.emitProvidersReady();
|
|
366
|
+
warnUserContentIgnored(frontMcp.log, frontMcp.config.http, "a serverless handler (createHandler)");
|
|
360
367
|
frontMcp.log?.info("FrontMCP handler created (serverless mode)");
|
|
361
368
|
return server.getHandler();
|
|
362
369
|
}
|
|
@@ -404,6 +411,11 @@ var FrontMcpInstance = class _FrontMcpInstance {
|
|
|
404
411
|
const metricsService = await frontMcp.createMetricsService();
|
|
405
412
|
const metricsConfig = frontMcp.config.metrics;
|
|
406
413
|
await frontMcp.emitProvidersReady();
|
|
414
|
+
warnUserContentIgnored(
|
|
415
|
+
frontMcp.log,
|
|
416
|
+
frontMcp.config.http,
|
|
417
|
+
"a fetch handler for Workers and edge runtimes (createFetchHandler)"
|
|
418
|
+
);
|
|
407
419
|
frontMcp.log?.info("FrontMCP fetch handler created (web-standard transport)");
|
|
408
420
|
return createServerFetchHandler(
|
|
409
421
|
scope,
|
|
@@ -693,6 +705,7 @@ var FrontMcpInstance = class _FrontMcpInstance {
|
|
|
693
705
|
const frontMcp = new _FrontMcpInstance(parsedConfig);
|
|
694
706
|
await frontMcp.ready;
|
|
695
707
|
startupCleanups.push(() => frontMcp.releaseWithin(STDIO_SHUTDOWN_DEADLINE_MS));
|
|
708
|
+
warnUserContentIgnored(frontMcp.log, options.http, "a stdio server (runStdio)");
|
|
696
709
|
const primaryScope = frontMcp.getPrimaryScope();
|
|
697
710
|
if (!primaryScope) {
|
|
698
711
|
throw new InternalMcpError("No scopes initialized. Ensure at least one app is configured.");
|
|
@@ -14,6 +14,8 @@ import { PEOPLE_SESSION_COOKIE } from "@frontmcp/auth";
|
|
|
14
14
|
import { internalZ as z } from "@frontmcp/lazy-zod";
|
|
15
15
|
import { getCookie } from "@frontmcp/utils";
|
|
16
16
|
import {
|
|
17
|
+
authInfoFromAuthorization,
|
|
18
|
+
challengeHeaders,
|
|
17
19
|
enforceGlobalRateLimit,
|
|
18
20
|
enforceIpFilter,
|
|
19
21
|
Flow,
|
|
@@ -31,12 +33,22 @@ import { corsAllowedOrigins } from "../../server/security/resolve-allowed-hosts.
|
|
|
31
33
|
import { isSameOriginRequest } from "../../server/security/same-origin.mjs";
|
|
32
34
|
import { SseRouteStream } from "../sse-route.stream.mjs";
|
|
33
35
|
var plan = {
|
|
34
|
-
pre: [
|
|
36
|
+
pre: [
|
|
37
|
+
"checkIpFilter",
|
|
38
|
+
"findRoute",
|
|
39
|
+
"acquireQuota",
|
|
40
|
+
"checkSameOrigin",
|
|
41
|
+
"authenticatePerson",
|
|
42
|
+
"authenticateBearer",
|
|
43
|
+
"checkAuthorities"
|
|
44
|
+
],
|
|
35
45
|
execute: ["handle"]
|
|
36
46
|
};
|
|
37
47
|
var httpRouteStateSchema = z.object({
|
|
38
48
|
route: z.custom(),
|
|
39
|
-
person: z.custom().optional()
|
|
49
|
+
person: z.custom().optional(),
|
|
50
|
+
// The caller the server's auth verified, on an `auth: 'bearer'` route.
|
|
51
|
+
caller: z.custom().optional()
|
|
40
52
|
});
|
|
41
53
|
var name = "http:route";
|
|
42
54
|
var { Stage } = FlowHooksOf(name);
|
|
@@ -87,11 +99,43 @@ var HttpRouteFlow = class extends FlowBase {
|
|
|
87
99
|
}
|
|
88
100
|
async authenticatePerson() {
|
|
89
101
|
const { auth } = this.state.required.route;
|
|
90
|
-
if (auth === "public") return;
|
|
91
|
-
const person = this.verifyPerson(auth);
|
|
102
|
+
if (auth === "public" || auth === "bearer") return;
|
|
103
|
+
const person = await this.verifyPerson(auth);
|
|
92
104
|
if (!person) throw new PersonNotSignedInError();
|
|
93
105
|
this.state.set("person", person);
|
|
94
106
|
}
|
|
107
|
+
async authenticateBearer() {
|
|
108
|
+
if (this.state.required.route.auth !== "bearer") return;
|
|
109
|
+
const verified = await this.scope.runFlow("session:verify", {
|
|
110
|
+
request: this.httpRequest,
|
|
111
|
+
sessionless: true
|
|
112
|
+
});
|
|
113
|
+
if (verified?.kind !== "authorized") {
|
|
114
|
+
const headers = challengeHeaders(verified?.prmMetadataHeader);
|
|
115
|
+
this.respond(
|
|
116
|
+
verified?.kind === "forbidden" ? httpRespond.forbidden({ headers }) : httpRespond.unauthorized({ headers })
|
|
117
|
+
);
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
const caller = authInfoFromAuthorization(verified.authorization);
|
|
121
|
+
this.tryGetContext()?.recordAuthorization(caller);
|
|
122
|
+
this.state.set("caller", caller);
|
|
123
|
+
}
|
|
124
|
+
async checkAuthorities() {
|
|
125
|
+
const { route } = this.state.required;
|
|
126
|
+
if (route.authorities === void 0) return;
|
|
127
|
+
const engine = this.scope.authoritiesEngine;
|
|
128
|
+
const contextBuilder = this.scope.authoritiesContextBuilder;
|
|
129
|
+
const result = engine && contextBuilder ? await engine.evaluate(
|
|
130
|
+
route.authorities,
|
|
131
|
+
contextBuilder.build(this.state.caller ?? {}, authoritiesInput(this.httpRequest))
|
|
132
|
+
) : void 0;
|
|
133
|
+
if (result?.granted) return;
|
|
134
|
+
this.scope.logger.verbose(
|
|
135
|
+
`HTTP route ${route.method} ${route.path} refused a caller: ${result?.deniedBy ?? "no authorities engine"}`
|
|
136
|
+
);
|
|
137
|
+
this.respond(httpRespond.forbidden());
|
|
138
|
+
}
|
|
95
139
|
async handle() {
|
|
96
140
|
const { route } = this.state.required;
|
|
97
141
|
const { person } = this.state;
|
|
@@ -100,10 +144,11 @@ var HttpRouteFlow = class extends FlowBase {
|
|
|
100
144
|
const context = new route.target({
|
|
101
145
|
providers: new FlowContextProviders(route.providers, views.context),
|
|
102
146
|
logger: this.scope.logger.child(`HttpRoute ${route.method} ${route.path}`),
|
|
103
|
-
authInfo: {},
|
|
147
|
+
authInfo: this.state.caller ?? {},
|
|
104
148
|
request: this.httpRequest,
|
|
105
149
|
person
|
|
106
150
|
});
|
|
151
|
+
if (route.auth === "bearer") await context.loadAuthContext?.();
|
|
107
152
|
this.respond(
|
|
108
153
|
route.sse ? this.openStream(route, route.sse, context) : this.toHttpOutput(route, await context.handle())
|
|
109
154
|
);
|
|
@@ -135,11 +180,18 @@ var HttpRouteFlow = class extends FlowBase {
|
|
|
135
180
|
};
|
|
136
181
|
}
|
|
137
182
|
/** The person signed in for routes of `auth`: a session for a subject opens `'subject'` routes only. */
|
|
138
|
-
verifyPerson(auth) {
|
|
183
|
+
async verifyPerson(auth) {
|
|
139
184
|
const cookie = getCookie(this.httpRequest.headers["cookie"], PEOPLE_SESSION_COOKIE);
|
|
140
|
-
const person = this.scope.httpRouteTable?.people?.
|
|
185
|
+
const person = await this.scope.httpRouteTable?.people?.findSession(cookie);
|
|
141
186
|
return person && person.subject !== void 0 === (auth === "subject") ? person : void 0;
|
|
142
187
|
}
|
|
188
|
+
/** What keeps a stream open: the person's session, or until the bearer token expires. */
|
|
189
|
+
streamAuthorized(route) {
|
|
190
|
+
if (route.auth === "public") return void 0;
|
|
191
|
+
if (route.auth !== "bearer") return async () => await this.verifyPerson(route.auth) !== void 0;
|
|
192
|
+
const expiresAt = this.state.caller?.expiresAt;
|
|
193
|
+
return expiresAt === void 0 ? void 0 : () => Date.now() < expiresAt * 1e3;
|
|
194
|
+
}
|
|
143
195
|
openStream(route, sse, context) {
|
|
144
196
|
const table = this.scope.httpRouteTable;
|
|
145
197
|
const stream = new SseRouteStream({
|
|
@@ -152,7 +204,7 @@ var HttpRouteFlow = class extends FlowBase {
|
|
|
152
204
|
heartbeatMs: sse.heartbeatMs,
|
|
153
205
|
onError: (error) => this.scope.logger.warn(`SSE route ${route.method} ${route.path} failed to make its view`, error),
|
|
154
206
|
onClose: () => table?.untrack(stream),
|
|
155
|
-
authorized:
|
|
207
|
+
authorized: this.streamAuthorized(route)
|
|
156
208
|
});
|
|
157
209
|
table?.track(stream);
|
|
158
210
|
return {
|
|
@@ -180,6 +232,12 @@ __decorateClass([
|
|
|
180
232
|
__decorateClass([
|
|
181
233
|
Stage("authenticatePerson")
|
|
182
234
|
], HttpRouteFlow.prototype, "authenticatePerson", 1);
|
|
235
|
+
__decorateClass([
|
|
236
|
+
Stage("authenticateBearer")
|
|
237
|
+
], HttpRouteFlow.prototype, "authenticateBearer", 1);
|
|
238
|
+
__decorateClass([
|
|
239
|
+
Stage("checkAuthorities")
|
|
240
|
+
], HttpRouteFlow.prototype, "checkAuthorities", 1);
|
|
183
241
|
__decorateClass([
|
|
184
242
|
Stage("handle")
|
|
185
243
|
], HttpRouteFlow.prototype, "handle", 1);
|
|
@@ -193,6 +251,11 @@ HttpRouteFlow = __decorateClass([
|
|
|
193
251
|
middleware: {}
|
|
194
252
|
})
|
|
195
253
|
], HttpRouteFlow);
|
|
254
|
+
function authoritiesInput(request) {
|
|
255
|
+
const { body } = request;
|
|
256
|
+
const fields = typeof body === "object" && body !== null && !Array.isArray(body) ? body : {};
|
|
257
|
+
return { ...fields, ...request.params };
|
|
258
|
+
}
|
|
196
259
|
export {
|
|
197
260
|
HttpRouteFlow as default,
|
|
198
261
|
httpRouteStateSchema
|
|
@@ -4,14 +4,17 @@ import { ProviderScope } from "@frontmcp/di";
|
|
|
4
4
|
import {
|
|
5
5
|
HTTP_ROUTE_PATH_MESSAGE,
|
|
6
6
|
httpRouteMetadataOf,
|
|
7
|
-
isHttpRoutePath
|
|
7
|
+
isHttpRoutePath,
|
|
8
|
+
isPublicMode
|
|
8
9
|
} from "../common/index.mjs";
|
|
9
10
|
import { questionInboxRoutes } from "../elicitation/questions/question-inbox.routes.mjs";
|
|
10
11
|
import { InvalidHttpRouteError, InvalidPeopleAuthOptionsError } from "../errors/index.mjs";
|
|
11
12
|
import { matchRoutePath } from "../flows/flow.http-path.mjs";
|
|
13
|
+
import { assertEntryAuthorities } from "../front-mcp/authorities-rules.check.mjs";
|
|
12
14
|
import { servedHealthPaths } from "../health/health.routes.mjs";
|
|
13
15
|
import { assertNotReserved, computeReservedPaths } from "../server/custom-routes.helper.mjs";
|
|
14
16
|
import { workflowWebhookRoutes } from "../workflow/trigger/workflow-webhook.routes.mjs";
|
|
17
|
+
import { openPeopleSessionStore, peopleStoreBackend } from "./people-session-store.mjs";
|
|
15
18
|
import { PEOPLE_SIGN_IN_ROUTES } from "./people-sign-in.routes.mjs";
|
|
16
19
|
function header(request, name) {
|
|
17
20
|
const value = request.headers[name];
|
|
@@ -56,17 +59,38 @@ function printSignInToken(token) {
|
|
|
56
59
|
if (typeof process !== "undefined" && typeof process.stderr?.write === "function") process.stderr.write(message);
|
|
57
60
|
else console.warn(message);
|
|
58
61
|
}
|
|
59
|
-
function
|
|
60
|
-
|
|
61
|
-
if (!
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
62
|
+
async function createPeopleAuth(scope, option, sqlite) {
|
|
63
|
+
const parsed = peopleAuthOptionsSchema().safeParse(option);
|
|
64
|
+
if (!parsed.success) throw new InvalidPeopleAuthOptionsError(parsed.error.issues.map((issue) => issue.message));
|
|
65
|
+
const { logger } = scope;
|
|
66
|
+
const kept = await openPeopleSessionStore(
|
|
67
|
+
parsed.data,
|
|
68
|
+
{ redis: scope.metadata.redis, sqlite, tokenStorage: scope.auth?.tokenStorageAdapter },
|
|
69
|
+
scope.metadata.info.name,
|
|
70
|
+
logger
|
|
71
|
+
);
|
|
72
|
+
const auth = kept ? new LoopbackTokenAuth(parsed.data, {
|
|
73
|
+
store: kept.store,
|
|
74
|
+
onError: (message, error) => logger.warn(message, error)
|
|
75
|
+
}) : new LoopbackTokenAuth(parsed.data);
|
|
76
|
+
if (kept) {
|
|
77
|
+
scope.onDispose(async () => {
|
|
78
|
+
await auth.flush();
|
|
79
|
+
await kept.close();
|
|
80
|
+
});
|
|
68
81
|
}
|
|
69
|
-
|
|
82
|
+
const token = auth.takeToken();
|
|
83
|
+
if (token) printSignInToken(token);
|
|
84
|
+
return auth;
|
|
85
|
+
}
|
|
86
|
+
async function peopleAuthFor(scope, server, option, sqlite) {
|
|
87
|
+
let pending = peopleAuthByServer.get(server);
|
|
88
|
+
if (!pending) {
|
|
89
|
+
pending = createPeopleAuth(scope, option, sqlite);
|
|
90
|
+
peopleAuthByServer.set(server, pending);
|
|
91
|
+
}
|
|
92
|
+
const auth = await pending;
|
|
93
|
+
scope.providers.injectProvider({
|
|
70
94
|
provide: LoopbackTokenAuth,
|
|
71
95
|
value: auth,
|
|
72
96
|
metadata: {
|
|
@@ -77,10 +101,16 @@ function peopleAuthFor(server, option, scopeProviders) {
|
|
|
77
101
|
});
|
|
78
102
|
return auth;
|
|
79
103
|
}
|
|
104
|
+
function peopleKeepSessionsInSqlite(scope, sqlite) {
|
|
105
|
+
const option = scope.metadata.http?.auth?.people;
|
|
106
|
+
if (option === void 0) return false;
|
|
107
|
+
const store = typeof option === "object" ? option.store : void 0;
|
|
108
|
+
return peopleStoreBackend({ store }, { redis: scope.metadata.redis, sqlite }) === "sqlite";
|
|
109
|
+
}
|
|
80
110
|
function withoutTrailingSlash(path) {
|
|
81
111
|
return path.length > 1 && path.endsWith("/") ? path.slice(0, -1) : path;
|
|
82
112
|
}
|
|
83
|
-
function attachHttpRoutes(scope, server) {
|
|
113
|
+
async function attachHttpRoutes(scope, server, stores = {}) {
|
|
84
114
|
const declared = scope.apps.getApps().flatMap((app) => {
|
|
85
115
|
const routes2 = app.metadata.routes ?? [];
|
|
86
116
|
return routes2.map((target) => ({ target, providers: app.providers }));
|
|
@@ -94,7 +124,7 @@ function attachHttpRoutes(scope, server) {
|
|
|
94
124
|
);
|
|
95
125
|
}
|
|
96
126
|
if (declared.length === 0 && webhooks.length === 0 && peopleOption === void 0) return;
|
|
97
|
-
const people = peopleOption === void 0 ? void 0 : peopleAuthFor(server, peopleOption,
|
|
127
|
+
const people = peopleOption === void 0 ? void 0 : await peopleAuthFor(scope, server, peopleOption, stores.sqlite);
|
|
98
128
|
const builtInRoutes = [...PEOPLE_SIGN_IN_ROUTES, ...questions ? questionInboxRoutes(questions.store) : []];
|
|
99
129
|
const builtIn = people ? builtInRoutes.map((target) => ({ target, providers: scope.providers })) : [];
|
|
100
130
|
const reserved = computeReservedPaths(scope.entryPath, scope.routeBase, servedHealthPaths(scope.metadata.health));
|
|
@@ -112,7 +142,7 @@ function attachHttpRoutes(scope, server) {
|
|
|
112
142
|
const path = typeof metadata.path === "function" ? metadata.path() : metadata.path;
|
|
113
143
|
if (!isHttpRoutePath(path)) throw new InvalidHttpRouteError(method, String(path), HTTP_ROUTE_PATH_MESSAGE);
|
|
114
144
|
const auth = metadata.auth ?? (people ? "people" : "public");
|
|
115
|
-
if (auth !== "public" && !people) {
|
|
145
|
+
if (auth !== "public" && auth !== "bearer" && !people) {
|
|
116
146
|
throw new InvalidHttpRouteError(
|
|
117
147
|
method,
|
|
118
148
|
path,
|
|
@@ -133,10 +163,12 @@ function attachHttpRoutes(scope, server) {
|
|
|
133
163
|
throw new InvalidHttpRouteError(method, path, "another route already serves this method and path");
|
|
134
164
|
}
|
|
135
165
|
served.add(key);
|
|
166
|
+
if (auth === "bearer") checkBearerRoute(scope, `${method} ${path}`, metadata.authorities);
|
|
136
167
|
return {
|
|
137
168
|
method,
|
|
138
169
|
path,
|
|
139
170
|
auth,
|
|
171
|
+
authorities: metadata.authorities,
|
|
140
172
|
sse: metadata.sse,
|
|
141
173
|
target,
|
|
142
174
|
providers,
|
|
@@ -152,6 +184,19 @@ function attachHttpRoutes(scope, server) {
|
|
|
152
184
|
scope.httpRouteTable = table;
|
|
153
185
|
scope.onDispose(() => table.closeStreams());
|
|
154
186
|
}
|
|
187
|
+
function checkBearerRoute(scope, route, authorities) {
|
|
188
|
+
if (authorities !== void 0) {
|
|
189
|
+
assertEntryAuthorities(scope.authoritiesEngine, scope.authoritiesContextBuilder, [
|
|
190
|
+
{ label: `HTTP route "${route}"`, authorities }
|
|
191
|
+
]);
|
|
192
|
+
}
|
|
193
|
+
const authOptions = scope.auth?.options;
|
|
194
|
+
if (!authOptions || isPublicMode(authOptions)) {
|
|
195
|
+
scope.logger.warn(
|
|
196
|
+
`HTTP route ${route} has auth: 'bearer', but the server's auth is in public mode, so it admits every caller as anonymous: set auth to the mode that verifies your tokens, or give the route auth: 'public'.`
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
155
200
|
function assertWebhookPathFree(webhook, routes, customRoutes, paths) {
|
|
156
201
|
const { method, path, workflow } = webhook;
|
|
157
202
|
const refuse = (reason) => new InvalidHttpRouteError(method, path, `the webhook of workflow "${workflow}" ${reason}`);
|
|
@@ -164,5 +209,6 @@ function assertWebhookPathFree(webhook, routes, customRoutes, paths) {
|
|
|
164
209
|
}
|
|
165
210
|
export {
|
|
166
211
|
HttpRouteTable,
|
|
167
|
-
attachHttpRoutes
|
|
212
|
+
attachHttpRoutes,
|
|
213
|
+
peopleKeepSessionsInSqlite
|
|
168
214
|
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// libs/sdk/src/http-route/people-session-store.ts
|
|
2
|
+
import {
|
|
3
|
+
createNamespacedStorage,
|
|
4
|
+
createStorage
|
|
5
|
+
} from "@frontmcp/utils";
|
|
6
|
+
import { isRedisProvider, redisOptionsSchema } from "../common/index.mjs";
|
|
7
|
+
function serverRedis(redis) {
|
|
8
|
+
const parsed = redis === void 0 ? void 0 : redisOptionsSchema.safeParse(redis);
|
|
9
|
+
if (!parsed?.success || !isRedisProvider(parsed.data)) return void 0;
|
|
10
|
+
const { host, port, password, db, tls } = parsed.data;
|
|
11
|
+
return { type: "redis", redis: { config: { host, port, password, db, tls } } };
|
|
12
|
+
}
|
|
13
|
+
function peopleStoreBackend(options, sources) {
|
|
14
|
+
if (options.store === false) return void 0;
|
|
15
|
+
if (serverRedis(sources.redis)) return "redis";
|
|
16
|
+
if (sources.sqlite) return "sqlite";
|
|
17
|
+
return sources.tokenStorage ? "auth.tokenStorage" : void 0;
|
|
18
|
+
}
|
|
19
|
+
async function openPeopleSessionStore(options, sources, serverName, logger) {
|
|
20
|
+
const backend = peopleStoreBackend(options, sources);
|
|
21
|
+
const prefix = `mcp:people:${serverName}:`;
|
|
22
|
+
if (backend === void 0) {
|
|
23
|
+
if (options.store !== false) {
|
|
24
|
+
logger.warn(
|
|
25
|
+
"http.auth.people keeps sign-in sessions in memory, so a restart signs everyone out: give the server a redis or sqlite store to keep them, or set http.auth.people to { mode: 'loopback-token', store: false } to keep them in memory without this warning"
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
return void 0;
|
|
29
|
+
}
|
|
30
|
+
if (backend === "auth.tokenStorage" && sources.tokenStorage) {
|
|
31
|
+
return { store: createNamespacedStorage(sources.tokenStorage, prefix), backend, close: async () => void 0 };
|
|
32
|
+
}
|
|
33
|
+
const config = backend === "redis" ? serverRedis(sources.redis) : { type: "sqlite", sqlite: sources.sqlite };
|
|
34
|
+
try {
|
|
35
|
+
const storage = await createStorage({ ...config, prefix, fallback: "error" });
|
|
36
|
+
return { store: storage, backend, close: () => storage.disconnect() };
|
|
37
|
+
} catch (error) {
|
|
38
|
+
logger.warn(
|
|
39
|
+
`http.auth.people: the ${backend} store cannot be opened, so sign-in sessions stay in memory and a restart signs everyone out: ${error instanceof Error ? error.message : String(error)}`
|
|
40
|
+
);
|
|
41
|
+
return void 0;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export {
|
|
45
|
+
openPeopleSessionStore,
|
|
46
|
+
peopleStoreBackend
|
|
47
|
+
};
|
|
@@ -24,7 +24,7 @@ var PEOPLE_CODE_PATH = "/_frontmcp/people/code";
|
|
|
24
24
|
var PEOPLE_LOGIN_PATH = "/_frontmcp/people/login";
|
|
25
25
|
var CODE = /^[A-Za-z0-9_-]{1,128}$/;
|
|
26
26
|
var SAME_SITE_PATH = /^\/(?![/\\])[^\s]*$/;
|
|
27
|
-
var PAGE_HEADERS = { ...NO_STORE_HEADERS, "Referrer-Policy": "
|
|
27
|
+
var PAGE_HEADERS = { ...NO_STORE_HEADERS, "Referrer-Policy": "same-origin" };
|
|
28
28
|
function first(value) {
|
|
29
29
|
const item = Array.isArray(value) ? value[0] : value;
|
|
30
30
|
return typeof item === "string" ? item : void 0;
|
|
@@ -79,11 +79,12 @@ PeopleLoginPageRoute = __decorateClass([
|
|
|
79
79
|
HttpRoute({ method: "GET", path: PEOPLE_LOGIN_PATH, auth: "public" })
|
|
80
80
|
], PeopleLoginPageRoute);
|
|
81
81
|
var PeopleLoginRoute = class extends HttpRouteContext {
|
|
82
|
-
handle() {
|
|
82
|
+
async handle() {
|
|
83
83
|
const fields = parseFormBody(this.request.body);
|
|
84
84
|
const auth = this.get(LoopbackTokenAuth);
|
|
85
85
|
const session = auth.exchangeCode(first(fields["code"]), clientOf(this));
|
|
86
86
|
if (!session.ok) throw refusal(session);
|
|
87
|
+
await auth.flush();
|
|
87
88
|
const next = escapeHtml(nextPath(fields["next"]));
|
|
88
89
|
const cookie = peopleSessionCookie(session.cookieValue, {
|
|
89
90
|
path: auth.options.cookiePath,
|
|
@@ -11,7 +11,7 @@ var SseRouteStream = class {
|
|
|
11
11
|
constructor(options) {
|
|
12
12
|
this.options = options;
|
|
13
13
|
this.unsubscribe = options.subscribe(() => this.schedule());
|
|
14
|
-
this.heartbeat = setInterval(() => this.beat(), options.heartbeatMs);
|
|
14
|
+
this.heartbeat = setInterval(() => void this.beat(), options.heartbeatMs);
|
|
15
15
|
this.heartbeat.unref?.();
|
|
16
16
|
void this.produce();
|
|
17
17
|
}
|
|
@@ -62,18 +62,29 @@ var SseRouteStream = class {
|
|
|
62
62
|
void this.produce();
|
|
63
63
|
}, this.options.coalesceMs);
|
|
64
64
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
65
|
+
/**
|
|
66
|
+
* Whether the reader may stay. A check that answers `false` ends the stream, and so does one that fails, which is
|
|
67
|
+
* reported first: the stream cannot tell whether the reader may stay.
|
|
68
|
+
*/
|
|
69
|
+
async stillAuthorized() {
|
|
70
|
+
let authorized;
|
|
71
|
+
try {
|
|
72
|
+
authorized = await this.options.authorized?.();
|
|
73
|
+
} catch (error) {
|
|
74
|
+
this.options.onError(error);
|
|
75
|
+
authorized = false;
|
|
76
|
+
}
|
|
77
|
+
if (authorized === false) this.close();
|
|
78
|
+
return authorized !== false;
|
|
79
|
+
}
|
|
80
|
+
async beat() {
|
|
81
|
+
if (await this.stillAuthorized()) this.offer(HEARTBEAT, false);
|
|
68
82
|
}
|
|
69
83
|
async produce() {
|
|
70
|
-
if (this.options.authorized?.() === false) {
|
|
71
|
-
this.close();
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
84
|
this.producing = true;
|
|
75
85
|
this.dirty = false;
|
|
76
86
|
try {
|
|
87
|
+
if (!await this.stillAuthorized()) return;
|
|
77
88
|
const view = await this.options.produce();
|
|
78
89
|
if (view !== void 0) this.offer(encoder.encode(formatSseEvent(view)), true);
|
|
79
90
|
} catch (error) {
|