@@ -11,7 +11,19 @@ const server = new McpServer({
1111 version : "0.2.0" ,
1212} ) ;
1313
14- const thinkingServer = new SequentialThinkingServer ( ) ;
14+ const thinkingSessions = new Map < string , SequentialThinkingServer > ( ) ;
15+
16+ function getOrCreateThinkingServer ( sessionId ?: string ) : SequentialThinkingServer {
17+ if ( ! sessionId ) {
18+ return new SequentialThinkingServer ( ) ;
19+ }
20+
21+ if ( ! thinkingSessions . has ( sessionId ) ) {
22+ thinkingSessions . set ( sessionId , new SequentialThinkingServer ( ) ) ;
23+ }
24+
25+ return thinkingSessions . get ( sessionId ) ! ;
26+ }
1527
1628server . registerTool (
1729 "sequentialthinking" ,
@@ -91,13 +103,13 @@ You should:
91103 } ,
92104 } ,
93105 async ( args ) => {
106+ const thinkingServer = getOrCreateThinkingServer ( ) ;
94107 const result = thinkingServer . processThought ( args ) ;
95108
96109 if ( result . isError ) {
97- return result ;
110+ return { content : result . content } ;
98111 }
99112
100- // Parse the JSON response to get structured content
101113 const parsedContent = JSON . parse ( result . content [ 0 ] . text ) ;
102114
103115 return {
@@ -111,28 +123,69 @@ async function runServer() {
111123 if ( process . env . MCP_TRANSPORT === 'http' ) {
112124 const { createServer } = await import ( 'http' ) ;
113125 const transports : Record < string , StreamableHTTPServerTransport > = { } ;
126+ const sessionTimeouts : Record < string , NodeJS . Timeout > = { } ;
127+ const SESSION_TIMEOUT_MS = 30 * 60 * 1000 ; // 30 minutes
128+ const MAX_BODY_SIZE = 10 * 1024 * 1024 ; // 10MB
129+
130+ function cleanupSession ( sid : string ) {
131+ delete transports [ sid ] ;
132+ thinkingSessions . delete ( sid ) ;
133+ if ( sessionTimeouts [ sid ] ) {
134+ clearTimeout ( sessionTimeouts [ sid ] ) ;
135+ delete sessionTimeouts [ sid ] ;
136+ }
137+ }
138+
139+ function resetSessionTimeout ( sid : string ) {
140+ if ( sessionTimeouts [ sid ] ) {
141+ clearTimeout ( sessionTimeouts [ sid ] ) ;
142+ }
143+ sessionTimeouts [ sid ] = setTimeout ( ( ) => cleanupSession ( sid ) , SESSION_TIMEOUT_MS ) ;
144+ }
114145
115146 const httpServer = createServer ( async ( req , res ) => {
116147 const sessionId = req . headers [ 'mcp-session-id' ] as string | undefined ;
117148
118149 if ( req . method === 'POST' ) {
119- let body = '' ;
120- req . on ( 'data' , chunk => body += chunk ) ;
150+ const chunks : Buffer [ ] = [ ] ;
151+ let totalSize = 0 ;
152+
153+ req . on ( 'data' , chunk => {
154+ totalSize += chunk . length ;
155+ if ( totalSize > MAX_BODY_SIZE ) {
156+ req . destroy ( ) ;
157+ res . writeHead ( 413 ) ;
158+ res . end ( 'Request body too large' ) ;
159+ return ;
160+ }
161+ chunks . push ( chunk ) ;
162+ } ) ;
163+
121164 req . on ( 'end' , async ( ) => {
122- const parsedBody = body . trim ( ) ? JSON . parse ( body ) : undefined ;
165+ let parsedBody ;
166+ try {
167+ const body = Buffer . concat ( chunks ) . toString ( ) ;
168+ parsedBody = body . trim ( ) ? JSON . parse ( body ) : undefined ;
169+ } catch ( error ) {
170+ res . writeHead ( 400 ) ;
171+ res . end ( 'Invalid JSON' ) ;
172+ return ;
173+ }
123174
124175 let transport : StreamableHTTPServerTransport ;
125176 if ( sessionId && transports [ sessionId ] ) {
126177 transport = transports [ sessionId ] ;
178+ resetSessionTimeout ( sessionId ) ;
127179 } else if ( ! sessionId ) {
128180 transport = new StreamableHTTPServerTransport ( {
129181 sessionIdGenerator : ( ) => crypto . randomUUID ( ) ,
130182 onsessioninitialized : ( sid ) => {
131183 transports [ sid ] = transport ;
184+ resetSessionTimeout ( sid ) ;
132185 console . error ( 'Session initialized:' , sid ) ;
133186 } ,
134187 onsessionclosed : ( sid ) => {
135- delete transports [ sid ] ;
188+ cleanupSession ( sid ) ;
136189 console . error ( 'Session closed:' , sid ) ;
137190 }
138191 } ) ;
@@ -151,6 +204,7 @@ async function runServer() {
151204 res . end ( 'Invalid or missing session ID' ) ;
152205 return ;
153206 }
207+ resetSessionTimeout ( sessionId ) ;
154208 await transports [ sessionId ] . handleRequest ( req , res ) ;
155209 } else if ( req . method === 'DELETE' ) {
156210 if ( ! sessionId || ! transports [ sessionId ] ) {
@@ -159,6 +213,7 @@ async function runServer() {
159213 return ;
160214 }
161215 await transports [ sessionId ] . handleRequest ( req , res ) ;
216+ cleanupSession ( sessionId ) ;
162217 } else {
163218 res . writeHead ( 405 ) ;
164219 res . end ( 'Method not allowed' ) ;
0 commit comments