@agledger/mcp-server 1.2.0 → 1.3.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 (137) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +1 -1
  3. package/dist/enums.d.ts +0 -1
  4. package/dist/enums.d.ts.map +1 -1
  5. package/dist/enums.js +0 -1
  6. package/dist/enums.js.map +1 -1
  7. package/dist/errors.d.ts +0 -1
  8. package/dist/errors.d.ts.map +1 -1
  9. package/dist/errors.js +0 -1
  10. package/dist/errors.js.map +1 -1
  11. package/dist/index.d.ts +0 -1
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +1 -2
  14. package/dist/index.js.map +1 -1
  15. package/dist/resources/contract-types.d.ts +0 -1
  16. package/dist/resources/contract-types.d.ts.map +1 -1
  17. package/dist/resources/contract-types.js +0 -3
  18. package/dist/resources/contract-types.js.map +1 -1
  19. package/dist/server.d.ts +0 -1
  20. package/dist/server.d.ts.map +1 -1
  21. package/dist/server.js +6 -3
  22. package/dist/server.js.map +1 -1
  23. package/dist/tool-scopes.d.ts +0 -12
  24. package/dist/tool-scopes.d.ts.map +1 -1
  25. package/dist/tool-scopes.js +20 -30
  26. package/dist/tool-scopes.js.map +1 -1
  27. package/dist/tools/a2a.d.ts +0 -1
  28. package/dist/tools/a2a.d.ts.map +1 -1
  29. package/dist/tools/a2a.js +21 -52
  30. package/dist/tools/a2a.js.map +1 -1
  31. package/dist/tools/agent-mandates.d.ts +0 -1
  32. package/dist/tools/agent-mandates.d.ts.map +1 -1
  33. package/dist/tools/agent-mandates.js +0 -10
  34. package/dist/tools/agent-mandates.js.map +1 -1
  35. package/dist/tools/agents-refs.d.ts +0 -1
  36. package/dist/tools/agents-refs.d.ts.map +1 -1
  37. package/dist/tools/agents-refs.js +0 -8
  38. package/dist/tools/agents-refs.js.map +1 -1
  39. package/dist/tools/capabilities.d.ts +0 -1
  40. package/dist/tools/capabilities.d.ts.map +1 -1
  41. package/dist/tools/capabilities.js +0 -3
  42. package/dist/tools/capabilities.js.map +1 -1
  43. package/dist/tools/code-mode.d.ts +0 -1
  44. package/dist/tools/code-mode.d.ts.map +1 -1
  45. package/dist/tools/code-mode.js +4 -10
  46. package/dist/tools/code-mode.js.map +1 -1
  47. package/dist/tools/compliance.d.ts +0 -1
  48. package/dist/tools/compliance.d.ts.map +1 -1
  49. package/dist/tools/compliance.js +0 -1
  50. package/dist/tools/compliance.js.map +1 -1
  51. package/dist/tools/dashboard.d.ts +0 -1
  52. package/dist/tools/dashboard.d.ts.map +1 -1
  53. package/dist/tools/dashboard.js +0 -1
  54. package/dist/tools/dashboard.js.map +1 -1
  55. package/dist/tools/disputes.d.ts +0 -1
  56. package/dist/tools/disputes.d.ts.map +1 -1
  57. package/dist/tools/disputes.js +0 -5
  58. package/dist/tools/disputes.js.map +1 -1
  59. package/dist/tools/enterprises.d.ts +0 -1
  60. package/dist/tools/enterprises.d.ts.map +1 -1
  61. package/dist/tools/enterprises.js +0 -7
  62. package/dist/tools/enterprises.js.map +1 -1
  63. package/dist/tools/events.d.ts +0 -1
  64. package/dist/tools/events.d.ts.map +1 -1
  65. package/dist/tools/events.js +0 -2
  66. package/dist/tools/events.js.map +1 -1
  67. package/dist/tools/federation-admin.d.ts +0 -1
  68. package/dist/tools/federation-admin.d.ts.map +1 -1
  69. package/dist/tools/federation-admin.js +0 -25
  70. package/dist/tools/federation-admin.js.map +1 -1
  71. package/dist/tools/federation.d.ts +0 -1
  72. package/dist/tools/federation.d.ts.map +1 -1
  73. package/dist/tools/federation.js +0 -21
  74. package/dist/tools/federation.js.map +1 -1
  75. package/dist/tools/health.d.ts +0 -1
  76. package/dist/tools/health.d.ts.map +1 -1
  77. package/dist/tools/health.js +0 -2
  78. package/dist/tools/health.js.map +1 -1
  79. package/dist/tools/mandates.d.ts +0 -1
  80. package/dist/tools/mandates.d.ts.map +1 -1
  81. package/dist/tools/mandates.js +0 -11
  82. package/dist/tools/mandates.js.map +1 -1
  83. package/dist/tools/openclaw.d.ts +0 -5
  84. package/dist/tools/openclaw.d.ts.map +1 -1
  85. package/dist/tools/openclaw.js +0 -5
  86. package/dist/tools/openclaw.js.map +1 -1
  87. package/dist/tools/receipts.d.ts +0 -1
  88. package/dist/tools/receipts.d.ts.map +1 -1
  89. package/dist/tools/receipts.js +3 -8
  90. package/dist/tools/receipts.js.map +1 -1
  91. package/dist/tools/reputation.d.ts +0 -1
  92. package/dist/tools/reputation.d.ts.map +1 -1
  93. package/dist/tools/reputation.js +0 -1
  94. package/dist/tools/reputation.js.map +1 -1
  95. package/dist/tools/schemas.d.ts +0 -1
  96. package/dist/tools/schemas.d.ts.map +1 -1
  97. package/dist/tools/schemas.js +0 -17
  98. package/dist/tools/schemas.js.map +1 -1
  99. package/dist/tools/verification-keys.d.ts +4 -0
  100. package/dist/tools/verification-keys.d.ts.map +1 -0
  101. package/dist/tools/verification-keys.js +48 -0
  102. package/dist/tools/verification-keys.js.map +1 -0
  103. package/dist/tools/verification.d.ts +0 -1
  104. package/dist/tools/verification.d.ts.map +1 -1
  105. package/dist/tools/verification.js +0 -3
  106. package/dist/tools/verification.js.map +1 -1
  107. package/package.json +2 -7
  108. package/src/enums.ts +46 -0
  109. package/src/errors.ts +35 -0
  110. package/src/index.ts +100 -0
  111. package/src/resources/contract-types.ts +130 -0
  112. package/src/server.ts +132 -0
  113. package/src/tool-scopes.ts +180 -0
  114. package/src/tools/a2a.ts +519 -0
  115. package/src/tools/agent-mandates.ts +325 -0
  116. package/src/tools/agents-refs.ts +287 -0
  117. package/src/tools/capabilities.ts +76 -0
  118. package/src/tools/code-mode.ts +461 -0
  119. package/src/tools/compliance.ts +79 -0
  120. package/src/tools/dashboard.ts +73 -0
  121. package/src/tools/disputes.ts +154 -0
  122. package/src/tools/enterprises.ts +235 -0
  123. package/src/tools/events.ts +52 -0
  124. package/src/tools/federation-admin.ts +922 -0
  125. package/src/tools/federation.ts +784 -0
  126. package/src/tools/health.ts +43 -0
  127. package/src/tools/mandates.ts +382 -0
  128. package/src/tools/openclaw.ts +168 -0
  129. package/src/tools/receipts.ts +179 -0
  130. package/src/tools/reputation.ts +78 -0
  131. package/src/tools/schemas.ts +667 -0
  132. package/src/tools/verification-keys.ts +56 -0
  133. package/src/tools/verification.ts +85 -0
  134. package/tests/code-mode-manifest.test.ts +144 -0
  135. package/tests/enterprises.test.ts +96 -0
  136. package/tests/schemas.test.ts +531 -0
  137. package/tsconfig.json +12 -0
