@@ -6,6 +6,7 @@ import { buildMcpAppSandboxPath, resolveMcpAppSandboxPort } from "../agents/mcp-
66import { getMcpAppViewLease , type McpAppViewLease } from "../agents/mcp-ui-resource.js" ;
77import { formatErrorMessage } from "../infra/errors.js" ;
88import { safeEqualSecret } from "../security/secret-equal.js" ;
9+ import { respondPlainText } from "./control-ui-http-utils.js" ;
910import {
1011 classifyMcpAppStandalonePath ,
1112 MCP_APP_STANDALONE_PATH ,
@@ -210,10 +211,17 @@ function supportsStandaloneToolOperations(
210211 return view . allowedAppToolNames !== undefined && view . readOnly !== true ;
211212}
212213
213- function sendText ( res : ServerResponse , statusCode : number , body : string ) : void {
214+ function sendJsonRepresentation (
215+ req : IncomingMessage ,
216+ res : ServerResponse ,
217+ statusCode : number ,
218+ body : unknown ,
219+ ) : void {
220+ const serialized = JSON . stringify ( body ) ;
214221 res . statusCode = statusCode ;
215- res . setHeader ( "Content-Type" , "text/plain; charset=utf-8" ) ;
216- res . end ( body ) ;
222+ res . setHeader ( "Content-Type" , "application/json; charset=utf-8" ) ;
223+ res . setHeader ( "Content-Length" , String ( Buffer . byteLength ( serialized ) ) ) ;
224+ res . end ( req . method === "HEAD" ? undefined : serialized ) ;
217225}
218226
219227function runStandaloneMcpAppHost ( config : {
@@ -593,20 +601,20 @@ export async function handleMcpAppStandaloneHttpRequest(
593601 req . method !== "HEAD" &&
594602 ! ( url . pathname === MCP_APP_STANDALONE_VIEW_PATH && req . method === "POST" )
595603 ) {
596- sendText ( res , 404 , "Not Found" ) ;
604+ respondPlainText ( res , 404 , "Not Found" ) ;
597605 return true ;
598606 }
599607
600608 const gatewayPort = options . gatewayPort ?? req . socket . localPort ;
601609 if ( ! gatewayPort ) {
602- sendText ( res , 503 , "MCP App host unavailable" ) ;
610+ respondPlainText ( res , 503 , "MCP App host unavailable" ) ;
603611 return true ;
604612 }
605613 let sandboxPort : number ;
606614 try {
607615 sandboxPort = resolveMcpAppSandboxPort ( gatewayPort , options . sandboxPort ) ;
608616 } catch {
609- sendText ( res , 503 , "MCP App host unavailable" ) ;
617+ respondPlainText ( res , 503 , "MCP App host unavailable" ) ;
610618 return true ;
611619 }
612620
@@ -622,6 +630,7 @@ export async function handleMcpAppStandaloneHttpRequest(
622630 const shell = standaloneHostHtml ( ) ;
623631 res . statusCode = 200 ;
624632 res . setHeader ( "Content-Type" , "text/html; charset=utf-8" ) ;
633+ res . setHeader ( "Content-Length" , String ( Buffer . byteLength ( shell . html ) ) ) ;
625634 res . setHeader (
626635 "Content-Security-Policy" ,
627636 `default-src 'none'; script-src 'sha256-${ shell . scriptHash } '; style-src 'unsafe-inline'; connect-src 'self'; frame-src ${ frameOrigin } ; base-uri 'none'; form-action 'none'; object-src 'none'` ,
@@ -639,7 +648,7 @@ export async function handleMcpAppStandaloneHttpRequest(
639648 const active = ticket ? resolveTicketActiveView ( ticket , nowMs , secret ) : undefined ;
640649 if ( ! active ) {
641650 res . setHeader ( "WWW-Authenticate" , "MCP-App" ) ;
642- sendText ( res , 401 , "Unauthorized" ) ;
651+ respondPlainText ( res , 401 , "Unauthorized" ) ;
643652 return true ;
644653 }
645654 if ( req . method === "POST" ) {
@@ -678,36 +687,28 @@ export async function handleMcpAppStandaloneHttpRequest(
678687 try {
679688 return await withMcpAppActiveView ( active , "read" , ( ) => {
680689 const { runtime, view } = active ;
681- res . statusCode = 200 ;
682- res . setHeader ( "Content-Type" , "application/json; charset=utf-8" ) ;
683- res . end (
684- req . method === "HEAD"
685- ? undefined
686- : JSON . stringify ( {
687- sandboxUrl : buildMcpAppSandboxPath ( view . csp ) ,
688- sandboxPort,
689- ...( options . sandboxOrigin
690- ? { sandboxOrigin : new URL ( options . sandboxOrigin ) . origin }
691- : { } ) ,
692- html : view . html ,
693- ...( view . csp ? { csp : view . csp } : { } ) ,
694- toolInput : view . toolInput ,
695- toolResult : view . toolResult ,
696- serverTools : supportsStandaloneToolOperations ( view ) ,
697- serverResources : runtime . readResource !== undefined ,
698- ...( view . requestTimeoutMs !== undefined
699- ? {
700- // Keep the browser's outer deadline behind the SDK request
701- // so a valid near-deadline response can reach the App.
702- operationTimeoutMs : addTimerTimeoutGraceMs ( view . requestTimeoutMs ) ,
703- }
704- : { } ) ,
705- } ) ,
706- ) ;
690+ sendJsonRepresentation ( req , res , 200 , {
691+ sandboxUrl : buildMcpAppSandboxPath ( view . csp ) ,
692+ sandboxPort,
693+ ...( options . sandboxOrigin ? { sandboxOrigin : new URL ( options . sandboxOrigin ) . origin } : { } ) ,
694+ html : view . html ,
695+ ...( view . csp ? { csp : view . csp } : { } ) ,
696+ toolInput : view . toolInput ,
697+ toolResult : view . toolResult ,
698+ serverTools : supportsStandaloneToolOperations ( view ) ,
699+ serverResources : runtime . readResource !== undefined ,
700+ ...( view . requestTimeoutMs !== undefined
701+ ? {
702+ // Keep the browser's outer deadline behind the SDK request
703+ // so a valid near-deadline response can reach the App.
704+ operationTimeoutMs : addTimerTimeoutGraceMs ( view . requestTimeoutMs ) ,
705+ }
706+ : { } ) ,
707+ } ) ;
707708 return true ;
708709 } ) ;
709710 } catch ( error ) {
710- sendJson ( res , 429 , { ok : false , error : formatErrorMessage ( error ) } ) ;
711+ sendJsonRepresentation ( req , res , 429 , { ok : false , error : formatErrorMessage ( error ) } ) ;
711712 return true ;
712713 }
713714}
0 commit comments