Merge pull request #38 from modelcontextprotocol/justin/support-older-protocol-version
Add backwards compatibility with 2024-10-07
This commit is contained in:
commit
7372b8abbd
4 changed files with 56 additions and 46 deletions
|
|
@ -1,36 +1,38 @@
|
|||
import { ProgressCallback, Protocol } from "../shared/protocol.js";
|
||||
import { Transport } from "../shared/transport.js";
|
||||
import {
|
||||
CallToolRequest,
|
||||
CallToolResultSchema,
|
||||
ClientNotification,
|
||||
ClientRequest,
|
||||
ClientResult,
|
||||
CompatibilityCallToolResultSchema,
|
||||
CompleteRequest,
|
||||
CompleteResultSchema,
|
||||
EmptyResultSchema,
|
||||
GetPromptRequest,
|
||||
GetPromptResultSchema,
|
||||
Implementation,
|
||||
InitializeResultSchema,
|
||||
LATEST_PROTOCOL_VERSION,
|
||||
ListPromptsRequest,
|
||||
ListPromptsResultSchema,
|
||||
ListResourcesRequest,
|
||||
ListResourcesResultSchema,
|
||||
ListResourceTemplatesRequest,
|
||||
ListResourceTemplatesResultSchema,
|
||||
ListToolsRequest,
|
||||
ListToolsResultSchema,
|
||||
LoggingLevel,
|
||||
Notification,
|
||||
PROTOCOL_VERSION,
|
||||
ReadResourceRequest,
|
||||
ReadResourceResultSchema,
|
||||
Request,
|
||||
Result,
|
||||
ServerCapabilities,
|
||||
CompleteRequest,
|
||||
GetPromptRequest,
|
||||
ListPromptsRequest,
|
||||
ListResourcesRequest,
|
||||
ReadResourceRequest,
|
||||
SubscribeRequest,
|
||||
UnsubscribeRequest,
|
||||
CallToolRequest,
|
||||
ListToolsRequest,
|
||||
CompleteResultSchema,
|
||||
GetPromptResultSchema,
|
||||
ListPromptsResultSchema,
|
||||
ListResourcesResultSchema,
|
||||
ReadResourceResultSchema,
|
||||
CallToolResultSchema,
|
||||
ListToolsResultSchema,
|
||||
EmptyResultSchema,
|
||||
LoggingLevel,
|
||||
ListResourceTemplatesRequest,
|
||||
ListResourceTemplatesResultSchema,
|
||||
SUPPORTED_PROTOCOL_VERSIONS,
|
||||
UnsubscribeRequest
|
||||
} from "../types.js";
|
||||
|
||||
/**
|
||||
|
|
@ -84,7 +86,7 @@ export class Client<
|
|||
{
|
||||
method: "initialize",
|
||||
params: {
|
||||
protocolVersion: PROTOCOL_VERSION,
|
||||
protocolVersion: LATEST_PROTOCOL_VERSION,
|
||||
capabilities: {},
|
||||
clientInfo: this._clientInfo,
|
||||
},
|
||||
|
|
@ -96,7 +98,7 @@ export class Client<
|
|||
throw new Error(`Server sent invalid initialize result: ${result}`);
|
||||
}
|
||||
|
||||
if (result.protocolVersion !== PROTOCOL_VERSION) {
|
||||
if (!SUPPORTED_PROTOCOL_VERSIONS.includes(result.protocolVersion)) {
|
||||
throw new Error(
|
||||
`Server's protocol version is not supported: ${result.protocolVersion}`,
|
||||
);
|
||||
|
|
@ -217,11 +219,12 @@ export class Client<
|
|||
|
||||
async callTool(
|
||||
params: CallToolRequest["params"],
|
||||
resultSchema: typeof CallToolResultSchema | typeof CompatibilityCallToolResultSchema = CallToolResultSchema,
|
||||
onprogress?: ProgressCallback,
|
||||
) {
|
||||
return this.request(
|
||||
{ method: "tools/call", params },
|
||||
CallToolResultSchema,
|
||||
resultSchema,
|
||||
onprogress,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,30 +1,31 @@
|
|||
import { ProgressCallback, Protocol } from "../shared/protocol.js";
|
||||
import {
|
||||
ClientCapabilities,
|
||||
CreateMessageRequest,
|
||||
CreateMessageResultSchema,
|
||||
EmptyResultSchema,
|
||||
Implementation,
|
||||
InitializedNotificationSchema,
|
||||
InitializeRequest,
|
||||
InitializeRequestSchema,
|
||||
InitializeResult,
|
||||
LATEST_PROTOCOL_VERSION,
|
||||
ListPromptsRequestSchema,
|
||||
ListResourcesRequestSchema,
|
||||
ListRootsRequest,
|
||||
ListRootsResultSchema,
|
||||
ListToolsRequestSchema,
|
||||
LoggingMessageNotification,
|
||||
Notification,
|
||||
PROTOCOL_VERSION,
|
||||
Request,
|
||||
ResourceUpdatedNotification,
|
||||
Result,
|
||||
ServerCapabilities,
|
||||
ServerNotification,
|
||||
ServerRequest,
|
||||
ServerResult,
|
||||
ServerCapabilities,
|
||||
ListResourcesRequestSchema,
|
||||
ListToolsRequestSchema,
|
||||
ListPromptsRequestSchema,
|
||||
SetLevelRequestSchema,
|
||||
CreateMessageRequest,
|
||||
CreateMessageResultSchema,
|
||||
EmptyResultSchema,
|
||||
LoggingMessageNotification,
|
||||
ResourceUpdatedNotification,
|
||||
ListRootsRequest,
|
||||
ListRootsResultSchema,
|
||||
SUPPORTED_PROTOCOL_VERSIONS
|
||||
} from "../types.js";
|
||||
|
||||
/**
|
||||
|
|
@ -86,17 +87,13 @@ export class Server<
|
|||
private async _oninitialize(
|
||||
request: InitializeRequest,
|
||||
): Promise<InitializeResult> {
|
||||
if (request.params.protocolVersion !== PROTOCOL_VERSION) {
|
||||
throw new Error(
|
||||
`Client's protocol version is not supported: ${request.params.protocolVersion}`,
|
||||
);
|
||||
}
|
||||
const requestedVersion = request.params.protocolVersion;
|
||||
|
||||
this._clientCapabilities = request.params.capabilities;
|
||||
this._clientVersion = request.params.clientInfo;
|
||||
|
||||
return {
|
||||
protocolVersion: PROTOCOL_VERSION,
|
||||
protocolVersion: SUPPORTED_PROTOCOL_VERSIONS.includes(requestedVersion) ? requestedVersion : LATEST_PROTOCOL_VERSION,
|
||||
capabilities: this.getCapabilities(),
|
||||
serverInfo: this._serverInfo,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { AnyZodObject, ZodLiteral, ZodObject, z } from "zod";
|
||||
import { ZodLiteral, ZodObject, ZodType, z } from "zod";
|
||||
import {
|
||||
ErrorCode,
|
||||
JSONRPCError,
|
||||
|
|
@ -250,7 +250,7 @@ export class Protocol<
|
|||
*
|
||||
* Do not use this method to emit notifications! Use notification() instead.
|
||||
*/
|
||||
request<T extends AnyZodObject>(
|
||||
request<T extends ZodType<object>>(
|
||||
request: SendRequestT,
|
||||
resultSchema: T,
|
||||
onprogress?: ProgressCallback,
|
||||
|
|
|
|||
16
src/types.ts
16
src/types.ts
|
|
@ -1,6 +1,7 @@
|
|||
import { z } from "zod";
|
||||
|
||||
export const PROTOCOL_VERSION = "2024-11-05";
|
||||
export const LATEST_PROTOCOL_VERSION = "2024-11-05";
|
||||
export const SUPPORTED_PROTOCOL_VERSIONS = [LATEST_PROTOCOL_VERSION, "2024-10-07"];
|
||||
|
||||
/* JSON-RPC types */
|
||||
export const JSONRPC_VERSION = "2.0";
|
||||
|
|
@ -196,7 +197,7 @@ export const InitializeRequestSchema = RequestSchema.extend({
|
|||
/**
|
||||
* The latest version of the Model Context Protocol that the client supports. The client MAY decide to support older versions as well.
|
||||
*/
|
||||
protocolVersion: z.string().or(z.number().int()),
|
||||
protocolVersion: z.string(),
|
||||
capabilities: ClientCapabilitiesSchema,
|
||||
clientInfo: ImplementationSchema,
|
||||
}),
|
||||
|
|
@ -268,7 +269,7 @@ export const InitializeResultSchema = ResultSchema.extend({
|
|||
/**
|
||||
* The version of the Model Context Protocol that the server wants to use. This may not match the version that the client requested. If the client cannot support this version, it MUST disconnect.
|
||||
*/
|
||||
protocolVersion: z.string().or(z.number().int()),
|
||||
protocolVersion: z.string(),
|
||||
capabilities: ServerCapabilitiesSchema,
|
||||
serverInfo: ImplementationSchema,
|
||||
});
|
||||
|
|
@ -721,6 +722,13 @@ export const CallToolResultSchema = ResultSchema.extend({
|
|||
isError: z.boolean(),
|
||||
});
|
||||
|
||||
/**
|
||||
* CallToolResultSchema extended with backwards compatibility to protocol version 2024-10-07.
|
||||
*/
|
||||
export const CompatibilityCallToolResultSchema = CallToolResultSchema.or(ResultSchema.extend({
|
||||
toolResult: z.unknown(),
|
||||
}));
|
||||
|
||||
/**
|
||||
* Used by the client to invoke a tool provided by the server.
|
||||
*/
|
||||
|
|
@ -1053,6 +1061,7 @@ export const ServerResultSchema = z.union([
|
|||
ListResourceTemplatesResultSchema,
|
||||
ReadResourceResultSchema,
|
||||
CallToolResultSchema,
|
||||
CompatibilityCallToolResultSchema,
|
||||
ListToolsResultSchema,
|
||||
]);
|
||||
|
||||
|
|
@ -1147,6 +1156,7 @@ export type Tool = z.infer<typeof ToolSchema>;
|
|||
export type ListToolsRequest = z.infer<typeof ListToolsRequestSchema>;
|
||||
export type ListToolsResult = z.infer<typeof ListToolsResultSchema>;
|
||||
export type CallToolResult = z.infer<typeof CallToolResultSchema>;
|
||||
export type CompatibilityCallToolResult = z.infer<typeof CompatibilityCallToolResultSchema>;
|
||||
export type CallToolRequest = z.infer<typeof CallToolRequestSchema>;
|
||||
export type ToolListChangedNotification = z.infer<
|
||||
typeof ToolListChangedNotificationSchema
|
||||
|
|
|
|||
Loading…
Reference in a new issue