inspector/core/mcp/samplingCreateMessage.ts
Cliff Hall 82269e78cb
refactor(core): apply v1.5-port cleanups deferred from #1306 (#1316)
* refactor(core): apply v1.5-port cleanups deferred from #1306

Addresses issue #1309 — five small, mechanical cleanups deferred from the
v1.5 runtime port:

1. Collapse BrowserOAuthStorage / NodeOAuthStorage / RemoteOAuthStorage
   onto a shared OAuthStorageBase. Three ~130-line duplicates become
   thin adapter wrappers; behavior + public API unchanged.
2. Re-enable erasableSyntaxOnly in tsconfig.app.json by rewriting the
   seven (plus two the issue missed in sampling/elicitation create-
   message) parameter-property constructors as explicit field + assign.
3. Route the lone console.error in inspectorClient.ts through this.logger.
4. Replace `${Date.now()}-${Math.random()}` with crypto.randomUUID() at
   the five local-only ID sites (message tracking + sampling/elicit).
5. Pin the remote-server hono app generic at new Hono<Env>() and drop
   the streamSSE cast. Root cause of the cast was an unresolved hono
   subpath: the tsconfig "hono/*" wildcard didn't honor the exports
   map, so hono/streaming leaked to the root-hoisted hono@4.12.12 while
   hono itself resolved to clients/web/hono@4.12.18. Replaced the
   wildcard with an explicit hono/streaming entry in both tsconfig.app
   and tsconfig.test, removing the package-identity mismatch.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* refactor(core): address #1316 PR review feedback

- Drop the stranded no-op getServerState() read at the top of
  clearClientInformation in OAuthStorageBase (carried over verbatim
  from the three originals).
- Drop the crypto.randomUUID() guard at the task-ID site so all
  six in-scope sites are consistent; the fallback branch was dead
  code on every supported runtime.
- Remove the residual `c as Context` cast at the streamSSE call site
  in core/mcp/remote/node/server.ts — once both hono imports
  resolve to the same package the route-typed Context flows through
  to streamSSE with no cast at all.
- Move the new `options` / `config` field declarations in
  RemoteClientTransport up with the rest of the field block, matching
  the layout used by the other five rewritten classes in this PR.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-17 19:15:48 -04:00

84 lines
2.4 KiB
TypeScript

import type {
CreateMessageRequest,
CreateMessageResult,
} from "@modelcontextprotocol/sdk/types.js";
import { RELATED_TASK_META_KEY } from "@modelcontextprotocol/sdk/types.js";
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);
}
}