MCPcopy Create free account
hub / github.com/modelcontextprotocol/typescript-sdk / executeToolHandler

Method executeToolHandler

src/server/mcp.ts:324–358  ·  view source on GitHub ↗

* Executes a tool handler (either regular or task-based).

(
        tool: RegisteredTool,
        args: unknown,
        extra: RequestHandlerExtra<ServerRequest, ServerNotification>
    )

Source from the content-addressed store, hash-verified

322 * Executes a tool handler (either regular or task-based).
323 */
324 private async executeToolHandler(
325 tool: RegisteredTool,
326 args: unknown,
327 extra: RequestHandlerExtra<ServerRequest, ServerNotification>
328 ): Promise<CallToolResult | CreateTaskResult> {
329 const handler = tool.handler as AnyToolHandler<ZodRawShapeCompat | undefined>;
330 const isTaskHandler = 'createTask' in handler;
331
332 if (isTaskHandler) {
333 if (!extra.taskStore) {
334 throw new Error('No task store provided.');
335 }
336 const taskExtra = { ...extra, taskStore: extra.taskStore };
337
338 if (tool.inputSchema) {
339 const typedHandler = handler as ToolTaskHandler<ZodRawShapeCompat>;
340 // eslint-disable-next-line @typescript-eslint/no-explicit-any
341 return await Promise.resolve(typedHandler.createTask(args as any, taskExtra));
342 } else {
343 const typedHandler = handler as ToolTaskHandler<undefined>;
344 // eslint-disable-next-line @typescript-eslint/no-explicit-any
345 return await Promise.resolve((typedHandler.createTask as any)(taskExtra));
346 }
347 }
348
349 if (tool.inputSchema) {
350 const typedHandler = handler as ToolCallback<ZodRawShapeCompat>;
351 // eslint-disable-next-line @typescript-eslint/no-explicit-any
352 return await Promise.resolve(typedHandler(args as any, extra));
353 } else {
354 const typedHandler = handler as ToolCallback<undefined>;
355 // eslint-disable-next-line @typescript-eslint/no-explicit-any
356 return await Promise.resolve((typedHandler as any)(extra));
357 }
358 }
359
360 /**
361 * Handles automatic task polling for tools with taskSupport 'optional'.

Callers 1

Calls 1

createTaskMethod · 0.65

Tested by

no test coverage detected