@agledger/mcp-server 1.5.0 → 2.0.1

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 (162) hide show
  1. package/dist/api-client.d.ts +16 -0
  2. package/dist/api-client.d.ts.map +1 -0
  3. package/dist/api-client.js +52 -0
  4. package/dist/api-client.js.map +1 -0
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +17 -58
  7. package/dist/index.js.map +1 -1
  8. package/dist/server.d.ts +2 -13
  9. package/dist/server.d.ts.map +1 -1
  10. package/dist/server.js +113 -109
  11. package/dist/server.js.map +1 -1
  12. package/package.json +2 -3
  13. package/src/api-client.ts +72 -0
  14. package/src/index.ts +27 -60
  15. package/src/server.ts +136 -120
  16. package/tests/api-client.test.ts +147 -0
  17. package/tests/harness.ts +8 -35
  18. package/tests/integration.test.ts +373 -0
  19. package/tests/server.test.ts +286 -0
  20. package/dist/enums.d.ts +0 -110
  21. package/dist/enums.d.ts.map +0 -1
  22. package/dist/enums.js +0 -42
  23. package/dist/enums.js.map +0 -1
  24. package/dist/errors.d.ts +0 -10
  25. package/dist/errors.d.ts.map +0 -1
  26. package/dist/errors.js +0 -49
  27. package/dist/errors.js.map +0 -1
  28. package/dist/resources/contract-types.d.ts +0 -4
  29. package/dist/resources/contract-types.d.ts.map +0 -1
  30. package/dist/resources/contract-types.js +0 -104
  31. package/dist/resources/contract-types.js.map +0 -1
  32. package/dist/tool-scopes.d.ts +0 -11
  33. package/dist/tool-scopes.d.ts.map +0 -1
  34. package/dist/tool-scopes.js +0 -164
  35. package/dist/tool-scopes.js.map +0 -1
  36. package/dist/tools/a2a.d.ts +0 -8
  37. package/dist/tools/a2a.d.ts.map +0 -1
  38. package/dist/tools/a2a.js +0 -360
  39. package/dist/tools/a2a.js.map +0 -1
  40. package/dist/tools/agent-mandates.d.ts +0 -4
  41. package/dist/tools/agent-mandates.d.ts.map +0 -1
  42. package/dist/tools/agent-mandates.js +0 -222
  43. package/dist/tools/agent-mandates.js.map +0 -1
  44. package/dist/tools/agents-refs.d.ts +0 -4
  45. package/dist/tools/agents-refs.d.ts.map +0 -1
  46. package/dist/tools/agents-refs.js +0 -196
  47. package/dist/tools/agents-refs.js.map +0 -1
  48. package/dist/tools/capabilities.d.ts +0 -4
  49. package/dist/tools/capabilities.d.ts.map +0 -1
  50. package/dist/tools/capabilities.js +0 -54
  51. package/dist/tools/capabilities.js.map +0 -1
  52. package/dist/tools/code-mode.d.ts +0 -4
  53. package/dist/tools/code-mode.d.ts.map +0 -1
  54. package/dist/tools/code-mode.js +0 -389
  55. package/dist/tools/code-mode.js.map +0 -1
  56. package/dist/tools/compliance.d.ts +0 -4
  57. package/dist/tools/compliance.d.ts.map +0 -1
  58. package/dist/tools/compliance.js +0 -63
  59. package/dist/tools/compliance.js.map +0 -1
  60. package/dist/tools/dashboard.d.ts +0 -4
  61. package/dist/tools/dashboard.d.ts.map +0 -1
  62. package/dist/tools/dashboard.js +0 -57
  63. package/dist/tools/dashboard.js.map +0 -1
  64. package/dist/tools/disputes.d.ts +0 -4
  65. package/dist/tools/disputes.d.ts.map +0 -1
  66. package/dist/tools/disputes.js +0 -103
  67. package/dist/tools/disputes.js.map +0 -1
  68. package/dist/tools/enterprises.d.ts +0 -4
  69. package/dist/tools/enterprises.d.ts.map +0 -1
  70. package/dist/tools/enterprises.js +0 -173
  71. package/dist/tools/enterprises.js.map +0 -1
  72. package/dist/tools/events.d.ts +0 -4
  73. package/dist/tools/events.d.ts.map +0 -1
  74. package/dist/tools/events.js +0 -28
  75. package/dist/tools/events.js.map +0 -1
  76. package/dist/tools/federation-admin.d.ts +0 -4
  77. package/dist/tools/federation-admin.d.ts.map +0 -1
  78. package/dist/tools/federation-admin.js +0 -567
  79. package/dist/tools/federation-admin.js.map +0 -1
  80. package/dist/tools/federation.d.ts +0 -4
  81. package/dist/tools/federation.d.ts.map +0 -1
  82. package/dist/tools/federation.js +0 -505
  83. package/dist/tools/federation.js.map +0 -1
  84. package/dist/tools/health.d.ts +0 -4
  85. package/dist/tools/health.d.ts.map +0 -1
  86. package/dist/tools/health.js +0 -25
  87. package/dist/tools/health.js.map +0 -1
  88. package/dist/tools/mandates.d.ts +0 -4
  89. package/dist/tools/mandates.d.ts.map +0 -1
  90. package/dist/tools/mandates.js +0 -227
  91. package/dist/tools/mandates.js.map +0 -1
  92. package/dist/tools/openclaw.d.ts +0 -4
  93. package/dist/tools/openclaw.d.ts.map +0 -1
  94. package/dist/tools/openclaw.js +0 -110
  95. package/dist/tools/openclaw.js.map +0 -1
  96. package/dist/tools/receipts.d.ts +0 -8
  97. package/dist/tools/receipts.d.ts.map +0 -1
  98. package/dist/tools/receipts.js +0 -111
  99. package/dist/tools/receipts.js.map +0 -1
  100. package/dist/tools/reputation.d.ts +0 -4
  101. package/dist/tools/reputation.d.ts.map +0 -1
  102. package/dist/tools/reputation.js +0 -62
  103. package/dist/tools/reputation.js.map +0 -1
  104. package/dist/tools/schemas.d.ts +0 -4
  105. package/dist/tools/schemas.d.ts.map +0 -1
  106. package/dist/tools/schemas.js +0 -453
  107. package/dist/tools/schemas.js.map +0 -1
  108. package/dist/tools/verification-keys.d.ts +0 -4
  109. package/dist/tools/verification-keys.d.ts.map +0 -1
  110. package/dist/tools/verification-keys.js +0 -27
  111. package/dist/tools/verification-keys.js.map +0 -1
  112. package/dist/tools/verification.d.ts +0 -4
  113. package/dist/tools/verification.d.ts.map +0 -1
  114. package/dist/tools/verification.js +0 -50
  115. package/dist/tools/verification.js.map +0 -1
  116. package/src/enums.ts +0 -47
  117. package/src/errors.ts +0 -44
  118. package/src/resources/contract-types.ts +0 -130
  119. package/src/tool-scopes.ts +0 -180
  120. package/src/tools/a2a.ts +0 -430
  121. package/src/tools/agent-mandates.ts +0 -259
  122. package/src/tools/agents-refs.ts +0 -232
  123. package/src/tools/capabilities.ts +0 -63
  124. package/src/tools/compliance.ts +0 -70
  125. package/src/tools/dashboard.ts +0 -64
  126. package/src/tools/disputes.ts +0 -121
  127. package/src/tools/enterprises.ts +0 -203
  128. package/src/tools/events.ts +0 -33
  129. package/src/tools/federation-admin.ts +0 -665
  130. package/src/tools/federation.ts +0 -587
  131. package/src/tools/health.ts +0 -30
  132. package/src/tools/mandates.ts +0 -264
  133. package/src/tools/openclaw.ts +0 -122
  134. package/src/tools/receipts.ts +0 -134
  135. package/src/tools/reputation.ts +0 -69
  136. package/src/tools/schemas.ts +0 -535
  137. package/src/tools/verification-keys.ts +0 -33
  138. package/src/tools/verification.ts +0 -59
  139. package/tests/conformance.test.ts +0 -197
  140. package/tests/conformance.ts +0 -65
  141. package/tests/mock-client.ts +0 -71
  142. package/tests/profiles.test.ts +0 -120
  143. package/tests/resources/contract-types.test.ts +0 -82
  144. package/tests/tools/a2a.test.ts +0 -213
  145. package/tests/tools/agent-mandates.test.ts +0 -196
  146. package/tests/tools/agents-refs.test.ts +0 -190
  147. package/tests/tools/capabilities.test.ts +0 -80
  148. package/tests/tools/compliance.test.ts +0 -123
  149. package/tests/tools/dashboard.test.ts +0 -111
  150. package/tests/tools/disputes.test.ts +0 -123
  151. package/tests/tools/enterprises.test.ts +0 -171
  152. package/tests/tools/events.test.ts +0 -56
  153. package/tests/tools/federation-admin.test.ts +0 -468
  154. package/tests/tools/federation.test.ts +0 -522
  155. package/tests/tools/health.test.ts +0 -56
  156. package/tests/tools/mandates.test.ts +0 -143
  157. package/tests/tools/openclaw.test.ts +0 -148
  158. package/tests/tools/receipts.test.ts +0 -157
  159. package/tests/tools/reputation.test.ts +0 -108
  160. package/tests/tools/schemas.test.ts +0 -374
  161. package/tests/tools/verification-keys.test.ts +0 -56
  162. package/tests/tools/verification.test.ts +0 -64
