Skip to main content

Server Adapters API Reference

Complete reference for @juspay/neurolink/server -- the HTTP server layer for NeuroLink.

import {
createServer,
createAllRoutes,
HonoServerAdapter,
// ... ~120 exports
} from "@juspay/neurolink/server";

Table of Contents


Factory and Base Class

Convenience function that creates a server adapter from a NeuroLink instance.

function createServer(
neurolink: NeuroLink,
options?: {
framework?: ServerFramework; // default: "hono"
config?: ServerAdapterConfig;
},
): Promise<BaseServerAdapter>;

ServerAdapterFactory

Static factory class for creating adapters. Supports dynamic imports so unused frameworks are never bundled.

MethodSignatureDescription
create(options: ServerAdapterFactoryOptions) => Promise<BaseServerAdapter>Create adapter by framework name
createHono(neurolink, config?) => Promise<BaseServerAdapter>Shortcut for Hono
createExpress(neurolink, config?) => Promise<BaseServerAdapter>Shortcut for Express
createFastify(neurolink, config?) => Promise<BaseServerAdapter>Shortcut for Fastify
createKoa(neurolink, config?) => Promise<BaseServerAdapter>Shortcut for Koa
registerAdapter(framework, adapterClass) => voidRegister a custom adapter class
isSupported(framework: string) => booleanCheck if a framework is supported
getSupportedFrameworks() => Array<{framework, status, description}>List all supported frameworks
getRecommendedFramework() => ServerFrameworkReturns "hono"

BaseServerAdapter

Abstract base class that all framework adapters extend. Extends EventEmitter.

MethodSignatureDescription
initialize() => Promise<void>Initialize routes, middleware, framework
start() => Promise<void>Start listening (abstract)
stop() => Promise<void>Stop server with graceful shutdown (abstract)
registerRoute(route: RouteDefinition) => voidRegister a single route
registerRouteGroup(group: RouteGroup) => voidRegister a route group with prefix
registerMiddleware(middleware: MiddlewareDefinition) => voidRegister middleware
getStatus() => ServerStatusGet running status, uptime, route count
listRoutes() => RouteDefinition[]List all registered routes
getConfig() => RequiredServerAdapterConfigGet resolved configuration
getLifecycleState() => ServerLifecycleStateGet current lifecycle state
getActiveConnectionCount() => numberNumber of active connections
getFrameworkInstance() => unknownGet underlying framework instance (abstract)

ServerAdapterConfig

All fields are optional; defaults are applied by the base class.

FieldTypeDefaultDescription
portnumber3000Server port
hoststring"0.0.0.0"Server host
basePathstring"/api"Base path for all routes
corsCORSConfigenabled, origins ["*"]CORS settings
rateLimitRateLimitConfigenabled, 100 req/15 minRate limiting
bodyParserBodyParserConfigenabled, 10 MB limitBody parsing
loggingLoggingConfigenabled, level "info"Request logging
timeoutnumber30000Request timeout (ms)
enableMetricsbooleantrueExpose /api/metrics
enableSwaggerbooleanfalseEnable OpenAPI docs
disableBuiltInHealthbooleanfalseSkip built-in /health, /ready
redactionRedactionConfigdisabledStream redaction settings
shutdownShutdownConfig30s shutdown, 15s drainGraceful shutdown behavior

ShutdownConfig

FieldTypeDefaultDescription
gracefulShutdownTimeoutMsnumber30000Max time for entire shutdown
drainTimeoutMsnumber15000Max time to drain connections
forceClosebooleantrueForce-close after timeout

Server Lifecycle States

"uninitialized" | "initializing" | "initialized" | "starting" | "running" | "draining" | "stopping" | "stopped" | "error"

Events (ServerAdapterEvents)

EventPayload
initialized{ config, routeCount, middlewareCount }
started{ port, host, timestamp }
stopped{ uptime, timestamp }
request{ requestId, method, path, timestamp }
response{ requestId, statusCode, duration, timestamp }
error{ requestId?, error, timestamp }

Framework Adapters

All adapters extend BaseServerAdapter and share the same public API. They differ in which underlying HTTP framework they wrap.

ClassFrameworkMulti-runtimeNotes
HonoServerAdapterHonoNode.js, Bun, Deno, EdgeRecommended. Auto-detects runtime.
ExpressServerAdapterExpressNode.jsDynamic-imports express, cors, express-rate-limit
FastifyServerAdapterFastifyNode.jsDynamic-imports fastify
KoaServerAdapterKoaNode.jsDynamic-imports koa, @koa/router, @koa/cors
import { HonoServerAdapter } from "@juspay/neurolink/server";

