@stage-labs/metro 0.1.0-beta.13 → 0.1.0-beta.130

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 (328) hide show
  1. package/dist/api.js +14 -0
  2. package/dist/claude-settings.js +40 -0
  3. package/dist/claude.js +176 -0
  4. package/dist/cli.js +101 -0
  5. package/dist/control.js +74 -0
  6. package/dist/hold.js +211 -0
  7. package/dist/local.js +93 -0
  8. package/dist/mcp-config.js +25 -0
  9. package/dist/node-name.js +71 -0
  10. package/dist/onboarding.js +43 -0
  11. package/dist/plugin.js +120 -0
  12. package/dist/provider-flags.js +9 -0
  13. package/dist/route.js +38 -0
  14. package/dist/runtime-install.js +97 -0
  15. package/dist/runtime.js +74 -0
  16. package/dist/serve.js +150 -0
  17. package/dist/service.js +242 -0
  18. package/dist/tail.js +119 -0
  19. package/dist/update.js +45 -0
  20. package/dist/version.js +97 -0
  21. package/package.json +14 -27
  22. package/runtime/marketplace/.claude-plugin/marketplace.json +12 -0
  23. package/runtime/marketplace/plugin/.claude-plugin/plugin.json +18 -0
  24. package/runtime/marketplace/plugin/.mcp.json +1 -0
  25. package/runtime/marketplace/plugin/bin/guard.mjs +55 -0
  26. package/runtime/marketplace/plugin/bin/metro-plugin.mjs +122 -0
  27. package/runtime/marketplace/plugin/bin/session-start.mjs +31 -0
  28. package/runtime/marketplace/plugin/hooks/hooks.json +25 -0
  29. package/runtime/marketplace/plugin/orchestrator.md +16 -0
  30. package/runtime/marketplace/plugin/skills/refresh/SKILL.md +15 -0
  31. package/runtime/node_modules/@metro-labs/core/package.json +43 -0
  32. package/runtime/node_modules/@metro-labs/core/src/address.ts +6 -0
  33. package/runtime/node_modules/@metro-labs/core/src/endpoints.ts +7 -0
  34. package/runtime/node_modules/@metro-labs/core/src/events.ts +120 -0
  35. package/runtime/node_modules/@metro-labs/core/src/identity.ts +7 -0
  36. package/runtime/node_modules/@metro-labs/core/src/ids.ts +21 -0
  37. package/runtime/node_modules/@metro-labs/core/src/is-record.ts +2 -0
  38. package/runtime/node_modules/@metro-labs/core/src/lines.ts +87 -0
  39. package/runtime/node_modules/@metro-labs/core/src/log.ts +42 -0
  40. package/runtime/node_modules/@metro-labs/core/src/protocol.ts +226 -0
  41. package/runtime/node_modules/@metro-labs/core/src/secure-fs.ts +57 -0
  42. package/runtime/node_modules/@metro-labs/core/src/station-names.ts +13 -0
  43. package/runtime/node_modules/@metro-labs/core/src/stations/account-store.ts +60 -0
  44. package/runtime/node_modules/@metro-labs/core/src/stations/attachments.ts +196 -0
  45. package/runtime/node_modules/@metro-labs/core/src/stations/messaging-normalize.ts +88 -0
  46. package/runtime/node_modules/@metro-labs/core/src/stations/station-runtime.ts +64 -0
  47. package/runtime/node_modules/@metro-labs/core/src/stations/types.ts +108 -0
  48. package/runtime/node_modules/@metro-labs/core/src/str.ts +5 -0
  49. package/runtime/node_modules/@metro-labs/core/src/train-error.ts +53 -0
  50. package/runtime/node_modules/@metro-labs/core/src/version.ts +2 -0
  51. package/runtime/node_modules/@metro-labs/daemon/package.json +48 -0
  52. package/runtime/node_modules/@metro-labs/daemon/src/agents/account-attach.ts +19 -0
  53. package/runtime/node_modules/@metro-labs/daemon/src/agents/account-routes.ts +70 -0
  54. package/runtime/node_modules/@metro-labs/daemon/src/agents/accounts-api.ts +365 -0
  55. package/runtime/node_modules/@metro-labs/daemon/src/agents/admin.ts +43 -0
  56. package/runtime/node_modules/@metro-labs/daemon/src/agents/allowlist.ts +37 -0
  57. package/runtime/node_modules/@metro-labs/daemon/src/agents/api.ts +300 -0
  58. package/runtime/node_modules/@metro-labs/daemon/src/agents/bundle.ts +122 -0
  59. package/runtime/node_modules/@metro-labs/daemon/src/agents/file-admin.ts +371 -0
  60. package/runtime/node_modules/@metro-labs/daemon/src/agents/files.ts +155 -0
  61. package/runtime/node_modules/@metro-labs/daemon/src/agents/keys.ts +45 -0
  62. package/runtime/node_modules/@metro-labs/daemon/src/agents/map.ts +95 -0
  63. package/runtime/node_modules/@metro-labs/daemon/src/agents/scope.ts +64 -0
  64. package/runtime/node_modules/@metro-labs/daemon/src/agents/senders.ts +38 -0
  65. package/runtime/node_modules/@metro-labs/daemon/src/boot/boot.ts +207 -0
  66. package/runtime/node_modules/@metro-labs/daemon/src/boot/connect-hint.ts +39 -0
  67. package/runtime/node_modules/@metro-labs/daemon/src/boot/crash-guard.ts +49 -0
  68. package/runtime/node_modules/@metro-labs/daemon/src/boot/local-owner.ts +14 -0
  69. package/runtime/node_modules/@metro-labs/daemon/src/boot/paths.ts +106 -0
  70. package/runtime/node_modules/@metro-labs/daemon/src/channels/addressed.ts +25 -0
  71. package/runtime/node_modules/@metro-labs/daemon/src/channels/dedupe.ts +24 -0
  72. package/runtime/node_modules/@metro-labs/daemon/src/channels/inbound.ts +377 -0
  73. package/runtime/node_modules/@metro-labs/daemon/src/channels/media-note.ts +85 -0
  74. package/runtime/node_modules/@metro-labs/daemon/src/channels/pending.ts +75 -0
  75. package/runtime/node_modules/@metro-labs/daemon/src/channels/relay.ts +129 -0
  76. package/runtime/node_modules/@metro-labs/daemon/src/channels/webhook-note.ts +51 -0
  77. package/runtime/node_modules/@metro-labs/daemon/src/claude/api.ts +309 -0
  78. package/runtime/node_modules/@metro-labs/daemon/src/claude/files.ts +358 -0
  79. package/runtime/node_modules/@metro-labs/daemon/src/claude/login.ts +169 -0
  80. package/runtime/node_modules/@metro-labs/daemon/src/claude/onboarding.ts +56 -0
  81. package/runtime/node_modules/@metro-labs/daemon/src/claude/plugin-install.ts +140 -0
  82. package/runtime/node_modules/@metro-labs/daemon/src/claude/session-files.ts +90 -0
  83. package/runtime/node_modules/@metro-labs/daemon/src/claude/session.ts +253 -0
  84. package/runtime/node_modules/@metro-labs/daemon/src/claude/settings.ts +117 -0
  85. package/runtime/node_modules/@metro-labs/daemon/src/claude/setup.ts +239 -0
  86. package/runtime/node_modules/@metro-labs/daemon/src/claude/skills.ts +154 -0
  87. package/runtime/node_modules/@metro-labs/daemon/src/claude/version.ts +109 -0
  88. package/runtime/node_modules/@metro-labs/daemon/src/connectors/api.ts +310 -0
  89. package/runtime/node_modules/@metro-labs/daemon/src/connectors/cli-api.ts +63 -0
  90. package/runtime/node_modules/@metro-labs/daemon/src/connectors/config.ts +175 -0
  91. package/runtime/node_modules/@metro-labs/daemon/src/connectors/json.ts +21 -0
  92. package/runtime/node_modules/@metro-labs/daemon/src/connectors/model.ts +77 -0
  93. package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-client.ts +166 -0
  94. package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-discovery.ts +139 -0
  95. package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-pending.ts +65 -0
  96. package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-routes.ts +171 -0
  97. package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth.ts +155 -0
  98. package/runtime/node_modules/@metro-labs/daemon/src/connectors/plugin-sync.ts +122 -0
  99. package/runtime/node_modules/@metro-labs/daemon/src/connectors/reach.ts +27 -0
  100. package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-target.ts +39 -0
  101. package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay.ts +296 -0
  102. package/runtime/node_modules/@metro-labs/daemon/src/connectors/store.ts +286 -0
  103. package/runtime/node_modules/@metro-labs/daemon/src/connectors/tools.ts +98 -0
  104. package/runtime/node_modules/@metro-labs/daemon/src/connectors/url.ts +70 -0
  105. package/runtime/node_modules/@metro-labs/daemon/src/connectors/verify.ts +332 -0
  106. package/runtime/node_modules/@metro-labs/daemon/src/connectors/watch.ts +54 -0
  107. package/runtime/node_modules/@metro-labs/daemon/src/files/attach-grant.ts +99 -0
  108. package/runtime/node_modules/@metro-labs/daemon/src/files/attach-owner.ts +32 -0
  109. package/runtime/node_modules/@metro-labs/daemon/src/files/attach-serve.ts +121 -0
  110. package/runtime/node_modules/@metro-labs/daemon/src/files/upload-api.ts +294 -0
  111. package/runtime/node_modules/@metro-labs/daemon/src/files/upload-store.ts +181 -0
  112. package/runtime/node_modules/@metro-labs/daemon/src/gateway/bedrock.ts +259 -0
  113. package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-auth.ts +231 -0
  114. package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-device.ts +91 -0
  115. package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-stream.ts +288 -0
  116. package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-translate.ts +203 -0
  117. package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex.ts +231 -0
  118. package/runtime/node_modules/@metro-labs/daemon/src/gateway/eventstream.ts +123 -0
  119. package/runtime/node_modules/@metro-labs/daemon/src/gateway/forward.ts +187 -0
  120. package/runtime/node_modules/@metro-labs/daemon/src/gateway/gateway.ts +229 -0
  121. package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-api.ts +269 -0
  122. package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-config.ts +207 -0
  123. package/runtime/node_modules/@metro-labs/daemon/src/gateway/openrouter.ts +90 -0
  124. package/runtime/node_modules/@metro-labs/daemon/src/gateway/provider-models.ts +65 -0
  125. package/runtime/node_modules/@metro-labs/daemon/src/gateway/served.ts +17 -0
  126. package/runtime/node_modules/@metro-labs/daemon/src/gateway/usage.ts +229 -0
  127. package/runtime/node_modules/@metro-labs/daemon/src/mcp/accounts.ts +151 -0
  128. package/runtime/node_modules/@metro-labs/daemon/src/mcp/call-tools.ts +216 -0
  129. package/runtime/node_modules/@metro-labs/daemon/src/mcp/channel-owner.ts +29 -0
  130. package/runtime/node_modules/@metro-labs/daemon/src/mcp/ctx.ts +62 -0
  131. package/runtime/node_modules/@metro-labs/daemon/src/mcp/event-store.ts +91 -0
  132. package/runtime/node_modules/@metro-labs/daemon/src/mcp/frame-scope.ts +21 -0
  133. package/runtime/node_modules/@metro-labs/daemon/src/mcp/group-tools.ts +129 -0
  134. package/runtime/node_modules/@metro-labs/daemon/src/mcp/index.ts +191 -0
  135. package/runtime/node_modules/@metro-labs/daemon/src/mcp/member-tools.ts +72 -0
  136. package/runtime/node_modules/@metro-labs/daemon/src/mcp/permission-relay.ts +66 -0
  137. package/runtime/node_modules/@metro-labs/daemon/src/mcp/raw-get-stream.ts +207 -0
  138. package/runtime/node_modules/@metro-labs/daemon/src/mcp/request-identity.ts +47 -0
  139. package/runtime/node_modules/@metro-labs/daemon/src/mcp/session-registry.ts +172 -0
  140. package/runtime/node_modules/@metro-labs/daemon/src/mcp/session-route.ts +43 -0
  141. package/runtime/node_modules/@metro-labs/daemon/src/mcp/session.ts +215 -0
  142. package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-dispatch.ts +215 -0
  143. package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-schemas.ts +400 -0
  144. package/runtime/node_modules/@metro-labs/daemon/src/mcp/upload-tool.ts +66 -0
  145. package/runtime/node_modules/@metro-labs/daemon/src/monitor/api.ts +237 -0
  146. package/runtime/node_modules/@metro-labs/daemon/src/net/tunnel.ts +389 -0
  147. package/runtime/node_modules/@metro-labs/daemon/src/routes/body.ts +24 -0
  148. package/runtime/node_modules/@metro-labs/daemon/src/routes/http.ts +372 -0
  149. package/runtime/node_modules/@metro-labs/daemon/src/routes/identity.ts +54 -0
  150. package/runtime/node_modules/@metro-labs/daemon/src/routes/local-mode.ts +223 -0
  151. package/runtime/node_modules/@metro-labs/daemon/src/routes/session-apis.ts +60 -0
  152. package/runtime/node_modules/@metro-labs/daemon/src/routes/session.ts +29 -0
  153. package/runtime/node_modules/@metro-labs/daemon/src/routes/threema-callback.ts +103 -0
  154. package/runtime/node_modules/@metro-labs/daemon/src/server/control.ts +64 -0
  155. package/runtime/node_modules/@metro-labs/daemon/src/server/machine.ts +61 -0
  156. package/runtime/node_modules/@metro-labs/daemon/src/server/update.ts +116 -0
  157. package/runtime/node_modules/@metro-labs/daemon/src/server.ts +2 -0
  158. package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-inline.ts +100 -0
  159. package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-interactive.ts +161 -0
  160. package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-resolve.ts +202 -0
  161. package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-session.ts +280 -0
  162. package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-xmtp.ts +111 -0
  163. package/runtime/node_modules/@metro-labs/daemon/src/stations/attach.ts +248 -0
  164. package/runtime/node_modules/@metro-labs/daemon/src/stations/materialize.ts +261 -0
  165. package/runtime/node_modules/@metro-labs/daemon/src/stations/registry.ts +37 -0
  166. package/runtime/node_modules/@metro-labs/daemon/src/stations/runtime-deps.ts +82 -0
  167. package/runtime/node_modules/@metro-labs/daemon/src/stations/supervisor-io.ts +97 -0
  168. package/runtime/node_modules/@metro-labs/daemon/src/stations/supervisor.ts +320 -0
  169. package/runtime/node_modules/@metro-labs/daemon/src/stations/threema-callbacks.ts +32 -0
  170. package/runtime/node_modules/@metro-labs/daemon/src/stations/train-call.ts +22 -0
  171. package/runtime/node_modules/@metro-labs/daemon/src/terminal/api.ts +96 -0
  172. package/runtime/node_modules/@metro-labs/daemon/src/terminal/socket.ts +123 -0
  173. package/runtime/node_modules/@metro-labs/daemon/src/terminal/tickets.ts +40 -0
  174. package/runtime/node_modules/@metro-labs/discord-bot/package.json +28 -0
  175. package/runtime/node_modules/@metro-labs/discord-bot/src/accounts.ts +118 -0
  176. package/runtime/node_modules/@metro-labs/discord-bot/src/actions.ts +388 -0
  177. package/runtime/node_modules/@metro-labs/discord-bot/src/api-base.ts +1 -0
  178. package/runtime/node_modules/@metro-labs/discord-bot/src/attachments.ts +29 -0
  179. package/runtime/node_modules/@metro-labs/discord-bot/src/format.ts +220 -0
  180. package/runtime/node_modules/@metro-labs/discord-bot/src/group-actions.ts +50 -0
  181. package/runtime/node_modules/@metro-labs/discord-bot/src/group.ts +115 -0
  182. package/runtime/node_modules/@metro-labs/discord-bot/src/index.ts +127 -0
  183. package/runtime/node_modules/@metro-labs/discord-bot/src/members.ts +215 -0
  184. package/runtime/node_modules/@metro-labs/discord-bot/src/send-files.ts +43 -0
  185. package/runtime/node_modules/@metro-labs/discord-bot/src/station.ts +19 -0
  186. package/runtime/node_modules/@metro-labs/discord-bot/src/verify.ts +82 -0
  187. package/runtime/node_modules/@metro-labs/discord-bot/src/voice-speak.ts +195 -0
  188. package/runtime/node_modules/@metro-labs/discord-bot/src/voice-transcribe.ts +292 -0
  189. package/runtime/node_modules/@metro-labs/discord-bot/src/voice.ts +197 -0
  190. package/runtime/node_modules/@metro-labs/discord-bot/src/wire.ts +5 -0
  191. package/runtime/node_modules/@metro-labs/http/package.json +33 -0
  192. package/runtime/node_modules/@metro-labs/http/src/api-error.ts +8 -0
  193. package/runtime/node_modules/@metro-labs/http/src/api-http.ts +95 -0
  194. package/runtime/node_modules/@metro-labs/http/src/cors.ts +29 -0
  195. package/runtime/node_modules/@metro-labs/http/src/identity-key.ts +33 -0
  196. package/runtime/node_modules/@metro-labs/http/src/identity-registry.ts +13 -0
  197. package/runtime/node_modules/@metro-labs/http/src/mode-api.ts +32 -0
  198. package/runtime/node_modules/@metro-labs/http/src/return-to.ts +21 -0
  199. package/runtime/node_modules/@metro-labs/http/src/signed-identity.ts +36 -0
  200. package/runtime/node_modules/@metro-labs/telegram/package.json +26 -0
  201. package/runtime/node_modules/@metro-labs/telegram/src/accounts.ts +80 -0
  202. package/runtime/node_modules/@metro-labs/telegram/src/actions.ts +254 -0
  203. package/runtime/node_modules/@metro-labs/telegram/src/attachments.ts +87 -0
  204. package/runtime/node_modules/@metro-labs/telegram/src/client.ts +77 -0
  205. package/runtime/node_modules/@metro-labs/telegram/src/format.ts +156 -0
  206. package/runtime/node_modules/@metro-labs/telegram/src/group.ts +261 -0
  207. package/runtime/node_modules/@metro-labs/telegram/src/history.ts +52 -0
  208. package/runtime/node_modules/@metro-labs/telegram/src/inbound.ts +78 -0
  209. package/runtime/node_modules/@metro-labs/telegram/src/index.ts +46 -0
  210. package/runtime/node_modules/@metro-labs/telegram/src/login.ts +166 -0
  211. package/runtime/node_modules/@metro-labs/telegram/src/media-actions.ts +80 -0
  212. package/runtime/node_modules/@metro-labs/telegram/src/members.ts +75 -0
  213. package/runtime/node_modules/@metro-labs/telegram/src/normalize.ts +22 -0
  214. package/runtime/node_modules/@metro-labs/telegram/src/reactions.ts +167 -0
  215. package/runtime/node_modules/@metro-labs/telegram/src/station.ts +24 -0
  216. package/runtime/node_modules/@metro-labs/telegram/src/types.ts +7 -0
  217. package/runtime/node_modules/@metro-labs/telegram/src/wire.ts +1 -0
  218. package/runtime/node_modules/@metro-labs/telegram-bot/package.json +25 -0
  219. package/runtime/node_modules/@metro-labs/telegram-bot/src/accounts.ts +120 -0
  220. package/runtime/node_modules/@metro-labs/telegram-bot/src/actions.ts +295 -0
  221. package/runtime/node_modules/@metro-labs/telegram-bot/src/attachments.ts +128 -0
  222. package/runtime/node_modules/@metro-labs/telegram-bot/src/format.ts +201 -0
  223. package/runtime/node_modules/@metro-labs/telegram-bot/src/index.ts +109 -0
  224. package/runtime/node_modules/@metro-labs/telegram-bot/src/media-actions.ts +166 -0
  225. package/runtime/node_modules/@metro-labs/telegram-bot/src/members.ts +47 -0
  226. package/runtime/node_modules/@metro-labs/telegram-bot/src/station.ts +17 -0
  227. package/runtime/node_modules/@metro-labs/telegram-bot/src/types.ts +58 -0
  228. package/runtime/node_modules/@metro-labs/telegram-bot/src/verify.ts +38 -0
  229. package/runtime/node_modules/@metro-labs/telegram-bot/src/wire.ts +3 -0
  230. package/runtime/node_modules/@metro-labs/threema/package.json +26 -0
  231. package/runtime/node_modules/@metro-labs/threema/src/accounts.ts +100 -0
  232. package/runtime/node_modules/@metro-labs/threema/src/actions.ts +219 -0
  233. package/runtime/node_modules/@metro-labs/threema/src/api.ts +130 -0
  234. package/runtime/node_modules/@metro-labs/threema/src/crypto.ts +132 -0
  235. package/runtime/node_modules/@metro-labs/threema/src/format.ts +123 -0
  236. package/runtime/node_modules/@metro-labs/threema/src/ids.ts +21 -0
  237. package/runtime/node_modules/@metro-labs/threema/src/index.ts +29 -0
  238. package/runtime/node_modules/@metro-labs/threema/src/station.ts +10 -0
  239. package/runtime/node_modules/@metro-labs/threema/src/verify.ts +117 -0
  240. package/runtime/node_modules/@metro-labs/webhook/package.json +23 -0
  241. package/runtime/node_modules/@metro-labs/webhook/src/station.ts +37 -0
  242. package/runtime/node_modules/@metro-labs/whatsapp/package.json +27 -0
  243. package/runtime/node_modules/@metro-labs/whatsapp/src/accounts.ts +66 -0
  244. package/runtime/node_modules/@metro-labs/whatsapp/src/ack.ts +101 -0
  245. package/runtime/node_modules/@metro-labs/whatsapp/src/actions.ts +190 -0
  246. package/runtime/node_modules/@metro-labs/whatsapp/src/attachments.ts +72 -0
  247. package/runtime/node_modules/@metro-labs/whatsapp/src/auth-state.ts +129 -0
  248. package/runtime/node_modules/@metro-labs/whatsapp/src/client.ts +347 -0
  249. package/runtime/node_modules/@metro-labs/whatsapp/src/delivery.ts +49 -0
  250. package/runtime/node_modules/@metro-labs/whatsapp/src/format.ts +138 -0
  251. package/runtime/node_modules/@metro-labs/whatsapp/src/inbound.ts +57 -0
  252. package/runtime/node_modules/@metro-labs/whatsapp/src/index.ts +40 -0
  253. package/runtime/node_modules/@metro-labs/whatsapp/src/keys.ts +59 -0
  254. package/runtime/node_modules/@metro-labs/whatsapp/src/logger.ts +60 -0
  255. package/runtime/node_modules/@metro-labs/whatsapp/src/login.ts +176 -0
  256. package/runtime/node_modules/@metro-labs/whatsapp/src/media.ts +84 -0
  257. package/runtime/node_modules/@metro-labs/whatsapp/src/normalize.ts +22 -0
  258. package/runtime/node_modules/@metro-labs/whatsapp/src/outbox.ts +38 -0
  259. package/runtime/node_modules/@metro-labs/whatsapp/src/parse.ts +185 -0
  260. package/runtime/node_modules/@metro-labs/whatsapp/src/resolve.ts +52 -0
  261. package/runtime/node_modules/@metro-labs/whatsapp/src/station.ts +18 -0
  262. package/runtime/node_modules/@metro-labs/whatsapp/src/token-store.ts +98 -0
  263. package/runtime/node_modules/@metro-labs/whatsapp/src/types.ts +6 -0
  264. package/runtime/node_modules/@metro-labs/whatsapp/src/wire.ts +5 -0
  265. package/runtime/node_modules/@metro-labs/xmtp/package.json +33 -0
  266. package/runtime/node_modules/@metro-labs/xmtp/src/accounts.ts +112 -0
  267. package/runtime/node_modules/@metro-labs/xmtp/src/actions-close.ts +110 -0
  268. package/runtime/node_modules/@metro-labs/xmtp/src/actions-conv.ts +299 -0
  269. package/runtime/node_modules/@metro-labs/xmtp/src/actions-meta.ts +157 -0
  270. package/runtime/node_modules/@metro-labs/xmtp/src/actions-push.ts +87 -0
  271. package/runtime/node_modules/@metro-labs/xmtp/src/actions.ts +375 -0
  272. package/runtime/node_modules/@metro-labs/xmtp/src/attachments.ts +107 -0
  273. package/runtime/node_modules/@metro-labs/xmtp/src/codecs.ts +171 -0
  274. package/runtime/node_modules/@metro-labs/xmtp/src/conv-helpers.ts +239 -0
  275. package/runtime/node_modules/@metro-labs/xmtp/src/emit-core.ts +55 -0
  276. package/runtime/node_modules/@metro-labs/xmtp/src/emit-payloads.ts +335 -0
  277. package/runtime/node_modules/@metro-labs/xmtp/src/emit.ts +80 -0
  278. package/runtime/node_modules/@metro-labs/xmtp/src/group-members.ts +34 -0
  279. package/runtime/node_modules/@metro-labs/xmtp/src/group.ts +56 -0
  280. package/runtime/node_modules/@metro-labs/xmtp/src/identity.ts +33 -0
  281. package/runtime/node_modules/@metro-labs/xmtp/src/index.ts +125 -0
  282. package/runtime/node_modules/@metro-labs/xmtp/src/labels.ts +157 -0
  283. package/runtime/node_modules/@metro-labs/xmtp/src/member-args.ts +40 -0
  284. package/runtime/node_modules/@metro-labs/xmtp/src/push.ts +349 -0
  285. package/runtime/node_modules/@metro-labs/xmtp/src/station.ts +13 -0
  286. package/runtime/node_modules/@metro-labs/xmtp/src/tools-handlers.ts +126 -0
  287. package/runtime/node_modules/@metro-labs/xmtp/src/tools.ts +212 -0
  288. package/runtime/node_modules/@metro-labs/xmtp/src/transcribe.ts +90 -0
  289. package/runtime/node_modules/@metro-labs/xmtp/src/verify.ts +84 -0
  290. package/runtime/node_modules/@metro-labs/xmtp/src/wire.ts +53 -0
  291. package/runtime/runtime.json +3 -0
  292. package/runtime/server.ts +1 -0
  293. package/runtime/stations.json +39 -0
  294. package/runtime/trains/.keep +0 -0
  295. package/LICENSE +0 -21
  296. package/README.md +0 -252
  297. package/dist/broker.js +0 -202
  298. package/dist/cache.js +0 -69
  299. package/dist/cli/actions.js +0 -206
  300. package/dist/cli/config.js +0 -185
  301. package/dist/cli/index.js +0 -188
  302. package/dist/cli/skill.js +0 -62
  303. package/dist/cli/tail.js +0 -164
  304. package/dist/cli/util.js +0 -72
  305. package/dist/cli/webhook.js +0 -81
  306. package/dist/codex-rc.js +0 -245
  307. package/dist/dispatcher.js +0 -128
  308. package/dist/history.js +0 -121
  309. package/dist/ipc.js +0 -72
  310. package/dist/log.js +0 -15
  311. package/dist/monitor.js +0 -194
  312. package/dist/paths.js +0 -68
  313. package/dist/registry.js +0 -48
  314. package/dist/stations/claude.js +0 -45
  315. package/dist/stations/codex.js +0 -68
  316. package/dist/stations/discord.js +0 -216
  317. package/dist/stations/index.js +0 -129
  318. package/dist/stations/telegram-md.js +0 -34
  319. package/dist/stations/telegram-upload.js +0 -113
  320. package/dist/stations/telegram.js +0 -234
  321. package/dist/stations/webhook.js +0 -103
  322. package/dist/tunnel.js +0 -64
  323. package/dist/webhooks.js +0 -41
  324. package/docs/broker.md +0 -214
  325. package/docs/monitor.md +0 -163
  326. package/docs/uri-scheme.md +0 -124
  327. package/docs/users.md +0 -226
  328. package/skills/metro/SKILL.md +0 -256
