import * as z from 'zod'; import { ProtocolError } from '@modelcontextprotocol/server'; export const EVENTS_EXTENSION_ID = 'io.modelcontextprotocol/events'; export const SUBSCRIPTION_ID_META_KEY = 'io.modelcontextprotocol/subscriptionId'; export const EVENTS_ERR = { InvalidParams: -32602, HeaderMismatch: -32020, NotFound: -32023, Forbidden: -32024, ResourceExhausted: -32025, Unsupported: -32026, } as const; export type EventsErrorCode = (typeof EVENTS_ERR)[keyof typeof EVENTS_ERR]; export type DeliveryMode = 'poll' | 'push' | 'webhook'; export const ListParamsSchema = z.looseObject({ cursor: z.string().optional() }).optional(); const SubscriptionParamsShape = { name: z.string().min(1), arguments: z.record(z.string(), z.unknown()).optional(), cursor: z.string().nullable().optional(), maxAgeMs: z.number().int().nonnegative().optional(), }; export const PollParamsSchema = z.looseObject({ ...SubscriptionParamsShape, maxEvents: z.number().int().positive().optional(), }); export const StreamParamsSchema = z.looseObject({ ...SubscriptionParamsShape, heartbeatMs: z.number().int().positive().optional(), }); export type ListParams = z.output; export type PollParams = z.output; export type StreamParams = z.output; export interface EventDescriptor { name: string; description?: string; delivery: DeliveryMode[]; inputSchema?: Record; payloadSchema?: Record; _meta?: Record; } export interface EventOccurrence { eventId: string; name: string; timestamp: string; data: Record; cursor?: string; _meta?: Record; } export interface ListEventsResult { resultType: 'complete'; events: EventDescriptor[]; nextCursor?: string; } export interface PollResult { resultType: 'complete'; events: EventOccurrence[]; cursor: string | null; truncated: boolean; hasMore: boolean; nextPollMs?: number; } export interface StreamResult { resultType: 'complete'; _meta?: Record; } export const EVENTS_METHODS = { list: 'events/list', poll: 'events/poll', stream: 'events/stream', } as const; export const EVENTS_NOTIFICATIONS = { active: 'notifications/events/active', event: 'notifications/events/event', heartbeat: 'notifications/events/heartbeat', error: 'notifications/events/error', terminated: 'notifications/events/terminated', } as const; export type WebcamEventName = 'webcam.message' | 'webcam.snapshot'; export interface WebcamSnapshotRef { eventId: string; mimeType: string; width?: number; height?: number; bytes: number; frozen: boolean; capturedAt: string; resourceUri: string; tool: 'get_event_snapshot'; } export interface WebcamEventPayload { message: string | null; user: string; source: 'ui' | 'auto' | 'api'; snapshot: WebcamSnapshotRef | null; } export function notFoundEventError(name: string): ProtocolError { return new ProtocolError(EVENTS_ERR.NotFound, 'NotFound', { kind: 'event', name }); } export function unsupportedDeliveryError(value: DeliveryMode): ProtocolError { return new ProtocolError(EVENTS_ERR.Unsupported, 'Unsupported', { feature: 'deliveryMode', value }); } export function invalidCursorError(cursor: string): ProtocolError { return new ProtocolError(EVENTS_ERR.InvalidParams, 'InvalidParams', { cursor }); } export function forbiddenError(reason: string): ProtocolError { return new ProtocolError(EVENTS_ERR.Forbidden, 'Forbidden', { reason }); } export function resourceExhaustedError(max: number): ProtocolError { return new ProtocolError(EVENTS_ERR.ResourceExhausted, 'ResourceExhausted', { limit: 'subscriptions', max, }); } export function buildActiveNotification( subscriptionId: string | number, params: { cursor: string | null; truncated: boolean; heartbeatMs: number }, ): { method: typeof EVENTS_NOTIFICATIONS.active; params: Record } { return { method: EVENTS_NOTIFICATIONS.active, params: { cursor: params.cursor, truncated: params.truncated, heartbeatMs: params.heartbeatMs, _meta: { [SUBSCRIPTION_ID_META_KEY]: subscriptionId }, }, }; } export function buildEventNotification( subscriptionId: string | number, occurrence: EventOccurrence & { cursor: string }, ): { method: typeof EVENTS_NOTIFICATIONS.event; params: Record } { return { method: EVENTS_NOTIFICATIONS.event, params: { eventId: occurrence.eventId, name: occurrence.name, timestamp: occurrence.timestamp, data: occurrence.data, cursor: occurrence.cursor, _meta: { ...(occurrence._meta ?? {}), [SUBSCRIPTION_ID_META_KEY]: subscriptionId }, }, }; } export function buildHeartbeatNotification( subscriptionId: string | number, cursor: string | null, ): { method: typeof EVENTS_NOTIFICATIONS.heartbeat; params: Record } { return { method: EVENTS_NOTIFICATIONS.heartbeat, params: { cursor, _meta: { [SUBSCRIPTION_ID_META_KEY]: subscriptionId }, }, }; } export function buildTerminatedNotification( subscriptionId: string | number, error: { code: number; message: string; data?: Record }, ): { method: typeof EVENTS_NOTIFICATIONS.terminated; params: Record } { return { method: EVENTS_NOTIFICATIONS.terminated, params: { error, _meta: { [SUBSCRIPTION_ID_META_KEY]: subscriptionId }, }, }; } export function buildStreamCompleteResult(subscriptionId: string | number): StreamResult { return { resultType: 'complete', _meta: { [SUBSCRIPTION_ID_META_KEY]: subscriptionId }, }; }