@fragno-dev/mcp-fragment 0.0.0-stage → 0.0.3

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 (90) hide show
  1. package/LICENSE.md +16 -0
  2. package/README.md +312 -2
  3. package/bin/run.js +7 -0
  4. package/dist/browser/client/react.d.ts +253 -0
  5. package/dist/browser/client/react.d.ts.map +1 -0
  6. package/dist/browser/client/react.js +194 -0
  7. package/dist/browser/client/react.js.map +1 -0
  8. package/dist/browser/client/solid.d.ts +256 -0
  9. package/dist/browser/client/solid.d.ts.map +1 -0
  10. package/dist/browser/client/solid.js +129 -0
  11. package/dist/browser/client/solid.js.map +1 -0
  12. package/dist/browser/client/svelte.d.ts +256 -0
  13. package/dist/browser/client/svelte.d.ts.map +1 -0
  14. package/dist/browser/client/svelte.js +131 -0
  15. package/dist/browser/client/svelte.js.map +1 -0
  16. package/dist/browser/client/vanilla.d.ts +283 -0
  17. package/dist/browser/client/vanilla.d.ts.map +1 -0
  18. package/dist/browser/client/vanilla.js +159 -0
  19. package/dist/browser/client/vanilla.js.map +1 -0
  20. package/dist/browser/client/vue.d.ts +256 -0
  21. package/dist/browser/client/vue.d.ts.map +1 -0
  22. package/dist/browser/client/vue.js +127 -0
  23. package/dist/browser/client/vue.js.map +1 -0
  24. package/dist/browser/client-BCzNDSlW.d.ts +711 -0
  25. package/dist/browser/client-BCzNDSlW.d.ts.map +1 -0
  26. package/dist/browser/index.d.ts +813 -0
  27. package/dist/browser/index.d.ts.map +1 -0
  28. package/dist/browser/index.js +1 -0
  29. package/dist/browser/src-B6dHZXqB.js +2303 -0
  30. package/dist/browser/src-B6dHZXqB.js.map +1 -0
  31. package/dist/cli/cli.d.ts +6 -0
  32. package/dist/cli/cli.d.ts.map +1 -0
  33. package/dist/cli/cli.js +227 -0
  34. package/dist/cli/cli.js.map +1 -0
  35. package/dist/cli/definition.js +330 -0
  36. package/dist/cli/definition.js.map +1 -0
  37. package/dist/cli/diagnostics.js +93 -0
  38. package/dist/cli/diagnostics.js.map +1 -0
  39. package/dist/cli/index.js +13 -0
  40. package/dist/cli/index.js.map +1 -0
  41. package/dist/cli/mcp-api.js +87 -0
  42. package/dist/cli/mcp-api.js.map +1 -0
  43. package/dist/cli/mcp-types.js +100 -0
  44. package/dist/cli/mcp-types.js.map +1 -0
  45. package/dist/cli/oauth-provider.js +113 -0
  46. package/dist/cli/oauth-provider.js.map +1 -0
  47. package/dist/cli/routes.js +743 -0
  48. package/dist/cli/routes.js.map +1 -0
  49. package/dist/cli/schema.js +13 -0
  50. package/dist/cli/schema.js.map +1 -0
  51. package/dist/cli/services.js +330 -0
  52. package/dist/cli/services.js.map +1 -0
  53. package/dist/node/definition.d.ts +260 -0
  54. package/dist/node/definition.d.ts.map +1 -0
  55. package/dist/node/definition.js +330 -0
  56. package/dist/node/definition.js.map +1 -0
  57. package/dist/node/diagnostics.js +93 -0
  58. package/dist/node/diagnostics.js.map +1 -0
  59. package/dist/node/index.d.ts +694 -0
  60. package/dist/node/index.d.ts.map +1 -0
  61. package/dist/node/index.js +26 -0
  62. package/dist/node/index.js.map +1 -0
  63. package/dist/node/mcp-api.d.ts +2 -0
  64. package/dist/node/mcp-api.js +87 -0
  65. package/dist/node/mcp-api.js.map +1 -0
  66. package/dist/node/mcp-types.d.ts +146 -0
  67. package/dist/node/mcp-types.d.ts.map +1 -0
  68. package/dist/node/mcp-types.js +100 -0
  69. package/dist/node/mcp-types.js.map +1 -0
  70. package/dist/node/oauth-provider.d.ts +24 -0
  71. package/dist/node/oauth-provider.d.ts.map +1 -0
  72. package/dist/node/oauth-provider.js +113 -0
  73. package/dist/node/oauth-provider.js.map +1 -0
  74. package/dist/node/routes.d.ts +466 -0
  75. package/dist/node/routes.d.ts.map +1 -0
  76. package/dist/node/routes.js +743 -0
  77. package/dist/node/routes.js.map +1 -0
  78. package/dist/node/schema.d.ts +9 -0
  79. package/dist/node/schema.d.ts.map +1 -0
  80. package/dist/node/schema.js +13 -0
  81. package/dist/node/schema.js.map +1 -0
  82. package/dist/node/services.d.ts +40 -0
  83. package/dist/node/services.d.ts.map +1 -0
  84. package/dist/node/services.js +330 -0
  85. package/dist/node/services.js.map +1 -0
  86. package/dist/node/testing/mcp-test-server.d.ts +42 -0
  87. package/dist/node/testing/mcp-test-server.d.ts.map +1 -0
  88. package/dist/node/testing/mcp-test-server.js +218 -0
  89. package/dist/node/testing/mcp-test-server.js.map +1 -0
  90. package/package.json +130 -3
