99 TurnItemId ,
1010} from "@t3tools/contracts" ;
1111import { PROJECT_FAVICON_FALLBACK_MARKER } from "@t3tools/shared/projectFavicon" ;
12+ import { MCP_APP_MAX_HTML_BYTES } from "@t3tools/shared/mcpApp" ;
1213import { describe , expect , it } from "@effect/vitest" ;
1314import * as Crypto from "effect/Crypto" ;
1415import * as DateTime from "effect/DateTime" ;
@@ -30,7 +31,7 @@ import * as ProjectFaviconResolver from "../project/ProjectFaviconResolver.ts";
3031import * as T3ProjectFileLoader from "../project/T3ProjectFileLoader.ts" ;
3132import * as WorkspacePaths from "../workspace/WorkspacePaths.ts" ;
3233import { assetFileResponse } from "../http.ts" ;
33- import { ASSET_ROUTE_PREFIX , issueAssetUrl , resolveAsset } from "./AssetAccess.ts" ;
34+ import { ASSET_ROUTE_PREFIX , issueAssetUrl , readMcpAppHtml , resolveAsset } from "./AssetAccess.ts" ;
3435import * as NativeAppIconResolver from "./NativeAppIconResolver.ts" ;
3536import { openMediaFile } from "./MediaFile.ts" ;
3637import { symlinksSupported } from "@t3tools/shared/testing/symlinks" ;
@@ -121,6 +122,64 @@ const layerTest = Layer.mergeAll(
121122) . pipe ( Layer . provideMerge ( NodeServices . layer ) ) ;
122123
123124describe ( "AssetAccess" , ( ) => {
125+ describe ( "readMcpAppHtml" , ( ) => {
126+ it . effect ( "accepts snapshots at the stored HTML byte limit" , ( ) =>
127+ Effect . gen ( function * ( ) {
128+ const fileSystem = yield * FileSystem . FileSystem ;
129+ const path = yield * Path . Path ;
130+ const directory = yield * fileSystem . makeTempDirectoryScoped ( { prefix : "t3-mcp-html-" } ) ;
131+ const file = path . join ( directory , "app.html" ) ;
132+ const limit = MCP_APP_MAX_HTML_BYTES + 128 * 1024 ;
133+ yield * fileSystem . writeFile ( file , new Uint8Array ( limit ) . fill ( 120 ) ) ;
134+
135+ const bytes = yield * readMcpAppHtml ( file ) ;
136+ expect ( bytes ?. byteLength ) . toBe ( limit ) ;
137+ expect ( bytes ?. every ( ( byte ) => byte === 120 ) ) . toBe ( true ) ;
138+ } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
139+ ) ;
140+
141+ it . effect ( "rejects snapshots one byte past the limit and much larger files" , ( ) =>
142+ Effect . gen ( function * ( ) {
143+ const fileSystem = yield * FileSystem . FileSystem ;
144+ const path = yield * Path . Path ;
145+ const directory = yield * fileSystem . makeTempDirectoryScoped ( { prefix : "t3-mcp-html-" } ) ;
146+ const file = path . join ( directory , "app.html" ) ;
147+ const limit = MCP_APP_MAX_HTML_BYTES + 128 * 1024 ;
148+ for ( const size of [ limit + 1 , limit * 2 ] ) {
149+ yield * fileSystem . writeFile ( file , new Uint8Array ( size ) . fill ( 120 ) ) ;
150+ expect ( yield * readMcpAppHtml ( file ) ) . toBeNull ( ) ;
151+ }
152+ } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
153+ ) ;
154+
155+ it . effect ( "preserves UTF-8 bytes across stream chunks and accepts empty snapshots" , ( ) =>
156+ Effect . gen ( function * ( ) {
157+ const fileSystem = yield * FileSystem . FileSystem ;
158+ const path = yield * Path . Path ;
159+ const directory = yield * fileSystem . makeTempDirectoryScoped ( { prefix : "t3-mcp-html-" } ) ;
160+ const file = path . join ( directory , "app.html" ) ;
161+ for ( const html of [ "" , "<body>" + "☃" . repeat ( 100_000 ) + "</body>" ] ) {
162+ const expected = new TextEncoder ( ) . encode ( html ) ;
163+ yield * fileSystem . writeFile ( file , expected ) ;
164+ const bytes = yield * readMcpAppHtml ( file ) ;
165+ expect ( bytes ?. byteLength ) . toBe ( expected . byteLength ) ;
166+ expect ( bytes && new TextDecoder ( ) . decode ( bytes ) ) . toBe ( html ) ;
167+ }
168+ } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
169+ ) ;
170+
171+ it . effect ( "preserves filesystem failures instead of reporting oversized HTML" , ( ) =>
172+ Effect . gen ( function * ( ) {
173+ const fileSystem = yield * FileSystem . FileSystem ;
174+ const path = yield * Path . Path ;
175+ const directory = yield * fileSystem . makeTempDirectoryScoped ( { prefix : "t3-mcp-html-" } ) ;
176+ const error = yield * readMcpAppHtml ( path . join ( directory , "missing.html" ) ) . pipe ( Effect . flip ) ;
177+ expect ( error . _tag ) . toBe ( "PlatformError" ) ;
178+ expect ( error . reason . _tag ) . toBe ( "NotFound" ) ;
179+ } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
180+ ) ;
181+ } ) ;
182+
124183 it . effect ( "loads private media immediately after login with the GitHub credential" , ( ) => {
125184 let lookups = 0 ;
126185 const authorizations : Array < string | undefined > = [ ] ;
@@ -903,6 +962,49 @@ describe("AssetAccess", () => {
903962 } ) . pipe ( Effect . provide ( layerTest ) ) ,
904963 ) ;
905964
965+ it . effect ( "signs MCP render intent only for inline HTML attachments" , ( ) =>
966+ Effect . gen ( function * ( ) {
967+ const config = yield * ServerConfig . ServerConfig ;
968+ const fs = yield * FileSystem . FileSystem ;
969+ const path = yield * Path . Path ;
970+ yield * fs . makeDirectory ( config . attachmentsDir , { recursive : true } ) ;
971+ for ( const extension of [ "html" , "pdf" ] ) {
972+ const attachmentId = `thread-1-00000000-0000-4000-8000-000000000031-${ extension } ` ;
973+ yield * fs . writeFileString (
974+ path . join ( config . attachmentsDir , `${ attachmentId } .${ extension } ` ) ,
975+ "capture" ,
976+ ) ;
977+ for ( const disposition of [ "inline" , "attachment" ] as const ) {
978+ const result = yield * issueAssetUrl ( {
979+ resource : {
980+ _tag : "attachment" ,
981+ attachmentId,
982+ disposition,
983+ renderIntent : "mcp-app" ,
984+ } ,
985+ } ) ;
986+ const suffix = result . relativeUrl . slice ( `${ ASSET_ROUTE_PREFIX } /` . length ) ;
987+ const separator = suffix . indexOf ( "/" ) ;
988+ const asset = yield * resolveAsset (
989+ suffix . slice ( 0 , separator ) ,
990+ suffix . slice ( separator + 1 ) ,
991+ ) ;
992+ if ( asset ?. kind !== "file" ) throw new Error ( "Expected attachment file" ) ;
993+ expect ( "renderIntent" in asset ? asset . renderIntent : undefined ) . toBe (
994+ extension === "html" && disposition === "inline" ? "mcp-app" : undefined ,
995+ ) ;
996+ // Changing signed claims cannot opt an ordinary download into rendering.
997+ const token = suffix . slice ( 0 , separator ) ;
998+ const [ payload , signature ] = token . split ( "." ) ;
999+ const claims = JSON . parse ( Buffer . from ( payload ! , "base64url" ) . toString ( "utf8" ) ) ;
1000+ claims . renderIntent = claims . renderIntent ? undefined : "mcp-app" ;
1001+ const tampered = `${ Buffer . from ( JSON . stringify ( claims ) ) . toString ( "base64url" ) } .${ signature } ` ;
1002+ expect ( yield * resolveAsset ( tampered , suffix . slice ( separator + 1 ) ) ) . toBeNull ( ) ;
1003+ }
1004+ }
1005+ } ) . pipe ( Effect . provide ( layerTest ) ) ,
1006+ ) ;
1007+
9061008 it . effect ( "issues exact attachment capabilities by attachment id" , ( ) =>
9071009 Effect . gen ( function * ( ) {
9081010 const config = yield * ServerConfig . ServerConfig ;
0 commit comments