@@ -0,0 +1,519 @@
1
+ import { z } from 'zod';
2
+ import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
3
+ import type { AgledgerClient } from '@agledger/sdk';
4
+ import { errorResult, apiErrorResult } from '../errors.js';
5
+ import { ContractTypeEnum } from '../enums.js';
6
+ import { toolMeta } from '../tool-scopes.js';
7
+
8
+ export interface A2aToolsConfig {
9
+ /** Enterprise ID for search queries. Falls back to AGLEDGER_ENTERPRISE_ID env var. */
10
+ enterpriseId?: string;
11
+ }
12
+
13
+ const A2aMandateOutputSchema = z.object({
14
+ id: z.string().describe('Mandate ID'),
15
+ status: z.string().describe('Mandate lifecycle status'),
16
+ contractType: z.string().describe('Contract type identifier'),
17
+ principalAgentId: z.string().optional().describe('Agent who proposed the work'),
18
+ agentId: z.string().optional().describe('Agent assigned to do the work'),
19
+ }).passthrough().describe('Mandate details');
20
+
21
+ const ProposalsOutputSchema = z.object({
22
+ proposals: z.array(A2aMandateOutputSchema).describe('Pending proposals'),
23
+ active: z.array(A2aMandateOutputSchema).describe('Active mandates needing receipts'),
24
+ verified: z.array(A2aMandateOutputSchema).describe('Mandates ready to settle'),
25
+ }).describe('Current proposals and obligations');
26
+
27
+ const ReceiptResultOutputSchema = z.object({
28
+ receipt: z.object({
29
+ id: z.string().describe('Receipt ID'),
30
+ status: z.string().describe('Receipt status'),
31
+ }).passthrough().describe('Submitted receipt'),
32
+ mandateStatus: z.string().describe('Mandate status after receipt submission'),
33
+ }).describe('Receipt submission result');
34
+
35
+ const MandateListOutputSchema = z.object({
36
+ mandates: z.array(A2aMandateOutputSchema).describe('All mandates'),
37
+ byStatus: z.record(z.string(), z.array(A2aMandateOutputSchema)).describe('Mandates grouped by status'),
38
+ }).describe('Mandate list grouped by status');
39
+
40
+ const ReputationOutputSchema = z.object({
41
+ agentId: z.string().describe('Agent ID'),
42
+ score: z.number().optional().describe('Reputation score'),
43
+ fulfillmentRate: z.number().optional().describe('Fulfillment rate'),
44
+ }).passthrough().describe('Agent reputation details');
45
+
46
+ function getEnterpriseId(config: A2aToolsConfig): string {
47
+ return config.enterpriseId ?? process.env.AGLEDGER_ENTERPRISE_ID ?? '';
48
+ }
49
+
50
+ export function registerA2aTools(mcp: McpServer, client: AgledgerClient, config: A2aToolsConfig = {}): void {
51
+
52
+ mcp.registerTool(
53
+ 'propose_mandate',
54
+ {
55
+ title: 'Propose Mandate',
56
+ description: 'Propose accountable work to another agent. performerAgentId must differ from principalAgentId. Criteria: simple key-value pairs (strings, not nested objects).',
57
+ inputSchema: {
58
+ principalAgentId: z.string().describe('Your agent ID'),
59
+ performerAgentId: z.string().describe('Agent to do the work (must differ from yours)'),
60
+ contractType: ContractTypeEnum,
61
+ criteria: z.record(z.string(), z.unknown()).describe('Acceptance criteria (key-value pairs)'),
62
+ projectRef: z.string().optional().describe('Project grouping ID'),
63
+ parentMandateId: z.string().optional().describe('Parent mandate if delegating'),
64
+ deadline: z.string().optional().describe('ISO 8601 future deadline'),
65
+ },
66
+ outputSchema: A2aMandateOutputSchema,
67
+ annotations: {
68
+ readOnlyHint: false,
69
+ destructiveHint: false,
70
+ idempotentHint: false,
71
+ openWorldHint: true,
72
+ },
73
+ _meta: toolMeta('propose_mandate'),
74
+ },
75
+ async (args) => {
76
+ try {
77
+ if (!args.performerAgentId) {
78
+ return errorResult('performerAgentId is required — you must specify who should do the work');
79
+ }
80
+ if (args.performerAgentId === args.principalAgentId) {
81
+ return errorResult('Cannot assign work to yourself. performerAgentId must be a DIFFERENT agent.');
82
+ }
83
+ // Validate deadline is in the future, strip if not
84
+ let deadline = args.deadline;
85
+ if (deadline) {
86
+ try {
87
+ const d = new Date(deadline);
88
+ if (isNaN(d.getTime()) || d.getTime() <= Date.now()) {
89
+ deadline = undefined; // silently drop invalid/past deadlines
90
+ }
91
+ } catch { deadline = undefined; }
92
+ }
93
+ const mandate = await client.mandates.createAgent({
94
+ principalAgentId: args.principalAgentId,
95
+ performerAgentId: args.performerAgentId,
96
+ contractType: args.contractType,
97
+ contractVersion: '1',
98
+ platform: 'agledger',
99
+ criteria: args.criteria,
100
+ parentMandateId: args.parentMandateId,
101
+ deadline,
102
+ });
103
+ return {
104
+ content: [{ type: 'text', text: ` Mandate ${mandate.id} proposed (${mandate.contractType}). Status: ${mandate.status}. The performer must call accept_mandate to begin. CRITICAL: after work is done, call submit_receipt — this is REQUIRED to close the accountability loop. A mandate without a receipt is an unfulfilled commitment.` }],
105
+ structuredContent: mandate as unknown as Record<string, unknown>,
106
+ };
107
+ } catch (err) {
108
+ return apiErrorResult(err);
109
+ }
110
+ },
111
+ );
112
+
113
+ mcp.registerTool(
114
+ 'accept_mandate',
115
+ {
116
+ title: 'Accept Mandate',
117
+ description: 'Accept a proposed mandate. After accepting, do the work, then call submit_receipt.',
118
+ inputSchema: {
119
+ mandateId: z.string().describe('Mandate ID to accept'),
120
+ },
121
+ outputSchema: A2aMandateOutputSchema,
122
+ annotations: {
123
+ readOnlyHint: false,
124
+ destructiveHint: false,
125
+ idempotentHint: true,
126
+ openWorldHint: true,
127
+ },
128
+ _meta: toolMeta('accept_mandate'),
129
+ },
130
+ async (args) => {
131
+ try {
132
+ let mandate = await client.mandates.accept(args.mandateId);
133
+
134
+ // Auto-activate: CREATED → ACTIVE so receipts can be submitted immediately
135
+ if (mandate.status === 'CREATED') {
136
+ try {
137
+ mandate = await client.mandates.transition(args.mandateId, 'activate');
138
+ } catch { /* activation may fail if already active or missing prerequisites */ }
139
+ }
140
+
141
+ // Include evidence template so models know exactly what to submit
142
+ const evidenceTemplates: Record<string, string> = {
143
+ 'ACH-ANALYZE-v1': '{"deliverable": "your analysis text", "deliverable_type": "report", "summary": "key findings"}',
144
+ 'ACH-COORD-v1': '{"deliverable": "coordination outcome", "deliverable_type": "report", "summary": "what was coordinated"}',
145
+ 'ACH-DATA-v1': '{"deliverable": "data results", "deliverable_type": "report"}',
146
+ 'ACH-DLVR-v1': '{"deliverable": "artifact content", "deliverable_type": "document"}',
147
+ 'ACH-PROC-v1': '{"item_secured": "item name", "quantity": 1, "total_cost": {"amount": 100, "currency": "USD"}, "supplier": {"id": "S1", "name": "Supplier"}, "confirmation_ref": "REF-1"}',
148
+ 'ACH-TXN-v1': '{"confirmations": [{"ref": "T1", "type": "payment", "provider": "provider", "cost": {"amount": 100, "currency": "USD"}}]}',
149
+ 'ACH-COMM-v1': '{"deliverable": "communication sent", "deliverable_type": "report"}',
150
+ 'ACH-AUTH-v1': '{"deliverable": "auth action completed", "deliverable_type": "report"}',
151
+ 'ACH-INFRA-v1': '{"deliverable": "infrastructure change", "deliverable_type": "report"}',
152
+ 'ACH-DEL-v1': '{"deliverable": "deletion completed", "deliverable_type": "report"}',
153
+ 'ACH-ORCH-v1': '{"deliverable": "orchestration result", "deliverable_type": "report"}',
154
+ };
155
+ const ct = (mandate as any).contractType as string;
156
+ const template = evidenceTemplates[ct] || '{"deliverable": "work result", "deliverable_type": "report"}';
157
+
158
+ return {
159
+ content: [{ type: 'text', text: ` Mandate ${mandate.id} accepted and activated (${ct}). Status: ${mandate.status}.\n\n NEXT STEP: Do the work, then call submit_receipt with mandateId="${mandate.id}" and evidence=${template}\n\nThis is REQUIRED — do NOT skip the receipt.` }],
160
+ structuredContent: mandate as unknown as Record<string, unknown>,
161
+ };
162
+ } catch (err) {
163
+ return apiErrorResult(err);
164
+ }
165
+ },
166
+ );
167
+
168
+ mcp.registerTool(
169
+ 'check_proposals',
170
+ {
171
+ title: 'Check Proposals',
172
+ description: 'Check pending proposals and open obligations. Call at start of each round.',
173
+ inputSchema: {
174
+ agentId: z.string().optional().describe('Your agent ID'),
175
+ },
176
+ outputSchema: ProposalsOutputSchema,
177
+ annotations: {
178
+ readOnlyHint: true,
179
+ destructiveHint: false,
180
+ idempotentHint: true,
181
+ openWorldHint: true,
182
+ },
183
+ _meta: toolMeta('check_proposals'),
184
+ },
185
+ async (args) => {
186
+ try {
187
+ // Agent keys automatically scope to their mandates — don't pass enterpriseId
188
+ // Enterprise keys need enterpriseId for scoping
189
+ const enterpriseId = getEnterpriseId(config);
190
+
191
+ // Search mandates — agent key auto-scopes, enterprise key needs enterpriseId
192
+ const searchParams: Record<string, unknown> = { limit: 100 };
193
+ if (enterpriseId) searchParams.enterpriseId = enterpriseId;
194
+
195
+ const results = await client.mandates.search(searchParams as any);
196
+
197
+ const allMandates = results.data || [];
198
+
199
+ // Client-side status filtering (backend status param unreliable)
200
+ let allProposals = allMandates.filter((m: any) => m.status === 'PROPOSED');
201
+ let allActive = allMandates.filter((m: any) => m.status === 'ACTIVE');
202
+ let allVerified = allMandates.filter((m: any) => m.status === 'PROCESSING');
203
+
204
+ // Filter to this agent if ID provided
205
+ if (args.agentId) {
206
+ const id = args.agentId;
207
+ allProposals = allProposals.filter((m: any) => m.agentId === id || m.principalAgentId === id);
208
+ allActive = allActive.filter((m: any) => m.agentId === id || m.principalAgentId === id);
209
+ allVerified = allVerified.filter((m: any) => m.agentId === id || m.principalAgentId === id);
210
+ }
211
+
212
+ let text = ` ${allProposals.length} pending proposal(s)`;
213
+ if (allProposals.length > 0) {
214
+ text += ':\n' + allProposals.map((m: any) =>
215
+ ` • ${m.id} (${m.contractType}) from ${m.principalAgentId?.slice(0, 8)}... — ${JSON.stringify(m.criteria).slice(0, 100)}`
216
+ ).join('\n');
217
+ text += '\n\n→ Call accept_mandate with the mandate ID to accept.';
218
+ }
219
+
220
+ if (allActive.length > 0) {
221
+ text += `\n\n ${allActive.length} ACTIVE mandate(s) need receipts:`;
222
+ for (const m of allActive.slice(0, 5)) {
223
+ text += `\n • ${m.id} (${(m as any).contractType})`;
224
+ }
225
+ text += '\n→ Call submit_receipt with evidence of completed work!';
226
+ }
227
+
228
+ if (allVerified.length > 0) {
229
+ text += `\n\n ${allVerified.length} mandate(s) ready to settle:`;
230
+ for (const m of allVerified.slice(0, 5)) {
231
+ text += `\n • ${m.id} (${(m as any).contractType})`;
232
+ }
233
+ text += '\n→ These should auto-settle soon, or call settle_mandate to close manually.';
234
+ }
235
+
236
+ return {
237
+ content: [{ type: 'text', text }],
238
+ structuredContent: { proposals: allProposals, active: allActive, verified: allVerified } as unknown as Record<string, unknown>,
239
+ };
240
+ } catch (err) {
241
+ return apiErrorResult(err);
242
+ }
243
+ },
244
+ );
245
+
246
+ mcp.registerTool(
247
+ 'submit_receipt',
248
+ {
249
+ title: 'Submit Receipt',
250
+ description: 'Submit evidence of completed work. REQUIRED to close the accountability loop. Evidence: {"deliverable": "result", "deliverable_type": "report"} for most types. PROC needs item_secured/quantity/total_cost/supplier/confirmation_ref.',
251
+ inputSchema: {
252
+ mandateId: z.string().describe('Mandate ID'),
253
+ agentId: z.string().describe('Your agent ID'),
254
+ evidence: z.record(z.string(), z.unknown()).describe('Evidence of completion'),
255
+ },
256
+ outputSchema: ReceiptResultOutputSchema,
257
+ annotations: {
258
+ readOnlyHint: false,
259
+ destructiveHint: false,
260
+ idempotentHint: false,
261
+ openWorldHint: true,
262
+ },
263
+ _meta: toolMeta('submit_receipt'),
264
+ },
265
+ async (args) => {
266
+ try {
267
+ const { mandateId, ...submitParams } = args;
268
+ const receipt = await client.receipts.submit(mandateId, submitParams);
269
+ // Check mandate status after receipt
270
+ let mandateStatus = '';
271
+ try {
272
+ const mandate = await client.mandates.get(mandateId);
273
+ mandateStatus = mandate.status;
274
+ } catch { /* best effort */ }
275
+
276
+ let text = ` Receipt ${receipt.id} submitted for mandate ${mandateId}. Validation: ${receipt.structuralValidation}.`;
277
+ if (mandateStatus) {
278
+ text += ` Mandate is now: ${mandateStatus}.`;
279
+ }
280
+ if (mandateStatus === 'FULFILLED') {
281
+ text += `\n\n Mandate auto-settled to FULFILLED! Accountability loop closed.`;
282
+ } else if (mandateStatus === 'PROCESSING' || mandateStatus === 'PROCESSING') {
283
+ text += `\n\n Call settle_mandate("${mandateId}") to close this mandate (or wait for auto-settle).`;
284
+ } else if (mandateStatus === 'ACTIVE') {
285
+ text += ` Evidence is being verified.`;
286
+ }
287
+
288
+ return {
289
+ content: [{ type: 'text', text }],
290
+ structuredContent: { receipt, mandateStatus } as unknown as Record<string, unknown>,
291
+ };
292
+ } catch (err) {
293
+ return apiErrorResult(err);
294
+ }
295
+ },
296
+ );
297
+
298
+ mcp.registerTool(
299
+ 'settle_mandate',
300
+ {
301
+ title: 'Settle Mandate',
302
+ description: 'Settle a mandate manually. Usually auto-settles — use only if stuck in PROCESSING.',
303
+ inputSchema: {
304
+ mandateId: z.string().describe('Mandate ID'),
305
+ reason: z.string().optional().describe('Settlement reason'),
306
+ },
307
+ outputSchema: A2aMandateOutputSchema,
308
+ annotations: {
309
+ readOnlyHint: false,
310
+ destructiveHint: false,
311
+ idempotentHint: true,
312
+ openWorldHint: true,
313
+ },
314
+ _meta: toolMeta('settle_mandate'),
315
+ },
316
+ async (args) => {
317
+ try {
318
+ const mandate = await client.mandates.transition(args.mandateId, 'settle', args.reason);
319
+ return {
320
+ content: [{ type: 'text', text: ` Mandate ${mandate.id} settled. Status: ${mandate.status}. Accountability loop closed.` }],
321
+ structuredContent: mandate as unknown as Record<string, unknown>,
322
+ };
323
+ } catch (err) {
324
+ return apiErrorResult(err);
325
+ }
326
+ },
327
+ );
328
+
329
+ mcp.registerTool(
330
+ 'get_mandate',
331
+ {
332
+ title: 'Get Mandate',
333
+ description: 'Get mandate status and details by ID.',
334
+ inputSchema: {
335
+ mandateId: z.string().describe('Mandate ID'),
336
+ },
337
+ outputSchema: A2aMandateOutputSchema,
338
+ annotations: {
339
+ readOnlyHint: true,
340
+ destructiveHint: false,
341
+ idempotentHint: true,
342
+ openWorldHint: true,
343
+ },
344
+ _meta: toolMeta('get_mandate'),
345
+ },
346
+ async (args) => {
347
+ try {
348
+ const mandate = await client.mandates.get(args.mandateId);
349
+ let text = `Mandate ${mandate.id}: ${mandate.contractType}, status=${mandate.status}`;
350
+ if (mandate.status === 'ACTIVE') text += '\n This mandate needs a receipt — call submit_receipt!';
351
+ if (mandate.status === 'PROCESSING') text += '\n Should auto-settle to FULFILLED soon. Call settle_mandate if stuck.';
352
+ if (mandate.status === 'FULFILLED') text += '\n Accountability loop closed.';
353
+ return {
354
+ content: [{ type: 'text', text }],
355
+ structuredContent: mandate as unknown as Record<string, unknown>,
356
+ };
357
+ } catch (err) {
358
+ return apiErrorResult(err);
359
+ }
360
+ },
361
+ );
362
+
363
+ mcp.registerTool(
364
+ 'reject_mandate',
365
+ {
366
+ title: 'Reject Mandate',
367
+ description: 'Reject a proposed mandate with a reason.',
368
+ inputSchema: {
369
+ mandateId: z.string().describe('Mandate ID'),
370
+ reason: z.string().describe('Rejection reason'),
371
+ },
372
+ outputSchema: A2aMandateOutputSchema,
373
+ annotations: {
374
+ readOnlyHint: false,
375
+ destructiveHint: true,
376
+ idempotentHint: false,
377
+ openWorldHint: true,
378
+ },
379
+ _meta: toolMeta('reject_mandate'),
380
+ },
381
+ async (args) => {
382
+ try {
383
+ const mandate = await client.mandates.reject(args.mandateId, args.reason);
384
+ return {
385
+ content: [{ type: 'text', text: `Mandate ${mandate.id} rejected. Reason: ${args.reason}` }],
386
+ structuredContent: mandate as unknown as Record<string, unknown>,
387
+ };
388
+ } catch (err) {
389
+ return apiErrorResult(err);
390
+ }
391
+ },
392
+ );
393
+
394
+ mcp.registerTool(
395
+ 'my_mandates',
396
+ {
397
+ title: 'My Mandates',
398
+ description: 'List your mandates grouped by status. Shows what needs action.',
399
+ inputSchema: {
400
+ agentId: z.string().optional().describe('Your agent ID'),
401
+ },
402
+ outputSchema: MandateListOutputSchema,
403
+ annotations: {
404
+ readOnlyHint: true,
405
+ destructiveHint: false,
406
+ idempotentHint: true,
407
+ openWorldHint: true,
408
+ },
409
+ _meta: toolMeta('my_mandates'),
410
+ },
411
+ async (args) => {
412
+ try {
413
+ const enterpriseId = getEnterpriseId(config);
414
+
415
+ // Agent keys auto-scope — don't require enterpriseId
416
+ const searchParams: Record<string, unknown> = { limit: 100 };
417
+ if (enterpriseId) searchParams.enterpriseId = enterpriseId;
418
+
419
+ const result = await client.mandates.search(searchParams as any);
420
+ const allMandates: any[] = result.data || [];
421
+
422
+ // Filter to this agent
423
+ let filtered = allMandates;
424
+ if (args.agentId) {
425
+ const id = args.agentId;
426
+ filtered = allMandates.filter((m: any) => m.agentId === id || m.principalAgentId === id);
427
+ }
428
+
429
+ // Group by status
430
+ const byStatus: Record<string, any[]> = {};
431
+ for (const m of filtered) {
432
+ const s = m.status || 'UNKNOWN';
433
+ if (!byStatus[s]) byStatus[s] = [];
434
+ byStatus[s].push(m);
435
+ }
436
+
437
+ let text = ` ${filtered.length} mandate(s) total:`;
438
+ for (const [status, mandates] of Object.entries(byStatus)) {
439
+ text += `\n ${status}: ${mandates.length}`;
440
+ if (status === 'ACTIVE') text += ' need receipts!';
441
+ if (status === 'PROCESSING') text += ' ready to settle!';
442
+ if (status === 'PROPOSED') text += ' — awaiting response';
443
+ for (const m of mandates.slice(0, 3)) {
444
+ text += `\n • ${m.id.slice(0, 8)}... (${m.contractType})`;
445
+ }
446
+ if (mandates.length > 3) text += `\n ... and ${mandates.length - 3} more`;
447
+ }
448
+
449
+ return {
450
+ content: [{ type: 'text', text }],
451
+ structuredContent: { mandates: filtered, byStatus } as unknown as Record<string, unknown>,
452
+ };
453
+ } catch (err) {
454
+ return apiErrorResult(err);
455
+ }
456
+ },
457
+ );
458
+
459
+ mcp.registerTool(
460
+ 'cancel_mandate',
461
+ {
462
+ title: 'Cancel Mandate',
463
+ description: 'Cancel a mandate with a reason.',
464
+ inputSchema: {
465
+ mandateId: z.string().describe('Mandate ID'),
466
+ reason: z.string().describe('Cancellation reason'),
467
+ },
468
+ outputSchema: A2aMandateOutputSchema,
469
+ annotations: {
470
+ readOnlyHint: false,
471
+ destructiveHint: true,
472
+ idempotentHint: false,
473
+ openWorldHint: true,
474
+ },
475
+ _meta: toolMeta('cancel_mandate'),
476
+ },
477
+ async (args) => {
478
+ try {
479
+ const mandate = await client.mandates.cancel(args.mandateId, args.reason);
480
+ return {
481
+ content: [{ type: 'text', text: `Mandate ${mandate.id} cancelled. Reason: ${args.reason}` }],
482
+ structuredContent: mandate as unknown as Record<string, unknown>,
483
+ };
484
+ } catch (err) {
485
+ return apiErrorResult(err);
486
+ }
487
+ },
488
+ );
489
+
490
+ mcp.registerTool(
491
+ 'check_reputation',
492
+ {
493
+ title: 'Check Reputation',
494
+ description: 'Check an agent\'s reputation score and fulfillment history.',
495
+ inputSchema: {
496
+ agentId: z.string().describe('Agent ID'),
497
+ },
498
+ outputSchema: ReputationOutputSchema,
499
+ annotations: {
500
+ readOnlyHint: true,
501
+ destructiveHint: false,
502
+ idempotentHint: true,
503
+ openWorldHint: true,
504
+ },
505
+ _meta: toolMeta('check_reputation'),
506
+ },
507
+ async (args) => {
508
+ try {
509
+ const rep = await client.reputation.getAgent(args.agentId);
510
+ return {
511
+ content: [{ type: 'text', text: `Agent ${args.agentId} reputation: ${JSON.stringify(rep)}` }],
512
+ structuredContent: rep as unknown as Record<string, unknown>,
513
+ };
514
+ } catch (err) {
515
+ return apiErrorResult(err);
516
+ }
517
+ },
518
+ );
519
+ }