const adapter = new HonoServerAdapter(neurolink, { port: 8080 });
await adapter.initialize();
await adapter.start();

Middleware

All middleware factory functions return MiddlewareDefinition objects. Register them with adapter.registerMiddleware(mw).

type MiddlewareDefinition = {
name: string;
order?: number; // lower = earlier (default varies)
handler: MiddlewareHandler;
paths?: string[]; // apply to these paths (default: all)
excludePaths?: string[]; // skip these paths
};

type MiddlewareHandler = (
ctx: ServerContext,
next: () => Promise<unknown>,
) => Promise<unknown>;

Authentication Middleware

createAuthMiddleware(config)

General-purpose authentication middleware supporting bearer, API key, basic, and custom strategies.

function createAuthMiddleware(config: AuthConfig): MiddlewareDefinition;

type AuthConfig = {
type: "bearer" | "api-key" | "basic" | "custom";
validate: (token: string, ctx: ServerContext) => Promise<AuthResult | null>;
headerName?: string; // default varies by type
skipPaths?: string[];
errorMessage?: string; // default: "Authentication required"
extractToken?: (ctx: ServerContext) => string | null; // for "custom" type
skipDevPlayground?: boolean; // default: true (skips auth for dev playground headers)
};

type AuthResult = {
id: string;
email?: string;
roles?: string[];
metadata?: Record<string, unknown>;
};

createBearerAuthMiddleware(validate, options?)

Simplified bearer token authentication.

function createBearerAuthMiddleware(
validate: TokenValidator,
options?: BearerAuthOptions,
): MiddlewareDefinition;

type TokenValidator = (
token: string,
) => Promise<AuthenticatedUser | null> | AuthenticatedUser | null;

type BearerAuthOptions = {
required?: boolean; // default: true
headerName?: string; // default: "authorization"
skipPaths?: string[];
};

createApiKeyAuthMiddleware(store, options?)

API key authentication using an ApiKeyStore.

function createApiKeyAuthMiddleware(
store: ApiKeyStore,
options?: ApiKeyAuthOptions,
): MiddlewareDefinition;

type ApiKeyAuthOptions = {
headerName?: string; // default: "x-api-key"
skipPaths?: string[];
};

ApiKeyStore

In-memory API key store.

MethodSignatureDescription
addKey(apiKey: string, user: AuthenticatedUser) => voidRegister a key
validate(apiKey: string) => AuthenticatedUser | nullValidate a key
removeKey(apiKey: string) => booleanRemove a key
clear() => voidRemove all keys
sizenumber (getter)Number of stored keys

createRoleMiddleware(config) / createRoleAuthMiddleware(requiredRoles, options?)

Role-based access control. Place after authentication middleware.

function createRoleMiddleware(config: {
requiredRoles: string[];
requireAll?: boolean; // default: false (any role matches)
errorMessage?: string;
}): MiddlewareDefinition;

// Simplified version
function createRoleAuthMiddleware(
requiredRoles: string[],
options?: { requireAll?: boolean },
): MiddlewareDefinition;

createPermissionAuthMiddleware(requiredPermissions, options?)

Permission-based access control.

function createPermissionAuthMiddleware(
requiredPermissions: string[],
options?: { requireAll?: boolean },
): MiddlewareDefinition;

Rate Limiting Middleware

createRateLimitMiddleware(config)

Fixed-window rate limiter with configurable store.

function createRateLimitMiddleware(
config: RateLimitMiddlewareConfig,
): MiddlewareDefinition;

type RateLimitMiddlewareConfig = {
maxRequests: number;
windowMs: number;
message?: string;
skipPaths?: string[];
keyGenerator?: (ctx: ServerContext) => string; // default: IP address
onRateLimitExceeded?: (ctx: ServerContext, retryAfter: number) => unknown;
store?: RateLimitStore; // default: InMemoryRateLimitStore
};

Sets response headers: X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, and Retry-After on 429.

createSlidingWindowRateLimitMiddleware(config)

Sliding-window variant for smoother rate limiting.

function createSlidingWindowRateLimitMiddleware(
config: RateLimitMiddlewareConfig & { subWindows?: number },
): MiddlewareDefinition;

createFixedWindowRateLimitMiddleware(config, store?)

Fixed-window rate limiter with store as a separate parameter.

function createFixedWindowRateLimitMiddleware(
config: FixedWindowRateLimitConfig,
store?: RateLimitStore,
): MiddlewareDefinition;

InMemoryRateLimitStore

