@@ -19,7 +19,8 @@ import type {
1919 ResultTypeMap ,
2020 SchemaOutput
2121} from '@modelcontextprotocol/core' ;
22- import { ProtocolError , ProtocolErrorCode } from '@modelcontextprotocol/core' ;
22+ import { CallToolResultSchema , getResultSchema , ProtocolError , ProtocolErrorCode } from '@modelcontextprotocol/core' ;
23+ import type { Request } from '@modelcontextprotocol/core' ;
2324
2425import type { Client } from '../../client/client.js' ;
2526
@@ -28,10 +29,6 @@ import type { Client } from '../../client/client.js';
2829 * @internal
2930 */
3031interface ClientInternal {
31- requestStream < M extends RequestMethod > (
32- request : { method : M ; params ?: Record < string , unknown > } ,
33- options ?: RequestOptions
34- ) : AsyncGenerator < ResponseMessage < ResultTypeMap [ M ] > , void , void > ;
3532 isToolTask ( toolName : string ) : boolean ;
3633 getToolOutputValidator ( toolName : string ) : ( ( data : unknown ) => { valid : boolean ; errorMessage ?: string } ) | undefined ;
3734}
@@ -50,6 +47,14 @@ interface ClientInternal {
5047export class ExperimentalClientTasks {
5148 constructor ( private readonly _client : Client ) { }
5249
50+ private get _module ( ) {
51+ const module = this . _client . taskModule ;
52+ if ( ! module ) {
53+ throw new Error ( 'Tasks capability is not configured. Declare tasks in capabilities to use task features.' ) ;
54+ }
55+ return module ;
56+ }
57+
5358 /**
5459 * Calls a tool and returns an AsyncGenerator that yields response messages.
5560 * The generator is guaranteed to end with either a `'result'` or `'error'` message.
@@ -104,7 +109,7 @@ export class ExperimentalClientTasks {
104109 task : options ?. task ?? ( clientInternal . isToolTask ( params . name ) ? { } : undefined )
105110 } ;
106111
107- const stream = clientInternal . requestStream ( { method : 'tools/call' , params } , optionsWithTask ) ;
112+ const stream = this . _module . requestStream ( { method : 'tools/call' , params } , CallToolResultSchema , optionsWithTask ) ;
108113
109114 // Get the validator for this tool (if it has an output schema)
110115 const validator = clientInternal . getToolOutputValidator ( params . name ) ;
@@ -176,9 +181,7 @@ export class ExperimentalClientTasks {
176181 * @experimental
177182 */
178183 async getTask ( taskId : string , options ?: RequestOptions ) : Promise < GetTaskResult > {
179- // Delegate to the client's underlying Protocol method
180- type ClientWithGetTask = { getTask ( params : { taskId : string } , options ?: RequestOptions ) : Promise < GetTaskResult > } ;
181- return ( this . _client as unknown as ClientWithGetTask ) . getTask ( { taskId } , options ) ;
184+ return this . _module . getTask ( { taskId } , options ) ;
182185 }
183186
184187 /**
@@ -192,16 +195,7 @@ export class ExperimentalClientTasks {
192195 * @experimental
193196 */
194197 async getTaskResult < T extends AnyObjectSchema > ( taskId : string , resultSchema ?: T , options ?: RequestOptions ) : Promise < SchemaOutput < T > > {
195- // Delegate to the client's underlying Protocol method
196- return (
197- this . _client as unknown as {
198- getTaskResult : < U extends AnyObjectSchema > (
199- params : { taskId : string } ,
200- resultSchema ?: U ,
201- options ?: RequestOptions
202- ) => Promise < SchemaOutput < U > > ;
203- }
204- ) . getTaskResult ( { taskId } , resultSchema , options ) ;
198+ return this . _module . getTaskResult ( { taskId } , resultSchema ! , options ) ;
205199 }
206200
207201 /**
@@ -214,12 +208,7 @@ export class ExperimentalClientTasks {
214208 * @experimental
215209 */
216210 async listTasks ( cursor ?: string , options ?: RequestOptions ) : Promise < ListTasksResult > {
217- // Delegate to the client's underlying Protocol method
218- return (
219- this . _client as unknown as {
220- listTasks : ( params ?: { cursor ?: string } , options ?: RequestOptions ) => Promise < ListTasksResult > ;
221- }
222- ) . listTasks ( cursor ? { cursor } : undefined , options ) ;
211+ return this . _module . listTasks ( cursor ? { cursor } : undefined , options ) ;
223212 }
224213
225214 /**
@@ -231,12 +220,7 @@ export class ExperimentalClientTasks {
231220 * @experimental
232221 */
233222 async cancelTask ( taskId : string , options ?: RequestOptions ) : Promise < CancelTaskResult > {
234- // Delegate to the client's underlying Protocol method
235- return (
236- this . _client as unknown as {
237- cancelTask : ( params : { taskId : string } , options ?: RequestOptions ) => Promise < CancelTaskResult > ;
238- }
239- ) . cancelTask ( { taskId } , options ) ;
223+ return this . _module . cancelTask ( { taskId } , options ) ;
240224 }
241225
242226 /**
@@ -281,7 +265,11 @@ export class ExperimentalClientTasks {
281265 request : { method : M ; params ?: Record < string , unknown > } ,
282266 options ?: RequestOptions
283267 ) : AsyncGenerator < ResponseMessage < ResultTypeMap [ M ] > , void , void > {
284- // Delegate to the client's underlying Protocol method
285- return ( this . _client as unknown as ClientInternal ) . requestStream ( request , options ) ;
268+ const resultSchema = getResultSchema ( request . method ) as unknown as AnyObjectSchema ;
269+ return this . _module . requestStream ( request as Request , resultSchema , options ) as AsyncGenerator <
270+ ResponseMessage < ResultTypeMap [ M ] > ,
271+ void ,
272+ void
273+ > ;
286274 }
287275}
0 commit comments