Skip to main content

sessions

Global sessions instance that automatically selects the appropriate backend based on environment configuration. It follows a Repository-inspired interface for managing MCP sessions.
import { sessions } from '@mcp-ts/sdk/server';

Configuration

The storage backend is selected automatically:
# Redis (Production)
MCP_TS_STORAGE_TYPE=redis
REDIS_URL=redis://localhost:6379

# File System (Development)
MCP_TS_STORAGE_TYPE=file
MCP_TS_STORAGE_FILE=./sessions.json

# In-Memory (Testing - Default)
MCP_TS_STORAGE_TYPE=memory

Storage Methods

generateSessionId(): string Generate a unique session ID.
const sessionId = sessions.generateSessionId();

create(session: Session): Promise<void> Create a new session. Throws if session already exists.
await sessions.create({
  sessionId: 'abc123',
  userId: 'user-123',
  serverId: 'server-id',
  serverName: 'My Server',
  serverUrl: 'https://mcp.example.com',
  callbackUrl: 'https://myapp.com/callback',
  transportType: 'sse',
  status: 'active',
  createdAt: Date.now(),
});

update(userId: string, sessionId: string, data: Partial<Session>): Promise<void> Update an existing session with partial data. Throws if session doesnโ€™t exist.
await sessions.update('user-123', 'abc123', {
  status: 'pending',
});
Expiration is managed by the backend. Pending sessions (status: 'pending') receive a short OAuth-window expiration; active sessions (status: 'active') are retained until explicit deletion or dormant-session cleanup.
get(userId: string, sessionId: string): Promise<Session | null> Retrieve session data.
const session = await sessions.get('user-123', 'abc123');

list(userId: string): Promise<Session[]> Get all session data for a user ID.
const sessionList = await sessions.list('user-123');

listIds(userId: string): Promise<string[]> Get all session IDs for a user ID.
const sessionIds = await sessions.listIds('user-123');

delete(userId: string, sessionId: string): Promise<void> Delete a session.
await sessions.delete('user-123', 'abc123');

listAllIds(): Promise<string[]> Get all session IDs across all users (admin operation).
const allSessions = await sessions.listAllIds();

clearAll(): Promise<void> Clear all sessions (admin operation).
await sessions.clearAll();

cleanupExpired(): Promise<void> Clean up expired pending sessions and dormant active sessions where the backend supports scheduled/manual cleanup.
await sessions.cleanupExpired();

disconnect(): Promise<void> Disconnect from storage backend.
await sessions.disconnect();

Custom Session Stores

You can also use specific session store backends directly:
import { 
  RedisStorageBackend,
  MemoryStorageBackend,
  FileStorageBackend 
} from '@mcp-ts/sdk/server';
import { Redis } from 'ioredis';

// Redis
const redis = new Redis(process.env.REDIS_URL);
const redisStorage = new RedisStorageBackend(redis);

// File System
const fileStorage = new FileStorageBackend({ path: './sessions.json' });
await fileStorage.init();

// In-Memory
const memoryStorage = new MemoryStorageBackend();