* Cancellation stops waiting for a result, but running work keeps its queue * until it settles, including response processing, to preserve serialization. * Operations without cancellation support can keep the queue indefinitely.
(
rawArgs: Record<string, unknown> = {},
options?: {viaMcp?: boolean; signal?: AbortSignal},
)
| 484 | * Operations without cancellation support can keep the queue indefinitely. |
| 485 | */ |
| 486 | async handle( |
| 487 | rawArgs: Record<string, unknown> = {}, |
| 488 | options?: {viaMcp?: boolean; signal?: AbortSignal}, |
| 489 | ): Promise<CallToolResult> { |
| 490 | const signal = options?.signal ?? new AbortController().signal; |
| 491 | signal.throwIfAborted(); |
| 492 | const context = await raceWithSignal(this.getContext(), signal); |
| 493 | let queueGuard: Disposable | undefined; |
| 494 | try { |
| 495 | queueGuard = await this.#queueFor(context, rawArgs)?.scheduleTask(signal); |
| 496 | } catch (err) { |
| 497 | signal.throwIfAborted(); |
| 498 | // The queue was disposed while the call waited, e.g. the page closed. |
| 499 | return errorResult(getErrorMessage(err)); |
| 500 | } |
| 501 | using guard = new DisposableStack(); |
| 502 | guard.use(queueGuard); |
| 503 | signal.throwIfAborted(); |
| 504 | if (options?.viaMcp && this.#disabled) { |
| 505 | throw new ProtocolError( |
| 506 | ProtocolErrorCode.InvalidParams, |
| 507 | `Tool ${this.#tool.name} disabled`, |
| 508 | ); |
| 509 | } |
| 510 | |
| 511 | if (this.#disabledReason) { |
| 512 | return errorResult(this.#disabledReason); |
| 513 | } |
| 514 | |
| 515 | const tool = this.#tool; |
| 516 | const parseResult = this.#registeredInputSchema.safeParse(rawArgs); |
| 517 | if (!parseResult.success) { |
| 518 | return errorResult(formatValidationError(tool.name, parseResult.error)); |
| 519 | } |
| 520 | const params = parseResult.data; |
| 521 | |
| 522 | const startTime = Date.now(); |
| 523 | let success = false; |
| 524 | let devToolsData: DevToolsData | undefined; |
| 525 | let pageUrl: string | undefined; |
| 526 | try { |
| 527 | logger?.(`${tool.name} request: ${JSON.stringify(params, null, ' ')}`); |
| 528 | const response = isSlimTool(tool) |
| 529 | ? new SlimMcpResponse(this.#config) |
| 530 | : new McpResponse(this.#config); |
| 531 | // The execution owns the queue even when its caller stops waiting. |
| 532 | const execution = isBrowserlessTool(tool) |
| 533 | ? this.#runBrowserlessTool(tool, params, response, context, signal) |
| 534 | : this.#runBrowserTool( |
| 535 | tool, |
| 536 | params, |
| 537 | response, |
| 538 | context, |
| 539 | signal, |
| 540 | (data, url) => { |
| 541 | devToolsData = data; |
| 542 | pageUrl = url; |
| 543 | }, |