@leanmcp/utils 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +289 -262
  3. package/package.json +54 -54
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2025 LeanMCP Contributors
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2025 LeanMCP Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,262 +1,289 @@
1
- # @leanmcp/utils
2
-
3
- Utility functions and helpers for LeanMCP SDK.
4
-
5
- ## Features
6
-
7
- - 🔄 **Retry logic** - Exponential backoff for resilient operations
8
- - 📊 **Response formatting** - Format data as JSON, Markdown, HTML, or tables
9
- - 🔀 **Object utilities** - Deep merge, validation, and manipulation
10
- - ⏱️ **Async helpers** - Sleep, timeout, and promise utilities
11
-
12
- ## Installation
13
-
14
- ```bash
15
- npm install @leanmcp/utils
16
- ```
17
-
18
- ## API Reference
19
-
20
- ### Response Formatting
21
-
22
- #### formatResponse(data, format)
23
-
24
- Format data based on specified format type.
25
-
26
- ```typescript
27
- import { formatResponse } from "@leanmcp/utils";
28
-
29
- // JSON formatting
30
- const json = formatResponse({ hello: "world" }, "json");
31
- // Output: '{\n "hello": "world"\n}'
32
-
33
- // Markdown formatting
34
- const md = formatResponse({ hello: "world" }, "markdown");
35
- // Output: '```json\n{\n "hello": "world"\n}\n```'
36
-
37
- // HTML formatting
38
- const html = formatResponse({ hello: "world" }, "html");
39
- // Output: '<pre>{\n "hello": "world"\n}</pre>'
40
-
41
- // Table formatting (for arrays)
42
- const table = formatResponse([
43
- { name: "Alice", age: 30 },
44
- { name: "Bob", age: 25 }
45
- ], "table");
46
- // Output: Markdown table format
47
- ```
48
-
49
- **Supported formats:**
50
- - `json` - Pretty-printed JSON
51
- - `markdown` - JSON wrapped in markdown code block
52
- - `html` - JSON wrapped in HTML pre tag
53
- - `table` - Markdown table (for arrays of objects)
54
- - Default - String conversion
55
-
56
- #### formatAsTable(data)
57
-
58
- Format array of objects as a Markdown table.
59
-
60
- ```typescript
61
- import { formatAsTable } from "@leanmcp/utils";
62
-
63
- const data = [
64
- { name: "Alice", age: 30, city: "NYC" },
65
- { name: "Bob", age: 25, city: "LA" }
66
- ];
67
-
68
- const table = formatAsTable(data);
69
- console.log(table);
70
- // | name | age | city |
71
- // | --- | --- | --- |
72
- // | Alice | 30 | NYC |
73
- // | Bob | 25 | LA |
74
- ```
75
-
76
- ### Object Utilities
77
-
78
- #### deepMerge(target, ...sources)
79
-
80
- Deep merge multiple objects.
81
-
82
- ```typescript
83
- import { deepMerge } from "@leanmcp/utils";
84
-
85
- const target = { a: 1, b: { c: 2 } };
86
- const source1 = { b: { d: 3 } };
87
- const source2 = { e: 4 };
88
-
89
- const result = deepMerge(target, source1, source2);
90
- // { a: 1, b: { c: 2, d: 3 }, e: 4 }
91
- ```
92
-
93
- #### isObject(item)
94
-
95
- Check if value is a plain object.
96
-
97
- ```typescript
98
- import { isObject } from "@leanmcp/utils";
99
-
100
- isObject({}); // true
101
- isObject([]); // false
102
- isObject(null); // false
103
- isObject("string"); // false
104
- ```
105
-
106
- ### Async Utilities
107
-
108
- #### retry(fn, options)
109
-
110
- Retry a function with exponential backoff.
111
-
112
- ```typescript
113
- import { retry } from "@leanmcp/utils";
114
-
115
- // Retry API call up to 3 times
116
- const result = await retry(
117
- async () => {
118
- const response = await fetch('https://api.example.com/data');
119
- if (!response.ok) throw new Error('API error');
120
- return response.json();
121
- },
122
- {
123
- maxRetries: 3, // Maximum number of retries
124
- delayMs: 1000, // Initial delay in milliseconds
125
- backoff: 2 // Backoff multiplier (2^n)
126
- }
127
- );
128
- ```
129
-
130
- **Retry logic:**
131
- - Attempt 1: Immediate
132
- - Attempt 2: Wait 1000ms
133
- - Attempt 3: Wait 2000ms
134
- - Attempt 4: Wait 4000ms
135
-
136
- #### sleep(ms)
137
-
138
- Async sleep function.
139
-
140
- ```typescript
141
- import { sleep } from "@leanmcp/utils";
142
-
143
- await sleep(1000); // Wait 1 second
144
- console.log("1 second later");
145
- ```
146
-
147
- #### timeout(promise, ms)
148
-
149
- Add timeout to a promise.
150
-
151
- ```typescript
152
- import { timeout } from "@leanmcp/utils";
153
-
154
- try {
155
- const result = await timeout(
156
- fetch('https://slow-api.example.com'),
157
- 5000 // 5 second timeout
158
- );
159
- } catch (error) {
160
- console.log('Request timed out');
161
- }
162
- ```
163
-
164
- ## Usage Examples
165
-
166
- ### Formatting API Responses
167
-
168
- ```typescript
169
- import { formatResponse } from "@leanmcp/utils";
170
-
171
- class DataService {
172
- @Tool({ description: 'Get user data' })
173
- async getUsers() {
174
- const users = await fetchUsers();
175
-
176
- // Return as formatted table
177
- return {
178
- content: [{
179
- type: "text",
180
- text: formatResponse(users, "table")
181
- }]
182
- };
183
- }
184
- }
185
- ```
186
-
187
- ### Resilient API Calls
188
-
189
- ```typescript
190
- import { retry } from "@leanmcp/utils";
191
-
192
- class ExternalService {
193
- @Tool({ description: 'Fetch external data' })
194
- async fetchData(input: { url: string }) {
195
- // Automatically retry failed requests
196
- const data = await retry(
197
- () => fetch(input.url).then(r => r.json()),
198
- { maxRetries: 3, delayMs: 1000 }
199
- );
200
-
201
- return { data };
202
- }
203
- }
204
- ```
205
-
206
- ### Deep Configuration Merging
207
-
208
- ```typescript
209
- import { deepMerge } from "@leanmcp/utils";
210
-
211
- const defaultConfig = {
212
- server: { port: 3000, host: 'localhost' },
213
- logging: { level: 'info' }
214
- };
215
-
216
- const userConfig = {
217
- server: { port: 4000 },
218
- features: { auth: true }
219
- };
220
-
221
- const config = deepMerge(defaultConfig, userConfig);
222
- // {
223
- // server: { port: 4000, host: 'localhost' },
224
- // logging: { level: 'info' },
225
- // features: { auth: true }
226
- // }
227
- ```
228
-
229
- ## Type Definitions
230
-
231
- All functions are fully typed with TypeScript:
232
-
233
- ```typescript
234
- export function formatResponse(data: any, format: string): string;
235
- export function formatAsTable(data: any[]): string;
236
- export function deepMerge<T extends Record<string, any>>(target: T, ...sources: Partial<T>[]): T;
237
- export function isObject(item: any): boolean;
238
- export function retry<T>(fn: () => Promise<T>, options?: RetryOptions): Promise<T>;
239
- export function sleep(ms: number): Promise<void>;
240
- export function timeout<T>(promise: Promise<T>, ms: number): Promise<T>;
241
-
242
- interface RetryOptions {
243
- maxRetries?: number;
244
- delayMs?: number;
245
- backoff?: number;
246
- }
247
- ```
248
-
249
- ## License
250
-
251
- MIT
252
-
253
- ## Related Packages
254
-
255
- - [@leanmcp/core](../core) - Core MCP server functionality
256
- - [@leanmcp/auth](../auth) - Authentication decorators
257
- - [@leanmcp/cli](../cli) - CLI tool for creating new projects
258
-
259
- ## Links
260
-
261
- - [GitHub Repository](https://github.com/LeanMCP/leanmcp-sdk)
262
- - [Documentation](https://github.com/LeanMCP/leanmcp-sdk#readme)
1
+ <p align="center">
2
+ <img
3
+ src="https://raw.githubusercontent.com/LeanMCP/leanmcp-sdk/refs/heads/main/assets/logo.png"
4
+ alt="LeanMCP Logo"
5
+ width="400"
6
+ />
7
+ </p>
8
+
9
+ <p align="center">
10
+ <strong>@leanmcp/utils</strong><br/>
11
+ Utility functions and helpers for LeanMCP SDK.
12
+ </p>
13
+
14
+ <p align="center">
15
+ <a href="https://www.npmjs.com/package/@leanmcp/utils">
16
+ <img src="https://img.shields.io/npm/v/@leanmcp/utils" alt="npm version" />
17
+ </a>
18
+ <a href="https://www.npmjs.com/package/@leanmcp/utils">
19
+ <img src="https://img.shields.io/npm/dm/@leanmcp/utils" alt="npm downloads" />
20
+ </a>
21
+ <a href="https://docs.leanmcp.com/sdk/utils">
22
+ <img src="https://img.shields.io/badge/Docs-leanmcp-0A66C2?" />
23
+ </a>
24
+ <a href="https://discord.com/invite/DsRcA3GwPy">
25
+ <img src="https://img.shields.io/badge/Discord-Join-5865F2?logo=discord&logoColor=white" />
26
+ </a>
27
+ <a href="https://x.com/LeanMcp">
28
+ <img src="https://img.shields.io/badge/@LeanMCP-f5f5f5?logo=x&logoColor=000000" />
29
+ </a>
30
+ </p>
31
+
32
+ ## Features
33
+
34
+ - **Retry logic** Exponential backoff for resilient operations
35
+ - **Response formatting** — Format data as JSON, Markdown, HTML, or tables
36
+ - **Object utilities** — Deep merge, validation, and manipulation
37
+ - **Async helpers** — Sleep, timeout, and promise utilities
38
+
39
+ ## Installation
40
+
41
+ ```bash
42
+ npm install @leanmcp/utils
43
+ ```
44
+
45
+ ## API Reference
46
+
47
+ ### Response Formatting
48
+
49
+ #### formatResponse(data, format)
50
+
51
+ Format data based on specified format type.
52
+
53
+ ```typescript
54
+ import { formatResponse } from "@leanmcp/utils";
55
+
56
+ // JSON formatting
57
+ const json = formatResponse({ hello: "world" }, "json");
58
+ // Output: '{\n "hello": "world"\n}'
59
+
60
+ // Markdown formatting
61
+ const md = formatResponse({ hello: "world" }, "markdown");
62
+ // Output: '```json\n{\n "hello": "world"\n}\n```'
63
+
64
+ // HTML formatting
65
+ const html = formatResponse({ hello: "world" }, "html");
66
+ // Output: '<pre>{\n "hello": "world"\n}</pre>'
67
+
68
+ // Table formatting (for arrays)
69
+ const table = formatResponse([
70
+ { name: "Alice", age: 30 },
71
+ { name: "Bob", age: 25 }
72
+ ], "table");
73
+ // Output: Markdown table format
74
+ ```
75
+
76
+ **Supported formats:**
77
+ - `json` - Pretty-printed JSON
78
+ - `markdown` - JSON wrapped in markdown code block
79
+ - `html` - JSON wrapped in HTML pre tag
80
+ - `table` - Markdown table (for arrays of objects)
81
+ - Default - String conversion
82
+
83
+ #### formatAsTable(data)
84
+
85
+ Format array of objects as a Markdown table.
86
+
87
+ ```typescript
88
+ import { formatAsTable } from "@leanmcp/utils";
89
+
90
+ const data = [
91
+ { name: "Alice", age: 30, city: "NYC" },
92
+ { name: "Bob", age: 25, city: "LA" }
93
+ ];
94
+
95
+ const table = formatAsTable(data);
96
+ console.log(table);
97
+ // | name | age | city |
98
+ // | --- | --- | --- |
99
+ // | Alice | 30 | NYC |
100
+ // | Bob | 25 | LA |
101
+ ```
102
+
103
+ ### Object Utilities
104
+
105
+ #### deepMerge(target, ...sources)
106
+
107
+ Deep merge multiple objects.
108
+
109
+ ```typescript
110
+ import { deepMerge } from "@leanmcp/utils";
111
+
112
+ const target = { a: 1, b: { c: 2 } };
113
+ const source1 = { b: { d: 3 } };
114
+ const source2 = { e: 4 };
115
+
116
+ const result = deepMerge(target, source1, source2);
117
+ // { a: 1, b: { c: 2, d: 3 }, e: 4 }
118
+ ```
119
+
120
+ #### isObject(item)
121
+
122
+ Check if value is a plain object.
123
+
124
+ ```typescript
125
+ import { isObject } from "@leanmcp/utils";
126
+
127
+ isObject({}); // true
128
+ isObject([]); // false
129
+ isObject(null); // false
130
+ isObject("string"); // false
131
+ ```
132
+
133
+ ### Async Utilities
134
+
135
+ #### retry(fn, options)
136
+
137
+ Retry a function with exponential backoff.
138
+
139
+ ```typescript
140
+ import { retry } from "@leanmcp/utils";
141
+
142
+ // Retry API call up to 3 times
143
+ const result = await retry(
144
+ async () => {
145
+ const response = await fetch('https://api.example.com/data');
146
+ if (!response.ok) throw new Error('API error');
147
+ return response.json();
148
+ },
149
+ {
150
+ maxRetries: 3, // Maximum number of retries
151
+ delayMs: 1000, // Initial delay in milliseconds
152
+ backoff: 2 // Backoff multiplier (2^n)
153
+ }
154
+ );
155
+ ```
156
+
157
+ **Retry logic:**
158
+ - Attempt 1: Immediate
159
+ - Attempt 2: Wait 1000ms
160
+ - Attempt 3: Wait 2000ms
161
+ - Attempt 4: Wait 4000ms
162
+
163
+ #### sleep(ms)
164
+
165
+ Async sleep function.
166
+
167
+ ```typescript
168
+ import { sleep } from "@leanmcp/utils";
169
+
170
+ await sleep(1000); // Wait 1 second
171
+ console.log("1 second later");
172
+ ```
173
+
174
+ #### timeout(promise, ms)
175
+
176
+ Add timeout to a promise.
177
+
178
+ ```typescript
179
+ import { timeout } from "@leanmcp/utils";
180
+
181
+ try {
182
+ const result = await timeout(
183
+ fetch('https://slow-api.example.com'),
184
+ 5000 // 5 second timeout
185
+ );
186
+ } catch (error) {
187
+ console.log('Request timed out');
188
+ }
189
+ ```
190
+
191
+ ## Usage Examples
192
+
193
+ ### Formatting API Responses
194
+
195
+ ```typescript
196
+ import { formatResponse } from "@leanmcp/utils";
197
+
198
+ class DataService {
199
+ @Tool({ description: 'Get user data' })
200
+ async getUsers() {
201
+ const users = await fetchUsers();
202
+
203
+ // Return as formatted table
204
+ return {
205
+ content: [{
206
+ type: "text",
207
+ text: formatResponse(users, "table")
208
+ }]
209
+ };
210
+ }
211
+ }
212
+ ```
213
+
214
+ ### Resilient API Calls
215
+
216
+ ```typescript
217
+ import { retry } from "@leanmcp/utils";
218
+
219
+ class ExternalService {
220
+ @Tool({ description: 'Fetch external data' })
221
+ async fetchData(input: { url: string }) {
222
+ // Automatically retry failed requests
223
+ const data = await retry(
224
+ () => fetch(input.url).then(r => r.json()),
225
+ { maxRetries: 3, delayMs: 1000 }
226
+ );
227
+
228
+ return { data };
229
+ }
230
+ }
231
+ ```
232
+
233
+ ### Deep Configuration Merging
234
+
235
+ ```typescript
236
+ import { deepMerge } from "@leanmcp/utils";
237
+
238
+ const defaultConfig = {
239
+ server: { port: 3000, host: 'localhost' },
240
+ logging: { level: 'info' }
241
+ };
242
+
243
+ const userConfig = {
244
+ server: { port: 4000 },
245
+ features: { auth: true }
246
+ };
247
+
248
+ const config = deepMerge(defaultConfig, userConfig);
249
+ // {
250
+ // server: { port: 4000, host: 'localhost' },
251
+ // logging: { level: 'info' },
252
+ // features: { auth: true }
253
+ // }
254
+ ```
255
+
256
+ ## Type Definitions
257
+
258
+ All functions are fully typed with TypeScript:
259
+
260
+ ```typescript
261
+ export function formatResponse(data: any, format: string): string;
262
+ export function formatAsTable(data: any[]): string;
263
+ export function deepMerge<T extends Record<string, any>>(target: T, ...sources: Partial<T>[]): T;
264
+ export function isObject(item: any): boolean;
265
+ export function retry<T>(fn: () => Promise<T>, options?: RetryOptions): Promise<T>;
266
+ export function sleep(ms: number): Promise<void>;
267
+ export function timeout<T>(promise: Promise<T>, ms: number): Promise<T>;
268
+
269
+ interface RetryOptions {
270
+ maxRetries?: number;
271
+ delayMs?: number;
272
+ backoff?: number;
273
+ }
274
+ ```
275
+
276
+ ## License
277
+
278
+ MIT
279
+
280
+ ## Related Packages
281
+
282
+ - [@leanmcp/core](../core) - Core MCP server functionality
283
+ - [@leanmcp/auth](../auth) - Authentication decorators
284
+ - [@leanmcp/cli](../cli) - CLI tool for creating new projects
285
+
286
+ ## Links
287
+
288
+ - [GitHub Repository](https://github.com/LeanMCP/leanmcp-sdk)
289
+ - [Documentation](https://github.com/LeanMCP/leanmcp-sdk#readme)
package/package.json CHANGED
@@ -1,54 +1,54 @@
1
- {
2
- "name": "@leanmcp/utils",
3
- "version": "0.1.1",
4
- "description": "Helper utilities and decorators shared across modules",
5
- "main": "dist/index.js",
6
- "module": "dist/index.mjs",
7
- "types": "dist/index.d.ts",
8
- "exports": {
9
- ".": {
10
- "types": "./dist/index.d.ts",
11
- "require": "./dist/index.js",
12
- "import": "./dist/index.mjs"
13
- }
14
- },
15
- "files": [
16
- "dist",
17
- "README.md",
18
- "LICENSE"
19
- ],
20
- "scripts": {
21
- "build": "tsup src/index.ts --format esm,cjs --dts",
22
- "dev": "tsup src/index.ts --format esm,cjs --dts --watch",
23
- "test": "jest --passWithNoTests",
24
- "test:watch": "jest --watch"
25
- },
26
- "dependencies": {
27
- "@leanmcp/core": "^0.3.0"
28
- },
29
- "devDependencies": {
30
- "@types/node": "^20.0.0"
31
- },
32
- "repository": {
33
- "type": "git",
34
- "url": "git+https://github.com/LeanMCP/leanmcp-sdk.git",
35
- "directory": "packages/utils"
36
- },
37
- "homepage": "https://github.com/LeanMCP/leanmcp-sdk#readme",
38
- "bugs": {
39
- "url": "https://github.com/LeanMCP/leanmcp-sdk/issues"
40
- },
41
- "keywords": [
42
- "mcp",
43
- "model-context-protocol",
44
- "typescript",
45
- "utilities",
46
- "helpers",
47
- "validation"
48
- ],
49
- "author": "LeanMCP <admin@leanmcp.com>",
50
- "license": "MIT",
51
- "publishConfig": {
52
- "access": "public"
53
- }
54
- }
1
+ {
2
+ "name": "@leanmcp/utils",
3
+ "version": "0.1.3",
4
+ "description": "Helper utilities and decorators shared across modules",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "require": "./dist/index.js",
12
+ "import": "./dist/index.mjs"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "README.md",
18
+ "LICENSE"
19
+ ],
20
+ "scripts": {
21
+ "build": "tsup src/index.ts --format esm,cjs --dts",
22
+ "dev": "tsup src/index.ts --format esm,cjs --dts --watch",
23
+ "test": "jest --passWithNoTests",
24
+ "test:watch": "jest --watch"
25
+ },
26
+ "dependencies": {
27
+ "@leanmcp/core": "^0.3.0"
28
+ },
29
+ "devDependencies": {
30
+ "@types/node": "^20.0.0"
31
+ },
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "git+https://github.com/LeanMCP/leanmcp-sdk.git",
35
+ "directory": "packages/utils"
36
+ },
37
+ "homepage": "https://github.com/LeanMCP/leanmcp-sdk#readme",
38
+ "bugs": {
39
+ "url": "https://github.com/LeanMCP/leanmcp-sdk/issues"
40
+ },
41
+ "keywords": [
42
+ "mcp",
43
+ "model-context-protocol",
44
+ "typescript",
45
+ "utilities",
46
+ "helpers",
47
+ "validation"
48
+ ],
49
+ "author": "LeanMCP <admin@leanmcp.com>",
50
+ "license": "MIT",
51
+ "publishConfig": {
52
+ "access": "public"
53
+ }
54
+ }