@stage-labs/metro 0.1.0-beta.303 → 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.
Files changed (61) hide show
  1. package/package.json +1 -1
  2. package/runtime/marketplace/plugin/.claude-plugin/plugin.json +3 -4
  3. package/runtime/marketplace/plugin/METRO.md +2 -0
  4. package/runtime/marketplace/plugin/bin/guard.mjs +2 -1
  5. package/runtime/marketplace/plugin/bin/metro-plugin.mjs +9 -15
  6. package/runtime/marketplace/plugin/bin/policy.mjs +1 -32
  7. package/runtime/node_modules/@metro-labs/core/package.json +0 -1
  8. package/runtime/node_modules/@metro-labs/daemon/src/agent-user/view.ts +4 -1
  9. package/runtime/node_modules/@metro-labs/daemon/src/agents/api.ts +3 -5
  10. package/runtime/node_modules/@metro-labs/daemon/src/approvals/pending.ts +11 -10
  11. package/runtime/node_modules/@metro-labs/daemon/src/boot/boot.ts +16 -9
  12. package/runtime/node_modules/@metro-labs/daemon/src/channels/inbound.ts +6 -3
  13. package/runtime/node_modules/@metro-labs/daemon/src/channels/notice.ts +22 -0
  14. package/runtime/node_modules/@metro-labs/daemon/src/claude/session.ts +1 -3
  15. package/runtime/node_modules/@metro-labs/daemon/src/claude/setup.ts +2 -0
  16. package/runtime/node_modules/@metro-labs/daemon/src/connectors/enroll-api.ts +0 -2
  17. package/runtime/node_modules/@metro-labs/daemon/src/connectors/plugin-sync.ts +11 -51
  18. package/runtime/node_modules/@metro-labs/daemon/src/mcp/index.ts +2 -0
  19. package/runtime/node_modules/@metro-labs/daemon/src/mcp/permission-prompt.ts +22 -15
  20. package/runtime/node_modules/@metro-labs/daemon/src/mcp/permission-relay.ts +6 -4
  21. package/runtime/node_modules/@metro-labs/daemon/src/mcp/policy-snapshot.ts +0 -14
  22. package/runtime/node_modules/@metro-labs/daemon/src/mcp/server-approvals.ts +108 -0
  23. package/runtime/node_modules/@metro-labs/daemon/src/mcp-server/announced.ts +68 -0
  24. package/runtime/node_modules/@metro-labs/daemon/src/mcp-server/config.ts +31 -0
  25. package/runtime/node_modules/@metro-labs/daemon/src/mcp-server/link.ts +186 -0
  26. package/runtime/node_modules/@metro-labs/daemon/src/mcp-server/token.ts +50 -0
  27. package/runtime/node_modules/@metro-labs/daemon/src/policy/policy.ts +6 -5
  28. package/runtime/node_modules/@metro-labs/daemon/src/routes/http.ts +0 -2
  29. package/runtime/node_modules/@metro-labs/daemon/src/routes/local-mode.ts +2 -27
  30. package/runtime/node_modules/@metro-labs/daemon/src/routes/session-apis.ts +0 -5
  31. package/runtime/node_modules/@metro-labs/http/package.json +3 -3
  32. package/runtime/node_modules/@metro-labs/http/src/connector-grant.ts +0 -51
  33. package/runtime/node_modules/@metro-labs/http/src/connector-tools.ts +2 -1
  34. package/runtime/node_modules/@metro-labs/http/src/mcp-server-protocol.ts +81 -0
  35. package/runtime/runtime.json +1 -1
  36. package/runtime/sdk-runner/node_modules/@metro-labs/core/package.json +0 -1
  37. package/runtime/sdk-runner/runner.json +1 -1
  38. package/runtime/node_modules/@metro-labs/core/src/connector-sync.ts +0 -17
  39. package/runtime/node_modules/@metro-labs/daemon/src/connectors/gates.ts +0 -104
  40. package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-adopt.ts +0 -57
  41. package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-relay.ts +0 -61
  42. package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-runtime-discovery.ts +0 -115
  43. package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-runtime.ts +0 -271
  44. package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-sync-api.ts +0 -25
  45. package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-sync-control.ts +0 -173
  46. package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-sync-setting.ts +0 -60
  47. package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-sync-watch.ts +0 -38
  48. package/runtime/node_modules/@metro-labs/daemon/src/connectors/org-sync.ts +0 -208
  49. package/runtime/node_modules/@metro-labs/daemon/src/connectors/publication.ts +0 -40
  50. package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-id.ts +0 -2
  51. package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-policy.ts +0 -90
  52. package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-session.ts +0 -10
  53. package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-target.ts +0 -4
  54. package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay.ts +0 -287
  55. package/runtime/node_modules/@metro-labs/daemon/src/connectors/server-keys.ts +0 -59
  56. package/runtime/node_modules/@metro-labs/daemon/src/connectors/tools.ts +0 -1
  57. package/runtime/node_modules/@metro-labs/http/src/connector-delivery-validation.ts +0 -116
  58. package/runtime/node_modules/@metro-labs/http/src/connector-delivery.ts +0 -19
  59. package/runtime/node_modules/@metro-labs/http/src/connector-migration.ts +0 -178
  60. package/runtime/node_modules/@metro-labs/http/src/transfer-crypto.ts +0 -44
  61. package/runtime/sdk-runner/node_modules/@metro-labs/core/src/connector-sync.ts +0 -17
