mcp-use
Version:
Opinionated MCP Framework for TypeScript (@modelcontextprotocol/sdk compatible) - Build MCP Agents, Clients and Servers with support for ChatGPT Apps, Code Mode, OAuth, Notifications, Sampling, Observability and more.
70 lines • 3.02 kB
TypeScript
/**
* Session Manager
*
* Simplified session management for tracking active sessions.
* The native SDK transport handles session lifecycle internally.
*/
import type { Context } from "hono";
import type { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
/**
* Serializable session metadata
*
* This is the data that can be stored in external stores (Redis, Postgres).
* It excludes runtime objects like transport and server instances.
*/
export interface SessionMetadata {
/** Timestamp of last activity for idle timeout tracking */
lastAccessedAt: number;
/** Minimum log level for filtering log messages (RFC 5424 levels) */
logLevel?: string;
/** Client capabilities advertised during initialization */
clientCapabilities?: Record<string, unknown>;
/** Client info (name, version) */
clientInfo?: Record<string, unknown>;
/** Protocol version negotiated during initialization */
protocolVersion?: string;
/** Progress token for current tool call (if any) */
progressToken?: number;
}
/**
* Complete session data including runtime instances
*
* This extends SessionMetadata with non-serializable runtime objects.
* The full SessionData is only available in memory on the server handling the session.
*/
export interface SessionData extends SessionMetadata {
/** Reference to this session's transport instance (not serializable) */
transport: Transport;
/** Reference to this session's server instance (not serializable) */
server?: McpServer;
/** Hono context for this session's current request (not serializable) */
context?: Context;
/** Function to send notifications to the client (not serializable) */
sendNotification?: (notification: {
method: string;
params: Record<string, unknown>;
}) => Promise<void>;
/** Express-like response object for notifications (not serializable) */
expressRes?: Response | Record<string, unknown>;
/** Hono context for direct response access (not serializable) */
honoContext?: Context;
}
/**
* Start idle session cleanup interval
*
* Monitors sessions and removes them if they've been inactive for too long.
* Also cleans up the associated transports to prevent "Server not initialized" errors
* when clients try to reconnect with expired session IDs.
*
* @param sessions - Map of active sessions
* @param idleTimeoutMs - Idle timeout in milliseconds
* @param transports - Optional map of transports by session ID (to close on cleanup)
* @param mcpServerInstance - Optional MCP server instance for cleanup callbacks
*/
export declare function startIdleCleanup(sessions: Map<string, SessionData>, idleTimeoutMs: number, transports?: Map<string, {
close?: () => Promise<void> | void;
}>, mcpServerInstance?: {
cleanupSessionSubscriptions?: (sessionId: string) => void;
}): NodeJS.Timeout | undefined;
//# sourceMappingURL=session-manager.d.ts.map