@agent-deck/backend 1.11.8 → 1.11.9
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/dist/.tsbuildinfo +1 -1
- package/dist/auth/client-grants.d.ts +109 -0
- package/dist/auth/client-grants.d.ts.map +1 -0
- package/dist/auth/client-grants.js +261 -0
- package/dist/auth/client-grants.js.map +1 -0
- package/dist/mcp-server.d.ts +124 -2
- package/dist/mcp-server.d.ts.map +1 -1
- package/dist/mcp-server.js +413 -12
- package/dist/mcp-server.js.map +1 -1
- package/dist/mcp-session-binding.d.ts +54 -0
- package/dist/mcp-session-binding.d.ts.map +1 -1
- package/dist/mcp-session-binding.js +81 -0
- package/dist/mcp-session-binding.js.map +1 -1
- package/dist/mcp-tools/register.d.ts.map +1 -1
- package/dist/mcp-tools/register.js +46 -0
- package/dist/mcp-tools/register.js.map +1 -1
- package/dist/models/database.d.ts +12 -8
- package/dist/models/database.d.ts.map +1 -1
- package/dist/models/database.js +49 -46
- package/dist/models/database.js.map +1 -1
- package/dist/routes/agent-grants.d.ts +9 -0
- package/dist/routes/agent-grants.d.ts.map +1 -0
- package/dist/routes/agent-grants.js +81 -0
- package/dist/routes/agent-grants.js.map +1 -0
- package/dist/routes/playbooks.d.ts.map +1 -1
- package/dist/routes/playbooks.js +9 -10
- package/dist/routes/playbooks.js.map +1 -1
- package/dist/routes/scope.d.ts.map +1 -1
- package/dist/routes/scope.js +4 -2
- package/dist/routes/scope.js.map +1 -1
- package/dist/routes/services.d.ts.map +1 -1
- package/dist/routes/services.js +6 -0
- package/dist/routes/services.js.map +1 -1
- package/dist/routes/trusted-session.d.ts.map +1 -1
- package/dist/routes/trusted-session.js +62 -1
- package/dist/routes/trusted-session.js.map +1 -1
- package/dist/routes/usage.d.ts +3 -1
- package/dist/routes/usage.d.ts.map +1 -1
- package/dist/routes/usage.js +20 -1
- package/dist/routes/usage.js.map +1 -1
- package/dist/scope/live-display-registry.d.ts +56 -1
- package/dist/scope/live-display-registry.d.ts.map +1 -1
- package/dist/scope/live-display-registry.js +115 -2
- package/dist/scope/live-display-registry.js.map +1 -1
- package/dist/server/cors-origins.d.ts +6 -0
- package/dist/server/cors-origins.d.ts.map +1 -0
- package/dist/server/cors-origins.js +103 -0
- package/dist/server/cors-origins.js.map +1 -0
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +11 -24
- package/dist/server/index.js.map +1 -1
- package/dist/trusted-session/route-policy-registry.d.ts.map +1 -1
- package/dist/trusted-session/route-policy-registry.js +3 -0
- package/dist/trusted-session/route-policy-registry.js.map +1 -1
- package/dist/trusted-session/store.d.ts +27 -0
- package/dist/trusted-session/store.d.ts.map +1 -1
- package/dist/trusted-session/store.js +34 -7
- package/dist/trusted-session/store.js.map +1 -1
- package/dist/vault/encrypted-file-secret-store.d.ts +61 -0
- package/dist/vault/encrypted-file-secret-store.d.ts.map +1 -0
- package/dist/vault/encrypted-file-secret-store.js +211 -0
- package/dist/vault/encrypted-file-secret-store.js.map +1 -0
- package/dist/vault/index.d.ts +1 -0
- package/dist/vault/index.d.ts.map +1 -1
- package/dist/vault/index.js +1 -0
- package/dist/vault/index.js.map +1 -1
- package/dist/vault/secret-store.d.ts.map +1 -1
- package/dist/vault/secret-store.js +14 -1
- package/dist/vault/secret-store.js.map +1 -1
- package/package.json +2 -2
- package/static-ui/assets/index-sENM6Awe.js +235 -0
- package/static-ui/index.html +1 -1
- package/static-ui/assets/index-DCImqPRz.js +0 -235
package/dist/mcp-server.js
CHANGED
|
@@ -3,14 +3,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.AgentDeckMCPServer = void 0;
|
|
6
|
+
exports.AgentDeckMCPServer = exports.LIVE_TOUCH_KEEPALIVE_ENV_VAR = exports.DEFAULT_LIVE_TOUCH_KEEPALIVE_MS = exports.MCP_REQUIRE_BEARER_ENV_VAR = void 0;
|
|
7
|
+
exports.isBearerGrantRequired = isBearerGrantRequired;
|
|
8
|
+
exports.resolveLiveTouchKeepAliveMs = resolveLiveTouchKeepAliveMs;
|
|
7
9
|
const mcp_js_1 = require("@modelcontextprotocol/sdk/server/mcp.js");
|
|
8
10
|
const streamableHttp_js_1 = require("@modelcontextprotocol/sdk/server/streamableHttp.js");
|
|
9
11
|
const types_js_1 = require("@modelcontextprotocol/sdk/types.js");
|
|
10
12
|
const shared_1 = require("@agent-deck/shared");
|
|
11
13
|
const express_1 = __importDefault(require("express"));
|
|
12
14
|
const node_crypto_1 = require("node:crypto");
|
|
15
|
+
const better_sqlite3_1 = __importDefault(require("better-sqlite3"));
|
|
13
16
|
const version_1 = require("./lib/version");
|
|
17
|
+
const paths_1 = require("./lib/paths");
|
|
18
|
+
const live_display_registry_1 = require("./scope/live-display-registry");
|
|
19
|
+
const client_grants_1 = require("./auth/client-grants");
|
|
14
20
|
const backend_api_error_1 = require("./lib/backend-api-error");
|
|
15
21
|
const policy_1 = require("./mcp-tools/policy");
|
|
16
22
|
const mcp_session_binding_1 = require("./mcp-session-binding");
|
|
@@ -29,6 +35,70 @@ function readLaunchDeckHeader(req) {
|
|
|
29
35
|
const value = typeof raw === 'string' ? raw.trim() : '';
|
|
30
36
|
return value || undefined;
|
|
31
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* Opaque run-correlation id for launch-selected sessions (NOT-304).
|
|
40
|
+
* Returned unvalidated — the binding store normalizes and adopt-once
|
|
41
|
+
* semantics keep it observability-only: it can never select a deck, grant
|
|
42
|
+
* access, change mode, or participate in authorization.
|
|
43
|
+
*/
|
|
44
|
+
function readCorrelationIdHeader(req) {
|
|
45
|
+
const raw = req.headers[shared_1.AGENT_DECK_CORRELATION_HEADER];
|
|
46
|
+
const value = typeof raw === 'string' ? raw.trim() : '';
|
|
47
|
+
return value || undefined;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* NOT-318: hosted-mode flag. When `AGENT_DECK_MCP_REQUIRE_BEARER=1`, every
|
|
51
|
+
* `/mcp` request requires a valid per-agent grant secret, regardless of
|
|
52
|
+
* peer address. Peer-address detection is explicitly rejected as the
|
|
53
|
+
* trigger (TLS-terminating proxies forward internet traffic over
|
|
54
|
+
* loopback), and `X-Forwarded-*` headers are never trusted for auth.
|
|
55
|
+
* The local launcher never sets this flag, so loopback sessions stay
|
|
56
|
+
* bearer-free with zero config change.
|
|
57
|
+
*/
|
|
58
|
+
exports.MCP_REQUIRE_BEARER_ENV_VAR = 'AGENT_DECK_MCP_REQUIRE_BEARER';
|
|
59
|
+
function isBearerGrantRequired(raw = process.env[exports.MCP_REQUIRE_BEARER_ENV_VAR]) {
|
|
60
|
+
return raw === '1' || raw?.trim().toLowerCase() === 'true';
|
|
61
|
+
}
|
|
62
|
+
/** Extract the bearer secret without ever logging or echoing it. */
|
|
63
|
+
function readBearerToken(req) {
|
|
64
|
+
const header = req.headers.authorization;
|
|
65
|
+
if (typeof header !== 'string') {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
const match = /^Bearer (.+)$/.exec(header.trim());
|
|
69
|
+
const token = match?.[1]?.trim();
|
|
70
|
+
return token || null;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* NOT-309: keep-alive touch cadence for idle-but-connected MCP sessions.
|
|
74
|
+
* Every MCP tool call already bumps `lastActivityAt` (debounced POST touch),
|
|
75
|
+
* but a session with an open transport and no tool calls sends no HTTP POSTs
|
|
76
|
+
* (the SSE stream is a GET), so without a keep-alive it would expire out of
|
|
77
|
+
* the live-display registry while still connected. Override via
|
|
78
|
+
* `AGENT_DECK_MCP_LIVE_TOUCH_KEEPALIVE_MS` (`0` disables); the default is
|
|
79
|
+
* also clamped under a third of the stale bound so a shortened
|
|
80
|
+
* `LIVE_DISPLAY_STALE_MS` never outruns it.
|
|
81
|
+
*
|
|
82
|
+
* Both processes must see the same `LIVE_DISPLAY_STALE_MS`: the MCP server
|
|
83
|
+
* runs separately (mcp-index) and resolves the bound from its own
|
|
84
|
+
* environment, so shortening it only on the backend leaves the keep-alive
|
|
85
|
+
* at 5 minutes and idle sessions expire. Export it for both processes.
|
|
86
|
+
*/
|
|
87
|
+
exports.DEFAULT_LIVE_TOUCH_KEEPALIVE_MS = 5 * 60_000;
|
|
88
|
+
exports.LIVE_TOUCH_KEEPALIVE_ENV_VAR = 'AGENT_DECK_MCP_LIVE_TOUCH_KEEPALIVE_MS';
|
|
89
|
+
function resolveLiveTouchKeepAliveMs(raw, staleMs) {
|
|
90
|
+
const fallback = staleMs > 0
|
|
91
|
+
? Math.max(1, Math.min(exports.DEFAULT_LIVE_TOUCH_KEEPALIVE_MS, Math.floor(staleMs / 3)))
|
|
92
|
+
: exports.DEFAULT_LIVE_TOUCH_KEEPALIVE_MS;
|
|
93
|
+
if (raw === undefined || raw.trim() === '') {
|
|
94
|
+
return fallback;
|
|
95
|
+
}
|
|
96
|
+
const parsed = Number(raw.trim());
|
|
97
|
+
if (!Number.isFinite(parsed) || parsed < 0) {
|
|
98
|
+
return fallback;
|
|
99
|
+
}
|
|
100
|
+
return Math.floor(parsed);
|
|
101
|
+
}
|
|
32
102
|
class AgentDeckMCPServer {
|
|
33
103
|
/** Cap on remembered stale session ids — the tally is diagnostics, not a ledger. */
|
|
34
104
|
static STALE_SESSION_SAMPLE_LIMIT = 200;
|
|
@@ -46,6 +116,8 @@ class AgentDeckMCPServer {
|
|
|
46
116
|
/** Session badge from the backend registry (POST /api/scope/live-display response). */
|
|
47
117
|
badgeBySession = new Map();
|
|
48
118
|
lastTouchAtMs = new Map();
|
|
119
|
+
/** NOT-309: keep-alive timer proving idle-but-connected sessions still live. */
|
|
120
|
+
liveTouchKeepAliveTimer = null;
|
|
49
121
|
/** In-flight live-display unregisters so `stop()` can drain them before closing. */
|
|
50
122
|
unregisterTasks = new Map();
|
|
51
123
|
/** Changes on every process start — how a client detects it outlived the server. */
|
|
@@ -70,11 +142,16 @@ class AgentDeckMCPServer {
|
|
|
70
142
|
}
|
|
71
143
|
return this.mcpServerForRegistration;
|
|
72
144
|
}
|
|
73
|
-
|
|
145
|
+
/** Injected grant store (tests). `undefined` means "resolve lazily". */
|
|
146
|
+
grantStoreOverride;
|
|
147
|
+
/** Lazily opened grant store for hosted mode (shared backend database file). */
|
|
148
|
+
grantStoreCache = null;
|
|
149
|
+
constructor(port = 3001, backendUrl = 'http://localhost:8000', toolProfile, host = '127.0.0.1', options) {
|
|
74
150
|
this.port = port;
|
|
75
151
|
this.backendUrl = backendUrl;
|
|
76
152
|
this.toolProfile = toolProfile ?? (0, profile_1.resolveMcpToolProfile)();
|
|
77
153
|
this.host = host;
|
|
154
|
+
this.grantStoreOverride = options?.grantStore;
|
|
78
155
|
this.app = (0, express_1.default)();
|
|
79
156
|
this.app.use(express_1.default.json());
|
|
80
157
|
this.setupRoutes();
|
|
@@ -83,6 +160,44 @@ class AgentDeckMCPServer {
|
|
|
83
160
|
deckId: process.env.AGENT_DECK_DECK_ID,
|
|
84
161
|
});
|
|
85
162
|
}
|
|
163
|
+
/**
|
|
164
|
+
* NOT-318: grant metadata store. An injected store (tests) wins; otherwise
|
|
165
|
+
* the shared backend database file is opened lazily — only when a bearer
|
|
166
|
+
* is presented or hosted mode requires one, so loopback-only processes
|
|
167
|
+
* and existing tests never touch the database file.
|
|
168
|
+
*/
|
|
169
|
+
getGrantStore() {
|
|
170
|
+
if (this.grantStoreOverride !== undefined) {
|
|
171
|
+
return this.grantStoreOverride;
|
|
172
|
+
}
|
|
173
|
+
if (this.grantStoreCache) {
|
|
174
|
+
return this.grantStoreCache;
|
|
175
|
+
}
|
|
176
|
+
try {
|
|
177
|
+
const dbPath = process.env.AGENT_DECK_DB_PATH?.trim() || (0, paths_1.resolveDatabasePath)();
|
|
178
|
+
this.grantStoreCache = new client_grants_1.ClientGrantStore(new better_sqlite3_1.default(dbPath));
|
|
179
|
+
return this.grantStoreCache;
|
|
180
|
+
}
|
|
181
|
+
catch {
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* NOT-318: authenticate one bearer token to a grant principal. Every
|
|
187
|
+
* failure mode (missing store, malformed, unknown id, secret mismatch,
|
|
188
|
+
* expired, revoked) returns null — the caller answers one uniform 401
|
|
189
|
+
* with no oracle. Touches `lastUsedAt`; never logs or returns the secret.
|
|
190
|
+
*/
|
|
191
|
+
authenticateGrant(token) {
|
|
192
|
+
if (!token) {
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
195
|
+
const store = this.getGrantStore();
|
|
196
|
+
if (!store) {
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
return store.authenticateToken(token);
|
|
200
|
+
}
|
|
86
201
|
/** Actual listening port (resolves OS-assigned `port: 0` after `start()`). */
|
|
87
202
|
getPort() {
|
|
88
203
|
return this.port;
|
|
@@ -162,13 +277,26 @@ class AgentDeckMCPServer {
|
|
|
162
277
|
}
|
|
163
278
|
}
|
|
164
279
|
static TOUCH_DEBOUNCE_MS = 5_000;
|
|
165
|
-
/**
|
|
166
|
-
|
|
280
|
+
/**
|
|
281
|
+
* Fire-and-forget lastActivityAt bump; only for sessions this process
|
|
282
|
+
* registered (badge-holding). `force` bypasses the per-request debounce —
|
|
283
|
+
* the keep-alive already runs on a minutes-long cadence, so debouncing it
|
|
284
|
+
* would only delay proof of life.
|
|
285
|
+
*
|
|
286
|
+
* NOT-309 repair round 2: when the backend reports `found: false` the
|
|
287
|
+
* entry was swept while this transport stayed open (host sleep longer
|
|
288
|
+
* than the stale bound, then a status-line read on wake). Re-register so
|
|
289
|
+
* the live session returns to the status line without a reconnect. An
|
|
290
|
+
* explicit `found === false` is required — older backends answer `{}` and
|
|
291
|
+
* must not trigger a re-register storm.
|
|
292
|
+
*/
|
|
293
|
+
touchLiveDisplay(sessionId, force = false) {
|
|
167
294
|
if (!this.badgeBySession.has(sessionId)) {
|
|
168
295
|
return;
|
|
169
296
|
}
|
|
170
297
|
const now = Date.now();
|
|
171
|
-
if (
|
|
298
|
+
if (!force &&
|
|
299
|
+
now - (this.lastTouchAtMs.get(sessionId) ?? 0) < AgentDeckMCPServer.TOUCH_DEBOUNCE_MS) {
|
|
172
300
|
return;
|
|
173
301
|
}
|
|
174
302
|
this.lastTouchAtMs.set(sessionId, now);
|
|
@@ -176,7 +304,17 @@ class AgentDeckMCPServer {
|
|
|
176
304
|
method: 'POST',
|
|
177
305
|
headers: { 'Content-Type': 'application/json' },
|
|
178
306
|
body: JSON.stringify({ at: new Date().toISOString() }),
|
|
179
|
-
}, sessionId)
|
|
307
|
+
}, sessionId)
|
|
308
|
+
.then((result) => {
|
|
309
|
+
// Guard against the close race: a keep-alive touch in flight when
|
|
310
|
+
// transport.onclose sends DELETE answers found:false after the entry
|
|
311
|
+
// is gone. Re-registering then would resurrect a closed session, so
|
|
312
|
+
// only re-register while the transport is still open.
|
|
313
|
+
if (result && result.found === false && this.sessions.has(sessionId)) {
|
|
314
|
+
void this.registerLiveDisplay(sessionId).catch(() => { });
|
|
315
|
+
}
|
|
316
|
+
})
|
|
317
|
+
.catch(() => { });
|
|
180
318
|
}
|
|
181
319
|
static UNREGISTER_TIMEOUT_MS = 3_000;
|
|
182
320
|
/** Override via AGENT_DECK_MCP_UNREGISTER_TIMEOUT_MS (tests use a short value). */
|
|
@@ -190,6 +328,36 @@ class AgentDeckMCPServer {
|
|
|
190
328
|
? parsed
|
|
191
329
|
: AgentDeckMCPServer.UNREGISTER_TIMEOUT_MS;
|
|
192
330
|
}
|
|
331
|
+
/**
|
|
332
|
+
* NOT-309: keep the registry fresh for sessions with an open transport but
|
|
333
|
+
* no tool calls. Only badge-holding (registered) sessions are touched; the
|
|
334
|
+
* backend ignores touches for sessions it never saw, so unassigned or
|
|
335
|
+
* half-initialized sessions are harmless no-ops by construction.
|
|
336
|
+
*/
|
|
337
|
+
touchAllLiveDisplays() {
|
|
338
|
+
for (const sessionId of this.sessions.keys()) {
|
|
339
|
+
this.touchLiveDisplay(sessionId, true);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
startLiveDisplayKeepAlive() {
|
|
343
|
+
this.stopLiveDisplayKeepAlive();
|
|
344
|
+
const staleMs = (0, live_display_registry_1.resolveLiveDisplayStaleMs)(process.env.LIVE_DISPLAY_STALE_MS);
|
|
345
|
+
const intervalMs = resolveLiveTouchKeepAliveMs(process.env[exports.LIVE_TOUCH_KEEPALIVE_ENV_VAR], staleMs);
|
|
346
|
+
if (intervalMs <= 0) {
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
const timer = setInterval(() => {
|
|
350
|
+
this.touchAllLiveDisplays();
|
|
351
|
+
}, intervalMs);
|
|
352
|
+
timer.unref?.();
|
|
353
|
+
this.liveTouchKeepAliveTimer = timer;
|
|
354
|
+
}
|
|
355
|
+
stopLiveDisplayKeepAlive() {
|
|
356
|
+
if (this.liveTouchKeepAliveTimer) {
|
|
357
|
+
clearInterval(this.liveTouchKeepAliveTimer);
|
|
358
|
+
this.liveTouchKeepAliveTimer = null;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
193
361
|
async unregisterLiveDisplay(sessionId) {
|
|
194
362
|
const controller = new AbortController();
|
|
195
363
|
const timer = setTimeout(() => controller.abort(), AgentDeckMCPServer.unregisterTimeoutMs());
|
|
@@ -696,6 +864,127 @@ class AgentDeckMCPServer {
|
|
|
696
864
|
id: null,
|
|
697
865
|
});
|
|
698
866
|
}
|
|
867
|
+
/**
|
|
868
|
+
* NOT-318: the single 401 envelope for every credential failure on a
|
|
869
|
+
* hosted endpoint (missing / malformed / invalid / expired / revoked).
|
|
870
|
+
* Byte-identical every time — including the constant `WWW-Authenticate:
|
|
871
|
+
* Bearer` challenge — so probers learn nothing, and generic HTTP
|
|
872
|
+
* clients stay well-behaved. Never carries grant state or deck detail.
|
|
873
|
+
*/
|
|
874
|
+
sendGrantRequired(res) {
|
|
875
|
+
res.status(401).set('WWW-Authenticate', 'Bearer').json({
|
|
876
|
+
jsonrpc: '2.0',
|
|
877
|
+
error: { code: -32001, message: 'GRANT_REQUIRED' },
|
|
878
|
+
id: null,
|
|
879
|
+
});
|
|
880
|
+
}
|
|
881
|
+
/**
|
|
882
|
+
* NOT-318: authenticated but outside the grant allowlist. Only reachable
|
|
883
|
+
* post-auth, so it oracles the allowlist solely to the already-
|
|
884
|
+
* authenticated owner. Transport kept.
|
|
885
|
+
*/
|
|
886
|
+
sendOutOfScope(res) {
|
|
887
|
+
res.status(403).json({
|
|
888
|
+
jsonrpc: '2.0',
|
|
889
|
+
error: { code: -32001, message: 'RESOURCE_OUT_OF_SCOPE' },
|
|
890
|
+
id: null,
|
|
891
|
+
});
|
|
892
|
+
}
|
|
893
|
+
/**
|
|
894
|
+
* NOT-318: bind a fresh MCP session to an authenticated grant principal.
|
|
895
|
+
* Mirrors `authenticateLaunchDeck` (workspace + correlation handling) but
|
|
896
|
+
* resolves through the grant: the deck was already constrained by
|
|
897
|
+
* `resolveGrantDeck`, and the owning grant id is linked on the runtime
|
|
898
|
+
* session so deck-switch creation/approval enforces the same allowlist.
|
|
899
|
+
*/
|
|
900
|
+
async authenticateGrantDeck(sessionId, req, principal, deckId) {
|
|
901
|
+
const response = await fetch(`${this.backendUrl}/api/trusted-session/mcp/connect-deck`, {
|
|
902
|
+
method: 'POST',
|
|
903
|
+
headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
|
|
904
|
+
body: JSON.stringify({
|
|
905
|
+
deckId,
|
|
906
|
+
mcpSessionId: sessionId,
|
|
907
|
+
grantId: principal.grantId,
|
|
908
|
+
}),
|
|
909
|
+
});
|
|
910
|
+
const body = (await response.json());
|
|
911
|
+
if (!response.ok || !body.success || !body.data) {
|
|
912
|
+
throw new Error(body.error ?? 'GRANT_DECK_INVALID');
|
|
913
|
+
}
|
|
914
|
+
const existing = this.sessionBinding.getBinding(sessionId);
|
|
915
|
+
const workspaceRoot = readWorkspaceRootHeader(req) ??
|
|
916
|
+
existing.workspaceRoot ??
|
|
917
|
+
(process.env.AGENT_DECK_WORKSPACE?.trim() || undefined);
|
|
918
|
+
this.sessionBinding.setGrantSession(sessionId, {
|
|
919
|
+
grantId: principal.grantId,
|
|
920
|
+
label: principal.label,
|
|
921
|
+
defaultDeck: principal.defaultDeck,
|
|
922
|
+
allowedDecks: principal.allowedDecks,
|
|
923
|
+
runtimeSessionId: body.data.sessionId,
|
|
924
|
+
deckId: body.data.deckId,
|
|
925
|
+
workspaceRoot,
|
|
926
|
+
mode: body.data.mode,
|
|
927
|
+
});
|
|
928
|
+
const correlationId = readCorrelationIdHeader(req);
|
|
929
|
+
if (correlationId) {
|
|
930
|
+
this.sessionBinding.setCorrelationId(sessionId, correlationId);
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
/**
|
|
934
|
+
* NOT-318: revalidate a grant-bound session on every follow-up request.
|
|
935
|
+
* The grant (not a cached decision) is re-resolved per request, so
|
|
936
|
+
* revocation and expiry fail the *next* request closed — no grace
|
|
937
|
+
* window, strictly inside the 60s bound. The deck header is
|
|
938
|
+
* request-only: a header outside the allowlist is denied with 403 and
|
|
939
|
+
* the binding is left untouched; a header for another allowed deck is
|
|
940
|
+
* ignored (deck changes go through human-approved switch_deck, which
|
|
941
|
+
* re-checks the same allowlist). Transport kept on every denial so a
|
|
942
|
+
* correct credential on the next attempt can succeed.
|
|
943
|
+
*/
|
|
944
|
+
async requireGrantFollowUp(sessionId, req, res) {
|
|
945
|
+
const scope = this.sessionBinding.getGrantScope(sessionId);
|
|
946
|
+
if (!scope) {
|
|
947
|
+
this.sendGrantRequired(res);
|
|
948
|
+
return false;
|
|
949
|
+
}
|
|
950
|
+
const principal = this.authenticateGrant(readBearerToken(req));
|
|
951
|
+
if (!principal || principal.grantId !== scope.grantId) {
|
|
952
|
+
const presented = (0, client_grants_1.parseGrantToken)(readBearerToken(req) ?? '');
|
|
953
|
+
console.warn('[agent-deck] Grant follow-up auth failed (transport kept):', `grantId=${presented?.grantId ?? 'none'}`);
|
|
954
|
+
this.sendGrantRequired(res);
|
|
955
|
+
return false;
|
|
956
|
+
}
|
|
957
|
+
// Renew the backend lease and pick up post-approval deck/mode moves,
|
|
958
|
+
// mirroring the launch follow-up refresh. A backend-revoked session
|
|
959
|
+
// (grant revoked via the owner API) fails closed here as well.
|
|
960
|
+
try {
|
|
961
|
+
await this.refreshRuntimeSession(sessionId);
|
|
962
|
+
}
|
|
963
|
+
catch {
|
|
964
|
+
console.warn('[agent-deck] Grant session refresh failed (transport kept):', `grantId=${principal.grantId}`);
|
|
965
|
+
this.sendGrantRequired(res);
|
|
966
|
+
return false;
|
|
967
|
+
}
|
|
968
|
+
// The unified authorization path: the session principal must allow
|
|
969
|
+
// the requested deck. `principalAllowsDeck` is the same check local
|
|
970
|
+
// launch sessions resolve through (local principals allow all).
|
|
971
|
+
const header = readLaunchDeckHeader(req);
|
|
972
|
+
const boundDeck = this.sessionBinding.getBinding(sessionId).deckId;
|
|
973
|
+
if (header && header !== boundDeck && !(0, client_grants_1.principalAllowsDeck)(principal, header)) {
|
|
974
|
+
console.warn('[agent-deck] Grant follow-up deck denied (transport kept):', `grantId=${principal.grantId}`);
|
|
975
|
+
this.sendOutOfScope(res);
|
|
976
|
+
return false;
|
|
977
|
+
}
|
|
978
|
+
const workspaceRoot = readWorkspaceRootHeader(req);
|
|
979
|
+
if (workspaceRoot) {
|
|
980
|
+
this.sessionBinding.setWorkspace(sessionId, workspaceRoot);
|
|
981
|
+
}
|
|
982
|
+
const followUpCorrelation = readCorrelationIdHeader(req);
|
|
983
|
+
if (followUpCorrelation) {
|
|
984
|
+
this.sessionBinding.setCorrelationId(sessionId, followUpCorrelation);
|
|
985
|
+
}
|
|
986
|
+
return true;
|
|
987
|
+
}
|
|
699
988
|
async authenticateLaunchDeck(sessionId, req) {
|
|
700
989
|
const deckId = readLaunchDeckHeader(req);
|
|
701
990
|
if (!deckId) {
|
|
@@ -712,6 +1001,12 @@ class AgentDeckMCPServer {
|
|
|
712
1001
|
if (workspaceRoot) {
|
|
713
1002
|
this.sessionBinding.setWorkspace(sessionId, workspaceRoot);
|
|
714
1003
|
}
|
|
1004
|
+
// Adopt-once: a launch that arrived without correlation can still
|
|
1005
|
+
// attach one; an established value never moves (NOT-304).
|
|
1006
|
+
const followUpCorrelation = readCorrelationIdHeader(req);
|
|
1007
|
+
if (followUpCorrelation) {
|
|
1008
|
+
this.sessionBinding.setCorrelationId(sessionId, followUpCorrelation);
|
|
1009
|
+
}
|
|
715
1010
|
return;
|
|
716
1011
|
}
|
|
717
1012
|
const response = await fetch(`${this.backendUrl}/api/trusted-session/mcp/connect-deck`, {
|
|
@@ -735,9 +1030,16 @@ class AgentDeckMCPServer {
|
|
|
735
1030
|
workspaceRoot,
|
|
736
1031
|
mode: body.data.mode,
|
|
737
1032
|
});
|
|
1033
|
+
// Attach the opaque run-correlation id to this launch-selected session
|
|
1034
|
+
// (NOT-304). Adopt-once and validated inside the store: an invalid or
|
|
1035
|
+
// later-changed value can never affect deck, workspace, mode, or auth.
|
|
1036
|
+
const correlationId = readCorrelationIdHeader(req);
|
|
1037
|
+
if (correlationId) {
|
|
1038
|
+
this.sessionBinding.setCorrelationId(sessionId, correlationId);
|
|
1039
|
+
}
|
|
738
1040
|
}
|
|
739
1041
|
async disconnectTrustedSession(sessionId, _req) {
|
|
740
|
-
if (this.sessionBinding.isLaunchSession(sessionId)) {
|
|
1042
|
+
if (this.sessionBinding.isLaunchSession(sessionId) || this.sessionBinding.isGrantSession(sessionId)) {
|
|
741
1043
|
try {
|
|
742
1044
|
await fetch(`${this.backendUrl}/api/trusted-session/mcp/disconnect-deck`, {
|
|
743
1045
|
method: 'POST',
|
|
@@ -793,12 +1095,62 @@ class AgentDeckMCPServer {
|
|
|
793
1095
|
id: null,
|
|
794
1096
|
});
|
|
795
1097
|
}
|
|
1098
|
+
/**
|
|
1099
|
+
* NOT-318: initialize with a bearer grant. Authenticates before
|
|
1100
|
+
* advertising any `mcp-session-id`: failure answers the uniform 401
|
|
1101
|
+
* with no transport or session record created. Success binds the fresh
|
|
1102
|
+
* session to the grant-constrained deck (absent header → default;
|
|
1103
|
+
* out-of-grant header → 403, no session).
|
|
1104
|
+
*/
|
|
1105
|
+
async handleGrantInitialize(req, res, bearer) {
|
|
1106
|
+
const principal = this.authenticateGrant(bearer);
|
|
1107
|
+
if (!principal) {
|
|
1108
|
+
const presented = (0, client_grants_1.parseGrantToken)(bearer);
|
|
1109
|
+
console.warn('[agent-deck] Grant initialize auth failed (no session created):', `grantId=${presented?.grantId ?? 'none'}`);
|
|
1110
|
+
this.sendGrantRequired(res);
|
|
1111
|
+
return;
|
|
1112
|
+
}
|
|
1113
|
+
const deckId = (0, client_grants_1.resolveGrantDeck)(principal, readLaunchDeckHeader(req));
|
|
1114
|
+
if (!deckId) {
|
|
1115
|
+
console.warn('[agent-deck] Grant initialize deck denied (no session created):', `grantId=${principal.grantId}`);
|
|
1116
|
+
this.sendOutOfScope(res);
|
|
1117
|
+
return;
|
|
1118
|
+
}
|
|
1119
|
+
const sessionId = (0, node_crypto_1.randomUUID)();
|
|
1120
|
+
try {
|
|
1121
|
+
await this.authenticateGrantDeck(sessionId, req, principal, deckId);
|
|
1122
|
+
}
|
|
1123
|
+
catch (error) {
|
|
1124
|
+
console.warn('[agent-deck] Grant deck bind failed before MCP init (no session created):', `grantId=${principal.grantId}`, error instanceof Error ? error.message : error);
|
|
1125
|
+
this.sessionBinding.clearSession(sessionId);
|
|
1126
|
+
this.sendGrantRequired(res);
|
|
1127
|
+
return;
|
|
1128
|
+
}
|
|
1129
|
+
await this.establishTransportSession(req, res, sessionId);
|
|
1130
|
+
}
|
|
796
1131
|
async handleMcpPost(req, res) {
|
|
797
1132
|
const sessionIdHeader = this.getSessionIdHeader(req);
|
|
798
1133
|
const existing = sessionIdHeader ? this.sessions.get(sessionIdHeader) : undefined;
|
|
1134
|
+
const bearerRequired = isBearerGrantRequired();
|
|
799
1135
|
if (existing && sessionIdHeader) {
|
|
800
|
-
|
|
801
|
-
|
|
1136
|
+
// Grant-bound sessions revalidate the bearer on every request and
|
|
1137
|
+
// never take the trust-the-header launch path: a forged deck header
|
|
1138
|
+
// is denied by the grant allowlist, not trusted into a new deck.
|
|
1139
|
+
if (this.sessionBinding.isGrantSession(sessionIdHeader)) {
|
|
1140
|
+
if (!(await this.requireGrantFollowUp(sessionIdHeader, req, res))) {
|
|
1141
|
+
return;
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
else {
|
|
1145
|
+
// Hosted mode has no bearer-free sessions; a non-grant session id
|
|
1146
|
+
// is unusable there — fail closed before touching the transport.
|
|
1147
|
+
if (bearerRequired) {
|
|
1148
|
+
this.sendGrantRequired(res);
|
|
1149
|
+
return;
|
|
1150
|
+
}
|
|
1151
|
+
if (!(await this.requireFollowUpDeckHeader(sessionIdHeader, req, res))) {
|
|
1152
|
+
return;
|
|
1153
|
+
}
|
|
802
1154
|
}
|
|
803
1155
|
this.touchLiveDisplay(sessionIdHeader);
|
|
804
1156
|
await existing.transport.handleRequest(req, res, req.body);
|
|
@@ -810,6 +1162,13 @@ class AgentDeckMCPServer {
|
|
|
810
1162
|
((0, types_js_1.isInitializeRequest)(body) ||
|
|
811
1163
|
(Array.isArray(body) && body.some((message) => (0, types_js_1.isInitializeRequest)(message))));
|
|
812
1164
|
if (!isInit) {
|
|
1165
|
+
// Hosted endpoints run the bearer-grant gate before session
|
|
1166
|
+
// existence: an unauthenticated prober gets 401, never the NOT-101
|
|
1167
|
+
// 404 body (which echoes the session id plus instance metadata).
|
|
1168
|
+
if (bearerRequired && !this.authenticateGrant(readBearerToken(req))) {
|
|
1169
|
+
this.sendGrantRequired(res);
|
|
1170
|
+
return;
|
|
1171
|
+
}
|
|
813
1172
|
// A session id we don't know is a restart, not a malformed request — 404 so
|
|
814
1173
|
// the client re-initializes. An initialize carrying a stale id falls through
|
|
815
1174
|
// and gets a fresh session, which is exactly the recovery we want.
|
|
@@ -824,6 +1183,18 @@ class AgentDeckMCPServer {
|
|
|
824
1183
|
});
|
|
825
1184
|
return;
|
|
826
1185
|
}
|
|
1186
|
+
// A presented bearer always resolves as a remote grant — even in local
|
|
1187
|
+
// mode — while loopback launcher connections without one keep the
|
|
1188
|
+
// existing deck-header path with zero config change.
|
|
1189
|
+
const presentedBearer = readBearerToken(req);
|
|
1190
|
+
if (presentedBearer) {
|
|
1191
|
+
await this.handleGrantInitialize(req, res, presentedBearer);
|
|
1192
|
+
return;
|
|
1193
|
+
}
|
|
1194
|
+
if (bearerRequired) {
|
|
1195
|
+
this.sendGrantRequired(res);
|
|
1196
|
+
return;
|
|
1197
|
+
}
|
|
827
1198
|
const launchDeck = readLaunchDeckHeader(req);
|
|
828
1199
|
const skipDeckHeader = (0, mcp_unassigned_1.skipDeckHeaderAuth)();
|
|
829
1200
|
const unassigned = !launchDeck && !skipDeckHeader;
|
|
@@ -846,6 +1217,16 @@ class AgentDeckMCPServer {
|
|
|
846
1217
|
this.sessionBinding.setWorkspace(sessionId, workspaceRoot);
|
|
847
1218
|
}
|
|
848
1219
|
}
|
|
1220
|
+
await this.establishTransportSession(req, res, sessionId);
|
|
1221
|
+
}
|
|
1222
|
+
/**
|
|
1223
|
+
* Shared transport handshake tail: create the per-session MCP server,
|
|
1224
|
+
* connect the streamable transport, and register the live session. Runs
|
|
1225
|
+
* only after the session is authenticated and bound (launch, grant, or
|
|
1226
|
+
* unassigned) — never before.
|
|
1227
|
+
*/
|
|
1228
|
+
async establishTransportSession(req, res, sessionId) {
|
|
1229
|
+
const body = req.body;
|
|
849
1230
|
const server = this.createMcpServer(sessionId);
|
|
850
1231
|
let sessionEntry;
|
|
851
1232
|
const transport = new streamableHttp_js_1.StreamableHTTPServerTransport({
|
|
@@ -877,7 +1258,7 @@ class AgentDeckMCPServer {
|
|
|
877
1258
|
await transport.handleRequest(req, res, body);
|
|
878
1259
|
}
|
|
879
1260
|
catch (error) {
|
|
880
|
-
console.warn('[agent-deck] MCP initialize failed after
|
|
1261
|
+
console.warn('[agent-deck] MCP initialize failed after session auth — revoking runtime session:', error instanceof Error ? error.message : error);
|
|
881
1262
|
this.sessions.delete(sessionId);
|
|
882
1263
|
await this.disconnectTrustedSession(sessionId, req);
|
|
883
1264
|
this.sessionBinding.clearSession(sessionId);
|
|
@@ -904,6 +1285,13 @@ class AgentDeckMCPServer {
|
|
|
904
1285
|
}
|
|
905
1286
|
async handleMcpSessionRequest(req, res) {
|
|
906
1287
|
const sessionId = this.getSessionIdHeader(req);
|
|
1288
|
+
const bearerRequired = isBearerGrantRequired();
|
|
1289
|
+
// Hosted endpoints run the Bearer [REDACTED] before session existence
|
|
1290
|
+
// (same no-oracle order as POST): 401 before any 404 metadata.
|
|
1291
|
+
if (bearerRequired && !this.authenticateGrant(readBearerToken(req))) {
|
|
1292
|
+
this.sendGrantRequired(res);
|
|
1293
|
+
return;
|
|
1294
|
+
}
|
|
907
1295
|
if (!sessionId) {
|
|
908
1296
|
res.status(400).send('Invalid or missing session ID');
|
|
909
1297
|
return;
|
|
@@ -913,8 +1301,19 @@ class AgentDeckMCPServer {
|
|
|
913
1301
|
this.sendSessionNotFound(sessionId, res);
|
|
914
1302
|
return;
|
|
915
1303
|
}
|
|
916
|
-
if (
|
|
917
|
-
|
|
1304
|
+
if (this.sessionBinding.isGrantSession(sessionId)) {
|
|
1305
|
+
if (!(await this.requireGrantFollowUp(sessionId, req, res))) {
|
|
1306
|
+
return;
|
|
1307
|
+
}
|
|
1308
|
+
}
|
|
1309
|
+
else {
|
|
1310
|
+
if (bearerRequired) {
|
|
1311
|
+
this.sendGrantRequired(res);
|
|
1312
|
+
return;
|
|
1313
|
+
}
|
|
1314
|
+
if (!(await this.requireFollowUpDeckHeader(sessionId, req, res))) {
|
|
1315
|
+
return;
|
|
1316
|
+
}
|
|
918
1317
|
}
|
|
919
1318
|
this.touchLiveDisplay(sessionId);
|
|
920
1319
|
await session.transport.handleRequest(req, res);
|
|
@@ -956,6 +1355,7 @@ class AgentDeckMCPServer {
|
|
|
956
1355
|
httpServer.once('error', reject);
|
|
957
1356
|
this.httpServer = httpServer;
|
|
958
1357
|
});
|
|
1358
|
+
this.startLiveDisplayKeepAlive();
|
|
959
1359
|
return this.app;
|
|
960
1360
|
}
|
|
961
1361
|
catch (error) {
|
|
@@ -966,6 +1366,7 @@ class AgentDeckMCPServer {
|
|
|
966
1366
|
}
|
|
967
1367
|
async stop() {
|
|
968
1368
|
try {
|
|
1369
|
+
this.stopLiveDisplayKeepAlive();
|
|
969
1370
|
for (const [sessionId, session] of this.sessions) {
|
|
970
1371
|
try {
|
|
971
1372
|
await session.transport.close();
|