@leanmcp/utils 0.1.2 → 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.
- package/LICENSE +21 -21
- package/README.md +289 -289
- 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,289 +1,289 @@
|
|
|
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)
|
|
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.
|
|
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
|
+
}
|