@@ -1,59 +1,8 @@
1
- import { deliveryRecord } from './connector-delivery-validation.js';
2
- import type { OAuthAuth } from './connector-mcp.js';
3
1
  import { boundOAuthResource, parsePublicConnectorUrl } from './connector-public-url.js';
4
2
 
5
- export interface PortableGrant extends OAuthAuth {
6
- resource: string;
7
- tokenEndpointAuthMethod: 'none' | 'client_secret_post';
8
- }
9
-
10
- const invalid = (): never => { throw new Error('This OAuth grant cannot be transferred. Its client authentication or provider binding is unsupported. Sign in separately.'); };
11
-
12
- function text(raw: unknown, max = 16384): string {
13
- if (typeof raw !== 'string' || raw.length === 0 || raw.length > max || /[^\x21-\x7e]/.test(raw)) return invalid();
14
- return raw;
15
- }
16
-
17
- function endpoint(raw: unknown): string {
18
- const value = text(raw, 2048);
19
- parsePublicConnectorUrl(value);
20
- return value;
21
- }
22
-
23
3
  export function grantResource(target: string, raw: unknown): string {
24
4
  const transport = parsePublicConnectorUrl(target);
25
5
  const resource = parsePublicConnectorUrl(raw);
26
6
  if (resource.toString() === transport.toString() && typeof raw === 'string') return raw;
27
7
  return boundOAuthResource(target, raw);
28
8
  }
29
-
30
- function binding(value: Record<string, unknown>): Pick<PortableGrant, 'clientId' | 'clientSecret' | 'tokenEndpointAuthMethod'> {
31
- const clientSecret = value.clientSecret === undefined ? undefined : text(value.clientSecret);
32
- const method = value.tokenEndpointAuthMethod ?? (clientSecret === undefined ? 'none' : 'client_secret_post');
33
- if (method !== 'none' && method !== 'client_secret_post') return invalid();
34
- if ((method === 'client_secret_post') !== (clientSecret !== undefined)) return invalid();
35
- return { clientId: text(value.clientId), tokenEndpointAuthMethod: method, ...(clientSecret === undefined ? {} : { clientSecret }) };
36
- }
37
-
38
- function expiry(raw: unknown): Pick<PortableGrant, 'expiresAt'> {
39
- if (raw === undefined) return {};
40
- if (typeof raw !== 'number' || !Number.isSafeInteger(raw) || raw < 0) return invalid();
41
- return { expiresAt: raw };
42
- }
43
-
44
- function scopeOf(raw: unknown): Pick<PortableGrant, 'scope'> {
45
- if (raw === undefined) return {};
46
- if (typeof raw !== 'string' || raw.length > 16384 || /[^\x20-\x7e]/.test(raw)) return invalid();
47
- return { scope: raw };
48
- }
49
-
50
- export function portableGrant(raw: unknown, target: string): PortableGrant {
51
- const value = deliveryRecord(raw, ['kind', 'accessToken', 'refreshToken', 'expiresAt', 'clientId', 'clientSecret', 'issuer', 'tokenEndpoint', 'scope', 'resource', 'tokenEndpointAuthMethod']);
52
- if (value.kind !== 'oauth') return invalid();
53
- return {
54
- kind: 'oauth', accessToken: text(value.accessToken, 16377), issuer: endpoint(value.issuer), tokenEndpoint: endpoint(value.tokenEndpoint),
55
- resource: grantResource(target, value.resource ?? parsePublicConnectorUrl(target).toString()),
56
- ...binding(value), ...expiry(value.expiresAt), ...scopeOf(value.scope),
57
- ...(value.refreshToken === undefined ? {} : { refreshToken: text(value.refreshToken) }),
58
- };
59
- }
@@ -9,6 +9,7 @@ export interface RemoteTool {
9
9
  title: string;
10
10
  description: string;
11
11
  readOnly: boolean;
12
+ inputSchema?: Record<string, unknown>;
12
13
  }
