v2 refactor!: remove Zod schema parameter from public-facing APIs (#1606)
This commit is contained in:
parent
1a78b0138f
commit
f88c376c52
2 changed files with 1483 additions and 1708 deletions
|
|
@ -15,13 +15,12 @@ import type {
|
|||
ElicitResult,
|
||||
GetTaskResult,
|
||||
ListTasksResult,
|
||||
Request,
|
||||
RequestMethod,
|
||||
RequestOptions,
|
||||
ResponseMessage,
|
||||
Result,
|
||||
ResultTypeMap,
|
||||
SchemaOutput
|
||||
} from '@modelcontextprotocol/core';
|
||||
import { CreateMessageResultSchema, ElicitResultSchema } from '@modelcontextprotocol/core';
|
||||
|
||||
import type { Server } from '../../server/server.js';
|
||||
|
||||
|
|
@ -30,7 +29,7 @@ import type { Server } from '../../server/server.js';
|
|||
*
|
||||
* Access via `server.experimental.tasks`:
|
||||
* ```typescript
|
||||
* const stream = server.experimental.tasks.requestStream(request, schema, options);
|
||||
* const stream = server.experimental.tasks.requestStream(request, options);
|
||||
* ```
|
||||
*
|
||||
* For high-level server usage with task-based tools, use {@linkcode index.McpServer | McpServer}.experimental.tasks instead.
|
||||
|
|
@ -47,27 +46,24 @@ export class ExperimentalServerTasks {
|
|||
* This method provides streaming access to request processing, allowing you to
|
||||
* observe intermediate task status updates for task-augmented requests.
|
||||
*
|
||||
* @param request - The request to send
|
||||
* @param resultSchema - Zod schema for validating the result
|
||||
* @param request - The request to send (method name determines the result schema)
|
||||
* @param options - Optional request options (timeout, signal, task creation params, etc.)
|
||||
* @returns AsyncGenerator that yields {@linkcode ResponseMessage} objects
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
requestStream<T extends AnySchema>(
|
||||
request: Request,
|
||||
resultSchema: T,
|
||||
requestStream<M extends RequestMethod>(
|
||||
request: { method: M; params?: Record<string, unknown> },
|
||||
options?: RequestOptions
|
||||
): AsyncGenerator<ResponseMessage<SchemaOutput<T> & Result>, void, void> {
|
||||
): AsyncGenerator<ResponseMessage<ResultTypeMap[M]>, void, void> {
|
||||
// Delegate to the server's underlying Protocol method
|
||||
type ServerWithRequestStream = {
|
||||
requestStream<U extends AnySchema>(
|
||||
request: Request,
|
||||
resultSchema: U,
|
||||
requestStream<N extends RequestMethod>(
|
||||
request: { method: N; params?: Record<string, unknown> },
|
||||
options?: RequestOptions
|
||||
): AsyncGenerator<ResponseMessage<SchemaOutput<U> & Result>, void, void>;
|
||||
): AsyncGenerator<ResponseMessage<ResultTypeMap[N]>, void, void>;
|
||||
};
|
||||
return (this._server as unknown as ServerWithRequestStream).requestStream(request, resultSchema, options);
|
||||
return (this._server as unknown as ServerWithRequestStream).requestStream(request, options);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -163,9 +159,8 @@ export class ExperimentalServerTasks {
|
|||
method: 'sampling/createMessage',
|
||||
params
|
||||
},
|
||||
CreateMessageResultSchema,
|
||||
options
|
||||
);
|
||||
) as AsyncGenerator<ResponseMessage<CreateMessageResult>, void, void>;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -241,9 +236,8 @@ export class ExperimentalServerTasks {
|
|||
method: 'elicitation/create',
|
||||
params: normalizedParams
|
||||
},
|
||||
ElicitResultSchema,
|
||||
options
|
||||
);
|
||||
) as AsyncGenerator<ResponseMessage<ElicitResult>, void, void>;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -460,7 +460,7 @@ export class Server extends Protocol<ServerContext> {
|
|||
}
|
||||
|
||||
async ping() {
|
||||
return this.request({ method: 'ping' }, EmptyResultSchema);
|
||||
return this._requestWithSchema({ method: 'ping' }, EmptyResultSchema);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -540,9 +540,9 @@ export class Server extends Protocol<ServerContext> {
|
|||
|
||||
// Use different schemas based on whether tools are provided
|
||||
if (params.tools) {
|
||||
return this.request({ method: 'sampling/createMessage', params }, CreateMessageResultWithToolsSchema, options);
|
||||
return this._requestWithSchema({ method: 'sampling/createMessage', params }, CreateMessageResultWithToolsSchema, options);
|
||||
}
|
||||
return this.request({ method: 'sampling/createMessage', params }, CreateMessageResultSchema, options);
|
||||
return this._requestWithSchema({ method: 'sampling/createMessage', params }, CreateMessageResultSchema, options);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -562,7 +562,7 @@ export class Server extends Protocol<ServerContext> {
|
|||
}
|
||||
|
||||
const urlParams = params as ElicitRequestURLParams;
|
||||
return this.request({ method: 'elicitation/create', params: urlParams }, ElicitResultSchema, options);
|
||||
return this._requestWithSchema({ method: 'elicitation/create', params: urlParams }, ElicitResultSchema, options);
|
||||
}
|
||||
case 'form': {
|
||||
if (!this._clientCapabilities?.elicitation?.form) {
|
||||
|
|
@ -572,7 +572,11 @@ export class Server extends Protocol<ServerContext> {
|
|||
const formParams: ElicitRequestFormParams =
|
||||
params.mode === 'form' ? (params as ElicitRequestFormParams) : { ...(params as ElicitRequestFormParams), mode: 'form' };
|
||||
|
||||
const result = await this.request({ method: 'elicitation/create', params: formParams }, ElicitResultSchema, options);
|
||||
const result = await this._requestWithSchema(
|
||||
{ method: 'elicitation/create', params: formParams },
|
||||
ElicitResultSchema,
|
||||
options
|
||||
);
|
||||
|
||||
if (result.action === 'accept' && result.content && formParams.requestedSchema) {
|
||||
try {
|
||||
|
|
@ -629,7 +633,7 @@ export class Server extends Protocol<ServerContext> {
|
|||
}
|
||||
|
||||
async listRoots(params?: ListRootsRequest['params'], options?: RequestOptions) {
|
||||
return this.request({ method: 'roots/list', params }, ListRootsResultSchema, options);
|
||||
return this._requestWithSchema({ method: 'roots/list', params }, ListRootsResultSchema, options);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in a new issue