Skip to content

Commit 486e8ed

Browse files
committed
Store task result before attempting to respond to client
1 parent 0bf2b42 commit 486e8ed

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/shared/protocol.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -613,21 +613,24 @@ export abstract class Protocol<SendRequestT extends Request, SendNotificationT e
613613
return;
614614
}
615615

616-
// Send the response
617-
await capturedTransport?.send({
618-
result,
619-
jsonrpc: '2.0',
620-
id: request.id
621-
});
622-
623-
// Store the result if this was a task-based request
616+
// Store the result if this was a task-based request.
617+
// This needs to be done before attempting to send the response so that
618+
// we can handle the case where the client has disconnected but will come
619+
// back to retrieve the result later.
624620
if (taskMetadata && this._taskStore) {
625621
try {
626622
await this._taskStore.storeTaskResult(taskMetadata.taskId, result);
627623
} catch (error) {
628624
throw new McpError(ErrorCode.InternalError, `Failed to store task result: ${error}`);
629625
}
630626
}
627+
628+
// Send the response
629+
await capturedTransport?.send({
630+
result,
631+
jsonrpc: '2.0',
632+
id: request.id
633+
});
631634
},
632635
async error => {
633636
if (abortController.signal.aborted) {

0 commit comments

Comments
 (0)