fix(everything): implement graceful HTTP elicitation degradation

Implement graceful degradation for elicitation on HTTP transport:
- STDIO: Full elicitation works via sendRequest
- HTTP: Catches elicitation failure, uses default interpretation
- Task completes successfully on both transports

simulate-research-query now uses try-catch around sendRequest and
includes explanatory message when elicitation is skipped on HTTP.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
olaservo
2026-01-18 19:19:32 -07:00
parent 1cda86effb
commit 5156cff9dc
4 changed files with 87 additions and 51 deletions

View File

@@ -2,7 +2,7 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { import {
InMemoryTaskStore, InMemoryTaskStore,
InMemoryTaskMessageQueue, InMemoryTaskMessageQueue,
} from "@modelcontextprotocol/sdk/experimental"; } from "@modelcontextprotocol/sdk/experimental/tasks";
import { import {
setSubscriptionHandlers, setSubscriptionHandlers,
stopSimulatedResourceUpdates, stopSimulatedResourceUpdates,

View File

@@ -4,11 +4,10 @@ import {
CallToolResult, CallToolResult,
GetTaskResult, GetTaskResult,
Task, Task,
ElicitResult,
ElicitResultSchema, ElicitResultSchema,
ServerRequest,
} from "@modelcontextprotocol/sdk/types.js"; } from "@modelcontextprotocol/sdk/types.js";
import { CreateTaskResult } from "@modelcontextprotocol/sdk/experimental"; import { CreateTaskResult } from "@modelcontextprotocol/sdk/experimental/tasks";
import type { AnySchema, SchemaOutput } from "@modelcontextprotocol/sdk/server/zod-compat.js";
// Tool input schema // Tool input schema
const SimulateResearchQuerySchema = z.object({ const SimulateResearchQuerySchema = z.object({
@@ -48,7 +47,11 @@ const researchStates = new Map<string, ResearchState>();
/** /**
* Runs the background research process. * Runs the background research process.
* Updates task status as it progresses through stages. * Updates task status as it progresses through stages.
* If clarification is needed, sends elicitation request directly. * If clarification is needed, attempts elicitation via sendRequest.
*
* Note: Elicitation only works on STDIO transport. On HTTP transport,
* sendRequest will fail and the task will use a default interpretation.
* Full HTTP support requires SDK PR #1210's elicitInputStream API.
*/ */
async function runResearchProcess( async function runResearchProcess(
taskId: string, taskId: string,
@@ -65,11 +68,8 @@ async function runResearchProcess(
result: CallToolResult result: CallToolResult
) => Promise<void>; ) => Promise<void>;
}, },
sendRequest: <U extends AnySchema>( // eslint-disable-next-line @typescript-eslint/no-explicit-any
request: ServerRequest, sendRequest: any
resultSchema: U,
options?: { timeout?: number }
) => Promise<SchemaOutput<U>>
): Promise<void> { ): Promise<void> {
const state = researchStates.get(taskId); const state = researchStates.get(taskId);
if (!state) return; if (!state) return;
@@ -86,56 +86,63 @@ async function runResearchProcess(
// At synthesis stage (index 2), check if clarification is needed // At synthesis stage (index 2), check if clarification is needed
if (i === 2 && state.ambiguous && !state.clarification) { if (i === 2 && state.ambiguous && !state.clarification) {
// Update status to show we're requesting input // Update status to show we're requesting input (spec SHOULD)
await taskStore.updateTaskStatus( await taskStore.updateTaskStatus(
taskId, taskId,
"input_required", "input_required",
`Found multiple interpretations for "${state.topic}". Requesting clarification...` `Found multiple interpretations for "${state.topic}". Requesting clarification...`
); );
// Send elicitation directly and await response try {
const elicitationResult = await sendRequest( // Try elicitation via sendRequest (works on STDIO, fails on HTTP)
{ const elicitResult: ElicitResult = await sendRequest(
method: "elicitation/create", {
params: { method: "elicitation/create",
message: `The research query "${state.topic}" could have multiple interpretations. Please clarify what you're looking for:`, params: {
requestedSchema: { message: `The research query "${state.topic}" could have multiple interpretations. Please clarify what you're looking for:`,
type: "object", requestedSchema: {
properties: { type: "object",
interpretation: { properties: {
type: "string", interpretation: {
title: "Clarification", type: "string",
description: "Which interpretation of the topic do you mean?", title: "Clarification",
oneOf: getInterpretationsForTopic(state.topic), description: "Which interpretation of the topic do you mean?",
oneOf: getInterpretationsForTopic(state.topic),
},
}, },
required: ["interpretation"],
}, },
required: ["interpretation"],
}, },
}, },
}, ElicitResultSchema
ElicitResultSchema, );
{ timeout: 5 * 60 * 1000 /* 5 minutes */ }
);
// Process elicitation response // Process elicitation response
if ( if (elicitResult.action === "accept" && elicitResult.content) {
elicitationResult.action === "accept" && state.clarification =
elicitationResult.content (elicitResult.content as { interpretation?: string })
) { .interpretation || "User accepted without selection";
} else if (elicitResult.action === "decline") {
state.clarification = "User declined - using default interpretation";
} else {
state.clarification = "User cancelled - using default interpretation";
}
} catch (error) {
// Elicitation failed (likely HTTP transport without streaming support)
// Use default interpretation and continue - task should still complete
console.warn(
`Elicitation failed for task ${taskId} (HTTP transport?):`,
error instanceof Error ? error.message : String(error)
);
state.clarification = state.clarification =
(elicitationResult.content as { interpretation?: string }) "technical (default - elicitation unavailable on HTTP)";
.interpretation || "User accepted without selection";
} else if (elicitationResult.action === "decline") {
state.clarification = "User declined - using default interpretation";
} else {
state.clarification = "User cancelled - using default interpretation";
} }
// Resume with working status // Resume with working status (spec SHOULD)
await taskStore.updateTaskStatus( await taskStore.updateTaskStatus(
taskId, taskId,
"working", "working",
`Received clarification: "${state.clarification}". Continuing...` `Continuing with interpretation: "${state.clarification}"...`
); );
// Continue processing (no return - just keep going through the loop) // Continue processing (no return - just keep going through the loop)
@@ -185,9 +192,12 @@ This tool demonstrates MCP's task-based execution pattern for long-running opera
${state.clarification ? `**Elicitation Flow:** ${state.clarification ? `**Elicitation Flow:**
When the query was ambiguous, the server sent an \`elicitation/create\` request When the query was ambiguous, the server sent an \`elicitation/create\` request
directly to the client. The task status changed to \`input_required\` while to the client. The task status changed to \`input_required\` while awaiting user input.
awaiting user input. After receiving clarification ("${state.clarification}"), ${state.clarification.includes("unavailable on HTTP") ? `
the task resumed processing and completed. **Note:** Elicitation was skipped because this server is running over HTTP transport.
The current SDK's \`sendRequest\` only works over STDIO. Full HTTP elicitation support
requires SDK PR #1210's streaming \`elicitInputStream\` API.
` : `After receiving clarification ("${state.clarification}"), the task resumed processing and completed.`}
` : ""} ` : ""}
**Key Concepts:** **Key Concepts:**
- Tasks enable "call now, fetch later" patterns - Tasks enable "call now, fetch later" patterns
@@ -258,7 +268,7 @@ export const registerSimulateResearchQueryTool = (server: McpServer) => {
researchStates.set(task.taskId, state); researchStates.set(task.taskId, state);
// Start background research (don't await - runs asynchronously) // Start background research (don't await - runs asynchronously)
// Pass sendRequest so elicitation can be sent directly from the background process // Pass sendRequest for elicitation (works on STDIO, gracefully degrades on HTTP)
runResearchProcess( runResearchProcess(
task.taskId, task.taskId,
validatedArgs, validatedArgs,

View File

@@ -1,6 +1,5 @@
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { ElicitResultSchema } from "@modelcontextprotocol/sdk/types.js"; import { ElicitResultSchema, CallToolResult } from "@modelcontextprotocol/sdk/types.js";
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
// Tool configuration // Tool configuration
const name = "trigger-elicitation-request"; const name = "trigger-elicitation-request";

View File

@@ -1,10 +1,37 @@
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js"; import { StreamableHTTPServerTransport, EventStore } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
import { InMemoryEventStore } from "@modelcontextprotocol/sdk/examples/shared/inMemoryEventStore.js";
import express, { Request, Response } from "express"; import express, { Request, Response } from "express";
import { createServer } from "../server/index.js"; import { createServer } from "../server/index.js";
import { randomUUID } from "node:crypto"; import { randomUUID } from "node:crypto";
import cors from "cors"; import cors from "cors";
// Simple in-memory event store for SSE resumability
class InMemoryEventStore implements EventStore {
private events: Map<string, { streamId: string; message: unknown }> = new Map();
async storeEvent(streamId: string, message: unknown): Promise<string> {
const eventId = randomUUID();
this.events.set(eventId, { streamId, message });
return eventId;
}
async replayEventsAfter(
lastEventId: string,
{ send }: { send: (eventId: string, message: unknown) => Promise<void> }
): Promise<string> {
const entries = Array.from(this.events.entries());
const startIndex = entries.findIndex(([id]) => id === lastEventId);
if (startIndex === -1) return lastEventId;
let lastId: string = lastEventId;
for (let i = startIndex + 1; i < entries.length; i++) {
const [eventId, { message }] = entries[i];
await send(eventId, message);
lastId = eventId;
}
return lastId;
}
}
console.log("Starting Streamable HTTP server..."); console.log("Starting Streamable HTTP server...");
// Express app with permissive CORS for testing with Inspector direct connect mode // Express app with permissive CORS for testing with Inspector direct connect mode