13
14
 
14
15
  const TIMEOUT_MS = 15_000;
@@ -39,7 +40,7 @@ function toTool(raw: unknown): RemoteTool | null {
39
40
  if (!isRecord(raw) || typeof raw.name !== 'string') return null;
40
41
  const annotations = isRecord(raw.annotations) ? raw.annotations : {};
41
42
  const title = typeof raw.title === 'string' && raw.title !== '' ? raw.title : raw.name;
42
- return { name: raw.name, title, description: typeof raw.description === 'string' ? raw.description : '', readOnly: annotations.readOnlyHint === true };
43
+ return { name: raw.name, title, description: typeof raw.description === 'string' ? raw.description : '', readOnly: annotations.readOnlyHint === true, ...(isRecord(raw.inputSchema) ? { inputSchema: raw.inputSchema } : {}) };
43
44
  }
44
45
 
45
46
  async function endSession(url: URL, auth: ConnectorAuth, session: Map<string, string>, fetchImpl?: FetchLike): Promise<void> {
@@ -0,0 +1,81 @@
1
+ import { isRecord } from '@metro-labs/core/is-record';
2
+
3
+ export const MCP_PATHS = { token: '/v1/token', approvals: '/v1/approvals', answer: '/v1/approvals/answer' } as const;
4
+ export const MCP_FIND_DEFAULT = 5;
5
+ export const MCP_FIND_MAX = 20;
6
+ export const MCP_CALLS_MAX = 10;
7
+ export const MCP_QUERY_MAX = 500;
8
+ export const MCP_APPROVAL_WAIT_MS = 25_000;
9
+ export const MCP_APPROVAL_ID_RE = /^[a-km-z]{5}$/;
10
+ export const MCP_TOKEN_RE = /^mcp_[A-Za-z0-9_-]{43}$/;
11
+
12
+ export type McpErrorCode = 'blocked' | 'needs_approval' | 'denied' | 'not_found' | 'signin' | 'unavailable' | 'vendor_error' | 'invalid';
13
+
14
+ export interface McpFindRequest {
15
+ query: string;
16
+ max_results: number;
17
+ }
18
+
19
+ export interface McpCall {
20
+ tool: string;
21
+ arguments: Record<string, unknown>;
22
+ approval?: string;
23
+ }
24
+
25
+ export interface McpApproval {
26
+ id: string;
27
+ connector: string;
28
+ tool: string;
29
+ preview: string;
30
+ requestedAt: number;
31
+ }
32
+
33
+ export type McpDecision = 'allow' | 'deny';
34
+
35
+ const TOOL_RE = /^([a-z0-9][a-z0-9-]{0,62})\.([A-Za-z0-9_][A-Za-z0-9_.:/-]{0,127})$/;
36
+
37
+ export function splitMcpTool(name: string): { connector: string; tool: string } | null {
38
+ const match = TOOL_RE.exec(name);
39
+ return match === null ? null : { connector: match[1] ?? '', tool: match[2] ?? '' };
40
+ }
41
+
42
+ export function mcpFindRequestOf(raw: unknown): McpFindRequest {
43
+ if (!isRecord(raw) || typeof raw.query !== 'string' || raw.query.trim() === '' || raw.query.length > MCP_QUERY_MAX) throw new Error('find_tools needs a query of 1 to 500 characters.');
44
+ const wanted = raw.max_results ?? MCP_FIND_DEFAULT;
45
+ if (typeof wanted !== 'number' || !Number.isInteger(wanted) || wanted < 1) throw new Error('max_results must be a whole number from 1.');
46
+ return { query: raw.query.trim(), max_results: Math.min(wanted, MCP_FIND_MAX) };
47
+ }
48
+
49
+ function callOf(raw: unknown): McpCall {
50
+ if (!isRecord(raw) || typeof raw.tool !== 'string' || splitMcpTool(raw.tool) === null) throw new Error('Each call needs a tool named <connector>.<tool>, as find_tools returns it.');
51
+ const args = raw.arguments ?? {};
52
+ if (!isRecord(args)) throw new Error(`The arguments of ${raw.tool} must be an object.`);
53
+ if (raw.approval !== undefined && (typeof raw.approval !== 'string' || !MCP_APPROVAL_ID_RE.test(raw.approval))) throw new Error('approval must be the 5-letter id the approval request gave.');
54
+ return { tool: raw.tool, arguments: args, ...(typeof raw.approval === 'string' ? { approval: raw.approval } : {}) };
55
+ }
56
+
57
+ export function mcpCallsOf(raw: unknown): McpCall[] {
58
+ const calls = isRecord(raw) ? raw.calls : undefined;
59
+ if (!Array.isArray(calls) || calls.length === 0 || calls.length > MCP_CALLS_MAX) throw new Error(`call_tool needs 1 to ${String(MCP_CALLS_MAX)} calls.`);
60
+ return calls.map(callOf);
61
+ }
62
+
63
+ export function mcpTokenHashOf(raw: unknown): string {
64
+ if (!isRecord(raw) || typeof raw.tokenHash !== 'string' || !/^[A-Za-z0-9_-]{43}$/.test(raw.tokenHash)) throw new Error('tokenHash must be the base64url SHA-256 of the MCP token.');
65
+ return raw.tokenHash;
66
+ }
67
+
68
+ function approvalOf(raw: unknown): McpApproval | null {
69
+ if (!isRecord(raw) || typeof raw.id !== 'string' || !MCP_APPROVAL_ID_RE.test(raw.id) || typeof raw.connector !== 'string' || typeof raw.tool !== 'string' || typeof raw.preview !== 'string' || typeof raw.requestedAt !== 'number') return null;
70
+ return { id: raw.id, connector: raw.connector, tool: raw.tool, preview: raw.preview, requestedAt: raw.requestedAt };
71
+ }
72
+
73
+ export function mcpApprovalsOf(raw: unknown): McpApproval[] {
74
+ if (!isRecord(raw) || !Array.isArray(raw.approvals)) throw new Error('Mcp sent an unreadable approval list.');
75
+ return raw.approvals.map(approvalOf).filter((row): row is McpApproval => row !== null);
76
+ }
77
+
78
+ export function mcpAnswerOf(raw: unknown): { id: string; decision: McpDecision } {
79
+ if (!isRecord(raw) || typeof raw.id !== 'string' || !MCP_APPROVAL_ID_RE.test(raw.id) || (raw.decision !== 'allow' && raw.decision !== 'deny')) throw new Error('An answer needs the approval id and allow or deny.');
80
+ return { id: raw.id, decision: raw.decision };
81
+ }
@@ -1,3 +1,3 @@
1
1
  {
2
- "version": "0.1.0-beta.303"
2
+ "version": "0.1.0-beta.305"
3
3
  }
@@ -6,7 +6,6 @@
6
6
  "type": "module",
7
7
  "exports": {
8
8
  "./log": "./src/log.ts",
9
- "./connector-sync": "./src/connector-sync.ts",
10
9
  "./events": "./src/events.ts",
11
10
  "./is-record": "./src/is-record.ts",
12
11
  "./runner-activity": "./src/runner-activity.ts",
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.1.0-beta.303",
2
+ "version": "0.1.0-beta.305",
3
3
  "dependencies": {
4
4
  "@anthropic-ai/claude-agent-sdk": "0.3.287",
5
5
  "@modelcontextprotocol/sdk": "^1.29.0",
@@ -1,17 +0,0 @@
1
- export interface ConnectorSyncReceipt {
2
- state: 'waiting' | 'received' | 'expired' | 'unavailable';
3
- lastReceivedAt: number | null;
4
- leaseExpiresAt: number | null;
5
- counts: { total: number; ready: number; unavailable: number } | null;
6
- }
7
-
8
- export interface ConnectorSyncStatus extends Omit<ConnectorSyncReceipt, 'state'> {
9
- enabled: boolean;
10
- source: 'saved' | 'environment' | 'default' | 'invalid' | 'scope_changed';
11
- enrolled: boolean;
12
- owner: string | null;
13
- agentId: string | null;
14
- scope: string;
15
- liveEnabled: boolean;
16
- state: 'off' | 'not_enrolled' | ConnectorSyncReceipt['state'];
17
- }
@@ -1,104 +0,0 @@
1
- import type { ToolGroup } from '@metro-labs/core/stations/types';
2
- import { decide, policyFor, setPolicies, type PolicyTarget, type ToolPolicy } from '../policy/policy.js';
3
- import { serverKeysOf } from './plugin-sync.js';
4
- import { NEEDS_APPROVAL, revokeToolPrompts, settlePrompts, takeGrant } from '../approvals/pending.js';
5
- import type { BlockedReason } from './relay-policy.js';
6
-
7
- export interface GateRow {
8
- id: string;
9
- name: string;
10
- config: { policy?: ToolPolicy; toolGroups?: Record<string, ToolGroup> };
11
- }
12
-
13
- export interface ConnectorGate {
14
- id: string;
15
- name: string;
16
- server: string;
17
- policy: ToolPolicy;
18
- tools: Record<string, ToolGroup>;
19
- }
20
-
21
- let gates: ConnectorGate[] = [];
22
-
23
- const normalized = (name: string): string => name.replace(/[^a-zA-Z0-9_-]/g, '_');
24
-
25
- function approvalChanged(gate: ConnectorGate, tool: string, next: GateRow | undefined, keys: ReadonlyMap<string, string>): boolean {
26
- if (next === undefined || gate.server !== keys.get(gate.id) || JSON.stringify([gate.name, gate.policy]) !== JSON.stringify([next.name, next.config.policy ?? {}])) return true;
27
- const before = decide(gate.policy, { name: tool, group: toolGroupOf(gate, tool) });
28
- const after = decide(next.config.policy, { name: tool, group: next.config.toolGroups?.[tool] === 'read' ? 'read' : 'write' });
29
- return before !== after;
30
- }
31
-
32
- export function registerConnectors(rows: readonly GateRow[], keys: ReadonlyMap<string, string> = serverKeysOf([...rows])): void {
33
- const next = rows.map((row) => ({
34
- id: row.id,
35
- name: row.name,
36
- server: keys.get(row.id) ?? '',
37
- policy: row.config.policy ?? {},
38
- tools: row.config.toolGroups ?? {},
39
- }));
40
- revokeToolPrompts((tool) => {
41
- const before = connectorToolOf(tool);
42
- const after = connectorToolOf(tool, next);
43
- if (before?.gate.id !== after?.gate.id) return true;
44
- return before !== undefined && approvalChanged(before.gate, before.tool, rows.find((row) => row.id === before.gate.id), keys);
45
- });
46
- gates = next;
47
- const entries = rows.flatMap((row): [PolicyTarget, ToolPolicy][] =>
48
- row.config.policy === undefined ? [] : [[{ kind: 'connector', id: row.id }, row.config.policy]],
49
- );
50
- setPolicies('connector', entries);
51
- }
52
-
53
- export const connectorGates = (): readonly ConnectorGate[] => gates;
54
-
55
- export const toolGroupOf = (gate: ConnectorGate, tool: string): ToolGroup => (gate.tools[tool] === 'read' ? 'read' : 'write');
56
-
57
- const promptFor = (connectorId: string, tool: string) => (asked: string): boolean => {
58
- const found = connectorToolOf(asked);
59
- return found?.gate.id === connectorId && found.tool === tool;
60
- };
61
-
62
- export function blockedReason(connectorId: string, tool: string, args: Record<string, unknown> = {}): string | null {
63
- const gate = gates.find((g) => g.id === connectorId);
64
- if (gate === undefined) return 'This connector is not available.';
65
- settlePrompts(promptFor(connectorId, tool), args);
66
- const access = policyFor({ kind: 'connector', id: connectorId }, { name: tool, group: toolGroupOf(gate, tool) });
67
- if (access === 'deny') return `Blocked by the owner's policy for ${gate.name} (${tool}).`;
68
- if (access === 'allow' || takeGrant(promptFor(connectorId, tool), args)) return null;
69
- return NEEDS_APPROVAL(gate.name, tool);
70
- }
71
-
72
- export function requestConnectorBlocker(): () => BlockedReason {
73
- const permits = new Map<string, number>();
74
- return () => {
75
- const seen = new Map<string, number>();
76
- return (id, tool, args) => {
77
- const gate = gates.find((entry) => entry.id === id);
78
- if (gate === undefined) return 'This organization connector is not available.';
79
- const group = toolGroupOf(gate, tool);
80
- const access = policyFor({ kind: 'connector', id }, { name: tool, group });
81
- if (access !== 'ask') return blockedReason(id, tool, args);
82
- const key = JSON.stringify([id, gate.server, gate.policy, group, tool, args]);
83
- const occurrence = seen.get(key) ?? 0;
84
- seen.set(key, occurrence + 1);
85
- if (occurrence < (permits.get(key) ?? 0)) return null;
86
- const reason = blockedReason(id, tool, args);
87
- if (reason === null) permits.set(key, (permits.get(key) ?? 0) + 1);
88
- return reason;
89
- };
90
- };
91
- }
92
-
93
- function resolvedTool(gate: ConnectorGate, called: string): { gate: ConnectorGate; tool: string } {
94
- const known = [...Object.keys(gate.tools), ...Object.keys(gate.policy.tools ?? {})];
95
- return { gate, tool: known.find((name) => name === called) ?? known.find((name) => normalized(name) === called) ?? called };
96
- }
97
-
98
- export function connectorToolOf(toolName: string, entries: readonly ConnectorGate[] = gates): { gate: ConnectorGate; tool: string } | undefined {
99
- if (toolName.startsWith('mcp__metro__')) return undefined;
100
- for (const gate of entries)
101
- for (const prefix of [`mcp__plugin_metro_${gate.server}__`, `mcp__${gate.server}__`])
102
- if (toolName.startsWith(prefix) && toolName.length > prefix.length) return resolvedTool(gate, toolName.slice(prefix.length));
103
- return undefined;
104
- }
@@ -1,57 +0,0 @@
1
- import { readFileSync } from 'node:fs';
2
- import { join } from 'node:path';
3
- import { isRecord } from '@metro-labs/core/is-record';
4
- import { errMsg, log } from '@metro-labs/core/log';
5
- import { agentsDir } from '../agents/files.js';
6
- import { isOrgRelayId, orgRelayId } from './relay-id.js';
7
- import { readServerKeys, writeServerKeys } from './server-keys.js';
8
-
9
- interface Alias {
10
- sourceId: string;
11
- relayId: string;
12
- }
13
-
14
- function recordsOf(dir: string): unknown[] {
15
- let raw: unknown;
16
- try {
17
- raw = JSON.parse(readFileSync(join(dir, 'connector-migrations.json'), 'utf8'));
18
- } catch (err) {
19
- if (isRecord(err) && err.code === 'ENOENT') return [];
20
- log.warn({ err: errMsg(err) }, 'connectors: the old migration records could not be read; server keys left as they are');
21
- return [];
22
- }
23
- if (isRecord(raw) && raw.version === 1 && Array.isArray(raw.records)) return raw.records;
24
- log.warn('connectors: the old migration records are not valid; server keys left as they are');
25
- return [];
26
- }
27
-
28
- function aliasOf(record: unknown): Alias[] {
29
- if (!isRecord(record) || record.phase !== 'active' || typeof record.sourceId !== 'string' || typeof record.destinationId !== 'string') return [];
30
- const relayId = orgRelayId(record.destinationId);
31
- return isOrgRelayId(relayId) && record.sourceId !== relayId ? [{ sourceId: record.sourceId, relayId }] : [];
32
- }
33
-
34
- export function adoptOrgServerKeys(dir = agentsDir()): number {
35
- try {
36
- const aliases = recordsOf(dir).flatMap(aliasOf);
37
- if (aliases.length === 0) return 0;
38
- const keys = readServerKeys(dir);
39
- if (keys === undefined) return 0;
40
- let moved = 0;
41
- for (const { sourceId, relayId } of aliases) {
42
- const key = keys.get(sourceId);
43
- if (key === undefined || keys.has(relayId)) continue;
44
- keys.delete(sourceId);
45
- keys.set(relayId, key);
46
- moved += 1;
47
- }
48
- if (moved > 0) {
49
- writeServerKeys(keys, dir);
50
- log.info({ moved }, 'connectors: server keys moved to organization connector ids, tool names unchanged');
51
- }
52
- return moved;
53
- } catch (err) {
54
- log.warn({ err: errMsg(err) }, 'connectors: the server keys could not be moved to organization connector ids');
55
- return 0;
56
- }
57
- }
@@ -1,61 +0,0 @@
1
- import { isRecord } from '@metro-labs/core/is-record';
2
- import { ApiError } from '@metro-labs/http/api-error';
3
- import { screenCalls, type Screened } from './relay-policy.js';
4
- import type { RelaySession } from './relay-session.js';
5
- import type { RelayTarget } from './relay-target.js';
6
-
7
- type Ready = Extract<RelayTarget, { kind: 'ok' }>;
8
- type Forward = (target: Ready, body: Uint8Array<ArrayBuffer> | null, signal: AbortSignal) => Promise<Response>;
9
- type OrgExchange =
10
- | { kind: 'response'; upstream: Response; answers: Record<string, unknown>[] }
11
- | { kind: 'blocked'; screened: Screened };
12
-
13
- const METHODS = new Set(['initialize', 'ping', 'tools/list', 'tools/call', 'notifications/initialized', 'notifications/cancelled']);
14
-
15
- function requestBatch(body: Uint8Array | null): boolean {
16
- if (body === null) return false;
17
- let parsed: unknown;
18
- try { parsed = JSON.parse(new TextDecoder().decode(body)); }
19
- catch { throw new ApiError('Invalid organization connector request.', 400); }
20
- const batch = Array.isArray(parsed);
21
- const entries: unknown[] = Array.isArray(parsed) ? parsed : [parsed];
22
- if (entries.length === 0 || entries.some((item) => !validMessage(item))) throw new ApiError('This organization connector accepts MCP tool requests only.', 400);
23
- return batch;
24
- }
25
-
26
- function validMessage(item: unknown): boolean {
27
- if (!isRecord(item) || item.jsonrpc !== '2.0' || typeof item.method !== 'string' || !METHODS.has(item.method)) return false;
28
- if (item.method !== 'tools/call') return true;
29
- if (!isRecord(item.params) || typeof item.params.name !== 'string' || item.params.name === '') return false;
30
- return item.params.arguments === undefined || isRecord(item.params.arguments);
31
- }
32
-
33
- function ready(session: RelaySession, signal: AbortSignal): Ready {
34
- if (signal.aborted) throw new ApiError('This organization connector is no longer available.', 503);
35
- const target = session.current();
36
- if (target.kind === 'missing') throw new ApiError('No such organization connector.', 404);
37
- if (target.kind === 'signin') throw new ApiError('This organization connector needs signing in again.', 424);
38
- return target;
39
- }
40
-
41
- export async function exchangeOrg(id: string, session: RelaySession, body: Uint8Array<ArrayBuffer> | null, clientSignal: AbortSignal, forward: Forward): Promise<OrgExchange> {
42
- const signal = AbortSignal.any([clientSignal, session.signal]);
43
- const batch = requestBatch(body);
44
- let pending = body;
45
- const answers: Record<string, unknown>[] = [];
46
- for (let attempt = 0; attempt < 2; attempt += 1) {
47
- await session.target(attempt > 0);
48
- ready(session, signal);
49
- const screened = pending === null ? null : screenCalls(pending, id, session.blocker());
50
- if (screened !== null) {
51
- answers.push(...screened.answers);
52
- pending = screened.forward;
53
- if (pending === null) return { kind: 'blocked', screened: { forward: null, answers, batch } };
54
- }
55
- const target = ready(session, signal);
56
- const upstream = await forward(target, pending, signal);
57
- if (upstream.status !== 401 && upstream.status !== 403) return { kind: 'response', upstream, answers };
58
- await upstream.body?.cancel();
59
- }
60
- throw new ApiError('The organization connector rejected its credential. Check its sign-in in the organization settings.', 424);
61
- }
@@ -1,115 +0,0 @@
1
- import type { FetchLike } from '@metro-labs/core/stations/oauth';
2
- import type { ToolGroup } from '@metro-labs/core/stations/types';
3
- import { ApiError } from '@metro-labs/http/api-error';
4
- import { validConnectorToolName, type DeliveredConnector } from '@metro-labs/http/connector-delivery';
5
- import { MetroApiRefusal } from './metro-api.js';
6
- import { listRemoteTools } from './tools.js';
7
-
8
- export const orgUnavailable = (): ApiError => new ApiError('This organization connector is not available. Try again.', 503);
9
- export const orgSignin = (): ApiError => new ApiError('This organization connector needs sign-in again.', 424);
10
- export const orgMissing = (): ApiError => new ApiError('No such organization connector is assigned to this agent.', 404);
11
-
12
- export function orgRuntimeError(error: unknown): ApiError {
13
- if (error instanceof MetroApiRefusal) return error.upstreamStatus === 401 || error.upstreamStatus === 403 ? orgSignin() : orgUnavailable();
14
- const status = error instanceof ApiError ? error.status : 503;
15
- if (status === 401 || status === 403 || status === 424) return orgSignin();
16
- if (status === 404) return orgMissing();
17
- if (status === 502) return new ApiError('The organization connector could not complete the request.', 502);
18
- return orgUnavailable();
19
- }
20
-
21
- interface DiscoveryGuard {
22
- fresh: (revision?: string) => Promise<DeliveredConnector>;
23
- current: () => DeliveredConnector;
24
- signal: AbortSignal;
25
- fetch: FetchLike;
26
- }
27
-
28
- const MAX_BYTES = 2 * 1024 * 1024;
29
- const MAX_TOOLS = 2000;
30
-
31
- async function boundedResponse(response: Response, signal: AbortSignal): Promise<Response> {
32
- const reader = response.body?.getReader();
33
- if (reader === undefined) return response;
34
- const chunks: Uint8Array[] = [];
35
- let size = 0;
36
- try {
37
- for (let part = await reader.read(); !part.done; part = await reader.read()) {
38
- signal.throwIfAborted();
39
- size += part.value.length;
40
- if (size > MAX_BYTES) throw orgUnavailable();
41
- chunks.push(part.value);
42
- }
43
- return new Response(Buffer.concat(chunks), { status: response.status, headers: response.headers });
44
- } finally {
45
- await reader.cancel();
46
- }
47
- }
48
-
49
- function requestInit(row: DeliveredConnector, init: RequestInit | undefined, scanSignal: AbortSignal): RequestInit & { signal: AbortSignal } {
50
- const headers = new Headers(init?.headers);
51
- for (const [name, value] of Object.entries(row.headers)) headers.set(name, value);
52
- const signals = [scanSignal, AbortSignal.timeout(15_000)];
53
- if (init?.signal !== undefined && init.signal !== null) signals.push(init.signal);
54
- return { ...init, headers, redirect: 'manual', signal: AbortSignal.any(signals) };
55
- }
56
-
57
- async function discoveryResponse(response: Response, init: RequestInit | undefined, signal: AbortSignal): Promise<Response> {
58
- if (response.status >= 300) throw new ApiError('The organization connector refused tool discovery.', 502);
59
- if (init?.method === 'DELETE') {
60
- await boundedResponse(response, signal);
61
- return new Response(null, { status: response.status });
62
- }
63
- return boundedResponse(response, signal);
64
- }
65
-
66
- async function guardedRequest(input: string, init: RequestInit | undefined, guard: DiscoveryGuard, budget: { refreshed: boolean }): Promise<Response> {
67
- let revision: string | undefined;
68
- for (let attempt = 0; attempt < 2; attempt += 1) {
69
- guard.signal.throwIfAborted();
70
- await guard.fresh(revision);
71
- guard.signal.throwIfAborted();
72
- const row = guard.current();
73
- if (input !== new URL(row.url).toString()) throw orgUnavailable();
74
- const options = requestInit(row, init, guard.signal);
75
- const response = await guard.fetch(input, options);
76
- try {
77
- guard.current();
78
- options.signal.throwIfAborted();
79
- if (response.status === 401 || response.status === 403) {
80
- if (row.auth !== 'oauth' || budget.refreshed) throw orgSignin();
81
- budget.refreshed = true;
82
- revision = row.revision;
83
- continue;
84
- }
85
- return await discoveryResponse(response, init, options.signal);
86
- } finally {
87
- if (!response.bodyUsed) await response.body?.cancel();
88
- }
89
- }
90
- throw orgSignin();
91
- }
92
-
93
- export async function discoverOrgGroups(url: string, guard: DiscoveryGuard): Promise<Record<string, ToolGroup>> {
94
- try {
95
- const failure: { error: ApiError | null } = { error: null };
96
- const budget = { refreshed: false };
97
- const send: FetchLike = async (input, init) => {
98
- try {
99
- return await guardedRequest(input, init, guard, budget);
100
- } catch (error) {
101
- const safe = orgRuntimeError(error);
102
- if (init?.method !== 'DELETE' || safe.status !== 502) failure.error = safe;
103
- throw safe;
104
- }
105
- };
106
- const tools = await listRemoteTools(new URL(url), { kind: 'none' }, send);
107
- if (failure.error !== null) throw failure.error;
108
- guard.current();
109
- guard.signal.throwIfAborted();
110
- if (tools.length > MAX_TOOLS || tools.some((tool) => !validConnectorToolName(tool.name))) throw orgUnavailable();
111
- return Object.fromEntries(tools.map((tool) => [tool.name, tool.readOnly ? 'read' : 'write']));
112
- } catch (error) {
113
- throw orgRuntimeError(error);
114
- }
115
- }