@@ -41,6 +41,43 @@ const { values } = parseArgs({
4141 } ,
4242} ) ;
4343
44+ // Function to get HTTP headers.
45+ // Supports only "sse" and "streamable-http" transport types.
46+ const getHttpHeaders = (
47+ req : express . Request ,
48+ transportType : string ,
49+ ) : HeadersInit => {
50+ const headers : HeadersInit = {
51+ Accept :
52+ transportType === "sse"
53+ ? "text/event-stream"
54+ : "text/event-stream, application/json" ,
55+ } ;
56+ const defaultHeaders =
57+ transportType === "sse"
58+ ? SSE_HEADERS_PASSTHROUGH
59+ : STREAMABLE_HTTP_HEADERS_PASSTHROUGH ;
60+
61+ for ( const key of defaultHeaders ) {
62+ if ( req . headers [ key ] === undefined ) {
63+ continue ;
64+ }
65+
66+ const value = req . headers [ key ] ;
67+ headers [ key ] = Array . isArray ( value ) ? value [ value . length - 1 ] : value ;
68+ }
69+
70+ // If the header "x-custom-auth-header" is present, use its value as the custom header name.
71+ if ( req . headers [ "x-custom-auth-header" ] !== undefined ) {
72+ const customHeaderName = req . headers [ "x-custom-auth-header" ] as string ;
73+ if ( req . headers [ customHeaderName ] !== undefined ) {
74+ const value = req . headers [ customHeaderName ] ;
75+ headers [ customHeaderName ] = value as string ;
76+ }
77+ }
78+ return headers ;
79+ } ;
80+
4481const app = express ( ) ;
4582app . use ( cors ( ) ) ;
4683app . use ( ( req , res , next ) => {
@@ -79,27 +116,8 @@ const createTransport = async (req: express.Request): Promise<Transport> => {
79116 return transport ;
80117 } else if ( transportType === "sse" ) {
81118 const url = query . url as string ;
82- const headers : HeadersInit = {
83- Accept : "text/event-stream" ,
84- } ;
85119
86- for ( const key of SSE_HEADERS_PASSTHROUGH ) {
87- if ( req . headers [ key ] === undefined ) {
88- continue ;
89- }
90-
91- const value = req . headers [ key ] ;
92- headers [ key ] = Array . isArray ( value ) ? value [ value . length - 1 ] : value ;
93- }
94-
95- // If the header "x-custom-auth-header" is present, use its value as the custom header name.
96- if ( req . headers [ "x-custom-auth-header" ] !== undefined ) {
97- const customHeaderName = req . headers [ "x-custom-auth-header" ] as string ;
98- if ( req . headers [ customHeaderName ] !== undefined ) {
99- const value = req . headers [ customHeaderName ] ;
100- headers [ customHeaderName ] = value as string ;
101- }
102- }
120+ const headers = getHttpHeaders ( req , transportType ) ;
103121
104122 console . log ( `SSE transport: url=${ url } , headers=${ Object . keys ( headers ) } ` ) ;
105123
@@ -116,27 +134,7 @@ const createTransport = async (req: express.Request): Promise<Transport> => {
116134 console . log ( "Connected to SSE transport" ) ;
117135 return transport ;
118136 } else if ( transportType === "streamable-http" ) {
119- const headers : HeadersInit = {
120- Accept : "text/event-stream, application/json" ,
121- } ;
122-
123- for ( const key of STREAMABLE_HTTP_HEADERS_PASSTHROUGH ) {
124- if ( req . headers [ key ] === undefined ) {
125- continue ;
126- }
127-
128- const value = req . headers [ key ] ;
129- headers [ key ] = Array . isArray ( value ) ? value [ value . length - 1 ] : value ;
130- }
131-
132- // If the header "x-custom-auth-header" is present, use its value as the custom header name.
133- if ( req . headers [ "x - custom - auth - header "] !== undefined ) {
134- const customHeaderName = req . headers [ "x-custom-auth-header" ] as string ;
135- if ( req . headers [ customHeaderName ] !== undefined ) {
136- const value = req . headers [ customHeaderName ] ;
137- headers [ customHeaderName ] = value as string ;
138- }
139- }
137+ const headers = getHttpHeaders ( req , transportType ) ;
140138
141139 const transport = new StreamableHTTPClientTransport (
142140 new URL ( query . url as string ) ,
0 commit comments