Skip to content

Commit f516c79

Browse files
committed
refactor(server): prepare MCP documents in the asset domain
1 parent 9775eef commit f516c79

3 files changed

Lines changed: 29 additions & 17 deletions

File tree

‎apps/server/src/assets/AssetAccess.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import {
3636
PROJECT_FAVICON_FALLBACK_MARKER,
3737
} from "@t3tools/shared/projectFavicon";
3838
import { MAX_TOOL_OUTPUT_IMAGE_BASE64_LENGTH, toolOutputImages } from "@t3tools/shared/toolOutput";
39+
import { injectMcpAppCookieBootstrap, MCP_APP_MAX_HTML_BYTES } from "@t3tools/shared/mcpApp";
40+
import * as Stream from "effect/Stream";
3941
import * as Clock from "effect/Clock";
4042
import * as Crypto from "effect/Crypto";
4143
import * as Effect from "effect/Effect";
@@ -815,6 +817,28 @@ export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (i
815817
};
816818
});
817819

820+
/** Read and prepare a saved MCP document without trusting its file length. */
821+
export const readMcpAppDocument = Effect.fn("AssetAccess.readMcpAppDocument")(function* (
822+
path: string,
823+
) {
824+
const fs = yield* FileSystem.FileSystem;
825+
// Saved captures also contain a bounded CSP meta tag added after the raw HTML limit.
826+
const limit = MCP_APP_MAX_HTML_BYTES + 128 * 1024;
827+
const chunks = yield* fs.stream(path, { bytesToRead: limit + 1 }).pipe(Stream.runCollect);
828+
const size = chunks.reduce((total, chunk) => total + chunk.byteLength, 0);
829+
if (size > limit) return { _tag: "TooLarge" as const };
830+
const bytes = new Uint8Array(size);
831+
let offset = 0;
832+
for (const chunk of chunks) {
833+
bytes.set(chunk, offset);
834+
offset += chunk.byteLength;
835+
}
836+
return {
837+
_tag: "Document" as const,
838+
html: injectMcpAppCookieBootstrap(new TextDecoder().decode(bytes)),
839+
};
840+
});
841+
818842
export const resolveAsset = Effect.fn("AssetAccess.resolveAsset")(function* (
819843
token: string,
820844
relativePath: string,

‎apps/server/src/http.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ describe("MCP App asset responses", () => {
336336
);
337337
expect(response.status).toBe(200);
338338
expect(response.headers.get("content-security-policy")).toBe(
339-
"sandbox allow-scripts allow-forms allow-popups",
339+
"sandbox allow-scripts allow-forms allow-popups allow-downloads",
340340
);
341341
expect(response.headers.get("content-length")).toBe(
342342
String(new TextEncoder().encode(transformed).byteLength),

‎apps/server/src/http.ts‎

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
AuthOrchestrationReadScope,
55
EnvironmentHttpApi,
66
} from "@t3tools/contracts";
7-
import { injectMcpAppCookieBootstrap, MCP_APP_MAX_HTML_BYTES } from "@t3tools/shared/mcpApp";
87
import { isDevProxiedPath } from "@t3tools/shared/devProxy";
98
import { decodeOtlpTraceRecords } from "@t3tools/shared/observability";
109
import * as Data from "effect/Data";
@@ -29,7 +28,7 @@ import * as HttpApiBuilder from "effect/http-api/HttpApiBuilder";
2928
import { OtlpTracer, OtlpSerialization } from "effect/observability";
3029

3130
import * 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";
3332
import { githubMediaResponse } from "./assets/GitHubMediaFetch.ts";
3433
import { statMediaFile, streamMediaFile, type OpenMediaFile } from "./assets/MediaFile.ts";
3534
import {
@@ -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

Comments
 (0)