@stage-labs/metro 0.1.0-beta.304 → 0.1.0-beta.305
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/package.json +1 -1
- package/runtime/marketplace/plugin/.claude-plugin/plugin.json +3 -4
- package/runtime/marketplace/plugin/METRO.md +1 -1
- package/runtime/marketplace/plugin/bin/metro-plugin.mjs +4 -28
- package/runtime/marketplace/plugin/bin/policy.mjs +1 -32
- package/runtime/node_modules/@metro-labs/core/package.json +0 -1
- package/runtime/node_modules/@metro-labs/daemon/src/agents/api.ts +3 -5
- package/runtime/node_modules/@metro-labs/daemon/src/approvals/pending.ts +1 -7
- package/runtime/node_modules/@metro-labs/daemon/src/boot/boot.ts +5 -9
- package/runtime/node_modules/@metro-labs/daemon/src/claude/session.ts +1 -3
- package/runtime/node_modules/@metro-labs/daemon/src/claude/setup.ts +1 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/enroll-api.ts +0 -2
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/plugin-sync.ts +9 -54
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/permission-prompt.ts +1 -13
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/policy-snapshot.ts +0 -14
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/server-approvals.ts +14 -15
- package/runtime/node_modules/@metro-labs/daemon/src/mcp-server/announced.ts +68 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp-server/config.ts +3 -1
- package/runtime/node_modules/@metro-labs/daemon/src/policy/policy.ts +6 -5
- package/runtime/node_modules/@metro-labs/daemon/src/routes/http.ts +0 -2
- package/runtime/node_modules/@metro-labs/daemon/src/routes/local-mode.ts +2 -27
- package/runtime/node_modules/@metro-labs/daemon/src/routes/session-apis.ts +0 -5
- package/runtime/node_modules/@metro-labs/http/package.json +1 -2
- package/runtime/node_modules/@metro-labs/http/src/connector-grant.ts +0 -51
- package/runtime/runtime.json +1 -1
- package/runtime/sdk-runner/node_modules/@metro-labs/core/package.json +0 -1
- package/runtime/sdk-runner/runner.json +1 -1
- package/runtime/node_modules/@metro-labs/core/src/connector-sync.ts +0 -17
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/gates.ts +0 -104
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-adopt.ts +0 -57
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-relay.ts +0 -61
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-runtime-discovery.ts +0 -115
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-runtime.ts +0 -271
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-sync-api.ts +0 -25
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-sync-control.ts +0 -173
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-sync-setting.ts +0 -60
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-sync-watch.ts +0 -38
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-sync.ts +0 -208
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/publication.ts +0 -40
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-id.ts +0 -2
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-policy.ts +0 -90
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-session.ts +0 -10
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-target.ts +0 -4
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay.ts +0 -287
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/server-keys.ts +0 -59
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/tools.ts +0 -1
- package/runtime/node_modules/@metro-labs/http/src/connector-delivery-validation.ts +0 -116
- package/runtime/node_modules/@metro-labs/http/src/connector-delivery.ts +0 -19
- package/runtime/node_modules/@metro-labs/http/src/connector-migration.ts +0 -178
- package/runtime/node_modules/@metro-labs/http/src/transfer-crypto.ts +0 -44
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/connector-sync.ts +0 -17
|
@@ -1,208 +0,0 @@
|
|
|
1
|
-
import { randomBytes } from 'node:crypto';
|
|
2
|
-
import { log } from '@metro-labs/core/log';
|
|
3
|
-
import type { ConnectorSyncReceipt } from '@metro-labs/core/connector-sync';
|
|
4
|
-
import { ApiError } from '@metro-labs/http/api-error';
|
|
5
|
-
import { AGENT_CLOCK_SKEW_MS } from '@metro-labs/http/agent-signature';
|
|
6
|
-
import { CONNECTOR_DELIVERY_LEASE_MS, CONNECTOR_DELIVERY_MAX_BYTES, openConnectorDelivery, type DeliveryContext, type DeliverySnapshot } from '@metro-labs/http/connector-delivery';
|
|
7
|
-
import type { AgentKey } from './agent-key.js';
|
|
8
|
-
import { agentCall, MetroApiRefusal, type MetroApi } from './metro-api.js';
|
|
9
|
-
|
|
10
|
-
interface Identity {
|
|
11
|
-
key: AgentKey;
|
|
12
|
-
context: Omit<DeliveryContext, 'requestId'>;
|
|
13
|
-
token: string;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface OrgSyncDeps {
|
|
17
|
-
enabled: () => boolean;
|
|
18
|
-
owner: () => string | null;
|
|
19
|
-
key: () => AgentKey | null;
|
|
20
|
-
api: MetroApi;
|
|
21
|
-
monotonic?: () => number;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
interface RefreshRequest {
|
|
25
|
-
connectorId: string;
|
|
26
|
-
revision: string;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const invalid = (): ApiError => new ApiError('Metro received an invalid or expired organization connector snapshot.', 503);
|
|
30
|
-
|
|
31
|
-
function lease(snapshot: DeliverySnapshot, started: number, elapsed: number, now: number): number {
|
|
32
|
-
const duration = snapshot.expiresAt - snapshot.issuedAt;
|
|
33
|
-
if (duration <= 0 || duration > CONNECTOR_DELIVERY_LEASE_MS || snapshot.issuedAt > now + AGENT_CLOCK_SKEW_MS || snapshot.issuedAt < started - AGENT_CLOCK_SKEW_MS) throw invalid();
|
|
34
|
-
const expiries = snapshot.connectors.flatMap((row) => row.state === 'ready' && row.expiresAt !== undefined ? [row.expiresAt] : []);
|
|
35
|
-
const remaining = Math.min(duration, ...expiries.map((at) => at - snapshot.issuedAt)) - elapsed;
|
|
36
|
-
if (remaining <= 0) throw invalid();
|
|
37
|
-
return remaining;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export class OrgConnectorSync {
|
|
41
|
-
private snapshot: DeliverySnapshot | null = null;
|
|
42
|
-
private token: string | null = null;
|
|
43
|
-
private controller: AbortController | null = null;
|
|
44
|
-
private receiptToken: string | null = null;
|
|
45
|
-
private receipt: ConnectorSyncReceipt = { state: 'waiting', lastReceivedAt: null, leaseExpiresAt: null, counts: null };
|
|
46
|
-
private deadline = 0;
|
|
47
|
-
private wallDeadline = 0;
|
|
48
|
-
private generation = 0;
|
|
49
|
-
private running: Promise<void> | null = null;
|
|
50
|
-
private recovery: Promise<void> | null = null;
|
|
51
|
-
private expiry: ReturnType<typeof setTimeout> | null = null;
|
|
52
|
-
private readonly monotonic: () => number;
|
|
53
|
-
private readonly listeners = new Set<() => void>();
|
|
54
|
-
|
|
55
|
-
constructor(private readonly deps: OrgSyncDeps) {
|
|
56
|
-
this.monotonic = deps.monotonic ?? (() => performance.now());
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
onChange(listener: () => void): () => void {
|
|
60
|
-
this.listeners.add(listener);
|
|
61
|
-
return () => { this.listeners.delete(listener); };
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
epoch(): number {
|
|
65
|
-
return this.generation;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
private notify(): void {
|
|
69
|
-
for (const listener of [...this.listeners]) {
|
|
70
|
-
try {
|
|
71
|
-
listener();
|
|
72
|
-
} catch {
|
|
73
|
-
log.warn('organization connectors: a memory change listener failed');
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
private identity(): Identity | null {
|
|
79
|
-
if (!this.deps.enabled()) return null;
|
|
80
|
-
const owner = this.deps.owner();
|
|
81
|
-
const key = this.deps.key();
|
|
82
|
-
const enrollment = key?.enrollment;
|
|
83
|
-
if (owner === null || key === null || enrollment === null || enrollment === undefined || enrollment.organization !== owner) return null;
|
|
84
|
-
const context = { owner, agent: enrollment.server, keyId: key.keyId };
|
|
85
|
-
return { key, context, token: JSON.stringify([context, key.sealingKey]) };
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
private forget(): void {
|
|
89
|
-
this.snapshot = null;
|
|
90
|
-
this.deadline = 0;
|
|
91
|
-
this.wallDeadline = 0;
|
|
92
|
-
if (this.expiry !== null) clearTimeout(this.expiry);
|
|
93
|
-
this.expiry = null;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
private refused = false;
|
|
97
|
-
private settled = false;
|
|
98
|
-
|
|
99
|
-
lapsed(): boolean {
|
|
100
|
-
return !this.refused && (!this.settled || this.receipt.state === 'expired' || this.receipt.state === 'unavailable');
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
invalidate(reason: 'waiting' | 'expired' | 'unavailable' = 'waiting'): void {
|
|
104
|
-
if (reason === 'waiting') this.settled = true;
|
|
105
|
-
this.generation += 1;
|
|
106
|
-
this.controller?.abort();
|
|
107
|
-
this.controller = null;
|
|
108
|
-
if (reason === 'waiting') {
|
|
109
|
-
this.receiptToken = null;
|
|
110
|
-
this.receipt = { state: reason, lastReceivedAt: null, leaseExpiresAt: null, counts: null };
|
|
111
|
-
} else this.receipt = { ...this.receipt, state: reason, counts: null };
|
|
112
|
-
this.token = null;
|
|
113
|
-
this.forget();
|
|
114
|
-
this.notify();
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
private validate(identity: Identity | null): void {
|
|
118
|
-
if (this.token === null) return;
|
|
119
|
-
if (identity === null || identity.token !== this.token) this.invalidate();
|
|
120
|
-
else if (this.snapshot !== null && (this.monotonic() >= this.deadline || this.deps.api.now() >= this.wallDeadline)) this.invalidate('expired');
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
read(): DeliverySnapshot | null {
|
|
124
|
-
if (this.token === null) return null;
|
|
125
|
-
try {
|
|
126
|
-
this.validate(this.identity());
|
|
127
|
-
} catch {
|
|
128
|
-
this.invalidate();
|
|
129
|
-
log.warn('organization connectors: memory could not be checked and was cleared');
|
|
130
|
-
}
|
|
131
|
-
return this.snapshot === null ? null : structuredClone(this.snapshot);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
status(): ConnectorSyncReceipt {
|
|
135
|
-
this.read();
|
|
136
|
-
try {
|
|
137
|
-
if (this.receiptToken !== null && this.identity()?.token !== this.receiptToken) this.invalidate();
|
|
138
|
-
} catch {
|
|
139
|
-
this.invalidate();
|
|
140
|
-
}
|
|
141
|
-
return structuredClone(this.receipt);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
async sync(refresh?: RefreshRequest): Promise<void> {
|
|
145
|
-
let identity: Identity | null;
|
|
146
|
-
try {
|
|
147
|
-
identity = this.identity();
|
|
148
|
-
this.validate(identity);
|
|
149
|
-
} catch (err) {
|
|
150
|
-
this.invalidate();
|
|
151
|
-
throw err;
|
|
152
|
-
}
|
|
153
|
-
if (identity === null) return;
|
|
154
|
-
if (this.running !== null) return refresh === undefined ? this.running : this.running.then(() => this.sync(refresh));
|
|
155
|
-
this.token = identity.token;
|
|
156
|
-
const generation = this.generation;
|
|
157
|
-
this.controller = new AbortController();
|
|
158
|
-
this.running = this.fetch(identity, refresh, generation, this.controller.signal).then(() => {
|
|
159
|
-
this.refused = false;
|
|
160
|
-
}, (err: unknown) => {
|
|
161
|
-
if (generation === this.generation) {
|
|
162
|
-
this.refused = err instanceof MetroApiRefusal && err.upstreamStatus < 500;
|
|
163
|
-
this.invalidate('unavailable');
|
|
164
|
-
}
|
|
165
|
-
throw err;
|
|
166
|
-
}).finally(() => { this.running = null; });
|
|
167
|
-
return this.running;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
recover(): Promise<void> {
|
|
171
|
-
if (this.read() !== null) return Promise.resolve();
|
|
172
|
-
this.recovery ??= this.syncFresh().finally(() => { this.recovery = null; });
|
|
173
|
-
return this.recovery;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
async syncFresh(refresh?: RefreshRequest): Promise<void> {
|
|
177
|
-
if (this.running !== null) {
|
|
178
|
-
await this.running.catch(() => {
|
|
179
|
-
log.debug('organization connectors: a previous sync failed before a fresh check');
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
return this.sync(refresh);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
private async fetch(identity: Identity, refresh: RefreshRequest | undefined, generation: number, signal: AbortSignal): Promise<void> {
|
|
186
|
-
const requestId = randomBytes(32).toString('base64url');
|
|
187
|
-
const context = { ...identity.context, requestId };
|
|
188
|
-
const started = this.deps.api.now();
|
|
189
|
-
const clock = this.monotonic();
|
|
190
|
-
const response = await agentCall(this.deps.api, identity.key, '/api/agents/connectors?policy_layers=1', { requestId, ...(refresh === undefined ? {} : { refresh }) }, { answerMax: CONNECTOR_DELIVERY_MAX_BYTES, timeoutMs: 60_000, signal });
|
|
191
|
-
this.validate(this.identity());
|
|
192
|
-
if (generation !== this.generation || this.token !== identity.token) throw invalid();
|
|
193
|
-
const snapshot = openConnectorDelivery(identity.key.sealing, context, response.sealed);
|
|
194
|
-
const remaining = lease(snapshot, started, this.monotonic() - clock, this.deps.api.now());
|
|
195
|
-
this.forget();
|
|
196
|
-
this.snapshot = snapshot;
|
|
197
|
-
this.deadline = this.monotonic() + remaining;
|
|
198
|
-
this.wallDeadline = this.deps.api.now() + remaining;
|
|
199
|
-
this.receiptToken = identity.token;
|
|
200
|
-
const total = snapshot.connectors.length;
|
|
201
|
-
const ready = snapshot.connectors.filter((row) => row.state === 'ready').length;
|
|
202
|
-
this.settled = true;
|
|
203
|
-
this.receipt = { state: 'received', lastReceivedAt: this.deps.api.now(), leaseExpiresAt: this.wallDeadline, counts: { total, ready, unavailable: total - ready } };
|
|
204
|
-
this.expiry = setTimeout(() => { this.invalidate('expired'); }, remaining);
|
|
205
|
-
this.expiry.unref();
|
|
206
|
-
this.notify();
|
|
207
|
-
}
|
|
208
|
-
}
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { registerConnectors, type GateRow } from './gates.js';
|
|
2
|
-
import { syncPluginServers } from './plugin-sync.js';
|
|
3
|
-
import { persistentServerKeys } from './server-keys.js';
|
|
4
|
-
|
|
5
|
-
let organization: GateRow[] | null = null;
|
|
6
|
-
const BOOT = Date.now();
|
|
7
|
-
const READY_WAIT_MS = 30_000;
|
|
8
|
-
|
|
9
|
-
export const connectorsReady = (now = Date.now()): boolean => organization !== null || now - BOOT >= READY_WAIT_MS;
|
|
10
|
-
|
|
11
|
-
function metadata(rows: readonly GateRow[]): GateRow[] {
|
|
12
|
-
return rows.map((row) => structuredClone({
|
|
13
|
-
id: row.id,
|
|
14
|
-
name: row.name,
|
|
15
|
-
config: {
|
|
16
|
-
...(row.config.policy === undefined ? {} : { policy: row.config.policy }),
|
|
17
|
-
...(row.config.toolGroups === undefined ? {} : { toolGroups: row.config.toolGroups }),
|
|
18
|
-
},
|
|
19
|
-
}));
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export function publishConnectors(): void {
|
|
23
|
-
if (organization === null) return;
|
|
24
|
-
let keys: Map<string, string>;
|
|
25
|
-
try {
|
|
26
|
-
keys = persistentServerKeys(organization);
|
|
27
|
-
}
|
|
28
|
-
catch (error) {
|
|
29
|
-
registerConnectors([]);
|
|
30
|
-
syncPluginServers([]);
|
|
31
|
-
throw error;
|
|
32
|
-
}
|
|
33
|
-
registerConnectors(organization, keys);
|
|
34
|
-
syncPluginServers(organization, { keys });
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export function publishOrgConnectors(rows: readonly GateRow[]): void {
|
|
38
|
-
organization = metadata(rows);
|
|
39
|
-
publishConnectors();
|
|
40
|
-
}
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import type { ServerResponse } from 'node:http';
|
|
2
|
-
import { isRecord } from '@metro-labs/core/is-record';
|
|
3
|
-
import { log } from '@metro-labs/core/log';
|
|
4
|
-
|
|
5
|
-
export type BlockedReason = (connectorId: string, tool: string, args: Record<string, unknown>) => string | null;
|
|
6
|
-
|
|
7
|
-
export interface Screened {
|
|
8
|
-
forward: Uint8Array<ArrayBuffer> | null;
|
|
9
|
-
answers: Record<string, unknown>[];
|
|
10
|
-
batch: boolean;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
const encoder = new TextEncoder();
|
|
14
|
-
|
|
15
|
-
function parsed(body: Uint8Array): unknown {
|
|
16
|
-
try {
|
|
17
|
-
return JSON.parse(new TextDecoder().decode(body));
|
|
18
|
-
} catch {
|
|
19
|
-
return undefined;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function toolOf(message: unknown): string | null {
|
|
24
|
-
if (!isRecord(message) || message.method !== 'tools/call' || !isRecord(message.params)) return null;
|
|
25
|
-
const name = message.params.name;
|
|
26
|
-
return typeof name === 'string' ? name : null;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function blockedAnswer(message: Record<string, unknown>, reason: string): Record<string, unknown> | null {
|
|
30
|
-
if (!('id' in message)) return null;
|
|
31
|
-
return { jsonrpc: '2.0', id: message.id, result: { isError: true, content: [{ type: 'text', text: reason }] } };
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function argsOf(message: unknown): Record<string, unknown> {
|
|
35
|
-
if (!isRecord(message) || !isRecord(message.params)) return {};
|
|
36
|
-
return isRecord(message.params.arguments) ? message.params.arguments : {};
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function reasonFor(message: unknown, connectorId: string, blocked: BlockedReason): string | null {
|
|
40
|
-
const tool = toolOf(message);
|
|
41
|
-
return tool === null ? null : blocked(connectorId, tool, argsOf(message));
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export function screenCalls(body: Uint8Array<ArrayBuffer>, connectorId: string, blocked: BlockedReason): Screened | null {
|
|
45
|
-
const message = parsed(body);
|
|
46
|
-
const batch = Array.isArray(message);
|
|
47
|
-
const list: unknown[] = batch ? message : [message];
|
|
48
|
-
const answers: Record<string, unknown>[] = [];
|
|
49
|
-
const kept: unknown[] = [];
|
|
50
|
-
for (const item of list) {
|
|
51
|
-
const reason = reasonFor(item, connectorId, blocked);
|
|
52
|
-
if (reason === null || !isRecord(item)) {
|
|
53
|
-
kept.push(item);
|
|
54
|
-
continue;
|
|
55
|
-
}
|
|
56
|
-
log.info({ connector: connectorId, tool: toolOf(item) }, 'relay: a call the owner blocked was answered without the connector');
|
|
57
|
-
const answer = blockedAnswer(item, reason);
|
|
58
|
-
if (answer !== null) answers.push(answer);
|
|
59
|
-
}
|
|
60
|
-
if (kept.length === list.length) return null;
|
|
61
|
-
const forward = kept.length === 0 ? null : new Uint8Array(encoder.encode(JSON.stringify(kept)));
|
|
62
|
-
return { forward, answers, batch };
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export function answerBlocked(res: ServerResponse, screened: Screened): void {
|
|
66
|
-
if (res.headersSent) return;
|
|
67
|
-
if (screened.answers.length === 0) {
|
|
68
|
-
res.writeHead(202).end();
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
const body = screened.batch ? screened.answers : screened.answers[0];
|
|
72
|
-
res.writeHead(200, { 'content-type': 'application/json' });
|
|
73
|
-
res.end(JSON.stringify(body));
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
const sseFrames = (answers: Record<string, unknown>[]): string =>
|
|
77
|
-
answers.map((answer) => `event: message\ndata: ${JSON.stringify(answer)}\n\n`).join('');
|
|
78
|
-
|
|
79
|
-
export async function mergeBlocked(res: ServerResponse, upstream: Response, headers: Record<string, string>, answers: Record<string, unknown>[]): Promise<void> {
|
|
80
|
-
const text = await upstream.text();
|
|
81
|
-
if ((headers['content-type'] ?? '').includes('text/event-stream')) {
|
|
82
|
-
res.writeHead(upstream.status, headers);
|
|
83
|
-
res.end(`${text}${sseFrames(answers)}`);
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
const theirs = text.trim() === '' ? [] : parsed(encoder.encode(text));
|
|
87
|
-
const list: unknown[] = Array.isArray(theirs) ? theirs : theirs === undefined || theirs === null ? [] : [theirs];
|
|
88
|
-
res.writeHead(200, { ...headers, 'content-type': 'application/json' });
|
|
89
|
-
res.end(JSON.stringify([...list, ...answers]));
|
|
90
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import type { BlockedReason } from './relay-policy.js';
|
|
2
|
-
import type { RelayTarget } from './relay-target.js';
|
|
3
|
-
|
|
4
|
-
export interface RelaySession {
|
|
5
|
-
target: (force: boolean) => Promise<RelayTarget>;
|
|
6
|
-
current: () => RelayTarget;
|
|
7
|
-
blocker: () => BlockedReason;
|
|
8
|
-
signal: AbortSignal;
|
|
9
|
-
close: () => void;
|
|
10
|
-
}
|
|
@@ -1,287 +0,0 @@
|
|
|
1
|
-
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
-
import type { RelaySession } from './relay-session.js';
|
|
3
|
-
import { exchangeOrg } from './org-relay.js';
|
|
4
|
-
import { ApiError } from '@metro-labs/http/api-error';
|
|
5
|
-
import { log } from '@metro-labs/core/log';
|
|
6
|
-
import { answerBlocked, mergeBlocked } from './relay-policy.js';
|
|
7
|
-
|
|
8
|
-
export interface RelayApiDeps {
|
|
9
|
-
identify: (req: IncomingMessage) => { subject: string; agentId: string } | null;
|
|
10
|
-
open: (connectorId: string) => RelaySession | Promise<RelaySession>;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
const ID_PATH_RE = /^\/relay\/(org-[A-Za-z0-9][A-Za-z0-9_-]{10})$/;
|
|
14
|
-
const METHODS = new Set(['POST', 'GET', 'DELETE']);
|
|
15
|
-
const RELAY_BODY_MAX = 8 * 1024 * 1024;
|
|
16
|
-
const FIRST_BYTE_MS = 120_000;
|
|
17
|
-
const PASS_REQ = [
|
|
18
|
-
'accept',
|
|
19
|
-
'content-type',
|
|
20
|
-
'mcp-session-id',
|
|
21
|
-
'mcp-protocol-version',
|
|
22
|
-
'last-event-id',
|
|
23
|
-
] as const;
|
|
24
|
-
const PASS_RES = [
|
|
25
|
-
'content-type',
|
|
26
|
-
'mcp-session-id',
|
|
27
|
-
'mcp-protocol-version',
|
|
28
|
-
'cache-control',
|
|
29
|
-
] as const;
|
|
30
|
-
|
|
31
|
-
const keepaliveMs = (): number =>
|
|
32
|
-
Number(process.env.METRO_RELAY_KEEPALIVE_MS) || 25_000;
|
|
33
|
-
|
|
34
|
-
const isRelaySse = (contentType: string | undefined): boolean => /^text\/event-stream\s*(?:;|$)/i.test(contentType ?? '');
|
|
35
|
-
|
|
36
|
-
function answer(
|
|
37
|
-
res: ServerResponse,
|
|
38
|
-
status: number,
|
|
39
|
-
body: Record<string, unknown>,
|
|
40
|
-
): void {
|
|
41
|
-
if (res.headersSent) return;
|
|
42
|
-
res.writeHead(status, { 'content-type': 'application/json' });
|
|
43
|
-
res.end(JSON.stringify(body));
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function closeIfBodyUnread(req: IncomingMessage, res: ServerResponse): void {
|
|
47
|
-
if (req.readableEnded) return;
|
|
48
|
-
const drop = (): void => {
|
|
49
|
-
req.socket?.destroy();
|
|
50
|
-
};
|
|
51
|
-
if (res.writableFinished) drop();
|
|
52
|
-
else res.once('finish', drop);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
async function readCapped(req: IncomingMessage): Promise<Uint8Array<ArrayBuffer>> {
|
|
56
|
-
const declared = Number(req.headers['content-length']);
|
|
57
|
-
if (Number.isFinite(declared) && declared > RELAY_BODY_MAX)
|
|
58
|
-
throw new ApiError('relay body exceeds 8 MiB', 413);
|
|
59
|
-
const chunks: Buffer[] = [];
|
|
60
|
-
let total = 0;
|
|
61
|
-
for await (const chunk of req) {
|
|
62
|
-
const buf = chunk as Buffer;
|
|
63
|
-
total += buf.length;
|
|
64
|
-
if (total > RELAY_BODY_MAX)
|
|
65
|
-
throw new ApiError('relay body exceeds 8 MiB', 413);
|
|
66
|
-
chunks.push(buf);
|
|
67
|
-
}
|
|
68
|
-
const merged = Buffer.concat(chunks);
|
|
69
|
-
const out = new Uint8Array(new ArrayBuffer(merged.byteLength));
|
|
70
|
-
out.set(merged);
|
|
71
|
-
return out;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function upstreamHeaders(
|
|
75
|
-
req: IncomingMessage,
|
|
76
|
-
injected: Record<string, string>,
|
|
77
|
-
): Record<string, string> {
|
|
78
|
-
const out: Record<string, string> = {};
|
|
79
|
-
for (const name of PASS_REQ) {
|
|
80
|
-
const value = req.headers[name];
|
|
81
|
-
if (typeof value === 'string') out[name] = value;
|
|
82
|
-
}
|
|
83
|
-
return { ...out, ...injected };
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
async function forward(
|
|
87
|
-
req: IncomingMessage,
|
|
88
|
-
target: { url: string; headers: Record<string, string> },
|
|
89
|
-
body: Uint8Array<ArrayBuffer> | null,
|
|
90
|
-
signal: AbortSignal,
|
|
91
|
-
): Promise<Response> {
|
|
92
|
-
const firstByte = setTimeout(() => {
|
|
93
|
-
log.warn('relay: upstream sent no response in time');
|
|
94
|
-
}, FIRST_BYTE_MS);
|
|
95
|
-
firstByte.unref?.();
|
|
96
|
-
try {
|
|
97
|
-
return await fetch(target.url, {
|
|
98
|
-
method: req.method,
|
|
99
|
-
headers: upstreamHeaders(req, target.headers),
|
|
100
|
-
...(body === null ? {} : { body }),
|
|
101
|
-
signal,
|
|
102
|
-
redirect: 'manual',
|
|
103
|
-
});
|
|
104
|
-
} finally {
|
|
105
|
-
clearTimeout(firstByte);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
interface Keepalive {
|
|
110
|
-
touch: () => void;
|
|
111
|
-
stop: () => void;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
function startKeepalive(res: ServerResponse): Keepalive {
|
|
115
|
-
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
116
|
-
const arm = (): void => {
|
|
117
|
-
timer = setTimeout(() => {
|
|
118
|
-
try {
|
|
119
|
-
res.write(': keepalive\n\n');
|
|
120
|
-
} catch {
|
|
121
|
-
log.debug('relay: keepalive write failed');
|
|
122
|
-
}
|
|
123
|
-
arm();
|
|
124
|
-
}, keepaliveMs());
|
|
125
|
-
timer.unref?.();
|
|
126
|
-
};
|
|
127
|
-
arm();
|
|
128
|
-
return {
|
|
129
|
-
touch: (): void => {
|
|
130
|
-
if (timer) clearTimeout(timer);
|
|
131
|
-
arm();
|
|
132
|
-
},
|
|
133
|
-
stop: (): void => {
|
|
134
|
-
if (timer) clearTimeout(timer);
|
|
135
|
-
},
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
function drainedOrClosed(res: ServerResponse): Promise<void> {
|
|
140
|
-
return new Promise((resolve) => {
|
|
141
|
-
const done = (): void => {
|
|
142
|
-
res.off('drain', done);
|
|
143
|
-
res.off('close', done);
|
|
144
|
-
resolve();
|
|
145
|
-
};
|
|
146
|
-
res.once('drain', done);
|
|
147
|
-
res.once('close', done);
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
async function pumpBody(
|
|
152
|
-
res: ServerResponse,
|
|
153
|
-
body: ReadableStream<Uint8Array>,
|
|
154
|
-
touch: () => void,
|
|
155
|
-
): Promise<void> {
|
|
156
|
-
const reader = body.getReader();
|
|
157
|
-
for (;;) {
|
|
158
|
-
const { done, value } = await reader.read();
|
|
159
|
-
if (done) return;
|
|
160
|
-
touch();
|
|
161
|
-
if (!res.write(value)) await drainedOrClosed(res);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
function passedHeaders(upstream: Response): Record<string, string> {
|
|
166
|
-
const headers: Record<string, string> = {};
|
|
167
|
-
for (const name of PASS_RES) {
|
|
168
|
-
const value = upstream.headers.get(name);
|
|
169
|
-
if (value !== null) headers[name] = value;
|
|
170
|
-
}
|
|
171
|
-
return headers;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
async function pipe(res: ServerResponse, upstream: Response, answers: Record<string, unknown>[]): Promise<void> {
|
|
175
|
-
const headers = passedHeaders(upstream);
|
|
176
|
-
if (answers.length > 0 && (upstream.status === 200 || upstream.status === 202)) {
|
|
177
|
-
await mergeBlocked(res, upstream, headers, answers);
|
|
178
|
-
return;
|
|
179
|
-
}
|
|
180
|
-
const sse = isRelaySse(headers['content-type']);
|
|
181
|
-
if (sse) headers['x-accel-buffering'] = 'no';
|
|
182
|
-
res.writeHead(upstream.status, headers);
|
|
183
|
-
if (sse) res.flushHeaders();
|
|
184
|
-
if (upstream.body === null) {
|
|
185
|
-
res.end();
|
|
186
|
-
return;
|
|
187
|
-
}
|
|
188
|
-
const keepalive = sse ? startKeepalive(res) : null;
|
|
189
|
-
try {
|
|
190
|
-
await pumpBody(res, upstream.body, () => keepalive?.touch());
|
|
191
|
-
} finally {
|
|
192
|
-
keepalive?.stop();
|
|
193
|
-
}
|
|
194
|
-
res.end();
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
async function deliver(res: ServerResponse, upstream: Response, answers: Record<string, unknown>[]): Promise<void> {
|
|
198
|
-
if (upstream.status >= 300 && upstream.status < 400) {
|
|
199
|
-
await upstream.body?.cancel();
|
|
200
|
-
answer(res, 502, { error: 'the connector redirected; metro does not follow' });
|
|
201
|
-
return;
|
|
202
|
-
}
|
|
203
|
-
await pipe(res, upstream, answers);
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
async function relayOnce(
|
|
207
|
-
req: IncomingMessage,
|
|
208
|
-
res: ServerResponse,
|
|
209
|
-
connectorId: string,
|
|
210
|
-
deps: RelayApiDeps,
|
|
211
|
-
signal: AbortSignal,
|
|
212
|
-
): Promise<void> {
|
|
213
|
-
const read = req.method === 'POST' ? await readCapped(req) : null;
|
|
214
|
-
const session = await deps.open(connectorId);
|
|
215
|
-
try {
|
|
216
|
-
const out = await exchangeOrg(connectorId, session, read, signal, (target, body, guard) => forward(req, target, body, guard));
|
|
217
|
-
if (out.kind === 'blocked') answerBlocked(res, out.screened);
|
|
218
|
-
else await deliver(res, out.upstream, out.answers);
|
|
219
|
-
} finally { session.close(); }
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
async function relayExchange(
|
|
223
|
-
req: IncomingMessage,
|
|
224
|
-
res: ServerResponse,
|
|
225
|
-
connectorId: string,
|
|
226
|
-
deps: RelayApiDeps,
|
|
227
|
-
): Promise<void> {
|
|
228
|
-
const control = new AbortController();
|
|
229
|
-
const bail = (): void => {
|
|
230
|
-
if (!res.writableEnded) control.abort();
|
|
231
|
-
};
|
|
232
|
-
res.once('close', bail);
|
|
233
|
-
res.socket?.once('close', bail);
|
|
234
|
-
res.once('finish', () => {
|
|
235
|
-
res.socket?.removeListener('close', bail);
|
|
236
|
-
});
|
|
237
|
-
try {
|
|
238
|
-
await relayOnce(req, res, connectorId, deps, control.signal);
|
|
239
|
-
} catch (err) {
|
|
240
|
-
if (!control.signal.aborted) throw err;
|
|
241
|
-
log.debug({ connector: connectorId }, 'relay: the client left before the answer ended');
|
|
242
|
-
if (!res.writableEnded) res.end();
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
function dispatch(
|
|
247
|
-
req: IncomingMessage,
|
|
248
|
-
res: ServerResponse,
|
|
249
|
-
connectorId: string,
|
|
250
|
-
deps: RelayApiDeps,
|
|
251
|
-
): void {
|
|
252
|
-
const who = deps.identify(req);
|
|
253
|
-
if (who === null) {
|
|
254
|
-
answer(res, 401, { error: 'unauthorized' });
|
|
255
|
-
return;
|
|
256
|
-
}
|
|
257
|
-
relayExchange(req, res, connectorId, deps)
|
|
258
|
-
.catch((err: unknown) => {
|
|
259
|
-
if (err instanceof ApiError) answer(res, err.status, { error: err.message });
|
|
260
|
-
else {
|
|
261
|
-
log.warn({ connector: connectorId }, 'organization relay: request failed');
|
|
262
|
-
answer(res, 503, { error: 'The organization connector is unavailable. Try again.' });
|
|
263
|
-
}
|
|
264
|
-
if (!res.writableEnded) res.end();
|
|
265
|
-
closeIfBodyUnread(req, res);
|
|
266
|
-
});
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
export function handleRelayRequest(
|
|
270
|
-
req: IncomingMessage,
|
|
271
|
-
res: ServerResponse,
|
|
272
|
-
deps: RelayApiDeps,
|
|
273
|
-
): boolean {
|
|
274
|
-
const path = (req.url ?? '').split('?')[0] ?? '';
|
|
275
|
-
if (path !== '/relay' && !path.startsWith('/relay/')) return false;
|
|
276
|
-
const match = ID_PATH_RE.exec(path);
|
|
277
|
-
if (match?.[1] === undefined) {
|
|
278
|
-
answer(res, 404, { error: 'no such connector' });
|
|
279
|
-
return true;
|
|
280
|
-
}
|
|
281
|
-
if (!METHODS.has(req.method ?? '')) {
|
|
282
|
-
answer(res, 405, { error: 'method not allowed' });
|
|
283
|
-
return true;
|
|
284
|
-
}
|
|
285
|
-
dispatch(req, res, match[1], deps);
|
|
286
|
-
return true;
|
|
287
|
-
}
|