Default in-memory rate limit store implementing RateLimitStore.

type RateLimitStore = {
get(key: string): Promise<RateLimitEntry | undefined>;
set(key: string, entry: RateLimitEntry): Promise<void>;
increment(key: string, windowMs: number): Promise<RateLimitEntry>;
reset(key: string): Promise<void>;
};

Also exported as MemoryRateLimitStore (alias).


Validation Middleware

createRequestValidationMiddleware(config)

Schema-based request validation for body, query, params, and headers.

function createRequestValidationMiddleware(
config: ValidationConfig,
): MiddlewareDefinition;

type ValidationConfig = {
bodySchema?: ValidationSchema;
querySchema?: ValidationSchema;
paramsSchema?: ValidationSchema;
headersSchema?: ValidationSchema;
customValidator?: (ctx: ServerContext) => Promise<void>;
skipPaths?: string[];
errorFormatter?: (errors: ValidationError[]) => unknown;
};

type ValidationSchema = {
required?: string[];
properties?: Record<string, PropertySchema>;
additionalProperties?: boolean;
};

type PropertySchema = {
type: "string" | "number" | "boolean" | "object" | "array";
minimum?: number;
maximum?: number;
minLength?: number;
maxLength?: number;
minItems?: number;
maxItems?: number;
pattern?: string;
enum?: unknown[];
default?: unknown;
validate?: (value: unknown) => boolean | string;
};

Also exported as createValidationMiddleware (alias).

createBodyValidationMiddleware(schema) / createQueryValidationMiddleware(schema)

Convenience wrappers for body-only or query-only validation.

createFieldValidator(fieldName, rules)

Returns a (value: unknown) => void function that throws ValidationError on failure.

CommonSchemas

Pre-built ValidationSchema objects: uuid, email, pagination, sorting, idParam, dateRange, search.

ValidationError (middleware)

Re-exported from errors.ts. Contains an errors array of { field, message, value? }.


Caching Middleware

createCacheMiddleware(config)

Response caching with LRU eviction and per-path TTL support.

function createCacheMiddleware(config: CacheConfig): MiddlewareDefinition;

type CacheConfig = {
ttlMs: number;
maxSize?: number; // default: 1000
keyGenerator?: (ctx: ServerContext) => string;
methods?: string[]; // default: ["GET"]
paths?: string[];
excludePaths?: string[];
store?: CacheStore; // default: InMemoryCacheStore
includeQuery?: boolean; // default: true
ttlByPath?: Record<string, number>;
};

Sets response headers: X-Cache (HIT / MISS), X-Cache-Age, Cache-Control.

createCacheInvalidator(store)

Returns { invalidate(pattern), clear() } for programmatic cache invalidation.

InMemoryCacheStore

LRU cache store implementing CacheStore.

type CacheStore = {
get(key: string): Promise<CacheEntry | undefined>;
set(key: string, entry: CacheEntry): Promise<void>;
delete(key: string): Promise<void>;
clear(): Promise<void>;
};

type CacheEntry = {
data: unknown;
createdAt: number;
ttlMs: number;
headers?: Record<string, string>;
};

LRUCache<K, V>

Generic synchronous LRU cache. Methods: get, set, has, delete, clear, size.

ResponseCacheStore<T>

Synchronous response cache with TTL. Methods: get, set, has, invalidate, invalidateByPattern, clear, size.


Common Middleware

FactoryOrderDescription
createTimingMiddleware()0Adds X-Response-Time and Server-Timing headers
createRequestIdMiddleware(options?)0Ensures every request has an X-Request-ID header
createErrorHandlingMiddleware(options?)1Catches errors and formats consistent error responses
createSecurityHeadersMiddleware(options?)2Adds X-Frame-Options, X-Content-Type-Options, HSTS, CSP, etc.
createLoggingMiddleware(options?)3Logs request/response information; skips health endpoints by default
createCompressionMiddleware(options?)5Signals compression preference to adapters

createRequestIdMiddleware options

{
headerName?: string; // default: "x-request-id"
prefix?: string; // default: "req"
generator?: () => string;
}

createErrorHandlingMiddleware options

{
includeStack?: boolean; // default: false
onError?: (error: Error, ctx: ServerContext) => unknown;
logErrors?: boolean; // default: true
}

createSecurityHeadersMiddleware options

{
contentSecurityPolicy?: string;
frameOptions?: "DENY" | "SAMEORIGIN" | false; // default: "DENY"
contentTypeOptions?: "nosniff" | false; // default: "nosniff"
hstsMaxAge?: number | false; // default: 31536000
referrerPolicy?: string | false; // default: "strict-origin-when-cross-origin"
customHeaders?: Record<string, string>;
}