@@ -0,0 +1,373 @@
1
+ import { describe, it, expect, beforeAll, afterAll } from 'vitest';
2
+ import { Client } from '@modelcontextprotocol/sdk/client/index.js';
3
+ import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js';
4
+ import { AgledgerMcpServer } from '../src/server.js';
5
+
6
+ const API_URL = process.env.AGLEDGER_API_URL ?? 'http://localhost:3001';
7
+ const API_KEY =
8
+ process.env.AGLEDGER_API_KEY ?? 'ach_age_LduM4xPbgrQZjg_B-SK147ah2gskEN-8d-5xwG8ismM';
9
+
10
+ let client: Client;
11
+ let cleanup: () => Promise<void>;
12
+ let apiReachable = false;
13
+
14
+ beforeAll(async () => {
15
+ try {
16
+ const res = await fetch(`${API_URL}/health`, { signal: AbortSignal.timeout(3000) });
17
+ apiReachable = res.ok;
18
+ } catch {
19
+ apiReachable = false;
20
+ }
21
+
22
+ if (!apiReachable) return;
23
+
24
+ const server = new AgledgerMcpServer({ apiKey: API_KEY, apiUrl: API_URL });
25
+ const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
26
+ client = new Client({ name: 'integration-test', version: '0.0.0-test' });
27
+
28
+ await Promise.all([server.mcp.connect(serverTransport), client.connect(clientTransport)]);
29
+
30
+ cleanup = async () => {
31
+ await client.close();
32
+ await server.mcp.close();
33
+ };
34
+ });
35
+
36
+ afterAll(async () => {
37
+ if (cleanup) await cleanup();
38
+ });
39
+
40
+ function skipIfNoApi() {
41
+ if (!apiReachable) {
42
+ console.log('Skipping: API not reachable at', API_URL);
43
+ return true;
44
+ }
45
+ return false;
46
+ }
47
+
48
+ describe('agledger_discover (live)', () => {
49
+ it('returns health and identity', async () => {
50
+ if (skipIfNoApi()) return;
51
+
52
+ const result = await client.callTool({ name: 'agledger_discover', arguments: {} });
53
+
54
+ expect(result.isError).toBeFalsy();
55
+ const content = result.structuredContent as Record<string, unknown>;
56
+
57
+ expect(content.health).toBeDefined();
58
+ const health = content.health as Record<string, unknown>;
59
+ expect(health.status).toBe('ok');
60
+ expect(health.version).toBeDefined();
61
+
62
+ expect(content.identity).toBeDefined();
63
+ const identity = content.identity as Record<string, unknown>;
64
+ expect(identity.role).toBeDefined();
65
+ expect(identity.ownerId).toBeDefined();
66
+ });
67
+ });
68
+
69
+ describe('enterprise key (live)', () => {
70
+ let entClient: Client;
71
+ let entCleanup: () => Promise<void>;
72
+
73
+ beforeAll(async () => {
74
+ if (!apiReachable) return;
75
+ const entKey = 'ach_ent_sVPW_3Em_nZbX-il0akyT7MKHO5OZI2eKEcaIn6XCMY';
76
+ const server = new AgledgerMcpServer({ apiKey: entKey, apiUrl: API_URL });
77
+ const [ct, st] = InMemoryTransport.createLinkedPair();
78
+ entClient = new Client({ name: 'ent-test', version: '0.0.0-test' });
79
+ await Promise.all([server.mcp.connect(st), entClient.connect(ct)]);
80
+ entCleanup = async () => {
81
+ await entClient.close();
82
+ await server.mcp.close();
83
+ };
84
+ });
85
+
86
+ afterAll(async () => {
87
+ if (entCleanup) await entCleanup();
88
+ });
89
+
90
+ it('discover works with enterprise key', async () => {
91
+ if (skipIfNoApi()) return;
92
+ const result = await entClient.callTool({ name: 'agledger_discover', arguments: {} });
93
+ expect(result.isError).toBeFalsy();
94
+ const content = result.structuredContent as Record<string, unknown>;
95
+ const identity = content.identity as Record<string, unknown>;
96
+ expect(identity.role).toBe('enterprise');
97
+ });
98
+
99
+ it('can list mandates with enterprise key', async () => {
100
+ if (skipIfNoApi()) return;
101
+ const result = await entClient.callTool({
102
+ name: 'agledger_api',
103
+ arguments: { method: 'GET', path: '/v1/mandates', params: { limit: 1 } },
104
+ });
105
+ expect(result.isError).toBeFalsy();
106
+ const content = result.structuredContent as Record<string, unknown>;
107
+ expect(content.data).toBeDefined();
108
+ });
109
+ });
110
+
111
+ describe('agledger_api (live)', () => {
112
+ it('GET /health', async () => {
113
+ if (skipIfNoApi()) return;
114
+
115
+ const result = await client.callTool({
116
+ name: 'agledger_api',
117
+ arguments: { method: 'GET', path: '/health' },
118
+ });
119
+
120
+ expect(result.isError).toBeFalsy();
121
+ const content = result.structuredContent as Record<string, unknown>;
122
+ expect(content.status).toBe('ok');
123
+ });
124
+
125
+ it('GET /v1/auth/me returns identity', async () => {
126
+ if (skipIfNoApi()) return;
127
+
128
+ const result = await client.callTool({
129
+ name: 'agledger_api',
130
+ arguments: { method: 'GET', path: '/v1/auth/me' },
131
+ });
132
+
133
+ expect(result.isError).toBeFalsy();
134
+ const content = result.structuredContent as Record<string, unknown>;
135
+ expect(content.role).toBeDefined();
136
+ expect(content.ownerId).toBeDefined();
137
+ });
138
+
139
+ it('GET /v1/schemas lists contract types', async () => {
140
+ if (skipIfNoApi()) return;
141
+
142
+ const result = await client.callTool({
143
+ name: 'agledger_api',
144
+ arguments: { method: 'GET', path: '/v1/schemas' },
145
+ });
146
+
147
+ expect(result.isError).toBeFalsy();
148
+ const content = result.structuredContent as Record<string, unknown>;
149
+ expect(content.data).toBeDefined();
150
+ expect(Array.isArray(content.data)).toBe(true);
151
+ });
152
+
153
+ it('GET /v1/mandates lists mandates', async () => {
154
+ if (skipIfNoApi()) return;
155
+
156
+ const result = await client.callTool({
157
+ name: 'agledger_api',
158
+ arguments: { method: 'GET', path: '/v1/mandates', params: { limit: 5 } },
159
+ });
160
+
161
+ expect(result.isError).toBeFalsy();
162
+ const content = result.structuredContent as Record<string, unknown>;
163
+ expect(content.data).toBeDefined();
164
+ });
165
+
166
+ it('POST /v1/mandates/agent creates a mandate', async () => {
167
+ if (skipIfNoApi()) return;
168
+
169
+ const result = await client.callTool({
170
+ name: 'agledger_api',
171
+ arguments: {
172
+ method: 'POST',
173
+ path: '/v1/mandates/agent',
174
+ params: {
175
+ contractType: 'ACH-PROC-v1',
176
+ platform: 'mcp-integration-test',
177
+ criteria: {
178
+ item_spec: 'integration-test-resource',
179
+ },
180
+ },
181
+ },
182
+ });
183
+
184
+ expect(result.isError).toBeFalsy();
185
+ const content = result.structuredContent as Record<string, unknown>;
186
+ expect(content.id).toBeDefined();
187
+ expect(content.contractType).toBe('ACH-PROC-v1');
188
+ });
189
+
190
+ it('returns structured error for not-found mandate', async () => {
191
+ if (skipIfNoApi()) return;
192
+
193
+ const result = await client.callTool({
194
+ name: 'agledger_api',
195
+ arguments: { method: 'GET', path: '/v1/mandates/nonexistent-id' },
196
+ });
197
+
198
+ expect(result.isError).toBe(true);
199
+ const content = result.structuredContent as Record<string, unknown>;
200
+ expect(content.error ?? content.message).toBeDefined();
201
+ });
202
+
203
+ it('returns structured error with schema help for bad criteria', async () => {
204
+ if (skipIfNoApi()) return;
205
+
206
+ const result = await client.callTool({
207
+ name: 'agledger_api',
208
+ arguments: {
209
+ method: 'POST',
210
+ path: '/v1/mandates/agent',
211
+ params: {
212
+ contractType: 'ACH-PROC-v1',
213
+ platform: 'test',
214
+ criteria: { bad_field: 'wrong' },
215
+ },
216
+ },
217
+ });
218
+
219
+ expect(result.isError).toBe(true);
220
+ const content = result.structuredContent as Record<string, unknown>;
221
+ expect(content.requiredFields).toBeDefined();
222
+ expect(content.examplePayload).toBeDefined();
223
+ });
224
+ });
225
+
226
+ describe('full mandate lifecycle (live)', () => {
227
+ let mandateId: string;
228
+
229
+ it('step 1: create mandate with autoActivate', async () => {
230
+ if (skipIfNoApi()) return;
231
+
232
+ const result = await client.callTool({
233
+ name: 'agledger_api',
234
+ arguments: {
235
+ method: 'POST',
236
+ path: '/v1/mandates/agent',
237
+ params: {
238
+ contractType: 'ACH-PROC-v1',
239
+ platform: 'mcp-lifecycle-test',
240
+ autoActivate: true,
241
+ criteria: {
242
+ item_spec: 'lifecycle-test-widget',
243
+ },
244
+ },
245
+ },
246
+ });
247
+
248
+ expect(result.isError).toBeFalsy();
249
+ const content = result.structuredContent as Record<string, unknown>;
250
+ mandateId = content.id as string;
251
+ expect(mandateId).toBeDefined();
252
+ expect(content.status).toBe('ACTIVE');
253
+ });
254
+
255
+ it('step 2: get mandate to confirm state', async () => {
256
+ if (skipIfNoApi() || !mandateId) return;
257
+
258
+ const result = await client.callTool({
259
+ name: 'agledger_api',
260
+ arguments: { method: 'GET', path: `/v1/mandates/${mandateId}` },
261
+ });
262
+
263
+ expect(result.isError).toBeFalsy();
264
+ const content = result.structuredContent as Record<string, unknown>;
265
+ expect(content.id).toBe(mandateId);
266
+ expect(content.status).toBe('ACTIVE');
267
+ });
268
+
269
+ it('step 3: submit receipt', async () => {
270
+ if (skipIfNoApi() || !mandateId) return;
271
+
272
+ const result = await client.callTool({
273
+ name: 'agledger_api',
274
+ arguments: {
275
+ method: 'POST',
276
+ path: `/v1/mandates/${mandateId}/receipts`,
277
+ params: {
278
+ evidence: {
279
+ item_secured: 'Lifecycle test widget delivered',
280
+ quantity: 1,
281
+ total_cost: { amount: 10, currency: 'USD' },
282
+ supplier: { id: 'SUP-TEST', name: 'Test Supplier' },
283
+ confirmation_ref: 'MCP-LIFECYCLE-001',
284
+ },
285
+ },
286
+ },
287
+ });
288
+
289
+ expect(result.isError).toBeFalsy();
290
+ const content = result.structuredContent as Record<string, unknown>;
291
+ expect(content.id).toBeDefined();
292
+ expect(content.mandateId).toBe(mandateId);
293
+ });
294
+
295
+ it('step 4: check mandate reached terminal state', async () => {
296
+ if (skipIfNoApi() || !mandateId) return;
297
+
298
+ const result = await client.callTool({
299
+ name: 'agledger_api',
300
+ arguments: { method: 'GET', path: `/v1/mandates/${mandateId}` },
301
+ });
302
+
303
+ expect(result.isError).toBeFalsy();
304
+ const content = result.structuredContent as Record<string, unknown>;
305
+ const status = content.status as string;
306
+ expect(['FULFILLED', 'VERIFIED_FAIL', 'PROCESSING']).toContain(status);
307
+ });
308
+
309
+ it('step 5: list receipts for mandate', async () => {
310
+ if (skipIfNoApi() || !mandateId) return;
311
+
312
+ const result = await client.callTool({
313
+ name: 'agledger_api',
314
+ arguments: { method: 'GET', path: `/v1/mandates/${mandateId}/receipts` },
315
+ });
316
+
317
+ expect(result.isError).toBeFalsy();
318
+ const content = result.structuredContent as Record<string, unknown>;
319
+ const data = content.data as Array<Record<string, unknown>>;
320
+ expect(data.length).toBeGreaterThanOrEqual(1);
321
+ expect(data[0].mandateId).toBe(mandateId);
322
+ });
323
+
324
+ it('step 6: check events for mandate', async () => {
325
+ if (skipIfNoApi() || !mandateId) return;
326
+
327
+ const result = await client.callTool({
328
+ name: 'agledger_api',
329
+ arguments: {
330
+ method: 'GET',
331
+ path: '/v1/events',
332
+ params: { mandateId, limit: 10, since: '2020-01-01T00:00:00Z' },
333
+ },
334
+ });
335
+
336
+ expect(result.isError).toBeFalsy();
337
+ const content = result.structuredContent as Record<string, unknown>;
338
+ const data = content.data as Array<Record<string, unknown>>;
339
+ expect(data.length).toBeGreaterThanOrEqual(1);
340
+ });
341
+ });
342
+
343
+ describe('HTTP method coverage (live)', () => {
344
+ it('DELETE returns structured error for nonexistent webhook', async () => {
345
+ if (skipIfNoApi()) return;
346
+
347
+ const result = await client.callTool({
348
+ name: 'agledger_api',
349
+ arguments: { method: 'DELETE', path: '/v1/webhooks/nonexistent-id' },
350
+ });
351
+
352
+ expect(result.isError).toBe(true);
353
+ const content = result.structuredContent as Record<string, unknown>;
354
+ expect(content.error ?? content.message).toBeDefined();
355
+ });
356
+
357
+ it('PATCH returns structured error for invalid mandate update', async () => {
358
+ if (skipIfNoApi()) return;
359
+
360
+ const result = await client.callTool({
361
+ name: 'agledger_api',
362
+ arguments: {
363
+ method: 'PATCH',
364
+ path: '/v1/mandates/nonexistent-id',
365
+ params: { platform: 'updated' },
366
+ },
367
+ });
368
+
369
+ expect(result.isError).toBe(true);
370
+ const content = result.structuredContent as Record<string, unknown>;
371
+ expect(content.error ?? content.message).toBeDefined();
372
+ });
373
+ });
@@ -0,0 +1,286 @@
1
+ import { describe, it, expect, beforeAll, afterAll, afterEach, vi } from 'vitest';
2
+ import type { Tool } from '@modelcontextprotocol/sdk/types.js';
3
+ import { createTestHarness, type TestHarness } from './harness.js';
4
+
5
+ let harness: TestHarness;
6
+ let tools: Tool[];
7
+
8
+ beforeAll(async () => {
9
+ harness = await createTestHarness();
10
+ ({ tools } = await harness.client.listTools());
11
+ });
12
+
13
+ afterAll(async () => {
14
+ await harness.cleanup();
15
+ });
16
+
17
+ afterEach(() => {
18
+ vi.unstubAllGlobals();
19
+ });
20
+
21
+ describe('tool registration', () => {
22
+ it('registers exactly 2 tools', () => {
23
+ expect(tools).toHaveLength(2);
24
+ });
25
+
26
+ it('registers agledger_discover', () => {
27
+ const tool = tools.find((t) => t.name === 'agledger_discover');
28
+ expect(tool).toBeDefined();
29
+ expect(tool!.description).toContain('health');
30
+ expect(tool!.description).toContain('identity');
31
+ });
32
+
33
+ it('registers agledger_api', () => {
34
+ const tool = tools.find((t) => t.name === 'agledger_api');
35
+ expect(tool).toBeDefined();
36
+ expect(tool!.description).toContain('nextSteps');
37
+ });
38
+ });
39
+
40
+ describe('agledger_discover', () => {
41
+ it('returns health and identity on success', async () => {
42
+ const mockFetch = vi
43
+ .fn()
44
+ .mockResolvedValueOnce(
45
+ new Response(JSON.stringify({ status: 'ok', version: '1.0.0' }), {
46
+ status: 200,
47
+ headers: { 'content-type': 'application/json' },
48
+ }),
49
+ )
50
+ .mockResolvedValueOnce(
51
+ new Response(JSON.stringify({ agentId: 'agent-1', scopes: ['mandates:read'] }), {
52
+ status: 200,
53
+ headers: { 'content-type': 'application/json' },
54
+ }),
55
+ );
56
+ vi.stubGlobal('fetch', mockFetch);
57
+
58
+ const result = await harness.client.callTool({ name: 'agledger_discover', arguments: {} });
59
+
60
+ expect(result.isError).toBeFalsy();
61
+ const content = result.structuredContent as Record<string, unknown>;
62
+ expect(content.health).toEqual({ status: 'ok', version: '1.0.0' });
63
+ expect(content.identity).toEqual({ agentId: 'agent-1', scopes: ['mandates:read'] });
64
+ });
65
+
66
+ it('returns partial results when one call fails', async () => {
67
+ const mockFetch = vi
68
+ .fn()
69
+ .mockResolvedValueOnce(
70
+ new Response(JSON.stringify({ status: 'ok' }), {
71
+ status: 200,
72
+ headers: { 'content-type': 'application/json' },
73
+ }),
74
+ )
75
+ .mockRejectedValueOnce(new TypeError('fetch failed'));
76
+ vi.stubGlobal('fetch', mockFetch);
77
+
78
+ const result = await harness.client.callTool({ name: 'agledger_discover', arguments: {} });
79
+
80
+ expect(result.isError).toBeFalsy();
81
+ const content = result.structuredContent as Record<string, unknown>;
82
+ expect(content.health).toEqual({ status: 'ok' });
83
+ expect(content.identity).toEqual({ error: 'fetch failed' });
84
+ });
85
+ });
86
+
87
+ describe('agledger_api', () => {
88
+ it('passes through successful GET response', async () => {
89
+ const apiResponse = {
90
+ data: [{ id: 'm-1', status: 'ACTIVE' }],
91
+ nextSteps: [{ action: 'Get mandate', method: 'GET', href: '/mandates/m-1' }],
92
+ };
93
+ vi.stubGlobal(
94
+ 'fetch',
95
+ vi.fn().mockResolvedValueOnce(
96
+ new Response(JSON.stringify(apiResponse), {
97
+ status: 200,
98
+ headers: { 'content-type': 'application/json' },
99
+ }),
100
+ ),
101
+ );
102
+
103
+ const result = await harness.client.callTool({
104
+ name: 'agledger_api',
105
+ arguments: { method: 'GET', path: '/mandates', params: { limit: 10 } },
106
+ });
107
+
108
+ expect(result.isError).toBeFalsy();
109
+ expect(result.structuredContent).toEqual(apiResponse);
110
+ });
111
+
112
+ it('passes through POST body', async () => {
113
+ const apiResponse = { id: 'm-2', status: 'CREATED' };
114
+ const mockFetch = vi.fn().mockResolvedValueOnce(
115
+ new Response(JSON.stringify(apiResponse), {
116
+ status: 201,
117
+ headers: { 'content-type': 'application/json' },
118
+ }),
119
+ );
120
+ vi.stubGlobal('fetch', mockFetch);
121
+
122
+ await harness.client.callTool({
123
+ name: 'agledger_api',
124
+ arguments: {
125
+ method: 'POST',
126
+ path: '/mandates',
127
+ params: { contractType: 'ACH-PROC-v1', platform: 'test' },
128
+ },
129
+ });
130
+
131
+ const [url, options] = mockFetch.mock.calls[0] as [string, RequestInit];
132
+ expect(url).toContain('/mandates');
133
+ expect(options.method).toBe('POST');
134
+ expect(JSON.parse(options.body as string)).toEqual({
135
+ contractType: 'ACH-PROC-v1',
136
+ platform: 'test',
137
+ });
138
+ });
139
+
140
+ it('forwards full API error body', async () => {
141
+ const errorBody = {
142
+ message: 'Mandate not found',
143
+ code: 'NOT_FOUND',
144
+ docUrl: 'https://docs.agledger.ai/errors/NOT_FOUND',
145
+ suggestion: 'Check the mandate ID and try again.',
146
+ };
147
+ vi.stubGlobal(
148
+ 'fetch',
149
+ vi.fn().mockResolvedValueOnce(
150
+ new Response(JSON.stringify(errorBody), {
151
+ status: 404,
152
+ headers: { 'content-type': 'application/json' },
153
+ }),
154
+ ),
155
+ );
156
+
157
+ const result = await harness.client.callTool({
158
+ name: 'agledger_api',
159
+ arguments: { method: 'GET', path: '/mandates/bad-id' },
160
+ });
161
+
162
+ expect(result.isError).toBe(true);
163
+ const content = result.structuredContent as Record<string, unknown>;
164
+ expect(content.docUrl).toBe('https://docs.agledger.ai/errors/NOT_FOUND');
165
+ expect(content.suggestion).toBe('Check the mandate ID and try again.');
166
+ });
167
+
168
+ it('forwards 403 with missingScopes', async () => {
169
+ const errorBody = {
170
+ message: 'Insufficient permissions',
171
+ code: 'FORBIDDEN',
172
+ missingScopes: ['mandates:write'],
173
+ suggestion: 'Request mandates:write scope on your API key.',
174
+ };
175
+ vi.stubGlobal(
176
+ 'fetch',
177
+ vi.fn().mockResolvedValueOnce(
178
+ new Response(JSON.stringify(errorBody), {
179
+ status: 403,
180
+ headers: { 'content-type': 'application/json' },
181
+ }),
182
+ ),
183
+ );
184
+
185
+ const result = await harness.client.callTool({
186
+ name: 'agledger_api',
187
+ arguments: { method: 'POST', path: '/mandates', params: {} },
188
+ });
189
+
190
+ expect(result.isError).toBe(true);
191
+ const content = result.structuredContent as Record<string, unknown>;
192
+ expect(content.missingScopes).toEqual(['mandates:write']);
193
+ });
194
+
195
+ it('rejects path not starting with /', async () => {
196
+ const result = await harness.client.callTool({
197
+ name: 'agledger_api',
198
+ arguments: { method: 'GET', path: 'mandates' },
199
+ });
200
+
201
+ expect(result.isError).toBe(true);
202
+ const text = (result.content as Array<{ type: string; text: string }>)[0].text;
203
+ expect(text).toContain('Path must start with /');
204
+ });
205
+
206
+ it('handles network errors without crashing', async () => {
207
+ vi.stubGlobal(
208
+ 'fetch',
209
+ vi.fn().mockRejectedValueOnce(new TypeError('fetch failed: DNS resolution failed')),
210
+ );
211
+
212
+ const result = await harness.client.callTool({
213
+ name: 'agledger_api',
214
+ arguments: { method: 'GET', path: '/health' },
215
+ });
216
+
217
+ expect(result.isError).toBe(true);
218
+ const text = (result.content as Array<{ type: string; text: string }>)[0].text;
219
+ expect(text).toContain('Network error');
220
+ });
221
+
222
+ it('handles timeout without crashing', async () => {
223
+ vi.stubGlobal(
224
+ 'fetch',
225
+ vi
226
+ .fn()
227
+ .mockRejectedValueOnce(
228
+ Object.assign(new DOMException('The operation was aborted', 'AbortError'), {}),
229
+ ),
230
+ );
231
+
232
+ const result = await harness.client.callTool({
233
+ name: 'agledger_api',
234
+ arguments: { method: 'GET', path: '/health' },
235
+ });
236
+
237
+ expect(result.isError).toBe(true);
238
+ const text = (result.content as Array<{ type: string; text: string }>)[0].text;
239
+ expect(text).toContain('timed out');
240
+ });
241
+
242
+ it('handles non-JSON responses', async () => {
243
+ vi.stubGlobal(
244
+ 'fetch',
245
+ vi.fn().mockResolvedValueOnce(
246
+ new Response('<html>Bad Gateway</html>', {
247
+ status: 502,
248
+ headers: { 'content-type': 'text/html' },
249
+ }),
250
+ ),
251
+ );
252
+
253
+ const result = await harness.client.callTool({
254
+ name: 'agledger_api',
255
+ arguments: { method: 'GET', path: '/health' },
256
+ });
257
+
258
+ expect(result.isError).toBe(true);
259
+ const content = result.structuredContent as Record<string, unknown>;
260
+ expect(content._raw).toBe('<html>Bad Gateway</html>');
261
+ expect(content._contentType).toBe('text/html');
262
+ });
263
+
264
+ it('sends query params for GET requests', async () => {
265
+ const mockFetch = vi.fn().mockResolvedValueOnce(
266
+ new Response(JSON.stringify({ data: [] }), {
267
+ status: 200,
268
+ headers: { 'content-type': 'application/json' },
269
+ }),
270
+ );
271
+ vi.stubGlobal('fetch', mockFetch);
272
+
273
+ await harness.client.callTool({
274
+ name: 'agledger_api',
275
+ arguments: {
276
+ method: 'GET',
277
+ path: '/mandates/search',
278
+ params: { status: 'ACTIVE', limit: 20 },
279
+ },
280
+ });
281
+
282
+ const url = new URL(mockFetch.mock.calls[0][0] as string);
283
+ expect(url.searchParams.get('status')).toBe('ACTIVE');
284
+ expect(url.searchParams.get('limit')).toBe('20');
285
+ });
286
+ });