MCP Examples

Real-world examples of using NoLag MCP with AI assistants to set up infrastructure, debug issues, and generate client code.

Example 1: Setting Up a Notification System

Conversation

You:

I need a notification system for my SaaS app. Users should receive notifications for new comments, mentions, and system announcements. Set this up with NoLag.

What Claude Does

  1. Calls nolag_create_app with name "notifications-service"
  2. Calls nolag_create_room to create a "notifications" room
  3. Calls nolag_create_actor with type "device" for the web client
  4. Calls nolag_create_actor with type "server" for the backend that publishes notifications
  5. Calls nolag_generate_code to produce client code

Generated Client Code

import { NoLag } from '@nolag/js-sdk'

// Use the actor token created via nolag_create_actor
const client = NoLag(process.env.NOLAG_ACTOR_TOKEN!)

await client.connect()

// Join the notifications room created via nolag_create_room
const room = client.room('notifications')

// Subscribe to user-specific notifications
room.on('comments', (data, meta) => {
  showNotification({ type: 'comment', ...data })
})

room.on('mentions', (data, meta) => {
  showNotification({ type: 'mention', ...data })
})

// Subscribe to system announcements
room.on('announcements', (data, meta) => {
  showNotification({ type: 'system', ...data })
})

function showNotification(notification: any) {
  console.log('New notification:', notification)
}

Example 2: Configuring Webhooks

Conversation

You:

I want my backend to receive a webhook whenever a message is published to my "orders" app. Also set up a hydration webhook so new subscribers get the current order state. My webhook endpoint is https://api.example.com/webhooks/nolag.

Claude:

Uses nolag_list_apps to find the "orders" app, then nolag_configure_webhooks

I've configured both webhooks for your "orders" app:

  • Trigger webhook: Will POST to your endpoint whenever a message is published
  • Hydration webhook: Will POST when actors subscribe, so you can send them current state

Webhook Configuration

// What Claude configures via nolag_configure_webhooks
{
  "appId": "app_abc123",
  "triggerWebhook": {
    "url": "https://api.example.com/webhooks/nolag",
    "headers": {
      "X-Webhook-Secret": "your-secret"
    }
  },
  "hydrationWebhook": {
    "url": "https://api.example.com/webhooks/hydration",
    "headers": {
      "X-Webhook-Secret": "your-secret"
    }
  }
}

Example 3: Debugging Message Delivery

Conversation

You:

Users are complaining that they don't receive messages in the support room. Can you investigate?

Claude:

I'll investigate. Let me check the app, room, and recent messages.

Uses nolag_list_apps, nolag_list_rooms, nolag_get_messages, nolag_get_webhook_config, nolag_list_webhook_dlq

Investigation Results

Findings:

App"support-app" (app_abc123)
Room"support" -- enabled, dynamic room
Recent Messages15 messages in the last hour on the "chat" topic
Webhook DLQ8 entries with status "dead" -- all HTTP 500 errors

Root Cause:

Messages are being published successfully to the room. The trigger webhook is failing because the receiving server is returning 500 errors. The DLQ shows 8 failed deliveries in the last 24 hours.

Solution:

Check your webhook receiver at the URL shown in nolag_get_webhook_config. The server is returning 500 errors, which means the issue is on your backend, not NoLag.

Example 4: Setting Up a Multiplayer Game

Conversation

You:

I'm building a multiplayer game. I need a lobby room where players wait for matches, and then individual game rooms for each match (up to 4 players). Set up the NoLag infrastructure.

Claude:

Uses nolag_create_app, nolag_create_room (lobby), nolag_create_actor (device type for game client), nolag_generate_code

I've set up your game infrastructure:

  • App: "multiplayer-game"
  • Room: "lobby" (static room for matchmaking)
  • Actor: "game-client" (device type) -- save the token!

For individual game rooms, create them dynamically with nolag_create_room when a match starts (e.g., "game-room-{matchId}"). Players join that room for the duration of the match, then you can delete it with nolag_delete_room when the match ends.

Generated Game Client Code

import { NoLag } from '@nolag/js-sdk'

// Actor token from nolag_create_actor (type: "device")
const client = NoLag(process.env.NOLAG_ACTOR_TOKEN!)

await client.connect()

// Join the lobby room
const lobby = client.room('lobby')
lobby.on('match-found', (data) => {
  console.log('Match found! Joining game room:', data.gameRoomId)
  joinGame(data.gameRoomId)
})

// Join a specific game room
function joinGame(gameRoomId: string) {
  const game = client.room(gameRoomId)

  game.on('position', (data, meta) => {
    updatePlayerPosition(meta.actorName, data.x, data.y)
  })

  // Send position updates
  setInterval(() => {
    game.emit('position', { x: player.x, y: player.y })
  }, 50) // 20 updates per second
}

function updatePlayerPosition(name: string, x: number, y: number) {
  console.log(`${name} moved to (${x}, ${y})`)
}

Example 5: Generating IoT Dashboard Code

Conversation

You:

Generate TypeScript code for a real-time IoT dashboard that subscribes to temperature and humidity sensor data in my "sensors" app.

Claude:

Uses nolag_list_apps to find the app, then nolag_generate_code with language "typescript", useCase "Real-time dashboard for IoT sensor data", topics ["temperature", "humidity"]

Here's the generated code for your IoT dashboard:

import { NoLag } from '@nolag/js-sdk'

// Real-time dashboard for IoT sensor data
async function main() {
  const client = NoLag(process.env.NOLAG_ACTOR_TOKEN!)

  await client.connect()
  console.log('Connected to NoLag')

  const room = client.room('sensors')

  // Subscribe to temperature
  room.on('temperature', (data, meta) => {
    console.log('Message on temperature:', data)
  })

  // Subscribe to humidity
  room.on('humidity', (data, meta) => {
    console.log('Message on humidity:', data)
  })

  // Graceful shutdown
  process.on('SIGINT', () => {
    client.close()
    process.exit(0)
  })
}

main().catch(console.error)

Next Steps