@@ -6,14 +6,20 @@ import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/
66import express from 'express' ;
77import { fromError } from 'zod-validation-error/v3' ;
88import { McpOptions , parseQueryOptions } from './options' ;
9- import { initMcpServer , newMcpServer } from './server' ;
9+ import { ClientOptions , initMcpServer , newMcpServer } from './server' ;
1010import { parseAuthHeaders } from './headers' ;
1111
12- const newServer = (
13- defaultMcpOptions : McpOptions ,
14- req : express . Request ,
15- res : express . Response ,
16- ) : McpServer | null => {
12+ const newServer = ( {
13+ clientOptions,
14+ mcpOptions : defaultMcpOptions ,
15+ req,
16+ res,
17+ } : {
18+ clientOptions : ClientOptions ;
19+ mcpOptions : McpOptions ;
20+ req : express . Request ;
21+ res : express . Response ;
22+ } ) : McpServer | null => {
1723 const server = newMcpServer ( ) ;
1824
1925 let mcpOptions : McpOptions ;
@@ -35,10 +41,8 @@ const newServer = (
3541 initMcpServer ( {
3642 server : server ,
3743 clientOptions : {
44+ ...clientOptions ,
3845 ...authOptions ,
39- defaultHeaders : {
40- 'X-Stainless-MCP' : 'true' ,
41- } ,
4246 } ,
4347 mcpOptions,
4448 } ) ;
@@ -56,17 +60,19 @@ const newServer = (
5660 return server ;
5761} ;
5862
59- const post = ( defaultOptions : McpOptions ) => async ( req : express . Request , res : express . Response ) => {
60- const server = newServer ( defaultOptions , req , res ) ;
61- // If we return null, we already set the authorization error.
62- if ( server === null ) return ;
63- const transport = new StreamableHTTPServerTransport ( {
64- // Stateless server
65- sessionIdGenerator : undefined ,
66- } ) ;
67- await server . connect ( transport ) ;
68- await transport . handleRequest ( req , res , req . body ) ;
69- } ;
63+ const post =
64+ ( options : { clientOptions : ClientOptions ; mcpOptions : McpOptions } ) =>
65+ async ( req : express . Request , res : express . Response ) => {
66+ const server = newServer ( { ...options , req, res } ) ;
67+ // If we return null, we already set the authorization error.
68+ if ( server === null ) return ;
69+ const transport = new StreamableHTTPServerTransport ( {
70+ // Stateless server
71+ sessionIdGenerator : undefined ,
72+ } ) ;
73+ await server . connect ( transport ) ;
74+ await transport . handleRequest ( req , res , req . body ) ;
75+ } ;
7076
7177const get = async ( req : express . Request , res : express . Response ) => {
7278 res . status ( 405 ) . json ( {
@@ -88,20 +94,26 @@ const del = async (req: express.Request, res: express.Response) => {
8894 } ) ;
8995} ;
9096
91- export const streamableHTTPApp = ( options : McpOptions ) : express . Express => {
97+ export const streamableHTTPApp = ( {
98+ clientOptions = { } ,
99+ mcpOptions = { } ,
100+ } : {
101+ clientOptions ?: ClientOptions ;
102+ mcpOptions ?: McpOptions ;
103+ } ) : express . Express => {
92104 const app = express ( ) ;
93105 app . set ( 'query parser' , 'extended' ) ;
94106 app . use ( express . json ( ) ) ;
95107
96108 app . get ( '/' , get ) ;
97- app . post ( '/' , post ( options ) ) ;
109+ app . post ( '/' , post ( { clientOptions , mcpOptions } ) ) ;
98110 app . delete ( '/' , del ) ;
99111
100112 return app ;
101113} ;
102114
103115export const launchStreamableHTTPServer = async ( options : McpOptions , port : number | string | undefined ) => {
104- const app = streamableHTTPApp ( options ) ;
116+ const app = streamableHTTPApp ( { mcpOptions : options } ) ;
105117 const server = app . listen ( port ) ;
106118 const address = server . address ( ) ;
107119
0 commit comments