44 AuthOrchestrationReadScope ,
55 EnvironmentHttpApi ,
66} from "@t3tools/contracts" ;
7- import { injectMcpAppCookieBootstrap , MCP_APP_MAX_HTML_BYTES } from "@t3tools/shared/mcpApp" ;
87import { isDevProxiedPath } from "@t3tools/shared/devProxy" ;
98import { decodeOtlpTraceRecords } from "@t3tools/shared/observability" ;
109import * as Data from "effect/Data" ;
@@ -29,7 +28,7 @@ import * as HttpApiBuilder from "effect/http-api/HttpApiBuilder";
2928import { OtlpTracer , OtlpSerialization } from "effect/observability" ;
3029
3130import * as ServerConfig from "./config.ts" ;
32- import { ASSET_ROUTE_PREFIX , resolveAsset } from "./assets/AssetAccess.ts" ;
31+ import { ASSET_ROUTE_PREFIX , readMcpAppDocument , resolveAsset } from "./assets/AssetAccess.ts" ;
3332import { githubMediaResponse } from "./assets/GitHubMediaFetch.ts" ;
3433import { statMediaFile , streamMediaFile , type OpenMediaFile } from "./assets/MediaFile.ts" ;
3534import {
@@ -180,24 +179,13 @@ export const assetFileResponse = Effect.fn("assetFileResponse")(function* (
180179 ! asset . download &&
181180 headers [ "Content-Type" ] === "text/html; charset=utf-8"
182181 ) {
183- const fs = yield * FileSystem . FileSystem ;
184- // Saved captures also contain a bounded CSP meta tag added after the raw HTML limit.
185- const limit = MCP_APP_MAX_HTML_BYTES + 128 * 1024 ;
186- const chunks = yield * fs . stream ( asset . path , { bytesToRead : limit + 1 } ) . pipe ( Stream . runCollect ) ;
187- const size = chunks . reduce ( ( total , chunk ) => total + chunk . byteLength , 0 ) ;
188- if ( size > limit ) {
182+ const document = yield * readMcpAppDocument ( asset . path ) ;
183+ if ( document . _tag === "TooLarge" ) {
189184 return HttpServerResponse . text ( "MCP App is too large to preview." , { status : 413 } ) ;
190185 }
191- const bytes = new Uint8Array ( size ) ;
192- let offset = 0 ;
193- for ( const chunk of chunks ) {
194- bytes . set ( chunk , offset ) ;
195- offset += chunk . byteLength ;
196- }
197- const html = injectMcpAppCookieBootstrap ( new TextDecoder ( ) . decode ( bytes ) ) ;
198186 // The served bytes differ from the saved capture: do not reuse its file
199187 // length or validators. HEAD reports the transformed document's length.
200- const response = HttpServerResponse . text ( html , { headers } ) ;
188+ const response = HttpServerResponse . text ( document . html , { headers } ) ;
201189 return method === "HEAD"
202190 ? HttpServerResponse . empty ( { status : 200 , headers : response . headers } )
203191 : response ;
0 commit comments