Skip to content

Commit ce264a5

Browse files
albatrossflyon-coderChris Brown
andauthored
refactor: Extract getTaskOrThrow helper for task handlers (#1069)
## Summary - Addresses one item from the checklist in #1066 ("getTaskOrThrow helper"). - `GetTaskRequestSchema`, `GetTaskPayloadRequestSchema`, and `CancelTaskRequestSchema` each duplicated the same "fetch task, softFail + throw McpError if missing" block, differing only in the log tag used for the softFail call. - Extracted a single private `getTaskOrThrow(taskId, mcpSessionId, logTag)` helper and call it from all three handlers. No behavior change — same softFail log call, same error code/message, same control flow, just de-duplicated. ## Test plan - [x] `pnpm run type-check` - [x] `pnpm run lint` (0 warnings/errors) - [x] `pnpm run test:unit` (1023 passed, 2 skipped) - [x] `pnpm run format` - [x] `pnpm run check:agents` Co-authored-by: Chris Brown <albatrossflyon1@gmail.com>
1 parent 2b33c07 commit ce264a5

1 file changed

Lines changed: 17 additions & 18 deletions

File tree

src/mcp/server.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import type {
1919
InitializeResult,
2020
Notification,
2121
Request,
22+
Task,
2223
TaskStatusNotification,
2324
} from '@modelcontextprotocol/sdk/types.js';
2425
import {
@@ -695,6 +696,19 @@ export class ActorsMcpServer {
695696
});
696697
}
697698

699+
/**
700+
* Fetches a task by ID, softFail-logging and throwing a client-facing McpError if it doesn't exist.
701+
*/
702+
private async getTaskOrThrow(taskId: string, mcpSessionId: string | undefined, logTag: string): Promise<Task> {
703+
const task = await this.taskStore.getTask(taskId, mcpSessionId);
704+
if (!task) {
705+
// Client error (invalid/unknown taskId) — softFail to avoid polluting error logs.
706+
log.softFail(`[${logTag}] Task not found`, { taskId, mcpSessionId, statusCode: 404 });
707+
throw new McpError(ErrorCode.InvalidParams, `Task "${taskId}" not found`);
708+
}
709+
return task;
710+
}
711+
698712
/**
699713
* Sets up MCP request handlers for long-running tasks.
700714
*/
@@ -717,12 +731,7 @@ export class ActorsMcpServer {
717731
const { taskId } = params;
718732
const mcpSessionId = params._meta?.mcpSessionId;
719733
log.debug('[GetTaskRequestSchema] Getting task status', { taskId, mcpSessionId });
720-
const task = await this.taskStore.getTask(taskId, mcpSessionId);
721-
if (task) return task;
722-
723-
// Client error (invalid/unknown taskId) — softFail to avoid polluting error logs.
724-
log.softFail('[GetTaskRequestSchema] Task not found', { taskId, mcpSessionId, statusCode: 404 });
725-
throw new McpError(ErrorCode.InvalidParams, `Task "${taskId}" not found`);
734+
return await this.getTaskOrThrow(taskId, mcpSessionId, 'GetTaskRequestSchema');
726735
});
727736

728737
// Get task result payload
@@ -732,12 +741,7 @@ export class ActorsMcpServer {
732741
const { taskId } = params;
733742
const mcpSessionId = params._meta?.mcpSessionId;
734743
log.debug('[GetTaskPayloadRequestSchema] Getting task result', { taskId, mcpSessionId });
735-
const task = await this.taskStore.getTask(taskId, mcpSessionId);
736-
if (!task) {
737-
// Client error (invalid/unknown taskId) — softFail to avoid polluting error logs.
738-
log.softFail('[GetTaskPayloadRequestSchema] Task not found', { taskId, mcpSessionId, statusCode: 404 });
739-
throw new McpError(ErrorCode.InvalidParams, `Task "${taskId}" not found`);
740-
}
744+
const task = await this.getTaskOrThrow(taskId, mcpSessionId, 'GetTaskPayloadRequestSchema');
741745
if (task.status !== 'completed' && task.status !== 'failed') {
742746
throw new McpError(
743747
ErrorCode.InvalidParams,
@@ -763,12 +767,7 @@ export class ActorsMcpServer {
763767
const mcpSessionId = params._meta?.mcpSessionId;
764768
log.debug('[CancelTaskRequestSchema] Cancelling task', { taskId, mcpSessionId });
765769

766-
const task = await this.taskStore.getTask(taskId, mcpSessionId);
767-
if (!task) {
768-
// Client error (invalid/unknown taskId) — softFail to avoid polluting error logs.
769-
log.softFail('[CancelTaskRequestSchema] Task not found', { taskId, mcpSessionId, statusCode: 404 });
770-
throw new McpError(ErrorCode.InvalidParams, `Task "${taskId}" not found`);
771-
}
770+
const task = await this.getTaskOrThrow(taskId, mcpSessionId, 'CancelTaskRequestSchema');
772771
if (task.status === 'completed' || task.status === 'failed' || task.status === 'cancelled') {
773772
// Client error (cancel on terminal task) — softFail to avoid polluting error logs.
774773
log.softFail('[CancelTaskRequestSchema] Task already in terminal state', {

0 commit comments

Comments
 (0)