You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Add logging
* Add mock 'fetch' factory for the API
* Tweak example log messages
* Extend mock Response shape to cover error paths
Add missing fields to mock Response objects:
- Success case: status, statusText
- Error case: statusText, text()
This ensures the mock matches McpdClient expectations for non-2xx responses and prevents runtime errors when tests hit unmapped routes.
* Clean up logging tests
Remove unused consoleErrorSpy and use vi.stubGlobal() consistently:
- Remove consoleErrorSpy from beforeEach/afterEach (never asserted)
- Replace direct global.fetch assignments with vi.stubGlobal('fetch', ...)
- Aligns with outer suite's mocking pattern for easier test cleanup
* Clean-up
* Remove serverCacheTtl from docs/comments as it was never implemented
* Extract constants and defaults from magic numbers in constructor
* Checked/updated all TSDocs in client.ts
* Make a private agent tools function actually private
* Type safety for log levels
* Updated examples (deps, README)
Copy file name to clipboardExpand all lines: README.md
+139-6Lines changed: 139 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -73,7 +73,6 @@ const client = new McpdClient({
73
73
apiEndpoint: "http://localhost:8090",
74
74
apiKey: "optional-key", // Optional API key
75
75
healthCacheTtl: 10, // Cache health checks for 10 seconds
76
-
serverCacheTtl: 60, // Cache server/tool metadata for 60 seconds
77
76
});
78
77
79
78
// Full type safety and autocomplete
@@ -107,11 +106,114 @@ const client = new McpdClient({
107
106
apiEndpoint: "http://localhost:8090", // Required
108
107
apiKey: "optional-key", // Optional: API key for authentication
109
108
healthCacheTtl: 10, // Optional: TTL in seconds for health cache (default: 10)
110
-
serverCacheTtl: 60, // Optional: TTL in seconds for server/tools cache (default: 60)
111
109
timeout: 30000, // Optional: Request timeout in ms (default: 30000)
112
110
});
113
111
```
114
112
113
+
### Logging
114
+
115
+
The SDK includes optional logging for warnings about unhealthy or non-existent servers that are skipped during operations.
116
+
117
+
**Important:** Logging is disabled by default. Only enable logging in non-MCP-server contexts. MCP servers using stdio transport for JSON-RPC communication should never enable logging, as it will contaminate stdout/stderr and break the protocol.
118
+
119
+
#### Using Environment Variable
120
+
121
+
Set the `MCPD_LOG_LEVEL` environment variable to control logging:
122
+
123
+
```bash
124
+
# Valid levels: trace, debug, info, warn, error, off (default)
// Logging is automatically enabled based on MCPD_LOG_LEVEL
141
+
const client =newMcpdClient({
142
+
apiEndpoint: "http://localhost:8090",
143
+
});
144
+
```
145
+
146
+
#### Using Custom Logger
147
+
148
+
For advanced use cases, inject your own logger implementation.
149
+
150
+
**Partial Logger Support:** You can provide only the methods you want to customize. Any omitted methods will fall back to the default logger, which respects `MCPD_LOG_LEVEL`.
151
+
152
+
```typescript
153
+
import { McpdClient } from"@mozilla-ai/mcpd";
154
+
155
+
// Full custom logger
156
+
const client =newMcpdClient({
157
+
apiEndpoint: "http://localhost:8090",
158
+
logger: {
159
+
trace: (...args) =>myLogger.trace(args),
160
+
debug: (...args) =>myLogger.debug(args),
161
+
info: (...args) =>myLogger.info(args),
162
+
warn: (...args) =>myLogger.warn(args),
163
+
error: (...args) =>myLogger.error(args),
164
+
},
165
+
});
166
+
167
+
// Partial logger: custom warn/error, default (MCPD_LOG_LEVEL-aware) for others
168
+
const client2 =newMcpdClient({
169
+
apiEndpoint: "http://localhost:8090",
170
+
logger: {
171
+
warn: (msg) =>console.warn(`[mcpd] ${msg}`),
172
+
error: (msg) =>console.error(`[mcpd] ${msg}`),
173
+
// trace, debug, info use default logger (respects MCPD_LOG_LEVEL)
174
+
},
175
+
});
176
+
```
177
+
178
+
#### Disabling Logging
179
+
180
+
To disable logging, simply ensure `MCPD_LOG_LEVEL` is unset or set to `off` (the default):
181
+
182
+
```typescript
183
+
// Logging is disabled by default (no configuration needed)
184
+
const client =newMcpdClient({
185
+
apiEndpoint: "http://localhost:8090",
186
+
});
187
+
```
188
+
189
+
If you need to disable logging even when `MCPD_LOG_LEVEL` is set (rare case), provide a custom logger with no-op implementations:
190
+
191
+
```typescript
192
+
// Override MCPD_LOG_LEVEL to force disable
193
+
const client =newMcpdClient({
194
+
apiEndpoint: "http://localhost:8090",
195
+
logger: {
196
+
trace: () => {},
197
+
debug: () => {},
198
+
info: () => {},
199
+
warn: () => {},
200
+
error: () => {},
201
+
},
202
+
});
203
+
```
204
+
205
+
When logging is enabled, warnings are emitted for:
206
+
207
+
- Unhealthy servers that are skipped (e.g., status `timeout`, `unreachable`)
208
+
- Non-existent servers specified in filter options
209
+
210
+
Example warning messages:
211
+
212
+
```text
213
+
Skipping unhealthy server 'time' with status 'timeout'
214
+
Skipping non-existent server 'unknown'
215
+
```
216
+
115
217
### Core Methods
116
218
117
219
#### `client.listServers()`
@@ -357,19 +459,28 @@ if (await client.isServerHealthy("time")) {
357
459
358
460
#### `client.getAgentTools(options?)`
359
461
360
-
Generate callable functions that work directly with AI agent frameworks. No conversion layers needed.
462
+
Generate (cached) callable functions that work directly with AI agent frameworks. No conversion layers needed.
463
+
464
+
> [!IMPORTANT]
465
+
> If you want to get agent tools mutiple times using different filter options, you need to call `client.clearAgentToolsCache()` to force regeneration.
361
466
362
-
**Why filter tools?**
467
+
##### Supports filtering by servers and by tools
363
468
364
-
AI agents perform better with focused tool sets.
469
+
AI agents perform better with focused tool sets they need to complete the given task.
365
470
Tool filtering enables progressive disclosure - operators can expose a subset of server tools via `mcpd` configuration,
366
471
then agents can further narrow down to only the tools needed for their specific task.
367
472
This prevents overwhelming the model's context window and improves response quality.
0 commit comments