client.d.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * Experimental client task features for MCP SDK.
  3. * WARNING: These APIs are experimental and may change without notice.
  4. *
  5. * @experimental
  6. */
  7. import type { Client } from '../../client/index.js';
  8. import type { RequestOptions } from '../../shared/protocol.js';
  9. import type { ResponseMessage } from '../../shared/responseMessage.js';
  10. import type { AnyObjectSchema, SchemaOutput } from '../../server/zod-compat.js';
  11. import type { CallToolRequest, ClientRequest, Notification, Request, Result } from '../../types.js';
  12. import { CallToolResultSchema, type CompatibilityCallToolResultSchema } from '../../types.js';
  13. import type { GetTaskResult, ListTasksResult, CancelTaskResult } from './types.js';
  14. /**
  15. * Experimental task features for MCP clients.
  16. *
  17. * Access via `client.experimental.tasks`:
  18. * ```typescript
  19. * const stream = client.experimental.tasks.callToolStream({ name: 'tool', arguments: {} });
  20. * const task = await client.experimental.tasks.getTask(taskId);
  21. * ```
  22. *
  23. * @experimental
  24. */
  25. export declare class ExperimentalClientTasks<RequestT extends Request = Request, NotificationT extends Notification = Notification, ResultT extends Result = Result> {
  26. private readonly _client;
  27. constructor(_client: Client<RequestT, NotificationT, ResultT>);
  28. /**
  29. * Calls a tool and returns an AsyncGenerator that yields response messages.
  30. * The generator is guaranteed to end with either a 'result' or 'error' message.
  31. *
  32. * This method provides streaming access to tool execution, allowing you to
  33. * observe intermediate task status updates for long-running tool calls.
  34. * Automatically validates structured output if the tool has an outputSchema.
  35. *
  36. * @example
  37. * ```typescript
  38. * const stream = client.experimental.tasks.callToolStream({ name: 'myTool', arguments: {} });
  39. * for await (const message of stream) {
  40. * switch (message.type) {
  41. * case 'taskCreated':
  42. * console.log('Tool execution started:', message.task.taskId);
  43. * break;
  44. * case 'taskStatus':
  45. * console.log('Tool status:', message.task.status);
  46. * break;
  47. * case 'result':
  48. * console.log('Tool result:', message.result);
  49. * break;
  50. * case 'error':
  51. * console.error('Tool error:', message.error);
  52. * break;
  53. * }
  54. * }
  55. * ```
  56. *
  57. * @param params - Tool call parameters (name and arguments)
  58. * @param resultSchema - Zod schema for validating the result (defaults to CallToolResultSchema)
  59. * @param options - Optional request options (timeout, signal, task creation params, etc.)
  60. * @returns AsyncGenerator that yields ResponseMessage objects
  61. *
  62. * @experimental
  63. */
  64. callToolStream<T extends typeof CallToolResultSchema | typeof CompatibilityCallToolResultSchema>(params: CallToolRequest['params'], resultSchema?: T, options?: RequestOptions): AsyncGenerator<ResponseMessage<SchemaOutput<T>>, void, void>;
  65. /**
  66. * Gets the current status of a task.
  67. *
  68. * @param taskId - The task identifier
  69. * @param options - Optional request options
  70. * @returns The task status
  71. *
  72. * @experimental
  73. */
  74. getTask(taskId: string, options?: RequestOptions): Promise<GetTaskResult>;
  75. /**
  76. * Retrieves the result of a completed task.
  77. *
  78. * @param taskId - The task identifier
  79. * @param resultSchema - Zod schema for validating the result
  80. * @param options - Optional request options
  81. * @returns The task result
  82. *
  83. * @experimental
  84. */
  85. getTaskResult<T extends AnyObjectSchema>(taskId: string, resultSchema?: T, options?: RequestOptions): Promise<SchemaOutput<T>>;
  86. /**
  87. * Lists tasks with optional pagination.
  88. *
  89. * @param cursor - Optional pagination cursor
  90. * @param options - Optional request options
  91. * @returns List of tasks with optional next cursor
  92. *
  93. * @experimental
  94. */
  95. listTasks(cursor?: string, options?: RequestOptions): Promise<ListTasksResult>;
  96. /**
  97. * Cancels a running task.
  98. *
  99. * @param taskId - The task identifier
  100. * @param options - Optional request options
  101. *
  102. * @experimental
  103. */
  104. cancelTask(taskId: string, options?: RequestOptions): Promise<CancelTaskResult>;
  105. /**
  106. * Sends a request and returns an AsyncGenerator that yields response messages.
  107. * The generator is guaranteed to end with either a 'result' or 'error' message.
  108. *
  109. * This method provides streaming access to request processing, allowing you to
  110. * observe intermediate task status updates for task-augmented requests.
  111. *
  112. * @param request - The request to send
  113. * @param resultSchema - Zod schema for validating the result
  114. * @param options - Optional request options (timeout, signal, task creation params, etc.)
  115. * @returns AsyncGenerator that yields ResponseMessage objects
  116. *
  117. * @experimental
  118. */
  119. requestStream<T extends AnyObjectSchema>(request: ClientRequest | RequestT, resultSchema: T, options?: RequestOptions): AsyncGenerator<ResponseMessage<SchemaOutput<T>>, void, void>;
  120. }
  121. //# sourceMappingURL=client.d.ts.map