@stage-labs/metro 0.1.0-beta.8 → 0.1.0-beta.80
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api.js +14 -0
- package/dist/claude-settings.js +40 -0
- package/dist/claude.js +107 -0
- package/dist/cli.js +101 -0
- package/dist/control.js +74 -0
- package/dist/hold.js +211 -0
- package/dist/local.js +93 -0
- package/dist/node-name.js +71 -0
- package/dist/plugin.js +80 -0
- package/dist/provider-flags.js +9 -0
- package/dist/runtime-install.js +87 -0
- package/dist/runtime.js +74 -0
- package/dist/serve.js +146 -0
- package/dist/service.js +242 -0
- package/dist/tail.js +119 -0
- package/dist/update.js +45 -0
- package/dist/version.js +97 -0
- package/package.json +14 -25
- package/runtime/node_modules/@metro-labs/core/package.json +43 -0
- package/runtime/node_modules/@metro-labs/core/src/address.ts +6 -0
- package/runtime/node_modules/@metro-labs/core/src/endpoints.ts +7 -0
- package/runtime/node_modules/@metro-labs/core/src/events.ts +120 -0
- package/runtime/node_modules/@metro-labs/core/src/identity.ts +7 -0
- package/runtime/node_modules/@metro-labs/core/src/ids.ts +21 -0
- package/runtime/node_modules/@metro-labs/core/src/is-record.ts +2 -0
- package/runtime/node_modules/@metro-labs/core/src/lines.ts +82 -0
- package/runtime/node_modules/@metro-labs/core/src/log.ts +42 -0
- package/runtime/node_modules/@metro-labs/core/src/protocol.ts +226 -0
- package/runtime/node_modules/@metro-labs/core/src/secure-fs.ts +57 -0
- package/runtime/node_modules/@metro-labs/core/src/station-names.ts +12 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/account-store.ts +60 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/attachments.ts +196 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/messaging-normalize.ts +88 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/station-runtime.ts +64 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/types.ts +107 -0
- package/runtime/node_modules/@metro-labs/core/src/str.ts +5 -0
- package/runtime/node_modules/@metro-labs/core/src/train-error.ts +53 -0
- package/runtime/node_modules/@metro-labs/core/src/version.ts +2 -0
- package/runtime/node_modules/@metro-labs/daemon/package.json +47 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/account-attach.ts +19 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/accounts-api.ts +311 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/admin.ts +43 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/api.ts +302 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/bundle.ts +111 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/file-admin.ts +329 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/files.ts +155 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/keys.ts +45 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/map.ts +84 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/scope.ts +64 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/boot.ts +176 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/connect-hint.ts +39 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/crash-guard.ts +49 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/local-owner.ts +14 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/paths.ts +106 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/addressed.ts +25 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/dedupe.ts +24 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/inbound.ts +377 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/media-note.ts +85 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/pending.ts +75 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/relay.ts +129 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/webhook-note.ts +51 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/api.ts +148 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/files.ts +329 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/login.ts +167 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/settings.ts +117 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/api.ts +307 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/cli-api.ts +63 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/config.ts +144 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/json.ts +21 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/model.ts +69 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-client.ts +159 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-discovery.ts +117 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-pending.ts +65 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-routes.ts +157 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth.ts +127 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-target.ts +39 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay.ts +295 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/store.ts +277 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/tools.ts +98 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/url.ts +70 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/verify.ts +330 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-grant.ts +99 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-owner.ts +32 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-serve.ts +121 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/upload-api.ts +294 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/upload-store.ts +181 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/bedrock.ts +252 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-auth.ts +231 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-device.ts +91 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-stream.ts +288 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-translate.ts +203 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex.ts +220 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/eventstream.ts +123 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/forward.ts +182 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gateway.ts +197 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-api.ts +195 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-config.ts +198 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/openrouter.ts +40 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/served.ts +17 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/accounts.ts +137 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/call-tools.ts +216 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/channel-owner.ts +29 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/ctx.ts +62 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/event-store.ts +91 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/frame-scope.ts +21 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/group-tools.ts +129 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/index.ts +191 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/member-tools.ts +72 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/permission-relay.ts +66 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/raw-get-stream.ts +207 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/request-identity.ts +47 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/session-registry.ts +172 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/session-route.ts +43 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/session.ts +215 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-dispatch.ts +215 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-schemas.ts +400 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/upload-tool.ts +66 -0
- package/runtime/node_modules/@metro-labs/daemon/src/monitor/api.ts +237 -0
- package/runtime/node_modules/@metro-labs/daemon/src/net/tunnel.ts +389 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/http.ts +390 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/identity.ts +54 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/local-mode.ts +187 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/session-apis.ts +60 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/session.ts +29 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/control.ts +64 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/machine.ts +61 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/update.ts +116 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server.ts +2 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-inline.ts +100 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-interactive.ts +161 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-resolve.ts +202 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-session.ts +280 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-xmtp.ts +111 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach.ts +184 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/materialize.ts +248 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/registry.ts +35 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/runtime-deps.ts +82 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/supervisor-io.ts +97 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/supervisor.ts +320 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/train-call.ts +22 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/api.ts +71 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/socket.ts +123 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/tickets.ts +40 -0
- package/runtime/node_modules/@metro-labs/discord-bot/package.json +28 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/accounts.ts +118 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/actions.ts +388 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/api-base.ts +1 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/attachments.ts +29 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/format.ts +220 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/group-actions.ts +50 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/group.ts +115 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/index.ts +127 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/members.ts +215 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/send-files.ts +43 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/station.ts +19 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/verify.ts +82 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/voice-speak.ts +195 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/voice-transcribe.ts +292 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/voice.ts +197 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/wire.ts +5 -0
- package/runtime/node_modules/@metro-labs/http/package.json +33 -0
- package/runtime/node_modules/@metro-labs/http/src/api-error.ts +8 -0
- package/runtime/node_modules/@metro-labs/http/src/api-http.ts +95 -0
- package/runtime/node_modules/@metro-labs/http/src/cors.ts +29 -0
- package/runtime/node_modules/@metro-labs/http/src/identity-key.ts +33 -0
- package/runtime/node_modules/@metro-labs/http/src/identity-registry.ts +13 -0
- package/runtime/node_modules/@metro-labs/http/src/mode-api.ts +32 -0
- package/runtime/node_modules/@metro-labs/http/src/return-to.ts +21 -0
- package/runtime/node_modules/@metro-labs/http/src/signed-identity.ts +36 -0
- package/runtime/node_modules/@metro-labs/telegram/package.json +26 -0
- package/runtime/node_modules/@metro-labs/telegram/src/accounts.ts +80 -0
- package/runtime/node_modules/@metro-labs/telegram/src/actions.ts +254 -0
- package/runtime/node_modules/@metro-labs/telegram/src/attachments.ts +87 -0
- package/runtime/node_modules/@metro-labs/telegram/src/client.ts +77 -0
- package/runtime/node_modules/@metro-labs/telegram/src/format.ts +156 -0
- package/runtime/node_modules/@metro-labs/telegram/src/group.ts +261 -0
- package/runtime/node_modules/@metro-labs/telegram/src/history.ts +52 -0
- package/runtime/node_modules/@metro-labs/telegram/src/inbound.ts +78 -0
- package/runtime/node_modules/@metro-labs/telegram/src/index.ts +46 -0
- package/runtime/node_modules/@metro-labs/telegram/src/login.ts +166 -0
- package/runtime/node_modules/@metro-labs/telegram/src/media-actions.ts +80 -0
- package/runtime/node_modules/@metro-labs/telegram/src/members.ts +75 -0
- package/runtime/node_modules/@metro-labs/telegram/src/normalize.ts +22 -0
- package/runtime/node_modules/@metro-labs/telegram/src/reactions.ts +167 -0
- package/runtime/node_modules/@metro-labs/telegram/src/station.ts +24 -0
- package/runtime/node_modules/@metro-labs/telegram/src/types.ts +7 -0
- package/runtime/node_modules/@metro-labs/telegram/src/wire.ts +1 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/package.json +25 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/accounts.ts +120 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/actions.ts +295 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/attachments.ts +128 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/format.ts +201 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/index.ts +109 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/media-actions.ts +166 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/members.ts +47 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/station.ts +17 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/types.ts +58 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/verify.ts +38 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/wire.ts +3 -0
- package/runtime/node_modules/@metro-labs/webhook/package.json +23 -0
- package/runtime/node_modules/@metro-labs/webhook/src/station.ts +37 -0
- package/runtime/node_modules/@metro-labs/whatsapp/package.json +27 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/accounts.ts +66 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/ack.ts +101 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/actions.ts +178 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/attachments.ts +72 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/auth-state.ts +129 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/client.ts +327 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/delivery.ts +49 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/format.ts +138 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/inbound.ts +57 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/index.ts +40 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/keys.ts +59 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/logger.ts +60 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/login.ts +176 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/media.ts +84 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/normalize.ts +22 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/outbox.ts +38 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/parse.ts +185 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/station.ts +17 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/token-store.ts +98 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/types.ts +6 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/wire.ts +5 -0
- package/runtime/node_modules/@metro-labs/xmtp/package.json +33 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/accounts.ts +112 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-close.ts +110 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-conv.ts +299 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-meta.ts +157 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-push.ts +87 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions.ts +375 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/attachments.ts +107 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/codecs.ts +171 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/conv-helpers.ts +239 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit-core.ts +55 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit-payloads.ts +335 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit.ts +80 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/group-members.ts +34 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/group.ts +56 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/identity.ts +33 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/index.ts +125 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/labels.ts +157 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/member-args.ts +40 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/push.ts +349 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/station.ts +13 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/tools-handlers.ts +126 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/tools.ts +212 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/transcribe.ts +90 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/verify.ts +84 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/wire.ts +53 -0
- package/runtime/runtime.json +3 -0
- package/runtime/server.ts +1 -0
- package/runtime/stations.json +36 -0
- package/runtime/trains/.keep +0 -0
- package/LICENSE +0 -21
- package/README.md +0 -249
- package/dist/cache.js +0 -75
- package/dist/cli/actions.js +0 -139
- package/dist/cli/config.js +0 -185
- package/dist/cli/index.js +0 -177
- package/dist/cli/skill.js +0 -62
- package/dist/cli/util.js +0 -72
- package/dist/cli/webhook.js +0 -83
- package/dist/codex-rc.js +0 -245
- package/dist/dispatcher.js +0 -122
- package/dist/history.js +0 -99
- package/dist/ipc.js +0 -72
- package/dist/log.js +0 -15
- package/dist/paths.js +0 -68
- package/dist/registry.js +0 -48
- package/dist/stations/claude.js +0 -45
- package/dist/stations/codex.js +0 -68
- package/dist/stations/discord.js +0 -200
- package/dist/stations/index.js +0 -133
- package/dist/stations/telegram-md.js +0 -34
- package/dist/stations/telegram-upload.js +0 -113
- package/dist/stations/telegram.js +0 -213
- package/dist/stations/webhook.js +0 -100
- package/dist/tunnel.js +0 -49
- package/dist/webhooks.js +0 -40
- package/docs/agents.md +0 -216
- package/docs/uri-scheme.md +0 -125
- package/skills/metro/SKILL.md +0 -249
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { ApiError } from '@metro-labs/http/api-error';
|
|
2
|
+
|
|
3
|
+
export class ConnectorVerifyError extends ApiError {}
|
|
4
|
+
|
|
5
|
+
export class ConnectorUnauthorized extends ConnectorVerifyError {}
|
|
6
|
+
|
|
7
|
+
export class ConnectorNotMcp extends ConnectorVerifyError {}
|
|
8
|
+
|
|
9
|
+
export const POLICY_MESSAGE =
|
|
10
|
+
'Metro connects from its own server, so it cannot reach a URL on your machine. localhost and private addresses are not usable as connectors.';
|
|
11
|
+
|
|
12
|
+
let localAllowed = false;
|
|
13
|
+
|
|
14
|
+
export function allowLocalConnectors(on: boolean): void {
|
|
15
|
+
localAllowed = on;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const IPV4 = /^\d{1,3}(?:\.\d{1,3}){3}$/;
|
|
19
|
+
const PRIVATE_SUFFIX = /(?:^|\.)(?:localhost|local|internal|flycast)$/;
|
|
20
|
+
|
|
21
|
+
export function refused(message: string): ConnectorVerifyError {
|
|
22
|
+
return new ConnectorVerifyError(message, 400);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function hostAllowed(host: string): boolean {
|
|
26
|
+
if (host === '' || host.startsWith('[')) return false;
|
|
27
|
+
if (IPV4.test(host) || !host.includes('.')) return false;
|
|
28
|
+
return !PRIVATE_SUFFIX.test(host);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function schemeAllowed(url: URL): boolean {
|
|
32
|
+
return url.protocol === 'https:' || (localAllowed && url.protocol === 'http:');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function refuseShape(url: URL): void {
|
|
36
|
+
if (!schemeAllowed(url)) throw refused('a connector url must start with https://');
|
|
37
|
+
if (url.username !== '' || url.password !== '')
|
|
38
|
+
throw refused('a connector url must not carry a user:password');
|
|
39
|
+
if (url.hash !== '')
|
|
40
|
+
throw refused('a connector url must not carry a #fragment');
|
|
41
|
+
if (!localAllowed && !hostAllowed(url.hostname.toLowerCase())) throw refused(POLICY_MESSAGE);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function parseConnectorUrl(raw: unknown): URL {
|
|
45
|
+
const text = typeof raw === 'string' ? raw.trim() : '';
|
|
46
|
+
if (text === '') throw refused('a connector url is required');
|
|
47
|
+
let url: URL;
|
|
48
|
+
try {
|
|
49
|
+
url = new URL(text);
|
|
50
|
+
} catch {
|
|
51
|
+
throw refused('that is not a valid url');
|
|
52
|
+
}
|
|
53
|
+
refuseShape(url);
|
|
54
|
+
return url;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function connectorUrlText(url: URL): string {
|
|
58
|
+
const text = url.toString();
|
|
59
|
+
if (url.pathname !== '/' || url.search !== '') return text;
|
|
60
|
+
return text.endsWith('/') ? text.slice(0, -1) : text;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function safeIconSrc(raw: unknown): string {
|
|
64
|
+
if (typeof raw !== 'string' || raw === '') return '';
|
|
65
|
+
try {
|
|
66
|
+
return new URL(raw).protocol === 'https:' ? raw.slice(0, 500) : '';
|
|
67
|
+
} catch {
|
|
68
|
+
return '';
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
import { errMsg, log } from '@metro-labs/core/log';
|
|
2
|
+
import {
|
|
3
|
+
ConnectorNotMcp,
|
|
4
|
+
ConnectorUnauthorized,
|
|
5
|
+
refused,
|
|
6
|
+
safeIconSrc,
|
|
7
|
+
} from './url.js';
|
|
8
|
+
import { toToolList, type ToolInfo } from './tools.js';
|
|
9
|
+
|
|
10
|
+
export {
|
|
11
|
+
ConnectorUnauthorized,
|
|
12
|
+
ConnectorVerifyError,
|
|
13
|
+
connectorUrlText,
|
|
14
|
+
parseConnectorUrl,
|
|
15
|
+
} from './url.js';
|
|
16
|
+
import { isRecord } from '@metro-labs/core/is-record';
|
|
17
|
+
|
|
18
|
+
export interface OAuthTokens {
|
|
19
|
+
accessToken: string;
|
|
20
|
+
refreshToken?: string;
|
|
21
|
+
expiresAt?: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface OAuthAuth extends OAuthTokens {
|
|
25
|
+
kind: 'oauth';
|
|
26
|
+
issuer: string;
|
|
27
|
+
tokenEndpoint: string;
|
|
28
|
+
clientId: string;
|
|
29
|
+
clientSecret?: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export type ConnectorAuth =
|
|
33
|
+
| { kind: 'none' }
|
|
34
|
+
| { kind: 'header'; name: string; value: string }
|
|
35
|
+
| OAuthAuth;
|
|
36
|
+
|
|
37
|
+
export interface VerifiedServer {
|
|
38
|
+
server: string;
|
|
39
|
+
version: string;
|
|
40
|
+
protocol: string;
|
|
41
|
+
icon: string;
|
|
42
|
+
tools: number;
|
|
43
|
+
catalog: ToolInfo[];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface VerifiedRecord extends VerifiedServer {
|
|
47
|
+
at: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const PROTOCOL_VERSIONS = ['2025-11-25', '2025-06-18'] as const;
|
|
51
|
+
const MAX_TOOL_PAGES = 10;
|
|
52
|
+
|
|
53
|
+
function notMcp(url: URL): ConnectorNotMcp {
|
|
54
|
+
return new ConnectorNotMcp(
|
|
55
|
+
`${url.hostname} answered, but it does not speak MCP.`,
|
|
56
|
+
400,
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
const PROBE_TIMEOUT_MS = 10_000;
|
|
60
|
+
const ACCEPT = 'application/json, text/event-stream';
|
|
61
|
+
|
|
62
|
+
async function discard(res: Response): Promise<void> {
|
|
63
|
+
await res.body?.cancel().catch((err: unknown) => {
|
|
64
|
+
log.debug(
|
|
65
|
+
{ err: errMsg(err) },
|
|
66
|
+
'connector-verify: could not discard a response body',
|
|
67
|
+
);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function authHeaders(auth: ConnectorAuth): Record<string, string> {
|
|
72
|
+
if (auth.kind === 'header') return { [auth.name]: auth.value };
|
|
73
|
+
if (auth.kind === 'oauth')
|
|
74
|
+
return { authorization: `Bearer ${auth.accessToken}` };
|
|
75
|
+
return {};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async function frame(
|
|
79
|
+
url: URL,
|
|
80
|
+
auth: ConnectorAuth,
|
|
81
|
+
extra: Record<string, string>,
|
|
82
|
+
body: unknown,
|
|
83
|
+
signal: AbortSignal,
|
|
84
|
+
): Promise<Response> {
|
|
85
|
+
let res: Response;
|
|
86
|
+
try {
|
|
87
|
+
res = await fetch(url, {
|
|
88
|
+
method: 'POST',
|
|
89
|
+
redirect: 'manual',
|
|
90
|
+
signal,
|
|
91
|
+
headers: {
|
|
92
|
+
'content-type': 'application/json',
|
|
93
|
+
accept: ACCEPT,
|
|
94
|
+
...authHeaders(auth),
|
|
95
|
+
...extra,
|
|
96
|
+
},
|
|
97
|
+
body: JSON.stringify(body),
|
|
98
|
+
});
|
|
99
|
+
} catch {
|
|
100
|
+
throw refused(`Metro could not reach ${url.hostname}.`);
|
|
101
|
+
}
|
|
102
|
+
if (res.status >= 300 && res.status < 400) {
|
|
103
|
+
await discard(res);
|
|
104
|
+
throw refused('that url redirects — use the url it redirects to');
|
|
105
|
+
}
|
|
106
|
+
return res;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function sseData(body: string): string {
|
|
110
|
+
for (const line of body.replace(/\r\n/g, '\n').split('\n'))
|
|
111
|
+
if (line.startsWith('data:')) return line.slice(5).trim();
|
|
112
|
+
return '';
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async function payloadOf(res: Response): Promise<string> {
|
|
116
|
+
const contentType = res.headers.get('content-type') ?? '';
|
|
117
|
+
const text = await res.text().catch(() => '');
|
|
118
|
+
return contentType.includes('text/event-stream') ? sseData(text) : text;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async function resultOrNull(
|
|
122
|
+
res: Response,
|
|
123
|
+
): Promise<Record<string, unknown> | null> {
|
|
124
|
+
const payload = await payloadOf(res);
|
|
125
|
+
let parsed: unknown;
|
|
126
|
+
try {
|
|
127
|
+
parsed = JSON.parse(payload);
|
|
128
|
+
} catch {
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
if (!isRecord(parsed) || !isRecord(parsed.result)) return null;
|
|
132
|
+
return parsed.result;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function initializeBody(version: string): unknown {
|
|
136
|
+
return {
|
|
137
|
+
jsonrpc: '2.0',
|
|
138
|
+
id: 1,
|
|
139
|
+
method: 'initialize',
|
|
140
|
+
params: {
|
|
141
|
+
protocolVersion: version,
|
|
142
|
+
capabilities: {},
|
|
143
|
+
clientInfo: { name: 'metro', version: '0.1.0' },
|
|
144
|
+
},
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function iconOf(info: Record<string, unknown>): string {
|
|
149
|
+
const icons = info.icons;
|
|
150
|
+
if (!Array.isArray(icons)) return '';
|
|
151
|
+
for (const entry of icons) {
|
|
152
|
+
if (!isRecord(entry)) continue;
|
|
153
|
+
const src = safeIconSrc(entry.src);
|
|
154
|
+
if (src !== '') return src;
|
|
155
|
+
}
|
|
156
|
+
return '';
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function serverFrom(url: URL, result: Record<string, unknown>): VerifiedServer {
|
|
160
|
+
const protocol = result.protocolVersion;
|
|
161
|
+
if (typeof protocol !== 'string') throw notMcp(url);
|
|
162
|
+
const info = isRecord(result.serverInfo) ? result.serverInfo : {};
|
|
163
|
+
const title = typeof info.title === 'string' ? info.title : '';
|
|
164
|
+
return {
|
|
165
|
+
server:
|
|
166
|
+
title !== ''
|
|
167
|
+
? title
|
|
168
|
+
: typeof info.name === 'string'
|
|
169
|
+
? info.name
|
|
170
|
+
: url.hostname,
|
|
171
|
+
version: typeof info.version === 'string' ? info.version : '',
|
|
172
|
+
protocol,
|
|
173
|
+
icon: iconOf(info),
|
|
174
|
+
tools: 0,
|
|
175
|
+
catalog: [],
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async function initialize(
|
|
180
|
+
url: URL,
|
|
181
|
+
auth: ConnectorAuth,
|
|
182
|
+
signal: AbortSignal,
|
|
183
|
+
version: string,
|
|
184
|
+
): Promise<{ session: string | null; server: VerifiedServer }> {
|
|
185
|
+
const res = await frame(url, auth, {}, initializeBody(version), signal);
|
|
186
|
+
if (res.status === 401 || res.status === 403) {
|
|
187
|
+
await discard(res);
|
|
188
|
+
throw new ConnectorUnauthorized(
|
|
189
|
+
auth.kind === 'none'
|
|
190
|
+
? `${url.hostname} requires authorization.`
|
|
191
|
+
: `${url.hostname} rejected that credential.`,
|
|
192
|
+
400,
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
if (res.status === 404 || res.status === 405) {
|
|
196
|
+
await discard(res);
|
|
197
|
+
throw refused(
|
|
198
|
+
`${url.hostname} did not accept an MCP initialize over HTTP POST. ` +
|
|
199
|
+
'If it is a legacy SSE server, Metro cannot verify it yet.',
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
if (!res.ok) {
|
|
203
|
+
await discard(res);
|
|
204
|
+
throw notMcp(url);
|
|
205
|
+
}
|
|
206
|
+
const result = await resultOrNull(res);
|
|
207
|
+
if (result === null) throw notMcp(url);
|
|
208
|
+
return {
|
|
209
|
+
session: res.headers.get('mcp-session-id'),
|
|
210
|
+
server: serverFrom(url, result),
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function sessionHeaders(
|
|
215
|
+
session: string | null,
|
|
216
|
+
version: string,
|
|
217
|
+
): Record<string, string> {
|
|
218
|
+
const headers: Record<string, string> = {
|
|
219
|
+
'mcp-protocol-version': version,
|
|
220
|
+
};
|
|
221
|
+
if (session !== null && session !== '') headers['mcp-session-id'] = session;
|
|
222
|
+
return headers;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
async function announce(
|
|
226
|
+
url: URL,
|
|
227
|
+
auth: ConnectorAuth,
|
|
228
|
+
headers: Record<string, string>,
|
|
229
|
+
signal: AbortSignal,
|
|
230
|
+
): Promise<void> {
|
|
231
|
+
const res = await frame(
|
|
232
|
+
url,
|
|
233
|
+
auth,
|
|
234
|
+
headers,
|
|
235
|
+
{ jsonrpc: '2.0', method: 'notifications/initialized' },
|
|
236
|
+
signal,
|
|
237
|
+
);
|
|
238
|
+
await discard(res);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
async function toolPage(
|
|
242
|
+
url: URL,
|
|
243
|
+
auth: ConnectorAuth,
|
|
244
|
+
headers: Record<string, string>,
|
|
245
|
+
signal: AbortSignal,
|
|
246
|
+
cursor: string,
|
|
247
|
+
): Promise<Record<string, unknown> | null> {
|
|
248
|
+
const params = cursor === '' ? {} : { cursor };
|
|
249
|
+
const res = await frame(
|
|
250
|
+
url,
|
|
251
|
+
auth,
|
|
252
|
+
headers,
|
|
253
|
+
{ jsonrpc: '2.0', id: 2, method: 'tools/list', params },
|
|
254
|
+
signal,
|
|
255
|
+
);
|
|
256
|
+
if (!res.ok) {
|
|
257
|
+
await discard(res);
|
|
258
|
+
return null;
|
|
259
|
+
}
|
|
260
|
+
return resultOrNull(res);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
async function listTools(
|
|
264
|
+
url: URL,
|
|
265
|
+
auth: ConnectorAuth,
|
|
266
|
+
headers: Record<string, string>,
|
|
267
|
+
signal: AbortSignal,
|
|
268
|
+
): Promise<ToolInfo[]> {
|
|
269
|
+
const tools: ToolInfo[] = [];
|
|
270
|
+
let cursor = '';
|
|
271
|
+
for (let page = 0; page < MAX_TOOL_PAGES; page += 1) {
|
|
272
|
+
const result = await toolPage(url, auth, headers, signal, cursor);
|
|
273
|
+
if (result === null) break;
|
|
274
|
+
tools.push(...toToolList(result.tools));
|
|
275
|
+
const next = result.nextCursor;
|
|
276
|
+
if (typeof next !== 'string' || next === '' || next === cursor) break;
|
|
277
|
+
cursor = next;
|
|
278
|
+
}
|
|
279
|
+
return tools;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
async function terminate(
|
|
283
|
+
url: URL,
|
|
284
|
+
auth: ConnectorAuth,
|
|
285
|
+
headers: Record<string, string>,
|
|
286
|
+
signal: AbortSignal,
|
|
287
|
+
): Promise<void> {
|
|
288
|
+
const res = await fetch(url, {
|
|
289
|
+
method: 'DELETE',
|
|
290
|
+
redirect: 'manual',
|
|
291
|
+
signal,
|
|
292
|
+
headers: { ...authHeaders(auth), ...headers },
|
|
293
|
+
});
|
|
294
|
+
await discard(res);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
async function probeWith(
|
|
298
|
+
url: URL,
|
|
299
|
+
auth: ConnectorAuth,
|
|
300
|
+
version: string,
|
|
301
|
+
): Promise<VerifiedServer> {
|
|
302
|
+
const signal = AbortSignal.timeout(PROBE_TIMEOUT_MS);
|
|
303
|
+
const { session, server } = await initialize(url, auth, signal, version);
|
|
304
|
+
const headers = sessionHeaders(session, server.protocol);
|
|
305
|
+
await announce(url, auth, headers, signal);
|
|
306
|
+
const catalog = await listTools(url, auth, headers, signal);
|
|
307
|
+
await terminate(url, auth, headers, signal).catch((err: unknown) => {
|
|
308
|
+
log.debug(
|
|
309
|
+
{ host: url.hostname, err: errMsg(err) },
|
|
310
|
+
'connector-verify: the remote declined to end the session',
|
|
311
|
+
);
|
|
312
|
+
});
|
|
313
|
+
return { ...server, tools: catalog.length, catalog };
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
export async function verifyRemoteMcp(
|
|
317
|
+
url: URL,
|
|
318
|
+
auth: ConnectorAuth,
|
|
319
|
+
): Promise<VerifiedServer> {
|
|
320
|
+
let last: unknown;
|
|
321
|
+
for (const version of PROTOCOL_VERSIONS) {
|
|
322
|
+
try {
|
|
323
|
+
return await probeWith(url, auth, version);
|
|
324
|
+
} catch (err) {
|
|
325
|
+
if (!(err instanceof ConnectorNotMcp)) throw err;
|
|
326
|
+
last = err;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
throw last;
|
|
330
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { randomBytes, timingSafeEqual } from 'node:crypto';
|
|
2
|
+
import { ID_RE } from '@metro-labs/core/ids';
|
|
3
|
+
import { readFileSync } from 'node:fs';
|
|
4
|
+
import { resolveCachedAttachment } from '@metro-labs/core/stations/attachments';
|
|
5
|
+
import { writeSecure } from '@metro-labs/core/secure-fs';
|
|
6
|
+
|
|
7
|
+
const GRANT_SUFFIX = '.grant';
|
|
8
|
+
|
|
9
|
+
export interface AttachmentGrant {
|
|
10
|
+
token: string;
|
|
11
|
+
agentId: string;
|
|
12
|
+
mintedAt: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function newAttachmentToken(prefix = 'at'): string {
|
|
16
|
+
return `${prefix}_${randomBytes(32).toString('base64url')}`;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function parseGrant(raw: string): AttachmentGrant | undefined {
|
|
20
|
+
let parsed: unknown;
|
|
21
|
+
try {
|
|
22
|
+
parsed = JSON.parse(raw);
|
|
23
|
+
} catch {
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
if (typeof parsed !== 'object' || parsed === null) return undefined;
|
|
27
|
+
const { token, agentId, mintedAt } = parsed as Record<string, unknown>;
|
|
28
|
+
if (typeof token !== 'string' || token === '') return undefined;
|
|
29
|
+
if (typeof agentId !== 'string' || !ID_RE.test(agentId)) return undefined;
|
|
30
|
+
return {
|
|
31
|
+
token,
|
|
32
|
+
agentId,
|
|
33
|
+
mintedAt: typeof mintedAt === 'number' ? mintedAt : 0,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function readGrant(path: string): AttachmentGrant | undefined {
|
|
38
|
+
try {
|
|
39
|
+
return parseGrant(readFileSync(`${path}${GRANT_SUFFIX}`, 'utf8'));
|
|
40
|
+
} catch {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function issueGrant(
|
|
46
|
+
path: string,
|
|
47
|
+
agentId: string,
|
|
48
|
+
prefix?: string,
|
|
49
|
+
): string {
|
|
50
|
+
const existing = readGrant(path);
|
|
51
|
+
if (existing?.agentId === agentId) return existing.token;
|
|
52
|
+
const grant: AttachmentGrant = {
|
|
53
|
+
token: newAttachmentToken(prefix),
|
|
54
|
+
agentId,
|
|
55
|
+
mintedAt: Date.now(),
|
|
56
|
+
};
|
|
57
|
+
writeSecure(`${path}${GRANT_SUFFIX}`, `${JSON.stringify(grant)}\n`);
|
|
58
|
+
return grant.token;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function sameToken(presented: string, stored: string): boolean {
|
|
62
|
+
const a = Buffer.from(presented, 'utf8');
|
|
63
|
+
const b = Buffer.from(stored, 'utf8');
|
|
64
|
+
return a.length === b.length && timingSafeEqual(a, b);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function grantAllowsPath(
|
|
68
|
+
path: string,
|
|
69
|
+
owner: string,
|
|
70
|
+
presented: string,
|
|
71
|
+
): boolean {
|
|
72
|
+
if (presented === '') return false;
|
|
73
|
+
const grant = readGrant(path);
|
|
74
|
+
if (grant === undefined) return false;
|
|
75
|
+
if (grant.agentId !== owner) return false;
|
|
76
|
+
return sameToken(presented, grant.token);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function readAttachmentGrant(name: string): AttachmentGrant | undefined {
|
|
80
|
+
const path = resolveCachedAttachment(name);
|
|
81
|
+
return path === null ? undefined : readGrant(path);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function issueAttachmentGrant(
|
|
85
|
+
name: string,
|
|
86
|
+
agentId: string,
|
|
87
|
+
): string | undefined {
|
|
88
|
+
const path = resolveCachedAttachment(name);
|
|
89
|
+
return path === null ? undefined : issueGrant(path, agentId);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function grantAllows(
|
|
93
|
+
name: string,
|
|
94
|
+
owner: string,
|
|
95
|
+
presented: string,
|
|
96
|
+
): boolean {
|
|
97
|
+
const path = resolveCachedAttachment(name);
|
|
98
|
+
return path === null ? false : grantAllowsPath(path, owner, presented);
|
|
99
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { ID_RE } from '@metro-labs/core/ids';
|
|
3
|
+
import { resolveCachedAttachment } from '@metro-labs/core/stations/attachments';
|
|
4
|
+
import { writeSecure } from '@metro-labs/core/secure-fs';
|
|
5
|
+
|
|
6
|
+
const OWNER_SUFFIX = '.owner';
|
|
7
|
+
|
|
8
|
+
export function recordOwner(path: string, agentId: string): void {
|
|
9
|
+
writeSecure(`${path}${OWNER_SUFFIX}`, `${agentId}\n`);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function ownerOf(path: string): string | undefined {
|
|
13
|
+
let raw: string;
|
|
14
|
+
try {
|
|
15
|
+
raw = readFileSync(`${path}${OWNER_SUFFIX}`, 'utf8');
|
|
16
|
+
} catch {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
const agentId = raw.trim();
|
|
20
|
+
return ID_RE.test(agentId) ? agentId : undefined;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function recordAttachmentOwner(name: string, agentId: string): void {
|
|
24
|
+
const path = resolveCachedAttachment(name);
|
|
25
|
+
if (path === null) return;
|
|
26
|
+
recordOwner(path, agentId);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function attachmentOwner(name: string): string | undefined {
|
|
30
|
+
const path = resolveCachedAttachment(name);
|
|
31
|
+
return path === null ? undefined : ownerOf(path);
|
|
32
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { createReadStream } from 'node:fs';
|
|
2
|
+
import { stat } from 'node:fs/promises';
|
|
3
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
4
|
+
import {
|
|
5
|
+
guessMime,
|
|
6
|
+
resolveCachedAttachment,
|
|
7
|
+
} from '@metro-labs/core/stations/attachments';
|
|
8
|
+
import {
|
|
9
|
+
allowedAgents,
|
|
10
|
+
authenticate,
|
|
11
|
+
extractToken,
|
|
12
|
+
} from '../mcp/request-identity.js';
|
|
13
|
+
import { attachmentOwner, recordAttachmentOwner } from './attach-owner.js';
|
|
14
|
+
import { grantAllows, issueAttachmentGrant } from './attach-grant.js';
|
|
15
|
+
import { errMsg, log } from '@metro-labs/core/log';
|
|
16
|
+
import { currentTunnelUrl, webhookPort } from '../net/tunnel.js';
|
|
17
|
+
|
|
18
|
+
function authorized(req: IncomingMessage, name: string): boolean {
|
|
19
|
+
const owner = attachmentOwner(name);
|
|
20
|
+
if (owner === undefined) return false;
|
|
21
|
+
if (grantAllows(name, owner, extractToken(req) ?? '')) return true;
|
|
22
|
+
return allowedAgents(authenticate(req) ?? undefined).has(
|
|
23
|
+
owner,
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function publicBaseUrl(): string | null {
|
|
28
|
+
const env = process.env.METRO_PUBLIC_URL?.trim();
|
|
29
|
+
if (env) return env.replace(/\/+$/, '');
|
|
30
|
+
return currentTunnelUrl();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const localBase = (): string => `http://127.0.0.1:${String(webhookPort())}`;
|
|
34
|
+
|
|
35
|
+
export const publicBaseOrDefault = (): string => publicBaseUrl() ?? localBase();
|
|
36
|
+
|
|
37
|
+
export function attachmentUrl(
|
|
38
|
+
pathOrName: string,
|
|
39
|
+
agentId: string,
|
|
40
|
+
): string | null {
|
|
41
|
+
const base = publicBaseUrl() ?? localBase();
|
|
42
|
+
if (!base) return null;
|
|
43
|
+
const name = pathOrName.split('/').pop();
|
|
44
|
+
if (!name || !resolveCachedAttachment(name)) return null;
|
|
45
|
+
recordAttachmentOwner(name, agentId);
|
|
46
|
+
const token = issueAttachmentGrant(name, agentId);
|
|
47
|
+
if (token === undefined) return null;
|
|
48
|
+
return `${base}/attach/${encodeURIComponent(name)}?token=${encodeURIComponent(token)}`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function attachmentEventUrl(
|
|
52
|
+
payload: Record<string, unknown>,
|
|
53
|
+
agentId: string,
|
|
54
|
+
): string | null {
|
|
55
|
+
if (payload.contentType !== 'attachmentSaved') return null;
|
|
56
|
+
if (typeof payload.url === 'string' && payload.url.length > 0) return null;
|
|
57
|
+
const p = payload.attachmentPath ?? payload.localPath;
|
|
58
|
+
return typeof p === 'string' ? attachmentUrl(p, agentId) : null;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async function serveFile(
|
|
62
|
+
res: ServerResponse,
|
|
63
|
+
method: string,
|
|
64
|
+
name: string,
|
|
65
|
+
): Promise<void> {
|
|
66
|
+
const path = resolveCachedAttachment(name);
|
|
67
|
+
if (!path) {
|
|
68
|
+
res.writeHead(404).end();
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
let size: number;
|
|
72
|
+
try {
|
|
73
|
+
size = (await stat(path)).size;
|
|
74
|
+
} catch {
|
|
75
|
+
res.writeHead(404).end();
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
res.writeHead(200, {
|
|
79
|
+
'content-type': guessMime(path),
|
|
80
|
+
'content-length': String(size),
|
|
81
|
+
'cache-control': 'private, max-age=86400',
|
|
82
|
+
});
|
|
83
|
+
if (method === 'HEAD') {
|
|
84
|
+
res.end();
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
const stream = createReadStream(path);
|
|
88
|
+
stream.on('error', (err) => {
|
|
89
|
+
log.warn({ err: errMsg(err) }, 'attach: read stream error');
|
|
90
|
+
res.end();
|
|
91
|
+
});
|
|
92
|
+
stream.pipe(res);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function handleAttachRequest(
|
|
96
|
+
req: IncomingMessage,
|
|
97
|
+
res: ServerResponse,
|
|
98
|
+
): boolean {
|
|
99
|
+
const [rawPath] = (req.url ?? '').split('?', 2);
|
|
100
|
+
const m = /^\/attach\/([^/]+)$/.exec(rawPath ?? '');
|
|
101
|
+
if (!m) return false;
|
|
102
|
+
const method = req.method ?? 'GET';
|
|
103
|
+
if (method !== 'GET' && method !== 'HEAD') {
|
|
104
|
+
res.writeHead(405).end();
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
const name = decodeURIComponent(m[1] ?? '');
|
|
108
|
+
if (resolveCachedAttachment(name) === null) {
|
|
109
|
+
res.writeHead(404).end();
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
if (!authorized(req, name)) {
|
|
113
|
+
res.writeHead(401).end();
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
serveFile(res, method, name).catch((err: unknown) => {
|
|
117
|
+
log.warn({ err: errMsg(err) }, 'attach: serve error');
|
|
118
|
+
if (!res.headersSent) res.writeHead(500).end();
|
|
119
|
+
});
|
|
120
|
+
return true;
|
|
121
|
+
}
|