@@ -0,0 +1 @@
1
+ {"version":3,"file":"src-B6dHZXqB.js","names":["extractPathParams","pathPattern","segments","split","filter","s","length","names","segment","startsWith","push","slice","buildPath","params","patternSegments","builtSegments","name","value","Error","encodeURIComponent","join","isBinaryRequestBody","body","ReadableStream","Blob","ArrayBuffer","isView","containsFileBody","value","File","FormData","Object","values","some","entry","appendFormDataValue","formData","key","append","name","JSON","stringify","prepareMultipartBody","entries","TypeError","prepareRouteRequestBody","contentType","prepareClientRequestBody","prepareInferredRequestBody","URLSearchParams","applyPreparedRequestBodyContentType","headers","preparedBody","delete","set","createRequestInitWithBody","method","options","requestInit","duplex","getMountRoute","opts","mountRoute","name","endsWith","slice","FragnoApiError","Error","status","code","constructor","message","name","toResponse","Response","json","FragnoApiValidationError","issues","FragnoApiValidationError","RequestInputContext","path","method","pathParams","searchParams","headers","body","request","parsedBody","inputSchema","shouldValidateInput","constructor","config","rawBody","fromRequest","state","fromSSRContext","URLSearchParams","Headers","query","formData","FormData","Error","isFormData","bodyStream","ReadableStream","isBodyStream","input","schema","valid","validateInput","#validateInput","Blob","result","validate","issues","value","ResponseStream","writer","encoder","abortSubscribers","responseReadable","aborted","abortPromise","closed","constructor","writable","readable","getWriter","TextEncoder","reader","getReader","push","cancel","ReadableStream","pull","controller","done","value","read","close","enqueue","abort","writeRaw","input","encode","write","JSON","stringify","sleep","ms","Promise","resolve","setTimeout","onAbort","listener","allSettled","map","subscriber","then","results","errors","result","status","reason","length","AggregateError","ResponseStream","mergeHeaders","headerSources","mergedHeaders","Headers","headerSource","key","value","entries","set","Array","isArray","Object","OutputContext","runJsonStreamCallback","constructor","options","error","message","code","initOrStatus","headers","Response","json","status","empty","defaultHeaders","object","jsonStream","cb","onError","readable","writable","TransformStream","stream","runStreamCallback","e","Error","console","close","Promise","resolve","catch","responseReadable","RequestOutputContext","outputSchema","mergeHeaders","resolveRouteFactories","context","routesOrFactories","routes","item","factoryRoutes","push","defineRoute","config","defineRoutes","_definition","create","fn","ctx","parseContentType","contentType","trimmed","trim","parts","split","map","part","mediaType","typeParts","length","type","subtype","toLowerCase","parameters","i","param","equalIndex","indexOf","key","slice","value","startsWith","endsWith","unwrapAtom","value","get","unwrapObject","params","Object","fromEntries","entries","map","key","isReadableAtom","lc","notify","off","subscribe","tasks","resolves","startTask","prevResolves","i","task","cb","endTask","promise","finally","t","allTasks","Promise","resolve","push","cleanTasks","cleanTasks","clean","Symbol","cleanStores","stores","process","env","NODE_ENV","Error","$store","mocked","clean","listenerQueue","lqIndex","QUEUE_ITEMS_PER_LISTENER","nanostoresGlobal","globalThis","epoch","atom","initialValue","listeners","$atom","get","lc","listen","value","init","listener","push","i","length","splice","index","indexOf","off","notify","oldValue","changedKey","runListenerQueue","set","newValue","subscribe","unbind","process","env","NODE_ENV","readonlyType","store","clean","START","STOP","SET","NOTIFY","MOUNT","UNMOUNT","REVERT_MUTATION","on","object","listener","eventKey","mutateStore","events","eventProps","reduceRight","event","l","shared","push","currentListeners","index","indexOf","splice","length","onStart","$store","runListeners","originListen","listen","arg","lc","starting","onStop","originOff","off","onSet","originSet","set","originSetKey","setKey","changed","changedValue","isAborted","abort","newValue","value","onNotify","originNotify","notify","oldValue","STORE_UNMOUNT_DELAY","onMount","initialize","payload","destroy","args","active","setTimeout","process","env","NODE_ENV","originClean","warned","warn","text","console","groupCollapsed","trace","groupEnd","atom","nanostoresGlobal","onMount","warn","computedStore","stores","cb","batched","Array","isArray","previousArgs","currentEpoch","set","epoch","args","map","$store","get","some","arg","i","value","then","t","process","env","NODE_ENV","asyncValue","$computed","undefined","timer","run","clearTimeout","setTimeout","unbinds","listen","unbind","computed","fn","atom","map","initial","$map","setKey","key","value","oldMap","notify","allTasks","stores","getStores","addStore","store","push","cleanStores","clientInitialData","hydrateFromWindow","window","__FRAGNO_INITIAL_DATA__","Map","console","warn","Array","from","entries","getInitialData","key","has","data","get","delete","listenToStores","listen","getFinalStoreValues","storesInitialValue","value","loading","set","FragnoClientError","Error","code","constructor","message","options","cause","name","FragnoClientFetchError","fromUnknownFetchError","error","FragnoClientFetchNetworkError","FragnoClientFetchAbortError","FragnoClientUnknownApiError","status","FragnoClientApiError","fromResponse","response","unknown","json","mergeFetcherConfigs","authorConfig","userConfig","type","authorOpts","options","userOpts","Object","keys","length","headers","mergeHeaders","author","user","merged","Headers","forEach","value","key","set","next","done","FragnoClientFetchAbortError","FragnoClientFetchError","FragnoClientUnknownApiError","createAbortPromise","abortSignal","Promise","_","reject","abortHandler","aborted","addEventListener","once","parseNdjsonBuffer","buffer","options","lines","split","trailingBuffer","flush","pop","index","length","line","trim","jsonObject","JSON","parse","parseError","parseErrorStatus","cause","onItem","remainingLines","slice","stopped","join","handleNdjsonStreamingFirstItem","response","store","body","decoder","TextDecoder","reader","getReader","firstItem","items","releaseLock","done","value","race","read","decode","stream","push","streamingPromise","continueStreaming","error","setError","clientError","initialBuffer","appendItem","setData","createNanoEvents","emit","event","args","callbacks","events","i","length","on","cb","push","filter","map","onStart","onStop","atom","batched","startTask","createNanoEvents","defaultOnErrorRetry","retryCount","Math","random","nanoqueryFactory","isAppVisible","visibilityChangeSubscribe","reconnectChangeSubscribe","nanoquery","cache","Map","fetcher","globalFetcher","globalSettings","events","focus","emit","FOCUS","RECONNECT","_revalidateOnInterval","_errorInvalidateTimeouts","_runningFetches","rewrittenSettings","getCachedValueByKey","key","fromCache","get","cacheHit","expires","getNow","data","error","runFetcher","keyParts","store","settings","set","v","SET_CACHE","setAsLoading","prev","toSet","loading","promise","dedupeTime","cacheLifetime","Infinity","onErrorRetry","now","has","value","cachedValue","cachedError","created","notLoading","finishTask","clearTimeout","res","onError","timer","setTimeout","invalidateKeys","delete","createFetcherStore","keyInput","fetcherSettings","process","env","NODE_ENV","Error","fetcherStore","_","fetcherSymbol","invalidate","revalidate","revalidateKeys","mutate","mutateCache","fetch","resolve","Promise","r","unsub","listen","finally","keysInternalUnsub","prevKey","prevKeyParts","keyUnsub","keyStore","evtUnsubs","firstRun","getKeyStore","subscribe","currentKeys","newKey","currentKeyValue","handleNewListener","revalidateInterval","revalidateOnFocus","revalidateOnReconnect","runRefetcher","setInterval","push","on","cacheKeyChangeHandler","keySelector","testKeyAgainstSelector","INVALIDATE_KEYS","REVALIDATE_KEYS","full","originListen","listener","forEach","fn","clearInterval","iterOverCache","cb","keys","cached","createMutatorStore","mutator","opts","throttleCalls","newMutator","keysToInvalidate","keysToRevalidate","safeKeySet","k","lc","setKey","result","getCacheUpdater","shouldRevalidate","newVal","__unsafeOverruleSettings","console","warn","isSomeKey","$key","keysAsStoresToIndexes","setKeyStoreValue","some","join","i","length","keyOrStore","storesAsArray","$storeKeys","storeValues","partIndex","noop","selector","Array","isArray","includes","Date","getTime","Symbol","name","isServer","window","addEventListener","browserCompat","document","hidden","buildPath","extractPathParams","createRequestInitWithBody","prepareClientRequestBody","getMountRoute","RequestInputContext","RequestOutputContext","resolveRouteFactories","parseContentType","unwrapObject","addStore","FragnoClientApiError","FragnoClientError","FragnoClientFetchAbortError","FragnoClientFetchError","FragnoClientFetchNetworkError","FragnoClientUnknownApiError","mergeFetcherConfigs","handleNdjsonStreamingFirstItem","computed","task","nanoquery","GET_HOOK_SYMBOL","Symbol","MUTATOR_HOOK_SYMBOL","STORE_SYMBOL","schemaAllowsUndefined","schema","validate","issues","assertBodyProvided","body","inputSchema","errorMessage","Error","mergeRequestHeaders","headerSources","result","hasHeaders","source","Headers","key","value","entries","Array","isArray","Object","buildUrl","config","params","baseUrl","mountRoute","path","pathParams","queryParams","normalizedPathParams","normalizedQueryParams","filteredQueryParams","fromEntries","filter","_","searchParams","URLSearchParams","toString","getCacheKey","method","pathParamValues","map","name","queryParamValues","keys","sort","v","isStreamingResponse","response","contentType","headers","get","subtype","isGetHook","hook","isMutatorHook","isStore","obj","ClientBuilder","publicConfig","fragmentConfig","fetcherConfig","cache","Map","createFetcherStore","createMutatorStore","invalidateKeys","constructor","cacheLifetime","Infinity","cacheEntries","createStore","input","factory","query","getFetcher","fetcher","defaultOptions","getFetcherOptions","#getFetcher","type","globalThis","fetch","bind","#getFetcherOptions","options","createHook","route","routes","find","r","outputSchema","createRouteQueryHook","createMutator","onInvalidate","createRouteQueryMutator","#createRouteQueryHook","String","fetcherOptions","useFetcherOnServer","useOnServer","callServerSideHandler","handler","fromSSRContext","executeQuery","window","url","error","fromUnknownFetchError","ok","fromResponse","store","args","isStreaming","json","getReader","cancel","items","setData","mutate","setError","set","onErrorRetry","dedupeTime","streamingPromise","#createRouteQueryMutator","invalidate","executeMutateQuery","preparedBody","bodyHeaders","requestOptions","mutatorStore","data","status","firstItem","loading","length","onError","console","mutateQuery","#invalidate","prefix","k","join","startsWith","createClientBuilder","definition","routesOrFactories","authorFetcherConfig","deps","services","serviceDeps","mergedFetcherConfig","defineFragment","HookContext","HookFn","McpFragmentConfig","BaseMcpFragmentConfig","isMcpAuthReady","mcpAuthReadyUntil","AuthPersistenceChanges","McpServerConfigurationChangedPayload","serverId","current","tools","McpServerConfigurationDeletedPayload","McpServerReadinessChangedPayload","ready","McpServerReadinessChange","server","name","authMode","McpFragmentHooksConfig","onServerConfigurationChanged","payload","context","Promise","onServerConfigurationDeleted","onServerReadinessChanged","McpInternalRefreshServerConfigurationPayload","McpInternalAuthExpiredPayload","expiresAt","McpHooksMap","internalRefreshServerConfiguration","internalAuthExpired","scheduleMcpAuthExpiry","uow","triggerHook","options","processAt","Date","after","readyUntil","toISOString","mcpFragmentDefinition","extend","x","providesBaseService","build","z","McpFragmentConfig","allowedEndpointUrls","url","URL","allowedOAuthRedirectUris","fetch","authConfigSchema","discriminatedUnion","object","type","literal","token","string","min","clientId","clientSecret","scopes","array","optional","mcpServerSlugSchema","regex","createServerInputSchema","slug","name","endpointUrl","auth","default","replaceServerConfigurationInputSchema","omit","toolCallInputSchema","arguments","record","unknown","timeoutMs","number","int","positive","max","tokenAuthInputSchema","mcpCredentialsPresenceSchema","enum","describe","mcpAuthStatusSchema","mode","credentials","state","mcpOAuthPendingSchema","pending","authorizationUrl","expiresAt","union","date","nullable","mcpToolSchema","looseObject","title","description","inputSchema","outputSchema","annotations","_meta","mcpToolCallResultSchema","isError","boolean","content","structuredContent","MCP_OAUTH_REDIRECT_URI_QUERY_PARAMETER","mcpOAuthRedirectUriSchema","refine","value","protocol","oauthStartInputSchema","scope","discardTokens","AuthConfig","infer","CreateServerInput","ToolCallInput","McpAuthStatus","McpOAuthPending","McpTool","McpToolCallResult","z","parseSecretPayload","mcpToolSchema","AuthPersistenceChanges","serverConnectionCacheOutputSchema","object","protocolVersion","string","nullable","optional","serverInfo","unknown","capabilities","tools","array","updatedAt","union","date","serverOutputSchema","slug","name","endpointUrl","authMode","cache","refreshOutputSchema","ok","boolean","stage","enum","checkedAt","server","omit","auth","authenticated","mode","tokenPresent","expiresAt","expired","scopes","requested","granted","missing","raw","live","reachable","listToolsOk","toolCount","number","presentBeforeCheck","previousToolCount","updatedToolCount","error","code","message","SecretForDiagnostics","id","toString","kind","payload","Date","OAuthStateForDiagnostics","scope","AuthPayloadForDiagnostics","type","token","tokens","access_token","splitScope","split","filter","Boolean","length","newestOAuthScope","states","state","toSorted","left","right","getTime","buildAuthDiagnostics","secrets","oauthStates","authChanges","authSecret","find","secret","authPayload","authExpiresAt","parsedAuth","undefined","grantedScopes","Set","has","now","errorMessage","Error","String","serverConnectionCacheOutputSchema","serverOutputSchema","z","defineRoutes","mcpFragmentDefinition","refreshOutputSchema","createServerInputSchema","MCP_OAUTH_REDIRECT_URI_QUERY_PARAMETER","mcpAuthStatusSchema","mcpOAuthPendingSchema","mcpToolCallResultSchema","mcpToolSchema","oauthStartInputSchema","replaceServerConfigurationInputSchema","tokenAuthInputSchema","toolCallInputSchema","AuthConfig","AuthPersistenceChanges","serverConnectionCacheOutputSchema","object","protocolVersion","string","nullable","optional","serverInfo","unknown","capabilities","tools","array","updatedAt","union","date","serverOutputSchema","slug","name","endpointUrl","authMode","cache","mcpRoutesFactory","create","services","defineRoute","config","method","path","inputSchema","outputSchema","errorCodes","handler","servers","authorizationUrl","state","queryParameters","authenticated","boolean","mode","createClientBuilder","FragnoPublicClientConfig","mcpFragmentDefinition","McpFragmentConfig","mcpRoutesFactory","routes","const","createMcpFragment","config","fragnoConfig","FragnoPublicConfigWithDatabase","createMcpFragmentClients","builder","useServers","createHook","useServer","createServer","createMutator","refreshServer","callTool","setToken","startOAuth","deleteAuth"],"sources":["../../../fragno/dist/api/internal/path.js","../../../fragno/dist/api/request-body.js","../../../fragno/dist/api/internal/route.js","../../../fragno/dist/api/error.js","../../../fragno/dist/api/request-input-context.js","../../../fragno/dist/api/internal/response-stream.js","../../../fragno/dist/api/request-output-context.js","../../../fragno/dist/api/route.js","../../../fragno/dist/util/content-type.js","../../../fragno/dist/util/nanostores.js","../../../../node_modules/.pnpm/nanostores@1.3.0/node_modules/nanostores/task/index.js","../../../../node_modules/.pnpm/nanostores@1.3.0/node_modules/nanostores/clean-stores/index.js","../../../../node_modules/.pnpm/nanostores@1.3.0/node_modules/nanostores/atom/index.js","../../../../node_modules/.pnpm/nanostores@1.3.0/node_modules/nanostores/lifecycle/index.js","../../../../node_modules/.pnpm/nanostores@1.3.0/node_modules/nanostores/warn/index.js","../../../../node_modules/.pnpm/nanostores@1.3.0/node_modules/nanostores/computed/index.js","../../../../node_modules/.pnpm/nanostores@1.3.0/node_modules/nanostores/map/index.js","../../../fragno/dist/util/ssr.js","../../../fragno/dist/client/client-error.js","../../../fragno/dist/client/internal/fetcher-merge.js","../../../fragno/dist/client/internal/ndjson-streaming.js","../../../../node_modules/.pnpm/nanoevents@9.1.0/node_modules/nanoevents/index.js","../../../../node_modules/.pnpm/@nanostores+query@0.3.4_nanostores@1.3.0/node_modules/@nanostores/query/dist/nanoquery.js","../../../fragno/dist/client/client.js","../../src/definition.ts","../../src/mcp-types.ts","../../src/diagnostics.ts","../../src/routes.ts","../../src/index.ts"],"sourcesContent":["//#region src/api/internal/path.ts\n/**\n* Extract parameter names from a path pattern at runtime.\n* Examples:\n* - \"/users/:id\" => [\"id\"]\n* - \"/files/**\" => [\"**\"]\n* - \"/files/**:rest\" => [\"rest\"]\n*/\nfunction extractPathParams(pathPattern) {\n\tconst segments = pathPattern.split(\"/\").filter((s) => s.length > 0);\n\tconst names = [];\n\tfor (const segment of segments) {\n\t\tif (segment.startsWith(\":\")) {\n\t\t\tnames.push(segment.slice(1));\n\t\t\tcontinue;\n\t\t}\n\t\tif (segment === \"**\") {\n\t\t\tnames.push(\"**\");\n\t\t\tcontinue;\n\t\t}\n\t\tif (segment.startsWith(\"**:\")) {\n\t\t\tnames.push(segment.slice(3));\n\t\t\tcontinue;\n\t\t}\n\t}\n\treturn names;\n}\n/**\n* Build a concrete path by replacing placeholders in a path pattern with values.\n*\n* Supports the same placeholder syntax as the matcher:\n* - Named parameter \":name\" is URL-encoded as a single segment\n* - Anonymous wildcard \"**\" inserts the remainder as-is (slashes preserved)\n* - Named wildcard \"**:name\" inserts the remainder from the named key\n*\n* Examples:\n* - buildPath(\"/users/:id\", { id: \"123\" }) => \"/users/123\"\n* - buildPath(\"/files/**\", { \"**\": \"a/b\" }) => \"/files/a/b\"\n* - buildPath(\"/files/**:rest\", { rest: \"a/b\" }) => \"/files/a/b\"\n*/\nfunction buildPath(pathPattern, params) {\n\tconst patternSegments = pathPattern.split(\"/\");\n\tconst builtSegments = [];\n\tfor (const segment of patternSegments) {\n\t\tif (segment.length === 0) {\n\t\t\tbuiltSegments.push(\"\");\n\t\t\tcontinue;\n\t\t}\n\t\tif (segment.startsWith(\":\")) {\n\t\t\tconst name = segment.slice(1);\n\t\t\tconst value = params[name];\n\t\t\tif (value === void 0) throw new Error(`Missing value for path parameter :${name}`);\n\t\t\tbuiltSegments.push(encodeURIComponent(value));\n\t\t\tcontinue;\n\t\t}\n\t\tif (segment === \"**\") {\n\t\t\tconst value = params[\"**\"];\n\t\t\tif (value === void 0) throw new Error(\"Missing value for path wildcard **\");\n\t\t\tbuiltSegments.push(value);\n\t\t\tcontinue;\n\t\t}\n\t\tif (segment.startsWith(\"**:\")) {\n\t\t\tconst name = segment.slice(3);\n\t\t\tconst value = params[name];\n\t\t\tif (value === void 0) throw new Error(`Missing value for path wildcard **:${name}`);\n\t\t\tbuiltSegments.push(value);\n\t\t\tcontinue;\n\t\t}\n\t\tbuiltSegments.push(segment);\n\t}\n\treturn builtSegments.join(\"/\");\n}\n//#endregion\nexport { buildPath, extractPathParams };\n\n//# sourceMappingURL=path.js.map","//#region src/api/request-body.ts\nconst isBinaryRequestBody = (body) => body instanceof ReadableStream || body instanceof Blob || body instanceof ArrayBuffer || ArrayBuffer.isView(body);\nconst containsFileBody = (value) => {\n\tif (value instanceof File || value instanceof Blob || value instanceof FormData) return true;\n\treturn typeof value === \"object\" && value !== null && Object.values(value).some((entry) => entry instanceof File || entry instanceof Blob || entry instanceof FormData);\n};\nconst appendFormDataValue = (formData, key, value) => {\n\tif (value instanceof File) formData.append(key, value, value.name);\n\telse if (value instanceof Blob) formData.append(key, value);\n\telse if (value !== void 0 && value !== null) formData.append(key, typeof value === \"string\" ? value : JSON.stringify(value));\n};\nconst prepareMultipartBody = (body) => {\n\tif (body instanceof FormData) return body;\n\tconst formData = new FormData();\n\tif (body instanceof File) {\n\t\tformData.append(\"file\", body, body.name);\n\t\treturn formData;\n\t}\n\tif (body instanceof Blob) {\n\t\tformData.append(\"file\", body);\n\t\treturn formData;\n\t}\n\tif (typeof body === \"object\" && body !== null) {\n\t\tfor (const [key, value] of Object.entries(body)) appendFormDataValue(formData, key, value);\n\t\treturn formData;\n\t}\n\tthrow new TypeError(\"Multipart routes require FormData, File, Blob, or an object body.\");\n};\nconst prepareRouteRequestBody = (body, contentType = \"application/json\") => {\n\tif (body === void 0) return {\n\t\tbody: void 0,\n\t\tcontentType: void 0\n\t};\n\tswitch (contentType) {\n\t\tcase \"application/json\": return {\n\t\t\tbody: JSON.stringify(body),\n\t\t\tcontentType\n\t\t};\n\t\tcase \"multipart/form-data\": return {\n\t\t\tbody: prepareMultipartBody(body),\n\t\t\tcontentType: null\n\t\t};\n\t\tcase \"application/octet-stream\":\n\t\t\tif (!isBinaryRequestBody(body)) throw new TypeError(\"Octet-stream routes require Blob, ArrayBuffer, ArrayBufferView, or ReadableStream bodies.\");\n\t\t\treturn {\n\t\t\t\tbody,\n\t\t\t\tcontentType\n\t\t\t};\n\t\tdefault: throw new TypeError(\"Unsupported route content type.\");\n\t}\n};\nconst prepareClientRequestBody = (body, contentType) => {\n\tif (body === void 0) return {\n\t\tbody: void 0,\n\t\tcontentType: void 0\n\t};\n\tif (contentType === \"application/octet-stream\") {\n\t\tif (!isBinaryRequestBody(body)) throw new TypeError(\"Octet-stream routes require Blob, ArrayBuffer, ArrayBufferView, or ReadableStream bodies.\");\n\t\treturn {\n\t\t\tbody,\n\t\t\tcontentType\n\t\t};\n\t}\n\tif (body instanceof FormData || body instanceof File || body instanceof Blob) return {\n\t\tbody: prepareMultipartBody(body),\n\t\tcontentType: null\n\t};\n\tif (containsFileBody(body)) return {\n\t\tbody: prepareMultipartBody(body),\n\t\tcontentType: null\n\t};\n\treturn {\n\t\tbody: JSON.stringify(body),\n\t\tcontentType: \"application/json\"\n\t};\n};\nconst prepareInferredRequestBody = (body) => {\n\tif (body === void 0) return {\n\t\tbody: void 0,\n\t\tcontentType: void 0\n\t};\n\tif (typeof body === \"string\" || body instanceof URLSearchParams || body instanceof FormData || isBinaryRequestBody(body)) return {\n\t\tbody,\n\t\tcontentType: null\n\t};\n\treturn {\n\t\tbody: JSON.stringify(body),\n\t\tcontentType: \"application/json\"\n\t};\n};\nconst applyPreparedRequestBodyContentType = (headers, preparedBody) => {\n\tif (preparedBody.contentType === null) headers.delete(\"content-type\");\n\telse if (preparedBody.contentType !== void 0) headers.set(\"content-type\", preparedBody.contentType);\n};\nconst createRequestInitWithBody = (method, headers, body, options = {}) => {\n\tconst requestInit = {\n\t\t...options,\n\t\tmethod,\n\t\theaders,\n\t\tbody\n\t};\n\tif (body instanceof ReadableStream) requestInit.duplex = \"half\";\n\treturn requestInit;\n};\n//#endregion\nexport { applyPreparedRequestBodyContentType, createRequestInitWithBody, prepareClientRequestBody, prepareInferredRequestBody, prepareRouteRequestBody };\n\n//# sourceMappingURL=request-body.js.map","//#region src/api/internal/route.ts\nfunction getMountRoute(opts) {\n\tconst mountRoute = opts.mountRoute ?? `/api/${opts.name}`;\n\tif (mountRoute.endsWith(\"/\")) return mountRoute.slice(0, -1);\n\treturn mountRoute;\n}\n//#endregion\nexport { getMountRoute };\n\n//# sourceMappingURL=route.js.map","//#region src/api/error.ts\nvar FragnoApiError = class extends Error {\n\t#status;\n\t#code;\n\tconstructor({ message, code }, status) {\n\t\tsuper(message);\n\t\tthis.name = \"FragnoApiError\";\n\t\tthis.#status = status;\n\t\tthis.#code = code;\n\t}\n\tget status() {\n\t\treturn this.#status;\n\t}\n\tget code() {\n\t\treturn this.#code;\n\t}\n\ttoResponse() {\n\t\treturn Response.json({\n\t\t\tmessage: this.message,\n\t\t\tcode: this.code\n\t\t}, { status: this.status });\n\t}\n};\nvar FragnoApiValidationError = class extends FragnoApiError {\n\tissues;\n\tconstructor(message, issues) {\n\t\tsuper({\n\t\t\tmessage,\n\t\t\tcode: \"FRAGNO_VALIDATION_ERROR\"\n\t\t}, 400);\n\t\tthis.name = \"FragnoApiValidationError\";\n\t\tthis.issues = issues;\n\t}\n\ttoResponse() {\n\t\treturn Response.json({\n\t\t\tmessage: this.message,\n\t\t\tissues: this.issues,\n\t\t\tcode: this.code\n\t\t}, { status: this.status });\n\t}\n};\n//#endregion\nexport { FragnoApiError, FragnoApiValidationError };\n\n//# sourceMappingURL=error.js.map","import { FragnoApiValidationError } from \"./error.js\";\n//#region src/api/request-input-context.ts\nvar RequestInputContext = class RequestInputContext {\n\t#path;\n\t#method;\n\t#pathParams;\n\t#searchParams;\n\t#headers;\n\t#body;\n\t#request;\n\t#parsedBody;\n\t#inputSchema;\n\t#shouldValidateInput;\n\tconstructor(config) {\n\t\tthis.#path = config.path;\n\t\tthis.#method = config.method;\n\t\tthis.#pathParams = config.pathParams;\n\t\tthis.#searchParams = config.searchParams;\n\t\tthis.#headers = config.headers;\n\t\tthis.#body = config.rawBody;\n\t\tthis.#request = config.request;\n\t\tthis.#parsedBody = config.parsedBody;\n\t\tthis.#inputSchema = config.inputSchema;\n\t\tthis.#shouldValidateInput = config.shouldValidateInput ?? true;\n\t}\n\t/**\n\t* Create a RequestContext from a Request object for server-side handling\n\t*/\n\tstatic async fromRequest(config) {\n\t\treturn new RequestInputContext({\n\t\t\tmethod: config.method,\n\t\t\tpath: config.path,\n\t\t\tpathParams: config.state.pathParams,\n\t\t\tsearchParams: config.state.searchParams,\n\t\t\theaders: config.state.headers,\n\t\t\trequest: config.request,\n\t\t\tparsedBody: config.state.body,\n\t\t\trawBody: config.rawBody,\n\t\t\tinputSchema: config.inputSchema,\n\t\t\tshouldValidateInput: config.shouldValidateInput\n\t\t});\n\t}\n\t/**\n\t* Create a RequestContext for server-side rendering contexts (no Request object)\n\t*/\n\tstatic fromSSRContext(config) {\n\t\treturn new RequestInputContext({\n\t\t\tmethod: config.method,\n\t\t\tpath: config.path,\n\t\t\tpathParams: config.pathParams,\n\t\t\tsearchParams: config.searchParams ?? new URLSearchParams(),\n\t\t\theaders: config.headers ?? new Headers(),\n\t\t\tparsedBody: \"body\" in config ? config.body : void 0,\n\t\t\tinputSchema: \"inputSchema\" in config ? config.inputSchema : void 0,\n\t\t\tshouldValidateInput: false\n\t\t});\n\t}\n\t/**\n\t* The HTTP method as string (e.g., `GET`, `POST`)\n\t*/\n\tget method() {\n\t\treturn this.#method;\n\t}\n\t/**\n\t* The matched route path (e.g., `/users/:id`)\n\t* @remarks `string`\n\t*/\n\tget path() {\n\t\treturn this.#path;\n\t}\n\t/**\n\t* Extracted path parameters as object (e.g., `{ id: '123' }`)\n\t* @remarks `Record<string, string>`\n\t*/\n\tget pathParams() {\n\t\treturn this.#pathParams;\n\t}\n\t/**\n\t* [URLSearchParams](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams) object for query parameters\n\t* @remarks `URLSearchParams`\n\t*/\n\tget query() {\n\t\treturn this.#searchParams;\n\t}\n\t/**\n\t* [Headers](https://developer.mozilla.org/en-US/docs/Web/API/Headers) object for request headers\n\t* @remarks `Headers`\n\t*/\n\tget headers() {\n\t\treturn this.#headers;\n\t}\n\tget rawBody() {\n\t\treturn this.#body;\n\t}\n\tget request() {\n\t\treturn this.#request;\n\t}\n\t/**\n\t* Access the request body as FormData.\n\t*\n\t* Use this method when handling file uploads or multipart form submissions.\n\t* The request must have been sent with Content-Type: multipart/form-data.\n\t*\n\t* @throws Error if the request body is not FormData\n\t*\n\t* @example\n\t* ```typescript\n\t* defineRoute({\n\t* method: \"POST\",\n\t* path: \"/upload\",\n\t* async handler(ctx, res) {\n\t* const formData = ctx.formData();\n\t* const file = formData.get(\"file\") as File;\n\t* const description = formData.get(\"description\") as string;\n\t* // ... process file\n\t* }\n\t* });\n\t* ```\n\t*/\n\tformData() {\n\t\tif (!(this.#parsedBody instanceof FormData)) throw new Error(\"Request body is not FormData. Ensure the request was sent with Content-Type: multipart/form-data.\");\n\t\treturn this.#parsedBody;\n\t}\n\t/**\n\t* Check if the request body is FormData.\n\t*\n\t* Useful for routes that accept both JSON and FormData payloads.\n\t*\n\t* @example\n\t* ```typescript\n\t* defineRoute({\n\t* method: \"POST\",\n\t* path: \"/upload\",\n\t* async handler(ctx, res) {\n\t* if (ctx.isFormData()) {\n\t* const formData = ctx.formData();\n\t* // handle file upload\n\t* } else {\n\t* const json = await ctx.input.valid();\n\t* // handle JSON payload\n\t* }\n\t* }\n\t* });\n\t* ```\n\t*/\n\tisFormData() {\n\t\treturn this.#parsedBody instanceof FormData;\n\t}\n\t/**\n\t* Access the request body as a ReadableStream (application/octet-stream).\n\t*\n\t* @throws Error if the request body is not a ReadableStream\n\t*/\n\tbodyStream() {\n\t\tif (!(this.#parsedBody instanceof ReadableStream)) throw new Error(\"Request body is not a ReadableStream. Ensure the request was sent with Content-Type: application/octet-stream.\");\n\t\treturn this.#parsedBody;\n\t}\n\t/**\n\t* Check if the request body is a ReadableStream.\n\t*/\n\tisBodyStream() {\n\t\treturn this.#parsedBody instanceof ReadableStream;\n\t}\n\t/**\n\t* Input validation context (only if inputSchema is defined)\n\t* @remarks `InputContext`\n\t*/\n\tget input() {\n\t\tif (!this.#inputSchema) return;\n\t\treturn {\n\t\t\tschema: this.#inputSchema,\n\t\t\tvalid: async () => {\n\t\t\t\tif (!this.#shouldValidateInput) return this.#parsedBody;\n\t\t\t\treturn this.#validateInput();\n\t\t\t}\n\t\t};\n\t}\n\tasync #validateInput() {\n\t\tif (!this.#inputSchema) throw new Error(\"No input schema defined for this route\");\n\t\tif (this.#parsedBody instanceof FormData || this.#parsedBody instanceof Blob) throw new Error(\"Schema validation is only supported for JSON data, not FormData or Blob\");\n\t\tif (this.#parsedBody instanceof ReadableStream) throw new Error(\"Schema validation is only supported for JSON data, not FormData, Blob, or ReadableStream\");\n\t\tconst result = await this.#inputSchema[\"~standard\"].validate(this.#parsedBody);\n\t\tif (result.issues) throw new FragnoApiValidationError(\"Validation failed\", result.issues);\n\t\treturn result.value;\n\t}\n};\n//#endregion\nexport { RequestInputContext };\n\n//# sourceMappingURL=request-input-context.js.map","//#region src/api/internal/response-stream.ts\nvar ResponseStream = class {\n\t#writer;\n\t#encoder;\n\t#abortSubscribers = [];\n\t#responseReadable;\n\t#aborted = false;\n\t#abortPromise;\n\t#closed = false;\n\t/**\n\t* Whether the stream has been aborted.\n\t*/\n\tget aborted() {\n\t\treturn this.#aborted;\n\t}\n\t/**\n\t* Whether the stream has been closed normally.\n\t*/\n\tget closed() {\n\t\treturn this.#closed;\n\t}\n\t/**\n\t* The readable stream that the response is piped to.\n\t*/\n\tget responseReadable() {\n\t\treturn this.#responseReadable;\n\t}\n\tconstructor(writable, readable) {\n\t\tthis.#writer = writable.getWriter();\n\t\tthis.#encoder = new TextEncoder();\n\t\tconst reader = readable.getReader();\n\t\tthis.#abortSubscribers.push(async () => {\n\t\t\tawait reader.cancel();\n\t\t});\n\t\tthis.#responseReadable = new ReadableStream({\n\t\t\tasync pull(controller) {\n\t\t\t\tconst { done, value } = await reader.read();\n\t\t\t\tif (done) controller.close();\n\t\t\t\telse controller.enqueue(value);\n\t\t\t},\n\t\t\tcancel: () => this.abort()\n\t\t});\n\t}\n\tasync writeRaw(input) {\n\t\ttry {\n\t\t\tif (typeof input === \"string\") input = this.#encoder.encode(input);\n\t\t\tawait this.#writer.write(input);\n\t\t\treturn true;\n\t\t} catch {\n\t\t\treturn false;\n\t\t}\n\t}\n\tasync write(input) {\n\t\tawait this.writeRaw(JSON.stringify(input) + \"\\n\");\n\t}\n\tsleep(ms) {\n\t\treturn new Promise((resolve) => {\n\t\t\tsetTimeout(resolve, ms);\n\t\t});\n\t}\n\tasync close() {\n\t\ttry {\n\t\t\tawait this.#writer.close();\n\t\t} catch {} finally {\n\t\t\tthis.#closed = true;\n\t\t}\n\t}\n\tonAbort(listener) {\n\t\tthis.#abortSubscribers.push(listener);\n\t}\n\t/**\n\t* Abort the stream.\n\t* You can call this method when stream is aborted by external event.\n\t*/\n\tabort() {\n\t\tif (!this.#abortPromise) {\n\t\t\tthis.#aborted = true;\n\t\t\tthis.#abortPromise = Promise.allSettled(this.#abortSubscribers.map(async (subscriber) => subscriber())).then((results) => {\n\t\t\t\tconst errors = [];\n\t\t\t\tfor (const result of results) if (result.status === \"rejected\") errors.push(result.reason);\n\t\t\t\tif (errors.length > 0) throw new AggregateError(errors, \"Failed to abort response stream.\");\n\t\t\t});\n\t\t}\n\t\treturn this.#abortPromise;\n\t}\n};\n//#endregion\nexport { ResponseStream };\n\n//# sourceMappingURL=response-stream.js.map","import { ResponseStream } from \"./internal/response-stream.js\";\n//#region src/api/request-output-context.ts\n/**\n* Utility function to merge headers from multiple sources.\n* Later headers override earlier ones.\n*/\nfunction mergeHeaders(...headerSources) {\n\tconst mergedHeaders = new Headers();\n\tfor (const headerSource of headerSources) {\n\t\tif (!headerSource) continue;\n\t\tif (headerSource instanceof Headers) for (const [key, value] of headerSource.entries()) mergedHeaders.set(key, value);\n\t\telse if (Array.isArray(headerSource)) for (const [key, value] of headerSource) mergedHeaders.set(key, value);\n\t\telse for (const [key, value] of Object.entries(headerSource)) mergedHeaders.set(key, value);\n\t}\n\treturn mergedHeaders;\n}\nvar OutputContext = class {\n\t#runJsonStreamCallback;\n\tconstructor(options = {}) {\n\t\tthis.#runJsonStreamCallback = options.runJsonStreamCallback;\n\t}\n\t/**\n\t* Creates an error response.\n\t*\n\t* Shortcut for `throw new FragnoApiError(...)`\n\t*/\n\terror = ({ message, code }, initOrStatus, headers) => {\n\t\tif (typeof initOrStatus === \"undefined\") return Response.json({\n\t\t\tmessage,\n\t\t\tcode\n\t\t}, {\n\t\t\tstatus: 500,\n\t\t\theaders\n\t\t});\n\t\tif (typeof initOrStatus === \"number\") return Response.json({\n\t\t\tmessage,\n\t\t\tcode\n\t\t}, {\n\t\t\tstatus: initOrStatus,\n\t\t\theaders\n\t\t});\n\t\tconst mergedHeaders = mergeHeaders(initOrStatus.headers, headers);\n\t\treturn Response.json({\n\t\t\tmessage,\n\t\t\tcode\n\t\t}, {\n\t\t\tstatus: initOrStatus.status,\n\t\t\theaders: mergedHeaders\n\t\t});\n\t};\n\tempty = (initOrStatus, headers) => {\n\t\tconst defaultHeaders = {};\n\t\tif (typeof initOrStatus === \"undefined\") {\n\t\t\tconst mergedHeaders = mergeHeaders(defaultHeaders, headers);\n\t\t\treturn new Response(null, {\n\t\t\t\tstatus: 201,\n\t\t\t\theaders: mergedHeaders\n\t\t\t});\n\t\t}\n\t\tif (typeof initOrStatus === \"number\") {\n\t\t\tconst mergedHeaders = mergeHeaders(defaultHeaders, headers);\n\t\t\treturn new Response(null, {\n\t\t\t\tstatus: initOrStatus,\n\t\t\t\theaders: mergedHeaders\n\t\t\t});\n\t\t}\n\t\tconst mergedHeaders = mergeHeaders(defaultHeaders, initOrStatus.headers, headers);\n\t\treturn new Response(null, {\n\t\t\tstatus: initOrStatus.status,\n\t\t\theaders: mergedHeaders\n\t\t});\n\t};\n\tjson = (object, initOrStatus, headers) => {\n\t\tif (typeof initOrStatus === \"undefined\") return Response.json(object, {\n\t\t\tstatus: 200,\n\t\t\theaders\n\t\t});\n\t\tif (typeof initOrStatus === \"number\") return Response.json(object, {\n\t\t\tstatus: initOrStatus,\n\t\t\theaders\n\t\t});\n\t\tconst mergedHeaders = mergeHeaders(initOrStatus.headers, headers);\n\t\treturn Response.json(object, {\n\t\t\tstatus: initOrStatus.status,\n\t\t\theaders: mergedHeaders\n\t\t});\n\t};\n\tjsonStream = (cb, { onError, headers } = {}) => {\n\t\tconst defaultHeaders = {\n\t\t\t\"content-type\": \"application/x-ndjson; charset=utf-8\",\n\t\t\t\"transfer-encoding\": \"chunked\",\n\t\t\t\"cache-control\": \"no-cache\"\n\t\t};\n\t\tconst { readable, writable } = new TransformStream();\n\t\tconst stream = new ResponseStream(writable, readable);\n\t\tconst runStreamCallback = async () => {\n\t\t\ttry {\n\t\t\t\tawait cb(stream);\n\t\t\t} catch (e) {\n\t\t\t\tif (e === void 0) {} else if (e instanceof Error && onError) await onError(e, stream);\n\t\t\t\telse console.error(e);\n\t\t\t} finally {\n\t\t\t\tawait stream.close();\n\t\t\t}\n\t\t};\n\t\ttry {\n\t\t\tPromise.resolve(this.#runJsonStreamCallback ? this.#runJsonStreamCallback(runStreamCallback) : runStreamCallback()).catch(async (error) => {\n\t\t\t\tconsole.error(error);\n\t\t\t\tawait stream.close();\n\t\t\t});\n\t\t} catch (error) {\n\t\t\tconsole.error(error);\n\t\t\tstream.close();\n\t\t}\n\t\treturn new Response(stream.responseReadable, {\n\t\t\tstatus: 200,\n\t\t\theaders: mergeHeaders(defaultHeaders, headers)\n\t\t});\n\t};\n};\nvar RequestOutputContext = class extends OutputContext {\n\t#outputSchema;\n\tconstructor(outputSchema, options) {\n\t\tsuper(options);\n\t\tthis.#outputSchema = outputSchema;\n\t}\n};\n//#endregion\nexport { OutputContext, RequestOutputContext };\n\n//# sourceMappingURL=request-output-context.js.map","//#region src/api/route.ts\n/**\n* Helper to resolve route factories into routes\n* @internal\n*/\nfunction resolveRouteFactories(context, routesOrFactories) {\n\tconst routes = [];\n\tfor (const item of routesOrFactories) if (typeof item === \"function\") {\n\t\tconst factoryRoutes = item(context);\n\t\troutes.push(...factoryRoutes);\n\t} else routes.push(item);\n\treturn routes;\n}\nfunction defineRoute(config) {\n\treturn config;\n}\nfunction defineRoutes(_definition) {\n\treturn { create: (fn) => {\n\t\treturn (ctx) => {\n\t\t\treturn fn({\n\t\t\t\t...ctx,\n\t\t\t\tdefineRoute\n\t\t\t});\n\t\t};\n\t} };\n}\n//#endregion\nexport { defineRoute, defineRoutes, resolveRouteFactories };\n\n//# sourceMappingURL=route.js.map","//#region src/util/content-type.ts\n/**\n* Parses a content-type header string into its components\n*\n* @param contentType - The content-type header value to parse\n* @returns A ParsedContentType object or null if the input is invalid\n*\n* @example\n* ```ts\n* const { type, subtype, mediaType, parameters }\n* = parseContentType(\"application/json; charset=utf-8\");\n* console.assert(type === \"application\");\n* console.assert(subtype === \"json\");\n* console.assert(mediaType === \"application/json\");\n* console.assert(parameters[\"charset\"] === \"utf-8\");\n*/\nfunction parseContentType(contentType) {\n\tif (!contentType || typeof contentType !== \"string\") return null;\n\tconst trimmed = contentType.trim();\n\tif (!trimmed) return null;\n\tconst parts = trimmed.split(\";\").map((part) => part.trim());\n\tconst mediaType = parts[0];\n\tif (!mediaType) return null;\n\tconst typeParts = mediaType.split(\"/\");\n\tif (typeParts.length !== 2) return null;\n\tconst [type, subtype] = typeParts.map((part) => part.trim().toLowerCase());\n\tif (!type || !subtype) return null;\n\tconst parameters = {};\n\tfor (let i = 1; i < parts.length; i++) {\n\t\tconst param = parts[i];\n\t\tconst equalIndex = param.indexOf(\"=\");\n\t\tif (equalIndex > 0) {\n\t\t\tconst key = param.slice(0, equalIndex).trim().toLowerCase();\n\t\t\tlet value = param.slice(equalIndex + 1).trim();\n\t\t\tif (value.startsWith(\"\\\"\") && value.endsWith(\"\\\"\")) value = value.slice(1, -1);\n\t\t\tif (key) parameters[key] = value;\n\t\t}\n\t}\n\treturn {\n\t\ttype,\n\t\tsubtype,\n\t\tmediaType: `${type}/${subtype}`,\n\t\tparameters\n\t};\n}\n//#endregion\nexport { parseContentType };\n\n//# sourceMappingURL=content-type.js.map","//#region src/util/nanostores.ts\n/**\n* Normalizes a value that could be a plain value, an Atom, or a Vue Ref to a plain value.\n*/\nfunction unwrapAtom(value) {\n\tif (value && typeof value === \"object\" && \"get\" in value && typeof value.get === \"function\") return value.get();\n\treturn value;\n}\n/**\n* Normalizes an object where values can be plain values, Atoms, or Vue Refs.\n* Returns a new object with all values normalized to plain values.\n*/\nfunction unwrapObject(params) {\n\tif (!params) return;\n\treturn Object.fromEntries(Object.entries(params).map(([key, value]) => [key, unwrapAtom(value)]));\n}\nfunction isReadableAtom(value) {\n\tif (!value) return false;\n\tif (typeof value !== \"object\" || value === null) return false;\n\tif (!(\"get\" in value) || typeof value.get !== \"function\") return false;\n\tif (!(\"lc\" in value) || typeof value.lc !== \"number\") return false;\n\tif (!(\"notify\" in value) || typeof value.notify !== \"function\") return false;\n\tif (!(\"off\" in value) || typeof value.off !== \"function\") return false;\n\tif (!(\"subscribe\" in value) || typeof value.subscribe !== \"function\") return false;\n\tif (!(\"value\" in value)) return false;\n\treturn true;\n}\n//#endregion\nexport { isReadableAtom, unwrapAtom, unwrapObject };\n\n//# sourceMappingURL=nanostores.js.map","let tasks = 0\nlet resolves = []\n\nexport function startTask() {\n tasks += 1\n return () => {\n tasks -= 1\n if (tasks === 0) {\n let prevResolves = resolves\n resolves = []\n for (let i of prevResolves) i()\n }\n }\n}\n\nexport function task(cb) {\n let endTask = startTask()\n let promise = cb().finally(endTask)\n promise.t = true\n return promise\n}\n\nexport function allTasks() {\n if (tasks === 0) {\n return Promise.resolve()\n } else {\n return new Promise(resolve => {\n resolves.push(resolve)\n })\n }\n}\n\nexport function cleanTasks() {\n tasks = 0\n}\n","import { cleanTasks } from '../task/index.js'\n\nexport const clean = Symbol('clean')\n\nexport const cleanStores = (...stores) => {\n if (process.env.NODE_ENV === 'production') {\n throw new Error(\n 'cleanStores() can be used only during development or tests'\n )\n }\n cleanTasks()\n for (let $store of stores) {\n if ($store) {\n if ($store.mocked) delete $store.mocked\n if ($store[clean]) $store[clean]()\n }\n }\n}\n","import { clean } from '../clean-stores/index.js'\n\nlet listenerQueue = []\nlet lqIndex = 0\nconst QUEUE_ITEMS_PER_LISTENER = 4\n// Use globalThis.nanostoresGlobal to store epoch so all module instances share\n// the same counter. This fixes issues when Nano Store is bundled separately\n// in different parts of an application (e.g., tree-shaking separates core\n// from React), causing each bundle to have its own epoch instance.\nexport const nanostoresGlobal = (globalThis.nanostoresGlobal ||= { epoch: 0 })\n\n/* @__NO_SIDE_EFFECTS__ */\nexport const atom = initialValue => {\n let listeners = []\n let $atom = {\n get() {\n if (!$atom.lc) {\n $atom.listen(() => {})()\n }\n return $atom.value\n },\n init: initialValue,\n lc: 0,\n listen(listener) {\n $atom.lc = listeners.push(listener)\n\n return () => {\n for (\n let i = lqIndex + QUEUE_ITEMS_PER_LISTENER;\n i < listenerQueue.length;\n ) {\n if (listenerQueue[i] === listener) {\n listenerQueue.splice(i, QUEUE_ITEMS_PER_LISTENER)\n } else {\n i += QUEUE_ITEMS_PER_LISTENER\n }\n }\n\n let index = listeners.indexOf(listener)\n if (~index) {\n listeners.splice(index, 1)\n if (!--$atom.lc) $atom.off()\n }\n }\n },\n notify(oldValue, changedKey) {\n nanostoresGlobal.epoch++\n let runListenerQueue = !listenerQueue.length\n for (let listener of listeners) {\n listenerQueue.push(listener, $atom.value, oldValue, changedKey)\n }\n\n if (runListenerQueue) {\n for (\n lqIndex = 0;\n lqIndex < listenerQueue.length;\n lqIndex += QUEUE_ITEMS_PER_LISTENER\n ) {\n listenerQueue[lqIndex](\n listenerQueue[lqIndex + 1],\n listenerQueue[lqIndex + 2],\n listenerQueue[lqIndex + 3]\n )\n }\n listenerQueue.length = 0\n }\n },\n /* It will be called on last listener unsubscribing.\n We will redefine it in onMount and onStop. */\n off() {},\n set(newValue) {\n let oldValue = $atom.value\n if (oldValue !== newValue) {\n $atom.value = newValue\n $atom.notify(oldValue)\n }\n },\n subscribe(listener) {\n let unbind = $atom.listen(listener)\n listener($atom.value)\n return unbind\n },\n value: initialValue\n }\n\n if (process.env.NODE_ENV !== 'production') {\n $atom[clean] = () => {\n listeners = []\n $atom.lc = 0\n $atom.off()\n }\n }\n\n return $atom\n}\n\nexport const readonlyType = store => store\n","import { clean } from '../clean-stores/index.js'\n\nconst START = 0\nconst STOP = 1\nconst SET = 2\nconst NOTIFY = 3\nconst MOUNT = 5\nconst UNMOUNT = 6\nconst REVERT_MUTATION = 10\n\nexport let on = (object, listener, eventKey, mutateStore) => {\n object.events = object.events || {}\n if (!object.events[eventKey + REVERT_MUTATION]) {\n object.events[eventKey + REVERT_MUTATION] = mutateStore(eventProps => {\n // eslint-disable-next-line no-sequences\n object.events[eventKey].reduceRight((event, l) => (l(event), event), {\n shared: {},\n ...eventProps\n })\n })\n }\n object.events[eventKey] = object.events[eventKey] || []\n object.events[eventKey].push(listener)\n return () => {\n let currentListeners = object.events[eventKey]\n let index = currentListeners.indexOf(listener)\n currentListeners.splice(index, 1)\n if (!currentListeners.length) {\n delete object.events[eventKey]\n object.events[eventKey + REVERT_MUTATION]()\n delete object.events[eventKey + REVERT_MUTATION]\n }\n }\n}\n\nexport let onStart = ($store, listener) =>\n on($store, listener, START, runListeners => {\n let originListen = $store.listen\n $store.listen = arg => {\n if (!$store.lc && !$store.starting) {\n $store.starting = true\n runListeners()\n delete $store.starting\n }\n return originListen(arg)\n }\n return () => {\n $store.listen = originListen\n }\n })\n\nexport let onStop = ($store, listener) =>\n on($store, listener, STOP, runListeners => {\n let originOff = $store.off\n $store.off = () => {\n runListeners()\n originOff()\n }\n return () => {\n $store.off = originOff\n }\n })\n\nexport let onSet = ($store, listener) =>\n on($store, listener, SET, runListeners => {\n let originSet = $store.set\n let originSetKey = $store.setKey\n if ($store.setKey) {\n $store.setKey = (changed, changedValue) => {\n let isAborted\n let abort = () => {\n isAborted = true\n }\n\n runListeners({\n abort,\n changed,\n newValue: { ...$store.value, [changed]: changedValue }\n })\n if (!isAborted) return originSetKey(changed, changedValue)\n }\n }\n $store.set = newValue => {\n let isAborted\n let abort = () => {\n isAborted = true\n }\n\n runListeners({ abort, newValue })\n if (!isAborted) return originSet(newValue)\n }\n return () => {\n $store.set = originSet\n $store.setKey = originSetKey\n }\n })\n\nexport let onNotify = ($store, listener) =>\n on($store, listener, NOTIFY, runListeners => {\n let originNotify = $store.notify\n $store.notify = (oldValue, changed) => {\n let isAborted\n let abort = () => {\n isAborted = true\n }\n\n runListeners({ abort, changed, oldValue })\n if (!isAborted) return originNotify(oldValue, changed)\n }\n return () => {\n $store.notify = originNotify\n }\n })\n\nexport const STORE_UNMOUNT_DELAY = 1000\n\nexport let onMount = ($store, initialize) => {\n let listener = payload => {\n let destroy = initialize(payload)\n if (destroy) $store.events[UNMOUNT].push(destroy)\n }\n return on($store, listener, MOUNT, runListeners => {\n let originListen = $store.listen\n $store.listen = (...args) => {\n if (!$store.lc && !$store.active) {\n $store.active = true\n runListeners()\n }\n return originListen(...args)\n }\n\n let originOff = $store.off\n $store.events[UNMOUNT] = []\n $store.off = () => {\n originOff()\n setTimeout(() => {\n if ($store.active && !$store.lc) {\n $store.active = false\n for (let destroy of $store.events[UNMOUNT]) destroy()\n $store.events[UNMOUNT] = []\n }\n }, STORE_UNMOUNT_DELAY)\n }\n\n if (process.env.NODE_ENV !== 'production') {\n let originClean = $store[clean]\n $store[clean] = () => {\n for (let destroy of $store.events[UNMOUNT]) destroy()\n $store.events[UNMOUNT] = []\n $store.active = false\n originClean()\n }\n }\n\n return () => {\n $store.listen = originListen\n $store.off = originOff\n }\n })\n}\n","let warned = {}\n\nexport function warn(text) {\n if (!warned[text]) {\n warned[text] = true\n if (typeof console !== 'undefined' && console.warn) {\n console.groupCollapsed('Nano Stores: ' + text)\n console.trace('Source of deprecated call')\n console.groupEnd()\n }\n }\n}\n","import { atom, nanostoresGlobal } from '../atom/index.js'\nimport { onMount } from '../lifecycle/index.js'\nimport { warn } from '../warn/index.js'\n\nlet computedStore = (stores, cb, batched) => {\n if (!Array.isArray(stores)) stores = [stores]\n\n let previousArgs\n let currentEpoch\n let set = () => {\n if (currentEpoch === nanostoresGlobal.epoch) return\n currentEpoch = nanostoresGlobal.epoch\n let args = stores.map($store => $store.get())\n if (!previousArgs || args.some((arg, i) => arg !== previousArgs[i])) {\n previousArgs = args\n let value = cb(...args)\n if (value && value.then && value.t) {\n if (process.env.NODE_ENV !== 'production') {\n warn(\n 'Use @nanostores/async for async computed. We will remove Promise support in computed() in Nano Stores 2.0'\n )\n }\n value.then(asyncValue => {\n if (previousArgs === args) {\n // Prevent a stale set\n $computed.set(asyncValue)\n }\n })\n } else {\n $computed.set(value)\n currentEpoch = nanostoresGlobal.epoch\n }\n }\n }\n let $computed = atom(undefined)\n let get = $computed.get\n $computed.get = () => {\n set()\n return get()\n }\n\n let timer\n let run = batched\n ? () => {\n clearTimeout(timer)\n timer = setTimeout(set)\n }\n : set\n\n onMount($computed, () => {\n let unbinds = stores.map($store => $store.listen(run))\n set()\n return () => {\n for (let unbind of unbinds) unbind()\n }\n })\n\n return $computed\n}\n\n/* @__NO_SIDE_EFFECTS__ */\nexport const computed = (stores, fn) => computedStore(stores, fn)\n\n/* @__NO_SIDE_EFFECTS__ */\nexport const batched = (stores, fn) => computedStore(stores, fn, true)\n","import { atom } from '../atom/index.js'\n\n/* @__NO_SIDE_EFFECTS__ */\nexport const map = (initial = {}) => {\n let $map = atom(initial)\n\n $map.setKey = function (key, value) {\n let oldMap = $map.value\n if (typeof value === 'undefined' && key in $map.value) {\n $map.value = { ...$map.value }\n delete $map.value[key]\n $map.notify(oldMap, key)\n } else if ($map.value[key] !== value) {\n $map.value = {\n ...$map.value,\n [key]: value\n }\n $map.notify(oldMap, key)\n }\n }\n\n return $map\n}\n","import { allTasks } from \"nanostores\";\n//#region src/util/ssr.ts\nlet stores = [];\nfunction getStores() {\n\treturn stores;\n}\nfunction addStore(store) {\n\tstores.push(store);\n}\nfunction cleanStores() {\n\tstores = [];\n}\nlet clientInitialData;\nfunction hydrateFromWindow() {\n\tif (window?.__FRAGNO_INITIAL_DATA__) {\n\t\tclientInitialData = new Map(window.__FRAGNO_INITIAL_DATA__);\n\t\tdelete window.__FRAGNO_INITIAL_DATA__;\n\t\tconsole.warn(\"hydrateFromWindow\", { clientInitialData: Array.from(clientInitialData.entries()) });\n\t}\n}\nfunction getInitialData(key) {\n\tif (clientInitialData?.has(key)) {\n\t\tconst data = clientInitialData.get(key);\n\t\tclientInitialData.delete(key);\n\t\treturn data;\n\t}\n}\nfunction listenToStores() {\n\tfor (const store of getStores()) store.listen(() => {});\n}\nasync function getFinalStoreValues() {\n\tlistenToStores();\n\tawait allTasks();\n\tconst stores = getStores();\n\tconst storesInitialValue = /* @__PURE__ */ new Map();\n\tfor (const store of stores) {\n\t\tconst value = store.get();\n\t\tif (!value || !store.key || value.loading) continue;\n\t\tstoresInitialValue.set(store.key, value.data);\n\t}\n\treturn storesInitialValue;\n}\n//#endregion\nexport { addStore, cleanStores, getFinalStoreValues, getInitialData, getStores, hydrateFromWindow };\n\n//# sourceMappingURL=ssr.js.map","//#region src/client/client-error.ts\n/**\n* Base error class for all Fragno client errors.\n*/\nvar FragnoClientError = class extends Error {\n\t#code;\n\tconstructor(message, code, options = {}) {\n\t\tsuper(message, { cause: options.cause });\n\t\tthis.name = \"FragnoClientError\";\n\t\tthis.#code = code;\n\t}\n\tget code() {\n\t\treturn this.#code;\n\t}\n};\nvar FragnoClientFetchError = class extends FragnoClientError {\n\tconstructor(message, code, options = {}) {\n\t\tsuper(message, code, options);\n\t\tthis.name = \"FragnoClientFetchError\";\n\t}\n\tstatic fromUnknownFetchError(error) {\n\t\tif (!(error instanceof Error)) return new FragnoClientFetchNetworkError(\"Network request failed\", { cause: error });\n\t\tif (error.name === \"AbortError\") return new FragnoClientFetchAbortError(\"Request was aborted\", { cause: error });\n\t\treturn new FragnoClientFetchNetworkError(\"Network request failed\", { cause: error });\n\t}\n};\n/**\n* Error thrown when a network request fails (e.g., no internet connection, DNS failure).\n*/\nvar FragnoClientFetchNetworkError = class extends FragnoClientFetchError {\n\tconstructor(message = \"Network request failed\", options = {}) {\n\t\tsuper(message, \"NETWORK_ERROR\", options);\n\t\tthis.name = \"FragnoClientFetchNetworkError\";\n\t}\n};\n/**\n* Error thrown when a request is aborted (e.g., user cancels request, timeout).\n*/\nvar FragnoClientFetchAbortError = class extends FragnoClientFetchError {\n\tconstructor(message = \"Request was aborted\", options = {}) {\n\t\tsuper(message, \"ABORT_ERROR\", options);\n\t\tthis.name = \"FragnoClientFetchAbortError\";\n\t}\n};\n/**\n* Error thrown when the API result is unexpected, e.g. no json is returned.\n*/\nvar FragnoClientUnknownApiError = class extends FragnoClientError {\n\t#status;\n\tconstructor(message = \"Unknown API error\", status, options = {}) {\n\t\tsuper(message, \"UNKNOWN_API_ERROR\", options);\n\t\tthis.name = \"FragnoClientUnknownApiError\";\n\t\tthis.#status = status;\n\t}\n\tget status() {\n\t\treturn this.#status;\n\t}\n};\nvar FragnoClientApiError = class FragnoClientApiError extends FragnoClientError {\n\t#status;\n\tconstructor({ message, code }, status, options = {}) {\n\t\tsuper(message, code, options);\n\t\tthis.name = \"FragnoClientApiError\";\n\t\tthis.#status = status;\n\t}\n\tget status() {\n\t\treturn this.#status;\n\t}\n\t/**\n\t* The error code returned by the API.\n\t*\n\t* The type is `TErrorCode` (the set of known error codes for this route), but may also be a string\n\t* for forward compatibility with future error codes.\n\t*/\n\tget code() {\n\t\treturn super.code;\n\t}\n\tstatic async fromResponse(response) {\n\t\tconst unknown = await response.json();\n\t\tconst status = response.status;\n\t\tif (!(\"message\" in unknown || \"code\" in unknown)) return new FragnoClientUnknownApiError(\"Unknown API error\", status);\n\t\tif (!(typeof unknown.message === \"string\" && typeof unknown.code === \"string\")) return new FragnoClientUnknownApiError(\"Unknown API error\", status);\n\t\treturn new FragnoClientApiError({\n\t\t\tmessage: unknown.message,\n\t\t\tcode: unknown.code\n\t\t}, status);\n\t}\n};\n//#endregion\nexport { FragnoClientApiError, FragnoClientError, FragnoClientFetchAbortError, FragnoClientFetchError, FragnoClientFetchNetworkError, FragnoClientUnknownApiError };\n\n//# sourceMappingURL=client-error.js.map","//#region src/client/internal/fetcher-merge.ts\n/**\n* Merge two fetcher configurations, with user config taking precedence.\n* If user provides a custom function, it takes full precedence.\n* Otherwise, deep merge RequestInit options.\n*/\nfunction mergeFetcherConfigs(authorConfig, userConfig) {\n\tif (userConfig?.type === \"function\") return userConfig;\n\tif (!userConfig && authorConfig?.type === \"function\") return authorConfig;\n\tconst authorOpts = authorConfig?.type === \"options\" ? authorConfig.options : {};\n\tconst userOpts = userConfig?.type === \"options\" ? userConfig.options : {};\n\tif (Object.keys(authorOpts).length === 0 && Object.keys(userOpts).length === 0) return;\n\treturn {\n\t\ttype: \"options\",\n\t\toptions: {\n\t\t\t...authorOpts,\n\t\t\t...userOpts,\n\t\t\theaders: mergeHeaders(authorOpts.headers, userOpts.headers)\n\t\t}\n\t};\n}\n/**\n* Merge headers from author and user configs.\n* User headers override author headers.\n*/\nfunction mergeHeaders(author, user) {\n\tif (!author && !user) return;\n\tconst merged = new Headers(author);\n\tnew Headers(user).forEach((value, key) => {\n\t\tmerged.set(key, value);\n\t});\n\tif (merged.keys().next().done) return;\n\treturn merged;\n}\n//#endregion\nexport { mergeFetcherConfigs };\n\n//# sourceMappingURL=fetcher-merge.js.map","import { FragnoClientFetchAbortError, FragnoClientFetchError, FragnoClientUnknownApiError } from \"../client-error.js\";\n//#region src/client/internal/ndjson-streaming.ts\n/**\n* Creates a promise that rejects when the abort signal is triggered\n*/\nfunction createAbortPromise(abortSignal) {\n\treturn new Promise((_, reject) => {\n\t\tconst abortHandler = () => {\n\t\t\treject(new FragnoClientFetchAbortError(\"Operation was aborted\"));\n\t\t};\n\t\tif (abortSignal.aborted) abortHandler();\n\t\telse abortSignal.addEventListener(\"abort\", abortHandler, { once: true });\n\t});\n}\nconst parseNdjsonBuffer = (buffer, options) => {\n\tconst lines = buffer.split(\"\\n\");\n\tconst trailingBuffer = options.flush ? \"\" : lines.pop() ?? \"\";\n\tfor (let index = 0; index < lines.length; index += 1) {\n\t\tconst line = lines[index];\n\t\tif (!line.trim()) continue;\n\t\tlet jsonObject;\n\t\ttry {\n\t\t\tjsonObject = JSON.parse(line);\n\t\t} catch (parseError) {\n\t\t\tthrow new FragnoClientUnknownApiError(\"Failed to parse NDJSON line\", options.parseErrorStatus, { cause: parseError });\n\t\t}\n\t\tif (options.onItem(jsonObject) === false) {\n\t\t\tconst remainingLines = lines.slice(index + 1);\n\t\t\treturn {\n\t\t\t\tstopped: true,\n\t\t\t\tbuffer: remainingLines.length > 0 ? `${remainingLines.join(\"\\n\")}\\n${trailingBuffer}` : trailingBuffer\n\t\t\t};\n\t\t}\n\t}\n\treturn {\n\t\tstopped: false,\n\t\tbuffer: trailingBuffer\n\t};\n};\n/**\n* Handles NDJSON streaming responses by returning the first item from the fetcher\n* and then continuing to stream updates via the store's mutate method.\n*\n* This makes it so that we can wait until the first chunk before updating the store, if we did\n* not do this, `loading` would briefly be false before the first item would be populated in the\n* result.\n*\n* @param response - The fetch Response object containing the NDJSON stream\n* @param store - The fetcher store to update with streaming data\n* @param abortSignal - Optional AbortSignal to cancel the streaming operation\n* @returns A promise that resolves to an object containing the first item and a streaming promise\n*/\nasync function handleNdjsonStreamingFirstItem(response, store, options = {}) {\n\tif (!response.body) throw new FragnoClientFetchError(\"Streaming response has no body\", \"NO_BODY\");\n\tconst { abortSignal } = options;\n\tif (abortSignal?.aborted) throw new FragnoClientFetchAbortError(\"Operation was aborted\");\n\tconst decoder = new TextDecoder();\n\tconst reader = response.body.getReader();\n\tlet buffer = \"\";\n\tlet firstItem = null;\n\tconst items = [];\n\ttry {\n\t\twhile (firstItem === null) {\n\t\t\tif (abortSignal?.aborted) {\n\t\t\t\treader.releaseLock();\n\t\t\t\tthrow new FragnoClientFetchAbortError(\"Operation was aborted\");\n\t\t\t}\n\t\t\tconst { done, value } = await (abortSignal ? Promise.race([reader.read(), createAbortPromise(abortSignal)]) : reader.read());\n\t\t\tif (done) break;\n\t\t\tbuffer += decoder.decode(value, { stream: true });\n\t\t\tbuffer = parseNdjsonBuffer(buffer, {\n\t\t\t\tparseErrorStatus: 500,\n\t\t\t\tonItem: (jsonObject) => {\n\t\t\t\t\titems.push(jsonObject);\n\t\t\t\t\tfirstItem = jsonObject;\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t}).buffer;\n\t\t\tif (firstItem !== null) {\n\t\t\t\tconst streamingPromise = continueStreaming(reader, decoder, buffer, items, store, abortSignal);\n\t\t\t\treturn {\n\t\t\t\t\tfirstItem,\n\t\t\t\t\titems,\n\t\t\t\t\tstreamingPromise\n\t\t\t\t};\n\t\t\t}\n\t\t}\n\t\tif (firstItem === null) {\n\t\t\treader.releaseLock();\n\t\t\tthrow new FragnoClientUnknownApiError(\"NDJSON stream contained no valid items\", 500);\n\t\t}\n\t\treader.releaseLock();\n\t\tthrow new FragnoClientFetchError(\"Unexpected end of stream processing\", \"NO_BODY\");\n\t} catch (error) {\n\t\tif (error instanceof FragnoClientFetchError || error instanceof FragnoClientUnknownApiError) {\n\t\t\tstore?.setError(error);\n\t\t\tthrow error;\n\t\t}\n\t\tconst clientError = new FragnoClientUnknownApiError(\"Unknown streaming error\", 500, { cause: error });\n\t\tstore?.setError(clientError);\n\t\tthrow clientError;\n\t}\n}\n/**\n* Continues streaming the remaining items in the background\n*/\nasync function continueStreaming(reader, decoder, initialBuffer, items, store, abortSignal) {\n\tlet buffer = initialBuffer;\n\tconst appendItem = (jsonObject) => {\n\t\titems.push(jsonObject);\n\t\tstore?.setData([...items]);\n\t};\n\ttry {\n\t\twhile (true) {\n\t\t\tbuffer = parseNdjsonBuffer(buffer, {\n\t\t\t\tparseErrorStatus: 400,\n\t\t\t\tonItem: appendItem\n\t\t\t}).buffer;\n\t\t\tif (abortSignal?.aborted) throw new FragnoClientFetchAbortError(\"Operation was aborted\");\n\t\t\tconst { done, value } = await (abortSignal ? Promise.race([reader.read(), createAbortPromise(abortSignal)]) : reader.read());\n\t\t\tif (done) {\n\t\t\t\tparseNdjsonBuffer(buffer, {\n\t\t\t\t\tflush: true,\n\t\t\t\t\tparseErrorStatus: 400,\n\t\t\t\t\tonItem: appendItem\n\t\t\t\t});\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tbuffer += decoder.decode(value, { stream: true });\n\t\t}\n\t} catch (error) {\n\t\tif (error instanceof FragnoClientFetchError || error instanceof FragnoClientUnknownApiError) {\n\t\t\tstore?.setError(error);\n\t\t\tthrow error;\n\t\t}\n\t\tconst clientError = new FragnoClientUnknownApiError(\"Unknown streaming error\", 400, { cause: error });\n\t\tstore?.setError(clientError);\n\t\tthrow clientError;\n\t} finally {\n\t\treader.releaseLock();\n\t}\n\treturn items;\n}\n//#endregion\nexport { handleNdjsonStreamingFirstItem };\n\n//# sourceMappingURL=ndjson-streaming.js.map","export let createNanoEvents = () => ({\n emit(event, ...args) {\n for (\n let callbacks = this.events[event] || [],\n i = 0,\n length = callbacks.length;\n i < length;\n i++\n ) {\n callbacks[i](...args)\n }\n },\n events: {},\n on(event, cb) {\n ;(this.events[event] ||= []).push(cb)\n return () => {\n this.events[event] = this.events[event]?.filter(i => cb !== i)\n }\n }\n})\n","import { map, onStart, onStop, atom, batched, startTask } from 'nanostores';\nimport { createNanoEvents } from 'nanoevents';\n\nfunction defaultOnErrorRetry({ retryCount }) {\n return ~~((Math.random() + 0.5) * (1 << (retryCount < 8 ? retryCount : 8))) * 2e3;\n}\nconst nanoqueryFactory = ([\n isAppVisible,\n visibilityChangeSubscribe,\n reconnectChangeSubscribe\n]) => {\n const nanoquery = ({\n cache = /* @__PURE__ */ new Map(),\n fetcher: globalFetcher,\n ...globalSettings\n } = {}) => {\n const events = createNanoEvents();\n let focus = true;\n visibilityChangeSubscribe(() => {\n focus = isAppVisible();\n focus && events.emit(FOCUS);\n });\n reconnectChangeSubscribe(() => events.emit(RECONNECT));\n const _revalidateOnInterval = /* @__PURE__ */ new Map(), _errorInvalidateTimeouts = /* @__PURE__ */ new Map(), _runningFetches = /* @__PURE__ */ new Map();\n let rewrittenSettings = {};\n const getCachedValueByKey = (key) => {\n const fromCache = cache.get(key);\n if (!fromCache)\n return [];\n const cacheHit = (fromCache.expires || 0) > getNow();\n return cacheHit ? [fromCache.data, fromCache.error] : [];\n };\n const runFetcher = async ([key, keyParts], store, settings) => {\n if (!focus)\n return;\n const set = (v) => {\n if (store.key === key) {\n store.set(v);\n events.emit(SET_CACHE, key, v, true);\n }\n };\n const setAsLoading = (prev) => {\n const toSet = prev === void 0 ? {} : { data: prev };\n set({\n ...toSet,\n ...loading,\n promise: _runningFetches.get(key)\n });\n };\n let {\n dedupeTime = 4e3,\n cacheLifetime = Infinity,\n fetcher,\n onErrorRetry = defaultOnErrorRetry\n } = {\n ...settings,\n ...rewrittenSettings\n };\n if (cacheLifetime < dedupeTime)\n cacheLifetime = dedupeTime;\n const now = getNow();\n if (_runningFetches.has(key)) {\n if (!store.value.loading)\n setAsLoading(getCachedValueByKey(key)[0]);\n return;\n }\n let cachedValue, cachedError;\n const fromCache = cache.get(key);\n if (fromCache?.data !== void 0 || fromCache?.error) {\n [cachedValue, cachedError] = getCachedValueByKey(key);\n if ((fromCache.created || 0) + dedupeTime > now) {\n if (store.value.data != cachedValue || store.value.error != cachedError) {\n set({ ...notLoading, data: cachedValue, error: cachedError });\n }\n return;\n }\n }\n const finishTask = startTask();\n try {\n clearTimeout(_errorInvalidateTimeouts.get(key));\n const promise = fetcher(...keyParts);\n _runningFetches.set(key, promise);\n setAsLoading(cachedValue);\n const res = await promise;\n cache.set(key, {\n data: res,\n created: getNow(),\n expires: getNow() + cacheLifetime\n });\n set({ data: res, ...notLoading });\n } catch (error) {\n settings.onError?.(error);\n const retryCount = (cache.get(key)?.retryCount || 0) + 1;\n cache.set(key, {\n error,\n created: getNow(),\n expires: getNow() + cacheLifetime,\n retryCount\n });\n if (onErrorRetry) {\n const timer = onErrorRetry({\n error,\n key,\n retryCount\n });\n if (timer)\n _errorInvalidateTimeouts.set(\n key,\n setTimeout(() => {\n invalidateKeys(key);\n cache.set(key, { retryCount });\n }, timer)\n );\n }\n set({ data: store.value.data, error, ...notLoading });\n } finally {\n finishTask();\n _runningFetches.delete(key);\n }\n };\n const createFetcherStore = (keyInput, {\n fetcher = globalFetcher,\n ...fetcherSettings\n } = {}) => {\n if (process.env.NODE_ENV !== \"production\" && !fetcher) {\n throw new Error(\n \"You need to set up either global fetcher of fetcher in createFetcherStore\"\n );\n }\n const fetcherStore = map({\n ...notLoading\n }), settings = { ...globalSettings, ...fetcherSettings, fetcher };\n fetcherStore._ = fetcherSymbol;\n fetcherStore.invalidate = () => {\n const { key } = fetcherStore;\n if (key) {\n invalidateKeys(key);\n }\n };\n fetcherStore.revalidate = () => {\n const { key } = fetcherStore;\n if (key) {\n revalidateKeys(key);\n }\n };\n fetcherStore.mutate = (data) => {\n const { key } = fetcherStore;\n if (key) {\n mutateCache(key, data);\n }\n };\n fetcherStore.fetch = async () => {\n let resolve;\n const promise = new Promise((r) => resolve = r);\n const unsub = fetcherStore.listen(({ error, data }) => {\n if (error !== void 0)\n resolve({ error });\n if (data !== void 0)\n resolve({ data });\n });\n return promise.finally(unsub);\n };\n let keysInternalUnsub, prevKey, prevKeyParts, keyUnsub, keyStore;\n let evtUnsubs = [];\n onStart(fetcherStore, () => {\n const firstRun = !keysInternalUnsub;\n [keyStore, keysInternalUnsub] = getKeyStore(keyInput);\n keyUnsub = keyStore.subscribe((currentKeys) => {\n if (currentKeys) {\n const [newKey, keyParts] = currentKeys;\n fetcherStore.key = newKey;\n runFetcher([newKey, keyParts], fetcherStore, settings);\n prevKey = newKey;\n prevKeyParts = keyParts;\n } else {\n fetcherStore.key = prevKey = prevKeyParts = void 0;\n fetcherStore.set({ ...notLoading });\n }\n });\n const currentKeyValue = keyStore.get();\n if (currentKeyValue) {\n [prevKey, prevKeyParts] = currentKeyValue;\n if (firstRun)\n handleNewListener();\n }\n const {\n revalidateInterval = 0,\n revalidateOnFocus,\n revalidateOnReconnect\n } = settings;\n const runRefetcher = () => {\n if (prevKey)\n runFetcher([prevKey, prevKeyParts], fetcherStore, settings);\n };\n if (revalidateInterval > 0) {\n _revalidateOnInterval.set(\n keyInput,\n setInterval(runRefetcher, revalidateInterval)\n );\n }\n if (revalidateOnFocus)\n evtUnsubs.push(events.on(FOCUS, runRefetcher));\n if (revalidateOnReconnect)\n evtUnsubs.push(events.on(RECONNECT, runRefetcher));\n const cacheKeyChangeHandler = (keySelector) => {\n if (prevKey && testKeyAgainstSelector(prevKey, keySelector)) {\n runFetcher([prevKey, prevKeyParts], fetcherStore, settings);\n }\n };\n evtUnsubs.push(\n events.on(INVALIDATE_KEYS, cacheKeyChangeHandler),\n events.on(REVALIDATE_KEYS, cacheKeyChangeHandler),\n events.on(SET_CACHE, (keySelector, data, full) => {\n if (prevKey && testKeyAgainstSelector(prevKey, keySelector) && fetcherStore.value !== data && fetcherStore.value.data !== data) {\n fetcherStore.set(\n full ? data : { data, ...notLoading }\n );\n }\n })\n );\n });\n const handleNewListener = () => {\n if (prevKey && prevKeyParts)\n runFetcher([prevKey, prevKeyParts], fetcherStore, settings);\n };\n const originListen = fetcherStore.listen;\n fetcherStore.listen = (listener) => {\n const unsub = originListen(listener);\n listener(fetcherStore.value);\n handleNewListener();\n return unsub;\n };\n onStop(fetcherStore, () => {\n fetcherStore.value = { ...notLoading };\n keysInternalUnsub?.();\n evtUnsubs.forEach((fn) => fn());\n evtUnsubs = [];\n keyUnsub?.();\n clearInterval(_revalidateOnInterval.get(keyInput));\n });\n return fetcherStore;\n };\n const iterOverCache = (keySelector, cb) => {\n for (const key of cache.keys()) {\n if (testKeyAgainstSelector(key, keySelector))\n cb(key);\n }\n };\n const invalidateKeys = (keySelector) => {\n iterOverCache(keySelector, (key) => {\n cache.delete(key);\n });\n events.emit(INVALIDATE_KEYS, keySelector);\n };\n const revalidateKeys = (keySelector) => {\n iterOverCache(keySelector, (key) => {\n const cached = cache.get(key);\n if (cached) {\n cache.set(key, { ...cached, created: -Infinity });\n }\n });\n events.emit(REVALIDATE_KEYS, keySelector);\n };\n const mutateCache = (keySelector, data) => {\n iterOverCache(keySelector, (key) => {\n if (data === void 0)\n cache.delete(key);\n else {\n cache.set(key, {\n data,\n created: getNow(),\n expires: getNow() + (globalSettings.cacheLifetime ?? 8e3)\n });\n }\n });\n events.emit(SET_CACHE, keySelector, data);\n };\n function createMutatorStore(mutator, opts) {\n const { throttleCalls, onError } = opts ?? {\n throttleCalls: true,\n onError: globalSettings?.onError\n };\n const mutate = async (data) => {\n if (throttleCalls && store.value?.loading)\n return;\n const newMutator = rewrittenSettings.fetcher ?? mutator;\n const keysToInvalidate = [], keysToRevalidate = [];\n const safeKeySet = (k, v) => {\n if (store.lc) {\n store.setKey(k, v);\n }\n };\n try {\n store.set({\n error: void 0,\n data: void 0,\n mutate,\n ...loading\n });\n const result = await newMutator({\n data,\n invalidate: (key) => {\n keysToInvalidate.push(key);\n },\n revalidate: (key) => {\n keysToRevalidate.push(key);\n },\n getCacheUpdater: (key, shouldRevalidate = true) => [\n (newVal) => {\n mutateCache(key, newVal);\n if (shouldRevalidate) {\n keysToRevalidate.push(key);\n }\n },\n cache.get(key)?.data\n ]\n });\n safeKeySet(\"data\", result);\n return result;\n } catch (error) {\n onError?.(error);\n safeKeySet(\"error\", error);\n store.setKey(\"error\", error);\n } finally {\n safeKeySet(\"loading\", false);\n keysToInvalidate.forEach(invalidateKeys);\n keysToRevalidate.forEach(revalidateKeys);\n }\n };\n const store = map({\n mutate,\n ...notLoading\n });\n onStop(\n store,\n () => store.set({ mutate, ...notLoading })\n );\n store.mutate = mutate;\n return store;\n }\n const __unsafeOverruleSettings = (data) => {\n if (process.env.NODE_ENV !== \"test\") {\n console.warn(\n `You should only use __unsafeOverruleSettings in test environment`\n );\n }\n rewrittenSettings = data;\n };\n return [\n createFetcherStore,\n createMutatorStore,\n { __unsafeOverruleSettings, invalidateKeys, revalidateKeys, mutateCache }\n ];\n };\n function isSomeKey(key) {\n return typeof key === \"string\" || typeof key === \"number\" || key === true;\n }\n const getKeyStore = (keys) => {\n if (isSomeKey(keys))\n return [\n atom([\"\" + keys, [keys]]),\n () => {\n }\n ];\n const keyParts = [];\n const $key = atom(null);\n const keysAsStoresToIndexes = /* @__PURE__ */ new Map();\n const setKeyStoreValue = () => {\n if (keyParts.some((v) => v === null || v === void 0 || v === false)) {\n $key.set(null);\n } else {\n $key.set([keyParts.join(\"\"), keyParts]);\n }\n };\n for (let i = 0; i < keys.length; i++) {\n const keyOrStore = keys[i];\n if (isSomeKey(keyOrStore)) {\n keyParts.push(keyOrStore);\n } else {\n keyParts.push(null);\n keysAsStoresToIndexes.set(keyOrStore, i);\n }\n }\n const storesAsArray = [...keysAsStoresToIndexes.keys()];\n const $storeKeys = batched(storesAsArray, (...storeValues) => {\n for (let i = 0; i < storeValues.length; i++) {\n const store = storesAsArray[i], partIndex = keysAsStoresToIndexes.get(store);\n keyParts[partIndex] = store._ === fetcherSymbol ? store.value && \"data\" in store.value ? store.key : null : storeValues[i];\n }\n setKeyStoreValue();\n });\n setKeyStoreValue();\n return [$key, $storeKeys.subscribe(noop)];\n };\n function noop() {\n }\n const FOCUS = 1, RECONNECT = 2, INVALIDATE_KEYS = 3, REVALIDATE_KEYS = 4, SET_CACHE = 5;\n const testKeyAgainstSelector = (key, selector) => {\n if (Array.isArray(selector))\n return selector.includes(key);\n else if (typeof selector === \"function\")\n return selector(key);\n else\n return key === selector;\n };\n const getNow = () => (/* @__PURE__ */ new Date()).getTime();\n const fetcherSymbol = Symbol();\n const loading = { loading: true }, notLoading = { loading: false };\n return nanoquery;\n};\n\nconst subscribe = (name, fn) => {\n const isServer = typeof window === \"undefined\";\n if (!isServer) {\n addEventListener(name, fn);\n }\n};\nconst browserCompat = [\n () => !document.hidden,\n (cb) => subscribe(\"visibilitychange\", cb),\n (cb) => subscribe(\"online\", cb)\n];\n\nconst nanoquery = nanoqueryFactory(browserCompat);\n\nexport { nanoquery, defaultOnErrorRetry as onErrorRetry };\n","import { buildPath, extractPathParams } from \"../api/internal/path.js\";\nimport { createRequestInitWithBody, prepareClientRequestBody } from \"../api/request-body.js\";\nimport { getMountRoute } from \"../api/internal/route.js\";\nimport { RequestInputContext } from \"../api/request-input-context.js\";\nimport { RequestOutputContext } from \"../api/request-output-context.js\";\nimport { resolveRouteFactories } from \"../api/route.js\";\nimport { parseContentType } from \"../util/content-type.js\";\nimport { unwrapObject } from \"../util/nanostores.js\";\nimport { addStore } from \"../util/ssr.js\";\nimport { FragnoClientApiError, FragnoClientError, FragnoClientFetchAbortError, FragnoClientFetchError, FragnoClientFetchNetworkError, FragnoClientUnknownApiError } from \"./client-error.js\";\nimport { mergeFetcherConfigs } from \"./internal/fetcher-merge.js\";\nimport { handleNdjsonStreamingFirstItem } from \"./internal/ndjson-streaming.js\";\nimport { computed, task } from \"nanostores\";\nimport { nanoquery } from \"@nanostores/query\";\n//#region src/client/client.ts\n/**\n* Symbols used to identify hook types\n*/\nconst GET_HOOK_SYMBOL = Symbol(\"fragno-get-hook\");\nconst MUTATOR_HOOK_SYMBOL = Symbol(\"fragno-mutator-hook\");\nconst STORE_SYMBOL = Symbol(\"fragno-store\");\nasync function schemaAllowsUndefined(schema) {\n\ttry {\n\t\treturn !(await schema[\"~standard\"].validate(void 0)).issues;\n\t} catch {\n\t\treturn false;\n\t}\n}\nasync function assertBodyProvided(body, inputSchema, errorMessage) {\n\tif (typeof body !== \"undefined\" || inputSchema === void 0) return;\n\tif (await schemaAllowsUndefined(inputSchema)) return;\n\tthrow new Error(errorMessage);\n}\n/**\n* Merge request headers from multiple sources.\n* Returns undefined if there are no headers to merge.\n* @internal\n*/\nfunction mergeRequestHeaders(...headerSources) {\n\tconst result = {};\n\tlet hasHeaders = false;\n\tfor (const source of headerSources) {\n\t\tif (!source) continue;\n\t\tif (source instanceof Headers) for (const [key, value] of source.entries()) {\n\t\t\tresult[key] = value;\n\t\t\thasHeaders = true;\n\t\t}\n\t\telse if (Array.isArray(source)) for (const [key, value] of source) {\n\t\t\tresult[key] = value;\n\t\t\thasHeaders = true;\n\t\t}\n\t\telse for (const [key, value] of Object.entries(source)) {\n\t\t\tresult[key] = value;\n\t\t\thasHeaders = true;\n\t\t}\n\t}\n\treturn hasHeaders ? result : void 0;\n}\n/**\n* @internal\n*/\nfunction buildUrl(config, params) {\n\tconst { baseUrl = \"\", mountRoute, path } = config;\n\tconst { pathParams, queryParams } = params ?? {};\n\tconst normalizedPathParams = unwrapObject(pathParams);\n\tconst normalizedQueryParams = unwrapObject(queryParams) ?? {};\n\tconst filteredQueryParams = Object.fromEntries(Object.entries(normalizedQueryParams).filter(([_, value]) => value !== void 0));\n\tconst searchParams = new URLSearchParams(filteredQueryParams);\n\treturn `${baseUrl}${mountRoute}${buildPath(path, normalizedPathParams ?? {})}${searchParams.toString() ? `?${searchParams.toString()}` : \"\"}`;\n}\n/**\n* This method returns an array, which can be passed directly to nanostores.\n*\n* The returned array is always: path, pathParams (In order they appear in the path), queryParams (In alphabetical order)\n* Missing pathParams are replaced with \"<missing>\".\n* Atoms with undefined values are wrapped in computed atoms that map undefined to \"\" to avoid nanoquery treating the key as incomplete.\n* @param path\n* @param params\n* @returns\n* @internal\n*/\nfunction getCacheKey(method, path, params) {\n\tif (!params) return [method, path];\n\tconst { pathParams, queryParams } = params;\n\tconst pathParamValues = extractPathParams(path).map((name) => pathParams?.[name] ?? \"<missing>\");\n\tconst queryParamValues = queryParams ? Object.keys(queryParams).sort().map((key) => {\n\t\tconst value = queryParams[key];\n\t\tif (value && typeof value === \"object\" && \"get\" in value) return computed(value, (v) => v ?? \"\");\n\t\treturn value ?? \"\";\n\t}) : [];\n\treturn [\n\t\tmethod,\n\t\tpath,\n\t\t...pathParamValues,\n\t\t...queryParamValues\n\t];\n}\nfunction isStreamingResponse(response) {\n\tconst contentType = parseContentType(response.headers.get(\"content-type\"));\n\tif (!contentType) return false;\n\tif (!(response.headers.get(\"transfer-encoding\") === \"chunked\")) return false;\n\tif (contentType.subtype === \"octet-stream\") return \"octet-stream\";\n\tif (contentType.subtype === \"x-ndjson\") return \"ndjson\";\n\treturn false;\n}\n/**\n* @internal\n*/\nfunction isGetHook(hook) {\n\treturn typeof hook === \"object\" && hook !== null && GET_HOOK_SYMBOL in hook && hook[GET_HOOK_SYMBOL] === true;\n}\n/**\n* @internal\n*/\nfunction isMutatorHook(hook) {\n\treturn typeof hook === \"object\" && hook !== null && MUTATOR_HOOK_SYMBOL in hook && hook[MUTATOR_HOOK_SYMBOL] === true;\n}\n/**\n* @internal\n*/\nfunction isStore(obj) {\n\treturn typeof obj === \"object\" && obj !== null && STORE_SYMBOL in obj && obj[STORE_SYMBOL] === true;\n}\nvar ClientBuilder = class {\n\t#publicConfig;\n\t#fragmentConfig;\n\t#fetcherConfig;\n\t#cache = /* @__PURE__ */ new Map();\n\t#createFetcherStore;\n\t#createMutatorStore;\n\t#invalidateKeys;\n\tconstructor(publicConfig, fragmentConfig) {\n\t\tthis.#publicConfig = publicConfig;\n\t\tthis.#fragmentConfig = fragmentConfig;\n\t\tthis.#fetcherConfig = publicConfig.fetcherConfig;\n\t\tconst [createFetcherStore, createMutatorStore, { invalidateKeys }] = nanoquery({\n\t\t\tcache: this.#cache,\n\t\t\tcacheLifetime: Infinity\n\t\t});\n\t\tthis.#createFetcherStore = createFetcherStore;\n\t\tthis.#createMutatorStore = createMutatorStore;\n\t\tthis.#invalidateKeys = invalidateKeys;\n\t}\n\tget cacheEntries() {\n\t\treturn Object.fromEntries(this.#cache.entries());\n\t}\n\tcreateStore(input) {\n\t\tif (typeof input === \"function\") return {\n\t\t\tfactory: input,\n\t\t\t[STORE_SYMBOL]: true\n\t\t};\n\t\treturn {\n\t\t\tobj: input,\n\t\t\t[STORE_SYMBOL]: true\n\t\t};\n\t}\n\t/**\n\t* Build a URL for a custom backend call using the configured baseUrl and mountRoute.\n\t* Useful for fragment authors who need to make custom fetch calls.\n\t*/\n\tbuildUrl(path, params) {\n\t\treturn buildUrl({\n\t\t\tbaseUrl: this.#publicConfig.baseUrl ?? \"\",\n\t\t\tmountRoute: getMountRoute({\n\t\t\t\tname: this.#fragmentConfig.name,\n\t\t\t\tmountRoute: this.#publicConfig.mountRoute\n\t\t\t}),\n\t\t\tpath\n\t\t}, {\n\t\t\tpathParams: params?.path,\n\t\t\tqueryParams: params?.query\n\t\t});\n\t}\n\t/**\n\t* Get the configured fetcher function for custom backend calls.\n\t* Returns fetch with merged options applied.\n\t*/\n\tgetFetcher() {\n\t\treturn {\n\t\t\tfetcher: this.#getFetcher(),\n\t\t\tdefaultOptions: this.#getFetcherOptions()\n\t\t};\n\t}\n\t#getFetcher() {\n\t\tif (this.#fetcherConfig?.type === \"function\") return this.#fetcherConfig.fetcher;\n\t\treturn globalThis.fetch.bind(globalThis);\n\t}\n\t#getFetcherOptions() {\n\t\tif (this.#fetcherConfig?.type === \"options\") return this.#fetcherConfig.options;\n\t}\n\tcreateHook(path, options) {\n\t\tconst route = this.#fragmentConfig.routes.find((r) => r.path === path && r.method === \"GET\" && r.outputSchema !== void 0);\n\t\tif (!route) throw new Error(`Route '${path}' not found or is not a GET route with an output schema.`);\n\t\treturn this.#createRouteQueryHook(route, options);\n\t}\n\tcreateMutator(method, path, onInvalidate) {\n\t\tconst route = this.#fragmentConfig.routes.find((r) => r.method !== \"GET\" && r.path === path && r.method === method);\n\t\tif (!route) throw new Error(`Route '${path}' not found or is a GET route with an input and output schema.`);\n\t\treturn this.#createRouteQueryMutator(route, onInvalidate);\n\t}\n\t#createRouteQueryHook(route, options = {}) {\n\t\tif (route.method !== \"GET\") throw new Error(`Only GET routes are supported for hooks. Route '${route.path}' is a ${String(route.method)} route.`);\n\t\tif (!route.outputSchema) throw new Error(`Output schema is required for GET routes. Route '${route.path}' has no output schema.`);\n\t\tconst baseUrl = this.#publicConfig.baseUrl ?? \"\";\n\t\tconst mountRoute = getMountRoute({\n\t\t\tname: this.#fragmentConfig.name,\n\t\t\tmountRoute: this.#publicConfig.mountRoute\n\t\t});\n\t\tconst fetcher = this.#getFetcher();\n\t\tconst fetcherOptions = this.#getFetcherOptions();\n\t\tconst useFetcherOnServer = this.#fetcherConfig?.type === \"function\" && this.#fetcherConfig.useOnServer;\n\t\tasync function callServerSideHandler(params) {\n\t\t\tconst { pathParams, queryParams } = params ?? {};\n\t\t\tconst normalizedPathParams = unwrapObject(pathParams);\n\t\t\tconst normalizedQueryParams = unwrapObject(queryParams) ?? {};\n\t\t\tconst filteredQueryParams = Object.fromEntries(Object.entries(normalizedQueryParams).filter(([_, value]) => value !== void 0));\n\t\t\tconst searchParams = new URLSearchParams(filteredQueryParams);\n\t\t\treturn await route.handler(RequestInputContext.fromSSRContext({\n\t\t\t\tmethod: route.method,\n\t\t\t\tpath: route.path,\n\t\t\t\tpathParams: normalizedPathParams,\n\t\t\t\tsearchParams\n\t\t\t}), new RequestOutputContext(route.outputSchema));\n\t\t}\n\t\tasync function executeQuery(params) {\n\t\t\tconst { pathParams, queryParams } = params ?? {};\n\t\t\tif (typeof window === \"undefined\" && !useFetcherOnServer) return task(async () => callServerSideHandler({\n\t\t\t\tpathParams,\n\t\t\t\tqueryParams\n\t\t\t}));\n\t\t\tconst url = buildUrl({\n\t\t\t\tbaseUrl,\n\t\t\t\tmountRoute,\n\t\t\t\tpath: route.path\n\t\t\t}, {\n\t\t\t\tpathParams,\n\t\t\t\tqueryParams\n\t\t\t});\n\t\t\tlet response;\n\t\t\ttry {\n\t\t\t\tresponse = fetcherOptions ? await fetcher(url, fetcherOptions) : await fetcher(url);\n\t\t\t} catch (error) {\n\t\t\t\tthrow FragnoClientFetchError.fromUnknownFetchError(error);\n\t\t\t}\n\t\t\tif (!response.ok) throw await FragnoClientApiError.fromResponse(response);\n\t\t\treturn response;\n\t\t}\n\t\treturn {\n\t\t\troute,\n\t\t\tstore: (args) => {\n\t\t\t\tconst { path, query } = args ?? {};\n\t\t\t\tconst key = getCacheKey(route.method, route.path, {\n\t\t\t\t\tpathParams: path,\n\t\t\t\t\tqueryParams: query\n\t\t\t\t});\n\t\t\t\tconst store = this.#createFetcherStore(key, {\n\t\t\t\t\tfetcher: async () => {\n\t\t\t\t\t\tconst response = await executeQuery({\n\t\t\t\t\t\t\tpathParams: path,\n\t\t\t\t\t\t\tqueryParams: query\n\t\t\t\t\t\t});\n\t\t\t\t\t\tconst isStreaming = isStreamingResponse(response);\n\t\t\t\t\t\tif (!isStreaming) return response.json();\n\t\t\t\t\t\tif (typeof window === \"undefined\" && !useFetcherOnServer) {\n\t\t\t\t\t\t\tif (response.body) await response.body.getReader().cancel();\n\t\t\t\t\t\t\treturn [];\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (isStreaming === \"ndjson\") {\n\t\t\t\t\t\t\tconst { items } = await handleNdjsonStreamingFirstItem(response, {\n\t\t\t\t\t\t\t\tsetData: (value) => {\n\t\t\t\t\t\t\t\t\tstore.mutate(value);\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\tsetError: (value) => {\n\t\t\t\t\t\t\t\t\tstore.set({\n\t\t\t\t\t\t\t\t\t\t...store.get(),\n\t\t\t\t\t\t\t\t\t\terror: value\n\t\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\treturn items;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (isStreaming === \"octet-stream\") throw new Error(\"Octet-stream streaming is not supported.\");\n\t\t\t\t\t\tthrow new Error(\"Unreachable\");\n\t\t\t\t\t},\n\t\t\t\t\tonErrorRetry: options?.onErrorRetry,\n\t\t\t\t\tdedupeTime: Infinity\n\t\t\t\t});\n\t\t\t\tif (typeof window === \"undefined\") addStore(store);\n\t\t\t\treturn store;\n\t\t\t},\n\t\t\tquery: async (args) => {\n\t\t\t\tconst { path, query } = args ?? {};\n\t\t\t\tconst response = await executeQuery({\n\t\t\t\t\tpathParams: path,\n\t\t\t\t\tqueryParams: query\n\t\t\t\t});\n\t\t\t\tconst isStreaming = isStreamingResponse(response);\n\t\t\t\tif (!isStreaming) return await response.json();\n\t\t\t\tif (isStreaming === \"ndjson\") {\n\t\t\t\t\tconst { streamingPromise } = await handleNdjsonStreamingFirstItem(response);\n\t\t\t\t\treturn await streamingPromise;\n\t\t\t\t}\n\t\t\t\tif (isStreaming === \"octet-stream\") throw new Error(\"Octet-stream streaming is not supported.\");\n\t\t\t\tthrow new Error(\"Unreachable\");\n\t\t\t},\n\t\t\t[GET_HOOK_SYMBOL]: true\n\t\t};\n\t}\n\t#createRouteQueryMutator(route, onInvalidate = (invalidate, params) => {\n\t\tinvalidate(\"GET\", route.path, params);\n\t}) {\n\t\tconst method = route.method;\n\t\tconst baseUrl = this.#publicConfig.baseUrl ?? \"\";\n\t\tconst mountRoute = getMountRoute({\n\t\t\tname: this.#fragmentConfig.name,\n\t\t\tmountRoute: this.#publicConfig.mountRoute\n\t\t});\n\t\tconst fetcher = this.#getFetcher();\n\t\tconst fetcherOptions = this.#getFetcherOptions();\n\t\tconst useFetcherOnServer = this.#fetcherConfig?.type === \"function\" && this.#fetcherConfig.useOnServer;\n\t\tasync function executeMutateQuery({ body, path, query }) {\n\t\t\tif (typeof window === \"undefined\" && !useFetcherOnServer) return task(async () => route.handler(RequestInputContext.fromSSRContext({\n\t\t\t\tinputSchema: route.inputSchema,\n\t\t\t\tmethod,\n\t\t\t\tpath: route.path,\n\t\t\t\tpathParams: path ?? {},\n\t\t\t\tsearchParams: new URLSearchParams(query),\n\t\t\t\tbody\n\t\t\t}), new RequestOutputContext(route.outputSchema)));\n\t\t\tconst url = buildUrl({\n\t\t\t\tbaseUrl,\n\t\t\t\tmountRoute,\n\t\t\t\tpath: route.path\n\t\t\t}, {\n\t\t\t\tpathParams: path,\n\t\t\t\tqueryParams: query\n\t\t\t});\n\t\t\tlet response;\n\t\t\ttry {\n\t\t\t\tconst preparedBody = prepareClientRequestBody(body, route.contentType);\n\t\t\t\tconst bodyHeaders = preparedBody.contentType === void 0 || preparedBody.contentType === null ? void 0 : { \"Content-Type\": preparedBody.contentType };\n\t\t\t\tconst headers = mergeRequestHeaders(fetcherOptions?.headers, bodyHeaders);\n\t\t\t\tconst requestOptions = createRequestInitWithBody(method, headers ?? {}, preparedBody.body, fetcherOptions);\n\t\t\t\tresponse = await fetcher(url, requestOptions);\n\t\t\t} catch (error) {\n\t\t\t\tthrow FragnoClientFetchError.fromUnknownFetchError(error);\n\t\t\t}\n\t\t\tif (!response.ok) throw await FragnoClientApiError.fromResponse(response);\n\t\t\treturn response;\n\t\t}\n\t\tconst mutatorStore = this.#createMutatorStore(async ({ data }) => {\n\t\t\tif (typeof window === \"undefined\") {}\n\t\t\tconst { body, path, query } = data;\n\t\t\tawait assertBodyProvided(body, route.inputSchema, \"Body is required.\");\n\t\t\tconst response = await executeMutateQuery({\n\t\t\t\tbody,\n\t\t\t\tpath,\n\t\t\t\tquery\n\t\t\t});\n\t\t\tonInvalidate(this.#invalidate.bind(this), {\n\t\t\t\tpathParams: path ?? {},\n\t\t\t\tqueryParams: query\n\t\t\t});\n\t\t\tif (response.status === 201 || response.status === 204) return;\n\t\t\tconst isStreaming = isStreamingResponse(response);\n\t\t\tif (!isStreaming) return await response.json();\n\t\t\tif (typeof window === \"undefined\") return [];\n\t\t\tif (isStreaming === \"ndjson\") {\n\t\t\t\tconst { firstItem } = await handleNdjsonStreamingFirstItem(response, {\n\t\t\t\t\tsetData: (value) => {\n\t\t\t\t\t\tmutatorStore.set({\n\t\t\t\t\t\t\t...mutatorStore.get(),\n\t\t\t\t\t\t\tloading: !(Array.isArray(value) && value.length > 0),\n\t\t\t\t\t\t\tdata: value\n\t\t\t\t\t\t});\n\t\t\t\t\t},\n\t\t\t\t\tsetError: (value) => {\n\t\t\t\t\t\tmutatorStore.set({\n\t\t\t\t\t\t\t...mutatorStore.get(),\n\t\t\t\t\t\t\terror: value\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t\treturn [firstItem];\n\t\t\t}\n\t\t\tif (isStreaming === \"octet-stream\") throw new Error(\"Octet-stream streaming is not supported.\");\n\t\t\tthrow new Error(\"Unreachable\");\n\t\t}, { onError: (error) => {\n\t\t\tconsole.error(\"Error in mutatorStore\", error);\n\t\t} });\n\t\tconst mutateQuery = (async (data) => {\n\t\t\tconst { body, path, query } = data;\n\t\t\tawait assertBodyProvided(body, route.inputSchema, \"Body is required for mutateQuery\");\n\t\t\tconst response = await executeMutateQuery({\n\t\t\t\tbody,\n\t\t\t\tpath,\n\t\t\t\tquery\n\t\t\t});\n\t\t\tif (response.status === 201 || response.status === 204) return;\n\t\t\tconst isStreaming = isStreamingResponse(response);\n\t\t\tif (!isStreaming) return await response.json();\n\t\t\tif (isStreaming === \"ndjson\") {\n\t\t\t\tconst { streamingPromise } = await handleNdjsonStreamingFirstItem(response);\n\t\t\t\treturn await streamingPromise;\n\t\t\t}\n\t\t\tif (isStreaming === \"octet-stream\") throw new Error(\"Octet-stream streaming is not supported for mutations\");\n\t\t\tthrow new Error(\"Unreachable\");\n\t\t});\n\t\treturn {\n\t\t\troute,\n\t\t\tmutateQuery,\n\t\t\tmutatorStore,\n\t\t\t[MUTATOR_HOOK_SYMBOL]: true\n\t\t};\n\t}\n\t#invalidate(method, path, params) {\n\t\tconst prefix = getCacheKey(method, path, {\n\t\t\tpathParams: params?.pathParams,\n\t\t\tqueryParams: params?.queryParams\n\t\t}).map((k) => typeof k === \"string\" ? k : k.get()).join(\"\");\n\t\tthis.#invalidateKeys((key) => key.startsWith(prefix));\n\t}\n};\n/**\n* Create a client builder for fragments using the new fragment definition API.\n* This is the same as createClientBuilder but works with FragmentDefinition.\n*/\nfunction createClientBuilder(definition, publicConfig, routesOrFactories, authorFetcherConfig) {\n\tconst routes = resolveRouteFactories({\n\t\tconfig: {},\n\t\tdeps: {},\n\t\tservices: {},\n\t\tserviceDeps: {}\n\t}, routesOrFactories);\n\tconst fragmentConfig = {\n\t\tname: definition.name,\n\t\troutes\n\t};\n\tconst mountRoute = getMountRoute({\n\t\tname: definition.name,\n\t\tmountRoute: publicConfig.mountRoute\n\t});\n\tconst mergedFetcherConfig = mergeFetcherConfigs(authorFetcherConfig, publicConfig.fetcherConfig);\n\treturn new ClientBuilder({\n\t\t...publicConfig,\n\t\tmountRoute,\n\t\tfetcherConfig: mergedFetcherConfig\n\t}, fragmentConfig);\n}\n//#endregion\nexport { ClientBuilder, FragnoClientApiError, FragnoClientError, FragnoClientFetchAbortError, FragnoClientFetchError, FragnoClientFetchNetworkError, FragnoClientUnknownApiError, buildUrl, createClientBuilder, getCacheKey, isGetHook, isMutatorHook, isStore };\n\n//# sourceMappingURL=client.js.map","import { defineFragment } from \"@fragno-dev/core\";\nimport { withDatabase, type HookContext, type HookFn } from \"@fragno-dev/db\";\n\nimport { listMcpTools } from \"./mcp-api\";\nimport type { McpFragmentConfig as BaseMcpFragmentConfig } from \"./mcp-types\";\nimport { mcpSchema } from \"./schema\";\nimport {\n createOAuthCallbackSnapshot,\n createMcpOperationAuth,\n createOAuthStartSnapshot,\n isMcpAuthReady,\n mcpAuthReadyUntil,\n resolveMcpOperationAuth,\n resolveOAuthStartClient,\n withoutOAuthConsent,\n writeOAuthSecretChanges,\n type AuthPersistenceChanges,\n} from \"./services\";\n\nexport interface McpServerConfigurationChangedPayload {\n serverId: string;\n current: {\n tools: unknown[];\n };\n}\n\nexport interface McpServerConfigurationDeletedPayload {\n serverId: string;\n}\n\nexport interface McpServerReadinessChangedPayload {\n serverId: string;\n /** Whether stored auth can authorize operations; the server can still reject it. */\n ready: boolean;\n}\n\n/**\n * A readiness change as delivered. The server is read at delivery rather than stored with the hook,\n * so it is the current configuration, or null after it was deleted.\n */\nexport interface McpServerReadinessChange extends McpServerReadinessChangedPayload {\n server: { name: string | null; authMode: string } | null;\n}\n\nexport interface McpFragmentHooksConfig {\n onServerConfigurationChanged?: (\n payload: McpServerConfigurationChangedPayload,\n context: HookContext,\n ) => Promise<void> | void;\n onServerConfigurationDeleted?: (\n payload: McpServerConfigurationDeletedPayload,\n context: HookContext,\n ) => Promise<void> | void;\n /**\n * Fires when a write changes whether the server can authorize operations, including creation\n * and credentials the server invalidates during an operation, and when an access token without a\n * refresh token expires.\n */\n onServerReadinessChanged?: (\n payload: McpServerReadinessChange,\n context: HookContext,\n ) => Promise<void> | void;\n}\n\nexport type McpFragmentConfig = BaseMcpFragmentConfig & McpFragmentHooksConfig;\n\nexport interface McpInternalRefreshServerConfigurationPayload {\n serverId: string;\n}\n\n/** The token stored when expiry was scheduled; a later write replaces it and reports itself. */\ninterface McpInternalAuthExpiredPayload {\n serverId: string;\n expiresAt: string;\n}\n\nexport type McpHooksMap = {\n internalRefreshServerConfiguration: HookFn<McpInternalRefreshServerConfigurationPayload>;\n internalAuthExpired: HookFn<McpInternalAuthExpiredPayload>;\n onServerConfigurationChanged: HookFn<McpServerConfigurationChangedPayload>;\n onServerConfigurationDeleted: HookFn<McpServerConfigurationDeletedPayload>;\n onServerReadinessChanged: HookFn<McpServerReadinessChangedPayload>;\n};\n\n/** Expiry changes readiness without a write, so a write that starts the countdown schedules it. */\nexport function scheduleMcpAuthExpiry(\n // Hook transactions type their hooks loosely, so only the one trigger is required.\n uow: {\n triggerHook(\n name: \"internalAuthExpired\",\n payload: McpInternalAuthExpiredPayload,\n options: { processAt: Date },\n ): void;\n },\n serverId: string,\n authMode: string,\n after: { payload: string; expiresAt: Date | null },\n) {\n const readyUntil = mcpAuthReadyUntil(authMode, after);\n if (readyUntil && after.expiresAt && isMcpAuthReady(authMode, after)) {\n uow.triggerHook(\n \"internalAuthExpired\",\n { serverId, expiresAt: after.expiresAt.toISOString() },\n { processAt: readyUntil },\n );\n }\n}\n\nexport const mcpFragmentDefinition = defineFragment<McpFragmentConfig>(\"mcp-fragment\")\n .extend(withDatabase(mcpSchema))\n .provideHooks<McpHooksMap>(({ defineHook, config }) => ({\n internalRefreshServerConfiguration: defineHook(async function ({ serverId }) {\n let prepared:\n | {\n authChanges?: AuthPersistenceChanges;\n toolsOperation: Awaited<ReturnType<typeof listMcpTools<AuthPersistenceChanges>>>;\n }\n | undefined;\n\n const result = await this.handlerTx()\n .retrieve(({ forSchema }) =>\n forSchema(mcpSchema)\n .findFirst(\"server_configuration\", (b) =>\n b.whereIndex(\"primary\", (eb) => eb(\"id\", \"=\", serverId)),\n )\n .find(\"secret\", (b) =>\n b.whereIndex(\"idx_secret_server_kind\", (eb) => eb(\"serverId\", \"=\", serverId)),\n )\n .findFirst(\"server_connection_cache\", (b) =>\n b.whereIndex(\"primary\", (eb) => eb(\"id\", \"=\", serverId)),\n ),\n )\n .afterRetrieve(async (_uow, [server, secrets]) => {\n if (!server) {\n return;\n }\n\n const auth = await createMcpOperationAuth({ config, server, secrets });\n const toolsOperation = await listMcpTools({\n endpointUrl: server.endpointUrl,\n auth,\n fetchImplementation: config.fetch,\n });\n prepared = {\n authChanges: toolsOperation.authChanges,\n toolsOperation,\n };\n })\n .mutate(({ forSchema, retrieveResult: [server, secrets, cache] }) => {\n if (!server) {\n return { action: \"missing\" as const };\n }\n if (!prepared) {\n throw new Error(\"MCP refresh operation was not prepared\");\n }\n\n const preparedOperation = prepared;\n const uow = forSchema(mcpSchema);\n const upsertSecret = (kind: string, payload: string, expiresAt: Date | null) => {\n const existing = secrets.find((secret) => secret.kind === kind);\n if (existing) {\n uow.update(\"secret\", existing.id, (b) =>\n b.set({ payload, expiresAt, updatedAt: b.now() }).check(),\n );\n return;\n }\n uow.create(\"secret\", {\n id: `${serverId}:${kind}`,\n serverId,\n kind,\n payload,\n expiresAt,\n });\n };\n\n if (preparedOperation.authChanges) {\n writeOAuthSecretChanges(preparedOperation.authChanges, {\n upsert: (kind, payload) => {\n upsertSecret(kind, payload, null);\n },\n remove: (kind) => {\n const existing = secrets.find((secret) => secret.kind === kind);\n if (existing) {\n uow.delete(\"secret\", existing.id);\n }\n },\n });\n }\n if (preparedOperation.authChanges?.authPayload) {\n const after = {\n payload: preparedOperation.authChanges.authPayload,\n expiresAt: preparedOperation.authChanges.authExpiresAt ?? null,\n };\n upsertSecret(\"auth\", after.payload, after.expiresAt);\n const before = secrets.find((secret) => secret.kind === \"auth\");\n const ready = isMcpAuthReady(server.authMode, after);\n if (ready !== isMcpAuthReady(server.authMode, before)) {\n uow.triggerHook(\"onServerReadinessChanged\", { serverId, ready });\n }\n scheduleMcpAuthExpiry(uow, serverId, server.authMode, after);\n }\n\n if (!preparedOperation.toolsOperation.ok) {\n return {\n action: \"error\" as const,\n error:\n preparedOperation.toolsOperation.error instanceof Error\n ? preparedOperation.toolsOperation.error.message\n : String(preparedOperation.toolsOperation.error),\n };\n }\n\n const tools = Array.isArray(preparedOperation.toolsOperation.result.tools)\n ? preparedOperation.toolsOperation.result.tools\n : [];\n const previousTools = Array.isArray(cache?.tools) ? cache.tools : null;\n if (!previousTools || JSON.stringify(previousTools) !== JSON.stringify(tools)) {\n uow.triggerHook(\"onServerConfigurationChanged\", {\n serverId,\n current: { tools },\n });\n }\n\n uow.delete(\"server_connection_cache\", serverId);\n uow.create(\"server_connection_cache\", {\n id: serverId,\n serverId,\n protocolVersion: preparedOperation.toolsOperation.metadata.protocolVersion ?? null,\n serverInfo: preparedOperation.toolsOperation.metadata.serverInfo ?? null,\n capabilities: preparedOperation.toolsOperation.metadata.capabilities ?? null,\n tools,\n });\n\n return { action: \"refreshed\" as const };\n })\n .execute();\n\n if (result.action === \"error\") {\n throw new Error(result.error);\n }\n }),\n onServerConfigurationChanged: defineHook(async function (payload) {\n await config.onServerConfigurationChanged?.(payload, this);\n }),\n onServerConfigurationDeleted: defineHook(async function (payload) {\n await config.onServerConfigurationDeleted?.(payload, this);\n }),\n onServerReadinessChanged: defineHook(async function (payload) {\n const server = await this.handlerTx()\n .retrieve(({ forSchema }) =>\n forSchema(mcpSchema).findFirst(\"server_configuration\", (b) =>\n b.whereIndex(\"primary\", (eb) => eb(\"id\", \"=\", payload.serverId)),\n ),\n )\n .transformRetrieve(([server]) => server && { name: server.name, authMode: server.authMode })\n .execute();\n await config.onServerReadinessChanged?.({ ...payload, server }, this);\n }),\n internalAuthExpired: defineHook(async function ({ serverId, expiresAt }) {\n const expired = await this.handlerTx()\n .retrieve(({ forSchema }) =>\n forSchema(mcpSchema)\n .findFirst(\"server_configuration\", (b) =>\n b.whereIndex(\"primary\", (eb) => eb(\"id\", \"=\", serverId)),\n )\n .findFirst(\"secret\", (b) =>\n b.whereIndex(\"idx_secret_server_kind\", (eb) =>\n eb.and(eb(\"serverId\", \"=\", serverId), eb(\"kind\", \"=\", \"auth\")),\n ),\n ),\n )\n .transformRetrieve(([server, secret]) =>\n // The hook's due time already passed, so an untouched token is the expired one.\n server !== null &&\n secret !== null &&\n secret.expiresAt?.getTime() === Date.parse(expiresAt) &&\n mcpAuthReadyUntil(server.authMode, secret) !== null\n ? { name: server.name, authMode: server.authMode }\n : null,\n )\n .execute();\n if (expired) {\n await config.onServerReadinessChanged?.({ serverId, ready: false, server: expired }, this);\n }\n }),\n }))\n .providesBaseService(({ defineService, config }) =>\n defineService({\n startOAuth: function (input: {\n serverId: string;\n stateId: string;\n redirectUri: string;\n scope?: string;\n clientId?: string;\n clientSecret?: string;\n discardTokens: boolean;\n }) {\n return this.serviceTx(mcpSchema)\n .retrieve((uow) =>\n uow\n .findFirst(\"server_configuration\", (b) =>\n b.whereIndex(\"primary\", (eb) => eb(\"id\", \"=\", input.serverId)),\n )\n .find(\"secret\", (b) =>\n b.whereIndex(\"idx_secret_server_kind\", (eb) => eb(\"serverId\", \"=\", input.serverId)),\n ),\n )\n .transformRetrieve(async ([server, secrets]) => {\n if (!server) {\n return { found: false as const };\n }\n const { authorizationUrl, changes } = await createOAuthStartSnapshot({\n config,\n server,\n secrets,\n stateId: input.stateId,\n redirectUri: input.redirectUri,\n ...resolveOAuthStartClient(secrets, input),\n });\n return {\n found: true as const,\n authorizationUrl: authorizationUrl.toString(),\n serverId: server.id.toString(),\n authMode: server.authMode,\n secrets,\n changes,\n };\n })\n .mutate(({ uow, retrieveResult }) => {\n if (!retrieveResult.found) {\n return { found: false as const };\n }\n\n const upsertSecret = (kind: string, payload: string, expiresAt: Date | null) => {\n const existing = retrieveResult.secrets.find((secret) => secret.kind === kind);\n if (existing) {\n uow.update(\"secret\", existing.id, (b) =>\n b.set({ payload, expiresAt, updatedAt: b.now() }).check(),\n );\n return;\n }\n uow.create(\"secret\", {\n id: `${retrieveResult.serverId}:${kind}`,\n serverId: retrieveResult.serverId,\n kind,\n payload,\n expiresAt,\n });\n };\n\n writeOAuthSecretChanges(retrieveResult.changes, {\n upsert: (kind, payload) => {\n upsertSecret(kind, payload, null);\n },\n remove: (kind) => {\n const existing = retrieveResult.secrets.find((secret) => secret.kind === kind);\n if (existing) {\n uow.delete(\"secret\", existing.id);\n }\n },\n });\n const authSecret = retrieveResult.secrets.find((secret) => secret.kind === \"auth\");\n if (input.discardTokens && authSecret) {\n // Keeps the client configuration; only consent evidence is discarded.\n const payload = withoutOAuthConsent(authSecret.payload);\n uow.update(\"secret\", authSecret.id, (b) =>\n b.set({ payload, expiresAt: null, updatedAt: b.now() }).check(),\n );\n const { authMode } = retrieveResult;\n const ready = isMcpAuthReady(authMode, { payload, expiresAt: null });\n if (ready !== isMcpAuthReady(authMode, authSecret)) {\n uow.triggerHook(\"onServerReadinessChanged\", {\n serverId: retrieveResult.serverId,\n ready,\n });\n }\n }\n if (retrieveResult.changes.oauthState) {\n uow.create(\"oauthState\", {\n id: retrieveResult.changes.oauthState.id,\n serverId: retrieveResult.serverId,\n // The link carries only the PKCE challenge, never the verifier.\n authorizationUrl: retrieveResult.authorizationUrl,\n codeVerifier: retrieveResult.changes.oauthState.codeVerifier,\n redirectUri: retrieveResult.changes.oauthState.redirectUri,\n scope: retrieveResult.changes.oauthState.scope ?? null,\n expiresAt: retrieveResult.changes.oauthState.expiresAt,\n consumedAt: null,\n });\n }\n\n return { found: true as const };\n })\n .transform(({ retrieveResult }) =>\n retrieveResult.found\n ? {\n found: true as const,\n authorizationUrl: retrieveResult.authorizationUrl,\n }\n : { found: false as const },\n )\n .build();\n },\n\n completeOAuthCallback: function (input: {\n stateId: string;\n code: string;\n iss: string | undefined;\n }) {\n const serverId = input.stateId.split(\":\")[0] ?? \"\";\n return this.serviceTx(mcpSchema)\n .retrieve((uow) =>\n uow\n .findFirst(\"oauthState\", (b) =>\n b.whereIndex(\"primary\", (eb) => eb(\"id\", \"=\", input.stateId)),\n )\n .findFirst(\"server_configuration\", (b) =>\n b.whereIndex(\"primary\", (eb) => eb(\"id\", \"=\", serverId)),\n )\n .find(\"secret\", (b) =>\n b.whereIndex(\"idx_secret_server_kind\", (eb) => eb(\"serverId\", \"=\", serverId)),\n )\n .findFirst(\"server_connection_cache\", (b) =>\n b.whereIndex(\"primary\", (eb) => eb(\"id\", \"=\", serverId)),\n ),\n )\n .transformRetrieve(async ([state, server, secrets, cache]) => {\n if (!state || state.consumedAt || new Date(state.expiresAt).getTime() < Date.now()) {\n return { found: false as const, reason: \"invalid_state\" as const };\n }\n if (!server) {\n return { found: false as const, reason: \"server_not_found\" as const };\n }\n const changes = await createOAuthCallbackSnapshot({\n config,\n stateId: input.stateId,\n code: input.code,\n iss: input.iss,\n state,\n server,\n secrets,\n });\n return { found: true as const, server, secrets, cache, changes };\n })\n .mutate(({ uow, retrieveResult }) => {\n if (!retrieveResult.found) {\n return { found: false as const, reason: retrieveResult.reason };\n }\n\n const serverId = retrieveResult.server.id.toString();\n const upsertSecret = (kind: string, payload: string, expiresAt: Date | null) => {\n const existing = retrieveResult.secrets.find((secret) => secret.kind === kind);\n if (existing) {\n uow.update(\"secret\", existing.id, (b) =>\n b.set({ payload, expiresAt, updatedAt: b.now() }).check(),\n );\n return;\n }\n uow.create(\"secret\", {\n id: `${serverId}:${kind}`,\n serverId,\n kind,\n payload,\n expiresAt,\n });\n };\n\n writeOAuthSecretChanges(retrieveResult.changes, {\n upsert: (kind, payload) => {\n upsertSecret(kind, payload, null);\n },\n remove: (kind) => {\n const existing = retrieveResult.secrets.find((secret) => secret.kind === kind);\n if (existing) {\n uow.delete(\"secret\", existing.id);\n }\n },\n });\n if (retrieveResult.changes.authPayload) {\n const after = {\n payload: retrieveResult.changes.authPayload,\n expiresAt: retrieveResult.changes.authExpiresAt ?? null,\n };\n upsertSecret(\"auth\", after.payload, after.expiresAt);\n uow.update(\"server_configuration\", retrieveResult.server.id, (b) =>\n b.set({ authMode: \"oauth\", updatedAt: b.now() }).check(),\n );\n const before = retrieveResult.secrets.find((secret) => secret.kind === \"auth\");\n const ready = isMcpAuthReady(\"oauth\", after);\n if (ready !== isMcpAuthReady(retrieveResult.server.authMode, before)) {\n uow.triggerHook(\"onServerReadinessChanged\", { serverId, ready });\n }\n scheduleMcpAuthExpiry(uow, serverId, \"oauth\", after);\n if (retrieveResult.cache) {\n uow.delete(\"server_connection_cache\", retrieveResult.cache.id);\n }\n uow.triggerHook(\"internalRefreshServerConfiguration\", { serverId });\n }\n if (retrieveResult.changes.consumeStateId) {\n uow.update(\"oauthState\", input.stateId, (b) => b.set({ consumedAt: b.now() }));\n }\n\n return { found: true as const };\n })\n .transform(({ retrieveResult }) =>\n retrieveResult.found\n ? { found: true as const }\n : { found: false as const, reason: retrieveResult.reason },\n )\n .build();\n },\n\n prepareMcpOperation: function (input: { serverId: string }) {\n return this.serviceTx(mcpSchema)\n .retrieve((uow) =>\n uow\n .findFirst(\"server_configuration\", (b) =>\n b.whereIndex(\"primary\", (eb) => eb(\"id\", \"=\", input.serverId)),\n )\n .find(\"secret\", (b) =>\n b.whereIndex(\"idx_secret_server_kind\", (eb) => eb(\"serverId\", \"=\", input.serverId)),\n )\n .findFirst(\"server_connection_cache\", (b) =>\n b.whereIndex(\"primary\", (eb) => eb(\"id\", \"=\", input.serverId)),\n ),\n )\n .transformRetrieve(async ([server, secrets, cache]) => {\n if (!server) {\n return { found: false as const };\n }\n const resolved = await resolveMcpOperationAuth({ config, server, secrets });\n return {\n found: true as const,\n serverId: server.id.toString(),\n endpointUrl: server.endpointUrl,\n token: resolved.token,\n secrets,\n cache,\n authChanges: resolved.authChanges,\n };\n })\n .transform(({ retrieveResult }) =>\n retrieveResult.found\n ? {\n found: true as const,\n endpointUrl: retrieveResult.endpointUrl,\n token: retrieveResult.token,\n secrets: retrieveResult.secrets,\n cache: retrieveResult.cache,\n authChanges: retrieveResult.authChanges,\n serverId: retrieveResult.serverId,\n }\n : { found: false as const },\n )\n .build();\n },\n }),\n )\n .build();\n","import { z } from \"zod\";\n\nexport interface McpFragmentConfig {\n /** Optional URL allow-list. If omitted, only http(s) URL syntax is checked. */\n allowedEndpointUrls?: (url: URL) => boolean;\n /** Allows OAuth callback URLs. OAuth start is rejected when this policy is omitted. */\n allowedOAuthRedirectUris?: (url: URL) => boolean;\n /** Optional fetch implementation for tests/custom runtimes. */\n fetch?: typeof fetch;\n}\n\nexport const authConfigSchema = z.discriminatedUnion(\"type\", [\n z.object({ type: z.literal(\"none\") }),\n z.object({ type: z.literal(\"bearer\"), token: z.string().min(1) }),\n z.object({\n type: z.literal(\"client_credentials\"),\n clientId: z.string().min(1),\n clientSecret: z.string().min(1),\n scopes: z.array(z.string()).optional(),\n }),\n z.object({\n type: z.literal(\"oauth\"),\n clientId: z.string().optional(),\n clientSecret: z.string().optional(),\n scopes: z.array(z.string()).optional(),\n }),\n]);\n\n/** Slugs are URL path segments and OAuth state prefixes, so they exclude ':' and dot segments. */\nexport const mcpServerSlugSchema = z.string().regex(/^[a-z0-9][a-z0-9-]*$/);\n\nexport const createServerInputSchema = z.object({\n slug: mcpServerSlugSchema,\n name: z.string().optional(),\n endpointUrl: z.url(),\n auth: authConfigSchema.default({ type: \"none\" }),\n});\n\n/** Replacement keeps the slug and creation time; everything else is supplied again. */\nexport const replaceServerConfigurationInputSchema = createServerInputSchema.omit({ slug: true });\n\nexport const toolCallInputSchema = z.object({\n name: z.string().min(1),\n arguments: z.record(z.string(), z.unknown()).optional(),\n timeoutMs: z.number().int().positive().max(120_000).optional(),\n});\n\nexport const tokenAuthInputSchema = z.object({\n token: z.string().min(1),\n});\n\nconst mcpCredentialsPresenceSchema = z\n .enum([\"present\", \"missing\"])\n .describe(\"Whether credentials are stored, not whether the server accepts them.\");\n\n/** Sanitized auth state derived from stored configuration; it never proves live server access. */\nexport const mcpAuthStatusSchema = z.discriminatedUnion(\"mode\", [\n z.object({ mode: z.literal(\"none\") }),\n z.object({ mode: z.literal(\"bearer\"), credentials: mcpCredentialsPresenceSchema }),\n z.object({ mode: z.literal(\"client_credentials\"), credentials: mcpCredentialsPresenceSchema }),\n z.object({\n mode: z.literal(\"oauth\"),\n state: z\n .enum([\"consent-required\", \"consent-pending\", \"authorized\", \"expired\"])\n .describe(\n \"Expired means the access token expired and no refresh token is stored; pending means an unexpired authorization link exists.\",\n ),\n }),\n]);\n\n/** The pending link embeds the callback state, so reading it needs server-create authority. */\nexport const mcpOAuthPendingSchema = z.object({\n pending: z\n .object({ authorizationUrl: z.url(), expiresAt: z.union([z.string(), z.date()]) })\n .nullable()\n .describe(\n \"The newest unexpired, unconsumed authorization link; any pending link completes it.\",\n ),\n});\n\n/**\n * Mirrors the MCP protocol's tool definition, whose optional members are defined by the spec.\n * Loose so newer spec members, such as icons, survive the cache and the API.\n */\nexport const mcpToolSchema = z.looseObject({\n name: z.string(),\n title: z.string().optional(),\n description: z.string().optional(),\n inputSchema: z.record(z.string(), z.unknown()),\n outputSchema: z.record(z.string(), z.unknown()).optional(),\n annotations: z.record(z.string(), z.unknown()).optional(),\n _meta: z.record(z.string(), z.unknown()).optional(),\n});\n\n/** Tool errors are results the model should see; transport and auth failures are route errors. */\nexport const mcpToolCallResultSchema = z.object({\n isError: z.boolean(),\n content: z.array(z.record(z.string(), z.unknown())),\n /** Since MCP 2026-07-28 any JSON value; null when the tool returned none. */\n structuredContent: z.unknown(),\n});\n\n/** Query parameter carrying the OAuth callback URI for an MCP OAuth start request. */\nexport const MCP_OAUTH_REDIRECT_URI_QUERY_PARAMETER = \"redirectUri\";\n\nexport const mcpOAuthRedirectUriSchema = z.url().refine((value) => {\n const protocol = new URL(value).protocol;\n return protocol === \"https:\" || protocol === \"http:\";\n}, \"MCP OAuth redirect URI must use HTTP or HTTPS\");\n\nexport const oauthStartInputSchema = z.object({\n scope: z.string().optional(),\n clientId: z.string().min(1).optional(),\n clientSecret: z.string().min(1).optional(),\n discardTokens: z\n .boolean()\n .default(false)\n .describe(\n \"Drop stored tokens so status reports pending consent until this new consent completes.\",\n ),\n});\n\nexport type AuthConfig = z.infer<typeof authConfigSchema>;\nexport type CreateServerInput = z.infer<typeof createServerInputSchema>;\nexport type ToolCallInput = z.infer<typeof toolCallInputSchema>;\nexport type McpAuthStatus = z.infer<typeof mcpAuthStatusSchema>;\nexport type McpOAuthPending = z.infer<typeof mcpOAuthPendingSchema>;\nexport type McpTool = z.infer<typeof mcpToolSchema>;\nexport type McpToolCallResult = z.infer<typeof mcpToolCallResultSchema>;\n","import { z } from \"zod\";\n\nimport { parseSecretPayload } from \"./mcp-api\";\nimport { mcpToolSchema } from \"./mcp-types\";\nimport type { AuthPersistenceChanges } from \"./services\";\n\nconst serverConnectionCacheOutputSchema = z.object({\n protocolVersion: z.string().nullable().optional(),\n serverInfo: z.unknown().nullable().optional(),\n capabilities: z.unknown().nullable().optional(),\n tools: z.array(mcpToolSchema).nullable().optional(),\n updatedAt: z.union([z.string(), z.date()]).optional(),\n});\n\nconst serverOutputSchema = z.object({\n slug: z.string(),\n name: z.string().nullable().optional(),\n endpointUrl: z.string(),\n authMode: z.string(),\n cache: serverConnectionCacheOutputSchema.nullable().optional(),\n});\n\nexport const refreshOutputSchema = z.object({\n ok: z.boolean(),\n tools: z.array(mcpToolSchema),\n stage: z.enum([\"auth\", \"list_tools\"]).nullable(),\n checkedAt: z.string(),\n server: serverOutputSchema.omit({ cache: true }),\n auth: z.object({\n authenticated: z.boolean(),\n mode: z.string(),\n tokenPresent: z.boolean(),\n expiresAt: z.union([z.string(), z.date()]).nullable(),\n expired: z.boolean().nullable(),\n scopes: z.object({\n requested: z.array(z.string()).nullable(),\n granted: z.array(z.string()).nullable(),\n missing: z.array(z.string()).nullable(),\n raw: z.string().nullable(),\n }),\n }),\n live: z.object({\n reachable: z.boolean(),\n listToolsOk: z.boolean(),\n toolCount: z.number().nullable(),\n protocolVersion: z.string().nullable(),\n serverInfo: z.unknown().nullable(),\n capabilities: z.unknown().nullable(),\n }),\n cache: z.object({\n presentBeforeCheck: z.boolean(),\n previousToolCount: z.number().nullable(),\n updatedToolCount: z.number().nullable(),\n }),\n error: z.object({ code: z.string(), message: z.string() }).nullable(),\n});\n\nexport type SecretForDiagnostics = {\n id: { toString(): string } | string;\n kind: string;\n payload: string;\n expiresAt?: Date | string | null;\n};\n\nexport type OAuthStateForDiagnostics = {\n scope?: string | null;\n expiresAt: Date | string;\n};\n\ntype AuthPayloadForDiagnostics =\n | { type: \"bearer\"; token?: string }\n | { type: \"oauth\"; tokens?: { access_token?: string; scope?: string } }\n | {\n type: \"client_credentials\";\n scopes?: string[];\n tokens?: { access_token?: string; scope?: string };\n };\n\nconst splitScope = (scope: string | null | undefined) => {\n const scopes = scope?.split(/\\s+/).filter(Boolean) ?? [];\n return scopes.length > 0 ? scopes : null;\n};\n\nconst newestOAuthScope = (states: OAuthStateForDiagnostics[]) => {\n const [state] = states.toSorted(\n (left, right) => new Date(right.expiresAt).getTime() - new Date(left.expiresAt).getTime(),\n );\n return splitScope(state?.scope);\n};\n\nexport const buildAuthDiagnostics = ({\n mode,\n secrets,\n oauthStates,\n authChanges,\n}: {\n mode: string;\n secrets: SecretForDiagnostics[];\n oauthStates: OAuthStateForDiagnostics[];\n authChanges?: AuthPersistenceChanges;\n}) => {\n const authSecret = secrets.find((secret) => secret.kind === \"auth\");\n const authPayload = authChanges?.authPayload ?? authSecret?.payload;\n const authExpiresAt = authChanges?.authExpiresAt ?? authSecret?.expiresAt ?? null;\n const parsedAuth = authPayload\n ? parseSecretPayload<AuthPayloadForDiagnostics>(authPayload)\n : undefined;\n const granted = splitScope(\n parsedAuth?.type === \"oauth\" || parsedAuth?.type === \"client_credentials\"\n ? parsedAuth.tokens?.scope\n : undefined,\n );\n const requested =\n parsedAuth?.type === \"client_credentials\"\n ? (parsedAuth.scopes ?? null)\n : mode === \"oauth\"\n ? newestOAuthScope(oauthStates)\n : null;\n const grantedScopes = granted ? new Set(granted) : null;\n const missing =\n requested && grantedScopes ? requested.filter((scope) => !grantedScopes.has(scope)) : null;\n const tokenPresent =\n parsedAuth?.type === \"bearer\"\n ? Boolean(parsedAuth.token)\n : Boolean(parsedAuth?.tokens?.access_token);\n const expired = authExpiresAt ? new Date(authExpiresAt).getTime() <= Date.now() : null;\n\n return {\n authenticated: mode === \"none\" || tokenPresent,\n mode,\n tokenPresent,\n expiresAt: authExpiresAt,\n expired,\n scopes: {\n requested,\n granted,\n missing,\n raw:\n parsedAuth?.type === \"oauth\" || parsedAuth?.type === \"client_credentials\"\n ? (parsedAuth.tokens?.scope ?? null)\n : null,\n },\n };\n};\n\nexport const errorMessage = (error: unknown) =>\n error instanceof Error ? error.message : String(error);\n","import { z } from \"zod\";\n\nimport { defineRoutes } from \"@fragno-dev/core\";\n\nimport { mcpFragmentDefinition, scheduleMcpAuthExpiry } from \"./definition\";\nimport { buildAuthDiagnostics, errorMessage, refreshOutputSchema } from \"./diagnostics\";\nimport {\n assertAllowedEndpoint,\n callMcpTool,\n stringifySecretPayload,\n listMcpTools,\n} from \"./mcp-api\";\nimport {\n createServerInputSchema,\n MCP_OAUTH_REDIRECT_URI_QUERY_PARAMETER,\n mcpAuthStatusSchema,\n mcpOAuthPendingSchema,\n mcpOAuthRedirectUriSchema,\n mcpToolCallResultSchema,\n mcpToolSchema,\n oauthStartInputSchema,\n replaceServerConfigurationInputSchema,\n tokenAuthInputSchema,\n toolCallInputSchema,\n type AuthConfig,\n type McpTool,\n} from \"./mcp-types\";\nimport { mcpSchema } from \"./schema\";\nimport {\n createMcpOperationAuth,\n isMcpAuthReady,\n projectMcpAuthStatus,\n pendingOAuthLink,\n withoutOAuthConsent,\n writeOAuthSecretChanges,\n type AuthPersistenceChanges,\n} from \"./services\";\n\nconst serverConnectionCacheOutputSchema = z.object({\n protocolVersion: z.string().nullable().optional(),\n serverInfo: z.unknown().nullable().optional(),\n capabilities: z.unknown().nullable().optional(),\n tools: z.array(mcpToolSchema).nullable().optional(),\n updatedAt: z.union([z.string(), z.date()]).optional(),\n});\n\nconst serverOutputSchema = z.object({\n slug: z.string(),\n name: z.string().nullable().optional(),\n endpointUrl: z.string(),\n authMode: z.string(),\n cache: serverConnectionCacheOutputSchema.nullable().optional(),\n});\n\nfunction publicServer(\n server: {\n id: { toString(): string } | string;\n name?: string | null;\n endpointUrl: string;\n authMode: string;\n },\n cache?: {\n protocolVersion?: string | null;\n serverInfo?: unknown;\n capabilities?: unknown;\n tools?: unknown;\n updatedAt?: Date;\n } | null,\n) {\n return {\n slug: server.id.toString(),\n name: server.name,\n endpointUrl: server.endpointUrl,\n authMode: server.authMode,\n ...(cache\n ? {\n cache: {\n protocolVersion: cache.protocolVersion ?? null,\n serverInfo: cache.serverInfo ?? null,\n capabilities: cache.capabilities ?? null,\n // Cached tools come from the SDK's validated tools/list result.\n tools: Array.isArray(cache.tools) ? (cache.tools as McpTool[]) : null,\n updatedAt: cache.updatedAt,\n },\n }\n : {}),\n };\n}\n\nfunction authMode(auth: AuthConfig) {\n return auth.type;\n}\n\nexport const mcpRoutesFactory = defineRoutes(mcpFragmentDefinition).create(\n ({ services, defineRoute, config }) => {\n return [\n defineRoute({\n method: \"POST\",\n path: \"/servers\",\n inputSchema: createServerInputSchema,\n outputSchema: serverOutputSchema,\n errorCodes: [\"SERVER_EXISTS\", \"ENDPOINT_NOT_ALLOWED\"],\n handler: async function ({ input }, { json, error }) {\n const body = await input.valid();\n try {\n assertAllowedEndpoint(body.endpointUrl, config);\n } catch (err) {\n return error({ code: \"ENDPOINT_NOT_ALLOWED\", message: (err as Error).message }, 400);\n }\n\n const payload = body.auth.type === \"none\" ? undefined : stringifySecretPayload(body.auth);\n\n const result = await this.handlerTx()\n .retrieve(({ forSchema }) =>\n forSchema(mcpSchema).findFirst(\"server_configuration\", (b) =>\n b.whereIndex(\"primary\", (eb) => eb(\"id\", \"=\", body.slug)),\n ),\n )\n .mutate(({ forSchema, retrieveResult: [existing] }) => {\n if (existing) {\n return { exists: true as const };\n }\n\n const uow = forSchema(mcpSchema);\n uow.create(\"server_configuration\", {\n id: body.slug,\n name: body.name ?? null,\n endpointUrl: body.endpointUrl,\n authMode: authMode(body.auth),\n });\n if (payload) {\n uow.create(\"secret\", {\n id: `${body.slug}:auth`,\n serverId: body.slug,\n kind: \"auth\",\n payload,\n expiresAt: null,\n });\n }\n if (body.auth.type !== \"oauth\") {\n uow.triggerHook(\"internalRefreshServerConfiguration\", { serverId: body.slug });\n }\n uow.triggerHook(\"onServerReadinessChanged\", {\n serverId: body.slug,\n ready: isMcpAuthReady(\n authMode(body.auth),\n payload === undefined ? undefined : { payload, expiresAt: null },\n ),\n });\n return { exists: false as const };\n })\n .execute();\n\n if (result.exists) {\n return error({ code: \"SERVER_EXISTS\", message: \"MCP server already exists\" }, 409);\n }\n\n return json(publicServer({ ...body, id: body.slug, authMode: authMode(body.auth) }), 201);\n },\n }),\n\n defineRoute({\n method: \"GET\",\n path: \"/servers\",\n outputSchema: z.object({ servers: z.array(serverOutputSchema) }),\n handler: async function (_, { json }) {\n const [servers, caches] = await this.handlerTx()\n .retrieve(({ forSchema }) =>\n forSchema(mcpSchema)\n .find(\"server_configuration\", (b) => b.whereIndex(\"primary\"))\n .find(\"server_connection_cache\", (b) => b.whereIndex(\"primary\")),\n )\n .execute();\n const cacheByServerId = new Map(caches.map((cache) => [cache.id.toString(), cache]));\n return json({\n servers: servers.map((server) =>\n publicServer(server, cacheByServerId.get(server.id.toString())),\n ),\n });\n },\n }),\n\n defineRoute({\n method: \"GET\",\n path: \"/servers/:slug\",\n outputSchema: serverOutputSchema,\n errorCodes: [\"SERVER_NOT_FOUND\"],\n handler: async function ({ pathParams }, { json, error }) {\n const [server, cache] = await this.handlerTx()\n .retrieve(({ forSchema }) =>\n forSchema(mcpSchema)\n .findFirst(\"server_configuration\", (b) =>\n b.whereIndex(\"primary\", (eb) => eb(\"id\", \"=\", pathParams.slug)),\n )\n .findFirst(\"server_connection_cache\", (b) =>\n b.whereIndex(\"primary\", (eb) => eb(\"id\", \"=\", pathParams.slug)),\n ),\n )\n .execute();\n if (!server) {\n return error({ code: \"SERVER_NOT_FOUND\", message: \"MCP server not found\" }, 404);\n }\n return json(publicServer(server, cache));\n },\n }),\n\n defineRoute({\n method: \"PUT\",\n path: \"/servers/:slug/configuration\",\n inputSchema: replaceServerConfigurationInputSchema,\n outputSchema: serverOutputSchema,\n errorCodes: [\"SERVER_NOT_FOUND\", \"ENDPOINT_NOT_ALLOWED\"],\n handler: async function ({ input, pathParams }, { json, error }) {\n const body = await input.valid();\n try {\n assertAllowedEndpoint(body.endpointUrl, config);\n } catch (err) {\n return error({ code: \"ENDPOINT_NOT_ALLOWED\", message: (err as Error).message }, 400);\n }\n const payload = body.auth.type === \"none\" ? null : stringifySecretPayload(body.auth);\n const result = await this.handlerTx()\n .retrieve(({ forSchema }) =>\n forSchema(mcpSchema)\n .findFirst(\"server_configuration\", (b) =>\n b.whereIndex(\"primary\", (eb) => eb(\"id\", \"=\", pathParams.slug)),\n )\n .find(\"secret\", (b) =>\n b.whereIndex(\"idx_secret_server_kind\", (eb) =>\n eb(\"serverId\", \"=\", pathParams.slug),\n ),\n )\n .find(\"oauthState\", (b) =>\n b.whereIndex(\"idx_oauth_state_server\", (eb) =>\n eb(\"serverId\", \"=\", pathParams.slug),\n ),\n )\n .findFirst(\"server_connection_cache\", (b) =>\n b.whereIndex(\"primary\", (eb) => eb(\"id\", \"=\", pathParams.slug)),\n ),\n )\n .mutate(({ forSchema, retrieveResult: [server, secrets, oauthStates, cache] }) => {\n if (!server) {\n return { server: null };\n }\n const uow = forSchema(mcpSchema);\n const replaced = {\n ...server,\n name: body.name ?? null,\n endpointUrl: body.endpointUrl,\n authMode: authMode(body.auth),\n };\n uow.update(\"server_configuration\", server.id, (b) =>\n b\n .set({\n name: replaced.name,\n endpointUrl: replaced.endpointUrl,\n authMode: replaced.authMode,\n updatedAt: b.now(),\n })\n .check(),\n );\n // Tokens, registered clients, discovery state, and pending links belong to the old\n // endpoint and credentials, so only the replacement auth survives.\n const authSecret = secrets.find((secret) => secret.kind === \"auth\");\n for (const secret of secrets) {\n if (secret !== authSecret || payload === null) {\n uow.delete(\"secret\", secret.id);\n }\n }\n if (payload !== null) {\n if (authSecret) {\n uow.update(\"secret\", authSecret.id, (b) =>\n b.set({ payload, expiresAt: null, updatedAt: b.now() }).check(),\n );\n } else {\n uow.create(\"secret\", {\n id: `${pathParams.slug}:auth`,\n serverId: pathParams.slug,\n kind: \"auth\",\n payload,\n expiresAt: null,\n });\n }\n }\n for (const oauthState of oauthStates) {\n uow.delete(\"oauthState\", oauthState.id);\n }\n if (cache) {\n uow.delete(\"server_connection_cache\", cache.id);\n }\n if (body.auth.type !== \"oauth\") {\n uow.triggerHook(\"internalRefreshServerConfiguration\", {\n serverId: pathParams.slug,\n });\n }\n const ready = isMcpAuthReady(\n replaced.authMode,\n payload === null ? undefined : { payload, expiresAt: null },\n );\n if (ready !== isMcpAuthReady(server.authMode, authSecret)) {\n uow.triggerHook(\"onServerReadinessChanged\", { serverId: pathParams.slug, ready });\n }\n return { server: replaced };\n })\n .execute();\n if (!result.server) {\n return error({ code: \"SERVER_NOT_FOUND\", message: \"MCP server not found\" }, 404);\n }\n return json(publicServer(result.server));\n },\n }),\n\n defineRoute({\n method: \"DELETE\",\n path: \"/servers/:slug\",\n errorCodes: [\"SERVER_NOT_FOUND\"],\n handler: async function ({ pathParams }, { empty, error }) {\n const result = await this.handlerTx()\n .retrieve(({ forSchema }) =>\n forSchema(mcpSchema)\n .findFirst(\"server_configuration\", (b) =>\n b.whereIndex(\"primary\", (eb) => eb(\"id\", \"=\", pathParams.slug)),\n )\n .find(\"secret\", (b) =>\n b.whereIndex(\"idx_secret_server_kind\", (eb) =>\n eb(\"serverId\", \"=\", pathParams.slug),\n ),\n )\n .find(\"oauthState\", (b) =>\n b.whereIndex(\"idx_oauth_state_server\", (eb) =>\n eb(\"serverId\", \"=\", pathParams.slug),\n ),\n )\n .findFirst(\"server_connection_cache\", (b) =>\n b.whereIndex(\"primary\", (eb) => eb(\"id\", \"=\", pathParams.slug)),\n ),\n )\n .mutate(({ forSchema, retrieveResult: [server, secrets, oauthStates, cache] }) => {\n if (!server) {\n return { deleted: false as const };\n }\n const uow = forSchema(mcpSchema);\n for (const secret of secrets) {\n uow.delete(\"secret\", secret.id);\n }\n for (const oauthState of oauthStates) {\n uow.delete(\"oauthState\", oauthState.id);\n }\n if (cache) {\n uow.delete(\"server_connection_cache\", cache.id);\n }\n uow.delete(\"server_configuration\", server.id);\n uow.triggerHook(\"onServerConfigurationDeleted\", { serverId: pathParams.slug });\n return { deleted: true as const };\n })\n .execute();\n if (!result.deleted) {\n return error({ code: \"SERVER_NOT_FOUND\", message: \"MCP server not found\" }, 404);\n }\n return empty(204);\n },\n }),\n\n defineRoute({\n method: \"GET\",\n path: \"/servers/:slug/auth/status\",\n outputSchema: mcpAuthStatusSchema,\n errorCodes: [\"SERVER_NOT_FOUND\"],\n handler: async function ({ pathParams }, { json, error }) {\n const [server, secret, pendingState] = await this.handlerTx()\n .retrieve(({ forSchema }) =>\n forSchema(mcpSchema)\n .findFirst(\"server_configuration\", (b) =>\n b.whereIndex(\"primary\", (eb) => eb(\"id\", \"=\", pathParams.slug)),\n )\n .findFirst(\"secret\", (b) =>\n b.whereIndex(\"idx_secret_server_kind\", (eb) =>\n eb.and(eb(\"serverId\", \"=\", pathParams.slug), eb(\"kind\", \"=\", \"auth\")),\n ),\n )\n .findFirst(\"oauthState\", (b) =>\n b\n .whereIndex(\"idx_oauth_state_pending\", (eb) =>\n eb.and(\n eb(\"serverId\", \"=\", pathParams.slug),\n eb.isNull(\"consumedAt\"),\n eb(\"expiresAt\", \">\", eb.now()),\n ),\n )\n .orderByIndex(\"idx_oauth_state_pending\", \"desc\"),\n ),\n )\n .execute();\n if (!server) {\n return error({ code: \"SERVER_NOT_FOUND\", message: \"MCP server not found\" }, 404);\n }\n return json(\n projectMcpAuthStatus({\n authMode: server.authMode,\n authSecret: secret ?? undefined,\n hasPendingOAuth: pendingOAuthLink(pendingState) !== null,\n }),\n );\n },\n }),\n\n defineRoute({\n method: \"GET\",\n path: \"/servers/:slug/auth/pending\",\n outputSchema: mcpOAuthPendingSchema,\n errorCodes: [\"SERVER_NOT_FOUND\"],\n handler: async function ({ pathParams }, { json, error }) {\n const [server, pendingState] = await this.handlerTx()\n .retrieve(({ forSchema }) =>\n forSchema(mcpSchema)\n .findFirst(\"server_configuration\", (b) =>\n b.whereIndex(\"primary\", (eb) => eb(\"id\", \"=\", pathParams.slug)),\n )\n .findFirst(\"oauthState\", (b) =>\n b\n .whereIndex(\"idx_oauth_state_pending\", (eb) =>\n eb.and(\n eb(\"serverId\", \"=\", pathParams.slug),\n eb.isNull(\"consumedAt\"),\n eb(\"expiresAt\", \">\", eb.now()),\n ),\n )\n .orderByIndex(\"idx_oauth_state_pending\", \"desc\"),\n ),\n )\n .execute();\n if (!server) {\n return error({ code: \"SERVER_NOT_FOUND\", message: \"MCP server not found\" }, 404);\n }\n return json({ pending: pendingOAuthLink(pendingState) });\n },\n }),\n\n defineRoute({\n method: \"POST\",\n path: \"/servers/:slug/auth/token\",\n inputSchema: tokenAuthInputSchema,\n outputSchema: mcpAuthStatusSchema,\n errorCodes: [\"SERVER_NOT_FOUND\"],\n handler: async function ({ input, pathParams }, { json, error }) {\n const { token } = await input.valid();\n const payload = stringifySecretPayload({ type: \"bearer\", token });\n const result = await this.handlerTx()\n .retrieve(({ forSchema }) =>\n forSchema(mcpSchema)\n .findFirst(\"server_configuration\", (b) =>\n b.whereIndex(\"primary\", (eb) => eb(\"id\", \"=\", pathParams.slug)),\n )\n .findFirst(\"secret\", (b) =>\n b.whereIndex(\"idx_secret_server_kind\", (eb) =>\n eb.and(eb(\"serverId\", \"=\", pathParams.slug), eb(\"kind\", \"=\", \"auth\")),\n ),\n )\n .findFirst(\"server_connection_cache\", (b) =>\n b.whereIndex(\"primary\", (eb) => eb(\"id\", \"=\", pathParams.slug)),\n ),\n )\n .mutate(({ forSchema, retrieveResult: [server, secret, cache] }) => {\n if (!server) {\n return { found: false as const };\n }\n const uow = forSchema(mcpSchema);\n uow.update(\"server_configuration\", server.id, (b) =>\n b.set({ authMode: \"bearer\", updatedAt: b.now() }).check(),\n );\n if (cache) {\n uow.delete(\"server_connection_cache\", cache.id);\n }\n if (secret) {\n uow.update(\"secret\", secret.id, (b) =>\n b.set({ payload, expiresAt: null, updatedAt: b.now() }).check(),\n );\n } else {\n uow.create(\"secret\", {\n id: `${pathParams.slug}:auth`,\n serverId: pathParams.slug,\n kind: \"auth\",\n payload,\n expiresAt: null,\n });\n }\n uow.triggerHook(\"internalRefreshServerConfiguration\", { serverId: pathParams.slug });\n if (!isMcpAuthReady(server.authMode, secret ?? undefined)) {\n uow.triggerHook(\"onServerReadinessChanged\", {\n serverId: pathParams.slug,\n ready: true,\n });\n }\n return { found: true as const };\n })\n .execute();\n if (!result.found) {\n return error({ code: \"SERVER_NOT_FOUND\", message: \"MCP server not found\" }, 404);\n }\n return json({ mode: \"bearer\" as const, credentials: \"present\" as const });\n },\n }),\n\n defineRoute({\n method: \"POST\",\n path: \"/servers/:slug/auth/start\",\n inputSchema: oauthStartInputSchema,\n outputSchema: z.object({ authorizationUrl: z.string(), state: z.string() }),\n queryParameters: [MCP_OAUTH_REDIRECT_URI_QUERY_PARAMETER],\n errorCodes: [\n \"INVALID_OAUTH_REDIRECT_URI\",\n \"OAUTH_REDIRECT_URI_NOT_ALLOWED\",\n \"SERVER_NOT_FOUND\",\n \"OAUTH_ERROR\",\n ],\n handler: async function ({ input, pathParams, query }, { json, error }) {\n const body = await input.valid();\n const redirectUri = mcpOAuthRedirectUriSchema.safeParse(\n query.get(MCP_OAUTH_REDIRECT_URI_QUERY_PARAMETER),\n );\n if (!redirectUri.success) {\n return error(\n {\n code: \"INVALID_OAUTH_REDIRECT_URI\",\n message: \"MCP OAuth redirect URI must be an HTTP or HTTPS URL\",\n },\n 400,\n );\n }\n const oauthRedirectUri = new URL(redirectUri.data);\n if (!config.allowedOAuthRedirectUris?.(oauthRedirectUri)) {\n return error(\n {\n code: \"OAUTH_REDIRECT_URI_NOT_ALLOWED\",\n message: \"MCP OAuth redirect URI is not allowed by the fragment configuration\",\n },\n 400,\n );\n }\n const state = `${pathParams.slug}:${crypto.randomUUID()}`;\n try {\n const [result] = await this.handlerTx()\n .withServiceCalls(\n () =>\n [\n services.startOAuth({\n serverId: pathParams.slug,\n stateId: state,\n redirectUri: oauthRedirectUri.toString(),\n scope: body.scope,\n clientId: body.clientId,\n clientSecret: body.clientSecret,\n discardTokens: body.discardTokens,\n }),\n ] as const,\n )\n .execute();\n if (!result.found) {\n return error({ code: \"SERVER_NOT_FOUND\", message: \"MCP server not found\" }, 404);\n }\n return json({ authorizationUrl: result.authorizationUrl, state });\n } catch (err) {\n return error({ code: \"OAUTH_ERROR\", message: (err as Error).message }, 502);\n }\n },\n }),\n\n defineRoute({\n method: \"GET\",\n path: \"/oauth/callback\",\n outputSchema: z.object({ authenticated: z.boolean(), mode: z.string() }),\n errorCodes: [\"SERVER_NOT_FOUND\", \"OAUTH_ERROR\", \"INVALID_OAUTH_STATE\"],\n handler: async function ({ query }, { json, error }) {\n const code = query.get(\"code\");\n const stateId = query.get(\"state\");\n if (!code || !stateId) {\n return error(\n { code: \"INVALID_OAUTH_STATE\", message: \"Missing OAuth code or state\" },\n 400,\n );\n }\n try {\n const [result] = await this.handlerTx()\n .withServiceCalls(\n () =>\n [\n services.completeOAuthCallback({\n stateId,\n code,\n iss: query.get(\"iss\") ?? undefined,\n }),\n ] as const,\n )\n .execute();\n if (!result.found) {\n if (result.reason === \"server_not_found\") {\n return error({ code: \"SERVER_NOT_FOUND\", message: \"MCP server not found\" }, 404);\n }\n return error({ code: \"INVALID_OAUTH_STATE\", message: \"Invalid OAuth state\" }, 400);\n }\n return json({ authenticated: true, mode: \"oauth\" });\n } catch (err) {\n return error({ code: \"OAUTH_ERROR\", message: (err as Error).message }, 502);\n }\n },\n }),\n\n defineRoute({\n method: \"DELETE\",\n path: \"/servers/:slug/auth\",\n outputSchema: mcpAuthStatusSchema,\n errorCodes: [\"SERVER_NOT_FOUND\"],\n handler: async function ({ pathParams }, { json, error }) {\n const result = await this.handlerTx()\n .retrieve(({ forSchema }) =>\n forSchema(mcpSchema)\n .findFirst(\"server_configuration\", (b) =>\n b.whereIndex(\"primary\", (eb) => eb(\"id\", \"=\", pathParams.slug)),\n )\n .find(\"secret\", (b) =>\n b.whereIndex(\"idx_secret_server_kind\", (eb) =>\n eb(\"serverId\", \"=\", pathParams.slug),\n ),\n )\n .find(\"oauthState\", (b) =>\n b.whereIndex(\"idx_oauth_state_server\", (eb) =>\n eb(\"serverId\", \"=\", pathParams.slug),\n ),\n )\n .findFirst(\"server_connection_cache\", (b) =>\n b.whereIndex(\"primary\", (eb) => eb(\"id\", \"=\", pathParams.slug)),\n ),\n )\n .mutate(({ forSchema, retrieveResult: [server, secrets, oauthStates, cache] }) => {\n if (!server) {\n return { found: false as const };\n }\n const uow = forSchema(mcpSchema);\n const authSecret = secrets.find((secret) => secret.kind === \"auth\");\n // Only OAuth consent is cleared: the registered client and discovery state stay usable.\n const remainingAuth =\n server.authMode === \"oauth\" && authSecret\n ? { payload: withoutOAuthConsent(authSecret.payload), expiresAt: null }\n : undefined;\n if (server.authMode === \"oauth\") {\n if (authSecret && remainingAuth) {\n uow.update(\"secret\", authSecret.id, (b) =>\n b\n .set({ payload: remainingAuth.payload, expiresAt: null, updatedAt: b.now() })\n .check(),\n );\n }\n } else {\n for (const secret of secrets) {\n uow.delete(\"secret\", secret.id);\n }\n }\n const ready = isMcpAuthReady(server.authMode, remainingAuth);\n if (ready !== isMcpAuthReady(server.authMode, authSecret)) {\n uow.triggerHook(\"onServerReadinessChanged\", { serverId: pathParams.slug, ready });\n }\n for (const oauthState of oauthStates) {\n uow.delete(\"oauthState\", oauthState.id);\n }\n if (cache) {\n uow.delete(\"server_connection_cache\", cache.id);\n }\n // Keep the mode: clearing credentials must not turn the server unauthenticated.\n uow.update(\"server_configuration\", server.id, (b) =>\n b.set({ updatedAt: b.now() }).check(),\n );\n return {\n found: true as const,\n status: projectMcpAuthStatus({\n authMode: server.authMode,\n authSecret: undefined,\n hasPendingOAuth: false,\n }),\n };\n })\n .execute();\n if (!result.found) {\n return error({ code: \"SERVER_NOT_FOUND\", message: \"MCP server not found\" }, 404);\n }\n return json(result.status);\n },\n }),\n\n defineRoute({\n method: \"POST\",\n path: \"/servers/:slug/refresh\",\n outputSchema: refreshOutputSchema,\n errorCodes: [\"SERVER_NOT_FOUND\"],\n handler: async function ({ pathParams }, { json, error }) {\n const checkedAt = new Date().toISOString();\n let prepared:\n | {\n authChanges?: AuthPersistenceChanges;\n toolsOperation: Awaited<ReturnType<typeof listMcpTools<AuthPersistenceChanges>>>;\n }\n | undefined;\n let authError: unknown;\n\n const result = await this.handlerTx()\n .retrieve(({ forSchema }) =>\n forSchema(mcpSchema)\n .findFirst(\"server_configuration\", (b) =>\n b.whereIndex(\"primary\", (eb) => eb(\"id\", \"=\", pathParams.slug)),\n )\n .find(\"secret\", (b) =>\n b.whereIndex(\"idx_secret_server_kind\", (eb) =>\n eb(\"serverId\", \"=\", pathParams.slug),\n ),\n )\n .findFirst(\"server_connection_cache\", (b) =>\n b.whereIndex(\"primary\", (eb) => eb(\"id\", \"=\", pathParams.slug)),\n )\n .find(\"oauthState\", (b) =>\n b.whereIndex(\"idx_oauth_state_server\", (eb) =>\n eb(\"serverId\", \"=\", pathParams.slug),\n ),\n ),\n )\n .afterRetrieve(async (_uow, [server, secrets]) => {\n if (!server) {\n return;\n }\n\n try {\n const auth = await createMcpOperationAuth({ config, server, secrets });\n const toolsOperation = await listMcpTools({\n endpointUrl: server.endpointUrl,\n auth,\n fetchImplementation: config.fetch,\n });\n prepared = {\n authChanges: toolsOperation.authChanges,\n toolsOperation,\n };\n } catch (err) {\n authError = err;\n }\n })\n .mutate(({ forSchema, retrieveResult: [server, secrets, cache, oauthStates] }) => {\n if (!server) {\n return { found: false as const };\n }\n\n const serverId = server.id.toString();\n const previousTools = Array.isArray(cache?.tools) ? cache.tools : null;\n const baseDiagnostics = {\n checkedAt,\n server: publicServer(server),\n cache: {\n presentBeforeCheck: Boolean(cache),\n previousToolCount: previousTools?.length ?? null,\n updatedToolCount: null,\n },\n };\n\n if (authError) {\n return {\n found: true as const,\n refresh: {\n ...baseDiagnostics,\n ok: false,\n tools: [],\n stage: \"auth\" as const,\n auth: buildAuthDiagnostics({\n mode: server.authMode,\n secrets,\n oauthStates,\n }),\n live: {\n reachable: false,\n listToolsOk: false,\n toolCount: null,\n protocolVersion: null,\n serverInfo: null,\n capabilities: null,\n },\n error: { code: \"AUTH_ERROR\", message: errorMessage(authError) },\n },\n };\n }\n\n if (!prepared) {\n throw new Error(\"MCP refresh operation was not prepared\");\n }\n\n const preparedOperation = prepared;\n const uow = forSchema(mcpSchema);\n const upsertSecret = (kind: string, payload: string, expiresAt: Date | null) => {\n const existing = secrets.find((secret) => secret.kind === kind);\n if (existing) {\n uow.update(\"secret\", existing.id, (b) =>\n b.set({ payload, expiresAt, updatedAt: b.now() }).check(),\n );\n return;\n }\n uow.create(\"secret\", {\n id: `${serverId}:${kind}`,\n serverId,\n kind,\n payload,\n expiresAt,\n });\n };\n\n if (preparedOperation.authChanges) {\n writeOAuthSecretChanges(preparedOperation.authChanges, {\n upsert: (kind, payload) => {\n upsertSecret(kind, payload, null);\n },\n remove: (kind) => {\n const existing = secrets.find((secret) => secret.kind === kind);\n if (existing) {\n uow.delete(\"secret\", existing.id);\n }\n },\n });\n }\n if (preparedOperation.authChanges?.authPayload) {\n const after = {\n payload: preparedOperation.authChanges.authPayload,\n expiresAt: preparedOperation.authChanges.authExpiresAt ?? null,\n };\n upsertSecret(\"auth\", after.payload, after.expiresAt);\n const before = secrets.find((secret) => secret.kind === \"auth\");\n const ready = isMcpAuthReady(server.authMode, after);\n if (ready !== isMcpAuthReady(server.authMode, before)) {\n uow.triggerHook(\"onServerReadinessChanged\", { serverId, ready });\n }\n scheduleMcpAuthExpiry(uow, serverId, server.authMode, after);\n }\n\n const auth = buildAuthDiagnostics({\n mode: server.authMode,\n secrets,\n oauthStates,\n authChanges: preparedOperation.authChanges,\n });\n\n if (!preparedOperation.toolsOperation.ok) {\n return {\n found: true as const,\n refresh: {\n ...baseDiagnostics,\n ok: false,\n tools: [],\n stage: \"list_tools\" as const,\n auth,\n live: {\n reachable: false,\n listToolsOk: false,\n toolCount: null,\n protocolVersion: null,\n serverInfo: null,\n capabilities: null,\n },\n error: {\n code: \"MCP_ERROR\",\n message: errorMessage(preparedOperation.toolsOperation.error),\n },\n },\n };\n }\n\n const tools = Array.isArray(preparedOperation.toolsOperation.result.tools)\n ? preparedOperation.toolsOperation.result.tools\n : [];\n if (!previousTools || JSON.stringify(previousTools) !== JSON.stringify(tools)) {\n uow.triggerHook(\"onServerConfigurationChanged\", {\n serverId,\n current: { tools },\n });\n }\n uow.delete(\"server_connection_cache\", serverId);\n uow.create(\"server_connection_cache\", {\n id: serverId,\n serverId,\n protocolVersion: preparedOperation.toolsOperation.metadata.protocolVersion ?? null,\n serverInfo: preparedOperation.toolsOperation.metadata.serverInfo ?? null,\n capabilities: preparedOperation.toolsOperation.metadata.capabilities ?? null,\n tools,\n });\n\n return {\n found: true as const,\n refresh: {\n ...baseDiagnostics,\n ok: true,\n tools,\n stage: null,\n auth,\n live: {\n reachable: true,\n listToolsOk: true,\n toolCount: tools.length,\n protocolVersion:\n preparedOperation.toolsOperation.metadata.protocolVersion ?? null,\n serverInfo: preparedOperation.toolsOperation.metadata.serverInfo ?? null,\n capabilities: preparedOperation.toolsOperation.metadata.capabilities ?? null,\n },\n cache: {\n ...baseDiagnostics.cache,\n updatedToolCount: tools.length,\n },\n error: null,\n },\n };\n })\n .execute();\n\n if (!result.found) {\n return error({ code: \"SERVER_NOT_FOUND\", message: \"MCP server not found\" }, 404);\n }\n\n return json(result.refresh);\n },\n }),\n\n defineRoute({\n method: \"POST\",\n path: \"/servers/:slug/tools/execute\",\n inputSchema: toolCallInputSchema,\n outputSchema: mcpToolCallResultSchema,\n errorCodes: [\"SERVER_NOT_FOUND\", \"MCP_ERROR\"],\n handler: async function ({ input, pathParams }, { json, error }) {\n const body = await input.valid();\n try {\n let prepared:\n | {\n serverId: string;\n authChanges?: AuthPersistenceChanges;\n toolOperation: Awaited<ReturnType<typeof callMcpTool<AuthPersistenceChanges>>>;\n }\n | undefined;\n\n const result = await this.handlerTx()\n .retrieve(({ forSchema }) =>\n forSchema(mcpSchema)\n .findFirst(\"server_configuration\", (b) =>\n b.whereIndex(\"primary\", (eb) => eb(\"id\", \"=\", pathParams.slug)),\n )\n .find(\"secret\", (b) =>\n b.whereIndex(\"idx_secret_server_kind\", (eb) =>\n eb(\"serverId\", \"=\", pathParams.slug),\n ),\n )\n .findFirst(\"server_connection_cache\", (b) =>\n b.whereIndex(\"primary\", (eb) => eb(\"id\", \"=\", pathParams.slug)),\n ),\n )\n .afterRetrieve(async (_uow, [server, secrets]) => {\n if (!server) {\n return;\n }\n\n const auth = await createMcpOperationAuth({ config, server, secrets });\n const toolOperation = await callMcpTool({\n endpointUrl: server.endpointUrl,\n auth,\n name: body.name,\n toolArguments: body.arguments,\n timeoutMs: body.timeoutMs,\n fetchImplementation: config.fetch,\n });\n prepared = {\n serverId: server.id.toString(),\n authChanges: toolOperation.authChanges,\n toolOperation,\n };\n })\n .mutate(({ forSchema, retrieveResult: [server, secrets, cache] }) => {\n if (!server) {\n return { found: false as const };\n }\n if (!prepared) {\n throw new Error(\"MCP operation was not prepared\");\n }\n\n const preparedOperation = prepared;\n const uow = forSchema(mcpSchema);\n const upsertSecret = (kind: string, payload: string, expiresAt: Date | null) => {\n const existing = secrets.find((secret) => secret.kind === kind);\n if (existing) {\n uow.update(\"secret\", existing.id, (b) =>\n b.set({ payload, expiresAt, updatedAt: b.now() }).check(),\n );\n return;\n }\n uow.create(\"secret\", {\n id: `${preparedOperation.serverId}:${kind}`,\n serverId: preparedOperation.serverId,\n kind,\n payload,\n expiresAt,\n });\n };\n\n if (preparedOperation.authChanges) {\n writeOAuthSecretChanges(preparedOperation.authChanges, {\n upsert: (kind, payload) => {\n upsertSecret(kind, payload, null);\n },\n remove: (kind) => {\n const existing = secrets.find((secret) => secret.kind === kind);\n if (existing) {\n uow.delete(\"secret\", existing.id);\n }\n },\n });\n }\n if (preparedOperation.authChanges?.authPayload) {\n const after = {\n payload: preparedOperation.authChanges.authPayload,\n expiresAt: preparedOperation.authChanges.authExpiresAt ?? null,\n };\n upsertSecret(\"auth\", after.payload, after.expiresAt);\n const before = secrets.find((secret) => secret.kind === \"auth\");\n const ready = isMcpAuthReady(server.authMode, after);\n if (ready !== isMcpAuthReady(server.authMode, before)) {\n uow.triggerHook(\"onServerReadinessChanged\", {\n serverId: preparedOperation.serverId,\n ready,\n });\n }\n scheduleMcpAuthExpiry(uow, preparedOperation.serverId, server.authMode, after);\n }\n\n const toolOperation = preparedOperation.toolOperation;\n if (!toolOperation.ok) {\n return {\n found: true as const,\n error:\n toolOperation.error instanceof Error\n ? toolOperation.error.message\n : String(toolOperation.error),\n };\n }\n\n if (cache) {\n uow.update(\"server_connection_cache\", cache.id, (b) =>\n b\n .set({\n protocolVersion: toolOperation.metadata.protocolVersion ?? null,\n serverInfo: toolOperation.metadata.serverInfo ?? null,\n capabilities: toolOperation.metadata.capabilities ?? null,\n updatedAt: b.now(),\n })\n .check(),\n );\n } else {\n uow.create(\"server_connection_cache\", {\n id: pathParams.slug,\n serverId: pathParams.slug,\n protocolVersion: toolOperation.metadata.protocolVersion ?? null,\n serverInfo: toolOperation.metadata.serverInfo ?? null,\n capabilities: toolOperation.metadata.capabilities ?? null,\n tools: null,\n });\n }\n return { found: true as const, result: toolOperation.result };\n })\n .execute();\n\n if (!result.found) {\n return error({ code: \"SERVER_NOT_FOUND\", message: \"MCP server not found\" }, 404);\n }\n if (\"error\" in result) {\n return error(\n { code: \"MCP_ERROR\", message: result.error ?? \"MCP operation failed\" },\n 502,\n );\n }\n return json(result.result);\n } catch (err) {\n return error({ code: \"MCP_ERROR\", message: (err as Error).message }, 502);\n }\n },\n }),\n ];\n },\n);\n","import { createClientBuilder, type FragnoPublicClientConfig } from \"@fragno-dev/core/client\";\n\nimport { instantiate } from \"@fragno-dev/core\";\nimport type { FragnoPublicConfigWithDatabase } from \"@fragno-dev/db\";\n\nimport { mcpFragmentDefinition, type McpFragmentConfig } from \"./definition\";\nimport { mcpRoutesFactory } from \"./routes\";\n\nconst routes = [mcpRoutesFactory] as const;\n\nexport function createMcpFragment(\n config: McpFragmentConfig,\n fragnoConfig: FragnoPublicConfigWithDatabase,\n) {\n return instantiate(mcpFragmentDefinition)\n .withConfig(config)\n .withRoutes(routes)\n .withOptions(fragnoConfig)\n .build();\n}\n\nexport function createMcpFragmentClients(fragnoConfig: FragnoPublicClientConfig = {}) {\n const builder = createClientBuilder(mcpFragmentDefinition, fragnoConfig, routes);\n\n return {\n useServers: builder.createHook(\"/servers\"),\n useServer: builder.createHook(\"/servers/:slug\"),\n createServer: builder.createMutator(\"POST\", \"/servers\"),\n refreshServer: builder.createMutator(\"POST\", \"/servers/:slug/refresh\"),\n callTool: builder.createMutator(\"POST\", \"/servers/:slug/tools/execute\"),\n setToken: builder.createMutator(\"POST\", \"/servers/:slug/auth/token\"),\n startOAuth: builder.createMutator(\"POST\", \"/servers/:slug/auth/start\"),\n deleteAuth: builder.createMutator(\"DELETE\", \"/servers/:slug/auth\"),\n };\n}\n"],"ignoreList":[10,11,12,13,14,15,16,21,22],"mappings":";;;;;;;;;;;;AAQA,SAASA,kBAAkBC,aAAa;CACvC,MAAMC,WAAWD,YAAYE,MAAM,GAAG,CAAC,CAACC,QAAQC,MAAMA,EAAEC,SAAS,CAAC;CAClE,MAAMC,QAAQ,CAAA;CACd,KAAK,MAAMC,WAAWN,UAAU;EAC/B,IAAIM,QAAQC,WAAW,GAAG,GAAG;GAC5BF,MAAMG,KAAKF,QAAQG,MAAM,CAAC,CAAC;GAC3B;EACD;EACA,IAAIH,YAAY,MAAM;GACrBD,MAAMG,KAAK,IAAI;GACf;EACD;EACA,IAAIF,QAAQC,WAAW,KAAK,GAAG;GAC9BF,MAAMG,KAAKF,QAAQG,MAAM,CAAC,CAAC;GAC3B;EACD;CACD;CACA,OAAOJ;AACR;;;;;;;;;;;;;;AAcA,SAASK,UAAUX,aAAaY,QAAQ;CACvC,MAAMC,kBAAkBb,YAAYE,MAAM,GAAG;CAC7C,MAAMY,gBAAgB,CAAA;CACtB,KAAK,MAAMP,WAAWM,iBAAiB;EACtC,IAAIN,QAAQF,WAAW,GAAG;GACzBS,cAAcL,KAAK,EAAE;GACrB;EACD;EACA,IAAIF,QAAQC,WAAW,GAAG,GAAG;GAC5B,MAAMO,OAAOR,QAAQG,MAAM,CAAC;GAC5B,MAAMM,QAAQJ,OAAOG;GACrB,IAAIC,UAAU,KAAK,GAAG,MAAM,IAAIC,MAAM,qCAAqCF,MAAM;GACjFD,cAAcL,KAAKS,mBAAmBF,KAAK,CAAC;GAC5C;EACD;EACA,IAAIT,YAAY,MAAM;GACrB,MAAMS,QAAQJ,OAAO;GACrB,IAAII,UAAU,KAAK,GAAG,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AEiB,OAAO;IAAEkC,eAAenD,EAAEoD,QAAQ;IAAGC,MAAMrD,EAAEmB,OAAO;GAAE,CAAC;GACvE0B,YAAY;IAAC;IAAoB;IAAe;GAAqB;GACrEC,eAAO,CAAA;EAiCT,CAAC;EAEDP,YAAY;GACVE,QAAQ;GACRC,MAAM;GACNE,cAActC;GACduC,YAAY,CAAC,kBAAkB;GAC/BC,eAAO,CAAA;EA0ET,CAAC;EAEDP,YAAY;GACVE,QAAQ;GACRC,MAAM;GACNE,cAAczC;GACd0C,YAAY,CAAC,kBAAkB;GAC/BC,eAAO,CAAA;EAmOT,CAAC;EAEDP,YAAY;GACVE,QAAQ;GACRC,MAAM;GACNC,aAAa9B;GACb+B,cAAcpC;GACdqC,YAAY,CAAC,oBAAoB,WAAW;GAC5CC,eAAO,CAAA;EAyJT,CAAC;CAAC;AAEN,CCnjCcY,CAAgB;AAEhC,SAAgBG,kBACdC,QACAC,cACA;CACA,OAAA,CAAA;AAKF;AAEA,SAAgBE,yBAAyBF,eAAyC,CAAC,GAAG;CACpF,MAAMG,UAAUZ,oBAAoBE,uBAAuBO,cAAcJ,MAAM;CAE/E,OAAO;EACLQ,YAAYD,QAAQE,WAAW,UAAU;EACzCC,WAAWH,QAAQE,WAAW,gBAAgB;EAC9CE,cAAcJ,QAAQK,cAAc,QAAQ,UAAU;EACtDC,eAAeN,QAAQK,cAAc,QAAQ,wBAAwB;EACrEE,UAAUP,QAAQK,cAAc,QAAQ,8BAA8B;EACtEG,UAAUR,QAAQK,cAAc,QAAQ,2BAA2B;EACnEI,YAAYT,QAAQK,cAAc,QAAQ,2BAA2B;EACrEK,YAAYV,QAAQK,cAAc,UAAU,qBAAqB;CACnE;AACF"}
@@ -0,0 +1,6 @@
1
+ //#region src/cli/cli.d.ts
2
+ type Logger = Pick<Console, "log" | "error">;
3
+ export declare function run(argv: string[], logger?: Logger): Promise<number>;
4
+ //#endregion
5
+ export {};
6
+ //# sourceMappingURL=cli.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.d.ts","names":[],"sources":["../../src/cli/cli.ts"],"mappings":";KAwCK,SAAS,KAAK;wBA0JG,IAAI,gBAAgB,SAAQ,SAAmB"}