createLoggingMiddleware options

{
logBody?: boolean; // default: false
logResponse?: boolean; // default: false
logger?: { info, error };
skipPaths?: string[]; // default: ["/health", "/ready", "/metrics"]
}

createCompressionMiddleware options

{
threshold?: number; // default: 1024 bytes
contentTypes?: string[]; // default: text/*, application/json, etc.
}

Abort Signal Middleware

createAbortSignalMiddleware(options?)

Attaches an AbortController to ctx.abortSignal and ctx.abortController for handling client disconnections and request timeouts.

function createAbortSignalMiddleware(
options?: AbortSignalMiddlewareOptions,
): MiddlewareDefinition;

type AbortSignalMiddlewareOptions = {
onAbort?: (ctx: ServerContext) => void;
timeout?: number; // request timeout in ms
};

createExpressAbortMiddleware(options?)

Express-specific middleware that sets res.locals.abortSignal and res.locals.abortController.

function createExpressAbortMiddleware(
options?: AbortSignalMiddlewareOptions,
): (req, res, next) => void;

Deprecation Middleware

createDeprecationMiddleware(config)

Adds RFC 8594 deprecation headers (Deprecation, Sunset, Link, X-Deprecation-Notice) to responses for routes marked as deprecated.

function createDeprecationMiddleware(
config: DeprecationConfig,
): MiddlewareDefinition;

type DeprecationConfig = {
routes: RouteDefinition[];
noticeHeader?: string; // default: "X-Deprecation-Notice"
includeLink?: boolean; // default: true
};

Stream Redaction

Redaction is disabled by default (opt-in security feature).

redactStreamChunk(chunk, config?)

Redact sensitive fields from a DataStreamEvent chunk. Returns the chunk unchanged when config.enabled is falsy.

function redactStreamChunk(
chunk: DataStreamEvent,
config?: RedactionConfig,
): DataStreamEvent;

createStreamRedactor(config?)

Returns a reusable transform function <T>(chunk: T) => T. No-op when redaction is disabled.

function createStreamRedactor(config?: RedactionConfig): <T>(chunk: T) => T;

RedactionConfig

type RedactionConfig = {
enabled?: boolean; // default: false
additionalFields?: string[];
preserveFields?: string[];
redactToolArgs?: boolean; // default: true (when enabled)
redactToolResults?: boolean; // default: true (when enabled)
placeholder?: string; // default: "[REDACTED]"
};

Default redacted fields: request, args, result, apiKey, token, authorization, credentials, password, secret.


MCP Body Attachment Middleware

createMCPBodyAttachmentMiddleware()

Bridges Fastify's body parsing with MCP SDK expectations by attaching request.body to request.raw.body.

function createMCPBodyAttachmentMiddleware(): MiddlewareDefinition;

fastifyMCPBodyHook(request)

Lower-level Fastify preHandler hook for the same purpose.

function fastifyMCPBodyHook(request: {
raw: { body?: unknown };
body?: unknown;
}): Promise<void>;

Route Groups

Route group factories return RouteGroup objects. Register them with adapter.registerRouteGroup(group).

createAllRoutes(basePath?, options?)

Creates all standard route groups in one call.

function createAllRoutes(
basePath?: string, // default: "/api"
options?: CreateRoutesOptions,
): RouteGroup[];

type CreateRoutesOptions = {
enableSwagger?: boolean;
getRoutes?: () => RouteDefinition[];
};

registerAllRoutes(adapter, basePath?, options?)

Registers all route groups with an adapter. If the adapter has listRoutes(), auto-binds it for OpenAPI spec generation.

function registerAllRoutes(
adapter: { registerRouteGroup; listRoutes? },
basePath?: string,
options?: CreateRoutesOptions,
): void;

Individual Route Factories

