@@ -9,6 +9,8 @@ import { child as childLogger } from "../logger.js";
99
1010export interface InboundHttpHandle {
1111 close : ( ) => Promise < void > ;
12+ /** Actual bound port (useful when config port is 0). */
13+ port : number ;
1214}
1315
1416/**
@@ -29,7 +31,9 @@ export async function startHttpServer(args: {
2931} ) : Promise < InboundHttpHandle > {
3032 const { cfg, createServer : createMcp , upstream } = args ;
3133 const log = childLogger ( { component : "inbound-http" } ) ;
32- const { host, port, path : mcpPath , sessions } = cfg . inbound . http ;
34+ const { host, path : mcpPath , sessions } = cfg . inbound . http ;
35+ const requestedPort = cfg . inbound . http . port ;
36+ let boundPort = requestedPort ;
3337
3438 if ( sessions !== "stateless" ) {
3539 log . warn (
@@ -39,7 +43,7 @@ export async function startHttpServer(args: {
3943 }
4044
4145 const handler = async ( req : IncomingMessage , res : ServerResponse ) => {
42- const url = new URL ( req . url ?? "/" , `http://${ host } :${ port } ` ) ;
46+ const url = new URL ( req . url ?? "/" , `http://${ host } :${ boundPort } ` ) ;
4347
4448 if ( url . pathname === "/healthz" ) {
4549 const upstreams = [ ...upstream . connections . values ( ) ] . map ( ( c ) => ( {
@@ -101,14 +105,19 @@ export async function startHttpServer(args: {
101105
102106 await new Promise < void > ( ( resolve , reject ) => {
103107 httpServer . once ( "error" , reject ) ;
104- httpServer . listen ( port , host , ( ) => {
108+ httpServer . listen ( requestedPort , host , ( ) => {
105109 httpServer . off ( "error" , reject ) ;
110+ const addr = httpServer . address ( ) ;
111+ if ( addr && typeof addr === "object" ) {
112+ boundPort = addr . port ;
113+ }
106114 resolve ( ) ;
107115 } ) ;
108116 } ) ;
109- log . info ( { host, port, path : mcpPath , sessions } , "inbound HTTP transport listening" ) ;
117+ log . info ( { host, port : boundPort , path : mcpPath , sessions } , "inbound HTTP transport listening" ) ;
110118
111119 return {
120+ port : boundPort ,
112121 close : ( ) =>
113122 new Promise < void > ( ( resolve ) => {
114123 httpServer . close ( ( ) => resolve ( ) ) ;
0 commit comments