Skip to main content

@leanmcp/utils

Utility functions and helpers for LeanMCP SDK.

Features

  • Retry logic - Exponential backoff for resilient operations
  • Response formatting - Format data as JSON, Markdown, HTML, or tables
  • Object utilities - Deep merge, validation, and manipulation
  • Async helpers - Sleep, timeout, and promise utilities

Installation

API Reference

Response Formatting

formatResponse(data, format)

Format data based on specified format type.
Supported formats:
  • json - Pretty-printed JSON
  • markdown - JSON wrapped in markdown code block
  • html - JSON wrapped in HTML pre tag
  • table - Markdown table (for arrays of objects)
  • Default - String conversion

formatAsTable(data)

Format array of objects as a Markdown table.

Object Utilities

deepMerge(target, …sources)

Deep merge multiple objects.

isObject(item)

Check if value is a plain object.

Async Utilities

retry(fn, options)

Retry a function with exponential backoff.
Retry logic:
  • Attempt 1: Immediate
  • Attempt 2: Wait 1000ms
  • Attempt 3: Wait 2000ms
  • Attempt 4: Wait 4000ms

sleep(ms)

Async sleep function.

timeout(promise, ms)

Add timeout to a promise.

Usage Examples

Formatting API Responses

Resilient API Calls

Deep Configuration Merging

Type Definitions

All functions are fully typed with TypeScript: