@belte/belte 0.19.0

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 (326) hide show
  1. package/CHANGELOG.md +313 -0
  2. package/LICENSE +21 -0
  3. package/README.md +559 -0
  4. package/bin/belte.ts +183 -0
  5. package/package.json +110 -0
  6. package/src/App.svelte +31 -0
  7. package/src/appEntry.ts +151 -0
  8. package/src/assets/app.html +14 -0
  9. package/src/belteResolverPlugin.ts +858 -0
  10. package/src/build.ts +147 -0
  11. package/src/buildCli.ts +129 -0
  12. package/src/buildDisconnected.ts +122 -0
  13. package/src/bundleApp.ts +149 -0
  14. package/src/bundleDisconnectedEntry.ts +17 -0
  15. package/src/cliEntry.ts +25 -0
  16. package/src/clientBuildPlugins.ts +41 -0
  17. package/src/clientEntry.ts +7 -0
  18. package/src/compile.ts +64 -0
  19. package/src/controlServerWorker.ts +422 -0
  20. package/src/dedupeSveltePlugin.ts +66 -0
  21. package/src/devEntry.ts +169 -0
  22. package/src/discoveryEntry.ts +81 -0
  23. package/src/lib/browser/applyStreamedResolution.ts +33 -0
  24. package/src/lib/browser/cacheEntryFromSnapshot.ts +48 -0
  25. package/src/lib/browser/flushUnresolvedPlaceholders.ts +16 -0
  26. package/src/lib/browser/installStreamingPlaceholders.ts +32 -0
  27. package/src/lib/browser/openResolveStream.ts +42 -0
  28. package/src/lib/browser/page.svelte.ts +258 -0
  29. package/src/lib/browser/pageStreamController.ts +17 -0
  30. package/src/lib/browser/refetchPlaceholder.ts +12 -0
  31. package/src/lib/browser/remoteProxy.ts +37 -0
  32. package/src/lib/browser/socketChannel.ts +192 -0
  33. package/src/lib/browser/socketProxy.ts +57 -0
  34. package/src/lib/browser/startClient.ts +153 -0
  35. package/src/lib/browser/subscribe.ts +131 -0
  36. package/src/lib/browser/types/Errors.ts +9 -0
  37. package/src/lib/browser/types/Layouts.ts +7 -0
  38. package/src/lib/browser/types/Pages.ts +7 -0
  39. package/src/lib/browser/types/StreamingDeferred.ts +9 -0
  40. package/src/lib/bundle/BundleMenu.ts +11 -0
  41. package/src/lib/bundle/BundleMenuItem.ts +24 -0
  42. package/src/lib/bundle/BundleWindow.ts +36 -0
  43. package/src/lib/bundle/WEBVIEW_BUILD_REVISION.ts +9 -0
  44. package/src/lib/bundle/WEBVIEW_VERSION.ts +7 -0
  45. package/src/lib/bundle/bindConnectedFlag.ts +29 -0
  46. package/src/lib/bundle/bindRequestNavigate.ts +31 -0
  47. package/src/lib/bundle/buildWebviewLib.ts +111 -0
  48. package/src/lib/bundle/disconnected.css +9 -0
  49. package/src/lib/bundle/disconnected.svelte +386 -0
  50. package/src/lib/bundle/ensureWebviewLib.ts +20 -0
  51. package/src/lib/bundle/exitWithParent.ts +28 -0
  52. package/src/lib/bundle/infoPlist.ts +46 -0
  53. package/src/lib/bundle/installDownloads.ts +24 -0
  54. package/src/lib/bundle/installMacMenu.ts +39 -0
  55. package/src/lib/bundle/listenLocalControlServer.ts +19 -0
  56. package/src/lib/bundle/native/belteMenu.mm +422 -0
  57. package/src/lib/bundle/native/webview.h +4557 -0
  58. package/src/lib/bundle/onMenu.ts +41 -0
  59. package/src/lib/bundle/openWebview.ts +104 -0
  60. package/src/lib/bundle/pngToIcns.ts +47 -0
  61. package/src/lib/bundle/probeBelteServer.ts +34 -0
  62. package/src/lib/bundle/resolveServerBinary.ts +12 -0
  63. package/src/lib/bundle/resolveWebviewLib.ts +53 -0
  64. package/src/lib/bundle/serverBinaryFilename.ts +8 -0
  65. package/src/lib/bundle/signMacApp.ts +35 -0
  66. package/src/lib/bundle/spawnEmbeddedServer.ts +65 -0
  67. package/src/lib/bundle/stableLocalPort.ts +19 -0
  68. package/src/lib/bundle/waitForServer.ts +23 -0
  69. package/src/lib/bundle/webviewCachePath.ts +23 -0
  70. package/src/lib/bundle/webviewLibName.ts +11 -0
  71. package/src/lib/cli/connectToServer.ts +23 -0
  72. package/src/lib/cli/createClient.ts +170 -0
  73. package/src/lib/cli/dispatchCommand.ts +71 -0
  74. package/src/lib/cli/loadEnvFromBinaryDir.ts +16 -0
  75. package/src/lib/cli/parseArgvForRpc.ts +97 -0
  76. package/src/lib/cli/printHelp.ts +119 -0
  77. package/src/lib/cli/printSessionHelp.ts +27 -0
  78. package/src/lib/cli/printSessionStatus.ts +21 -0
  79. package/src/lib/cli/printTrimmed.ts +8 -0
  80. package/src/lib/cli/printValue.ts +10 -0
  81. package/src/lib/cli/resolveCliTarget.ts +48 -0
  82. package/src/lib/cli/runCli.ts +139 -0
  83. package/src/lib/cli/runSession.ts +105 -0
  84. package/src/lib/cli/startLocalInstance.ts +14 -0
  85. package/src/lib/cli/tokenizeLine.ts +51 -0
  86. package/src/lib/cli/types/CliManifest.ts +9 -0
  87. package/src/lib/cli/types/CliManifestEntry.ts +17 -0
  88. package/src/lib/cli/types/CliTarget.ts +13 -0
  89. package/src/lib/mcp/annotationsForMethod.ts +29 -0
  90. package/src/lib/mcp/createMcpResourceServer.ts +101 -0
  91. package/src/lib/mcp/createMcpServer.ts +42 -0
  92. package/src/lib/mcp/dispatchMcpRequest.ts +146 -0
  93. package/src/lib/mcp/mcpResourceServerSlot.ts +18 -0
  94. package/src/lib/mcp/mcpSurface.ts +265 -0
  95. package/src/lib/mcp/toolResultFromResponse.ts +66 -0
  96. package/src/lib/mcp/types/JsonRpcRequest.ts +12 -0
  97. package/src/lib/mcp/types/JsonRpcResponse.ts +20 -0
  98. package/src/lib/mcp/types/McpResourceContents.ts +10 -0
  99. package/src/lib/mcp/types/McpResourceDescriptor.ts +6 -0
  100. package/src/lib/mcp/types/McpResourceServer.ts +12 -0
  101. package/src/lib/mcp/types/McpServer.ts +9 -0
  102. package/src/lib/mcp/types/McpServerOptions.ts +16 -0
  103. package/src/lib/server/AppModule.ts +33 -0
  104. package/src/lib/server/DELETE.ts +9 -0
  105. package/src/lib/server/GET.ts +9 -0
  106. package/src/lib/server/HEAD.ts +9 -0
  107. package/src/lib/server/PATCH.ts +9 -0
  108. package/src/lib/server/POST.ts +9 -0
  109. package/src/lib/server/PUT.ts +9 -0
  110. package/src/lib/server/agent.ts +76 -0
  111. package/src/lib/server/appDataDir.ts +15 -0
  112. package/src/lib/server/cli/buildEnvContent.ts +19 -0
  113. package/src/lib/server/cli/createTarGz.ts +76 -0
  114. package/src/lib/server/cli/handleCliDownload.ts +153 -0
  115. package/src/lib/server/cli/handleCliInstall.ts +37 -0
  116. package/src/lib/server/cli/installScript.ts +29 -0
  117. package/src/lib/server/cli/maxSourceMtime.ts +26 -0
  118. package/src/lib/server/cookies.ts +29 -0
  119. package/src/lib/server/env.ts +50 -0
  120. package/src/lib/server/error.ts +70 -0
  121. package/src/lib/server/json.ts +28 -0
  122. package/src/lib/server/jsonl.ts +46 -0
  123. package/src/lib/server/prompts/definePrompt.ts +20 -0
  124. package/src/lib/server/prompts/promptRegistry.ts +9 -0
  125. package/src/lib/server/prompts/registerPrompt.ts +6 -0
  126. package/src/lib/server/prompts/renderPromptTemplate.ts +16 -0
  127. package/src/lib/server/prompts/types/Prompt.ts +13 -0
  128. package/src/lib/server/prompts/types/PromptOptions.ts +12 -0
  129. package/src/lib/server/prompts/types/PromptRegistryEntry.ts +13 -0
  130. package/src/lib/server/prompts/types/PromptRoutes.ts +10 -0
  131. package/src/lib/server/redirect.ts +42 -0
  132. package/src/lib/server/request.ts +18 -0
  133. package/src/lib/server/rpc/defineVerb.ts +133 -0
  134. package/src/lib/server/rpc/dispatchVerbInProcess.ts +46 -0
  135. package/src/lib/server/rpc/findVerbByCommandName.ts +18 -0
  136. package/src/lib/server/rpc/parseArgs.ts +95 -0
  137. package/src/lib/server/rpc/registerVerb.ts +6 -0
  138. package/src/lib/server/rpc/types/RemoteHandler.ts +27 -0
  139. package/src/lib/server/rpc/types/RemoteRoutes.ts +13 -0
  140. package/src/lib/server/rpc/types/TypedResponse.ts +18 -0
  141. package/src/lib/server/rpc/types/VerbHelper.ts +68 -0
  142. package/src/lib/server/rpc/types/VerbRegistryEntry.ts +29 -0
  143. package/src/lib/server/rpc/unprocessed.ts +14 -0
  144. package/src/lib/server/rpc/verbRegistry.ts +11 -0
  145. package/src/lib/server/runtime/DEFAULT_PORT.ts +6 -0
  146. package/src/lib/server/runtime/DEV_REBUILD_MESSAGE.ts +4 -0
  147. package/src/lib/server/runtime/DEV_RELOAD_CLIENT_SCRIPT.ts +29 -0
  148. package/src/lib/server/runtime/acceptsZstd.ts +8 -0
  149. package/src/lib/server/runtime/buildOpenApiSpec.ts +106 -0
  150. package/src/lib/server/runtime/cacheControlForAsset.ts +22 -0
  151. package/src/lib/server/runtime/containsTraversal.ts +37 -0
  152. package/src/lib/server/runtime/createAssetHeaderCache.ts +35 -0
  153. package/src/lib/server/runtime/createPublicAssetServer.ts +63 -0
  154. package/src/lib/server/runtime/createRouteDispatcher.ts +100 -0
  155. package/src/lib/server/runtime/createServer.ts +692 -0
  156. package/src/lib/server/runtime/devReloadResponse.ts +35 -0
  157. package/src/lib/server/runtime/disableIdleTimeoutForStream.ts +27 -0
  158. package/src/lib/server/runtime/envSchemaStore.ts +15 -0
  159. package/src/lib/server/runtime/findOpenPort.ts +35 -0
  160. package/src/lib/server/runtime/getActiveServer.ts +6 -0
  161. package/src/lib/server/runtime/globToPathSet.ts +29 -0
  162. package/src/lib/server/runtime/inProcessServer.ts +20 -0
  163. package/src/lib/server/runtime/internalErrorResponse.ts +25 -0
  164. package/src/lib/server/runtime/isCrossOriginUpgrade.ts +19 -0
  165. package/src/lib/server/runtime/listenOnOpenPort.ts +36 -0
  166. package/src/lib/server/runtime/logExposedSurfaces.ts +162 -0
  167. package/src/lib/server/runtime/mimeForExtension.ts +20 -0
  168. package/src/lib/server/runtime/parseIdleTimeout.ts +10 -0
  169. package/src/lib/server/runtime/parsePort.ts +11 -0
  170. package/src/lib/server/runtime/registryManifests.ts +66 -0
  171. package/src/lib/server/runtime/requestContext.ts +5 -0
  172. package/src/lib/server/runtime/resolveStreamResponse.ts +29 -0
  173. package/src/lib/server/runtime/runWithRequestScope.ts +57 -0
  174. package/src/lib/server/runtime/safeJsonForScript.ts +17 -0
  175. package/src/lib/server/runtime/serializeCacheSnapshot.ts +45 -0
  176. package/src/lib/server/runtime/serverSlot.ts +13 -0
  177. package/src/lib/server/runtime/setActiveServer.ts +6 -0
  178. package/src/lib/server/runtime/snapshotEntryFromCache.ts +81 -0
  179. package/src/lib/server/runtime/streamCacheResolutions.ts +37 -0
  180. package/src/lib/server/runtime/streamFromIterator.ts +86 -0
  181. package/src/lib/server/runtime/streamStash.ts +64 -0
  182. package/src/lib/server/runtime/types/Assets.ts +1 -0
  183. package/src/lib/server/runtime/types/RequestStore.ts +27 -0
  184. package/src/lib/server/runtime/withResponseDefaults.ts +24 -0
  185. package/src/lib/server/server.ts +32 -0
  186. package/src/lib/server/socket.ts +31 -0
  187. package/src/lib/server/sockets/createSocketDispatcher.ts +311 -0
  188. package/src/lib/server/sockets/defineSocket.ts +167 -0
  189. package/src/lib/server/sockets/lookupSocket.ts +6 -0
  190. package/src/lib/server/sockets/recentHistory.ts +11 -0
  191. package/src/lib/server/sockets/registerSocket.ts +6 -0
  192. package/src/lib/server/sockets/socketOperations.ts +35 -0
  193. package/src/lib/server/sockets/socketRegistry.ts +9 -0
  194. package/src/lib/server/sockets/types/Socket.ts +21 -0
  195. package/src/lib/server/sockets/types/SocketClientFrame.ts +18 -0
  196. package/src/lib/server/sockets/types/SocketOperation.ts +22 -0
  197. package/src/lib/server/sockets/types/SocketOptions.ts +22 -0
  198. package/src/lib/server/sockets/types/SocketRegistryEntry.ts +17 -0
  199. package/src/lib/server/sockets/types/SocketRoutes.ts +10 -0
  200. package/src/lib/server/sockets/types/SocketServerFrame.ts +15 -0
  201. package/src/lib/server/sse.ts +53 -0
  202. package/src/lib/shared/BELTE_PACKAGE_NAME.ts +7 -0
  203. package/src/lib/shared/CACHE_CONTROL_VALUES.ts +16 -0
  204. package/src/lib/shared/HttpError.ts +19 -0
  205. package/src/lib/shared/RESOLVE_STREAM_PATH.ts +7 -0
  206. package/src/lib/shared/STREAMING_CONTENT_TYPES.ts +11 -0
  207. package/src/lib/shared/activeCacheStore.ts +20 -0
  208. package/src/lib/shared/appDataDir.ts +34 -0
  209. package/src/lib/shared/belteImportName.ts +44 -0
  210. package/src/lib/shared/browserClientFlags.ts +10 -0
  211. package/src/lib/shared/buildRpcRequest.ts +70 -0
  212. package/src/lib/shared/bundleLayout.ts +36 -0
  213. package/src/lib/shared/bundled.ts +34 -0
  214. package/src/lib/shared/cache.ts +559 -0
  215. package/src/lib/shared/cacheStoreSlot.ts +16 -0
  216. package/src/lib/shared/canonicalJson.ts +63 -0
  217. package/src/lib/shared/carriesBodyArgs.ts +13 -0
  218. package/src/lib/shared/clearLastConnection.ts +7 -0
  219. package/src/lib/shared/commandNameForUrl.ts +17 -0
  220. package/src/lib/shared/createCacheStore.ts +75 -0
  221. package/src/lib/shared/createPushIterator.ts +93 -0
  222. package/src/lib/shared/createRemoteFunction.ts +99 -0
  223. package/src/lib/shared/decodeResponse.ts +47 -0
  224. package/src/lib/shared/detectTarget.ts +27 -0
  225. package/src/lib/shared/exeSuffix.ts +9 -0
  226. package/src/lib/shared/exitOnBuildFailure.ts +17 -0
  227. package/src/lib/shared/extraForwardHeaders.ts +16 -0
  228. package/src/lib/shared/fileStem.ts +9 -0
  229. package/src/lib/shared/findExportCallSite.ts +479 -0
  230. package/src/lib/shared/forwardHeaders.ts +41 -0
  231. package/src/lib/shared/getRemoteMeta.ts +5 -0
  232. package/src/lib/shared/globalCacheStore.ts +15 -0
  233. package/src/lib/shared/globalCacheStoreSlot.ts +14 -0
  234. package/src/lib/shared/importNamesToStrip.ts +13 -0
  235. package/src/lib/shared/invalidateEvent.ts +11 -0
  236. package/src/lib/shared/isCompileTarget.ts +15 -0
  237. package/src/lib/shared/isDebugEnabled.ts +23 -0
  238. package/src/lib/shared/isModuleNotFound.ts +16 -0
  239. package/src/lib/shared/isReadOnlyMethod.ts +14 -0
  240. package/src/lib/shared/isStreamingResponse.ts +11 -0
  241. package/src/lib/shared/jsonSchemaForPromptArguments.ts +29 -0
  242. package/src/lib/shared/jsonSchemaForSchema.ts +32 -0
  243. package/src/lib/shared/jsonlErrorFrame.ts +24 -0
  244. package/src/lib/shared/keyForRemoteCall.ts +29 -0
  245. package/src/lib/shared/lastConnectionPath.ts +7 -0
  246. package/src/lib/shared/loadEnvFile.ts +17 -0
  247. package/src/lib/shared/loadEnvFromDataDir.ts +15 -0
  248. package/src/lib/shared/loadSvelteConfig.ts +18 -0
  249. package/src/lib/shared/log.ts +104 -0
  250. package/src/lib/shared/manifestModule.ts +39 -0
  251. package/src/lib/shared/memoizeByKey.ts +24 -0
  252. package/src/lib/shared/nearestLayoutPrefix.ts +36 -0
  253. package/src/lib/shared/normalizeTarget.ts +10 -0
  254. package/src/lib/shared/pageUrlForFile.ts +14 -0
  255. package/src/lib/shared/parseBoundedEnvInt.ts +20 -0
  256. package/src/lib/shared/parseEnv.ts +30 -0
  257. package/src/lib/shared/parsePromptMarkdown.ts +34 -0
  258. package/src/lib/shared/parseRouteSegments.ts +22 -0
  259. package/src/lib/shared/prepareRpcModule.ts +59 -0
  260. package/src/lib/shared/prepareSocketModule.ts +49 -0
  261. package/src/lib/shared/programNameForPackage.ts +14 -0
  262. package/src/lib/shared/promptNameForFile.ts +10 -0
  263. package/src/lib/shared/queryStringFromArgs.ts +27 -0
  264. package/src/lib/shared/readEnvFile.ts +15 -0
  265. package/src/lib/shared/readLastConnection.ts +18 -0
  266. package/src/lib/shared/readPackageJson.ts +9 -0
  267. package/src/lib/shared/recordRemoteMeta.ts +5 -0
  268. package/src/lib/shared/remoteMetaStore.ts +16 -0
  269. package/src/lib/shared/resolveClientFlags.ts +20 -0
  270. package/src/lib/shared/responseErrorText.ts +9 -0
  271. package/src/lib/shared/rpcUrlForFile.ts +19 -0
  272. package/src/lib/shared/runningAsStandaloneBinary.ts +13 -0
  273. package/src/lib/shared/serializeEnv.ts +18 -0
  274. package/src/lib/shared/setCacheStoreResolver.ts +6 -0
  275. package/src/lib/shared/setGlobalCacheStoreResolver.ts +6 -0
  276. package/src/lib/shared/socketNameForFile.ts +11 -0
  277. package/src/lib/shared/sseErrorFrame.ts +29 -0
  278. package/src/lib/shared/streamResponse.ts +169 -0
  279. package/src/lib/shared/stripImport.ts +27 -0
  280. package/src/lib/shared/subscribableFromResponse.ts +51 -0
  281. package/src/lib/shared/toBunRoutePattern.ts +28 -0
  282. package/src/lib/shared/types/CacheEntry.ts +63 -0
  283. package/src/lib/shared/types/CacheInvalidation.ts +9 -0
  284. package/src/lib/shared/types/CacheOptions.ts +33 -0
  285. package/src/lib/shared/types/CacheSnapshot.ts +16 -0
  286. package/src/lib/shared/types/CacheSnapshotEntry.ts +15 -0
  287. package/src/lib/shared/types/CacheStore.ts +32 -0
  288. package/src/lib/shared/types/ClientFlags.ts +11 -0
  289. package/src/lib/shared/types/CompileTarget.ts +6 -0
  290. package/src/lib/shared/types/HttpVerb.ts +1 -0
  291. package/src/lib/shared/types/LastConnection.ts +9 -0
  292. package/src/lib/shared/types/PromptArgument.ts +12 -0
  293. package/src/lib/shared/types/RawRemoteFunction.ts +13 -0
  294. package/src/lib/shared/types/RemoteFunction.ts +42 -0
  295. package/src/lib/shared/types/StandardSchemaV1.ts +57 -0
  296. package/src/lib/shared/types/StreamedResolution.ts +10 -0
  297. package/src/lib/shared/types/StreamingPlaceholder.ts +13 -0
  298. package/src/lib/shared/types/Subscribable.ts +15 -0
  299. package/src/lib/shared/types/SvelteConfig.ts +5 -0
  300. package/src/lib/shared/withJsonSchema.ts +20 -0
  301. package/src/lib/shared/writeLastConnection.ts +13 -0
  302. package/src/lib/shared/writeRoutesDts.ts +67 -0
  303. package/src/lib/test/clearVerbRegistry.ts +11 -0
  304. package/src/lib/test/createTestClient.ts +78 -0
  305. package/src/preload.ts +20 -0
  306. package/src/scaffold.ts +92 -0
  307. package/src/serverBuildPlugins.ts +25 -0
  308. package/src/serverEntry.ts +94 -0
  309. package/src/sveltePlugin.ts +58 -0
  310. package/src/tailwindStylePreprocessor.ts +62 -0
  311. package/template/bunfig.toml +4 -0
  312. package/template/package.json +19 -0
  313. package/template/src/app.ts +23 -0
  314. package/template/src/browser/app.css +21 -0
  315. package/template/src/browser/app.html +24 -0
  316. package/template/src/browser/pages/about/page.svelte +5 -0
  317. package/template/src/browser/pages/layout.svelte +26 -0
  318. package/template/src/browser/pages/page.svelte +20 -0
  319. package/template/src/bundle/icon.png +0 -0
  320. package/template/src/cli/banner.txt +3 -0
  321. package/template/src/cli/footer.txt +1 -0
  322. package/template/src/server/config.ts +17 -0
  323. package/template/src/server/rpc/getHello.ts +35 -0
  324. package/template/svelte.config.js +12 -0
  325. package/template/tsconfig.json +18 -0
  326. package/tsconfig.app.json +16 -0
