@@ -7,17 +7,6 @@ import {
77 OpenClawStreamableHTTPClientTransport ,
88} from "./mcp-http-transport.js" ;
99
10- function textContentLength ( content : unknown ) : number {
11- if ( ! Array . isArray ( content ) || content . length === 0 ) {
12- return 0 ;
13- }
14- const first : unknown = content [ 0 ] ;
15- if ( ! first || typeof first !== "object" || ! ( "type" in first ) || first . type !== "text" ) {
16- return 0 ;
17- }
18- return "text" in first && typeof first . text === "string" ? first . text . length : 0 ;
19- }
20-
2110function jsonResponse ( value : unknown , init ?: ResponseInit ) : Response {
2211 const headers = new Headers ( init ?. headers ) ;
2312 headers . set ( "content-type" , "application/json" ) ;
@@ -92,24 +81,16 @@ describe("OpenClaw MCP HTTP lifecycle adapters", () => {
9281 expect ( redacted ) . toContain ( "[redacted response body]" ) ;
9382 } ) ;
9483
95- it ( "rejects a chunked tools/list JSON body before the SDK parses it" , async ( ) => {
84+ it ( "rejects an oversized JSON message before the SDK parses it" , async ( ) => {
9685 const fetchMock = initializedFetch ( {
9786 onGet : ( ) => new Response ( null , { status : 405 } ) ,
9887 onPost : ( message ) => {
99- if ( message . method !== "tools/list " ) {
88+ if ( message . method !== "tools/call " ) {
10089 return new Response ( null , { status : 202 } ) ;
10190 }
10291 return mcpResultResponse (
10392 message . id ,
104- {
105- tools : [
106- {
107- name : "oversized" ,
108- description : OVERSIZED_MCP_TEXT ,
109- inputSchema : { type : "object" } ,
110- } ,
111- ] ,
112- } ,
93+ { content : [ { type : "text" , text : OVERSIZED_MCP_TEXT } ] } ,
11394 { contentType : 'application/json; note="text/event-stream"' } ,
11495 ) ;
11596 } ,
@@ -121,35 +102,27 @@ describe("OpenClaw MCP HTTP lifecycle adapters", () => {
121102
122103 try {
123104 await client . connect ( transport ) ;
124- const error = await client . listTools ( ) . then (
105+ const error = await client . callTool ( { name : "oversized" , arguments : { } } ) . then (
125106 ( ) => undefined ,
126107 ( reason : unknown ) => reason ,
127108 ) ;
128- expect ( String ( error ) ) . toContain ( "tools/list response exceeds 10485760 bytes" ) ;
109+ expect ( String ( error ) ) . toContain ( "HTTP response exceeds 10485760 bytes" ) ;
129110 } finally {
130111 await disposeMcpClient ( { client, transport, transportType : "streamable-http" } ) ;
131112 }
132113 } ) ;
133114
134- it ( "rejects an oversized streamed tools/list event before the SDK parses it" , async ( ) => {
115+ it ( "rejects an oversized SSE message before the SDK parses it" , async ( ) => {
135116 const fetchMock = initializedFetch ( {
136117 onGet : ( ) => new Response ( null , { status : 405 } ) ,
137118 onPost : ( message ) => {
138- if ( message . method !== "tools/list " ) {
119+ if ( message . method !== "tools/call " ) {
139120 return new Response ( null , { status : 202 } ) ;
140121 }
141122 const payload = JSON . stringify ( {
142123 jsonrpc : "2.0" ,
143124 id : message . id ,
144- result : {
145- tools : [
146- {
147- name : "oversized" ,
148- description : OVERSIZED_MCP_TEXT ,
149- inputSchema : { type : "object" } ,
150- } ,
151- ] ,
152- } ,
125+ result : { content : [ { type : "text" , text : OVERSIZED_MCP_TEXT } ] } ,
153126 } ) ;
154127 return new Response ( `event: message\ndata: ${ payload } \n\n` , {
155128 headers : { "content-type" : "text/event-stream" } ,
@@ -167,10 +140,12 @@ describe("OpenClaw MCP HTTP lifecycle adapters", () => {
167140
168141 try {
169142 await client . connect ( transport ) ;
170- const error = await client . listTools ( undefined , { timeout : 100 } ) . then (
171- ( ) => undefined ,
172- ( reason : unknown ) => reason ,
173- ) ;
143+ const error = await client
144+ . callTool ( { name : "oversized" , arguments : { } } , undefined , { timeout : 100 } )
145+ . then (
146+ ( ) => undefined ,
147+ ( reason : unknown ) => reason ,
148+ ) ;
174149 expect ( String ( error ) ) . toContain ( "Connection closed" ) ;
175150 expect ( onerror ) . toHaveBeenCalledWith (
176151 expect . objectContaining ( { message : expect . stringContaining ( "SSE event exceeds 10485760" ) } ) ,
@@ -180,106 +155,17 @@ describe("OpenClaw MCP HTTP lifecycle adapters", () => {
180155 }
181156 } ) ;
182157
183- it ( "limits only the pending legacy SSE tools/list event" , async ( ) => {
184- const encoder = new TextEncoder ( ) ;
185- let streamController : ReadableStreamDefaultController < Uint8Array > | undefined ;
186- let catalogRequestId : number | undefined ;
187- const fetchMock = vi . fn ( async ( _input : RequestInfo | URL , init ?: RequestInit ) => {
188- if ( ( init ?. method ?? "GET" ) === "GET" ) {
189- return new Response (
190- new ReadableStream < Uint8Array > ( {
191- start ( controller ) {
192- streamController = controller ;
193- controller . enqueue ( encoder . encode ( "event: endpoint\ndata: /messages\n\n" ) ) ;
194- } ,
195- } ) ,
196- { headers : { "content-type" : "text/event-stream" } } ,
197- ) ;
198- }
199- if ( typeof init ?. body !== "string" ) {
200- throw new Error ( "expected serialized JSON-RPC request body" ) ;
201- }
202- const message = JSON . parse ( init . body ) as { id ?: number ; method ?: string } ;
203- if ( message . method === "initialize" ) {
204- streamController ?. enqueue (
205- encoder . encode (
206- `event: message\ndata: ${ JSON . stringify ( {
207- jsonrpc : "2.0" ,
208- id : message . id ,
209- result : {
210- protocolVersion : "2025-06-18" ,
211- capabilities : { tools : { } } ,
212- serverInfo : { name : "fixture" , version : "1" } ,
213- } ,
214- } ) } \n\n`,
215- ) ,
216- ) ;
217- } else if ( message . method === "tools/list" ) {
218- catalogRequestId = message . id ;
219- } else if ( message . method === "tools/call" ) {
220- const payload = JSON . stringify ( {
221- jsonrpc : "2.0" ,
222- id : message . id ,
223- result : { content : [ { type : "text" , text : OVERSIZED_MCP_TEXT } ] } ,
224- } ) ;
225- streamController ?. enqueue ( encoder . encode ( `event: message\ndata: ${ payload } \n\n` ) ) ;
226- }
227- return new Response ( null , { status : 202 } ) ;
228- } ) ;
229- const transport = new OpenClawSSEClientTransport ( new URL ( "http://mcp.invalid/sse" ) , {
230- fetch : fetchMock ,
231- eventSourceInit : { fetch : fetchMock } ,
232- } ) ;
233- const client = new Client ( { name : "test" , version : "1" } ) ;
234- const onerror = vi . fn ( ) ;
235- // MCP clients expose callback properties rather than EventTarget listeners.
236- // oxlint-disable-next-line unicorn/prefer-add-event-listener
237- client . onerror = onerror ;
238-
239- try {
240- await client . connect ( transport ) ;
241- const catalogResult = client . listTools ( undefined , { timeout : 200 } ) . then (
242- ( ) => undefined ,
243- ( reason : unknown ) => reason ,
244- ) ;
245- await vi . waitFor ( ( ) => expect ( catalogRequestId ) . toBeDefined ( ) ) ;
246- const toolResult = await client . callTool ( { name : "large_result" , arguments : { } } ) ;
247- expect ( textContentLength ( toolResult . content ) ) . toBe ( OVERSIZED_MCP_TEXT . length ) ;
248- const catalogPayload = JSON . stringify ( {
249- jsonrpc : "2.0" ,
250- result : {
251- tools : [
252- {
253- name : "oversized" ,
254- description : OVERSIZED_MCP_TEXT ,
255- inputSchema : { type : "object" } ,
256- } ,
257- ] ,
258- } ,
259- id : catalogRequestId ,
260- } ) ;
261- streamController ?. enqueue ( encoder . encode ( `event: message\ndata: ${ catalogPayload } \n\n` ) ) ;
262- const error = await catalogResult ;
263- expect ( String ( error ) ) . toContain ( "Request timed out" ) ;
264- expect ( onerror ) . toHaveBeenCalledWith (
265- expect . objectContaining ( { message : expect . stringContaining ( "SSE event exceeds 10485760" ) } ) ,
266- ) ;
267- } finally {
268- await client . close ( ) ;
269- }
270- } ) ;
271-
272158 it . each ( [
273159 { label : "JSON" , stream : false } ,
274- { label : "streamed " , stream : true } ,
275- ] ) ( "keeps large non-catalog $label responses outside the catalog limit " , async ( { stream } ) => {
160+ { label : "SSE " , stream : true } ,
161+ ] ) ( "accepts an under-limit $label message " , async ( { stream } ) => {
276162 const fetchMock = initializedFetch ( {
277163 onGet : ( ) => new Response ( null , { status : 405 } ) ,
278164 onPost : ( message ) =>
279165 message . method === "tools/call"
280166 ? mcpResultResponse (
281167 message . id ,
282- { content : [ { type : "text" , text : OVERSIZED_MCP_TEXT } ] } ,
168+ { content : [ { type : "text" , text : "under-limit" } ] } ,
283169 { stream } ,
284170 )
285171 : new Response ( null , { status : 202 } ) ,
@@ -291,14 +177,14 @@ describe("OpenClaw MCP HTTP lifecycle adapters", () => {
291177
292178 try {
293179 await client . connect ( transport ) ;
294- const result = await client . callTool ( { name : "large_result " , arguments : { } } ) ;
295- expect ( textContentLength ( result . content ) ) . toBe ( OVERSIZED_MCP_TEXT . length ) ;
180+ const result = await client . callTool ( { name : "under_limit " , arguments : { } } ) ;
181+ expect ( result ) . toMatchObject ( { content : [ { type : "text" , text : "under-limit" } ] } ) ;
296182 } finally {
297183 await disposeMcpClient ( { client, transport, transportType : "streamable-http" } ) ;
298184 }
299185 } ) ;
300186
301- it ( "keeps a legacy SSE stream open beyond the cumulative catalog limit" , async ( ) => {
187+ it ( "keeps a legacy SSE stream open beyond the cumulative message limit" , async ( ) => {
302188 const encoder = new TextEncoder ( ) ;
303189 let streamController : ReadableStreamDefaultController < Uint8Array > | undefined ;
304190 const fetchMock = vi . fn ( async ( _input : RequestInfo | URL , init ?: RequestInit ) => {
@@ -319,23 +205,81 @@ describe("OpenClaw MCP HTTP lifecycle adapters", () => {
319205 fetch : fetchMock ,
320206 eventSourceInit : { fetch : fetchMock } ,
321207 } ) ;
322- const onmessage = vi . fn ( ) ;
208+ let messageCount = 0 ;
209+ let lastMessage : unknown ;
323210 // MCP transports expose callback properties rather than EventTarget listeners.
324211 // oxlint-disable-next-line unicorn/prefer-add-event-listener
325- transport . onmessage = onmessage ;
212+ transport . onmessage = ( message ) => {
213+ messageCount += 1 ;
214+ lastMessage = message ;
215+ } ;
326216
327217 try {
328218 await transport . start ( ) ;
329- const commentEvent = encoder . encode ( `: ${ "k" . repeat ( 32 * 1024 ) } \n\n` ) ;
219+ const notificationEvent = encoder . encode (
220+ `event: message\ndata: ${ JSON . stringify ( {
221+ jsonrpc : "2.0" ,
222+ method : "notifications/message" ,
223+ params : { level : "info" , data : "k" . repeat ( 32 * 1024 ) } ,
224+ } ) } \n\n`,
225+ ) ;
330226 for ( let index = 0 ; index < 321 ; index += 1 ) {
331- streamController ?. enqueue ( commentEvent ) ;
227+ streamController ?. enqueue ( notificationEvent ) ;
332228 }
333229 streamController ?. enqueue (
334230 encoder . encode ( 'event: message\ndata: {"jsonrpc":"2.0","id":7,"result":{}}\n\n' ) ,
335231 ) ;
336232
337- await vi . waitFor ( ( ) => expect ( onmessage ) . toHaveBeenCalledOnce ( ) ) ;
338- expect ( onmessage ) . toHaveBeenCalledWith ( { jsonrpc : "2.0" , id : 7 , result : { } } ) ;
233+ await vi . waitFor ( ( ) => expect ( messageCount ) . toBe ( 322 ) ) ;
234+ expect ( lastMessage ) . toEqual ( { jsonrpc : "2.0" , id : 7 , result : { } } ) ;
235+ } finally {
236+ await transport . close ( ) ;
237+ }
238+ } ) ;
239+
240+ it ( "closes legacy SSE after an oversized event without reconnecting" , async ( ) => {
241+ const encoder = new TextEncoder ( ) ;
242+ let streamController : ReadableStreamDefaultController < Uint8Array > | undefined ;
243+ let getCount = 0 ;
244+ const fetchMock = vi . fn ( async ( _input : RequestInfo | URL , init ?: RequestInit ) => {
245+ if ( ( init ?. method ?? "GET" ) !== "GET" ) {
246+ return new Response ( null , { status : 202 } ) ;
247+ }
248+ getCount += 1 ;
249+ return new Response (
250+ new ReadableStream < Uint8Array > ( {
251+ start ( controller ) {
252+ streamController = controller ;
253+ controller . enqueue ( encoder . encode ( "retry: 1\n\nevent: endpoint\ndata: /messages\n\n" ) ) ;
254+ } ,
255+ } ) ,
256+ { headers : { "content-type" : "text/event-stream" } } ,
257+ ) ;
258+ } ) ;
259+ const transport = new OpenClawSSEClientTransport ( new URL ( "http://mcp.invalid/sse" ) , {
260+ fetch : fetchMock ,
261+ eventSourceInit : { fetch : fetchMock } ,
262+ } ) ;
263+ const onclose = vi . fn ( ) ;
264+ const onerror = vi . fn ( ) ;
265+ // MCP transports expose callback properties rather than EventTarget listeners.
266+ // oxlint-disable-next-line unicorn/prefer-add-event-listener
267+ transport . onclose = onclose ;
268+ // oxlint-disable-next-line unicorn/prefer-add-event-listener
269+ transport . onerror = onerror ;
270+
271+ try {
272+ await transport . start ( ) ;
273+ streamController ?. enqueue ( encoder . encode ( `event: message\ndata: ${ OVERSIZED_MCP_TEXT } \n\n` ) ) ;
274+
275+ await vi . waitFor ( ( ) => expect ( onclose ) . toHaveBeenCalledOnce ( ) ) ;
276+ await new Promise < void > ( ( resolve ) => {
277+ setTimeout ( resolve , 20 ) ;
278+ } ) ;
279+ expect ( onerror ) . toHaveBeenCalledWith (
280+ expect . objectContaining ( { message : expect . stringContaining ( "SSE event exceeds 10485760" ) } ) ,
281+ ) ;
282+ expect ( getCount ) . toBe ( 1 ) ;
339283 } finally {
340284 await transport . close ( ) ;
341285 }
0 commit comments