> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mcp-assistant.in/llms.txt
> Use this file to discover all available pages before exploring further.

# Redis

> Use Redis as the mcp-ts session storage backend for production and serverless deployments, with automatic expiration, atomic operations, and distributed access.

**Recommended for production and serverless deployments.**

Redis provides distributed, persistent storage with automatic expiration management. Perfect for:

* Production environments
* Serverless deployments (Vercel, AWS Lambda)
* Multi-instance applications
* High-availability setups

## Installation

```bash theme={null}
npm install @mcp-ts/sdk ioredis
```

## Configuration

```bash theme={null}
# Explicit selection (optional)
MCP_TS_STORAGE_TYPE=redis

# Redis connection URL (required)
REDIS_URL=redis://localhost:6379

# Or for cloud Redis with TLS
REDIS_URL=rediss://default:password@host.upstash.io:6379
```

## Features

* **Automatic session expiration** for pending sessions and dormant active sessions
* **Atomic operations** for data consistency
* **Distributed storage** across instances
* **Production-ready scalability**

## Usage

### Option 1: Automatic Detection (Recommended)

When `REDIS_URL` is present in your environment, the global `sessions` proxy automatically uses the Redis backend.

```typescript theme={null}
import { sessions } from '@mcp-ts/sdk/server';

// This will use Redis automatically if env vars are set
const sessionId = await sessions.generateSessionId();

await sessions.create({
  sessionId,
  userId: 'user-123',
  serverUrl: 'https://mcp.example.com',
  callbackUrl: 'https://app.com/callback',
  transportType: 'streamable-http',
  status: 'active',
  createdAt: Date.now(),
});
```

### Option 2: Manual Instantiation

If you want to manage the Redis client yourself or use multiple storage backends:

```typescript theme={null}
import Redis from 'ioredis';
import { RedisStorageBackend } from '@mcp-ts/sdk/server';

const redis = new Redis(process.env.REDIS_URL!);
const redisBackend = new RedisStorageBackend(redis);

await redisBackend.create({
  sessionId: 'test-session',
  userId: 'user-123',
  serverUrl: 'https://mcp.example.com',
  callbackUrl: 'https://app.com/callback',
  transportType: 'streamable-http',
  status: 'active',
  createdAt: Date.now(),
});
```

## Troubleshooting

### Redis Connection Failed

```bash theme={null}
# Verify Redis is running
redis-cli ping  # Should return PONG

# Check connection string
echo $REDIS_URL

# Test with redis-cli
redis-cli -u $REDIS_URL ping
```
