@agledger/mcp-server 1.5.0 → 2.0.0

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 (66) 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/src/enums.ts +0 -47
  21. package/src/errors.ts +0 -44
  22. package/src/resources/contract-types.ts +0 -130
  23. package/src/tool-scopes.ts +0 -180
  24. package/src/tools/a2a.ts +0 -430
  25. package/src/tools/agent-mandates.ts +0 -259
  26. package/src/tools/agents-refs.ts +0 -232
  27. package/src/tools/capabilities.ts +0 -63
  28. package/src/tools/compliance.ts +0 -70
  29. package/src/tools/dashboard.ts +0 -64
  30. package/src/tools/disputes.ts +0 -121
  31. package/src/tools/enterprises.ts +0 -203
  32. package/src/tools/events.ts +0 -33
  33. package/src/tools/federation-admin.ts +0 -665
  34. package/src/tools/federation.ts +0 -587
  35. package/src/tools/health.ts +0 -30
  36. package/src/tools/mandates.ts +0 -264
  37. package/src/tools/openclaw.ts +0 -122
  38. package/src/tools/receipts.ts +0 -134
  39. package/src/tools/reputation.ts +0 -69
  40. package/src/tools/schemas.ts +0 -535
  41. package/src/tools/verification-keys.ts +0 -33
  42. package/src/tools/verification.ts +0 -59
  43. package/tests/conformance.test.ts +0 -197
  44. package/tests/conformance.ts +0 -65
  45. package/tests/mock-client.ts +0 -71
  46. package/tests/profiles.test.ts +0 -120
  47. package/tests/resources/contract-types.test.ts +0 -82
  48. package/tests/tools/a2a.test.ts +0 -213
  49. package/tests/tools/agent-mandates.test.ts +0 -196
  50. package/tests/tools/agents-refs.test.ts +0 -190
  51. package/tests/tools/capabilities.test.ts +0 -80
  52. package/tests/tools/compliance.test.ts +0 -123
  53. package/tests/tools/dashboard.test.ts +0 -111
  54. package/tests/tools/disputes.test.ts +0 -123
  55. package/tests/tools/enterprises.test.ts +0 -171
  56. package/tests/tools/events.test.ts +0 -56
  57. package/tests/tools/federation-admin.test.ts +0 -468
  58. package/tests/tools/federation.test.ts +0 -522
  59. package/tests/tools/health.test.ts +0 -56
  60. package/tests/tools/mandates.test.ts +0 -143
  61. package/tests/tools/openclaw.test.ts +0 -148
  62. package/tests/tools/receipts.test.ts +0 -157
  63. package/tests/tools/reputation.test.ts +0 -108
  64. package/tests/tools/schemas.test.ts +0 -374
  65. package/tests/tools/verification-keys.test.ts +0 -56
  66. 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
+ });
package/src/enums.ts DELETED
@@ -1,47 +0,0 @@
1
- import { z } from 'zod';
2
-
3
- export const ContractTypeEnum = z.enum([
4
- 'ACH-PROC-v1',
5
- 'ACH-DLVR-v1',
6
- 'ACH-DATA-v1',
7
- 'ACH-TXN-v1',
8
- 'ACH-ORCH-v1',
9
- 'ACH-COMM-v1',
10
- 'ACH-AUTH-v1',
11
- 'ACH-INFRA-v1',
12
- 'ACH-DEL-v1',
13
- 'ACH-ANALYZE-v1',
14
- 'ACH-COORD-v1',
15
- 'ACH-MON-v1',
16
- 'ACH-REVIEW-v1',
17
- ]).describe('AGLedger standard contract type identifier');
18
-
19
- export const MandateStatusEnum = z.enum([
20
- 'CREATED', 'PROPOSED', 'ACTIVE', 'PROCESSING',
21
- 'REVISION_REQUESTED', 'FULFILLED', 'FAILED',
22
- 'REMEDIATED', 'EXPIRED', 'CANCELLED', 'REJECTED',
23
- ]).describe('Mandate lifecycle status');
24
-
25
- export const HubStateEnum = z.enum(['OFFERED', 'ACCEPTED', 'ACTIVE', 'COMPLETED', 'DISPUTED', 'TERMINAL']).describe('Federation hub-level mandate state');
26
- export const GatewayStatusEnum = z.enum(['active', 'suspended', 'revoked']).describe('Federation gateway registration status');
27
- export const RevocationReasonEnum = z.enum(['key_compromise', 'decommission', 'administrative']).describe('Reason for revoking a federation gateway');
28
- export const FederationSignalEnum = z.enum(['SETTLE', 'HOLD', 'RELEASE']).describe('Settlement signal type');
29
- export const FederationOutcomeEnum = z.enum(['PASS', 'FAIL']).describe('Federation verification outcome');
30
-
31
- export const OperatingModeEnum = z.enum(['cleartext', 'encrypted']).describe('Operating mode for the mandate');
32
- export const RiskClassificationEnum = z.enum(['high', 'limited', 'minimal', 'unclassified']).describe('EU AI Act risk classification');
33
- export const DisputeStatusEnum = z.enum([
34
- 'OPENED', 'TIER_1_REVIEW', 'EVIDENCE_WINDOW', 'TIER_2_REVIEW',
35
- 'ESCALATED', 'TIER_3_ARBITRATION', 'RESOLVED', 'WITHDRAWN',
36
- ]).describe('Dispute lifecycle status');
37
- export const StructuralValidationEnum = z.enum(['ACCEPTED', 'INVALID']).describe('Receipt structural validation result');
38
- export const VerificationOutcomeEnum = z.enum(['PASS', 'FAIL', 'REVIEW_REQUIRED']).describe('Verification outcome (system verdict)');
39
-
40
- export const NextStepSchema = z.object({
41
- action: z.string().describe('What to do next'),
42
- method: z.enum(['GET', 'POST', 'PUT', 'PATCH', 'DELETE']).describe('HTTP method'),
43
- href: z.string().describe('Relative URL template'),
44
- description: z.string().describe('Why this step matters'),
45
- }).describe('Suggested next API call');
46
-
47
- export const NextStepsField = z.array(NextStepSchema).nullable().optional().describe('Suggested next API calls — guides agents through the lifecycle');