|
| 1 | +/** |
| 2 | + * Pre-2026 wire types and schemas. |
| 3 | + * |
| 4 | + * These types represent JSON-RPC methods and notifications that were part of |
| 5 | + * MCP up to and including 2025-11-25 but are removed from the 2026-06 draft |
| 6 | + * spec. They are NOT in the current `spec.types.ts` (regenerated from the |
| 7 | + * spec repo) and are SDK-maintained here so `LegacyServer` / `LegacyClient` |
| 8 | + * can continue to speak the pre-2026 wire protocol. |
| 9 | + * |
| 10 | + * Deleted when pre-2026 protocol support is dropped. |
| 11 | + */ |
| 12 | +import * as z from 'zod/v4'; |
| 13 | + |
| 14 | +import { |
| 15 | + ClientCapabilitiesSchema, |
| 16 | + ImplementationSchema, |
| 17 | + LoggingLevelSchema, |
| 18 | + NotificationSchema, |
| 19 | + ProgressTokenSchema, |
| 20 | + registerLegacySchemas, |
| 21 | + ServerCapabilitiesSchema |
| 22 | +} from './schemas.js'; |
| 23 | +import type { |
| 24 | + ClientCapabilities, |
| 25 | + Implementation, |
| 26 | + JSONRPCNotification, |
| 27 | + JSONRPCRequest, |
| 28 | + LoggingLevel, |
| 29 | + MetaObject, |
| 30 | + NotificationParams, |
| 31 | + ProgressToken, |
| 32 | + ServerCapabilities |
| 33 | +} from './spec.types.js'; |
| 34 | + |
| 35 | +/* ────────────────────────────────────────────────────────────────────────── */ |
| 36 | +/* Legacy base shapes */ |
| 37 | +/* */ |
| 38 | +/* The 2026-06 spec's `RequestParams._meta` is required and carries */ |
| 39 | +/* namespaced `io.modelcontextprotocol/*` keys. Pre-2026 requests have an */ |
| 40 | +/* optional `_meta` with only `progressToken`. These local base types let */ |
| 41 | +/* the legacy interfaces below avoid the strict 2026 shape. */ |
| 42 | +/* ────────────────────────────────────────────────────────────────────────── */ |
| 43 | + |
| 44 | +/** Pre-2026 `_meta` shape: only `progressToken`, no namespaced keys. */ |
| 45 | +export interface LegacyRequestMetaObject extends MetaObject { |
| 46 | + progressToken?: ProgressToken; |
| 47 | +} |
| 48 | + |
| 49 | +/** Pre-2026 request params: `_meta` is optional. */ |
| 50 | +export interface LegacyRequestParams { |
| 51 | + _meta?: LegacyRequestMetaObject; |
| 52 | + [key: string]: unknown; |
| 53 | +} |
| 54 | + |
| 55 | +/** Pre-2026 result: no `resultType`. */ |
| 56 | +export interface LegacyResult { |
| 57 | + _meta?: MetaObject; |
| 58 | + [key: string]: unknown; |
| 59 | +} |
| 60 | + |
| 61 | +/* Zod base shapes for legacy schemas. Kept separate from `schemas.ts` so the |
| 62 | + * 2026 file is spec-only at the source level. The shapes here are the pre-2026 |
| 63 | + * wire format (no namespaced `_meta` keys, no `resultType`). */ |
| 64 | + |
| 65 | +const LegacyRequestMetaSchema = z.looseObject({ |
| 66 | + progressToken: ProgressTokenSchema.optional() |
| 67 | +}); |
| 68 | + |
| 69 | +const LegacyBaseRequestParamsSchema = z.object({ |
| 70 | + _meta: LegacyRequestMetaSchema.optional() |
| 71 | +}); |
| 72 | + |
| 73 | +const LegacyRequestSchema = z.object({ |
| 74 | + method: z.string(), |
| 75 | + params: LegacyBaseRequestParamsSchema.loose().optional() |
| 76 | +}); |
| 77 | + |
| 78 | +const LegacyResultSchema = z.looseObject({ |
| 79 | + _meta: z.record(z.string(), z.unknown()).optional() |
| 80 | +}); |
| 81 | + |
| 82 | +/* ────────────────────────────────────────────────────────────────────────── */ |
| 83 | +/* `initialize` / `notifications/initialized` */ |
| 84 | +/* ────────────────────────────────────────────────────────────────────────── */ |
| 85 | + |
| 86 | +export interface InitializeRequestParams extends LegacyRequestParams { |
| 87 | + /** |
| 88 | + * The latest version of the Model Context Protocol that the client |
| 89 | + * supports. The client MAY decide to support older versions as well. |
| 90 | + */ |
| 91 | + protocolVersion: string; |
| 92 | + capabilities: ClientCapabilities; |
| 93 | + clientInfo: Implementation; |
| 94 | +} |
| 95 | + |
| 96 | +export interface InitializeRequest extends JSONRPCRequest { |
| 97 | + method: 'initialize'; |
| 98 | + params: InitializeRequestParams; |
| 99 | +} |
| 100 | + |
| 101 | +export interface InitializeResult extends LegacyResult { |
| 102 | + /** |
| 103 | + * The version of the Model Context Protocol that the server wants to use. |
| 104 | + * This may not match the version that the client requested. If the client |
| 105 | + * cannot support this version, it MUST disconnect. |
| 106 | + */ |
| 107 | + protocolVersion: string; |
| 108 | + capabilities: ServerCapabilities; |
| 109 | + serverInfo: Implementation; |
| 110 | + /** |
| 111 | + * Instructions describing how to use the server and its features. |
| 112 | + */ |
| 113 | + instructions?: string; |
| 114 | +} |
| 115 | + |
| 116 | +export interface InitializedNotification extends JSONRPCNotification { |
| 117 | + method: 'notifications/initialized'; |
| 118 | + params?: NotificationParams; |
| 119 | +} |
| 120 | + |
| 121 | +export const InitializeRequestParamsSchema = LegacyBaseRequestParamsSchema.extend({ |
| 122 | + protocolVersion: z.string(), |
| 123 | + capabilities: ClientCapabilitiesSchema, |
| 124 | + clientInfo: ImplementationSchema |
| 125 | +}); |
| 126 | + |
| 127 | +export const InitializeRequestSchema = LegacyRequestSchema.extend({ |
| 128 | + method: z.literal('initialize'), |
| 129 | + params: InitializeRequestParamsSchema |
| 130 | +}); |
| 131 | + |
| 132 | +export const InitializeResultSchema = LegacyResultSchema.extend({ |
| 133 | + protocolVersion: z.string(), |
| 134 | + capabilities: ServerCapabilitiesSchema, |
| 135 | + serverInfo: ImplementationSchema, |
| 136 | + instructions: z.string().optional() |
| 137 | +}); |
| 138 | + |
| 139 | +export const InitializedNotificationSchema = NotificationSchema.extend({ |
| 140 | + method: z.literal('notifications/initialized') |
| 141 | +}); |
| 142 | + |
| 143 | +/* ────────────────────────────────────────────────────────────────────────── */ |
| 144 | +/* `ping` */ |
| 145 | +/* ────────────────────────────────────────────────────────────────────────── */ |
| 146 | + |
| 147 | +export interface PingRequest extends JSONRPCRequest { |
| 148 | + method: 'ping'; |
| 149 | + params?: LegacyRequestParams; |
| 150 | +} |
| 151 | + |
| 152 | +export const PingRequestSchema = LegacyRequestSchema.extend({ |
| 153 | + method: z.literal('ping') |
| 154 | +}); |
| 155 | + |
| 156 | +/* ────────────────────────────────────────────────────────────────────────── */ |
| 157 | +/* `logging/setLevel` */ |
| 158 | +/* ────────────────────────────────────────────────────────────────────────── */ |
| 159 | + |
| 160 | +export interface SetLevelRequestParams extends LegacyRequestParams { |
| 161 | + /** |
| 162 | + * The level of logging that the client wants to receive from the server. |
| 163 | + */ |
| 164 | + level: LoggingLevel; |
| 165 | +} |
| 166 | + |
| 167 | +export interface SetLevelRequest extends JSONRPCRequest { |
| 168 | + method: 'logging/setLevel'; |
| 169 | + params: SetLevelRequestParams; |
| 170 | +} |
| 171 | + |
| 172 | +export const SetLevelRequestParamsSchema = LegacyBaseRequestParamsSchema.extend({ |
| 173 | + level: LoggingLevelSchema |
| 174 | +}); |
| 175 | + |
| 176 | +export const SetLevelRequestSchema = LegacyRequestSchema.extend({ |
| 177 | + method: z.literal('logging/setLevel'), |
| 178 | + params: SetLevelRequestParamsSchema |
| 179 | +}); |
| 180 | + |
| 181 | +/* ────────────────────────────────────────────────────────────────────────── */ |
| 182 | +/* `resources/subscribe` / `resources/unsubscribe` */ |
| 183 | +/* ────────────────────────────────────────────────────────────────────────── */ |
| 184 | + |
| 185 | +export interface SubscribeRequestParams extends LegacyRequestParams { |
| 186 | + /** The URI of the resource to subscribe to. */ |
| 187 | + uri: string; |
| 188 | +} |
| 189 | + |
| 190 | +export interface SubscribeRequest extends JSONRPCRequest { |
| 191 | + method: 'resources/subscribe'; |
| 192 | + params: SubscribeRequestParams; |
| 193 | +} |
| 194 | + |
| 195 | +export type UnsubscribeRequestParams = SubscribeRequestParams; |
| 196 | + |
| 197 | +export interface UnsubscribeRequest extends JSONRPCRequest { |
| 198 | + method: 'resources/unsubscribe'; |
| 199 | + params: UnsubscribeRequestParams; |
| 200 | +} |
| 201 | + |
| 202 | +export const SubscribeRequestParamsSchema = LegacyBaseRequestParamsSchema.extend({ |
| 203 | + uri: z.string() |
| 204 | +}); |
| 205 | + |
| 206 | +export const SubscribeRequestSchema = LegacyRequestSchema.extend({ |
| 207 | + method: z.literal('resources/subscribe'), |
| 208 | + params: SubscribeRequestParamsSchema |
| 209 | +}); |
| 210 | + |
| 211 | +export const UnsubscribeRequestParamsSchema = SubscribeRequestParamsSchema; |
| 212 | + |
| 213 | +export const UnsubscribeRequestSchema = LegacyRequestSchema.extend({ |
| 214 | + method: z.literal('resources/unsubscribe'), |
| 215 | + params: UnsubscribeRequestParamsSchema |
| 216 | +}); |
| 217 | + |
| 218 | +/* ────────────────────────────────────────────────────────────────────────── */ |
| 219 | +/* `notifications/roots/list_changed` (client → server) */ |
| 220 | +/* ────────────────────────────────────────────────────────────────────────── */ |
| 221 | + |
| 222 | +export interface RootsListChangedNotification extends JSONRPCNotification { |
| 223 | + method: 'notifications/roots/list_changed'; |
| 224 | + params?: NotificationParams; |
| 225 | +} |
| 226 | + |
| 227 | +export const RootsListChangedNotificationSchema = NotificationSchema.extend({ |
| 228 | + method: z.literal('notifications/roots/list_changed') |
| 229 | +}); |
| 230 | + |
| 231 | +/* ────────────────────────────────────────────────────────────────────────── */ |
| 232 | +/* Maps for legacy method/result/notification dispatch */ |
| 233 | +/* ────────────────────────────────────────────────────────────────────────── */ |
| 234 | + |
| 235 | +/** Legacy request methods that are not in the 2026-06 `RequestTypeMap`. */ |
| 236 | +export const legacyRequestSchemas = { |
| 237 | + initialize: InitializeRequestSchema, |
| 238 | + ping: PingRequestSchema, |
| 239 | + 'logging/setLevel': SetLevelRequestSchema, |
| 240 | + 'resources/subscribe': SubscribeRequestSchema, |
| 241 | + 'resources/unsubscribe': UnsubscribeRequestSchema |
| 242 | +} as const; |
| 243 | + |
| 244 | +/** Legacy result schemas keyed by request method. */ |
| 245 | +export const legacyResultSchemas = { |
| 246 | + initialize: InitializeResultSchema, |
| 247 | + ping: LegacyResultSchema, |
| 248 | + 'logging/setLevel': LegacyResultSchema, |
| 249 | + 'resources/subscribe': LegacyResultSchema, |
| 250 | + 'resources/unsubscribe': LegacyResultSchema |
| 251 | +} as const; |
| 252 | + |
| 253 | +/** Legacy notification methods that are not in the 2026-06 `NotificationTypeMap`. */ |
| 254 | +export const legacyNotificationSchemas = { |
| 255 | + 'notifications/initialized': InitializedNotificationSchema, |
| 256 | + 'notifications/roots/list_changed': RootsListChangedNotificationSchema |
| 257 | +} as const; |
| 258 | + |
| 259 | +export type LegacyRequestMethod = keyof typeof legacyRequestSchemas; |
| 260 | +export type LegacyNotificationMethod = keyof typeof legacyNotificationSchemas; |
| 261 | + |
| 262 | +// Merge into the runtime lookup maps so `getRequestSchema('initialize')` etc. |
| 263 | +// continue to work. Runs at module load (this file is barrel-imported). |
| 264 | +registerLegacySchemas({ |
| 265 | + requests: legacyRequestSchemas, |
| 266 | + results: legacyResultSchemas, |
| 267 | + notifications: legacyNotificationSchemas |
| 268 | +}); |
0 commit comments