@@ -0,0 +1,172 @@
1
+ import { randomUUID } from 'node:crypto';
2
+ import { currentBusSeq } from '@metro-labs/core/events';
3
+ import { errMsg } from '@metro-labs/core/log';
4
+ import { newReplayLedger, type ReplayLedger } from '../channels/relay.js';
5
+ import { McpSession, channelLog } from './session.js';
6
+ import { agentsInScopeKey } from './session-route.js';
7
+ import { sessionScopeKey, type SessionOwnership } from './session-route.js';
8
+ import type { RequestIdentity } from './request-identity.js';
9
+
10
+ export const MAX_SESSIONS = 64;
11
+ export const SESSION_IDLE_MS = 10 * 60_000;
12
+ export const LEDGER_MAX = 256;
13
+ const SWEEP_MS = 60_000;
14
+
15
+ export interface AgentLiveness {
16
+ connected: boolean;
17
+ lastSeenAt: number;
18
+ }
19
+
20
+ export class SessionCapacityError extends Error {
21
+ constructor() {
22
+ super('too many live MCP sessions');
23
+ }
24
+ }
25
+
26
+ export class SessionRegistry {
27
+ private readonly byId = new Map<string, McpSession>();
28
+ private readonly byScope = new Map<string, McpSession>();
29
+ private readonly ledgers = new Map<string, ReplayLedger>();
30
+ private readonly sweeper: ReturnType<typeof setInterval>;
31
+ private readonly log: (...a: unknown[]) => void;
32
+ private started = false;
33
+ private floor = 0;
34
+
35
+ constructor(log: (...a: unknown[]) => void = channelLog) {
36
+ this.log = log;
37
+ this.sweeper = setInterval(() => {
38
+ this.sweep(Date.now());
39
+ }, SWEEP_MS);
40
+ this.sweeper.unref?.();
41
+ }
42
+
43
+ get size(): number {
44
+ return this.byId.size;
45
+ }
46
+
47
+ get(sessionId: string): McpSession | undefined {
48
+ return this.byId.get(sessionId);
49
+ }
50
+
51
+ liveness(): Map<string, AgentLiveness> {
52
+ const out = new Map<string, AgentLiveness>();
53
+ for (const session of this.byId.values())
54
+ for (const id of agentsInScopeKey(session.scopeKey)) {
55
+ const prev = out.get(id);
56
+ out.set(id, {
57
+ connected: (prev?.connected ?? false) || session.streamAttached,
58
+ lastSeenAt: Math.max(prev?.lastSeenAt ?? 0, session.lastSeenAt),
59
+ });
60
+ }
61
+ return out;
62
+ }
63
+
64
+ forScope(scopeKey: string): McpSession | undefined {
65
+ return this.byScope.get(scopeKey);
66
+ }
67
+
68
+ ownership(
69
+ presented: string | undefined,
70
+ scopeKey: string,
71
+ ): SessionOwnership {
72
+ if (presented === undefined) return 'none';
73
+ const found = this.byId.get(presented);
74
+ if (!found) return 'none';
75
+ return found.scopeKey === scopeKey ? 'mine' : 'theirs';
76
+ }
77
+
78
+ startInbound(): void {
79
+ if (this.started) return;
80
+ this.started = true;
81
+ this.floor = currentBusSeq();
82
+ for (const session of this.byId.values()) session.startChannel();
83
+ }
84
+
85
+ async create(
86
+ identity: RequestIdentity,
87
+ adoptId?: string,
88
+ ): Promise<McpSession> {
89
+ const scopeKey = sessionScopeKey(identity);
90
+ const id = adoptId ?? randomUUID();
91
+ await this.byScope.get(scopeKey)?.close();
92
+ await this.byId.get(id)?.close();
93
+ await this.makeRoom();
94
+ const session = await McpSession.open({
95
+ id,
96
+ scopeKey,
97
+ adopted: adoptId !== undefined,
98
+ ledger: this.ledgerFor(scopeKey),
99
+ onClosed: (s) => {
100
+ this.forget(s);
101
+ },
102
+ });
103
+ this.byId.set(id, session);
104
+ this.byScope.set(scopeKey, session);
105
+ if (this.started) session.startChannel();
106
+ this.log('session: opened', 'id', id, 'scope', scopeKey, 'live', this.byId.size);
107
+ return session;
108
+ }
109
+
110
+ async closeScope(scopeKey: string): Promise<boolean> {
111
+ const session = this.byScope.get(scopeKey);
112
+ if (!session) return false;
113
+ this.log('session: closed, credential rotated', 'id', session.id, 'scope', scopeKey);
114
+ await session.close();
115
+ return true;
116
+ }
117
+
118
+ async closeAll(): Promise<void> {
119
+ clearInterval(this.sweeper);
120
+ for (const session of [...this.byId.values()]) await session.close();
121
+ this.ledgers.clear();
122
+ }
123
+
124
+ sweep(now: number): number {
125
+ let reaped = 0;
126
+ for (const session of [...this.byId.values()]) {
127
+ if (session.streamAttached) continue;
128
+ if (now - session.lastSeenAt <= SESSION_IDLE_MS) continue;
129
+ this.log('session: reaped idle', 'id', session.id, 'scope', session.scopeKey);
130
+ session.close().catch((err: unknown) => {
131
+ this.log('session: reap close failed', 'id', session.id, errMsg(err));
132
+ });
133
+ reaped += 1;
134
+ }
135
+ return reaped;
136
+ }
137
+
138
+ private forget(session: McpSession): void {
139
+ if (this.byId.get(session.id) === session) this.byId.delete(session.id);
140
+ if (this.byScope.get(session.scopeKey) === session)
141
+ this.byScope.delete(session.scopeKey);
142
+ }
143
+
144
+ private async makeRoom(): Promise<void> {
145
+ if (this.byId.size < MAX_SESSIONS) return;
146
+ this.sweep(Date.now());
147
+ if (this.byId.size < MAX_SESSIONS) return;
148
+ const idle = [...this.byId.values()]
149
+ .filter((s) => !s.streamAttached)
150
+ .sort((a, b) => a.lastSeenAt - b.lastSeenAt)
151
+ .at(0);
152
+ if (!idle) throw new SessionCapacityError();
153
+ await idle.close();
154
+ }
155
+
156
+ private ledgerFor(scopeKey: string): ReplayLedger {
157
+ const found = this.ledgers.get(scopeKey);
158
+ if (found) {
159
+ this.ledgers.delete(scopeKey);
160
+ this.ledgers.set(scopeKey, found);
161
+ return found;
162
+ }
163
+ const fresh = newReplayLedger(this.started ? this.floor : currentBusSeq());
164
+ this.ledgers.set(scopeKey, fresh);
165
+ while (this.ledgers.size > LEDGER_MAX) {
166
+ const oldest = this.ledgers.keys().next();
167
+ if (oldest.done) break;
168
+ this.ledgers.delete(oldest.value);
169
+ }
170
+ return fresh;
171
+ }
172
+ }
@@ -0,0 +1,43 @@
1
+ import { allowedAgents, type RequestIdentity } from './request-identity.js';
2
+
3
+ export function sessionScopeKey(identity: RequestIdentity): string {
4
+ const ids = [...allowedAgents(identity)].sort();
5
+ if (ids.length > 0) return `${AGENTS_PREFIX}${ids.join(',')}`;
6
+ return `user:${identity.kind === 'session' ? identity.subject : identity.agentId}`;
7
+ }
8
+
9
+ const AGENTS_PREFIX = 'agents:';
10
+
11
+ export function agentsInScopeKey(scopeKey: string): string[] {
12
+ if (!scopeKey.startsWith(AGENTS_PREFIX)) return [];
13
+ return scopeKey.slice(AGENTS_PREFIX.length).split(',').filter(Boolean);
14
+ }
15
+
16
+ export type SessionOwnership = 'none' | 'mine' | 'theirs';
17
+
18
+ export type SessionRoute =
19
+ | { kind: 'create'; adoptId?: string }
20
+ | { kind: 'use' }
21
+ | { kind: 'reject'; status: number; message: string };
22
+
23
+ export interface SessionRouteInput {
24
+ isInitialize: boolean;
25
+ presented: string | undefined;
26
+ ownership: SessionOwnership;
27
+ hasOwnSession: boolean;
28
+ }
29
+
30
+ const NO_SESSION_HEADER = 'Bad Request: Mcp-Session-Id header is required';
31
+ const NOT_FOUND = 'Session not found';
32
+
33
+ export function routeSession(input: SessionRouteInput): SessionRoute {
34
+ if (input.isInitialize) return { kind: 'create' };
35
+ if (input.presented === undefined)
36
+ return input.hasOwnSession
37
+ ? { kind: 'use' }
38
+ : { kind: 'reject', status: 400, message: NO_SESSION_HEADER };
39
+ if (input.ownership === 'mine') return { kind: 'use' };
40
+ if (input.ownership === 'theirs')
41
+ return { kind: 'reject', status: 404, message: NOT_FOUND };
42
+ return { kind: 'create', adoptId: input.presented };
43
+ }
@@ -0,0 +1,215 @@
1
+ import { Server } from '@modelcontextprotocol/sdk/server/index.js';
2
+ import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
3
+ import { InboundRelay } from '../channels/inbound.js';
4
+ import { ChannelRelay, type ReplayLedger } from '../channels/relay.js';
5
+ import { errMsg } from '@metro-labs/core/log';
6
+ import {
7
+ allowlistForLine,
8
+ senderMatchesAllowlist,
9
+ } from '../agents/map.js';
10
+ import { accountStationNames } from '../stations/registry.js';
11
+ import { MCP_INSTRUCTIONS } from './tool-schemas.js';
12
+ import { ChannelOwner } from './channel-owner.js';
13
+ import { BoundedEventStore } from './event-store.js';
14
+ import { registerPermissionRelay } from './permission-relay.js';
15
+ import { registerToolHandlers, toolSchemaSignature } from './tool-dispatch.js';
16
+ import { web, type RawGetSink } from './raw-get-stream.js';
17
+ import type { RequestIdentity } from './request-identity.js';
18
+
19
+ export const channelLog = (...a: unknown[]): void => {
20
+ console.error('[metro-mcp]', ...a);
21
+ };
22
+
23
+ const getStations = (): Set<string> => new Set(accountStationNames());
24
+
25
+ const senderAllowed = (from: string, line: string): boolean => {
26
+ const allowlist = allowlistForLine(line);
27
+ return allowlist ? senderMatchesAllowlist(allowlist, from) : true;
28
+ };
29
+
30
+ function makeTransport(
31
+ id: string,
32
+ eventStore: BoundedEventStore,
33
+ adopted: boolean,
34
+ ): StreamableHTTPServerTransport {
35
+ const t = new StreamableHTTPServerTransport({
36
+ sessionIdGenerator: () => id,
37
+ eventStore,
38
+ });
39
+ if (!adopted) return t;
40
+ const inner = web(t);
41
+ if (inner) {
42
+ inner.sessionId = id;
43
+ inner._initialized = true;
44
+ }
45
+ return t;
46
+ }
47
+
48
+ export interface SessionInit {
49
+ id: string;
50
+ scopeKey: string;
51
+ adopted: boolean;
52
+ ledger: ReplayLedger;
53
+ onClosed: (session: McpSession) => void;
54
+ }
55
+
56
+ export class McpSession {
57
+ readonly id: string;
58
+ readonly scopeKey: string;
59
+ readonly owner = new ChannelOwner();
60
+ readonly eventStore: BoundedEventStore;
61
+ readonly transport: StreamableHTTPServerTransport;
62
+ readonly server: Server;
63
+ readonly relay: InboundRelay;
64
+ readonly channel: ChannelRelay;
65
+ lastSeenAt = Date.now();
66
+ private sink: RawGetSink | undefined;
67
+ private unsubscribe: (() => void) | undefined;
68
+ private closed = false;
69
+ private issuedSchema: string | undefined;
70
+ private announcing = false;
71
+ private readonly onClosed: (session: McpSession) => void;
72
+
73
+ private constructor(init: SessionInit) {
74
+ this.id = init.id;
75
+ this.scopeKey = init.scopeKey;
76
+ this.issuedSchema = init.adopted ? undefined : toolSchemaSignature();
77
+ this.onClosed = init.onClosed;
78
+ this.eventStore = new BoundedEventStore({
79
+ scopeOf: () => this.owner.scope(),
80
+ });
81
+ this.transport = makeTransport(init.id, this.eventStore, init.adopted);
82
+ this.server = new Server(
83
+ { name: 'metro', version: '0.1.0' },
84
+ {
85
+ capabilities: {
86
+ experimental: {
87
+ 'claude/channel': {},
88
+ 'claude/channel/permission': {},
89
+ },
90
+ tools: { listChanged: true },
91
+ },
92
+ instructions: MCP_INSTRUCTIONS,
93
+ },
94
+ );
95
+ registerToolHandlers(this.server, {
96
+ markCurrent: (): void => {
97
+ this.issuedSchema = toolSchemaSignature();
98
+ },
99
+ deliver: (send): void => {
100
+ this.deliverSchemaNotice(send, 'request');
101
+ },
102
+ }, {
103
+ onSent: (id): void => {
104
+ this.relay.noteSent(id);
105
+ },
106
+ });
107
+ this.relay = new InboundRelay({
108
+ mcp: this.server,
109
+ log: channelLog,
110
+ getStations,
111
+ senderAllowed,
112
+ });
113
+ registerPermissionRelay({
114
+ mcp: this.server,
115
+ relay: this.relay,
116
+ owner: this.owner,
117
+ log: channelLog,
118
+ });
119
+ this.channel = new ChannelRelay({
120
+ relay: this.relay,
121
+ log: channelLog,
122
+ inScope: (line) => this.owner.inScope(line),
123
+ ledger: init.ledger,
124
+ });
125
+ }
126
+
127
+ static async open(init: SessionInit): Promise<McpSession> {
128
+ const session = new McpSession(init);
129
+ await session.server.connect(session.transport);
130
+ session.server.onclose = (): void => {
131
+ session.close().catch((err: unknown) => {
132
+ channelLog('session: close failed', 'id', init.id, errMsg(err));
133
+ });
134
+ };
135
+ return session;
136
+ }
137
+
138
+ get streamAttached(): boolean {
139
+ return this.sink !== undefined && !this.sink.closed;
140
+ }
141
+
142
+ get currentSink(): RawGetSink | undefined {
143
+ return this.sink;
144
+ }
145
+
146
+ touch(): void {
147
+ this.lastSeenAt = Date.now();
148
+ }
149
+
150
+ startChannel(): void {
151
+ this.unsubscribe ??= this.channel.start();
152
+ }
153
+
154
+ bindSink(sink: RawGetSink | undefined, identity: RequestIdentity): void {
155
+ this.sink = sink;
156
+ if (sink === undefined) {
157
+ this.owner.releaseStream();
158
+ return;
159
+ }
160
+ this.owner.bindStream(identity);
161
+ this.announceToolSchema();
162
+ }
163
+
164
+ private get schemaNoticeDue(): boolean {
165
+ return this.issuedSchema !== toolSchemaSignature();
166
+ }
167
+
168
+ private announceToolSchema(): void {
169
+ if (!this.streamAttached) return;
170
+ this.deliverSchemaNotice(
171
+ () => this.server.sendToolListChanged(),
172
+ 'stream',
173
+ );
174
+ }
175
+
176
+ private deliverSchemaNotice(send: () => Promise<void>, via: string): void {
177
+ if (!this.schemaNoticeDue || this.announcing) return;
178
+ this.announcing = true;
179
+ channelLog(
180
+ 'session: tool list changed',
181
+ 'id',
182
+ this.id,
183
+ 'scope',
184
+ this.scopeKey,
185
+ 'via',
186
+ via,
187
+ );
188
+ send()
189
+ .then(() => {
190
+ this.issuedSchema = toolSchemaSignature();
191
+ this.announcing = false;
192
+ })
193
+ .catch((err: unknown) => {
194
+ this.announcing = false;
195
+ channelLog('session: tool list changed notice failed', errMsg(err));
196
+ });
197
+ }
198
+
199
+ dropStream(): void {
200
+ const sink = this.sink;
201
+ this.sink = undefined;
202
+ this.owner.releaseStream();
203
+ sink?.close();
204
+ }
205
+
206
+ async close(): Promise<void> {
207
+ if (this.closed) return;
208
+ this.closed = true;
209
+ this.onClosed(this);
210
+ this.dropStream();
211
+ this.unsubscribe?.();
212
+ this.unsubscribe = undefined;
213
+ await this.server.close().catch(() => undefined);
214
+ }
215
+ }
@@ -0,0 +1,215 @@
1
+ import { createHash } from 'node:crypto';
2
+ import type { Server } from '@modelcontextprotocol/sdk/server/index.js';
3
+ import {
4
+ ListToolsRequestSchema,
5
+ CallToolRequestSchema,
6
+ } from '@modelcontextprotocol/sdk/types.js';
7
+ import { gatherAccounts } from './accounts.js';
8
+ import {
9
+ STATIONS,
10
+ accountStationCapabilities,
11
+ } from '../stations/registry.js';
12
+ import type { Station, StationTool, ToolResult } from '@metro-labs/core/stations/types';
13
+ import { COMMON_TOOLS, LIST_ACCOUNTS_TOOL } from './tool-schemas.js';
14
+ import { errResult, makeCtx, okJson, toErr } from './ctx.js';
15
+ import { dispatchMessageTool, type ToolHooks } from './call-tools.js';
16
+ import { dispatchListMembers } from './member-tools.js';
17
+ import {
18
+ dispatchAddMembers,
19
+ dispatchCreateGroup,
20
+ dispatchInviteLink,
21
+ dispatchRemoveMembers,
22
+ } from './group-tools.js';
23
+ import { dispatchCreateUpload } from './upload-tool.js';
24
+ import { callTargetDenied, lineTargetDenied } from '../agents/scope.js';
25
+ import { SOURCE_KEYS } from '../stations/attach-resolve.js';
26
+ import { decodedLengthOf } from '../stations/attach-inline.js';
27
+ import { log } from '@metro-labs/core/log';
28
+ import {
29
+ allowedAgents,
30
+ currentIdentity,
31
+ type RequestIdentity,
32
+ } from './request-identity.js';
33
+ import { str } from '@metro-labs/core/str';
34
+
35
+ const STATION_TOOLS = new Map<
36
+ string,
37
+ { station: Station; tool: StationTool }
38
+ >();
39
+ for (const s of STATIONS)
40
+ for (const t of s.tools) STATION_TOOLS.set(t.name, { station: s, tool: t });
41
+
42
+ const CORE_DISPATCH: Record<
43
+ string,
44
+ (a: Record<string, unknown>) => Promise<ToolResult>
45
+ > = {
46
+ list_members: dispatchListMembers,
47
+ create_group: dispatchCreateGroup,
48
+ add_members: dispatchAddMembers,
49
+ remove_members: dispatchRemoveMembers,
50
+ export_invite: dispatchInviteLink,
51
+ create_upload: dispatchCreateUpload,
52
+ };
53
+
54
+ const toolList = (): { tools: unknown[] } => ({
55
+ tools: [
56
+ ...COMMON_TOOLS,
57
+ ...STATIONS.flatMap((s) =>
58
+ s.tools.map((t) => ({
59
+ name: t.name,
60
+ description: t.description,
61
+ inputSchema: t.inputSchema,
62
+ })),
63
+ ),
64
+ LIST_ACCOUNTS_TOOL,
65
+ ],
66
+ });
67
+
68
+ let schemaSignature: string | undefined;
69
+
70
+ export const toolSchemaSignature = (): string => {
71
+ schemaSignature ??= createHash('sha256')
72
+ .update(JSON.stringify(toolList()))
73
+ .digest('hex')
74
+ .slice(0, 16);
75
+ return schemaSignature;
76
+ };
77
+
78
+ async function handleListAccounts(
79
+ identity: RequestIdentity | undefined,
80
+ ): Promise<ToolResult> {
81
+ try {
82
+ return okJson({
83
+ accounts: await gatherAccounts(allowedAgents(identity)),
84
+ capabilities: accountStationCapabilities(),
85
+ });
86
+ } catch (e) {
87
+ return errResult(`metro list_accounts failed: ${String(e)}`);
88
+ }
89
+ }
90
+
91
+ function stationForTool(
92
+ name: string,
93
+ args: Record<string, unknown>,
94
+ ): string | undefined {
95
+ if (name === 'create_group') return str(args.station) || undefined;
96
+ return STATION_TOOLS.get(name)?.station.name;
97
+ }
98
+
99
+ export function scopeDenied(
100
+ identity: RequestIdentity | undefined,
101
+ name: string,
102
+ args: Record<string, unknown>,
103
+ ): boolean {
104
+ const allowed = allowedAgents(identity);
105
+ const station = stationForTool(name, args);
106
+ if (station !== undefined) return callTargetDenied(allowed, station, args);
107
+ return lineTargetDenied(allowed, args);
108
+ }
109
+
110
+ const sourceOf = (entry: unknown): string => {
111
+ const att = (entry ?? {}) as Record<string, unknown>;
112
+ const named = SOURCE_KEYS.filter(
113
+ (k) => typeof att[k] === 'string' && att[k] !== '',
114
+ );
115
+ if (named.length === 0) return 'none';
116
+ return named
117
+ .map((k) =>
118
+ k === 'data' ? `data(${decodedLengthOf(String(att.data))}B)` : k,
119
+ )
120
+ .join('+');
121
+ };
122
+
123
+ export const attachmentShape = (
124
+ a: Record<string, unknown>,
125
+ ): string[] | undefined =>
126
+ Array.isArray(a.attachments) ? a.attachments.map(sourceOf) : undefined;
127
+
128
+ function logToolFailure(
129
+ name: string,
130
+ a: Record<string, unknown>,
131
+ result: ToolResult,
132
+ ): void {
133
+ log.warn(
134
+ {
135
+ tool: name,
136
+ agents: [...allowedAgents(currentIdentity())],
137
+ line: str(a.line),
138
+ attachments: attachmentShape(a),
139
+ err: result.content[0]?.text,
140
+ },
141
+ 'mcp: tool call failed',
142
+ );
143
+ }
144
+
145
+ async function runTool(
146
+ req: { params: { name: string; arguments?: Record<string, unknown> } },
147
+ hooks: ToolHooks,
148
+ ): Promise<ToolResult> {
149
+ const name = req.params.name;
150
+ const a = req.params.arguments ?? {};
151
+
152
+ const identity = currentIdentity();
153
+ if (name !== 'list_accounts' && scopeDenied(identity, name, a))
154
+ return errResult('metro: this account is outside your authorized scope');
155
+
156
+ const owned = STATION_TOOLS.get(name);
157
+ if (owned) {
158
+ try {
159
+ return await owned.tool.handle(a, makeCtx(owned.station.name));
160
+ } catch (e) {
161
+ return toErr(name, e);
162
+ }
163
+ }
164
+
165
+ const core = CORE_DISPATCH[name];
166
+ if (core) return core(a);
167
+
168
+ if (name === 'list_accounts') return handleListAccounts(identity);
169
+
170
+ return dispatchMessageTool(name, a, hooks);
171
+ }
172
+
173
+ export async function callToolHandler(
174
+ req: { params: { name: string; arguments?: Record<string, unknown> } },
175
+ hooks: ToolHooks = {},
176
+ ): Promise<ToolResult> {
177
+ const result = await runTool(req, hooks);
178
+ if (result.isError === true)
179
+ logToolFailure(req.params.name, req.params.arguments ?? {}, result);
180
+ return result;
181
+ }
182
+
183
+ const TOOL_LIST_CHANGED = 'notifications/tools/list_changed';
184
+
185
+ export interface SchemaNotice {
186
+ markCurrent: () => void;
187
+ deliver: (send: () => Promise<void>) => void;
188
+ }
189
+
190
+ interface NotifyingExtra {
191
+ sendNotification: (n: { method: string }) => Promise<void>;
192
+ }
193
+
194
+ export function registerToolHandlers(
195
+ server: Server,
196
+ notice?: SchemaNotice,
197
+ hooks: ToolHooks = {},
198
+ ): void {
199
+ server.setRequestHandler(ListToolsRequestSchema, () => {
200
+ notice?.markCurrent();
201
+ return toolList();
202
+ });
203
+ server.setRequestHandler(
204
+ CallToolRequestSchema,
205
+ ((
206
+ req: { params: { name: string; arguments?: Record<string, unknown> } },
207
+ extra: NotifyingExtra,
208
+ ): Promise<ToolResult> => {
209
+ notice?.deliver(() =>
210
+ extra.sendNotification({ method: TOOL_LIST_CHANGED }),
211
+ );
212
+ return callToolHandler(req, hooks);
213
+ }) as Parameters<typeof server.setRequestHandler>[1],
214
+ );
215
+ }