FactoryPrefixEndpoints
createAgentRoutes(basePath?)/agentPOST /agent/execute -- Execute agent
POST /agent/stream -- Stream agent response (SSE)
GET /agent/providers -- List available providers
POST /agent/embed -- Generate single embedding
POST /agent/embed-many -- Generate batch embeddings
createToolRoutes(basePath?)/toolsGET /tools -- List all tools
GET /tools/search -- Search tools by query
GET /tools/:name -- Get tool details
POST /tools/:name/execute -- Execute tool
POST /tools/execute -- Execute tool (body-based)
createMCPRoutes(basePath?)/mcpGET /mcp/servers -- List MCP servers
GET /mcp/servers/:name -- Get server status
GET /mcp/servers/:name/tools -- List server tools
POST /mcp/servers/:name/tools/:toolName/execute -- Execute server tool
createMemoryRoutes(basePath?)/memoryGET /memory/sessions -- List sessions
GET /memory/sessions/:id -- Get session details
GET /memory/sessions/:id/messages -- Get session messages
DELETE /memory/sessions/:id -- Delete session
POST /memory/sessions/:sessionId/clear -- Clear session history
createHealthRoutes(basePath?)/healthGET /health -- Basic health check
GET /health/live -- Liveness probe
GET /health/ready -- Readiness probe
GET /health/detailed -- Detailed health with service status
createOpenApiRoutes(basePath?, getRoutes?)/docsGET /docs/openapi.json -- OpenAPI spec (JSON)
GET /docs/openapi.yaml -- OpenAPI spec (YAML)

All route factories accept a basePath parameter (default: "/api").


OpenAPI Generation

OpenAPIGenerator

Class that generates OpenAPI 3.1 specifications from route definitions.

class OpenAPIGenerator {
constructor(config?: OpenAPIGeneratorConfig);
addRoutes(routes: RouteDefinition[]): void;
addRoute(route: RouteDefinition): void;
generate(): OpenAPISpec;
toJSON(pretty?: boolean): string;
toYAML(): string;
}

OpenAPIGeneratorConfig

type OpenAPIGeneratorConfig = {
info?: { title?; version?; description? };
servers?: Array<{ url; description? }>;
includeSecurity?: boolean; // default: true
basePath?: string; // default: "/api"
additionalTags?: Array<{ name; description }>;
customSchemas?: Record<string, JsonObject>;
routes?: RouteDefinition[];
};

OpenAPISpec

type OpenAPISpec = {
openapi: "3.1.0";
info: JsonObject;
servers: JsonObject[];
tags: JsonObject[];
paths: Record<string, JsonObject>;
components: {
schemas: Record<string, JsonObject>;
securitySchemes?: Record<string, JsonObject>;
parameters?: Record<string, JsonObject>;
};
security?: JsonObject[];
};

Factory Functions

FunctionSignatureDescription
createOpenAPIGenerator(config?) => OpenAPIGeneratorCreate generator with defaults
generateOpenAPISpec(routes, config?) => OpenAPISpecOne-shot spec from routes
generateOpenAPIFromConfig(serverConfig, routes?) => OpenAPISpecGenerate from ServerAdapterConfig

Pre-built Schemas

All schemas are plain JSON Schema objects exported from ./openapi/schemas.ts:

SchemaDescription
ErrorResponseSchemaStandard error response
TokenUsageSchemaToken usage breakdown
AgentInputSchemaAgent input (string or multimodal object)
AgentExecuteRequestSchema (OpenAPI)Agent execute request body
AgentExecuteResponseSchemaAgent execute response
ToolCallSchemaTool call object
ProviderInfoSchemaProvider information
ToolParameterSchemaTool parameter definition
ToolDefinitionSchemaFull tool definition
ToolListResponseSchemaTool list response
ToolExecuteRequestSchema (OpenAPI)Tool execute request body
ToolExecuteResponseSchemaTool execute response
MCPServerToolSchemaMCP server tool
MCPServerStatusSchemaMCP server status
MCPServersListResponseSchemaMCP servers list
ConversationMessageSchemaConversation message
SessionSchemaSession object
SessionsListResponseSchemaSessions list
HealthResponseSchemaHealth check response
ReadyResponseSchemaReadiness check response
MetricsResponseSchemaMetrics response
OpenAPISchemasRegistry object containing all schemas above

Templates

ExportDescription
createSuccessResponse(schemaRef)Build a 200 response object
createOpenAPIErrorResponse(code, description)Build an error response object
createStreamingResponse(description)Build a streaming (SSE) response
StandardErrorResponsesMap of 400/401/403/404/429/500 responses
createPathParameter(name, description?)Build a path parameter
createQueryParameter(name, schema, description?)Build a query parameter
createHeaderParameter(name, schema, description?)Build a header parameter
CommonParametersPre-built parameters: sessionId, serverName, toolName, limitQuery, offsetQuery, searchQuery
createGetOperation(...)Build a GET operation
createPostOperation(...)Build a POST operation
createStreamingPostOperation(...)Build a streaming POST operation
createDeleteOperation(...)Build a DELETE operation
BearerSecuritySchemeBearer token security scheme object
ApiKeySecuritySchemeAPI key security scheme object
BasicSecuritySchemeHTTP basic security scheme object
StandardTagsDefault tag definitions (agent, tools, mcp, memory, health, streaming)
createOpenAPIServer(url, description)Build a server object
DefaultServersDefault server list
createApiInfo(title, version, description)Build an info object
NeuroLinkApiInfoDefault NeuroLink API info object

