11// Avoid exporting Vite types to prevent cross-version type mismatches in consumers
22import ansis from 'ansis' ;
33import type { BrowserLogLevel } from '@browser-echo/core' ;
4+ import type { IncomingMessage , ServerResponse } from 'node:http' ;
45import { mkdirSync , appendFileSync } from 'node:fs' ;
56import { dirname , join as joinPath } from 'node:path' ;
67import { startMcpServer , handleMcpHttpRequest , publishLogEntry , isMcpEnabled as _mcpEnvEnabled } from '@browser-echo/mcp' ;
@@ -44,7 +45,7 @@ const DEFAULTS: ResolvedOptions = {
4445 mcpRoute : '/__mcp'
4546} ;
4647
47- export default function browserEcho
48+ export default function browserEcho ( opts : BrowserLogsToTerminalOptions = { } ) : import ( 'vite' ) . Plugin {
4849 const options : ResolvedOptions = {
4950 ...DEFAULTS ,
5051 ...opts ,
@@ -74,21 +75,22 @@ export default function browserEcho
7475 configureServer ( server ) {
7576 if ( ! options . enabled ) return ;
7677 try { startMcpServer ( ) ; } catch { }
77- server . middlewares . use ( options . mcpRoute , ( req , res , next ) => {
78- // Allow only MCP methods; otherwise fall-through
79- const m = req . method || 'GET' ;
80- if ( m !== 'GET' && m !== 'POST' && m !== 'DELETE' ) return next ( ) ;
78+ server . middlewares . use ( options . mcpRoute , ( req : IncomingMessage , res : ServerResponse , _next : ( ) => void ) => {
79+ const m = ( req . method || 'GET' ) . toUpperCase ( ) ;
8180 ( async ( ) => {
8281 try {
8382 let body : Buffer | undefined ;
84- if ( m === 'POST' ) {
83+ if ( m === 'POST' || m === 'PUT' || m === 'PATCH' ) {
8584 body = await collectBody ( req ) ;
8685 }
8786 await handleMcpHttpRequest ( req as any , res as any , body ) ;
8887 } catch ( err : any ) {
8988 server . config . logger . error ( `${ options . tag } MCP error: ${ err ?. message || err } ` ) ;
90- res . statusCode = 500 ;
91- res . end ( 'mcp error' ) ;
89+ if ( ! res . headersSent ) {
90+ res . statusCode = 500 ;
91+ try { res . setHeader ( 'content-type' , 'application/json' ) ; } catch { }
92+ }
93+ res . end ( JSON . stringify ( { jsonrpc : '2.0' , error : { code : - 32603 , message : 'Internal server error' } , id : null } ) ) ;
9294 }
9395 } ) ( ) ;
9496 } ) ;
@@ -102,7 +104,7 @@ function attachMiddleware(server: any, options: ResolvedOptions) {
102104 const logFilePath = joinPath ( options . fileLog . dir , `dev-${ sessionStamp } .log` ) ;
103105 if ( options . fileLog . enabled ) { try { mkdirSync ( dirname ( logFilePath ) , { recursive : true } ) ; } catch { } }
104106
105- server . middlewares . use ( options . route , ( req , res , next ) => {
107+ server . middlewares . use ( options . route , ( req : IncomingMessage , res : ServerResponse , next : ( ) => void ) => {
106108 if ( req . method !== 'POST' ) return next ( ) ;
107109 collectBody ( req ) . then ( ( raw ) => {
108110 let payload : ClientPayload | null = null ;
0 commit comments