@leanmcp/utils 0.1.5-alpha.6.6dae082 → 0.1.6
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/README.md +43 -51
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
src="https://raw.githubusercontent.com/LeanMCP/leanmcp-sdk/refs/heads/main/assets/logo.png"
|
|
4
4
|
alt="LeanMCP Logo"
|
|
5
5
|
width="400"
|
|
6
|
-
|
|
7
6
|
/>
|
|
8
7
|
</p>
|
|
9
8
|
|
|
@@ -55,34 +54,30 @@ npm install @leanmcp/utils
|
|
|
55
54
|
|
|
56
55
|
Format data based on specified format type.
|
|
57
56
|
|
|
58
|
-
|
|
59
|
-
import { formatResponse } from
|
|
57
|
+
```typescript
|
|
58
|
+
import { formatResponse } from "@leanmcp/utils";
|
|
60
59
|
|
|
61
60
|
// JSON formatting
|
|
62
|
-
const json = formatResponse({ hello:
|
|
61
|
+
const json = formatResponse({ hello: "world" }, "json");
|
|
63
62
|
// Output: '{\n "hello": "world"\n}'
|
|
64
63
|
|
|
65
64
|
// Markdown formatting
|
|
66
|
-
const md = formatResponse({ hello:
|
|
65
|
+
const md = formatResponse({ hello: "world" }, "markdown");
|
|
67
66
|
// Output: '```json\n{\n "hello": "world"\n}\n```'
|
|
68
67
|
|
|
69
68
|
// HTML formatting
|
|
70
|
-
const html = formatResponse({ hello:
|
|
69
|
+
const html = formatResponse({ hello: "world" }, "html");
|
|
71
70
|
// Output: '<pre>{\n "hello": "world"\n}</pre>'
|
|
72
71
|
|
|
73
72
|
// Table formatting (for arrays)
|
|
74
|
-
const table = formatResponse(
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
],
|
|
79
|
-
'table'
|
|
80
|
-
);
|
|
73
|
+
const table = formatResponse([
|
|
74
|
+
{ name: "Alice", age: 30 },
|
|
75
|
+
{ name: "Bob", age: 25 }
|
|
76
|
+
], "table");
|
|
81
77
|
// Output: Markdown table format
|
|
82
|
-
|
|
78
|
+
```
|
|
83
79
|
|
|
84
80
|
**Supported formats:**
|
|
85
|
-
|
|
86
81
|
- `json` - Pretty-printed JSON
|
|
87
82
|
- `markdown` - JSON wrapped in markdown code block
|
|
88
83
|
- `html` - JSON wrapped in HTML pre tag
|
|
@@ -94,11 +89,11 @@ const table = formatResponse(
|
|
|
94
89
|
Format array of objects as a Markdown table.
|
|
95
90
|
|
|
96
91
|
```typescript
|
|
97
|
-
import { formatAsTable } from
|
|
92
|
+
import { formatAsTable } from "@leanmcp/utils";
|
|
98
93
|
|
|
99
94
|
const data = [
|
|
100
|
-
{ name:
|
|
101
|
-
{ name:
|
|
95
|
+
{ name: "Alice", age: 30, city: "NYC" },
|
|
96
|
+
{ name: "Bob", age: 25, city: "LA" }
|
|
102
97
|
];
|
|
103
98
|
|
|
104
99
|
const table = formatAsTable(data);
|
|
@@ -116,7 +111,7 @@ console.log(table);
|
|
|
116
111
|
Deep merge multiple objects.
|
|
117
112
|
|
|
118
113
|
```typescript
|
|
119
|
-
import { deepMerge } from
|
|
114
|
+
import { deepMerge } from "@leanmcp/utils";
|
|
120
115
|
|
|
121
116
|
const target = { a: 1, b: { c: 2 } };
|
|
122
117
|
const source1 = { b: { d: 3 } };
|
|
@@ -131,12 +126,12 @@ const result = deepMerge(target, source1, source2);
|
|
|
131
126
|
Check if value is a plain object.
|
|
132
127
|
|
|
133
128
|
```typescript
|
|
134
|
-
import { isObject } from
|
|
129
|
+
import { isObject } from "@leanmcp/utils";
|
|
135
130
|
|
|
136
|
-
isObject({});
|
|
137
|
-
isObject([]);
|
|
138
|
-
isObject(null);
|
|
139
|
-
isObject(
|
|
131
|
+
isObject({}); // true
|
|
132
|
+
isObject([]); // false
|
|
133
|
+
isObject(null); // false
|
|
134
|
+
isObject("string"); // false
|
|
140
135
|
```
|
|
141
136
|
|
|
142
137
|
### Async Utilities
|
|
@@ -146,7 +141,7 @@ isObject('string'); // false
|
|
|
146
141
|
Retry a function with exponential backoff.
|
|
147
142
|
|
|
148
143
|
```typescript
|
|
149
|
-
import { retry } from
|
|
144
|
+
import { retry } from "@leanmcp/utils";
|
|
150
145
|
|
|
151
146
|
// Retry API call up to 3 times
|
|
152
147
|
const result = await retry(
|
|
@@ -156,15 +151,14 @@ const result = await retry(
|
|
|
156
151
|
return response.json();
|
|
157
152
|
},
|
|
158
153
|
{
|
|
159
|
-
maxRetries: 3,
|
|
160
|
-
delayMs: 1000,
|
|
161
|
-
backoff: 2
|
|
154
|
+
maxRetries: 3, // Maximum number of retries
|
|
155
|
+
delayMs: 1000, // Initial delay in milliseconds
|
|
156
|
+
backoff: 2 // Backoff multiplier (2^n)
|
|
162
157
|
}
|
|
163
158
|
);
|
|
164
159
|
```
|
|
165
160
|
|
|
166
161
|
**Retry logic:**
|
|
167
|
-
|
|
168
162
|
- Attempt 1: Immediate
|
|
169
163
|
- Attempt 2: Wait 1000ms
|
|
170
164
|
- Attempt 3: Wait 2000ms
|
|
@@ -175,10 +169,10 @@ const result = await retry(
|
|
|
175
169
|
Async sleep function.
|
|
176
170
|
|
|
177
171
|
```typescript
|
|
178
|
-
import { sleep } from
|
|
172
|
+
import { sleep } from "@leanmcp/utils";
|
|
179
173
|
|
|
180
|
-
await sleep(1000);
|
|
181
|
-
console.log(
|
|
174
|
+
await sleep(1000); // Wait 1 second
|
|
175
|
+
console.log("1 second later");
|
|
182
176
|
```
|
|
183
177
|
|
|
184
178
|
#### timeout(promise, ms)
|
|
@@ -186,12 +180,12 @@ console.log('1 second later');
|
|
|
186
180
|
Add timeout to a promise.
|
|
187
181
|
|
|
188
182
|
```typescript
|
|
189
|
-
import { timeout } from
|
|
183
|
+
import { timeout } from "@leanmcp/utils";
|
|
190
184
|
|
|
191
185
|
try {
|
|
192
186
|
const result = await timeout(
|
|
193
187
|
fetch('https://slow-api.example.com'),
|
|
194
|
-
5000
|
|
188
|
+
5000 // 5 second timeout
|
|
195
189
|
);
|
|
196
190
|
} catch (error) {
|
|
197
191
|
console.log('Request timed out');
|
|
@@ -203,21 +197,19 @@ try {
|
|
|
203
197
|
### Formatting API Responses
|
|
204
198
|
|
|
205
199
|
```typescript
|
|
206
|
-
import { formatResponse } from
|
|
200
|
+
import { formatResponse } from "@leanmcp/utils";
|
|
207
201
|
|
|
208
202
|
class DataService {
|
|
209
203
|
@Tool({ description: 'Get user data' })
|
|
210
204
|
async getUsers() {
|
|
211
205
|
const users = await fetchUsers();
|
|
212
|
-
|
|
206
|
+
|
|
213
207
|
// Return as formatted table
|
|
214
208
|
return {
|
|
215
|
-
content: [
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
},
|
|
220
|
-
],
|
|
209
|
+
content: [{
|
|
210
|
+
type: "text",
|
|
211
|
+
text: formatResponse(users, "table")
|
|
212
|
+
}]
|
|
221
213
|
};
|
|
222
214
|
}
|
|
223
215
|
}
|
|
@@ -226,17 +218,17 @@ class DataService {
|
|
|
226
218
|
### Resilient API Calls
|
|
227
219
|
|
|
228
220
|
```typescript
|
|
229
|
-
import { retry } from
|
|
221
|
+
import { retry } from "@leanmcp/utils";
|
|
230
222
|
|
|
231
223
|
class ExternalService {
|
|
232
224
|
@Tool({ description: 'Fetch external data' })
|
|
233
225
|
async fetchData(input: { url: string }) {
|
|
234
226
|
// Automatically retry failed requests
|
|
235
|
-
const data = await retry(
|
|
236
|
-
|
|
237
|
-
delayMs: 1000
|
|
238
|
-
|
|
239
|
-
|
|
227
|
+
const data = await retry(
|
|
228
|
+
() => fetch(input.url).then(r => r.json()),
|
|
229
|
+
{ maxRetries: 3, delayMs: 1000 }
|
|
230
|
+
);
|
|
231
|
+
|
|
240
232
|
return { data };
|
|
241
233
|
}
|
|
242
234
|
}
|
|
@@ -245,16 +237,16 @@ class ExternalService {
|
|
|
245
237
|
### Deep Configuration Merging
|
|
246
238
|
|
|
247
239
|
```typescript
|
|
248
|
-
import { deepMerge } from
|
|
240
|
+
import { deepMerge } from "@leanmcp/utils";
|
|
249
241
|
|
|
250
242
|
const defaultConfig = {
|
|
251
243
|
server: { port: 3000, host: 'localhost' },
|
|
252
|
-
logging: { level: 'info' }
|
|
244
|
+
logging: { level: 'info' }
|
|
253
245
|
};
|
|
254
246
|
|
|
255
247
|
const userConfig = {
|
|
256
248
|
server: { port: 4000 },
|
|
257
|
-
features: { auth: true }
|
|
249
|
+
features: { auth: true }
|
|
258
250
|
};
|
|
259
251
|
|
|
260
252
|
const config = deepMerge(defaultConfig, userConfig);
|