@stage-labs/metro 0.1.0-beta.287 → 0.1.0-beta.289
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 +1 -1
- package/runtime/marketplace/plugin/bin/policy.mjs +3 -2
- package/runtime/node_modules/@metro-labs/core/package.json +1 -0
- package/runtime/node_modules/@metro-labs/core/src/connector-sync.ts +17 -0
- package/runtime/node_modules/@metro-labs/daemon/src/approvals/pending.ts +6 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/boot.ts +7 -3
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/agent-key.ts +148 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/api.ts +14 -2
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/config.ts +2 -72
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/enroll-api.ts +101 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/gates.ts +40 -4
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/metro-api.ts +93 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/migration-preview.ts +201 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/migration-url.ts +32 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-routes.ts +3 -3
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth.ts +4 -7
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-relay.ts +61 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-runtime-discovery.ts +111 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-runtime.ts +246 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-sync-api.ts +25 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-sync-control.ts +138 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-sync-setting.ts +60 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-sync-watch.ts +39 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-sync.ts +187 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/plugin-sync.ts +11 -1
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/publication.ts +33 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-id.ts +2 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-session.ts +10 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-target.ts +4 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay.ts +25 -4
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/store.ts +10 -20
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/tools.ts +16 -13
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/transfer-store.ts +3 -2
- package/runtime/node_modules/@metro-labs/daemon/src/policy/policy.ts +2 -1
- package/runtime/node_modules/@metro-labs/daemon/src/routes/local-mode.ts +13 -3
- package/runtime/node_modules/@metro-labs/daemon/src/routes/session-apis.ts +6 -0
- package/runtime/node_modules/@metro-labs/http/package.json +10 -2
- package/runtime/node_modules/@metro-labs/http/src/agent-signature.ts +110 -0
- package/runtime/node_modules/@metro-labs/http/src/api-http.ts +6 -2
- package/runtime/node_modules/@metro-labs/http/src/connector-delivery-validation.ts +115 -0
- package/runtime/node_modules/@metro-labs/http/src/connector-delivery.ts +54 -0
- package/runtime/node_modules/@metro-labs/{daemon/src/connectors/oauth-discovery.ts → http/src/connector-discovery.ts} +31 -14
- package/runtime/node_modules/@metro-labs/http/src/connector-input.ts +73 -0
- package/runtime/node_modules/@metro-labs/{daemon/src/connectors/verify.ts → http/src/connector-mcp.ts} +16 -16
- package/runtime/node_modules/@metro-labs/{daemon/src/connectors/oauth-client.ts → http/src/connector-oauth.ts} +36 -13
- package/runtime/node_modules/@metro-labs/http/src/connector-policy.ts +53 -0
- package/runtime/node_modules/@metro-labs/{daemon/src/connectors/url.ts → http/src/connector-url.ts} +1 -1
- package/runtime/node_modules/@metro-labs/http/src/ed25519.ts +56 -0
- package/runtime/runtime.json +1 -1
- package/runtime/sdk-runner/node_modules/@metro-labs/core/package.json +1 -0
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/connector-sync.ts +17 -0
- package/runtime/sdk-runner/runner.json +1 -1
- /package/runtime/node_modules/@metro-labs/{daemon/src/connectors/reach.ts → http/src/connector-reach.ts} +0 -0
|
@@ -0,0 +1,187 @@
|
|
|
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, 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 expiry: ReturnType<typeof setTimeout> | null = null;
|
|
51
|
+
private readonly monotonic: () => number;
|
|
52
|
+
private readonly listeners = new Set<() => void>();
|
|
53
|
+
|
|
54
|
+
constructor(private readonly deps: OrgSyncDeps) {
|
|
55
|
+
this.monotonic = deps.monotonic ?? (() => performance.now());
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
onChange(listener: () => void): () => void {
|
|
59
|
+
this.listeners.add(listener);
|
|
60
|
+
return () => { this.listeners.delete(listener); };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
epoch(): number {
|
|
64
|
+
return this.generation;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
private notify(): void {
|
|
68
|
+
for (const listener of [...this.listeners]) {
|
|
69
|
+
try {
|
|
70
|
+
listener();
|
|
71
|
+
} catch {
|
|
72
|
+
log.warn('organization connectors: a memory change listener failed');
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
private identity(): Identity | null {
|
|
78
|
+
if (!this.deps.enabled()) return null;
|
|
79
|
+
const owner = this.deps.owner();
|
|
80
|
+
const key = this.deps.key();
|
|
81
|
+
const enrollment = key?.enrollment;
|
|
82
|
+
if (owner === null || key === null || enrollment === null || enrollment === undefined || enrollment.organization !== owner) return null;
|
|
83
|
+
const context = { owner, agent: enrollment.server, keyId: key.keyId };
|
|
84
|
+
return { key, context, token: JSON.stringify([context, key.sealingKey]) };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
private forget(): void {
|
|
88
|
+
this.snapshot = null;
|
|
89
|
+
this.deadline = 0;
|
|
90
|
+
this.wallDeadline = 0;
|
|
91
|
+
if (this.expiry !== null) clearTimeout(this.expiry);
|
|
92
|
+
this.expiry = null;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
invalidate(reason: 'waiting' | 'expired' | 'unavailable' = 'waiting'): void {
|
|
96
|
+
this.generation += 1;
|
|
97
|
+
this.controller?.abort();
|
|
98
|
+
this.controller = null;
|
|
99
|
+
if (reason === 'waiting') {
|
|
100
|
+
this.receiptToken = null;
|
|
101
|
+
this.receipt = { state: reason, lastReceivedAt: null, leaseExpiresAt: null, counts: null };
|
|
102
|
+
} else this.receipt = { ...this.receipt, state: reason, counts: null };
|
|
103
|
+
this.token = null;
|
|
104
|
+
this.forget();
|
|
105
|
+
this.notify();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
private validate(identity: Identity | null): void {
|
|
109
|
+
if (this.token === null) return;
|
|
110
|
+
if (identity === null || identity.token !== this.token) this.invalidate();
|
|
111
|
+
else if (this.snapshot !== null && (this.monotonic() >= this.deadline || this.deps.api.now() >= this.wallDeadline)) this.invalidate('expired');
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
read(): DeliverySnapshot | null {
|
|
115
|
+
if (this.token === null) return null;
|
|
116
|
+
try {
|
|
117
|
+
this.validate(this.identity());
|
|
118
|
+
} catch {
|
|
119
|
+
this.invalidate();
|
|
120
|
+
log.warn('organization connectors: memory could not be checked and was cleared');
|
|
121
|
+
}
|
|
122
|
+
return this.snapshot === null ? null : structuredClone(this.snapshot);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
status(): ConnectorSyncReceipt {
|
|
126
|
+
this.read();
|
|
127
|
+
try {
|
|
128
|
+
if (this.receiptToken !== null && this.identity()?.token !== this.receiptToken) this.invalidate();
|
|
129
|
+
} catch {
|
|
130
|
+
this.invalidate();
|
|
131
|
+
}
|
|
132
|
+
return structuredClone(this.receipt);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
async sync(refresh?: RefreshRequest): Promise<void> {
|
|
136
|
+
let identity: Identity | null;
|
|
137
|
+
try {
|
|
138
|
+
identity = this.identity();
|
|
139
|
+
this.validate(identity);
|
|
140
|
+
} catch (err) {
|
|
141
|
+
this.invalidate();
|
|
142
|
+
throw err;
|
|
143
|
+
}
|
|
144
|
+
if (identity === null) return;
|
|
145
|
+
if (this.running !== null) return refresh === undefined ? this.running : this.running.then(() => this.sync(refresh));
|
|
146
|
+
this.token = identity.token;
|
|
147
|
+
const generation = this.generation;
|
|
148
|
+
this.controller = new AbortController();
|
|
149
|
+
this.running = this.fetch(identity, refresh, generation, this.controller.signal).catch((err: unknown) => {
|
|
150
|
+
if (generation === this.generation) this.invalidate('unavailable');
|
|
151
|
+
throw err;
|
|
152
|
+
}).finally(() => { this.running = null; });
|
|
153
|
+
return this.running;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async syncFresh(refresh?: RefreshRequest): Promise<void> {
|
|
157
|
+
if (this.running !== null) {
|
|
158
|
+
await this.running.catch(() => {
|
|
159
|
+
log.debug('organization connectors: a previous sync failed before a fresh check');
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
return this.sync(refresh);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
private async fetch(identity: Identity, refresh: RefreshRequest | undefined, generation: number, signal: AbortSignal): Promise<void> {
|
|
166
|
+
const requestId = randomBytes(32).toString('base64url');
|
|
167
|
+
const context = { ...identity.context, requestId };
|
|
168
|
+
const started = this.deps.api.now();
|
|
169
|
+
const clock = this.monotonic();
|
|
170
|
+
const response = await agentCall(this.deps.api, identity.key, '/api/agents/connectors', { requestId, ...(refresh === undefined ? {} : { refresh }) }, { answerMax: CONNECTOR_DELIVERY_MAX_BYTES, timeoutMs: 60_000, signal });
|
|
171
|
+
this.validate(this.identity());
|
|
172
|
+
if (generation !== this.generation || this.token !== identity.token) throw invalid();
|
|
173
|
+
const snapshot = openConnectorDelivery(identity.key.sealing, context, response.sealed);
|
|
174
|
+
const remaining = lease(snapshot, started, this.monotonic() - clock, this.deps.api.now());
|
|
175
|
+
this.forget();
|
|
176
|
+
this.snapshot = snapshot;
|
|
177
|
+
this.deadline = this.monotonic() + remaining;
|
|
178
|
+
this.wallDeadline = this.deps.api.now() + remaining;
|
|
179
|
+
this.receiptToken = identity.token;
|
|
180
|
+
const total = snapshot.connectors.length;
|
|
181
|
+
const ready = snapshot.connectors.filter((row) => row.state === 'ready').length;
|
|
182
|
+
this.receipt = { state: 'received', lastReceivedAt: this.deps.api.now(), leaseExpiresAt: this.wallDeadline, counts: { total, ready, unavailable: total - ready } };
|
|
183
|
+
this.expiry = setTimeout(() => { this.invalidate('expired'); }, remaining);
|
|
184
|
+
this.expiry.unref();
|
|
185
|
+
this.notify();
|
|
186
|
+
}
|
|
187
|
+
}
|
|
@@ -5,6 +5,7 @@ import { join } from 'node:path';
|
|
|
5
5
|
import { errMsg, log } from '@metro-labs/core/log';
|
|
6
6
|
import { claudeDir } from '../claude/files.js';
|
|
7
7
|
import { loopbackBase } from '../files/attach-serve.js';
|
|
8
|
+
import { isOrgRelayId } from './relay-id.js';
|
|
8
9
|
|
|
9
10
|
const MARKER = join('bin', 'metro-plugin.mjs');
|
|
10
11
|
const FILE = '.mcp.json';
|
|
@@ -35,12 +36,21 @@ export function serverKey(name: string): string {
|
|
|
35
36
|
export function serverKeysOf(rows: PluginRow[]): Map<string, string> {
|
|
36
37
|
const taken = new Map<string, number>();
|
|
37
38
|
const keys = new Map<string, string>();
|
|
38
|
-
for (const row of rows) {
|
|
39
|
+
for (const row of rows.filter((row) => !isOrgRelayId(row.id))) {
|
|
39
40
|
const wanted = serverKey(row.name);
|
|
40
41
|
const seen = taken.get(wanted) ?? 0;
|
|
41
42
|
taken.set(wanted, seen + 1);
|
|
42
43
|
keys.set(row.id, seen === 0 ? wanted : `${wanted}-${row.id.toLowerCase().replace(/[^a-z0-9]/g, '').slice(0, 4)}`);
|
|
43
44
|
}
|
|
45
|
+
const reserved = new Set(keys.values());
|
|
46
|
+
for (const row of rows.filter((row) => isOrgRelayId(row.id)).sort((a, b) => a.id.localeCompare(b.id))) {
|
|
47
|
+
const wanted = `org-${serverKey(row.name).slice(0, 35)}-${Buffer.from(row.id.slice(4)).toString('hex')}`;
|
|
48
|
+
let key = wanted;
|
|
49
|
+
let suffix = 1;
|
|
50
|
+
while (reserved.has(key)) key = `${wanted}-${String(suffix++)}`;
|
|
51
|
+
reserved.add(key);
|
|
52
|
+
keys.set(row.id, key);
|
|
53
|
+
}
|
|
44
54
|
return keys;
|
|
45
55
|
}
|
|
46
56
|
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { registerConnectors, type GateRow } from './gates.js';
|
|
2
|
+
import { syncPluginServers } from './plugin-sync.js';
|
|
3
|
+
import { isOrgRelayId } from './relay-id.js';
|
|
4
|
+
|
|
5
|
+
let local: GateRow[] = [];
|
|
6
|
+
let organization: GateRow[] = [];
|
|
7
|
+
|
|
8
|
+
function metadata(rows: readonly GateRow[]): GateRow[] {
|
|
9
|
+
return rows.map((row) => structuredClone({
|
|
10
|
+
id: row.id,
|
|
11
|
+
name: row.name,
|
|
12
|
+
config: {
|
|
13
|
+
...(row.config.policy === undefined ? {} : { policy: row.config.policy }),
|
|
14
|
+
...(row.config.toolGroups === undefined ? {} : { toolGroups: row.config.toolGroups }),
|
|
15
|
+
},
|
|
16
|
+
}));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function publishConnectors(): void {
|
|
20
|
+
const rows = [...local, ...organization];
|
|
21
|
+
registerConnectors(rows);
|
|
22
|
+
syncPluginServers(rows);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function publishLocalConnectors(rows: readonly GateRow[]): void {
|
|
26
|
+
local = metadata(rows.filter((row) => !isOrgRelayId(row.id)));
|
|
27
|
+
publishConnectors();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function publishOrgConnectors(rows: readonly GateRow[]): void {
|
|
31
|
+
organization = metadata(rows.filter((row) => isOrgRelayId(row.id)));
|
|
32
|
+
publishConnectors();
|
|
33
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
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,7 +1,10 @@
|
|
|
1
1
|
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
-
import { whyUnreachable } from '
|
|
2
|
+
import { whyUnreachable } from '@metro-labs/http/connector-reach';
|
|
3
3
|
import { noteHealth } from './health.js';
|
|
4
|
-
import type { RelayTarget } from './
|
|
4
|
+
import type { RelayTarget } from './relay-target.js';
|
|
5
|
+
import type { RelaySession } from './relay-session.js';
|
|
6
|
+
import { isOrgRelayId } from './relay-id.js';
|
|
7
|
+
import { exchangeOrg } from './org-relay.js';
|
|
5
8
|
import { ApiError } from '@metro-labs/http/api-error';
|
|
6
9
|
import { errMsg, log } from '@metro-labs/core/log';
|
|
7
10
|
import { answerBlocked, mergeBlocked, screenCalls, type BlockedReason } from './relay-policy.js';
|
|
@@ -11,9 +14,10 @@ export interface RelayApiDeps {
|
|
|
11
14
|
identify: (req: IncomingMessage) => { subject: string; agentId: string } | null;
|
|
12
15
|
signedOut: (connectorId: string) => void;
|
|
13
16
|
blocked?: BlockedReason;
|
|
17
|
+
open?: (connectorId: string) => RelaySession | null;
|
|
14
18
|
}
|
|
15
19
|
|
|
16
|
-
const ID_PATH_RE = /^\/relay\/([A-Za-z0-9][A-Za-z0-9_-]{10})$/;
|
|
20
|
+
const ID_PATH_RE = /^\/relay\/((?:org-)?[A-Za-z0-9][A-Za-z0-9_-]{10})$/;
|
|
17
21
|
const METHODS = new Set(['POST', 'GET', 'DELETE']);
|
|
18
22
|
const RELAY_BODY_MAX = 8 * 1024 * 1024;
|
|
19
23
|
const FIRST_BYTE_MS = 120_000;
|
|
@@ -99,9 +103,10 @@ async function forward(
|
|
|
99
103
|
target: { url: string; headers: Record<string, string> },
|
|
100
104
|
body: Uint8Array<ArrayBuffer> | null,
|
|
101
105
|
signal: AbortSignal,
|
|
106
|
+
sensitive = false,
|
|
102
107
|
): Promise<Response> {
|
|
103
108
|
const firstByte = setTimeout(() => {
|
|
104
|
-
log.warn({ url: target.url }, 'relay: upstream sent no response in time');
|
|
109
|
+
log.warn(sensitive ? {} : { url: target.url }, 'relay: upstream sent no response in time');
|
|
105
110
|
}, FIRST_BYTE_MS);
|
|
106
111
|
firstByte.unref?.();
|
|
107
112
|
try {
|
|
@@ -284,6 +289,16 @@ async function deliver(
|
|
|
284
289
|
await pipe(res, out.upstream, answers);
|
|
285
290
|
}
|
|
286
291
|
|
|
292
|
+
async function relayOrg(req: IncomingMessage, res: ServerResponse, id: string, deps: RelayApiDeps, read: Uint8Array<ArrayBuffer> | null, signal: AbortSignal): Promise<void> {
|
|
293
|
+
const session = deps.open?.(id);
|
|
294
|
+
if (session === undefined || session === null) throw new ApiError('Organization connector tools are not switched on yet.', 503);
|
|
295
|
+
try {
|
|
296
|
+
const out = await exchangeOrg(id, session, read, signal, (target, body, guard) => forward(req, target, body, guard, true));
|
|
297
|
+
if (out.kind === 'blocked') answerBlocked(res, out.screened);
|
|
298
|
+
else await deliver(res, id, deps, out, out.answers);
|
|
299
|
+
} finally { session.close(); }
|
|
300
|
+
}
|
|
301
|
+
|
|
287
302
|
async function relayOnce(
|
|
288
303
|
req: IncomingMessage,
|
|
289
304
|
res: ServerResponse,
|
|
@@ -292,6 +307,7 @@ async function relayOnce(
|
|
|
292
307
|
signal: AbortSignal,
|
|
293
308
|
): Promise<void> {
|
|
294
309
|
const read = req.method === 'POST' ? await readCapped(req) : null;
|
|
310
|
+
if (isOrgRelayId(connectorId)) return relayOrg(req, res, connectorId, deps, read, signal);
|
|
295
311
|
const screened = read === null || deps.blocked === undefined ? null : screenCalls(read, connectorId, deps.blocked);
|
|
296
312
|
if (screened?.forward === null) {
|
|
297
313
|
answerBlocked(res, screened);
|
|
@@ -316,6 +332,11 @@ function dispatch(
|
|
|
316
332
|
.catch((err: unknown) => {
|
|
317
333
|
if (err instanceof ApiError) {
|
|
318
334
|
answer(res, err.status, { error: err.message });
|
|
335
|
+
if (!res.writableEnded) res.end();
|
|
336
|
+
} else if (isOrgRelayId(connectorId)) {
|
|
337
|
+
log.warn({ connector: connectorId }, 'organization relay: request failed');
|
|
338
|
+
answer(res, 503, { error: 'The organization connector is unavailable. Try again.' });
|
|
339
|
+
if (!res.writableEnded) res.end();
|
|
319
340
|
} else {
|
|
320
341
|
log.warn({ err: errMsg(err), connector: connectorId }, 'relay: failed');
|
|
321
342
|
noteHealth(connectorId, false, `metro could not reach the connector: ${whyUnreachable(err)}`);
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { join } from 'node:path';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { publishLocalConnectors } from './publication.js';
|
|
3
|
+
import type { RelayTarget } from './relay-target.js';
|
|
4
|
+
export type { RelayTarget } from './relay-target.js';
|
|
4
5
|
import type { ToolPolicy } from '../policy/policy.js';
|
|
5
6
|
import type { ToolGroup } from '@metro-labs/core/stations/types';
|
|
6
7
|
import { listRemoteTools, type RemoteTool } from './tools.js';
|
|
@@ -8,28 +9,23 @@ import { readJson, writeSecure } from '@metro-labs/core/secure-fs';
|
|
|
8
9
|
import { errMsg, log } from '@metro-labs/core/log';
|
|
9
10
|
import {
|
|
10
11
|
authHeaders,
|
|
11
|
-
connectorUrlText,
|
|
12
|
-
ConnectorVerifyError,
|
|
13
|
-
parseConnectorUrl,
|
|
14
12
|
verifyRemoteMcp,
|
|
15
13
|
type ConnectorAuth,
|
|
16
14
|
type OAuthAuth,
|
|
17
15
|
type VerifiedRecord,
|
|
18
|
-
} from '
|
|
16
|
+
} from '@metro-labs/http/connector-mcp';
|
|
17
|
+
import { connectorUrlText, ConnectorVerifyError, parseConnectorUrl } from '@metro-labs/http/connector-url';
|
|
19
18
|
import { oauthExpired, refreshOAuth } from './oauth.js';
|
|
20
|
-
import { advertisesOAuth } from '
|
|
19
|
+
import { advertisesOAuth } from '@metro-labs/http/connector-discovery';
|
|
20
|
+
import { ConnectorError, connectorAuth, connectorClient, connectorName } from '@metro-labs/http/connector-input';
|
|
21
21
|
import {
|
|
22
|
-
ConnectorError,
|
|
23
|
-
connectorAuth,
|
|
24
|
-
connectorClient,
|
|
25
|
-
connectorName,
|
|
26
22
|
readConfig,
|
|
27
23
|
signInState,
|
|
28
24
|
stamp,
|
|
29
25
|
type ConnectorConfig,
|
|
30
26
|
type ConnectorSignIn,
|
|
31
27
|
} from './config.js';
|
|
32
|
-
import type { OAuthClient } from '
|
|
28
|
+
import type { OAuthClient } from '@metro-labs/http/connector-oauth';
|
|
33
29
|
import { agentsDir } from '../agents/files.js';
|
|
34
30
|
import { newId } from '@metro-labs/core/ids';
|
|
35
31
|
import type { LoadedConnector } from '../stations/materialize.js';
|
|
@@ -119,8 +115,7 @@ export function readLocalConnectors(dir = agentsDir()): LocalConnectorRow[] {
|
|
|
119
115
|
function writeRows(dir: string, rows: LocalConnectorRow[]): void {
|
|
120
116
|
writeSecure(filePath(dir), `${JSON.stringify({ version: 1, connectors: rows }, null, 2)}\n`);
|
|
121
117
|
if (dir !== agentsDir()) return;
|
|
122
|
-
|
|
123
|
-
registerConnectors(rows);
|
|
118
|
+
publishLocalConnectors(rows);
|
|
124
119
|
}
|
|
125
120
|
|
|
126
121
|
const missing = (): ConnectorError => new ConnectorError('no such connector', 404);
|
|
@@ -298,7 +293,7 @@ export async function localSetConnectorPolicy(id: string, policy: ToolPolicy, di
|
|
|
298
293
|
|
|
299
294
|
export async function loadConnectorPolicies(dir = agentsDir()): Promise<void> {
|
|
300
295
|
const rows = readLocalConnectors(dir);
|
|
301
|
-
|
|
296
|
+
publishLocalConnectors(rows);
|
|
302
297
|
for (const row of rows) if (row.config.policy !== undefined) await refreshGroups(row.id, dir);
|
|
303
298
|
}
|
|
304
299
|
|
|
@@ -332,11 +327,6 @@ function refreshOnce(row: LocalConnectorRow, auth: OAuthAuth, dir: string): Prom
|
|
|
332
327
|
return job;
|
|
333
328
|
}
|
|
334
329
|
|
|
335
|
-
export type RelayTarget =
|
|
336
|
-
| { kind: 'ok'; url: string; headers: Record<string, string> }
|
|
337
|
-
| { kind: 'missing' }
|
|
338
|
-
| { kind: 'signin' };
|
|
339
|
-
|
|
340
330
|
function staleUsable(auth: OAuthAuth, now = Date.now()): boolean {
|
|
341
331
|
return auth.expiresAt === undefined || auth.expiresAt > now;
|
|
342
332
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { isRecord } from '@metro-labs/core/is-record';
|
|
2
2
|
import { errMsg, log } from '@metro-labs/core/log';
|
|
3
|
-
import {
|
|
3
|
+
import type { FetchLike } from '@metro-labs/core/stations/oauth';
|
|
4
|
+
import { authHeaders, INITIALIZE, mcpPost, payloadOf, type ConnectorAuth } from '@metro-labs/http/connector-mcp';
|
|
5
|
+
import { refused } from '@metro-labs/http/connector-url';
|
|
4
6
|
|
|
5
7
|
export interface RemoteTool {
|
|
6
8
|
name: string;
|
|
@@ -22,8 +24,8 @@ function resultOf(text: string, contentType: string): Record<string, unknown> |
|
|
|
22
24
|
return isRecord(parsed) && isRecord(parsed.result) ? parsed.result : null;
|
|
23
25
|
}
|
|
24
26
|
|
|
25
|
-
async function rpc(url: URL, auth: ConnectorAuth, session: Map<string, string>, body: unknown): Promise<Record<string, unknown> | null> {
|
|
26
|
-
const res = await mcpPost(url, auth, body, { timeoutMs: TIMEOUT_MS, headers: Object.fromEntries(session) });
|
|
27
|
+
async function rpc(url: URL, auth: ConnectorAuth, session: Map<string, string>, body: unknown, fetchImpl?: FetchLike): Promise<Record<string, unknown> | null> {
|
|
28
|
+
const res = await mcpPost(url, auth, body, { timeoutMs: TIMEOUT_MS, headers: Object.fromEntries(session), fetch: fetchImpl });
|
|
27
29
|
const id = res.headers.get('mcp-session-id');
|
|
28
30
|
if (id !== null && id !== '') session.set('mcp-session-id', id);
|
|
29
31
|
const text = await res.text().catch(() => '');
|
|
@@ -39,13 +41,14 @@ function toTool(raw: unknown): RemoteTool | null {
|
|
|
39
41
|
return { name: raw.name, title, description: typeof raw.description === 'string' ? raw.description : '', readOnly: annotations.readOnlyHint === true };
|
|
40
42
|
}
|
|
41
43
|
|
|
42
|
-
async function endSession(url: URL, auth: ConnectorAuth, session: Map<string, string
|
|
44
|
+
async function endSession(url: URL, auth: ConnectorAuth, session: Map<string, string>, fetchImpl?: FetchLike): Promise<void> {
|
|
43
45
|
if (!session.has('mcp-session-id')) return;
|
|
44
46
|
try {
|
|
45
|
-
const
|
|
47
|
+
const send = fetchImpl ?? fetch;
|
|
48
|
+
const res = await send(url.toString(), { method: 'DELETE', redirect: 'manual', signal: AbortSignal.timeout(TIMEOUT_MS), headers: { ...authHeaders(auth), ...Object.fromEntries(session) } });
|
|
46
49
|
await res.body?.cancel();
|
|
47
50
|
} catch (err) {
|
|
48
|
-
log.debug({ host: url.hostname, err: errMsg(err) }, 'connector-tools: the remote declined to end the session');
|
|
51
|
+
log.debug(fetchImpl === undefined ? { host: url.hostname, err: errMsg(err) } : {}, 'connector-tools: the remote declined to end the session');
|
|
49
52
|
}
|
|
50
53
|
}
|
|
51
54
|
|
|
@@ -57,11 +60,11 @@ function nextCursor(result: Record<string, unknown>, current: string): string |
|
|
|
57
60
|
return typeof next !== 'string' || next === '' || next === current ? null : next;
|
|
58
61
|
}
|
|
59
62
|
|
|
60
|
-
async function walkTools(url: URL, auth: ConnectorAuth, session: Map<string, string
|
|
63
|
+
async function walkTools(url: URL, auth: ConnectorAuth, session: Map<string, string>, fetchImpl?: FetchLike): Promise<RemoteTool[]> {
|
|
61
64
|
const tools: RemoteTool[] = [];
|
|
62
65
|
let cursor: string | null = '';
|
|
63
66
|
for (let page = 0; page < MAX_PAGES && cursor !== null; page += 1) {
|
|
64
|
-
const result = await rpc(url, auth, session, { jsonrpc: '2.0', id: 2 + page, method: 'tools/list', params: cursor === '' ? {} : { cursor } });
|
|
67
|
+
const result = await rpc(url, auth, session, { jsonrpc: '2.0', id: 2 + page, method: 'tools/list', params: cursor === '' ? {} : { cursor } }, fetchImpl);
|
|
65
68
|
if (result === null) break;
|
|
66
69
|
tools.push(...toolsOf(result));
|
|
67
70
|
cursor = nextCursor(result, cursor);
|
|
@@ -69,13 +72,13 @@ async function walkTools(url: URL, auth: ConnectorAuth, session: Map<string, str
|
|
|
69
72
|
return tools;
|
|
70
73
|
}
|
|
71
74
|
|
|
72
|
-
export async function listRemoteTools(url: URL, auth: ConnectorAuth): Promise<RemoteTool[]> {
|
|
75
|
+
export async function listRemoteTools(url: URL, auth: ConnectorAuth, fetchImpl?: FetchLike): Promise<RemoteTool[]> {
|
|
73
76
|
const session = new Map<string, string>();
|
|
74
|
-
const init = await rpc(url, auth, session, INITIALIZE);
|
|
77
|
+
const init = await rpc(url, auth, session, INITIALIZE, fetchImpl);
|
|
75
78
|
if (init === null || typeof init.protocolVersion !== 'string') throw refused(`${url.hostname} answered, but it does not speak MCP.`);
|
|
76
79
|
session.set('mcp-protocol-version', init.protocolVersion);
|
|
77
|
-
await rpc(url, auth, session, { jsonrpc: '2.0', method: 'notifications/initialized' });
|
|
78
|
-
const tools = await walkTools(url, auth, session);
|
|
79
|
-
await endSession(url, auth, session);
|
|
80
|
+
await rpc(url, auth, session, { jsonrpc: '2.0', method: 'notifications/initialized' }, fetchImpl);
|
|
81
|
+
const tools = await walkTools(url, auth, session, fetchImpl);
|
|
82
|
+
await endSession(url, auth, session, fetchImpl);
|
|
80
83
|
return tools;
|
|
81
84
|
}
|
|
@@ -2,8 +2,9 @@ import { isDeepStrictEqual } from 'node:util';
|
|
|
2
2
|
import { isRecord } from '@metro-labs/core/is-record';
|
|
3
3
|
import { newId, parseId } from '@metro-labs/core/ids';
|
|
4
4
|
import { ApiError } from '@metro-labs/http/api-error';
|
|
5
|
-
import { connectorAuth, connectorName
|
|
6
|
-
import {
|
|
5
|
+
import { connectorAuth, connectorName } from '@metro-labs/http/connector-input';
|
|
6
|
+
import { readConfig } from './config.js';
|
|
7
|
+
import { parseConnectorUrl } from '@metro-labs/http/connector-url';
|
|
7
8
|
import { normalizePolicy } from '../policy/policy.js';
|
|
8
9
|
import { localImportConnectors, readLocalConnectors, type LocalConnectorRow } from './store.js';
|
|
9
10
|
|
|
@@ -32,7 +32,8 @@ export const targetKey = (target: PolicyTarget): string =>
|
|
|
32
32
|
target.kind === 'channel' ? `channel:${target.station}/${target.account}` : `connector:${target.id}`;
|
|
33
33
|
|
|
34
34
|
export function decide(policy: ToolPolicy | undefined, tool: PolicyTool): Access {
|
|
35
|
-
|
|
35
|
+
const byTool = policy?.tools !== undefined && Object.hasOwn(policy.tools, tool.name) ? policy.tools[tool.name] : undefined;
|
|
36
|
+
return byTool ?? policy?.[tool.group] ?? 'allow';
|
|
36
37
|
}
|
|
37
38
|
|
|
38
39
|
export function strictest(list: readonly Access[]): Access {
|
|
@@ -6,7 +6,8 @@ import { assertAttachOwner, finishGmailUpgrade, startGmailAttach } from './gmail
|
|
|
6
6
|
import { detachWithGmailRevocation } from './gmail-detach.js';
|
|
7
7
|
import { recentSenders } from '../agents/senders.js';
|
|
8
8
|
import { forwardTrainCall } from '../stations/train-call.js';
|
|
9
|
-
import {
|
|
9
|
+
import { publishLocalConnectors } from '../connectors/publication.js';
|
|
10
|
+
import { invalidateOrgConnectorSync, orgConnectorRelaySession, orgConnectorSyncStatus, setOrgConnectorSync } from '../connectors/org-sync-watch.js';
|
|
10
11
|
import type { AgentApiDeps } from '../agents/api.js';
|
|
11
12
|
import { ATTACHABLE, type AccountApiDeps } from '../agents/accounts-api.js';
|
|
12
13
|
import type { SessionApis } from './session-apis.js';
|
|
@@ -14,6 +15,7 @@ import type { ModeInfo } from '@metro-labs/http/mode-api';
|
|
|
14
15
|
import { loadedAgentOf, type AgentBundle, type BundleApiDeps } from '../agents/bundle.js';
|
|
15
16
|
import { METRO_VERSION } from '@metro-labs/core/version';
|
|
16
17
|
import type { ConnectorApiDeps } from '../connectors/api.js';
|
|
18
|
+
import { connectorMigrationPreview } from '../connectors/migration-preview.js';
|
|
17
19
|
import { ConnectorTransfers } from '../connectors/transfer-api.js';
|
|
18
20
|
import { copyConnectorRows } from '../connectors/transfer-store.js';
|
|
19
21
|
import { authenticate } from '../mcp/request-identity.js';
|
|
@@ -121,9 +123,10 @@ function connectorIdsOfLocalAgents(ids: string[]): Promise<Map<string, string[]>
|
|
|
121
123
|
}
|
|
122
124
|
|
|
123
125
|
const connectorApi: ConnectorApiDeps = {
|
|
126
|
+
migrationPreview: () => connectorMigrationPreview(localOwner()),
|
|
124
127
|
listConnectors: async () => {
|
|
125
128
|
const rows = await localListConnectors();
|
|
126
|
-
|
|
129
|
+
publishLocalConnectors(readLocalConnectors());
|
|
127
130
|
return rows;
|
|
128
131
|
},
|
|
129
132
|
createConnector: localCreateConnector,
|
|
@@ -140,6 +143,7 @@ const connectorApi: ConnectorApiDeps = {
|
|
|
140
143
|
|
|
141
144
|
const relayApi: RelayApiDeps = {
|
|
142
145
|
target: (connectorId, force) => localRelayTarget(connectorId, force),
|
|
146
|
+
open: orgConnectorRelaySession,
|
|
143
147
|
identify: (req) => {
|
|
144
148
|
const who = authenticate(req);
|
|
145
149
|
return who?.kind === 'agent' ? { subject: 'agent-key', agentId: who.agentId } : null;
|
|
@@ -201,12 +205,18 @@ export function localSessionApis(deps: LocalModeDeps): SessionApis {
|
|
|
201
205
|
agentApi: agentApi(deps),
|
|
202
206
|
bundleApi: bundleApi(deps),
|
|
203
207
|
connectorApi,
|
|
208
|
+
connectorSyncApi: { status: orgConnectorSyncStatus, set: setOrgConnectorSync },
|
|
204
209
|
connectorTransfers: new ConnectorTransfers({ read: readLocalConnectors, copy: copyConnectorRows }),
|
|
210
|
+
enrollApi: { owner: () => localOwner(), invalidate: invalidateOrgConnectorSync },
|
|
205
211
|
relayApi,
|
|
206
212
|
claudeApi: { liveEvents: deps.liveEvents },
|
|
207
213
|
updateApi: { restart: deps.restart },
|
|
208
214
|
controlApi: { restart: deps.restart, stop: deps.stop },
|
|
209
|
-
ownerApi: { setOwner: (owner) =>
|
|
215
|
+
ownerApi: { setOwner: (owner) => {
|
|
216
|
+
const saved = setLocalOwner(owner);
|
|
217
|
+
invalidateOrgConnectorSync();
|
|
218
|
+
return saved;
|
|
219
|
+
} },
|
|
210
220
|
machineApi: {},
|
|
211
221
|
modelApi: { switchModel: deps.switchModel, owner: localOwner },
|
|
212
222
|
gateway: { config: readModelConfig },
|
|
@@ -8,6 +8,8 @@ import {
|
|
|
8
8
|
} from '../connectors/api.js';
|
|
9
9
|
import type { RelayApiDeps } from '../connectors/relay.js';
|
|
10
10
|
import type { ConnectorTransfers } from '../connectors/transfer-api.js';
|
|
11
|
+
import { handleConnectorSyncRequest, type ConnectorSyncApiDeps } from '../connectors/org-sync-api.js';
|
|
12
|
+
import { handleEnrollRequest, type EnrollApiDeps } from '../connectors/enroll-api.js';
|
|
11
13
|
import { handleClaudeRequest, type ClaudeApiDeps } from '../claude/api.js';
|
|
12
14
|
import type { ModeInfo } from '@metro-labs/http/mode-api';
|
|
13
15
|
import { handleBundleRequest, type BundleApiDeps } from '../agents/bundle.js';
|
|
@@ -37,6 +39,8 @@ export interface SessionApis {
|
|
|
37
39
|
claudeApi?: ClaudeApiDeps;
|
|
38
40
|
connectorApi?: ConnectorApiDeps;
|
|
39
41
|
connectorTransfers?: ConnectorTransfers;
|
|
42
|
+
enrollApi?: EnrollApiDeps;
|
|
43
|
+
connectorSyncApi?: ConnectorSyncApiDeps;
|
|
40
44
|
relayApi?: RelayApiDeps;
|
|
41
45
|
mode?: () => ModeInfo;
|
|
42
46
|
}
|
|
@@ -54,6 +58,8 @@ export function handleSessionApis(
|
|
|
54
58
|
() => handleRunEventsRequest(req, res),
|
|
55
59
|
() => handleApprovalsRequest(req, res),
|
|
56
60
|
...when(apis.connectorTransfers, (d) => d.handle(req, res)),
|
|
61
|
+
...when(apis.enrollApi, (d) => handleEnrollRequest(req, res, d)),
|
|
62
|
+
...when(apis.connectorSyncApi, (d) => handleConnectorSyncRequest(req, res, d)),
|
|
57
63
|
...when(apis.connectorApi, (d) => handleConnectorApiRequest(req, res, d)),
|
|
58
64
|
...when(apis.bundleApi, (d) => handleBundleRequest(req, res, d)),
|
|
59
65
|
...when(apis.updateApi, (d) => handleUpdateRequest(req, res, d)),
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@metro-labs/http",
|
|
3
3
|
"version": "0.1.0",
|
|
4
4
|
"private": true,
|
|
5
|
-
"description": "How a metro server authenticates and answers a request: CORS, the JSON helpers, ApiError, the signed-identity header, the identity registry
|
|
5
|
+
"description": "How a metro server authenticates and answers a request: CORS, the JSON helpers, ApiError, the signed-identity header, the identity registry, AWS request signing, the signature a box puts on its requests to api.metro.box, and the connector client both use (url checks, the MCP initialize, OAuth discovery, registration and tokens). Shared by the daemon and api.metro.box, never by a station.",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"exports": {
|
|
8
8
|
"./cors": "./src/cors.ts",
|
|
@@ -11,7 +11,15 @@
|
|
|
11
11
|
"./return-to": "./src/return-to.ts",
|
|
12
12
|
"./mode-api": "./src/mode-api.ts",
|
|
13
13
|
"./workos-token": "./src/workos-token.ts",
|
|
14
|
-
"./sigv4": "./src/sigv4.ts"
|
|
14
|
+
"./sigv4": "./src/sigv4.ts",
|
|
15
|
+
"./agent-signature": "./src/agent-signature.ts",
|
|
16
|
+
"./connector-url": "./src/connector-url.ts",
|
|
17
|
+
"./connector-reach": "./src/connector-reach.ts",
|
|
18
|
+
"./connector-mcp": "./src/connector-mcp.ts",
|
|
19
|
+
"./connector-discovery": "./src/connector-discovery.ts",
|
|
20
|
+
"./connector-oauth": "./src/connector-oauth.ts",
|
|
21
|
+
"./connector-input": "./src/connector-input.ts",
|
|
22
|
+
"./connector-delivery": "./src/connector-delivery.ts"
|
|
15
23
|
},
|
|
16
24
|
"scripts": {
|
|
17
25
|
"build": "tsc",
|