@cyanheads/toolkit-mcp-server
Version:
MCP server providing system, network, geolocation, generator, datetime, and security tools
82 lines (81 loc) • 2.65 kB
JavaScript
export class ProgressReporter {
current = 0;
server;
token;
total;
title;
unit;
constructor(server, token, options) {
this.server = server;
this.token = token;
this.total = options.total;
this.title = options.title;
this.unit = options.unit || 'items';
}
async report(increment = 1, message) {
this.current += increment;
try {
await this.server.notification({
method: 'notifications/progress',
params: {
token: this.token,
title: this.title,
message: message || this.getDefaultMessage(),
current: this.current,
total: this.total,
unit: this.unit
}
});
}
catch (error) {
// Silently handle notification errors to not interrupt the main operation
console.error('Failed to send progress notification:', error);
}
}
getDefaultMessage() {
if (this.total) {
const percentage = Math.round((this.current / this.total) * 100);
return `Processing ${this.current}/${this.total} ${this.unit} (${percentage}%)`;
}
return `Processed ${this.current} ${this.unit}`;
}
async complete(message) {
try {
await this.server.notification({
method: 'notifications/progress/complete',
params: {
token: this.token,
message: message || `Completed processing ${this.current} ${this.unit}`
}
});
}
catch (error) {
console.error('Failed to send completion notification:', error);
}
}
async error(error) {
try {
await this.server.notification({
method: 'notifications/progress/error',
params: {
token: this.token,
error: error.message
}
});
}
catch (notificationError) {
console.error('Failed to send error notification:', notificationError);
}
}
}
export async function withProgress(server, options, operation) {
const token = `progress-${Date.now()}-${Math.random().toString(36).slice(2)}`;
const progress = new ProgressReporter(server, token, options);
try {
return await operation(progress);
}
catch (error) {
await progress.error(error instanceof Error ? error : new Error('Unknown error'));
throw error;
}
}