Skip to content

Commit 275175c

Browse files
authored
Merge pull request #3816 from galagaevdc/fix/everything-elicitation-http-transport
fix(everything): use relatedTask option to enable elicitation over HTTP (#3228)
2 parents 25e5f3a + 67a71f1 commit 275175c

2 files changed

Lines changed: 99 additions & 20 deletions

File tree

‎src/everything/__tests__/tools.test.ts‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
} from '../tools/trigger-url-elicitation.js';
2020
import { registerGetRootsListTool } from '../tools/get-roots-list.js';
2121
import { registerGZipFileAsResourceTool } from '../tools/gzip-file-as-resource.js';
22+
import { registerSimulateResearchQueryTool } from '../tools/simulate-research-query.js';
2223

2324
// Helper to capture registered tool handlers
2425
function createMockServer() {
@@ -1054,6 +1055,90 @@ describe('Tools', () => {
10541055
});
10551056
});
10561057

1058+
describe('simulate-research-query', () => {
1059+
function createMockServerWithTasks() {
1060+
const taskHandlers: Record<string, any> = {};
1061+
const mockServer = {
1062+
experimental: {
1063+
tasks: {
1064+
registerToolTask: vi.fn((_name: string, _config: any, handler: any) => {
1065+
Object.assign(taskHandlers, handler);
1066+
}),
1067+
},
1068+
},
1069+
server: { getClientCapabilities: vi.fn(() => ({ elicitation: {} })) },
1070+
} as unknown as McpServer;
1071+
return { mockServer, taskHandlers };
1072+
}
1073+
1074+
function createMockTaskStore(taskId: string) {
1075+
return {
1076+
createTask: vi.fn().mockResolvedValue({
1077+
taskId,
1078+
status: 'working',
1079+
createdAt: new Date().toISOString(),
1080+
lastUpdatedAt: new Date().toISOString(),
1081+
ttl: 300000,
1082+
pollInterval: 1000,
1083+
}),
1084+
updateTaskStatus: vi.fn().mockResolvedValue(undefined),
1085+
storeTaskResult: vi.fn().mockResolvedValue(undefined),
1086+
getTask: vi.fn(),
1087+
getTaskResult: vi.fn(),
1088+
};
1089+
}
1090+
1091+
it('should pass relatedTask to sendRequest when elicitation is triggered', async () => {
1092+
vi.useFakeTimers();
1093+
1094+
const { mockServer, taskHandlers } = createMockServerWithTasks();
1095+
registerSimulateResearchQueryTool(mockServer);
1096+
1097+
const mockTaskStore = createMockTaskStore('task-abc');
1098+
const mockSendRequest = vi.fn().mockResolvedValue({
1099+
action: 'accept',
1100+
content: { interpretation: 'technical' },
1101+
});
1102+
1103+
await taskHandlers.createTask(
1104+
{ topic: 'python', ambiguous: true },
1105+
{ taskStore: mockTaskStore, sendRequest: mockSendRequest }
1106+
);
1107+
1108+
await vi.runAllTimersAsync();
1109+
vi.useRealTimers();
1110+
1111+
expect(mockSendRequest).toHaveBeenCalledWith(
1112+
expect.objectContaining({ method: 'elicitation/create' }),
1113+
expect.anything(),
1114+
expect.objectContaining({ relatedTask: { taskId: 'task-abc' } })
1115+
);
1116+
});
1117+
1118+
it('should complete without elicitation for non-ambiguous query', async () => {
1119+
vi.useFakeTimers();
1120+
1121+
const { mockServer, taskHandlers } = createMockServerWithTasks();
1122+
registerSimulateResearchQueryTool(mockServer);
1123+
1124+
const mockTaskStore = createMockTaskStore('task-def');
1125+
const mockSendRequest = vi.fn();
1126+
1127+
await taskHandlers.createTask(
1128+
{ topic: 'python', ambiguous: false },
1129+
{ taskStore: mockTaskStore, sendRequest: mockSendRequest }
1130+
);
1131+
1132+
await vi.runAllTimersAsync();
1133+
vi.useRealTimers();
1134+
1135+
expect(mockSendRequest).not.toHaveBeenCalled();
1136+
expect(mockTaskStore.storeTaskResult).toHaveBeenCalledWith(
1137+
'task-def', 'completed', expect.anything()
1138+
);
1139+
});
1140+
});
1141+
10571142
describe('gzip-file-as-resource', () => {
10581143
it('should compress data URI and return resource link', async () => {
10591144
const registeredResources: any[] = [];

‎src/everything/tools/simulate-research-query.ts‎

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,10 @@ const researchStates = new Map<string, ResearchState>();
4747
/**
4848
* Runs the background research process.
4949
* Updates task status as it progresses through stages.
50-
* If clarification is needed, attempts elicitation via sendRequest.
51-
*
52-
* Note: Elicitation only works on STDIO transport. On HTTP transport,
53-
* sendRequest will fail and the task will use a default interpretation.
54-
* Full HTTP support requires SDK PR #1210's elicitInputStream API.
50+
* If clarification is needed, sends elicitation via sendRequest with relatedTask,
51+
* which queues the request in the task message queue. The SDK delivers it through
52+
* the tasks/result stream when the client calls tasks/result (per spec input_required flow).
53+
* This works on all transports (STDIO, SSE, Streamable HTTP).
5554
*/
5655
async function runResearchProcess(
5756
taskId: string,
@@ -94,7 +93,7 @@ async function runResearchProcess(
9493
);
9594

9695
try {
97-
// Try elicitation via sendRequest (works on STDIO, fails on HTTP)
96+
// relatedTask queues elicitation via task message queue → delivered through tasks/result on all transports
9897
const elicitResult: ElicitResult = await sendRequest(
9998
{
10099
method: "elicitation/create",
@@ -115,7 +114,8 @@ async function runResearchProcess(
115114
},
116115
},
117116
},
118-
ElicitResultSchema
117+
ElicitResultSchema,
118+
{ relatedTask: { taskId } }
119119
);
120120

121121
// Process elicitation response
@@ -129,14 +129,12 @@ async function runResearchProcess(
129129
state.clarification = "User cancelled - using default interpretation";
130130
}
131131
} catch (error) {
132-
// Elicitation failed (likely HTTP transport without streaming support)
133-
// Use default interpretation and continue - task should still complete
132+
// Elicitation failed - use default interpretation and continue
134133
console.warn(
135-
`Elicitation failed for task ${taskId} (HTTP transport?):`,
134+
`Elicitation failed for task ${taskId}:`,
136135
error instanceof Error ? error.message : String(error)
137136
);
138-
state.clarification =
139-
"technical (default - elicitation unavailable on HTTP)";
137+
state.clarification = "technical (default - elicitation unavailable)";
140138
}
141139

142140
// Resume with working status (spec SHOULD)
@@ -199,12 +197,8 @@ ${
199197
When the query was ambiguous, the server sent an \`elicitation/create\` request
200198
to the client. The task status changed to \`input_required\` while awaiting user input.
201199
${
202-
state.clarification.includes("unavailable on HTTP")
203-
? `
204-
**Note:** Elicitation was skipped because this server is running over HTTP transport.
205-
The current SDK's \`sendRequest\` only works over STDIO. Full HTTP elicitation support
206-
requires SDK PR #1210's streaming \`elicitInputStream\` API.
207-
`
200+
state.clarification.includes("unavailable")
201+
? `**Note:** Elicitation failed and a default interpretation was used.`
208202
: `After receiving clarification ("${state.clarification}"), the task resumed processing and completed.`
209203
}
210204
`
@@ -215,7 +209,7 @@ requires SDK PR #1210's streaming \`elicitInputStream\` API.
215209
- \`statusMessage\` provides human-readable progress updates
216210
- Tasks have TTL (time-to-live) for automatic cleanup
217211
- \`pollInterval\` suggests how often to check status
218-
- Elicitation requests can be sent directly during task execution
212+
- Elicitation requests use \`relatedTask\` to queue via tasks/result (works on all transports)
219213
220214
*This is a simulated research report from the Everything MCP Server.*
221215
`;
@@ -285,7 +279,7 @@ export const registerSimulateResearchQueryTool = (server: McpServer) => {
285279
researchStates.set(task.taskId, state);
286280

287281
// Start background research (don't await - runs asynchronously)
288-
// Pass sendRequest for elicitation (works on STDIO, gracefully degrades on HTTP)
282+
// Pass sendRequest for elicitation (queued via task message queue, works on all transports)
289283
runResearchProcess(
290284
task.taskId,
291285
validatedArgs,

0 commit comments

Comments
 (0)