MCP Server Framework - v1.0.0
    Preparing search index...

    Interface ServerLifecycleHooks

    Lifecycle hooks for MCP servers.

    Implement these hooks to execute code at specific points in the server lifecycle. All hooks are optional and async-friendly.

    const hooks: ServerLifecycleHooks = {
    onStarting: async () => {
    await initializeDatabase();
    },
    onStarted: async () => {
    logger.info('Server is ready');
    },
    onStopping: async () => {
    await flushMetrics();
    },
    onStopped: async () => {
    await closeConnections();
    },
    };
    interface ServerLifecycleHooks {
        onClientConnected?: (sessionId: string) => void | Promise<void>;
        onClientDisconnected?: (sessionId: string) => void | Promise<void>;
        onError?: (error: Error) => void | Promise<void>;
        onStarted?: () => void | Promise<void>;
        onStarting?: () => void | Promise<void>;
        onStopped?: () => void | Promise<void>;
        onStopping?: () => void | Promise<void>;
    }
    Index

    Properties

    onClientConnected?: (sessionId: string) => void | Promise<void>

    Called when a client connects to the server. Use for session initialization or connection tracking.

    Important: This hook is invoked asynchronously (fire-and-forget) during session creation. It is NOT awaited — the session is returned to the transport before this hook completes. For async setup that must finish before the session handles requests, use the tool/resource handlers themselves.

    During session close, onClientDisconnected IS awaited.

    onClientDisconnected?: (sessionId: string) => void | Promise<void>

    Called when a client disconnects from the server. Use for session cleanup or connection tracking.

    onError?: (error: Error) => void | Promise<void>

    Called when an unhandled error occurs. Use for error reporting/logging.

    onStarted?: () => void | Promise<void>

    Called after server is fully started and accepting connections. Use for logging, metrics, or notifying external systems.

    onStarting?: () => void | Promise<void>

    Called before server starts accepting connections. Use for initialization tasks (database, cache, external services).

    onStopped?: () => void | Promise<void>

    Called after server has fully stopped. Use for final cleanup (close file handles, database connections).

    onStopping?: () => void | Promise<void>

    Called when server begins shutdown process. Use for graceful cleanup (flush buffers, notify clients).