@layervai/qurl-mcp 0.8.3 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +118 -6
- package/dist/auth/hosted-oauth.d.ts +30 -0
- package/dist/auth/hosted-oauth.d.ts.map +1 -0
- package/dist/auth/hosted-oauth.js +232 -0
- package/dist/auth/hosted-oauth.js.map +1 -0
- package/dist/client.d.ts +2 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js.map +1 -1
- package/dist/config.d.ts +4 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +38 -0
- package/dist/config.js.map +1 -1
- package/dist/http-config.d.ts +5 -0
- package/dist/http-config.d.ts.map +1 -1
- package/dist/http-config.js +6 -36
- package/dist/http-config.js.map +1 -1
- package/dist/http-feedback.d.ts +3 -0
- package/dist/http-feedback.d.ts.map +1 -0
- package/dist/http-feedback.js +90 -0
- package/dist/http-feedback.js.map +1 -0
- package/dist/http-public-routes.d.ts +19 -0
- package/dist/http-public-routes.d.ts.map +1 -0
- package/dist/http-public-routes.js +183 -0
- package/dist/http-public-routes.js.map +1 -0
- package/dist/http-routes.d.ts +4 -1
- package/dist/http-routes.d.ts.map +1 -1
- package/dist/http-routes.js +6 -0
- package/dist/http-routes.js.map +1 -1
- package/dist/http-sessions.d.ts +39 -0
- package/dist/http-sessions.d.ts.map +1 -0
- package/dist/http-sessions.js +140 -0
- package/dist/http-sessions.js.map +1 -0
- package/dist/http.d.ts +1 -1
- package/dist/http.d.ts.map +1 -1
- package/dist/http.js +153 -302
- package/dist/http.js.map +1 -1
- package/dist/logging.d.ts.map +1 -1
- package/dist/logging.js +2 -0
- package/dist/logging.js.map +1 -1
- package/dist/prompts/rotate-access.js +1 -1
- package/dist/prompts/rotate-access.js.map +1 -1
- package/dist/prompts/secure-a-service.d.ts.map +1 -1
- package/dist/prompts/secure-a-service.js +1 -0
- package/dist/prompts/secure-a-service.js.map +1 -1
- package/dist/server.d.ts +24 -37
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +116 -30
- package/dist/server.js.map +1 -1
- package/dist/services/feedback.d.ts +30 -0
- package/dist/services/feedback.d.ts.map +1 -0
- package/dist/services/feedback.js +133 -0
- package/dist/services/feedback.js.map +1 -0
- package/dist/tools/_shared.d.ts +6 -2
- package/dist/tools/_shared.d.ts.map +1 -1
- package/dist/tools/_shared.js +13 -0
- package/dist/tools/_shared.js.map +1 -1
- package/dist/tools/batch-create.d.ts +4 -8
- package/dist/tools/batch-create.d.ts.map +1 -1
- package/dist/tools/consumer-access.d.ts +174 -16
- package/dist/tools/consumer-access.d.ts.map +1 -1
- package/dist/tools/consumer-access.js +67 -4
- package/dist/tools/consumer-access.js.map +1 -1
- package/dist/tools/create-qurl.d.ts +4 -8
- package/dist/tools/create-qurl.d.ts.map +1 -1
- package/dist/tools/create-qurl.js +6 -2
- package/dist/tools/create-qurl.js.map +1 -1
- package/dist/tools/delete-qurl.d.ts +1 -8
- package/dist/tools/delete-qurl.d.ts.map +1 -1
- package/dist/tools/duration.d.ts.map +1 -1
- package/dist/tools/duration.js +20 -5
- package/dist/tools/duration.js.map +1 -1
- package/dist/tools/extend-qurl.d.ts +5 -12
- package/dist/tools/extend-qurl.d.ts.map +1 -1
- package/dist/tools/get-qurl.d.ts +5 -12
- package/dist/tools/get-qurl.d.ts.map +1 -1
- package/dist/tools/list-qurl-sessions.d.ts +1 -8
- package/dist/tools/list-qurl-sessions.d.ts.map +1 -1
- package/dist/tools/list-qurls.d.ts +5 -12
- package/dist/tools/list-qurls.d.ts.map +1 -1
- package/dist/tools/local-cli.d.ts.map +1 -1
- package/dist/tools/local-cli.js +20 -13
- package/dist/tools/local-cli.js.map +1 -1
- package/dist/tools/mint-link.d.ts +1 -8
- package/dist/tools/mint-link.d.ts.map +1 -1
- package/dist/tools/mint-link.js +1 -1
- package/dist/tools/mint-link.js.map +1 -1
- package/dist/tools/mint-uploaded-file-link.d.ts +53 -0
- package/dist/tools/mint-uploaded-file-link.d.ts.map +1 -0
- package/dist/tools/mint-uploaded-file-link.js +43 -0
- package/dist/tools/mint-uploaded-file-link.js.map +1 -0
- package/dist/tools/output-schemas.d.ts +37 -24
- package/dist/tools/output-schemas.d.ts.map +1 -1
- package/dist/tools/output-schemas.js +40 -14
- package/dist/tools/output-schemas.js.map +1 -1
- package/dist/tools/resolve-qurl.d.ts +1 -8
- package/dist/tools/resolve-qurl.d.ts.map +1 -1
- package/dist/tools/revoke-qurl-token.d.ts +1 -8
- package/dist/tools/revoke-qurl-token.d.ts.map +1 -1
- package/dist/tools/share-by-crid.d.ts +1 -8
- package/dist/tools/share-by-crid.d.ts.map +1 -1
- package/dist/tools/share-by-crid.js +2 -2
- package/dist/tools/share-by-crid.js.map +1 -1
- package/dist/tools/submit-feedback.d.ts +47 -0
- package/dist/tools/submit-feedback.d.ts.map +1 -0
- package/dist/tools/submit-feedback.js +81 -0
- package/dist/tools/submit-feedback.js.map +1 -0
- package/dist/tools/terminate-qurl-sessions.d.ts +1 -8
- package/dist/tools/terminate-qurl-sessions.d.ts.map +1 -1
- package/dist/tools/update-qurl-token.d.ts +3 -10
- package/dist/tools/update-qurl-token.d.ts.map +1 -1
- package/dist/tools/update-qurl.d.ts +5 -12
- package/dist/tools/update-qurl.d.ts.map +1 -1
- package/dist/tools/upload-file-data-qurl.d.ts +1 -8
- package/dist/tools/upload-file-data-qurl.d.ts.map +1 -1
- package/dist/tools/upload-file-data-qurl.js +1 -1
- package/dist/tools/upload-file-data-qurl.js.map +1 -1
- package/dist/tools/upload-file-qurl.d.ts +1 -8
- package/dist/tools/upload-file-qurl.d.ts.map +1 -1
- package/dist/tools/upload-file-qurl.js +1 -1
- package/dist/tools/upload-file-qurl.js.map +1 -1
- package/dist/tools/upload-file-shared.d.ts +22 -8
- package/dist/tools/upload-file-shared.d.ts.map +1 -1
- package/dist/tools/upload-file-shared.js +47 -34
- package/dist/tools/upload-file-shared.js.map +1 -1
- package/dist/tools/upload-mint-options.d.ts +2 -1
- package/dist/tools/upload-mint-options.d.ts.map +1 -1
- package/dist/tools/upload-mint-options.js +4 -2
- package/dist/tools/upload-mint-options.js.map +1 -1
- package/dist/tools/upload-text-qurl.d.ts +4 -11
- package/dist/tools/upload-text-qurl.d.ts.map +1 -1
- package/package.json +7 -7
package/dist/http.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
import { registerFeedbackRoute } from "./http-feedback.js";
|
|
3
|
+
import { createHttpSessionRegistry, } from "./http-sessions.js";
|
|
4
|
+
import { createPublicHttpRoutes, createPublicRateLimiter } from "./http-public-routes.js";
|
|
2
5
|
import { isHttpEmailAuthorized } from "./services/email.js";
|
|
3
6
|
import { createHash, randomUUID, timingSafeEqual } from "node:crypto";
|
|
4
|
-
import {
|
|
5
|
-
import { lstat } from "node:fs/promises";
|
|
7
|
+
import { existsSync, realpathSync } from "node:fs";
|
|
6
8
|
import { createRequire } from "node:module";
|
|
7
9
|
import { fileURLToPath } from "node:url";
|
|
8
10
|
import { clearSensitiveLogValues, formatErrorForLog, installTimestampedConsole, logInfo, registerSensitiveLogValues, sanitizeLogValue, } from "./logging.js";
|
|
@@ -10,19 +12,18 @@ import express from "express";
|
|
|
10
12
|
import { hostHeaderValidation, localhostHostValidation, } from "@modelcontextprotocol/sdk/server/middleware/hostHeaderValidation.js";
|
|
11
13
|
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
12
14
|
import { requireBearerAuth } from "@modelcontextprotocol/sdk/server/auth/middleware/bearerAuth.js";
|
|
15
|
+
import { ServerError as OAuthServerError } from "@modelcontextprotocol/sdk/server/auth/errors.js";
|
|
13
16
|
import { isInitializeRequest } from "@modelcontextprotocol/sdk/types.js";
|
|
14
17
|
import { rateLimit } from "express-rate-limit";
|
|
18
|
+
import { createHostedOAuth, OAUTH_RESOURCE, OAUTH_API_AUDIENCE, OAUTH_CLIENT_ID, OAUTH_ISSUER, SANDBOX_OAUTH_RESOURCE, SANDBOX_OAUTH_API_AUDIENCE, OAUTH_SCOPES, } from "./auth/hosted-oauth.js";
|
|
15
19
|
import { runWithRequestAuthContext } from "./auth/request-context.js";
|
|
16
20
|
import { canonicalizeBearerToken, createPassthroughBearerVerifier, createQurlClientFromBearerToken, } from "./auth/static-bearer.js";
|
|
17
21
|
import { DEFAULT_MAX_UPLOAD_FILE_DATA_BYTES, getDefaultConfigPath, inspectSmtpConfig, loadRuntimeConfig, isLoopbackHostname, } from "./config.js";
|
|
18
22
|
import { assertDeployedStatelessRequirements, assertStatelessParserBudget, getDefaultHttpConfigPath, getJsonBodyLimitBytes, loadHttpServerConfig, normalizeMaxConcurrentRequests, normalizeRateLimitDynamoDbTable, } from "./http-config.js";
|
|
19
|
-
import { HEALTH_HTTP_PATH, MCP_HTTP_PATH } from "./http-routes.js";
|
|
20
|
-
import { getLegalDocuments, renderLegalDocumentHtml } from "./services/legal-pages.js";
|
|
21
|
-
import { getPublicVideoFileRoute, renderPublicVideoPageHtml } from "./services/video-page.js";
|
|
23
|
+
import { GOOGLE_CONNECTED_APPS_VERIFICATION_PATH, HEALTH_HTTP_PATH, MCP_HTTP_PATH, OAUTH_RESOURCE_METADATA_PATH, } from "./http-routes.js";
|
|
22
24
|
import { createServer } from "./server.js";
|
|
23
25
|
import { DynamoDbCredentialRateLimitStore, MemoryCredentialRateLimitStore, } from "./credential-rate-limit-store.js";
|
|
24
26
|
import { McpEmfMetrics, normalizeMcpMetricsIdentity } from "./emf-metrics.js";
|
|
25
|
-
const DISCONNECTED_SESSION_GRACE_MS = 30_000;
|
|
26
27
|
const STATELESS_HEADERS_TIMEOUT_MS = 15_000;
|
|
27
28
|
const STATELESS_REQUEST_TIMEOUT_MS = 120_000;
|
|
28
29
|
const STATELESS_SOCKET_IDLE_TIMEOUT_MS = 120_000;
|
|
@@ -52,6 +53,36 @@ export function createHttpRuntime(config, options) {
|
|
|
52
53
|
const defaultQurlApiUrl = config.defaultQurlApiUrl;
|
|
53
54
|
const defaultQurlConnectorUrl = config.defaultQurlConnectorUrl;
|
|
54
55
|
const stateless = config.stateless ?? false;
|
|
56
|
+
const resource = `${baseUrl.replace(/\/$/, "")}${MCP_HTTP_PATH}`;
|
|
57
|
+
const sandboxOAuth = resource === SANDBOX_OAUTH_RESOURCE;
|
|
58
|
+
const exchangeClientId = sandboxOAuth ? config.oauthSandboxExchangeClientId : OAUTH_CLIENT_ID;
|
|
59
|
+
const signInClientId = config.oauthSignInClientId;
|
|
60
|
+
if (config.hostedOAuth) {
|
|
61
|
+
if (!stateless ||
|
|
62
|
+
(sandboxOAuth
|
|
63
|
+
? defaultQurlApiUrl !== SANDBOX_OAUTH_API_AUDIENCE
|
|
64
|
+
: resource !== OAUTH_RESOURCE || defaultQurlApiUrl !== OAUTH_API_AUDIENCE)) {
|
|
65
|
+
throw new Error("Hosted OAuth requires matching stateless Sandbox or production MCP/API URLs.");
|
|
66
|
+
}
|
|
67
|
+
if (!exchangeClientId ||
|
|
68
|
+
!/^[A-Za-z0-9_-]{1,128}$/.test(exchangeClientId) ||
|
|
69
|
+
(sandboxOAuth && exchangeClientId === OAUTH_CLIENT_ID) ||
|
|
70
|
+
!signInClientId ||
|
|
71
|
+
!/^[A-Za-z0-9_-]{1,128}$/.test(signInClientId) ||
|
|
72
|
+
signInClientId === exchangeClientId ||
|
|
73
|
+
signInClientId === OAUTH_CLIENT_ID) {
|
|
74
|
+
throw new Error("Hosted OAuth requires separate environment-specific exchange and sign-in clients.");
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const oauth = config.hostedOAuth
|
|
78
|
+
? createHostedOAuth(config.oauthClientSecret ?? "", undefined, {
|
|
79
|
+
resource,
|
|
80
|
+
apiAudience: defaultQurlApiUrl,
|
|
81
|
+
exchangeClientId,
|
|
82
|
+
signInClientId,
|
|
83
|
+
})
|
|
84
|
+
: undefined;
|
|
85
|
+
const resourceMetadataUrl = `${new URL(baseUrl).origin}${OAUTH_RESOURCE_METADATA_PATH}/mcp`;
|
|
55
86
|
const maxConcurrentRequests = normalizeMaxConcurrentRequests(config.maxConcurrentRequests);
|
|
56
87
|
assertStatelessParserBudget(stateless, maxConcurrentRequests, config.maxUploadFileDataBytes);
|
|
57
88
|
let inFlightRequests = 0;
|
|
@@ -80,7 +111,7 @@ export function createHttpRuntime(config, options) {
|
|
|
80
111
|
config.credentialRateLimitStore !== "dynamodb";
|
|
81
112
|
// Bound completed-response teardown independently from active parser work.
|
|
82
113
|
// A slow SDK close path can consume at most one additional concurrency pool.
|
|
83
|
-
const closingStatelessRequests =
|
|
114
|
+
const { sessions, closingSessions, activeStatelessRequests, closingStatelessRequests, markSessionDisconnected, closeSession, sweepExpiredSessions, closeAllSessions, closeStatelessRequest, trackSessionActivity, getActiveSessionCount, } = createHttpSessionRegistry(config, withRequestAuth);
|
|
84
115
|
const app = express();
|
|
85
116
|
app.disable("x-powered-by");
|
|
86
117
|
if (config.trustProxyHops > 0) {
|
|
@@ -99,26 +130,57 @@ export function createHttpRuntime(config, options) {
|
|
|
99
130
|
legacyHeaders: false,
|
|
100
131
|
handler: (_req, res) => rejectJsonRpc(res, 429, "Too many requests."),
|
|
101
132
|
});
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
res.
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
const
|
|
117
|
-
const healthRateLimiter = createPublicRateLimiter();
|
|
118
|
-
const bearerAuthMiddleware = requireBearerAuth({
|
|
133
|
+
const publicRoutes = createPublicHttpRoutes(config, options);
|
|
134
|
+
const { streamPublicVideo, publicRouteRateLimiter } = publicRoutes;
|
|
135
|
+
const healthRateLimiter = createPublicRateLimiter(config);
|
|
136
|
+
function rejectOAuth(res, error) {
|
|
137
|
+
if (res.destroyed)
|
|
138
|
+
return;
|
|
139
|
+
if (error instanceof OAuthServerError) {
|
|
140
|
+
res.set("Retry-After", "1");
|
|
141
|
+
rejectJsonRpc(res, 503, "Delegated authentication is temporarily unavailable.");
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
res.set("WWW-Authenticate", `Bearer resource_metadata="${resourceMetadataUrl}"${error === undefined ? "" : ', error="invalid_token", error_description="Reconnect your account."'}`);
|
|
145
|
+
rejectJsonRpc(res, 401, "Invalid or unavailable delegated access. Reconnect your account.");
|
|
146
|
+
}
|
|
147
|
+
const apiKeyAuth = requireBearerAuth({
|
|
119
148
|
verifier: createPassthroughBearerVerifier(),
|
|
120
149
|
requiredScopes: ["mcp:tools"],
|
|
121
150
|
});
|
|
151
|
+
const verifyBearer = oauth
|
|
152
|
+
? async (req, res, next) => {
|
|
153
|
+
try {
|
|
154
|
+
const match = /^Bearer ([^\s]+)$/i.exec(req.headers.authorization ?? "");
|
|
155
|
+
if (!match) {
|
|
156
|
+
rejectOAuth(res, req.headers.authorization ? new Error("Malformed bearer header") : undefined);
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
// Match the qURL API's credential split. JWTs never fall back to API-key passthrough.
|
|
160
|
+
if (/^lv_(live|test)_[A-Za-z0-9_-]+$/.test(match[1])) {
|
|
161
|
+
await apiKeyAuth(req, res, next);
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
req.auth = await oauth.verifyAccessToken(match[1]);
|
|
165
|
+
res.locals.oauthVerified = true;
|
|
166
|
+
if (!res.destroyed)
|
|
167
|
+
next();
|
|
168
|
+
}
|
|
169
|
+
catch (error) {
|
|
170
|
+
rejectOAuth(res, error);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
: apiKeyAuth;
|
|
174
|
+
const bearerAuthMiddleware = oauth
|
|
175
|
+
? (req, res, next) => {
|
|
176
|
+
const work = Promise.resolve(verifyBearer(req, res, () => {
|
|
177
|
+
if (!res.destroyed)
|
|
178
|
+
next();
|
|
179
|
+
}));
|
|
180
|
+
res.locals.oauthWork = work;
|
|
181
|
+
void work.catch(next);
|
|
182
|
+
}
|
|
183
|
+
: verifyBearer;
|
|
122
184
|
const concurrencyLimiter = (_req, res, next) => {
|
|
123
185
|
if (inFlightRequests >= maxConcurrentRequests ||
|
|
124
186
|
closingStatelessRequests.size >= maxConcurrentRequests) {
|
|
@@ -133,12 +195,20 @@ export function createHttpRuntime(config, options) {
|
|
|
133
195
|
inFlightRequests += 1;
|
|
134
196
|
metrics.observeConcurrencyUtilization();
|
|
135
197
|
let released = false;
|
|
136
|
-
const
|
|
198
|
+
const releasePermit = () => {
|
|
137
199
|
if (released)
|
|
138
200
|
return;
|
|
139
201
|
released = true;
|
|
140
202
|
inFlightRequests -= 1;
|
|
141
203
|
};
|
|
204
|
+
const release = () => {
|
|
205
|
+
// A disconnected client must not free capacity while OAuth work still runs.
|
|
206
|
+
const work = res.locals.oauthWork;
|
|
207
|
+
if (work)
|
|
208
|
+
void work.then(releasePermit, releasePermit);
|
|
209
|
+
else
|
|
210
|
+
releasePermit();
|
|
211
|
+
};
|
|
142
212
|
// Release when the response lifetime ends: this permit bounds active body
|
|
143
213
|
// parsing/request work, not asynchronously closing MCP objects. Teardown is
|
|
144
214
|
// tracked separately in closingStatelessRequests and drained on shutdown.
|
|
@@ -182,8 +252,9 @@ export function createHttpRuntime(config, options) {
|
|
|
182
252
|
// DELETE are unsupported probes and bypass the fleet-wide store below.
|
|
183
253
|
const authenticatedMcpMiddleware = [bearerAuthMiddleware, credentialRateLimiter];
|
|
184
254
|
const authenticatedMcpPostMiddleware = [
|
|
255
|
+
...(stateless && oauth ? [concurrencyLimiter] : []),
|
|
185
256
|
bearerAuthMiddleware,
|
|
186
|
-
...(stateless ? [concurrencyLimiter] : []),
|
|
257
|
+
...(stateless && !oauth ? [concurrencyLimiter] : []),
|
|
187
258
|
credentialRateLimiter,
|
|
188
259
|
];
|
|
189
260
|
const rejectUnsupportedStatelessMethod = (method) => (_req, res) => {
|
|
@@ -204,10 +275,31 @@ export function createHttpRuntime(config, options) {
|
|
|
204
275
|
else {
|
|
205
276
|
app.use(localhostHostValidation());
|
|
206
277
|
}
|
|
278
|
+
const googleVerificationToken = config.googleConnectedAppsVerificationToken;
|
|
279
|
+
if (googleVerificationToken !== undefined) {
|
|
280
|
+
if (!/^[!-~]{1,4096}$/.test(googleVerificationToken)) {
|
|
281
|
+
throw new Error("MCP_GOOGLE_CONNECTED_APPS_VERIFICATION_TOKEN must be a single token.");
|
|
282
|
+
}
|
|
283
|
+
app.get(GOOGLE_CONNECTED_APPS_VERIFICATION_PATH, publicRouteRateLimiter, (_req, res) => {
|
|
284
|
+
res.set("Cache-Control", "no-store");
|
|
285
|
+
res.json({ verification_tokens: [googleVerificationToken] });
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
if (oauth) {
|
|
289
|
+
app.get([OAUTH_RESOURCE_METADATA_PATH, `${OAUTH_RESOURCE_METADATA_PATH}/mcp`], publicRouteRateLimiter, (_req, res) => {
|
|
290
|
+
res.json({
|
|
291
|
+
resource: oauth.resource,
|
|
292
|
+
authorization_servers: [OAUTH_ISSUER],
|
|
293
|
+
scopes_supported: OAUTH_SCOPES,
|
|
294
|
+
bearer_methods_supported: ["header"],
|
|
295
|
+
});
|
|
296
|
+
});
|
|
297
|
+
}
|
|
207
298
|
// Host validation may admit aliases for routing, but browser-originated MCP
|
|
208
299
|
// requests intentionally use the single canonical public origin in baseUrl.
|
|
209
300
|
// Do not widen this check merely because allowedHosts contains extra names.
|
|
210
301
|
const allowedOrigin = new URL(baseUrl).origin;
|
|
302
|
+
const feedbackToolRateLimiter = registerFeedbackRoute(app, allowedOrigin);
|
|
211
303
|
// This is the complete stateful browser-route set today. Add any future
|
|
212
304
|
// state-changing browser route here rather than assuming this check is global.
|
|
213
305
|
app.use((req, res, next) => {
|
|
@@ -239,9 +331,6 @@ export function createHttpRuntime(config, options) {
|
|
|
239
331
|
limit: getJsonBodyLimitBytes(Math.min(config.maxUploadFileDataBytes, DEFAULT_MAX_UPLOAD_FILE_DATA_BYTES)),
|
|
240
332
|
strict: true,
|
|
241
333
|
});
|
|
242
|
-
const sessions = new Map();
|
|
243
|
-
const closingSessions = new Map();
|
|
244
|
-
const activeStatelessRequests = new Set();
|
|
245
334
|
let pendingInitializations = 0;
|
|
246
335
|
const pendingInitializationsByCredential = new Map();
|
|
247
336
|
const parseMcpJsonBody = (req, res, next) => {
|
|
@@ -262,124 +351,6 @@ export function createHttpRuntime(config, options) {
|
|
|
262
351
|
const parser = mayUseConfiguredLimit ? parseConfiguredMcpJsonBody : parseUnvalidatedMcpJsonBody;
|
|
263
352
|
parser(req, res, next);
|
|
264
353
|
};
|
|
265
|
-
function markSessionDisconnected(sessionId) {
|
|
266
|
-
if (!sessionId)
|
|
267
|
-
return;
|
|
268
|
-
const session = sessions.get(sessionId);
|
|
269
|
-
if (!session)
|
|
270
|
-
return;
|
|
271
|
-
const disconnectedAt = Date.now();
|
|
272
|
-
session.lastActivityAt = disconnectedAt;
|
|
273
|
-
session.disconnectedAt = disconnectedAt;
|
|
274
|
-
}
|
|
275
|
-
async function closeSession(sessionId) {
|
|
276
|
-
const existingClose = closingSessions.get(sessionId);
|
|
277
|
-
if (existingClose) {
|
|
278
|
-
await existingClose;
|
|
279
|
-
return;
|
|
280
|
-
}
|
|
281
|
-
const session = sessions.get(sessionId);
|
|
282
|
-
if (!session)
|
|
283
|
-
return;
|
|
284
|
-
// Admission replacement relies on removal before the first await.
|
|
285
|
-
sessions.delete(sessionId);
|
|
286
|
-
const closePromise = withRequestAuth(session.sessionId, session.bearerTokenForRedaction, async () => {
|
|
287
|
-
try {
|
|
288
|
-
await session.server.close();
|
|
289
|
-
}
|
|
290
|
-
catch (error) {
|
|
291
|
-
// Format while the credential-scoped redaction context is still active.
|
|
292
|
-
console.error(`[mcp-http] session close failed (${formatErrorForLog(error)})`);
|
|
293
|
-
}
|
|
294
|
-
});
|
|
295
|
-
closingSessions.set(sessionId, closePromise);
|
|
296
|
-
try {
|
|
297
|
-
await closePromise;
|
|
298
|
-
}
|
|
299
|
-
finally {
|
|
300
|
-
closingSessions.delete(sessionId);
|
|
301
|
-
clearSensitiveLogValues(`http-session:${sessionId}`);
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
async function sweepExpiredSessions(now = Date.now()) {
|
|
305
|
-
const expiredIds = [...sessions.values()]
|
|
306
|
-
.filter((session) => {
|
|
307
|
-
if (now - session.createdAt >= config.sessionAbsoluteTtlMs) {
|
|
308
|
-
// The absolute lifetime applies even to active tool/SSE requests so
|
|
309
|
-
// keepalives cannot pin a validated session slot indefinitely.
|
|
310
|
-
return true;
|
|
311
|
-
}
|
|
312
|
-
if (session.activeRequests === 0 &&
|
|
313
|
-
session.disconnectedAt !== undefined &&
|
|
314
|
-
now - session.disconnectedAt >=
|
|
315
|
-
// A deliberately shorter configured idle TTL remains authoritative.
|
|
316
|
-
Math.min(DISCONNECTED_SESSION_GRACE_MS, config.sessionIdleTtlMs)) {
|
|
317
|
-
return true;
|
|
318
|
-
}
|
|
319
|
-
if (!session.credentialValidated) {
|
|
320
|
-
// The pending-session window is an absolute validation deadline.
|
|
321
|
-
// Close even an active SSE stream so it cannot hold an unvalidated
|
|
322
|
-
// slot indefinitely.
|
|
323
|
-
return now - session.createdAt >= config.unvalidatedSessionTtlMs;
|
|
324
|
-
}
|
|
325
|
-
return (session.activeRequests === 0 && now - session.lastActivityAt >= config.sessionIdleTtlMs);
|
|
326
|
-
})
|
|
327
|
-
.map((session) => session.sessionId);
|
|
328
|
-
await Promise.all(expiredIds.map((sessionId) => closeSession(sessionId)));
|
|
329
|
-
return expiredIds.length;
|
|
330
|
-
}
|
|
331
|
-
async function closeAllSessions() {
|
|
332
|
-
await Promise.all([...sessions.keys()].map((sessionId) => closeSession(sessionId)));
|
|
333
|
-
// A concurrent DELETE/sweep may have removed its registry entry while its
|
|
334
|
-
// asynchronous server.close() is still settling. Shutdown callers wait for
|
|
335
|
-
// that ownership cleanup too rather than observing an empty registry early.
|
|
336
|
-
await Promise.all([...closingSessions.values()]);
|
|
337
|
-
await Promise.all([...activeStatelessRequests].map((request) => closeStatelessRequest(request)));
|
|
338
|
-
await Promise.all([...closingStatelessRequests]);
|
|
339
|
-
}
|
|
340
|
-
async function closeStatelessRequest(request) {
|
|
341
|
-
if (request.closePromise) {
|
|
342
|
-
await request.closePromise;
|
|
343
|
-
return;
|
|
344
|
-
}
|
|
345
|
-
activeStatelessRequests.delete(request);
|
|
346
|
-
const bearerToken = request.bearerToken;
|
|
347
|
-
const closePromise = withRequestAuth(undefined, bearerToken, async () => {
|
|
348
|
-
try {
|
|
349
|
-
await request.transport.close();
|
|
350
|
-
}
|
|
351
|
-
catch (error) {
|
|
352
|
-
console.error(`[mcp-http] stateless transport close failed (${formatErrorForLog(error)})`);
|
|
353
|
-
}
|
|
354
|
-
try {
|
|
355
|
-
await request.server.close();
|
|
356
|
-
}
|
|
357
|
-
catch (error) {
|
|
358
|
-
console.error(`[mcp-http] stateless server close failed (${formatErrorForLog(error)})`);
|
|
359
|
-
}
|
|
360
|
-
}).finally(() => {
|
|
361
|
-
request.bearerToken = "";
|
|
362
|
-
closingStatelessRequests.delete(closePromise);
|
|
363
|
-
});
|
|
364
|
-
request.closePromise = closePromise;
|
|
365
|
-
closingStatelessRequests.add(closePromise);
|
|
366
|
-
await closePromise;
|
|
367
|
-
}
|
|
368
|
-
async function trackSessionActivity(session, fn) {
|
|
369
|
-
session.lastActivityAt = Date.now();
|
|
370
|
-
session.disconnectedAt = undefined;
|
|
371
|
-
session.activeRequests += 1;
|
|
372
|
-
try {
|
|
373
|
-
return await fn();
|
|
374
|
-
}
|
|
375
|
-
finally {
|
|
376
|
-
session.activeRequests -= 1;
|
|
377
|
-
session.lastActivityAt = Date.now();
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
function getActiveSessionCount() {
|
|
381
|
-
return sessions.size;
|
|
382
|
-
}
|
|
383
354
|
function getSessionId(req) {
|
|
384
355
|
const header = req.headers["mcp-session-id"];
|
|
385
356
|
return typeof header === "string" ? header : undefined;
|
|
@@ -449,98 +420,6 @@ export function createHttpRuntime(config, options) {
|
|
|
449
420
|
id: null,
|
|
450
421
|
}));
|
|
451
422
|
}
|
|
452
|
-
async function streamPublicVideo(req, res, filePath) {
|
|
453
|
-
setPublicPageSecurityHeaders(res);
|
|
454
|
-
let stats;
|
|
455
|
-
try {
|
|
456
|
-
// Refuse a final-component symlink so a public video path cannot be
|
|
457
|
-
// retargeted to a different local file after operator configuration.
|
|
458
|
-
// Intermediate directory symlinks retain normal resolution because this
|
|
459
|
-
// absolute path is trusted operator config, never request-derived input.
|
|
460
|
-
stats = await lstat(filePath);
|
|
461
|
-
}
|
|
462
|
-
catch (error) {
|
|
463
|
-
console.error(`[public-video] file inspection failed (${formatErrorForLog(error)})`);
|
|
464
|
-
res.status(404).send("Configured video file was not found.");
|
|
465
|
-
return;
|
|
466
|
-
}
|
|
467
|
-
if (stats.isSymbolicLink() || !stats.isFile() || stats.size === 0) {
|
|
468
|
-
res.status(404).send("Configured video file was not found.");
|
|
469
|
-
return;
|
|
470
|
-
}
|
|
471
|
-
// Content-Length is a request-time snapshot. Public media is trusted,
|
|
472
|
-
// operator-owned configuration and should be replaced atomically; if it
|
|
473
|
-
// is truncated in place, the stream error path below destroys the response
|
|
474
|
-
// rather than serving a silently successful partial body.
|
|
475
|
-
const fileSize = stats.size;
|
|
476
|
-
const range = req.headers.range;
|
|
477
|
-
const pipeFile = (start, end) => {
|
|
478
|
-
const stream = options.fileStreamFactory
|
|
479
|
-
? options.fileStreamFactory(filePath, { start, end })
|
|
480
|
-
: createReadStream(filePath, { start, end });
|
|
481
|
-
const destroyStream = () => stream.destroy();
|
|
482
|
-
res.once("close", destroyStream);
|
|
483
|
-
stream.once("close", () => res.off("close", destroyStream));
|
|
484
|
-
stream.once("error", (error) => {
|
|
485
|
-
console.error(`[public-video] stream failed (${formatErrorForLog(error)})`);
|
|
486
|
-
res.destroy(error);
|
|
487
|
-
});
|
|
488
|
-
stream.pipe(res);
|
|
489
|
-
};
|
|
490
|
-
res.setHeader("Accept-Ranges", "bytes");
|
|
491
|
-
res.setHeader("Content-Type", "video/mp4");
|
|
492
|
-
res.setHeader("Cache-Control", "public, max-age=300");
|
|
493
|
-
res.setHeader("Cross-Origin-Resource-Policy", "same-origin");
|
|
494
|
-
res.vary("Range");
|
|
495
|
-
const rejectRange = () => {
|
|
496
|
-
res.status(416);
|
|
497
|
-
res.setHeader("Content-Range", `bytes */${fileSize}`);
|
|
498
|
-
// Do not let a shared cache retain a malformed/unsatisfiable request's
|
|
499
|
-
// response under the public representation's cache policy.
|
|
500
|
-
res.setHeader("Cache-Control", "no-store");
|
|
501
|
-
res.end();
|
|
502
|
-
};
|
|
503
|
-
if (!range) {
|
|
504
|
-
res.setHeader("Content-Length", fileSize);
|
|
505
|
-
pipeFile();
|
|
506
|
-
return;
|
|
507
|
-
}
|
|
508
|
-
// Single ranges cover browser video playback. RFC 9110 permits a server to
|
|
509
|
-
// reject unsupported multi-range requests, which we answer with 416.
|
|
510
|
-
const match = /^bytes=(\d*)-(\d*)$/.exec(range);
|
|
511
|
-
if (!match) {
|
|
512
|
-
rejectRange();
|
|
513
|
-
return;
|
|
514
|
-
}
|
|
515
|
-
const parsedStart = match[1] ? Number(match[1]) : undefined;
|
|
516
|
-
const parsedEnd = match[2] ? Number(match[2]) : undefined;
|
|
517
|
-
if ((parsedStart === undefined && parsedEnd === undefined) ||
|
|
518
|
-
(parsedStart !== undefined && !Number.isSafeInteger(parsedStart)) ||
|
|
519
|
-
(parsedEnd !== undefined && !Number.isSafeInteger(parsedEnd))) {
|
|
520
|
-
rejectRange();
|
|
521
|
-
return;
|
|
522
|
-
}
|
|
523
|
-
let start = parsedStart ?? 0;
|
|
524
|
-
let end = parsedEnd ?? fileSize - 1;
|
|
525
|
-
if (parsedStart === undefined && parsedEnd !== undefined) {
|
|
526
|
-
start = Math.max(fileSize - parsedEnd, 0);
|
|
527
|
-
end = fileSize - 1;
|
|
528
|
-
}
|
|
529
|
-
else {
|
|
530
|
-
// RFC 9110 treats an explicit last-byte position beyond the selected
|
|
531
|
-
// representation as the representation's final byte.
|
|
532
|
-
end = Math.min(end, fileSize - 1);
|
|
533
|
-
}
|
|
534
|
-
if (start < 0 || end < 0 || start >= fileSize || start > end) {
|
|
535
|
-
rejectRange();
|
|
536
|
-
return;
|
|
537
|
-
}
|
|
538
|
-
const chunkSize = end - start + 1;
|
|
539
|
-
res.status(206);
|
|
540
|
-
res.setHeader("Content-Range", `bytes ${start}-${end}/${fileSize}`);
|
|
541
|
-
res.setHeader("Content-Length", chunkSize);
|
|
542
|
-
pipeFile(start, end);
|
|
543
|
-
}
|
|
544
423
|
const REINITIALIZE_MESSAGE = "Session not found. The MCP server may have restarted. Please re-initialize the MCP connection.";
|
|
545
424
|
function logMissingSession() {
|
|
546
425
|
console.warn("[mcp-http] session missing; client must re-initialize");
|
|
@@ -581,17 +460,46 @@ export function createHttpRuntime(config, options) {
|
|
|
581
460
|
}, fn);
|
|
582
461
|
}
|
|
583
462
|
const handleStatelessMcpPost = async (req, res) => {
|
|
584
|
-
|
|
463
|
+
let bearerToken = getAuthenticatedBearerToken(req);
|
|
585
464
|
const startedAt = Date.now();
|
|
586
465
|
const hasToolCall = containsToolCall(req.body);
|
|
587
466
|
if (!bearerToken) {
|
|
588
467
|
rejectJsonRpc(res, 401, "Bearer authentication required.");
|
|
589
468
|
return;
|
|
590
469
|
}
|
|
470
|
+
let delegatedScopes;
|
|
471
|
+
if (oauth && res.locals.oauthVerified) {
|
|
472
|
+
const auth = req.auth;
|
|
473
|
+
if (!auth || res.destroyed)
|
|
474
|
+
return;
|
|
475
|
+
try {
|
|
476
|
+
const work = oauth.delegate(auth);
|
|
477
|
+
res.locals.oauthWork = work;
|
|
478
|
+
const delegated = await work;
|
|
479
|
+
if (res.destroyed)
|
|
480
|
+
return;
|
|
481
|
+
bearerToken = delegated.token;
|
|
482
|
+
delegatedScopes = delegated.scopes;
|
|
483
|
+
const redactionScope = `http-oauth:${randomUUID()}`;
|
|
484
|
+
registerSensitiveLogValues(redactionScope, [auth.token, bearerToken]);
|
|
485
|
+
const clearRedaction = () => clearSensitiveLogValues(redactionScope);
|
|
486
|
+
res.once("finish", clearRedaction);
|
|
487
|
+
res.once("close", clearRedaction);
|
|
488
|
+
}
|
|
489
|
+
catch (error) {
|
|
490
|
+
rejectOAuth(res, error);
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
}
|
|
591
494
|
// Stateless transports deliberately ignore caller-provided affinity.
|
|
592
495
|
delete req.headers["mcp-session-id"];
|
|
593
496
|
const server = createServer(options.clientFactory?.(bearerToken) ??
|
|
594
|
-
createQurlClientFromBearerToken(bearerToken, { qurlApiUrl: defaultQurlApiUrl }), version, "http", config.maxUploadFileDataBytes,
|
|
497
|
+
createQurlClientFromBearerToken(bearerToken, { qurlApiUrl: defaultQurlApiUrl }), version, "http", config.maxUploadFileDataBytes, {
|
|
498
|
+
...getToolCapabilities(bearerToken),
|
|
499
|
+
...(delegatedScopes
|
|
500
|
+
? { email: false, oauth: { scopes: delegatedScopes, resourceMetadataUrl } }
|
|
501
|
+
: {}),
|
|
502
|
+
});
|
|
595
503
|
const transport = options.transportFactory?.(true) ??
|
|
596
504
|
new StreamableHTTPServerTransport({ sessionIdGenerator: undefined });
|
|
597
505
|
const request = { server, transport, bearerToken };
|
|
@@ -780,7 +688,7 @@ export function createHttpRuntime(config, options) {
|
|
|
780
688
|
}
|
|
781
689
|
}
|
|
782
690
|
};
|
|
783
|
-
app.post(MCP_HTTP_PATH, mcpRateLimiter, ...authenticatedMcpPostMiddleware, parseMcpJsonBody, stateless ? handleStatelessMcpPost : handleMcpPost);
|
|
691
|
+
app.post(MCP_HTTP_PATH, mcpRateLimiter, ...authenticatedMcpPostMiddleware, parseMcpJsonBody, feedbackToolRateLimiter, stateless ? handleStatelessMcpPost : handleMcpPost);
|
|
784
692
|
app.get(MCP_HTTP_PATH,
|
|
785
693
|
// Keep unsupported stateless probes in the cheap per-IP bucket so they
|
|
786
694
|
// cannot flood 405 responses; the credential/DynamoDB limiter is bypassed.
|
|
@@ -872,46 +780,7 @@ export function createHttpRuntime(config, options) {
|
|
|
872
780
|
console.error(`HTTP middleware failed (${formatErrorForLog(error)})`);
|
|
873
781
|
rejectJsonRpc(res, 500, "Internal server error.");
|
|
874
782
|
};
|
|
875
|
-
|
|
876
|
-
// Page renderers must keep style blocks static. Interpolating request or
|
|
877
|
-
// config data into CSS would make this live-content hash authorize it.
|
|
878
|
-
return [...html.matchAll(/<style[^>]*>([\s\S]*?)<\/style>/gi)].map((match) => `'sha256-${createHash("sha256").update(match[1], "utf8").digest("base64")}'`);
|
|
879
|
-
}
|
|
880
|
-
function setPublicPageSecurityHeaders(res, styleSources = [], allowSameOriginMedia = false) {
|
|
881
|
-
const stylePolicy = styleSources.length > 0 ? styleSources.join(" ") : "'none'";
|
|
882
|
-
const mediaPolicy = allowSameOriginMedia ? "'self'" : "'none'";
|
|
883
|
-
res.set({
|
|
884
|
-
"Content-Security-Policy": `default-src 'none'; style-src ${stylePolicy}; img-src 'none'; media-src ${mediaPolicy}; base-uri 'none'; form-action 'none'; frame-ancestors 'none'`,
|
|
885
|
-
"Referrer-Policy": "no-referrer",
|
|
886
|
-
"X-Content-Type-Options": "nosniff",
|
|
887
|
-
"X-Frame-Options": "DENY",
|
|
888
|
-
});
|
|
889
|
-
}
|
|
890
|
-
const legalDocuments = config.serveLayerVLegalPages ? getLegalDocuments() : [];
|
|
891
|
-
for (const document of legalDocuments) {
|
|
892
|
-
const html = renderLegalDocumentHtml(document.path, baseUrl);
|
|
893
|
-
if (!html)
|
|
894
|
-
continue;
|
|
895
|
-
const styleSources = getInlineStyleSources(html);
|
|
896
|
-
app.get(document.path, publicRouteRateLimiter, (_req, res) => {
|
|
897
|
-
setPublicPageSecurityHeaders(res, styleSources);
|
|
898
|
-
res.type("html").send(html);
|
|
899
|
-
});
|
|
900
|
-
}
|
|
901
|
-
if (config.publicVideo) {
|
|
902
|
-
const publicVideo = config.publicVideo;
|
|
903
|
-
const videoPagePath = publicVideo.pagePath;
|
|
904
|
-
const videoFileRoute = getPublicVideoFileRoute(videoPagePath);
|
|
905
|
-
const videoPageHtml = renderPublicVideoPageHtml(publicVideo, baseUrl);
|
|
906
|
-
const videoStyleSources = getInlineStyleSources(videoPageHtml);
|
|
907
|
-
app.get(videoPagePath, publicRouteRateLimiter, (_req, res) => {
|
|
908
|
-
setPublicPageSecurityHeaders(res, videoStyleSources, true);
|
|
909
|
-
res.type("html").send(videoPageHtml);
|
|
910
|
-
});
|
|
911
|
-
app.get(videoFileRoute, videoFileRateLimiter, async (req, res) => {
|
|
912
|
-
await streamPublicVideo(req, res, publicVideo.filePath);
|
|
913
|
-
});
|
|
914
|
-
}
|
|
783
|
+
publicRoutes.register(app);
|
|
915
784
|
// Keep JSON-RPC error envelopes scoped to the protocol route. Public pages
|
|
916
785
|
// use a plain-text fallback so an unrelated handler failure cannot return a
|
|
917
786
|
// misleading JSON-RPC response (or Express's default stack-bearing HTML).
|
|
@@ -962,7 +831,7 @@ export function createHttpRuntime(config, options) {
|
|
|
962
831
|
sweepTimer?.unref();
|
|
963
832
|
const httpServer = app.listen(port, host, () => {
|
|
964
833
|
logInfo(`qURL MCP HTTP server listening on ${sanitizeLogValue(host)}:${port}`);
|
|
965
|
-
logInfo(
|
|
834
|
+
logInfo(`HTTP MCP auth mode: ${oauth ? "qURL API keys and Auth0 per-user OAuth" : "qURL API-key passthrough"}`);
|
|
966
835
|
if (config.maxUploadFileDataBytes > DEFAULT_MAX_UPLOAD_FILE_DATA_BYTES) {
|
|
967
836
|
console.warn(stateless
|
|
968
837
|
? "Warning: maxUploadFileDataBytes exceeds the default; stateless requests may use the configured parser ceiling before downstream bearer validation. Apply an authenticated edge request-size limit on hostile networks."
|
|
@@ -977,25 +846,7 @@ export function createHttpRuntime(config, options) {
|
|
|
977
846
|
console.warn("Warning: non-loopback HTTP listener has trustProxyHops=0, which is valid only for direct connections. Clients behind a reverse proxy will share the proxy's rate-limit bucket; set the exact trusted hop count.");
|
|
978
847
|
}
|
|
979
848
|
logInfo("HTTP and runtime config loaded.");
|
|
980
|
-
|
|
981
|
-
if (config.publicVideo) {
|
|
982
|
-
logInfo("Public video page enabled.");
|
|
983
|
-
// The video is an optional public asset, not an MCP readiness
|
|
984
|
-
// dependency. Warn at boot but keep /healthz and MCP available; the
|
|
985
|
-
// request path independently fails closed until the asset is usable.
|
|
986
|
-
void lstat(config.publicVideo.filePath)
|
|
987
|
-
.then((stats) => {
|
|
988
|
-
if (stats.isSymbolicLink() || !stats.isFile()) {
|
|
989
|
-
console.warn("[public-video] configured path is not a regular file at startup");
|
|
990
|
-
}
|
|
991
|
-
else if (stats.size === 0) {
|
|
992
|
-
console.warn("[public-video] configured video file is empty at startup");
|
|
993
|
-
}
|
|
994
|
-
})
|
|
995
|
-
.catch(() => {
|
|
996
|
-
console.warn("[public-video] configured video file is unavailable at startup");
|
|
997
|
-
});
|
|
998
|
-
}
|
|
849
|
+
publicRoutes.logStartupStatus();
|
|
999
850
|
if (defaultQurlConnectorUrl)
|
|
1000
851
|
logInfo("qURL Connector uploads enabled.");
|
|
1001
852
|
const smtpInspection = inspectSmtpConfig(runtimeConfigPath);
|