Streaming Utilities

Event Types

type DataStreamEventType =
| "text-start"
| "text-delta"
| "text-end"
| "tool-call"
| "tool-result"
| "data"
| "error"
| "finish";

type DataStreamEvent = {
type: DataStreamEventType;
id?: string;
timestamp: number;
data: unknown;
};

Specialized event types: TextStartEvent, TextDeltaEvent, TextEndEvent, ToolCallEvent, ToolResultEvent, DataEvent, ErrorEvent, FinishEvent.

createDataStreamWriter(config)

Creates a DataStreamWriter that writes events in SSE or NDJSON format.

function createDataStreamWriter(
config: DataStreamWriterConfig,
): DataStreamWriter;

type DataStreamWriterConfig = {
write: (chunk: string) => void | Promise<void>;
close?: () => void | Promise<void>;
format?: "sse" | "ndjson"; // default: "sse"
includeTimestamps?: boolean; // default: true
};

DataStreamWriter interface

MethodSignature
writeTextStart(id: string) => Promise<void>
writeTextDelta(id: string, delta: string) => Promise<void>
writeTextEnd(id: string) => Promise<void>
writeToolCall(toolCall: { id, name, arguments }) => Promise<void>
writeToolResult(toolResult: { id, name, result }) => Promise<void>
writeData(data: unknown) => Promise<void>
writeError(error: { message, code? }) => Promise<void>
close() => Promise<void>

DataStreamResponse

High-level class that creates a ReadableStream<Uint8Array> with a DataStreamWriter interface.

class DataStreamResponse {
constructor(config?: DataStreamResponseConfig);
readonly stream: ReadableStream<Uint8Array>;
readonly headers: Record<string, string>;
getWriter(): DataStreamWriter;
writeTextStart(id: string): Promise<void>;
writeTextDelta(id: string, delta: string): Promise<void>;
writeTextEnd(id: string): Promise<void>;
writeToolCall(toolCall): Promise<void>;
writeToolResult(toolResult): Promise<void>;
writeData(data: unknown): Promise<void>;
writeError(error: { message; code? }): Promise<void>;
finish(options?: { reason?; usage? }): Promise<void>;
close(): void;
isClosed(): boolean;
}

type DataStreamResponseConfig = {
contentType?: "text/event-stream" | "application/x-ndjson";
headers?: Record<string, string>;
keepAliveInterval?: number;
includeTimestamps?: boolean;
};

createDataStreamResponse(config?)

Factory function for DataStreamResponse.

Helper Functions

FunctionSignatureDescription
pipeAsyncIterableToDataStream(iterable, response, options?) => Promise<void>Pipe an async iterable into a DataStreamResponse
createSSEHeaders(additionalHeaders?) => Record<string, string>Standard SSE headers
createNDJSONHeaders(additionalHeaders?) => Record<string, string>Standard NDJSON headers
formatSSEEvent(options: SSEEventOptions) => stringFormat a single SSE message

SSEEventOptions

type SSEEventOptions = {
event?: string;
data: string;
id?: string;
retry?: number;
};

BaseDataStreamWriter

Abstract base class providing isClosed(), onClose(handler), and close().

WebStreamWriter

Concrete class extending BaseDataStreamWriter. Writes SSE events to a ReadableStream<Uint8Array>.

Property/MethodTypeDescription
streamReadableStream<Uint8Array>The readable stream
writeData(data)voidWrite a data event
writeError(message)voidWrite an error event
writeDone()voidWrite a done event
writeEvent(eventType, data)voidWrite a custom event
close()voidClose the stream
isClosed()booleanCheck if closed
onClose(handler)voidRegister a close handler

WebSocket

WebSocketConnectionManager

Manages WebSocket connections, ping/pong, and handler dispatch.

class WebSocketConnectionManager {
constructor(config?: WebSocketConfig);
registerHandler(path: string, handler: WebSocketHandler): void;
getHandler(path: string): WebSocketHandler | undefined;
handleConnection(socket, path, user?): Promise<WebSocketConnection>;
handleMessage(connectionId, data, isBinary): Promise<void>;
handleClose(connectionId, code, reason): Promise<void>;
handleError(connectionId, error): Promise<void>;
getConnection(connectionId): WebSocketConnection | undefined;
getAllConnections(): WebSocketConnection[];
getConnectionsByUser(userId): WebSocketConnection[];
getConnectionsByPath(path): WebSocketConnection[];
send(connectionId, data): void;
broadcast(data, filter?): void;
close(connectionId, code?, reason?): Promise<void>;
closeAll(code?, reason?): Promise<void>;
getConnectionCount(): number;
}

