Spaces:
Running

mcp-webcam / server /events /protocol.ts
evalstate's picture
evalstate HF Staff
Deploy mcp-webcam v0.3.0 (stateless MCP 2026-07-28 + SEP-3415 Events + Studio UI)
6f5f15e verified
Raw History Blame Contribute Delete
5.78 kB
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<typeof ListParamsSchema>;
export type PollParams = z.output<typeof PollParamsSchema>;
export type StreamParams = z.output<typeof StreamParamsSchema>;
export interface EventDescriptor {
name: string;
description?: string;
delivery: DeliveryMode[];
inputSchema?: Record<string, unknown>;
payloadSchema?: Record<string, unknown>;
_meta?: Record<string, unknown>;
}
export interface EventOccurrence {
eventId: string;
name: string;
timestamp: string;
data: Record<string, unknown>;
cursor?: string;
_meta?: Record<string, unknown>;
}
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<string, unknown>;
}
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<string, unknown> } {
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<string, unknown> } {
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<string, unknown> } {
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<string, unknown> },
): { method: typeof EVENTS_NOTIFICATIONS.terminated; params: Record<string, unknown> } {
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 },
};
}