Skip to content

Commit cbe9279

Browse files
committed
add new tool for testing resumability - leave the rest normal
1 parent 77a284c commit cbe9279

File tree

1 file changed

+51
-6
lines changed

1 file changed

+51
-6
lines changed

src/examples/client/simpleStreamableHttp.ts

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ function printHelp(): void {
5555
console.log(' greet [name] - Call the greet tool');
5656
console.log(' multi-greet [name] - Call the multi-greet tool with notifications');
5757
console.log(' start-notifications [interval] [count] - Start periodic notifications');
58+
console.log(' run-notifications-tool-with-resumability [interval] [count] - Run notification tool with resumability');
5859
console.log(' list-prompts - List available prompts');
5960
console.log(' get-prompt [name] [args] - Get a prompt with optional JSON arguments');
6061
console.log(' list-resources - List available resources');
@@ -121,6 +122,13 @@ function commandLoop(): void {
121122
break;
122123
}
123124

125+
case 'run-notifications-tool-with-resumability': {
126+
const interval = args[1] ? parseInt(args[1], 10) : 2000;
127+
const count = args[2] ? parseInt(args[2], 10) : 10;
128+
await runNotificationsToolWithResumability(interval, count);
129+
break;
130+
}
131+
124132
case 'list-prompts':
125133
await listPrompts();
126134
break;
@@ -333,12 +341,7 @@ async function callTool(name: string, args: Record<string, unknown>): Promise<vo
333341
};
334342

335343
console.log(`Calling tool '${name}' with args:`, args);
336-
const onLastEventIdUpdate = (event: string) => {
337-
notificationsToolLastEventId = event;
338-
};
339-
const result = await client.request(request, CallToolResultSchema, {
340-
resumptionToken: notificationsToolLastEventId, onresumptiontoken: onLastEventIdUpdate
341-
});
344+
const result = await client.request(request, CallToolResultSchema);
342345

343346
console.log('Tool result:');
344347
result.content.forEach(item => {
@@ -353,6 +356,7 @@ async function callTool(name: string, args: Record<string, unknown>): Promise<vo
353356
}
354357
}
355358

359+
356360
async function callGreetTool(name: string): Promise<void> {
357361
await callTool('greet', { name });
358362
}
@@ -367,6 +371,47 @@ async function startNotifications(interval: number, count: number): Promise<void
367371
await callTool('start-notification-stream', { interval, count });
368372
}
369373

374+
async function runNotificationsToolWithResumability(interval: number, count: number): Promise<void> {
375+
if (!client) {
376+
console.log('Not connected to server.');
377+
return;
378+
}
379+
380+
try {
381+
console.log(`Starting notification stream with resumability: interval=${interval}ms, count=${count || 'unlimited'}`);
382+
console.log(`Using resumption token: ${notificationsToolLastEventId || 'none'}`);
383+
384+
const request: CallToolRequest = {
385+
method: 'tools/call',
386+
params: {
387+
name: 'start-notification-stream',
388+
arguments: { interval, count }
389+
}
390+
};
391+
392+
const onLastEventIdUpdate = (event: string) => {
393+
notificationsToolLastEventId = event;
394+
console.log(`Updated resumption token: ${event}`);
395+
};
396+
397+
const result = await client.request(request, CallToolResultSchema, {
398+
resumptionToken: notificationsToolLastEventId,
399+
onresumptiontoken: onLastEventIdUpdate
400+
});
401+
402+
console.log('Tool result:');
403+
result.content.forEach(item => {
404+
if (item.type === 'text') {
405+
console.log(` ${item.text}`);
406+
} else {
407+
console.log(` ${item.type} content:`, item);
408+
}
409+
});
410+
} catch (error) {
411+
console.log(`Error starting notification stream: ${error}`);
412+
}
413+
}
414+
370415
async function listPrompts(): Promise<void> {
371416
if (!client) {
372417
console.log('Not connected to server.');

0 commit comments

Comments
 (0)