WebSocketConfig

type WebSocketConfig = {
path?: string; // default: "/ws"
maxConnections?: number; // default: 1000
pingInterval?: number; // default: 30000
pongTimeout?: number; // default: 10000
maxMessageSize?: number; // default: 1 MB
auth?: AuthConfig;
};

WebSocketMessageRouter

Routes JSON messages by type field to registered handlers.

class WebSocketMessageRouter {
route(type: string, handler: (conn, payload) => Promise<unknown>): void;
handle(connection, message: WebSocketMessage): Promise<unknown>;
getRoutes(): string[];
}

Creates a WebSocketHandler with pre-registered routes for generate, stream, and tool_call messages.

function createAgentWebSocketHandler(neurolink: unknown): WebSocketHandler;

Validation Utilities (Zod)

Zod schemas and helpers exported from ./utils/validation.ts. Used internally by route handlers.

Zod Schemas

SchemaValidates
AgentExecuteRequestSchemaAgent execute request body
ToolExecuteRequestSchemaTool execute request body
ToolArgumentsSchemaTool arguments (Record<string, unknown>)
SessionIdParamSchema{ sessionId: string }
ServerNameParamSchema{ name: string }
ToolNameParamSchema{ name: string }
ToolSearchQuerySchema{ q?, source?, limit? }

Validation Functions

FunctionSignatureDescription
validateRequest<T>(schema: ZodSchema<T>, data, requestId?) => ValidationResult<T>Validate request body
validateQuery<T>(schema: ZodSchema<T>, query, requestId?) => ValidationResult<T>Validate query params
validateParams<T>(schema: ZodSchema<T>, params, requestId?) => ValidationResult<T>Validate path params
createErrorResponse(code, message, details?, requestId?, httpStatus?) => ErrorResponseBuild a standardized error response
type ValidationResult<T> =
| { success: true; data: T }
| { success: false; error: ErrorResponse };

type ErrorResponse = {
error: { code: string; message: string; details?: unknown };
metadata?: { timestamp: string; requestId?: string };
httpStatus?: number;
};

Error Classes

All error classes extend ServerAdapterError, which extends Error. Every error carries code, category, severity, retryable, and optional context fields (retryAfterMs, requestId, path, method, details, cause).

ServerAdapterError provides:

  • toJSON() -- serializes to { error: { code, message, category, requestId, details, retryAfter } }
  • getHttpStatus() -- maps error code to HTTP status

Error Class Table

ClassHTTP StatusCategoryRetryableDescription
ServerAdapterErrorvariesEXECUTIONnoBase error class
ConfigurationError400CONFIGnoInvalid server configuration
MissingDependencyError500CONFIGnoMissing framework dependency (e.g., express)
RouteConflictError500CONFIGnoDuplicate route registration
RouteNotFoundError404VALIDATIONnoRoute not found
ServerValidationError400VALIDATIONnoRequest validation failed; carries errors[]
AuthenticationError401AUTHENTICATIONnoAuthentication required
InvalidAuthenticationError401AUTHENTICATIONnoInvalid credentials
AuthorizationError403AUTHORIZATIONnoInsufficient permissions
ServerRateLimitError429RATE_LIMITyesRate limit exceeded
HandlerError500EXECUTIONnoRoute handler threw
TimeoutError408EXECUTIONyesOperation timed out
StreamingError500STREAMINGnoStream processing error
StreamAbortedError499STREAMINGnoClient disconnected
WebSocketError500WEBSOCKETyesWebSocket error
WebSocketConnectionError500WEBSOCKETyesWebSocket connection failed
ServerStartError500CONFIGyesServer failed to start
ServerStopError500EXECUTIONnoServer failed to stop
AlreadyRunningError500CONFIGnoServer already running
NotRunningError500CONFIGnoServer not running

Note: ShutdownTimeoutError, DrainTimeoutError, and InvalidLifecycleStateError are used internally by the shutdown lifecycle and are not re-exported from the public index.

wrapError(error, requestId?, path?, method?)

Wraps any error as a ServerAdapterError. Returns the error as-is if it is already a ServerAdapterError; otherwise wraps it in a HandlerError.