@@ -0,0 +1,139 @@
1
+ import { clearLastConnection } from '../shared/clearLastConnection.ts'
2
+ import { loadEnvFromDataDir } from '../shared/loadEnvFromDataDir.ts'
3
+ import { connectToServer } from './connectToServer.ts'
4
+ import { dispatchCommand } from './dispatchCommand.ts'
5
+ import { loadEnvFromBinaryDir } from './loadEnvFromBinaryDir.ts'
6
+ import { printCommandHelp, printTopLevelHelp } from './printHelp.ts'
7
+ import { printTrimmed } from './printTrimmed.ts'
8
+ import { resolveCliTarget } from './resolveCliTarget.ts'
9
+ import { runSession } from './runSession.ts'
10
+ import { startLocalInstance } from './startLocalInstance.ts'
11
+ import type { CliManifest } from './types/CliManifest.ts'
12
+ import type { CliTarget } from './types/CliTarget.ts'
13
+
14
+ const isHelpFlag = (arg: string): boolean => arg === '--help' || arg === '-h'
15
+
16
+ /*
17
+ Top-level CLI driver for the standalone binary. The binary is a thin remote client
18
+ — it carries no handler code, so it always talks to a running server over HTTP, but
19
+ it can boot one: the full binary ships the server beside it, so `/start` spawns a
20
+ local instance. One rule governs the first positional — `/` manages the connection,
21
+ a bare word runs a command:
22
+
23
+ --help / -h → top-level help
24
+ /help [cmd] → help (per-command with an arg)
25
+ (none) + TTY → interactive session, resuming the saved connection
26
+ (none) + non-TTY → top-level help (scripts use `<cmd>` one-shot)
27
+ /connect <url> → connect to a remote server, open a session
28
+ /start → boot a local instance, open a session
29
+ /disconnect → forget the saved connection, exit
30
+ <cmd> [--flags] → one-shot RPC against the resumed target
31
+
32
+ The connection verbs are `/`-prefixed only — no bare aliases — so a bare word is
33
+ always an RPC command and never collides. Env layers BELTE_APP_URL/BELTE_APP_TOKEN (shell >
34
+ data-dir > binary-dir) supply the baked default a fresh download resumes against.
35
+ */
36
+ export async function runCli({
37
+ programName,
38
+ manifest,
39
+ banner = '',
40
+ footer = '',
41
+ argv,
42
+ }: {
43
+ programName: string
44
+ manifest: CliManifest
45
+ banner?: string
46
+ footer?: string
47
+ argv: string[]
48
+ }): Promise<number> {
49
+ await loadEnvFromDataDir(programName)
50
+ await loadEnvFromBinaryDir()
51
+
52
+ const first = argv[0]
53
+
54
+ // Explicit help, top-level and per-command.
55
+ if (first && isHelpFlag(first)) {
56
+ printTopLevelHelp(programName, manifest, banner, footer)
57
+ return 0
58
+ }
59
+ if (first === '/help') {
60
+ if (argv[1]) {
61
+ printCommandHelp(programName, argv[1], manifest)
62
+ } else {
63
+ printTopLevelHelp(programName, manifest, banner, footer)
64
+ }
65
+ return 0
66
+ }
67
+ if (first && argv.some(isHelpFlag)) {
68
+ printCommandHelp(programName, first, manifest)
69
+ return 0
70
+ }
71
+
72
+ // No command: interactive session on a TTY, help otherwise (scripts/pipes).
73
+ if (!first) {
74
+ if (!process.stdin.isTTY) {
75
+ printTopLevelHelp(programName, manifest, banner, footer)
76
+ return 0
77
+ }
78
+ printTrimmed(banner)
79
+ const target = await resolveCliTarget(programName)
80
+ return runSession({ programName, manifest, footer, target })
81
+ }
82
+
83
+ // Disconnect (reset): clear the saved connection and exit.
84
+ if (first === '/disconnect') {
85
+ await clearLastConnection(programName)
86
+ console.log('disconnected')
87
+ return 0
88
+ }
89
+
90
+ // Connect to a remote server, then open a session.
91
+ if (first === '/connect') {
92
+ const url = argv[1]
93
+ if (!url) {
94
+ console.error(`${programName}: /connect requires a url`)
95
+ return 1
96
+ }
97
+ printTrimmed(banner)
98
+ const target = await connectToServer(programName, url)
99
+ if (!target) {
100
+ return 1
101
+ }
102
+ return runSession({ programName, manifest, footer, target })
103
+ }
104
+
105
+ // Start a local instance, then open a session.
106
+ if (first === '/start') {
107
+ printTrimmed(banner)
108
+ let target: CliTarget
109
+ try {
110
+ target = await startLocalInstance(programName)
111
+ } catch (error) {
112
+ console.error(
113
+ `${programName}: ${error instanceof Error ? error.message : String(error)}`,
114
+ )
115
+ return 1
116
+ }
117
+ return runSession({ programName, manifest, footer, target })
118
+ }
119
+
120
+ // One-shot RPC dispatch (scripting): resolve the target without a session.
121
+ const target = await resolveCliTarget(programName)
122
+ if (!target) {
123
+ console.error(
124
+ `${programName}: not connected — run \`${programName} /connect <url>\` or \`${programName} /start\``,
125
+ )
126
+ return 1
127
+ }
128
+ const code = await dispatchCommand({
129
+ programName,
130
+ manifest,
131
+ command: first,
132
+ argvTail: argv.slice(1),
133
+ url: target.url,
134
+ token: target.token,
135
+ })
136
+ // Reap any local instance booted just to resolve the target.
137
+ target.child?.kill()
138
+ return code
139
+ }
@@ -0,0 +1,105 @@
1
+ import { clearLastConnection } from '../shared/clearLastConnection.ts'
2
+ import { connectToServer } from './connectToServer.ts'
3
+ import { dispatchCommand } from './dispatchCommand.ts'
4
+ import { printSessionHelp } from './printSessionHelp.ts'
5
+ import { printSessionStatus } from './printSessionStatus.ts'
6
+ import { printTrimmed } from './printTrimmed.ts'
7
+ import { startLocalInstance } from './startLocalInstance.ts'
8
+ import { tokenizeLine } from './tokenizeLine.ts'
9
+ import type { CliManifest } from './types/CliManifest.ts'
10
+ import type { CliTarget } from './types/CliTarget.ts'
11
+
12
+ /*
13
+ Interactive session (REPL). The banner is printed once by the caller; this prints
14
+ the status line, then loops reading stdin lines via Bun's async-iterable console:
15
+ - bare words → dispatch the RPC against the current target
16
+ - /connect <url>, /start, /disconnect, /help, /clear, /exit → meta commands
17
+ The session owns the current target's child (a local instance) and reaps it when
18
+ the connection is swapped or the loop ends — including on SIGINT.
19
+ */
20
+ export async function runSession({
21
+ programName,
22
+ manifest,
23
+ footer,
24
+ target,
25
+ }: {
26
+ programName: string
27
+ manifest: CliManifest
28
+ footer: string
29
+ target: CliTarget | undefined
30
+ }): Promise<number> {
31
+ let current = target
32
+ // Reap any local instance on Ctrl+C — the closure reads the latest `current`.
33
+ process.on('SIGINT', () => {
34
+ current?.child?.kill()
35
+ process.exit(0)
36
+ })
37
+
38
+ // Swap the active connection: reap the previous local instance (only one runs
39
+ // at a time), adopt the next target, and reprint the status line.
40
+ async function swap(next: CliTarget | undefined): Promise<void> {
41
+ current?.child?.kill()
42
+ current = next
43
+ await printSessionStatus(current)
44
+ }
45
+
46
+ await printSessionStatus(current)
47
+ const promptText = `${programName}> `
48
+ process.stdout.write(promptText)
49
+
50
+ for await (const line of console) {
51
+ const tokens = tokenizeLine(line.trim())
52
+ const head = tokens[0]
53
+ if (head === undefined) {
54
+ process.stdout.write(promptText)
55
+ continue
56
+ }
57
+ if (head === '/exit' || head === '/quit') {
58
+ break
59
+ }
60
+ if (head === '/clear') {
61
+ console.clear()
62
+ } else if (head === '/help') {
63
+ printSessionHelp(programName, manifest, tokens[1])
64
+ } else if (head === '/connect') {
65
+ const url = tokens[1]
66
+ if (!url) {
67
+ console.error('/connect requires a url')
68
+ } else {
69
+ const next = await connectToServer(programName, url)
70
+ if (next) {
71
+ await swap(next)
72
+ }
73
+ }
74
+ } else if (head === '/start') {
75
+ try {
76
+ await swap(await startLocalInstance(programName))
77
+ } catch (error) {
78
+ console.error(
79
+ `could not start local instance: ${error instanceof Error ? error.message : String(error)}`,
80
+ )
81
+ }
82
+ } else if (head === '/disconnect') {
83
+ await clearLastConnection(programName)
84
+ await swap(undefined)
85
+ } else if (head.startsWith('/')) {
86
+ console.error(`unknown command "${head}" — /help for the list`)
87
+ } else if (!current) {
88
+ console.error('not connected — /connect <url> or /start')
89
+ } else {
90
+ await dispatchCommand({
91
+ programName,
92
+ manifest,
93
+ command: head,
94
+ argvTail: tokens.slice(1),
95
+ url: current.url,
96
+ token: current.token,
97
+ })
98
+ }
99
+ process.stdout.write(promptText)
100
+ }
101
+
102
+ current?.child?.kill()
103
+ printTrimmed(footer)
104
+ return 0
105
+ }
@@ -0,0 +1,14 @@
1
+ import { spawnEmbeddedServer } from '../bundle/spawnEmbeddedServer.ts'
2
+ import { writeLastConnection } from '../shared/writeLastConnection.ts'
3
+ import type { CliTarget } from './types/CliTarget.ts'
4
+
5
+ /*
6
+ Boots a local (embedded) instance for this session and records the intent so the
7
+ next bare run resumes a local instance. The caller owns the returned child and
8
+ reaps it on disconnect/exit. Throws if the server crashes before binding.
9
+ */
10
+ export async function startLocalInstance(programName: string): Promise<CliTarget> {
11
+ const { url, child } = await spawnEmbeddedServer({ programName })
12
+ await writeLastConnection(programName, { kind: 'embedded' })
13
+ return { url, child }
14
+ }
@@ -0,0 +1,51 @@
1
+ /*
2
+ Splits a session input line into argv tokens, honouring single and double quotes
3
+ so values with spaces survive (e.g. `post --title "hello world"`). Quotes group;
4
+ a backslash escapes the next character (outside single quotes). Unterminated
5
+ quotes consume to end of line. Pure; no shell features beyond quoting — no
6
+ globbing, no variable expansion.
7
+ */
8
+ export function tokenizeLine(line: string): string[] {
9
+ const tokens: string[] = []
10
+ let current = ''
11
+ let hasToken = false
12
+ let quote: '"' | "'" | undefined
13
+ for (let index = 0; index < line.length; index++) {
14
+ const char = line[index]
15
+ if (char === '\\' && quote !== "'") {
16
+ const next = line[++index]
17
+ if (next !== undefined) {
18
+ current += next
19
+ hasToken = true
20
+ }
21
+ continue
22
+ }
23
+ if (quote) {
24
+ if (char === quote) {
25
+ quote = undefined
26
+ } else {
27
+ current += char
28
+ }
29
+ continue
30
+ }
31
+ if (char === '"' || char === "'") {
32
+ quote = char
33
+ hasToken = true
34
+ continue
35
+ }
36
+ if (char === ' ' || char === '\t') {
37
+ if (hasToken) {
38
+ tokens.push(current)
39
+ current = ''
40
+ hasToken = false
41
+ }
42
+ continue
43
+ }
44
+ current += char
45
+ hasToken = true
46
+ }
47
+ if (hasToken) {
48
+ tokens.push(current)
49
+ }
50
+ return tokens
51
+ }
@@ -0,0 +1,9 @@
1
+ import type { CliManifestEntry } from './CliManifestEntry.ts'
2
+
3
+ /*
4
+ Map from rpc export-name (e.g. "getReport") to its manifest entry. Built
5
+ by the bundler from the same verbRegistry MCP consumes; entries are
6
+ emitted only for rpcs with `clients.cli: true`. The CLI binary and any
7
+ programmatic createClient caller read this to dispatch calls.
8
+ */
9
+ export type CliManifest = Record<string, CliManifestEntry>
@@ -0,0 +1,17 @@
1
+ import type { HttpVerb } from '../../shared/types/HttpVerb.ts'
2
+
3
+ /*
4
+ Per-command manifest entry baked into the standalone CLI binary by the
5
+ bundler. Carries enough info to make the right HTTP request without
6
+ importing the handler module (which the thin build doesn't ship). Covers
7
+ both rpcs and socket commands — a socket `tail` is a GET against
8
+ `/__belte/sockets/<name>` with `accept: text/event-stream` so the CLI
9
+ streams it live; a socket `publish` is a POST to the same path.
10
+ */
11
+ export type CliManifestEntry = {
12
+ method: HttpVerb
13
+ url: string
14
+ jsonSchema?: Record<string, unknown>
15
+ // Request Accept header. Socket tail sets text/event-stream to stream live frames.
16
+ accept?: string
17
+ }
@@ -0,0 +1,13 @@
1
+ /*
2
+ A resolved CLI connection: the server URL plus an optional bearer token, and the
3
+ child process when this is a locally-spawned instance — the session owns that
4
+ child and reaps it on disconnect/exit.
5
+ */
6
+ export type CliTarget = {
7
+ url: string
8
+ token?: string
9
+ child?: ReturnType<typeof Bun.spawn>
10
+ // The app's name from its identity probe, when already fetched while resolving
11
+ // the target — lets the status line print it without re-probing.
12
+ name?: string
13
+ }
@@ -0,0 +1,29 @@
1
+ import type { HttpVerb } from '../shared/types/HttpVerb.ts'
2
+
3
+ /*
4
+ Maps an HTTP verb to MCP tool annotations so a model can tell a read from
5
+ a write before calling. Belte derives these from the verb the RPC was
6
+ declared with rather than asking the author to repeat the intent:
7
+ - GET / HEAD → read-only, non-destructive
8
+ - POST → creates; not idempotent, not (necessarily) destructive
9
+ - PUT → replaces; idempotent + destructive
10
+ - PATCH → modifies; destructive, not idempotent
11
+ - DELETE → removes; idempotent + destructive
12
+ The shape matches MCP's ToolAnnotations (readOnlyHint / destructiveHint /
13
+ idempotentHint); fields a verb doesn't imply are left off.
14
+ */
15
+ export function annotationsForMethod(method: HttpVerb): Record<string, boolean> {
16
+ switch (method) {
17
+ case 'GET':
18
+ case 'HEAD':
19
+ return { readOnlyHint: true, destructiveHint: false }
20
+ case 'POST':
21
+ return { readOnlyHint: false, destructiveHint: false, idempotentHint: false }
22
+ case 'PUT':
23
+ return { readOnlyHint: false, destructiveHint: true, idempotentHint: true }
24
+ case 'PATCH':
25
+ return { readOnlyHint: false, destructiveHint: true, idempotentHint: false }
26
+ case 'DELETE':
27
+ return { readOnlyHint: false, destructiveHint: true, idempotentHint: true }
28
+ }
29
+ }
@@ -0,0 +1,101 @@
1
+ // node:fs existsSync — cheap sync presence check, mirrors createPublicAssetServer
2
+ import { existsSync } from 'node:fs'
3
+ import { Glob } from 'bun'
4
+ import { mimeForExtension } from '../server/runtime/mimeForExtension.ts'
5
+ import type { Assets } from '../server/runtime/types/Assets.ts'
6
+ import type { McpResourceContents } from './types/McpResourceContents.ts'
7
+ import type { McpResourceDescriptor } from './types/McpResourceDescriptor.ts'
8
+ import type { McpResourceServer } from './types/McpResourceServer.ts'
9
+
10
+ /*
11
+ The belte:// URI namespace for file-based resources. A resource's URI is this
12
+ prefix followed by its path relative to src/mcp/resources.
13
+ */
14
+ const URI_PREFIX = 'belte://resources/'
15
+
16
+ /*
17
+ MIME essences returned inline as UTF-8 `text` in resources/read; everything
18
+ else is returned as a base64 `blob`. The essence is taken before any `;charset`
19
+ parameter that Bun.file().type appends.
20
+ */
21
+ function isTextMime(mime: string): boolean {
22
+ const essence = mime.split(';')[0].trim()
23
+ return (
24
+ essence.startsWith('text/') ||
25
+ essence === 'application/json' ||
26
+ essence === 'application/xml' ||
27
+ essence === 'image/svg+xml' ||
28
+ essence.endsWith('+json') ||
29
+ essence.endsWith('+xml')
30
+ )
31
+ }
32
+
33
+ function descriptorFor(relativePath: string): McpResourceDescriptor {
34
+ return {
35
+ uri: `${URI_PREFIX}${relativePath}`,
36
+ name: relativePath,
37
+ mimeType: mimeForExtension(relativePath),
38
+ }
39
+ }
40
+
41
+ function contentsFor(relativePath: string, bytes: Uint8Array): McpResourceContents {
42
+ const mimeType = mimeForExtension(relativePath)
43
+ const uri = `${URI_PREFIX}${relativePath}`
44
+ if (isTextMime(mimeType)) {
45
+ return { uri, mimeType, text: new TextDecoder().decode(bytes) }
46
+ }
47
+ return { uri, mimeType, blob: bytes.toBase64() }
48
+ }
49
+
50
+ /*
51
+ Serves files under src/mcp/resources as MCP resources. Two sources, picked at
52
+ construction (mirrors createPublicAssetServer):
53
+ - `mcpResources` (standalone compile): a map of relative-path → zstd bytes
54
+ embedded into the binary.
55
+ - `resourcesDir` on disk (dev + `belte start`): files read straight from
56
+ `${cwd}/src/mcp/resources`.
57
+ */
58
+ export function createMcpResourceServer({
59
+ resourcesDir,
60
+ mcpResources,
61
+ }: {
62
+ resourcesDir: string
63
+ mcpResources?: Assets
64
+ }): McpResourceServer {
65
+ return {
66
+ async list(): Promise<McpResourceDescriptor[]> {
67
+ if (mcpResources) {
68
+ return Object.keys(mcpResources).map(descriptorFor)
69
+ }
70
+ if (!existsSync(resourcesDir)) {
71
+ return []
72
+ }
73
+ const files = await Array.fromAsync(
74
+ new Glob('**/*').scan({ cwd: resourcesDir, onlyFiles: true }),
75
+ )
76
+ return files.map(descriptorFor)
77
+ },
78
+ async read(uri: string): Promise<McpResourceContents | undefined> {
79
+ if (!uri.startsWith(URI_PREFIX)) {
80
+ return undefined
81
+ }
82
+ const relativePath = uri.slice(URI_PREFIX.length)
83
+ // reject `..` traversal in the requested uri before any disk read
84
+ if (relativePath.split('/').includes('..')) {
85
+ return undefined
86
+ }
87
+ if (mcpResources) {
88
+ const compressed = mcpResources[relativePath]
89
+ if (!compressed) {
90
+ return undefined
91
+ }
92
+ return contentsFor(relativePath, await Bun.zstdDecompress(compressed))
93
+ }
94
+ const file = Bun.file(`${resourcesDir}/${relativePath}`)
95
+ if (!(await file.exists())) {
96
+ return undefined
97
+ }
98
+ return contentsFor(relativePath, await file.bytes())
99
+ },
100
+ }
101
+ }
@@ -0,0 +1,42 @@
1
+ import { NO_STORE } from '../shared/CACHE_CONTROL_VALUES.ts'
2
+ import { dispatchMcpRequest, MCP_NO_STORE_HEADERS } from './dispatchMcpRequest.ts'
3
+ import type { McpServer } from './types/McpServer.ts'
4
+ import type { McpServerOptions } from './types/McpServerOptions.ts'
5
+
6
+ const DEFAULT_NAME = 'belte-app'
7
+ const DEFAULT_VERSION = '0.0.0'
8
+
9
+ /*
10
+ Constructs an MCP server bound to the project's rpc registry. Returns an
11
+ object whose `handle(request)` is the function the bun route at
12
+ /__belte/mcp invokes. Framework-internal — the belte:mcp virtual
13
+ default-constructs it; there is no user-authored server module. Server
14
+ name/version default from package.json.
15
+
16
+ Tools are derived from every verb with `clients.mcp: true` (auto-on for
17
+ read-only verbs that carry a schema; mutating verbs opt in explicitly)
18
+ and from every socket with `clients.mcp: true` (a `<base>-tail` read tool
19
+ plus a `<base>-publish` tool when clientPublish is set). The HTTP verb
20
+ feeds each rpc tool's annotations. Auth inherits from the inbound request
21
+ — bearer / cookie headers flow into the synthesized Request that hits
22
+ each rpc handler. An optional `authorize` hook in opts can short-circuit
23
+ the request before any tool dispatches.
24
+ */
25
+ export function createMcpServer(opts: McpServerOptions = {}): McpServer {
26
+ const serverInfo = {
27
+ name: opts.name ?? DEFAULT_NAME,
28
+ version: opts.version ?? DEFAULT_VERSION,
29
+ }
30
+ return {
31
+ async handle(request: Request): Promise<Response> {
32
+ if (request.method !== 'POST') {
33
+ return new Response('Method Not Allowed', {
34
+ status: 405,
35
+ headers: { Allow: 'POST', 'Cache-Control': NO_STORE },
36
+ })
37
+ }
38
+ const envelope = await dispatchMcpRequest(request, opts, serverInfo)
39
+ return new Response(JSON.stringify(envelope), { headers: MCP_NO_STORE_HEADERS })
40
+ },
41
+ }
42
+ }
@@ -0,0 +1,146 @@
1
+ import { promptRegistry } from '../server/prompts/promptRegistry.ts'
2
+ import { ensureRegistriesLoaded } from '../server/runtime/registryManifests.ts'
3
+ import { NO_STORE } from '../shared/CACHE_CONTROL_VALUES.ts'
4
+ import { getMcpResourceServer } from './mcpResourceServerSlot.ts'
5
+ import { buildPrompts, buildTools, callTool } from './mcpSurface.ts'
6
+ import type { JsonRpcRequest } from './types/JsonRpcRequest.ts'
7
+ import type { JsonRpcResponse } from './types/JsonRpcResponse.ts'
8
+ import type { McpServerOptions } from './types/McpServerOptions.ts'
9
+
10
+ const PROTOCOL_VERSION = '2025-06-18'
11
+
12
+ function jsonRpcError(
13
+ id: string | number | null,
14
+ code: number,
15
+ message: string,
16
+ data?: unknown,
17
+ ): JsonRpcResponse {
18
+ return { jsonrpc: '2.0', id, error: { code, message, ...(data === undefined ? {} : { data }) } }
19
+ }
20
+
21
+ function jsonRpcOk(id: string | number | null, result: unknown): JsonRpcResponse {
22
+ return { jsonrpc: '2.0', id, result }
23
+ }
24
+
25
+ /*
26
+ Interpolates the caller's arguments into the prompt body and wraps the
27
+ result in the MCP prompts/get wire shape — a markdown prompt is a single
28
+ user message whose text is the rendered template. (The in-process agent
29
+ loop reaches the same prompts via mcpSurface().getPrompt, which returns
30
+ plain messages rather than this wire shape.)
31
+ */
32
+ function getPrompt(
33
+ name: string,
34
+ args: Record<string, unknown> | undefined,
35
+ ): Record<string, unknown> {
36
+ const entry = promptRegistry.get(name)
37
+ if (!entry) {
38
+ throw new Error(`unknown prompt: ${name}`)
39
+ }
40
+ const rendered = entry.prompt.render((args ?? {}) as Record<string, string>)
41
+ return {
42
+ ...(entry.prompt.description ? { description: entry.prompt.description } : {}),
43
+ messages: [{ role: 'user', content: { type: 'text', text: rendered } }],
44
+ }
45
+ }
46
+
47
+ /*
48
+ Parses a single JSON-RPC envelope and dispatches by method. Errors
49
+ become JSON-RPC error responses (the HTTP layer always returns 200 with
50
+ an envelope for JSON-RPC over HTTP; transport errors are different). The
51
+ tool/prompt derivation and tool dispatch live in mcpSurface — the same
52
+ projection the in-process agent loop builds on.
53
+ */
54
+ export async function dispatchMcpRequest(
55
+ request: Request,
56
+ opts: McpServerOptions,
57
+ serverInfo: { name: string; version: string },
58
+ ): Promise<JsonRpcResponse> {
59
+ let envelope: JsonRpcRequest
60
+ try {
61
+ envelope = (await request.clone().json()) as JsonRpcRequest
62
+ } catch {
63
+ return jsonRpcError(null, -32700, 'Parse error')
64
+ }
65
+ const id = envelope.id ?? null
66
+ if (envelope.jsonrpc !== '2.0' || typeof envelope.method !== 'string') {
67
+ return jsonRpcError(id, -32600, 'Invalid Request')
68
+ }
69
+
70
+ if (opts.authorize) {
71
+ try {
72
+ await opts.authorize(request)
73
+ } catch (error) {
74
+ const message = error instanceof Error ? error.message : String(error)
75
+ return jsonRpcError(id, -32001, message)
76
+ }
77
+ }
78
+
79
+ try {
80
+ await ensureRegistriesLoaded()
81
+ switch (envelope.method) {
82
+ case 'initialize':
83
+ return jsonRpcOk(id, {
84
+ protocolVersion: PROTOCOL_VERSION,
85
+ capabilities: {
86
+ tools: { listChanged: false },
87
+ prompts: { listChanged: false },
88
+ resources: { listChanged: false },
89
+ },
90
+ serverInfo,
91
+ })
92
+ case 'ping':
93
+ return jsonRpcOk(id, {})
94
+ case 'tools/list':
95
+ return jsonRpcOk(id, { tools: buildTools() })
96
+ case 'tools/call': {
97
+ const params = envelope.params as
98
+ | { name?: string; arguments?: Record<string, unknown> }
99
+ | undefined
100
+ if (!params?.name) {
101
+ return jsonRpcError(id, -32602, 'Missing tool name')
102
+ }
103
+ return jsonRpcOk(id, await callTool(params.name, params.arguments, request))
104
+ }
105
+ case 'resources/list': {
106
+ const resourceServer = getMcpResourceServer()
107
+ return jsonRpcOk(id, {
108
+ resources: resourceServer ? await resourceServer.list() : [],
109
+ })
110
+ }
111
+ case 'resources/read': {
112
+ const params = envelope.params as { uri?: string } | undefined
113
+ if (!params?.uri) {
114
+ return jsonRpcError(id, -32602, 'Missing resource uri')
115
+ }
116
+ const resourceServer = getMcpResourceServer()
117
+ const contents = resourceServer ? await resourceServer.read(params.uri) : undefined
118
+ if (!contents) {
119
+ return jsonRpcError(id, -32602, `unknown resource: ${params.uri}`)
120
+ }
121
+ return jsonRpcOk(id, { contents: [contents] })
122
+ }
123
+ case 'prompts/list':
124
+ return jsonRpcOk(id, { prompts: buildPrompts() })
125
+ case 'prompts/get': {
126
+ const params = envelope.params as
127
+ | { name?: string; arguments?: Record<string, unknown> }
128
+ | undefined
129
+ if (!params?.name) {
130
+ return jsonRpcError(id, -32602, 'Missing prompt name')
131
+ }
132
+ return jsonRpcOk(id, getPrompt(params.name, params.arguments))
133
+ }
134
+ default:
135
+ return jsonRpcError(id, -32601, `Method not found: ${envelope.method}`)
136
+ }
137
+ } catch (error) {
138
+ const message = error instanceof Error ? error.message : String(error)
139
+ return jsonRpcError(id, -32603, message)
140
+ }
141
+ }
142
+
143
+ export const MCP_NO_STORE_HEADERS = {
144
+ 'Content-Type': 'application/json',
145
+ 'Cache-Control': NO_STORE,
146
+ } as const