@@ -1053,6 +1053,149 @@ app.use(
10531053// Handle POST requests - stateful mode
10541054app . post ( '/mcp' , async ( req , res ) => {
10551055 const sessionId = req . headers [ 'mcp-session-id' ] as string | undefined ;
1056+ const reqVersion = req . headers [ 'mcp-protocol-version' ] as string | undefined ;
1057+ const body = req . body || { } ;
1058+ const method = body . method ;
1059+ const id = body . id ?? null ;
1060+ const params = body . params || { } ;
1061+ const meta = params . _meta ;
1062+ const metaVersion = meta ?. [ 'io.modelcontextprotocol/protocolVersion' ] ;
1063+
1064+ // If it's a stateless request (no session ID, and has either _meta or MCP-Protocol-Version header indicating stateless mode)
1065+ if ( ! sessionId && ( reqVersion || meta ) ) {
1066+ if ( process . env . STATELESS_NEGATIVE === 'true' ) {
1067+ return res . json ( {
1068+ jsonrpc : '2.0' ,
1069+ id,
1070+ result : { }
1071+ } ) ;
1072+ }
1073+
1074+ if ( ! reqVersion ) {
1075+ return res . status ( 400 ) . json ( {
1076+ jsonrpc : '2.0' ,
1077+ id,
1078+ error : { code : - 32600 , message : 'Missing MCP-Protocol-Version header' }
1079+ } ) ;
1080+ }
1081+
1082+ if (
1083+ ! meta ||
1084+ ! meta [ 'io.modelcontextprotocol/protocolVersion' ] ||
1085+ ! meta [ 'io.modelcontextprotocol/clientInfo' ] ||
1086+ ! meta [ 'io.modelcontextprotocol/clientCapabilities' ]
1087+ ) {
1088+ return res . status ( 200 ) . json ( {
1089+ jsonrpc : '2.0' ,
1090+ id,
1091+ error : {
1092+ code : - 32602 ,
1093+ message : 'Invalid params: missing _meta or required fields'
1094+ }
1095+ } ) ;
1096+ }
1097+
1098+ if ( reqVersion !== metaVersion ) {
1099+ return res . status ( 400 ) . json ( {
1100+ jsonrpc : '2.0' ,
1101+ id,
1102+ error : {
1103+ code : - 32600 ,
1104+ message : 'Mismatched MCP-Protocol-Version header'
1105+ }
1106+ } ) ;
1107+ }
1108+
1109+ if ( metaVersion !== 'DRAFT-2026-v1' ) {
1110+ return res . status ( 400 ) . json ( {
1111+ jsonrpc : '2.0' ,
1112+ id,
1113+ error : {
1114+ code : - 32001 ,
1115+ message : 'UnsupportedProtocolVersionError' ,
1116+ data : { supportedVersions : [ 'DRAFT-2026-v1' ] }
1117+ }
1118+ } ) ;
1119+ }
1120+
1121+ res . setHeader ( 'mcp-protocol-version' , 'DRAFT-2026-v1' ) ;
1122+
1123+ if ( method === 'server/discover' ) {
1124+ return res . json ( {
1125+ jsonrpc : '2.0' ,
1126+ id,
1127+ result : {
1128+ supportedVersions : [ 'DRAFT-2026-v1' ] ,
1129+ capabilities : { tools : { } } ,
1130+ serverInfo : { name : 'everything-stateless-server' , version : '1.0.0' }
1131+ }
1132+ } ) ;
1133+ }
1134+
1135+ if ( method === 'tools/list' ) {
1136+ return res . json ( {
1137+ jsonrpc : '2.0' ,
1138+ id,
1139+ result : {
1140+ tools : [
1141+ {
1142+ name : 'test_missing_capability' ,
1143+ description : 'Test tool requiring sampling' ,
1144+ inputSchema : { type : 'object' , properties : { } }
1145+ }
1146+ ]
1147+ }
1148+ } ) ;
1149+ }
1150+
1151+ if ( method === 'tools/call' ) {
1152+ const name = params . name ;
1153+ if ( name === 'test_missing_capability' ) {
1154+ const clientCaps = meta [ 'io.modelcontextprotocol/clientCapabilities' ] ;
1155+ if ( ! clientCaps ?. sampling ) {
1156+ return res . status ( 400 ) . json ( {
1157+ jsonrpc : '2.0' ,
1158+ id,
1159+ error : {
1160+ code : - 32003 ,
1161+ message : 'MissingRequiredClientCapabilityError' ,
1162+ data : { requiredCapabilities : [ 'sampling' ] }
1163+ }
1164+ } ) ;
1165+ }
1166+ return res . json ( {
1167+ jsonrpc : '2.0' ,
1168+ id,
1169+ result : { content : [ { type : 'text' , text : 'Success' } ] }
1170+ } ) ;
1171+ }
1172+ }
1173+
1174+ if (
1175+ [
1176+ 'initialize' ,
1177+ 'ping' ,
1178+ 'logging/setLevel' ,
1179+ 'resources/subscribe' ,
1180+ 'resources/unsubscribe'
1181+ ] . includes ( method )
1182+ ) {
1183+ return res . status ( 200 ) . json ( {
1184+ jsonrpc : '2.0' ,
1185+ id,
1186+ error : {
1187+ code : - 32601 ,
1188+ message : 'Method not found: removed stateful RPC'
1189+ }
1190+ } ) ;
1191+ }
1192+
1193+ return res . status ( 404 ) . json ( {
1194+ jsonrpc : '2.0' ,
1195+ id,
1196+ error : { code : - 32601 , message : 'Method not found' }
1197+ } ) ;
1198+ }
10561199
10571200 try {
10581201 let transport : StreamableHTTPServerTransport ;
0 commit comments