function wrapError(
error: unknown,
requestId?: string,
path?: string,
method?: string,
): ServerAdapterError;

ErrorRecoveryStrategies

A Record<ErrorCategoryType, { strategy, maxRetries, baseDelayMs }> mapping each error category to a recommended recovery strategy ("retry", "exponentialBackoff", "circuitBreak", or "fail").


Type Exports

These are type-only exports (no runtime value).

Configuration Types

TypeDescription
ServerAdapterConfigServer configuration (all optional)
RequiredServerAdapterConfigSame, with defaults applied (all required)
CORSConfigCORS settings
RateLimitConfigRate limit settings
BodyParserConfigBody parser settings
LoggingConfigLogging settings
StreamingConfigStreaming response configuration
RedactionConfigStream redaction settings
ShutdownConfigGraceful shutdown settings

Request/Response Types

TypeDescription
ServerContextRequest context passed to all handlers and middleware
ServerResponse<T>Generic server response envelope
AgentExecuteRequestAgent execute request body
AgentExecuteResponseAgent execute response
ToolExecuteRequestTool execute request body
ToolExecuteResponseTool execute response
MCPServerStatusResponseMCP server status
HealthResponseHealth check response
ReadyResponseReadiness check response
ErrorResponseStandardized error response
ValidationResult<T>Success/failure discriminated union

Route and Middleware Types

TypeDescription
HttpMethod"GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "OPTIONS"
RouteDefinitionFull route definition
RouteGroupGroup of routes with prefix and optional middleware
RouteHandler<T>(ctx: ServerContext) => Promise<T | ServerResponse<T> | AsyncIterable<unknown>>
MiddlewareDefinitionMiddleware definition with name, order, handler, paths
MiddlewareHandler(ctx, next) => Promise<unknown>
CreateRoutesOptionsOptions for createAllRoutes

Factory Types

TypeDescription
ServerFramework"hono" | "express" | "fastify" | "koa"
ServerAdapterFactoryOptions{ framework, neurolink, config? }
ServerStatusServer status snapshot

Streaming Types

TypeDescription
DataStreamWriterWriter interface for data streams
DataStreamEventTypeEvent type union
DataStreamEventBase event
TextStartEvent / TextDeltaEvent / TextEndEventText streaming events
ToolCallEvent / ToolResultEventTool events
DataEvent / ErrorEvent / FinishEventUtility events
DataStreamWriterConfigWriter factory config
DataStreamResponseConfigResponse factory config
SSEEventOptionsSSE formatting options
SSEWriteOptionsSSE write options

WebSocket Types

TypeDescription
WebSocketConfigWebSocket server settings
WebSocketConnectionConnection object
WebSocketHandlerEvent handler interface (onOpen, onMessage, onClose, onError)
WebSocketMessageMessage object
WebSocketMessageType"text" | "binary" | "ping" | "pong" | "close"
WebSocketAuthConfigAuth config for WebSocket (same shape as AuthConfig from types)
AuthenticatedUserUser object with id, email, name, roles, permissions, metadata
AuthStrategy"bearer" | "apiKey" | "basic" | "custom" | "none"

Error Types

TypeDescription
ErrorCategoryTypeError category union
ErrorSeverityTypeError severity union
ServerAdapterErrorCodeTypeError code union
ServerAdapterErrorContextContext object for error construction

Constants

ErrorCategory

const ErrorCategory = {
CONFIG,
VALIDATION,
EXECUTION,
EXTERNAL,
RATE_LIMIT,
AUTHENTICATION,
AUTHORIZATION,
STREAMING,
WEBSOCKET,
} as const;

ErrorSeverity

const ErrorSeverity = {
LOW,
MEDIUM,
HIGH,
CRITICAL,
} as const;

ServerAdapterErrorCode

const ServerAdapterErrorCode = {
INVALID_CONFIG,
MISSING_DEPENDENCY,
FRAMEWORK_INIT_FAILED,
ROUTE_NOT_FOUND,
ROUTE_CONFLICT,
INVALID_ROUTE,
HANDLER_ERROR,
TIMEOUT,
MIDDLEWARE_ERROR,
RATE_LIMIT_EXCEEDED,
AUTH_REQUIRED,
AUTH_INVALID,
FORBIDDEN,
STREAM_ERROR,
STREAM_ABORTED,
WEBSOCKET_ERROR,
WEBSOCKET_CONNECTION_FAILED,
VALIDATION_ERROR,
SCHEMA_ERROR,
START_FAILED,
STOP_FAILED,
ALREADY_RUNNING,
NOT_RUNNING,
} as const;