@harness-fe/mcp-server 3.4.1 → 4.0.0-next.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/dist/bridge.d.ts +26 -5
- package/dist/bridge.js +43 -5
- package/dist/daemon.d.ts +7 -0
- package/dist/daemon.js +1 -0
- package/dist/identity.d.ts +90 -0
- package/dist/identity.js +123 -0
- package/dist/mcp.d.ts +7 -0
- package/dist/mcp.js +91 -86
- package/dist/mcpHttp.js +7 -1
- package/dist/sessionRouter.d.ts +3 -0
- package/dist/store/JsonlStore.js +4 -0
- package/dist/store/types.d.ts +11 -0
- package/package.json +2 -2
- package/src/bridge.ts +57 -7
- package/src/daemon.ts +9 -0
- package/src/identity.test.ts +109 -0
- package/src/identity.ts +137 -0
- package/src/mcp.ts +114 -102
- package/src/mcpHttp.ts +7 -1
- package/src/sessionRouter.ts +3 -0
- package/src/store/JsonlStore.ts +4 -0
- package/src/store/identityTagging.test.ts +67 -0
- package/src/store/types.ts +11 -0
package/dist/bridge.d.ts
CHANGED
|
@@ -12,7 +12,8 @@
|
|
|
12
12
|
*/
|
|
13
13
|
import { type IncomingMessage, type ServerResponse } from 'node:http';
|
|
14
14
|
import { type AuthOptions } from './auth.js';
|
|
15
|
-
import { type
|
|
15
|
+
import { type Principal } from './identity.js';
|
|
16
|
+
import { type ConsentPolicy, type EventFrame, type HttpBatch, type TabInfo, type Task, type TaskStatus } from '@harness-fe/protocol';
|
|
16
17
|
import { SessionRouter, type PeerSession } from './sessionRouter.js';
|
|
17
18
|
import { type IStore, type ITaskStore, type IMemoryStore, type RetentionPolicy } from './store/index.js';
|
|
18
19
|
/**
|
|
@@ -29,8 +30,8 @@ export interface IBridge {
|
|
|
29
30
|
status?: TaskStatus | 'all';
|
|
30
31
|
limit?: number;
|
|
31
32
|
}): Promise<Task[]>;
|
|
32
|
-
claimTask(id: string): Promise<Task | undefined>;
|
|
33
|
-
resolveTask(id: string, note?: string): Promise<Task | undefined>;
|
|
33
|
+
claimTask(id: string, principal?: Principal): Promise<Task | undefined>;
|
|
34
|
+
resolveTask(id: string, note?: string, principal?: Principal): Promise<Task | undefined>;
|
|
34
35
|
getMemoryStore(): IMemoryStore;
|
|
35
36
|
/**
|
|
36
37
|
* Base URL (e.g. http://127.0.0.1:47729) where the replay viewer is reachable.
|
|
@@ -75,6 +76,13 @@ export interface BridgeOptions {
|
|
|
75
76
|
* token disables auth (only valid when bound to a loopback host).
|
|
76
77
|
*/
|
|
77
78
|
auth?: AuthOptions;
|
|
79
|
+
/**
|
|
80
|
+
* Browser-consent policy for control commands (4.0 · P2). When omitted it
|
|
81
|
+
* defaults to `session` while auth is enabled (non-loopback / exposed) and
|
|
82
|
+
* `off` on loopback — so solo dev keeps its zero-friction flow and any
|
|
83
|
+
* exposed daemon prompts the user before an agent drives the page.
|
|
84
|
+
*/
|
|
85
|
+
consent?: ConsentPolicy;
|
|
78
86
|
/**
|
|
79
87
|
* Override the host used when building outbound URLs (dashboard links,
|
|
80
88
|
* replay viewer URLs). When omitted and `host` is `0.0.0.0` / `::`, the
|
|
@@ -157,6 +165,8 @@ export declare class Bridge implements IBridge {
|
|
|
157
165
|
private tasks;
|
|
158
166
|
private opts;
|
|
159
167
|
private auth;
|
|
168
|
+
/** Browser-consent policy pushed to runtime clients in hello.ack (4.0 · P2). */
|
|
169
|
+
private readonly consentPolicy;
|
|
160
170
|
private publicHostOverride;
|
|
161
171
|
private readonly attachDataDir;
|
|
162
172
|
private autoPurgeOpts;
|
|
@@ -167,6 +177,15 @@ export declare class Bridge implements IBridge {
|
|
|
167
177
|
* or sessionId (for runtime-client connections).
|
|
168
178
|
*/
|
|
169
179
|
private connToStoreId;
|
|
180
|
+
/** Caller identity per connection (4.0 · P1). Resolved at WS upgrade. */
|
|
181
|
+
private connToPrincipal;
|
|
182
|
+
/**
|
|
183
|
+
* Identity attributed to MCP-driven writes (task claim/resolve). The MCP
|
|
184
|
+
* tool layer is a daemon-wide singleton today with no per-call caller
|
|
185
|
+
* context, so stdio/HTTP agents collapse to one principal. P4 (per-session
|
|
186
|
+
* MCP transport) replaces this with the real per-call principal.
|
|
187
|
+
*/
|
|
188
|
+
private readonly defaultPrincipal;
|
|
170
189
|
/** Connections that already logged a "no store session" warning. */
|
|
171
190
|
private warnedNoSession;
|
|
172
191
|
/**
|
|
@@ -228,6 +247,8 @@ export declare class Bridge implements IBridge {
|
|
|
228
247
|
getBoundPort(): number | undefined;
|
|
229
248
|
getViewerBaseUrl(): string | undefined;
|
|
230
249
|
getAuthToken(): string | undefined;
|
|
250
|
+
/** Daemon auth options — used by the MCP layer to identify the per-call principal (4.0 · P4). */
|
|
251
|
+
getAuthOptions(): AuthOptions;
|
|
231
252
|
/**
|
|
232
253
|
* Broadcast a `dashboard.update` frame to every subscribed dashboard SPA.
|
|
233
254
|
*
|
|
@@ -264,9 +285,9 @@ export declare class Bridge implements IBridge {
|
|
|
264
285
|
status?: TaskStatus | 'all';
|
|
265
286
|
limit?: number;
|
|
266
287
|
}): Promise<Task[]>;
|
|
267
|
-
claimTask(id: string): Promise<Task | undefined>;
|
|
288
|
+
claimTask(id: string, principal?: Principal): Promise<Task | undefined>;
|
|
268
289
|
getTaskAttachmentData(taskId: string, attachmentId: string): Promise<string | null>;
|
|
269
|
-
resolveTask(id: string, note?: string): Promise<Task | undefined>;
|
|
290
|
+
resolveTask(id: string, note?: string, principal?: Principal): Promise<Task | undefined>;
|
|
270
291
|
private persistTaskEvent;
|
|
271
292
|
private recordTask;
|
|
272
293
|
/**
|
package/dist/bridge.js
CHANGED
|
@@ -14,7 +14,8 @@ import { WebSocket, WebSocketServer } from 'ws';
|
|
|
14
14
|
import { randomUUID } from 'node:crypto';
|
|
15
15
|
import { createServer } from 'node:http';
|
|
16
16
|
import { networkInterfaces } from 'node:os';
|
|
17
|
-
import { DEFAULT_LOGIN_PATH, handleLoginPost, isAuthorized, sendUnauthorized, } from './auth.js';
|
|
17
|
+
import { DEFAULT_LOGIN_PATH, handleLoginPost, isAuthEnabled, isAuthorized, sendUnauthorized, } from './auth.js';
|
|
18
|
+
import { LOCAL_PRINCIPAL, resolvePrincipal } from './identity.js';
|
|
18
19
|
import { join as joinPath } from 'node:path';
|
|
19
20
|
import { homedir } from 'node:os';
|
|
20
21
|
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
@@ -59,6 +60,8 @@ export class Bridge {
|
|
|
59
60
|
tasks = new Map();
|
|
60
61
|
opts;
|
|
61
62
|
auth;
|
|
63
|
+
/** Browser-consent policy pushed to runtime clients in hello.ack (4.0 · P2). */
|
|
64
|
+
consentPolicy;
|
|
62
65
|
publicHostOverride;
|
|
63
66
|
attachDataDir;
|
|
64
67
|
autoPurgeOpts;
|
|
@@ -69,6 +72,15 @@ export class Bridge {
|
|
|
69
72
|
* or sessionId (for runtime-client connections).
|
|
70
73
|
*/
|
|
71
74
|
connToStoreId = new Map();
|
|
75
|
+
/** Caller identity per connection (4.0 · P1). Resolved at WS upgrade. */
|
|
76
|
+
connToPrincipal = new Map();
|
|
77
|
+
/**
|
|
78
|
+
* Identity attributed to MCP-driven writes (task claim/resolve). The MCP
|
|
79
|
+
* tool layer is a daemon-wide singleton today with no per-call caller
|
|
80
|
+
* context, so stdio/HTTP agents collapse to one principal. P4 (per-session
|
|
81
|
+
* MCP transport) replaces this with the real per-call principal.
|
|
82
|
+
*/
|
|
83
|
+
defaultPrincipal = LOCAL_PRINCIPAL;
|
|
72
84
|
/** Connections that already logged a "no store session" warning. */
|
|
73
85
|
warnedNoSession = new Set();
|
|
74
86
|
/**
|
|
@@ -107,6 +119,12 @@ export class Bridge {
|
|
|
107
119
|
host: opts.host ?? '127.0.0.1',
|
|
108
120
|
};
|
|
109
121
|
this.auth = opts.auth ?? {};
|
|
122
|
+
// Consent defaults track the auth boundary: exposed (auth on) ⇒ prompt
|
|
123
|
+
// once per session; loopback solo (auth off) ⇒ no prompts. Explicit
|
|
124
|
+
// opts.consent always wins.
|
|
125
|
+
this.consentPolicy = opts.consent ?? {
|
|
126
|
+
mode: isAuthEnabled(this.auth) ? 'session' : 'off',
|
|
127
|
+
};
|
|
110
128
|
this.publicHostOverride = opts.publicHost;
|
|
111
129
|
// Default auto-purge ON. CI / tests pass `enabled: false` (or set
|
|
112
130
|
// env HARNESS_FE_PURGE_DISABLED=1) to opt out.
|
|
@@ -272,7 +290,7 @@ export class Bridge {
|
|
|
272
290
|
res.end('Not Found');
|
|
273
291
|
});
|
|
274
292
|
const wss = new WebSocketServer({ noServer: true });
|
|
275
|
-
wss.on('connection', (ws) => this.onConnection(ws));
|
|
293
|
+
wss.on('connection', (ws, req) => this.onConnection(ws, req));
|
|
276
294
|
httpServer.on('upgrade', (req, socket, head) => {
|
|
277
295
|
if (!isAuthorized(req, this.auth)) {
|
|
278
296
|
// Spec-compliant 401 on the upgrade reply so client sees a
|
|
@@ -379,6 +397,10 @@ export class Bridge {
|
|
|
379
397
|
getAuthToken() {
|
|
380
398
|
return this.auth.token;
|
|
381
399
|
}
|
|
400
|
+
/** Daemon auth options — used by the MCP layer to identify the per-call principal (4.0 · P4). */
|
|
401
|
+
getAuthOptions() {
|
|
402
|
+
return this.auth;
|
|
403
|
+
}
|
|
382
404
|
/**
|
|
383
405
|
* Broadcast a `dashboard.update` frame to every subscribed dashboard SPA.
|
|
384
406
|
*
|
|
@@ -539,12 +561,16 @@ export class Bridge {
|
|
|
539
561
|
filtered.sort((a, b) => b.createdAt - a.createdAt);
|
|
540
562
|
return filtered.slice(0, limit);
|
|
541
563
|
}
|
|
542
|
-
async claimTask(id) {
|
|
564
|
+
async claimTask(id, principal) {
|
|
543
565
|
const task = this.tasks.get(id);
|
|
544
566
|
if (!task)
|
|
545
567
|
return undefined;
|
|
546
568
|
task.status = 'claimed';
|
|
547
569
|
task.claimedAt = Date.now();
|
|
570
|
+
// Tag which agent picked it up (4.0 · P1/P4). The per-call principal
|
|
571
|
+
// (HTTP MCP, resolved from request headers) wins; stdio / no-caller
|
|
572
|
+
// falls back to the daemon's local principal.
|
|
573
|
+
task.agentId = (principal ?? this.defaultPrincipal).id;
|
|
548
574
|
this.persistTasks();
|
|
549
575
|
// Persist status change to store
|
|
550
576
|
this.persistTaskEvent(task, 'task:claim');
|
|
@@ -556,7 +582,7 @@ export class Bridge {
|
|
|
556
582
|
return null;
|
|
557
583
|
return this.readTaskAttachment(task.projectId, taskId, attachmentId);
|
|
558
584
|
}
|
|
559
|
-
async resolveTask(id, note) {
|
|
585
|
+
async resolveTask(id, note, principal) {
|
|
560
586
|
const task = this.tasks.get(id);
|
|
561
587
|
if (!task)
|
|
562
588
|
return undefined;
|
|
@@ -564,6 +590,8 @@ export class Bridge {
|
|
|
564
590
|
task.resolvedAt = Date.now();
|
|
565
591
|
if (note !== undefined)
|
|
566
592
|
task.note = note;
|
|
593
|
+
if (!task.agentId)
|
|
594
|
+
task.agentId = (principal ?? this.defaultPrincipal).id;
|
|
567
595
|
this.persistTasks();
|
|
568
596
|
// Persist status change to store
|
|
569
597
|
this.persistTaskEvent(task, 'task:resolve');
|
|
@@ -818,9 +846,13 @@ export class Bridge {
|
|
|
818
846
|
}
|
|
819
847
|
return false;
|
|
820
848
|
}
|
|
821
|
-
onConnection(ws) {
|
|
849
|
+
onConnection(ws, req) {
|
|
822
850
|
const connectionId = randomUUID();
|
|
823
851
|
this.sockets.set(connectionId, ws);
|
|
852
|
+
// Resolve caller identity once at connection time (4.0 · P1). The
|
|
853
|
+
// upgrade handler already enforced isAuthorized, so resolvePrincipal
|
|
854
|
+
// won't reject here; fall back to LOCAL for the loopback / no-req path.
|
|
855
|
+
this.connToPrincipal.set(connectionId, (req && resolvePrincipal(req, this.auth)) || LOCAL_PRINCIPAL);
|
|
824
856
|
ws.on('message', (raw) => {
|
|
825
857
|
let parsed;
|
|
826
858
|
try {
|
|
@@ -880,6 +912,7 @@ export class Bridge {
|
|
|
880
912
|
}
|
|
881
913
|
}
|
|
882
914
|
this.router.unregister(connectionId);
|
|
915
|
+
this.connToPrincipal.delete(connectionId);
|
|
883
916
|
});
|
|
884
917
|
ws.on('error', () => {
|
|
885
918
|
/* swallow; close will follow */
|
|
@@ -924,6 +957,7 @@ export class Bridge {
|
|
|
924
957
|
// production / staging deployment where the bundler plugin is
|
|
925
958
|
// absent. The runtime-client branch below opens its own store
|
|
926
959
|
// session if one does not already exist for this project.
|
|
960
|
+
const principal = this.connToPrincipal.get(connectionId) ?? LOCAL_PRINCIPAL;
|
|
927
961
|
const session = this.router.register({
|
|
928
962
|
role: frame.role,
|
|
929
963
|
projectId: frame.projectId,
|
|
@@ -933,6 +967,7 @@ export class Bridge {
|
|
|
933
967
|
userId: frame.userId,
|
|
934
968
|
connectionId,
|
|
935
969
|
page: frame.page,
|
|
970
|
+
principal,
|
|
936
971
|
});
|
|
937
972
|
// Persist to store
|
|
938
973
|
if (this.store) {
|
|
@@ -944,6 +979,7 @@ export class Bridge {
|
|
|
944
979
|
this.store.upsertProject(frame.projectId, {
|
|
945
980
|
parentProjectId: frame.parentProjectId,
|
|
946
981
|
displayName: frame.displayName,
|
|
982
|
+
createdBy: principal.id,
|
|
947
983
|
});
|
|
948
984
|
}
|
|
949
985
|
catch (err) {
|
|
@@ -1018,6 +1054,7 @@ export class Bridge {
|
|
|
1018
1054
|
referrer: undefined,
|
|
1019
1055
|
userAgent: frame.page?.userAgent,
|
|
1020
1056
|
participants,
|
|
1057
|
+
createdBy: principal.id,
|
|
1021
1058
|
});
|
|
1022
1059
|
this.connToStoreId.set(connectionId, sessionId);
|
|
1023
1060
|
this.notifyDashboard({
|
|
@@ -1060,6 +1097,7 @@ export class Bridge {
|
|
|
1060
1097
|
id: frame.id,
|
|
1061
1098
|
tabId: session.tabId,
|
|
1062
1099
|
serverVersion: PROTOCOL_VERSION,
|
|
1100
|
+
consent: this.consentPolicy,
|
|
1063
1101
|
};
|
|
1064
1102
|
ws.send(JSON.stringify(ack));
|
|
1065
1103
|
// One concise line per accepted peer. Visibility for "is the
|
package/dist/daemon.d.ts
CHANGED
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
* not pushed into the factory.
|
|
24
24
|
*/
|
|
25
25
|
import type { IncomingMessage } from 'node:http';
|
|
26
|
+
import type { ConsentPolicy } from '@harness-fe/protocol';
|
|
26
27
|
import { Bridge } from './bridge.js';
|
|
27
28
|
import type { EventStore, IStore } from './store/types.js';
|
|
28
29
|
import type { ITaskStore, IMemoryStore } from './store/types.js';
|
|
@@ -56,6 +57,12 @@ export interface DaemonOptions {
|
|
|
56
57
|
* flows through here. Mutually exclusive with `authorize`.
|
|
57
58
|
*/
|
|
58
59
|
token?: string;
|
|
60
|
+
/**
|
|
61
|
+
* Browser-consent policy for control commands (4.0 · P2). Omit to track the
|
|
62
|
+
* auth boundary automatically: `session` when auth is on (exposed daemon),
|
|
63
|
+
* `off` on loopback solo dev. Pass `{ mode }` to force a policy.
|
|
64
|
+
*/
|
|
65
|
+
consent?: ConsentPolicy;
|
|
59
66
|
/**
|
|
60
67
|
* IStore implementation. Omit for the default JSONL store at `dataDir`.
|
|
61
68
|
* Pass `null` to disable session/event persistence entirely.
|
package/dist/daemon.js
CHANGED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Caller identity (4.0 · P1) — turns the auth boundary from a plain
|
|
3
|
+
* allow/deny into "allow/deny + *who*".
|
|
4
|
+
*
|
|
5
|
+
* Phase 1 scope: this module only *establishes* and *carries* a Principal,
|
|
6
|
+
* and the bridge *tags* writes with it (createdBy). It deliberately does NOT
|
|
7
|
+
* filter reads by owner — that's P3 (tenant isolation). Keeping the two apart
|
|
8
|
+
* means identity plumbing lands with zero behaviour change: loopback solo dev
|
|
9
|
+
* stays a single implicit `local` principal, and an authorized caller sees
|
|
10
|
+
* everything exactly as before.
|
|
11
|
+
*
|
|
12
|
+
* Why a separate module from auth.ts: `isAuthorized` answers a boolean and is
|
|
13
|
+
* consumed on the hot path of every HTTP route / WS upgrade. `resolvePrincipal`
|
|
14
|
+
* is the richer, additive view layered on top — it reuses the same primitives
|
|
15
|
+
* (isAuthEnabled / extractToken / verifyToken) so the two can never disagree on
|
|
16
|
+
* who is allowed in.
|
|
17
|
+
*/
|
|
18
|
+
import type { IncomingMessage } from 'node:http';
|
|
19
|
+
import { type AuthOptions } from './auth.js';
|
|
20
|
+
export type PrincipalKind = 'local' | 'token' | 'host';
|
|
21
|
+
export interface Principal {
|
|
22
|
+
/** Stable id for this caller. Loopback / stdio solo dev → `local`. */
|
|
23
|
+
id: string;
|
|
24
|
+
/** How the identity was established. */
|
|
25
|
+
kind: PrincipalKind;
|
|
26
|
+
/** Optional human-readable label (for dashboards / audit). */
|
|
27
|
+
displayName?: string;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The implicit single principal for loopback and stdio solo dev. The daemon
|
|
31
|
+
* trusts everything that can reach the loopback socket, so there is one
|
|
32
|
+
* caller and it owns everything — exactly today's behaviour, now named.
|
|
33
|
+
*/
|
|
34
|
+
export declare const LOCAL_PRINCIPAL: Principal;
|
|
35
|
+
/**
|
|
36
|
+
* Principal for the custom-`authorize` path. Hosts that embed the daemon own
|
|
37
|
+
* their own user model; until `authorize` can return a richer identity
|
|
38
|
+
* (future work), an authorized host caller maps to this single principal.
|
|
39
|
+
*/
|
|
40
|
+
export declare const HOST_PRINCIPAL: Principal;
|
|
41
|
+
/**
|
|
42
|
+
* Derive a stable principal id from a bearer token. One token = one principal
|
|
43
|
+
* in 4.0's trusted-team model. We hash so the raw secret never becomes an id
|
|
44
|
+
* that could leak into stored `createdBy` tags or audit logs.
|
|
45
|
+
*/
|
|
46
|
+
export declare function tokenPrincipalId(token: string): string;
|
|
47
|
+
/**
|
|
48
|
+
* Resolve the caller behind a request.
|
|
49
|
+
*
|
|
50
|
+
* Returns `null` when auth is enabled and the request is NOT authorized — this
|
|
51
|
+
* mirrors `isAuthorized(req) === false` exactly, so callers can treat a null
|
|
52
|
+
* principal as "reject" without a second auth check.
|
|
53
|
+
*
|
|
54
|
+
* - auth disabled (loopback): {@link LOCAL_PRINCIPAL}
|
|
55
|
+
* - custom `authorize`: {@link HOST_PRINCIPAL} when it accepts, else `null`
|
|
56
|
+
* - token: a {@link tokenPrincipalId}-derived principal when it matches, else `null`
|
|
57
|
+
*/
|
|
58
|
+
export declare function resolvePrincipal(req: IncomingMessage, opts: AuthOptions): Principal | null;
|
|
59
|
+
type HeaderBag = Record<string, string | string[] | undefined>;
|
|
60
|
+
/**
|
|
61
|
+
* Identify (not authorize) the caller behind an MCP tool call (4.0 · P4).
|
|
62
|
+
*
|
|
63
|
+
* The HTTP MCP request has already cleared the bridge's auth wrapper by the
|
|
64
|
+
* time a tool runs, so this only needs to *name* the caller — never to
|
|
65
|
+
* re-check them. Pass the per-request headers from the MCP SDK's
|
|
66
|
+
* `extra.requestInfo`; stdio calls have no requestInfo and resolve to `local`
|
|
67
|
+
* (the daemon trusts its local stdio agent).
|
|
68
|
+
*
|
|
69
|
+
* - auth disabled, or no headers (stdio) → {@link LOCAL_PRINCIPAL}
|
|
70
|
+
* - custom authorize → {@link HOST_PRINCIPAL}
|
|
71
|
+
* - token mode → token principal from the Authorization header (LOCAL if absent)
|
|
72
|
+
*/
|
|
73
|
+
export declare function identifyPrincipal(headers: HeaderBag | undefined, opts: AuthOptions): Principal;
|
|
74
|
+
/**
|
|
75
|
+
* Tenant-isolation visibility check (4.0 · P3). Decides whether `principal`
|
|
76
|
+
* may see a record tagged with `createdBy`.
|
|
77
|
+
*
|
|
78
|
+
* - `local` (loopback / stdio solo / no-auth) → sees everything. This keeps
|
|
79
|
+
* solo dev's behaviour completely unchanged.
|
|
80
|
+
* - unowned data (`createdBy` null/undefined — legacy rows from before P1, or
|
|
81
|
+
* records the daemon never tagged) → visible to everyone (backward compat).
|
|
82
|
+
* - otherwise → visible only to the principal that created it.
|
|
83
|
+
*
|
|
84
|
+
* Note: in the current single-token / loopback reality the data creator
|
|
85
|
+
* (plugin / runtime client) and the querying agent share one principal, so
|
|
86
|
+
* this is exact. A full `project → agent` binding (creator ≠ consumer, once
|
|
87
|
+
* P6 splits write/read scopes) is deferred to P6.
|
|
88
|
+
*/
|
|
89
|
+
export declare function canSee(principal: Principal, createdBy: string | null | undefined): boolean;
|
|
90
|
+
export {};
|
package/dist/identity.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Caller identity (4.0 · P1) — turns the auth boundary from a plain
|
|
3
|
+
* allow/deny into "allow/deny + *who*".
|
|
4
|
+
*
|
|
5
|
+
* Phase 1 scope: this module only *establishes* and *carries* a Principal,
|
|
6
|
+
* and the bridge *tags* writes with it (createdBy). It deliberately does NOT
|
|
7
|
+
* filter reads by owner — that's P3 (tenant isolation). Keeping the two apart
|
|
8
|
+
* means identity plumbing lands with zero behaviour change: loopback solo dev
|
|
9
|
+
* stays a single implicit `local` principal, and an authorized caller sees
|
|
10
|
+
* everything exactly as before.
|
|
11
|
+
*
|
|
12
|
+
* Why a separate module from auth.ts: `isAuthorized` answers a boolean and is
|
|
13
|
+
* consumed on the hot path of every HTTP route / WS upgrade. `resolvePrincipal`
|
|
14
|
+
* is the richer, additive view layered on top — it reuses the same primitives
|
|
15
|
+
* (isAuthEnabled / extractToken / verifyToken) so the two can never disagree on
|
|
16
|
+
* who is allowed in.
|
|
17
|
+
*/
|
|
18
|
+
import { createHash } from 'node:crypto';
|
|
19
|
+
import { extractToken, isAuthEnabled, verifyToken } from './auth.js';
|
|
20
|
+
/**
|
|
21
|
+
* The implicit single principal for loopback and stdio solo dev. The daemon
|
|
22
|
+
* trusts everything that can reach the loopback socket, so there is one
|
|
23
|
+
* caller and it owns everything — exactly today's behaviour, now named.
|
|
24
|
+
*/
|
|
25
|
+
export const LOCAL_PRINCIPAL = Object.freeze({
|
|
26
|
+
id: 'local',
|
|
27
|
+
kind: 'local',
|
|
28
|
+
displayName: 'local',
|
|
29
|
+
});
|
|
30
|
+
/**
|
|
31
|
+
* Principal for the custom-`authorize` path. Hosts that embed the daemon own
|
|
32
|
+
* their own user model; until `authorize` can return a richer identity
|
|
33
|
+
* (future work), an authorized host caller maps to this single principal.
|
|
34
|
+
*/
|
|
35
|
+
export const HOST_PRINCIPAL = Object.freeze({
|
|
36
|
+
id: 'host',
|
|
37
|
+
kind: 'host',
|
|
38
|
+
displayName: 'host',
|
|
39
|
+
});
|
|
40
|
+
/**
|
|
41
|
+
* Derive a stable principal id from a bearer token. One token = one principal
|
|
42
|
+
* in 4.0's trusted-team model. We hash so the raw secret never becomes an id
|
|
43
|
+
* that could leak into stored `createdBy` tags or audit logs.
|
|
44
|
+
*/
|
|
45
|
+
export function tokenPrincipalId(token) {
|
|
46
|
+
return `token:${createHash('sha256').update(token).digest('hex').slice(0, 12)}`;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Resolve the caller behind a request.
|
|
50
|
+
*
|
|
51
|
+
* Returns `null` when auth is enabled and the request is NOT authorized — this
|
|
52
|
+
* mirrors `isAuthorized(req) === false` exactly, so callers can treat a null
|
|
53
|
+
* principal as "reject" without a second auth check.
|
|
54
|
+
*
|
|
55
|
+
* - auth disabled (loopback): {@link LOCAL_PRINCIPAL}
|
|
56
|
+
* - custom `authorize`: {@link HOST_PRINCIPAL} when it accepts, else `null`
|
|
57
|
+
* - token: a {@link tokenPrincipalId}-derived principal when it matches, else `null`
|
|
58
|
+
*/
|
|
59
|
+
export function resolvePrincipal(req, opts) {
|
|
60
|
+
if (!isAuthEnabled(opts))
|
|
61
|
+
return LOCAL_PRINCIPAL;
|
|
62
|
+
if (opts.authorize)
|
|
63
|
+
return opts.authorize(req) ? HOST_PRINCIPAL : null;
|
|
64
|
+
const token = extractToken(req, opts);
|
|
65
|
+
if (!verifyToken(token, opts.token))
|
|
66
|
+
return null;
|
|
67
|
+
return { id: tokenPrincipalId(token), kind: 'token' };
|
|
68
|
+
}
|
|
69
|
+
function bearerFromHeaders(headers) {
|
|
70
|
+
const raw = headers['authorization'] ?? headers['Authorization'];
|
|
71
|
+
const v = Array.isArray(raw) ? raw[0] : raw;
|
|
72
|
+
if (typeof v === 'string' && v.startsWith('Bearer ')) {
|
|
73
|
+
const t = v.slice(7).trim();
|
|
74
|
+
if (t)
|
|
75
|
+
return t;
|
|
76
|
+
}
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Identify (not authorize) the caller behind an MCP tool call (4.0 · P4).
|
|
81
|
+
*
|
|
82
|
+
* The HTTP MCP request has already cleared the bridge's auth wrapper by the
|
|
83
|
+
* time a tool runs, so this only needs to *name* the caller — never to
|
|
84
|
+
* re-check them. Pass the per-request headers from the MCP SDK's
|
|
85
|
+
* `extra.requestInfo`; stdio calls have no requestInfo and resolve to `local`
|
|
86
|
+
* (the daemon trusts its local stdio agent).
|
|
87
|
+
*
|
|
88
|
+
* - auth disabled, or no headers (stdio) → {@link LOCAL_PRINCIPAL}
|
|
89
|
+
* - custom authorize → {@link HOST_PRINCIPAL}
|
|
90
|
+
* - token mode → token principal from the Authorization header (LOCAL if absent)
|
|
91
|
+
*/
|
|
92
|
+
export function identifyPrincipal(headers, opts) {
|
|
93
|
+
if (!isAuthEnabled(opts))
|
|
94
|
+
return LOCAL_PRINCIPAL;
|
|
95
|
+
if (opts.authorize)
|
|
96
|
+
return HOST_PRINCIPAL;
|
|
97
|
+
if (!headers)
|
|
98
|
+
return LOCAL_PRINCIPAL;
|
|
99
|
+
const token = bearerFromHeaders(headers);
|
|
100
|
+
return token ? { id: tokenPrincipalId(token), kind: 'token' } : LOCAL_PRINCIPAL;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Tenant-isolation visibility check (4.0 · P3). Decides whether `principal`
|
|
104
|
+
* may see a record tagged with `createdBy`.
|
|
105
|
+
*
|
|
106
|
+
* - `local` (loopback / stdio solo / no-auth) → sees everything. This keeps
|
|
107
|
+
* solo dev's behaviour completely unchanged.
|
|
108
|
+
* - unowned data (`createdBy` null/undefined — legacy rows from before P1, or
|
|
109
|
+
* records the daemon never tagged) → visible to everyone (backward compat).
|
|
110
|
+
* - otherwise → visible only to the principal that created it.
|
|
111
|
+
*
|
|
112
|
+
* Note: in the current single-token / loopback reality the data creator
|
|
113
|
+
* (plugin / runtime client) and the querying agent share one principal, so
|
|
114
|
+
* this is exact. A full `project → agent` binding (creator ≠ consumer, once
|
|
115
|
+
* P6 splits write/read scopes) is deferred to P6.
|
|
116
|
+
*/
|
|
117
|
+
export function canSee(principal, createdBy) {
|
|
118
|
+
if (principal.kind === 'local')
|
|
119
|
+
return true;
|
|
120
|
+
if (createdBy == null)
|
|
121
|
+
return true;
|
|
122
|
+
return createdBy === principal.id;
|
|
123
|
+
}
|
package/dist/mcp.d.ts
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
9
9
|
import type { IBridge } from './bridge.js';
|
|
10
|
+
import type { AuthOptions } from './auth.js';
|
|
10
11
|
export interface McpServerOptions {
|
|
11
12
|
/**
|
|
12
13
|
* Name of the environment variable that gates experimental tools.
|
|
@@ -17,6 +18,12 @@ export interface McpServerOptions {
|
|
|
17
18
|
* var is set to a non-empty value at server-construction time.
|
|
18
19
|
*/
|
|
19
20
|
experimentalEnvVar?: string;
|
|
21
|
+
/**
|
|
22
|
+
* Daemon auth options, used to identify the per-call principal from MCP
|
|
23
|
+
* request headers (4.0 · P4). Omit for stdio / no-auth — calls resolve to
|
|
24
|
+
* the local principal.
|
|
25
|
+
*/
|
|
26
|
+
auth?: AuthOptions;
|
|
20
27
|
}
|
|
21
28
|
/**
|
|
22
29
|
* Experimental-feature gate.
|