- Run Prettier on the 60 core/ files this PR touched — the deterministic import rewrite had collapsed multi-line imports and left throws unwrapped; core/ is in no format gate so it slipped past CI. Now Prettier-clean. - Default-path output-schema violation now throws ProtocolError(ProtocolErrorCode.InvalidParams, …) instead of a bare Error, matching the prior v1 contract (SDK threw a typed McpError with a code) so downstream `instanceof`/`.code` branching keeps working. - Document the "assumes serial construction" invariant on pollTaskToolCall's progress-subscription revival (the `_progressHandlers` new-key heuristic). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
84 lines
2.4 KiB
TypeScript
84 lines
2.4 KiB
TypeScript
import type {
|
|
CreateMessageRequest,
|
|
CreateMessageResult,
|
|
} from "@modelcontextprotocol/client";
|
|
import { RELATED_TASK_META_KEY } from "@modelcontextprotocol/client";
|
|
|
|
export type { CreateMessageRequest, CreateMessageResult };
|
|
|
|
/**
|
|
* Data shape of a pending sampling request tracked by the InspectorClient.
|
|
* v2's state/screen layer consumes this interface; the runtime class below
|
|
* (SamplingCreateMessage) implements it.
|
|
*/
|
|
export interface InspectorPendingSampling {
|
|
id: string;
|
|
timestamp: Date;
|
|
request: CreateMessageRequest;
|
|
taskId?: string;
|
|
}
|
|
|
|
/**
|
|
* Represents a pending sampling request from the server
|
|
*/
|
|
export class SamplingCreateMessage {
|
|
public readonly id: string;
|
|
public readonly timestamp: Date;
|
|
public readonly request: CreateMessageRequest;
|
|
public readonly taskId?: string;
|
|
private resolvePromise?: (result: CreateMessageResult) => void;
|
|
private rejectPromise?: (error: Error) => void;
|
|
private onRemove: (id: string) => void;
|
|
|
|
constructor(
|
|
request: CreateMessageRequest,
|
|
resolve: (result: CreateMessageResult) => void,
|
|
reject: (error: Error) => void,
|
|
onRemove: (id: string) => void,
|
|
) {
|
|
this.onRemove = onRemove;
|
|
this.id = `sampling-${crypto.randomUUID()}`;
|
|
this.timestamp = new Date();
|
|
this.request = request;
|
|
// Extract taskId from request params metadata if present
|
|
const relatedTask = request.params?._meta?.[RELATED_TASK_META_KEY];
|
|
this.taskId = relatedTask?.taskId;
|
|
this.resolvePromise = resolve;
|
|
this.rejectPromise = reject;
|
|
}
|
|
|
|
/**
|
|
* Respond to the sampling request with a result
|
|
*/
|
|
async respond(result: CreateMessageResult): Promise<void> {
|
|
if (!this.resolvePromise) {
|
|
throw new Error("Request already resolved or rejected");
|
|
}
|
|
this.resolvePromise(result);
|
|
this.resolvePromise = undefined;
|
|
this.rejectPromise = undefined;
|
|
// Remove from pending list after responding
|
|
this.remove();
|
|
}
|
|
|
|
/**
|
|
* Reject the sampling request with an error
|
|
*/
|
|
async reject(error: Error): Promise<void> {
|
|
if (!this.rejectPromise) {
|
|
throw new Error("Request already resolved or rejected");
|
|
}
|
|
this.rejectPromise(error);
|
|
this.resolvePromise = undefined;
|
|
this.rejectPromise = undefined;
|
|
// Remove from pending list after rejecting
|
|
this.remove();
|
|
}
|
|
|
|
/**
|
|
* Remove this pending sample from the list
|
|
*/
|
|
remove(): void {
|
|
this.onRemove(this.id);
|
|
}
|
|
}
|