feat(mcp): expose progress resources and availability
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
import { setImmediate } from "node:timers/promises";
|
||||
|
||||
import { AnalysisCancelledError } from "../domain/errors.js";
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
import { mcpProgressReporter } from "./mcpProgress.js";
|
||||
|
||||
interface DerivedOperationContext {
|
||||
readonly mcpReq: {
|
||||
readonly signal: AbortSignal;
|
||||
readonly _meta?: { readonly progressToken?: string | number | undefined };
|
||||
notify(notification: {
|
||||
readonly method: "notifications/progress";
|
||||
readonly params: {
|
||||
readonly progressToken: string | number;
|
||||
readonly progress: number;
|
||||
readonly total?: number;
|
||||
readonly message?: string;
|
||||
};
|
||||
}): Promise<void>;
|
||||
};
|
||||
}
|
||||
|
||||
/** Run synchronous derived work with transport yields and a publication guard. */
|
||||
export const runDerivedOperation = async <Value>(
|
||||
context: DerivedOperationContext,
|
||||
operation: string,
|
||||
compute: () => Value,
|
||||
): Promise<Result<Value, AnalysisCancelledError>> => {
|
||||
const progress = mcpProgressReporter(context);
|
||||
await progress.report({
|
||||
phase: "prepare",
|
||||
completed: 0,
|
||||
total: 2,
|
||||
message: `Preparing ${operation}`,
|
||||
});
|
||||
await setImmediate();
|
||||
if (context.mcpReq.signal.aborted)
|
||||
return err(new AnalysisCancelledError(operation));
|
||||
const value = compute();
|
||||
await progress.report({
|
||||
phase: "compute",
|
||||
completed: 1,
|
||||
total: 2,
|
||||
message: `Computed ${operation}`,
|
||||
});
|
||||
await setImmediate();
|
||||
if (context.mcpReq.signal.aborted)
|
||||
return err(new AnalysisCancelledError(operation));
|
||||
await progress.report({
|
||||
phase: "complete",
|
||||
completed: 2,
|
||||
total: 2,
|
||||
message: `Completed ${operation}`,
|
||||
terminal: true,
|
||||
});
|
||||
return ok(value);
|
||||
};
|
||||
Reference in New Issue
Block a user