agent-envelope-mcp 1.1.0 → 1.2.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.
- package/README.md +9 -2
- package/package.json +14 -12
- package/server.json +15 -8
- package/src/server.js +134 -35
- package/tests/server.test.js +174 -0
package/README.md
CHANGED
|
@@ -41,6 +41,10 @@ No API key is needed to start the server or to use sovereign signature/record
|
|
|
41
41
|
verification. Hosted-governance tools require `AE_API_KEY` or, in HTTP mode, an
|
|
42
42
|
`Authorization: Bearer <portal-api-key>` header.
|
|
43
43
|
|
|
44
|
+
For verification-only deployments, set `AE_TOOLS=readonly`. In that mode the
|
|
45
|
+
server does not register `ae_mint`, so MCP clients can only call sovereign
|
|
46
|
+
verification and hosted read/query tools.
|
|
47
|
+
|
|
44
48
|
## Tools
|
|
45
49
|
|
|
46
50
|
| Tool | Mode | Credential | Notes |
|
|
@@ -52,7 +56,8 @@ verification. Hosted-governance tools require `AE_API_KEY` or, in HTTP mode, an
|
|
|
52
56
|
| `ae_authorize_action` | Hosted governance | `AE_API_KEY` or bearer | Normalizes hosted verification into an `allowed`/`denied` decision |
|
|
53
57
|
| `ae_get_delegate` | Hosted governance | `AE_API_KEY` or bearer | Fetches one active hosted delegate |
|
|
54
58
|
| `ae_check_legitimacy` | Hosted governance | `AE_API_KEY` or bearer | Normalizes legitimacy state into a decision |
|
|
55
|
-
| `ae_mint` | Hosted governance | `AE_API_KEY` or bearer | Governed mint request; returns receipt, not private material |
|
|
59
|
+
| `ae_mint` | Hosted governance | `AE_API_KEY` or bearer | Governed mint request; returns receipt, not private material. Omitted when `AE_TOOLS=readonly` |
|
|
60
|
+
| `ae_register_delegated_action` | Hosted governance | `AE_API_KEY` or bearer | Registers the public action record after an accepted mint. Omitted when `AE_TOOLS=readonly` |
|
|
56
61
|
|
|
57
62
|
Most tools return both readable MCP `content` and machine-readable
|
|
58
63
|
`structuredContent`.
|
|
@@ -239,6 +244,8 @@ await startHttp({ port: 8787, host: "127.0.0.1", path: "/mcp" });
|
|
|
239
244
|
|---|---|---|
|
|
240
245
|
| `AE_API_KEY` | Hosted tools | Portal-issued API key for hosted governance |
|
|
241
246
|
| `AE_API_BASE_URL` | Hosted tools | Optional override for the AgentEnvelope hosted API |
|
|
247
|
+
| `AE_TOOLS` | Tool exposure | Set to `readonly` to omit `ae_mint` |
|
|
248
|
+
| `AE_MCP_SESSION_IDLE_MS` | HTTP mode | Optional idle timeout for Streamable HTTP sessions; defaults to 30 minutes |
|
|
242
249
|
| `PORT` | HTTP mode | Default HTTP port when `--port` is omitted |
|
|
243
250
|
| `HOST` | HTTP mode | Default HTTP bind host when `--host` is omitted |
|
|
244
251
|
| `MCP_PATH` | HTTP mode | Default MCP path when `--path` is omitted |
|
|
@@ -246,7 +253,7 @@ await startHttp({ port: 8787, host: "127.0.0.1", path: "/mcp" });
|
|
|
246
253
|
## Security Notes
|
|
247
254
|
|
|
248
255
|
- Verification-only tools are annotated as read-only.
|
|
249
|
-
- `ae_mint`
|
|
256
|
+
- `ae_mint` and `ae_register_delegated_action` are annotated as governed, non-idempotent hosted actions.
|
|
250
257
|
- API keys meter service access; signatures prove authority.
|
|
251
258
|
- The runtime keeps secrets. The model asks for authority; AgentEnvelope returns
|
|
252
259
|
the decision.
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "agent-envelope-mcp",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"mcpName": "io.github.BlackBoxEngineering/agent-envelope-mcp",
|
|
5
|
-
"description": "MCP adapter for AgentEnvelope: sovereign verification, hosted decisions, delegates, legitimacy, and mint receipts.",
|
|
5
|
+
"description": "MCP adapter for AgentEnvelope: sovereign verification, hosted decisions, delegates, legitimacy, and mint receipts.",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"bin": {
|
|
8
8
|
"agent-envelope-mcp": "bin/agent-envelope-mcp.js"
|
|
@@ -13,23 +13,25 @@
|
|
|
13
13
|
"files": [
|
|
14
14
|
"bin",
|
|
15
15
|
"src",
|
|
16
|
+
"tests",
|
|
16
17
|
"server.json",
|
|
17
18
|
"README.md",
|
|
18
19
|
"LICENSE",
|
|
19
20
|
"NOTICE"
|
|
20
21
|
],
|
|
21
|
-
"scripts": {
|
|
22
|
-
"
|
|
23
|
-
"start
|
|
24
|
-
|
|
22
|
+
"scripts": {
|
|
23
|
+
"test": "node --test tests/*.test.js",
|
|
24
|
+
"start": "node bin/agent-envelope-mcp.js",
|
|
25
|
+
"start:http": "node bin/agent-envelope-mcp.js --http --port 8787"
|
|
26
|
+
},
|
|
25
27
|
"engines": {
|
|
26
28
|
"node": ">=18"
|
|
27
|
-
},
|
|
28
|
-
"dependencies": {
|
|
29
|
-
"@modelcontextprotocol/sdk": "^1.30.0",
|
|
30
|
-
"agent-envelope-sdk": "^1.4.
|
|
31
|
-
"zod": "^4.4.3"
|
|
32
|
-
},
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@modelcontextprotocol/sdk": "^1.30.0",
|
|
32
|
+
"agent-envelope-sdk": "^1.4.6",
|
|
33
|
+
"zod": "^4.4.3"
|
|
34
|
+
},
|
|
33
35
|
"keywords": [
|
|
34
36
|
"mcp",
|
|
35
37
|
"model-context-protocol",
|
package/server.json
CHANGED
|
@@ -6,24 +6,31 @@
|
|
|
6
6
|
"url": "https://github.com/BlackBoxEngineering/agent-envelope-mcp",
|
|
7
7
|
"source": "github"
|
|
8
8
|
},
|
|
9
|
-
"version": "1.
|
|
9
|
+
"version": "1.2.0",
|
|
10
10
|
"packages": [
|
|
11
11
|
{
|
|
12
12
|
"registryType": "npm",
|
|
13
13
|
"identifier": "agent-envelope-mcp",
|
|
14
|
-
"version": "1.
|
|
14
|
+
"version": "1.2.0",
|
|
15
15
|
"transport": {
|
|
16
16
|
"type": "stdio"
|
|
17
17
|
},
|
|
18
18
|
"environmentVariables": [
|
|
19
|
-
{
|
|
19
|
+
{
|
|
20
20
|
"name": "AE_API_KEY",
|
|
21
21
|
"description": "Portal-issued API key. Required only for hosted-governance tools (ae_get_agent, ae_verify_action, ae_authorize_action, ae_get_delegate, ae_check_legitimacy, ae_mint). Sovereign signature and record verification need no key.",
|
|
22
|
-
"isRequired": false,
|
|
23
|
-
"isSecret": true,
|
|
24
|
-
"format": "string"
|
|
25
|
-
}
|
|
26
|
-
|
|
22
|
+
"isRequired": false,
|
|
23
|
+
"isSecret": true,
|
|
24
|
+
"format": "string"
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
"name": "AE_TOOLS",
|
|
28
|
+
"description": "Optional tool exposure mode. Set to readonly to omit ae_mint and expose only verification/query tools.",
|
|
29
|
+
"isRequired": false,
|
|
30
|
+
"isSecret": false,
|
|
31
|
+
"format": "string"
|
|
32
|
+
}
|
|
33
|
+
]
|
|
27
34
|
}
|
|
28
35
|
]
|
|
29
36
|
}
|
package/src/server.js
CHANGED
|
@@ -18,9 +18,14 @@ import { AgentEnvelopeClient } from 'agent-envelope-sdk/client';
|
|
|
18
18
|
import { verifyAction as verifyActionSovereign, verifyRecord as verifyRecordSovereign } from 'agent-envelope-sdk';
|
|
19
19
|
|
|
20
20
|
const HOSTED_BASE_URL = process.env.AE_API_BASE_URL?.trim() || 'https://jemdjwteae.execute-api.us-east-1.amazonaws.com/v1';
|
|
21
|
-
const SERVER_VERSION = '1.
|
|
21
|
+
const SERVER_VERSION = '1.2.0';
|
|
22
22
|
const NEEDS_KEY =
|
|
23
23
|
'AE_API_KEY is not set. This is a hosted-governance tool; set the portal-issued API key to use it. Sovereign signature and record verification need no key.';
|
|
24
|
+
const TOOLS_MODE = process.env.AE_TOOLS?.trim().toLowerCase() || 'all';
|
|
25
|
+
const MINT_ENABLED = TOOLS_MODE !== 'readonly' && TOOLS_MODE !== 'read-only';
|
|
26
|
+
const MAX_HOSTED_CLIENTS = 100;
|
|
27
|
+
const HTTP_SESSION_IDLE_MS = Number.parseInt(process.env.AE_MCP_SESSION_IDLE_MS || '', 10) || 30 * 60 * 1000;
|
|
28
|
+
const HTTP_SESSION_SWEEP_MS = Math.min(60 * 1000, Math.max(5 * 1000, Math.floor(HTTP_SESSION_IDLE_MS / 2)));
|
|
24
29
|
|
|
25
30
|
const encoder = new TextEncoder();
|
|
26
31
|
const MAX_SIGNED_MESSAGE_BYTES = 64 * 1024;
|
|
@@ -44,7 +49,17 @@ function getApiKey(extra) {
|
|
|
44
49
|
function governanceClient(extra) {
|
|
45
50
|
const apiKey = getApiKey(extra);
|
|
46
51
|
if (!apiKey) return null;
|
|
47
|
-
if (
|
|
52
|
+
if (hostedClients.has(apiKey)) {
|
|
53
|
+
const client = hostedClients.get(apiKey);
|
|
54
|
+
hostedClients.delete(apiKey);
|
|
55
|
+
hostedClients.set(apiKey, client);
|
|
56
|
+
return client;
|
|
57
|
+
}
|
|
58
|
+
hostedClients.set(apiKey, new AgentEnvelopeClient({ apiKey }));
|
|
59
|
+
if (hostedClients.size > MAX_HOSTED_CLIENTS) {
|
|
60
|
+
const oldestKey = hostedClients.keys().next().value;
|
|
61
|
+
if (oldestKey) hostedClients.delete(oldestKey);
|
|
62
|
+
}
|
|
48
63
|
return hostedClients.get(apiKey);
|
|
49
64
|
}
|
|
50
65
|
|
|
@@ -52,6 +67,25 @@ function isPlainObject(value) {
|
|
|
52
67
|
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
|
53
68
|
}
|
|
54
69
|
|
|
70
|
+
function unwrapProxyResponse(value) {
|
|
71
|
+
if (!isPlainObject(value) || !Number.isInteger(value.statusCode) || !('body' in value)) return value;
|
|
72
|
+
let body = value.body;
|
|
73
|
+
if (typeof body === 'string') {
|
|
74
|
+
try {
|
|
75
|
+
body = JSON.parse(body);
|
|
76
|
+
} catch {
|
|
77
|
+
body = { message: body };
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (value.statusCode >= 400) {
|
|
81
|
+
const error = new Error(body?.message || body?.error || `AgentEnvelope API error ${value.statusCode}`);
|
|
82
|
+
error.status = value.statusCode;
|
|
83
|
+
error.body = body;
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
return body;
|
|
87
|
+
}
|
|
88
|
+
|
|
55
89
|
function jsonByteLength(value) {
|
|
56
90
|
try {
|
|
57
91
|
return encoder.encode(JSON.stringify(value)).length;
|
|
@@ -92,10 +126,11 @@ function fail(message, details) {
|
|
|
92
126
|
}
|
|
93
127
|
|
|
94
128
|
function hostedResult(value, fallback) {
|
|
95
|
-
|
|
96
|
-
|
|
129
|
+
const normalized = unwrapProxyResponse(value);
|
|
130
|
+
if (normalized && typeof normalized === 'object' && !Array.isArray(normalized) && 'error' in normalized) {
|
|
131
|
+
return fail(`${fallback}: ${String(normalized.error)}`, normalized);
|
|
97
132
|
}
|
|
98
|
-
return ok(
|
|
133
|
+
return ok(normalized);
|
|
99
134
|
}
|
|
100
135
|
|
|
101
136
|
function errorBody(err) {
|
|
@@ -146,18 +181,20 @@ async function readJsonBody(req) {
|
|
|
146
181
|
return JSON.parse(text);
|
|
147
182
|
}
|
|
148
183
|
|
|
149
|
-
function decisionFromReport(report, input) {
|
|
184
|
+
export function decisionFromReport(report, input) {
|
|
150
185
|
const reportValid = Boolean(report?.valid);
|
|
151
|
-
const
|
|
152
|
-
const
|
|
186
|
+
const hasOperation = typeof report?.envelope?.operation === 'string';
|
|
187
|
+
const hasResources = Array.isArray(report?.envelope?.resources);
|
|
188
|
+
const operationMatches = !input.operation || (hasOperation && report.envelope.operation === input.operation);
|
|
189
|
+
const resourceMatches = !input.resource || (hasResources && resourceAllowed(report.envelope.resources, input.resource));
|
|
153
190
|
const valid = reportValid && operationMatches && resourceMatches;
|
|
154
191
|
const reason = valid
|
|
155
192
|
? 'verified'
|
|
156
193
|
: !reportValid
|
|
157
194
|
? (report?.reason || report?.legitimacyReason || 'verification_failed')
|
|
158
195
|
: !operationMatches
|
|
159
|
-
? 'operation_mismatch'
|
|
160
|
-
: 'resource_mismatch';
|
|
196
|
+
? (hasOperation ? 'operation_mismatch' : 'operation_missing')
|
|
197
|
+
: (hasResources ? 'resource_mismatch' : 'resources_missing');
|
|
161
198
|
return {
|
|
162
199
|
type: 'agentenvelope.authorizationDecision',
|
|
163
200
|
version: 1,
|
|
@@ -202,8 +239,18 @@ const publicActionRecordSchema = z.object({
|
|
|
202
239
|
agentAddress: addressSchema,
|
|
203
240
|
status: z.string().min(1).max(32),
|
|
204
241
|
domain: z.object({
|
|
205
|
-
|
|
242
|
+
domainInfo: z.object({
|
|
243
|
+
type: z.literal('agentenvelope.domainInfo'),
|
|
244
|
+
version: z.literal(1),
|
|
245
|
+
namespace: z.string().min(1).max(128),
|
|
246
|
+
domainId: z.string().min(1).max(128),
|
|
247
|
+
kind: z.string().min(1).max(128),
|
|
248
|
+
}).passthrough(),
|
|
249
|
+
canonicalDomainInfo: z.string().min(1).max(4096),
|
|
206
250
|
domainHash: hashSchema,
|
|
251
|
+
domainAddress: addressSchema,
|
|
252
|
+
domainFingerprint: z.string().min(1).max(128),
|
|
253
|
+
createdAt: z.string().min(1).max(64),
|
|
207
254
|
}).passthrough(),
|
|
208
255
|
actionEnvelope: z.object({
|
|
209
256
|
actionIndex: z.number().int().nonnegative(),
|
|
@@ -334,7 +381,7 @@ export function createServer() {
|
|
|
334
381
|
const client = governanceClient(extra);
|
|
335
382
|
if (!client) return fail(NEEDS_KEY);
|
|
336
383
|
try {
|
|
337
|
-
const report = await client.verifyAction(input);
|
|
384
|
+
const report = unwrapProxyResponse(await client.verifyAction(input));
|
|
338
385
|
return ok(decisionFromReport(report, input));
|
|
339
386
|
} catch (err) {
|
|
340
387
|
const body = errorBody(err);
|
|
@@ -411,28 +458,55 @@ export function createServer() {
|
|
|
411
458
|
},
|
|
412
459
|
);
|
|
413
460
|
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
461
|
+
if (MINT_ENABLED) {
|
|
462
|
+
server.registerTool(
|
|
463
|
+
'ae_mint',
|
|
464
|
+
{
|
|
465
|
+
title: 'Verify mint request (hosted receipt)',
|
|
466
|
+
description:
|
|
467
|
+
'Verify a MintDelegate and signed MintRequest through hosted governance. Returns a mint receipt; it does not return private capability material. Requires a portal-issued API key. This is a governed action.',
|
|
468
|
+
annotations: governedHosted,
|
|
469
|
+
inputSchema: {
|
|
470
|
+
delegate: mintObjectSchema.describe('The signed MintDelegate'),
|
|
471
|
+
request: mintObjectSchema.describe('The bot-signed MintRequest'),
|
|
472
|
+
},
|
|
424
473
|
},
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
|
|
474
|
+
async ({ delegate, request }, extra) => {
|
|
475
|
+
const client = governanceClient(extra);
|
|
476
|
+
if (!client) return fail(NEEDS_KEY);
|
|
477
|
+
try {
|
|
478
|
+
return hostedResult(await client.mint({ delegate, request }), 'mint failed');
|
|
479
|
+
} catch (err) {
|
|
480
|
+
return fail(err instanceof Error ? err.message : 'mint failed', errorBody(err));
|
|
481
|
+
}
|
|
482
|
+
},
|
|
483
|
+
);
|
|
484
|
+
|
|
485
|
+
server.registerTool(
|
|
486
|
+
'ae_register_delegated_action',
|
|
487
|
+
{
|
|
488
|
+
title: 'Register delegated action record',
|
|
489
|
+
description:
|
|
490
|
+
'Publish a verifier-safe public action record after a successful hosted mint. The hosted service confirms the delegate, signed request, mint nonce, record metadata, plan limits, and legitimacy before registration.',
|
|
491
|
+
annotations: governedHosted,
|
|
492
|
+
inputSchema: {
|
|
493
|
+
record: publicActionRecordSchema.describe('The public action record derived from the minted capability'),
|
|
494
|
+
request: mintObjectSchema.describe('The same signed MintRequest accepted by ae_mint'),
|
|
495
|
+
delegateId: delegateIdSchema.describe('The hosted delegate used for the mint'),
|
|
496
|
+
},
|
|
497
|
+
},
|
|
498
|
+
async ({ record, request, delegateId }, extra) => {
|
|
499
|
+
try {
|
|
500
|
+
return hostedResult(await hostedJson('/sovereign/agents/register-delegated', {
|
|
501
|
+
method: 'POST',
|
|
502
|
+
body: JSON.stringify({ record, request, delegateId }),
|
|
503
|
+
}, extra), 'delegated record registration failed');
|
|
504
|
+
} catch (err) {
|
|
505
|
+
return fail(err instanceof Error ? err.message : 'delegated record registration failed', errorBody(err));
|
|
506
|
+
}
|
|
507
|
+
},
|
|
508
|
+
);
|
|
509
|
+
}
|
|
436
510
|
|
|
437
511
|
return server;
|
|
438
512
|
}
|
|
@@ -447,9 +521,27 @@ export async function start() {
|
|
|
447
521
|
/** Build the server and expose it over Streamable HTTP. */
|
|
448
522
|
export async function startHttp({ port = 8787, host = '127.0.0.1', path = '/mcp' } = {}) {
|
|
449
523
|
const transports = new Map();
|
|
524
|
+
const closeSession = (id, entry) => {
|
|
525
|
+
transports.delete(id);
|
|
526
|
+
try {
|
|
527
|
+
const closeResult = entry?.transport?.close?.();
|
|
528
|
+
if (closeResult && typeof closeResult.catch === 'function') closeResult.catch(() => {});
|
|
529
|
+
} catch {
|
|
530
|
+
// Session eviction should never crash the HTTP listener.
|
|
531
|
+
}
|
|
532
|
+
};
|
|
533
|
+
const sweepIdleSessions = () => {
|
|
534
|
+
const cutoff = Date.now() - HTTP_SESSION_IDLE_MS;
|
|
535
|
+
for (const [id, entry] of transports) {
|
|
536
|
+
if (entry.lastSeen <= cutoff) closeSession(id, entry);
|
|
537
|
+
}
|
|
538
|
+
};
|
|
539
|
+
const sweepTimer = setInterval(sweepIdleSessions, HTTP_SESSION_SWEEP_MS);
|
|
540
|
+
sweepTimer.unref?.();
|
|
450
541
|
|
|
451
542
|
const httpServer = createHttpServer(async (req, res) => {
|
|
452
543
|
try {
|
|
544
|
+
sweepIdleSessions();
|
|
453
545
|
const url = new URL(req.url || '/', `http://${req.headers.host || `${host}:${port}`}`);
|
|
454
546
|
|
|
455
547
|
if (url.pathname === '/health') {
|
|
@@ -468,7 +560,9 @@ export async function startHttp({ port = 8787, host = '127.0.0.1', path = '/mcp'
|
|
|
468
560
|
const rawSessionId = req.headers['mcp-session-id'];
|
|
469
561
|
const sessionId = Array.isArray(rawSessionId) ? rawSessionId[0] : rawSessionId;
|
|
470
562
|
const body = req.method === 'POST' ? await readJsonBody(req) : undefined;
|
|
471
|
-
|
|
563
|
+
const entry = sessionId ? transports.get(sessionId) : undefined;
|
|
564
|
+
let transport = entry?.transport;
|
|
565
|
+
if (entry) entry.lastSeen = Date.now();
|
|
472
566
|
|
|
473
567
|
if (!transport) {
|
|
474
568
|
if (req.method !== 'POST' || !isInitializeRequest(body)) {
|
|
@@ -483,7 +577,7 @@ export async function startHttp({ port = 8787, host = '127.0.0.1', path = '/mcp'
|
|
|
483
577
|
transport = new StreamableHTTPServerTransport({
|
|
484
578
|
sessionIdGenerator: () => randomUUID(),
|
|
485
579
|
enableJsonResponse: true,
|
|
486
|
-
onsessioninitialized: (id) => transports.set(id, transport),
|
|
580
|
+
onsessioninitialized: (id) => transports.set(id, { transport, lastSeen: Date.now() }),
|
|
487
581
|
});
|
|
488
582
|
|
|
489
583
|
transport.onclose = () => {
|
|
@@ -506,6 +600,11 @@ export async function startHttp({ port = 8787, host = '127.0.0.1', path = '/mcp'
|
|
|
506
600
|
}
|
|
507
601
|
});
|
|
508
602
|
|
|
603
|
+
httpServer.on('close', () => {
|
|
604
|
+
clearInterval(sweepTimer);
|
|
605
|
+
for (const [id, entry] of transports) closeSession(id, entry);
|
|
606
|
+
});
|
|
607
|
+
|
|
509
608
|
await new Promise((resolve, reject) => {
|
|
510
609
|
httpServer.once('error', reject);
|
|
511
610
|
httpServer.listen(port, host, resolve);
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import { execFileSync } from 'node:child_process';
|
|
3
|
+
import test from 'node:test';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
|
6
|
+
import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js';
|
|
7
|
+
import { createServer, decisionFromReport } from '../src/server.js';
|
|
8
|
+
|
|
9
|
+
const projectRoot = fileURLToPath(new URL('..', import.meta.url));
|
|
10
|
+
|
|
11
|
+
function spawnedToolNames(env) {
|
|
12
|
+
return execFileSync(
|
|
13
|
+
process.execPath,
|
|
14
|
+
[
|
|
15
|
+
'-e',
|
|
16
|
+
"import('./src/server.js').then(({createServer})=>console.log(Object.keys(createServer()._registeredTools||{}).sort().join(',')))",
|
|
17
|
+
],
|
|
18
|
+
{
|
|
19
|
+
cwd: projectRoot,
|
|
20
|
+
env,
|
|
21
|
+
encoding: 'utf8',
|
|
22
|
+
},
|
|
23
|
+
).trim().split(',').filter(Boolean);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
test('registers mint tool by default', () => {
|
|
27
|
+
const { AE_TOOLS: _aeTools, ...env } = process.env;
|
|
28
|
+
const names = spawnedToolNames(env);
|
|
29
|
+
assert.ok(names.includes('ae_mint'));
|
|
30
|
+
assert.ok(names.includes('ae_register_delegated_action'));
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test('AE_TOOLS=readonly omits mint tool', () => {
|
|
34
|
+
const names = spawnedToolNames({ ...process.env, AE_TOOLS: 'readonly' });
|
|
35
|
+
assert.equal(names.includes('ae_mint'), false);
|
|
36
|
+
assert.equal(names.includes('ae_register_delegated_action'), false);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test('delegated action registration uses the hosted registration route', async () => {
|
|
40
|
+
const previousKey = process.env.AE_API_KEY;
|
|
41
|
+
const previousFetch = globalThis.fetch;
|
|
42
|
+
let request;
|
|
43
|
+
process.env.AE_API_KEY = 'test-api-key';
|
|
44
|
+
globalThis.fetch = async (url, init) => {
|
|
45
|
+
request = { url, init };
|
|
46
|
+
return new Response(JSON.stringify({
|
|
47
|
+
statusCode: 201,
|
|
48
|
+
headers: { 'content-type': 'application/json' },
|
|
49
|
+
body: JSON.stringify({ recordId: 'ae-action-test' }),
|
|
50
|
+
}), {
|
|
51
|
+
status: 200,
|
|
52
|
+
headers: { 'content-type': 'application/json' },
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
try {
|
|
57
|
+
const server = createServer();
|
|
58
|
+
const result = await server._registeredTools.ae_register_delegated_action.handler({
|
|
59
|
+
record: { recordId: 'ae-action-test' },
|
|
60
|
+
request: { requestId: 'request-test' },
|
|
61
|
+
delegateId: 'ae-delegate-test0001',
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
assert.equal(result.isError, undefined);
|
|
65
|
+
assert.equal(result.structuredContent.recordId, 'ae-action-test');
|
|
66
|
+
assert.ok(request.url.endsWith('/sovereign/agents/register-delegated'));
|
|
67
|
+
assert.equal(request.init.headers['X-Api-Key'], 'test-api-key');
|
|
68
|
+
assert.deepEqual(JSON.parse(request.init.body), {
|
|
69
|
+
record: { recordId: 'ae-action-test' },
|
|
70
|
+
request: { requestId: 'request-test' },
|
|
71
|
+
delegateId: 'ae-delegate-test0001',
|
|
72
|
+
});
|
|
73
|
+
} finally {
|
|
74
|
+
globalThis.fetch = previousFetch;
|
|
75
|
+
if (previousKey === undefined) delete process.env.AE_API_KEY;
|
|
76
|
+
else process.env.AE_API_KEY = previousKey;
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test('delegated action registration accepts the canonical domain projection over MCP', async () => {
|
|
81
|
+
const previousKey = process.env.AE_API_KEY;
|
|
82
|
+
const previousFetch = globalThis.fetch;
|
|
83
|
+
let called = false;
|
|
84
|
+
process.env.AE_API_KEY = 'test-api-key';
|
|
85
|
+
globalThis.fetch = async () => {
|
|
86
|
+
called = true;
|
|
87
|
+
return new Response(JSON.stringify({ recordId: 'ae-action-test' }), {
|
|
88
|
+
status: 200,
|
|
89
|
+
headers: { 'content-type': 'application/json' },
|
|
90
|
+
});
|
|
91
|
+
};
|
|
92
|
+
const server = createServer();
|
|
93
|
+
const client = new Client({ name: 'domain-projection-test', version: '1.0.0' });
|
|
94
|
+
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
|
|
95
|
+
|
|
96
|
+
try {
|
|
97
|
+
await server.connect(serverTransport);
|
|
98
|
+
await client.connect(clientTransport);
|
|
99
|
+
const domainHash = `0x${'1'.repeat(64)}`;
|
|
100
|
+
const domainAddress = `0x${'2'.repeat(40)}`;
|
|
101
|
+
const actionEnvelopeHash = `0x${'3'.repeat(64)}`;
|
|
102
|
+
const result = await client.callTool({
|
|
103
|
+
name: 'ae_register_delegated_action',
|
|
104
|
+
arguments: {
|
|
105
|
+
record: {
|
|
106
|
+
type: 'agentenvelope.publicActionRecord',
|
|
107
|
+
version: 1,
|
|
108
|
+
recordId: 'ae-action-test',
|
|
109
|
+
agentId: 'mobile:device-01:0',
|
|
110
|
+
agentAddress: `0x${'4'.repeat(40)}`,
|
|
111
|
+
status: 'active',
|
|
112
|
+
domain: {
|
|
113
|
+
domainInfo: {
|
|
114
|
+
type: 'agentenvelope.domainInfo',
|
|
115
|
+
version: 1,
|
|
116
|
+
namespace: 'mobile-testing',
|
|
117
|
+
domainId: 'hmgcc-mobile-mimic',
|
|
118
|
+
kind: 'device',
|
|
119
|
+
},
|
|
120
|
+
canonicalDomainInfo: '{}',
|
|
121
|
+
domainHash,
|
|
122
|
+
domainAddress,
|
|
123
|
+
domainFingerprint: 'ae-domain-test',
|
|
124
|
+
createdAt: '2026-09-26T15:51:06.679Z',
|
|
125
|
+
},
|
|
126
|
+
actionEnvelope: { actionIndex: 0 },
|
|
127
|
+
actionEnvelopeHash,
|
|
128
|
+
expiry: null,
|
|
129
|
+
},
|
|
130
|
+
request: { requestId: 'request-test' },
|
|
131
|
+
delegateId: 'ae-delegate-test0001',
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
assert.equal(result.isError, undefined);
|
|
136
|
+
assert.equal(called, true);
|
|
137
|
+
} finally {
|
|
138
|
+
await client.close();
|
|
139
|
+
await server.close();
|
|
140
|
+
globalThis.fetch = previousFetch;
|
|
141
|
+
if (previousKey === undefined) delete process.env.AE_API_KEY;
|
|
142
|
+
else process.env.AE_API_KEY = previousKey;
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
test('decisionFromReport fails closed when operation is requested but absent from report envelope', () => {
|
|
147
|
+
const decision = decisionFromReport(
|
|
148
|
+
{ valid: true, envelope: { resources: ['refund:123'] } },
|
|
149
|
+
{ agentId: 'agent-1', actionIndex: 0, operation: 'refund', resource: 'refund:123' },
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
assert.equal(decision.allowed, false);
|
|
153
|
+
assert.equal(decision.reason, 'operation_missing');
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
test('decisionFromReport fails closed when resource is requested but absent from report envelope', () => {
|
|
157
|
+
const decision = decisionFromReport(
|
|
158
|
+
{ valid: true, envelope: { operation: 'refund' } },
|
|
159
|
+
{ agentId: 'agent-1', actionIndex: 0, operation: 'refund', resource: 'refund:123' },
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
assert.equal(decision.allowed, false);
|
|
163
|
+
assert.equal(decision.reason, 'resources_missing');
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
test('decisionFromReport allows matching operation and resource', () => {
|
|
167
|
+
const decision = decisionFromReport(
|
|
168
|
+
{ valid: true, envelope: { operation: 'refund', resources: ['refund:*'] } },
|
|
169
|
+
{ agentId: 'agent-1', actionIndex: 0, operation: 'refund', resource: 'refund:123' },
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
assert.equal(decision.allowed, true);
|
|
173
|
+
assert.equal(decision.reason, 'verified');
|
|
174
|
+
});
|