import type { Server } from "@modelcontextprotocol/sdk/server/index.js"; import type { z } from "zod"; export type Session = { id: string; get: (key: string) => Promise; set: (key: string, value: unknown) => Promise; delete: (key: string) => Promise; }; export type StatelessServerContext = { config: TConfig; env: Record; accessToken?: string; }; export type StatefulServerContext = { config: TConfig; session: Session; env: Record; accessToken?: string; /** * Register a handler for messages sent via `sessions.send()`. * This enables callback-based message passing outside the MCP protocol. */ onMessage: (handler: (message: unknown) => unknown | Promise) => void; }; export type ServerContext = StatelessServerContext | StatefulServerContext; export type SandboxServerContext = { session: Session; }; export type CreateServerFn = (context: ServerContext) => Server | Promise; export type CreateSandboxServerFn = (context: SandboxServerContext) => Server | Promise; /** * OAuth adapter for servers that require user authorization. * @unstable This interface is subject to change. */ export interface AuthAdapter { getAuthorizationUrl(args: { callbackUrl: string; state: string; codeChallenge?: string; config: unknown; }): Promise<{ authorizationUrl: string; }>; exchangeCode(args: { code: string; callbackUrl: string; codeVerifier?: string; config: unknown; }): Promise<{ accessToken: string; refreshToken?: string; expiresIn?: number; }>; refreshToken(args: { refreshToken: string; config: unknown; }): Promise<{ accessToken: string; refreshToken?: string; expiresIn?: number; }>; } /** * Context passed to handleHttp for stateless servers. * @unstable This type is subject to change. */ export type StatelessHttpContext = { env: Record; }; /** * Context passed to handleHttp for stateful servers. * @unstable This type is subject to change. */ export type StatefulHttpContext = { env: Record; sessions: { /** * Send an arbitrary message to the session's registered `onMessage` handler. * Returns the handler's response, or undefined if no handler is registered. */ send(sessionId: string, message: unknown): Promise; }; }; /** * Handler for non-MCP HTTP requests (e.g. webhook endpoints on subpaths). * @unstable This type is subject to change. */ export type HandleHttpFn = (request: Request, context: StatelessHttpContext | StatefulHttpContext) => Promise; /** * ServerModule - expected exports from an MCP server entry point */ export interface ServerModule { default: CreateServerFn; configSchema?: z.ZodSchema; createSandboxServer?: CreateSandboxServerFn; /** * Whether the server is stateful. * Stateful servers maintain state between calls within a session. * Stateless servers are fresh for each request. * @default false */ stateful?: boolean; createAuthAdapter?: (context: { env: Record; }) => Promise; handleHttp?: HandleHttpFn; }