@kya-os/mcp-i 1.4.0 → 1.5.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.
- package/dist/runtime/auth-handshake.d.ts +144 -0
- package/dist/runtime/auth-handshake.js +254 -0
- package/dist/runtime/delegation-verifier-agentshield.d.ts +64 -0
- package/dist/runtime/delegation-verifier-agentshield.js +301 -0
- package/dist/runtime/delegation-verifier-kv.d.ts +51 -0
- package/dist/runtime/delegation-verifier-kv.js +280 -0
- package/dist/runtime/delegation-verifier-memory.d.ts +50 -0
- package/dist/runtime/delegation-verifier-memory.js +128 -0
- package/dist/runtime/delegation-verifier.d.ts +133 -0
- package/dist/runtime/delegation-verifier.js +107 -0
- package/dist/runtime/index.d.ts +7 -0
- package/dist/runtime/index.js +24 -1
- package/dist/runtime/mcpi-runtime.d.ts +31 -1
- package/dist/runtime/mcpi-runtime.js +46 -1
- package/dist/runtime/proof-batch-queue.d.ts +117 -0
- package/dist/runtime/proof-batch-queue.js +257 -0
- package/package.json +1 -1
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Memory Delegation Verifier
|
|
4
|
+
*
|
|
5
|
+
* Simple in-memory delegation storage for testing and development.
|
|
6
|
+
* NOT suitable for production - delegations lost on restart.
|
|
7
|
+
*
|
|
8
|
+
* Use for:
|
|
9
|
+
* - Unit tests
|
|
10
|
+
* - Local development
|
|
11
|
+
* - Integration tests
|
|
12
|
+
*
|
|
13
|
+
* Related: PHASE_1_XMCP_I_SERVER.md Testing section
|
|
14
|
+
*/
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.MemoryDelegationVerifier = void 0;
|
|
17
|
+
const delegation_1 = require("@kya-os/contracts/delegation");
|
|
18
|
+
const delegation_verifier_1 = require("./delegation-verifier");
|
|
19
|
+
/**
|
|
20
|
+
* Memory Delegation Verifier
|
|
21
|
+
*
|
|
22
|
+
* Simple in-memory storage (for testing only)
|
|
23
|
+
*/
|
|
24
|
+
class MemoryDelegationVerifier {
|
|
25
|
+
delegations = new Map();
|
|
26
|
+
agentIndex = new Map(); // agentDid -> Set<delegationId>
|
|
27
|
+
debug;
|
|
28
|
+
constructor(config) {
|
|
29
|
+
this.debug = config.debug || false;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Verify agent delegation
|
|
33
|
+
*/
|
|
34
|
+
async verify(agentDid, scopes, _options) {
|
|
35
|
+
// Find all delegations for this agent
|
|
36
|
+
const delegationIds = this.agentIndex.get(agentDid);
|
|
37
|
+
if (!delegationIds || delegationIds.size === 0) {
|
|
38
|
+
return {
|
|
39
|
+
valid: false,
|
|
40
|
+
reason: 'No delegation found for agent',
|
|
41
|
+
cached: false,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
// Check each delegation to find one with matching scopes
|
|
45
|
+
for (const id of delegationIds) {
|
|
46
|
+
const delegation = this.delegations.get(id);
|
|
47
|
+
if (!delegation)
|
|
48
|
+
continue;
|
|
49
|
+
// Validate delegation
|
|
50
|
+
const validation = (0, delegation_verifier_1.validateDelegation)(delegation);
|
|
51
|
+
if (!validation.valid)
|
|
52
|
+
continue;
|
|
53
|
+
// Check scopes
|
|
54
|
+
const delegationScopes = (0, delegation_verifier_1.extractScopes)(delegation);
|
|
55
|
+
if ((0, delegation_verifier_1.checkScopes)(delegationScopes, scopes)) {
|
|
56
|
+
return {
|
|
57
|
+
valid: true,
|
|
58
|
+
delegation,
|
|
59
|
+
cached: false,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
// No matching delegation found
|
|
64
|
+
return {
|
|
65
|
+
valid: false,
|
|
66
|
+
reason: 'No delegation with required scopes',
|
|
67
|
+
cached: false,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Get delegation by ID
|
|
72
|
+
*/
|
|
73
|
+
async get(delegationId) {
|
|
74
|
+
return this.delegations.get(delegationId) || null;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Store delegation
|
|
78
|
+
*/
|
|
79
|
+
async put(delegation) {
|
|
80
|
+
// Validate first
|
|
81
|
+
const parsed = delegation_1.DelegationRecordSchema.safeParse(delegation);
|
|
82
|
+
if (!parsed.success) {
|
|
83
|
+
throw new Error(`Invalid delegation record: ${parsed.error.message}`);
|
|
84
|
+
}
|
|
85
|
+
const validated = parsed.data;
|
|
86
|
+
// Store delegation
|
|
87
|
+
this.delegations.set(validated.id, validated);
|
|
88
|
+
// Update agent index
|
|
89
|
+
if (!this.agentIndex.has(validated.subjectDid)) {
|
|
90
|
+
this.agentIndex.set(validated.subjectDid, new Set());
|
|
91
|
+
}
|
|
92
|
+
this.agentIndex.get(validated.subjectDid).add(validated.id);
|
|
93
|
+
if (this.debug) {
|
|
94
|
+
console.log(`[Memory] Stored delegation ${validated.id}`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Revoke delegation
|
|
99
|
+
*/
|
|
100
|
+
async revoke(delegationId, reason) {
|
|
101
|
+
const delegation = this.delegations.get(delegationId);
|
|
102
|
+
if (!delegation) {
|
|
103
|
+
throw new Error(`Delegation not found: ${delegationId}`);
|
|
104
|
+
}
|
|
105
|
+
delegation.status = 'revoked';
|
|
106
|
+
delegation.revokedAt = Date.now();
|
|
107
|
+
if (reason) {
|
|
108
|
+
delegation.revokedReason = reason;
|
|
109
|
+
}
|
|
110
|
+
if (this.debug) {
|
|
111
|
+
console.log(`[Memory] Revoked delegation ${delegationId}`);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Clear all delegations (for testing)
|
|
116
|
+
*/
|
|
117
|
+
clear() {
|
|
118
|
+
this.delegations.clear();
|
|
119
|
+
this.agentIndex.clear();
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Get all delegations (for testing/debugging)
|
|
123
|
+
*/
|
|
124
|
+
getAll() {
|
|
125
|
+
return Array.from(this.delegations.values());
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
exports.MemoryDelegationVerifier = MemoryDelegationVerifier;
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Delegation Verification Module
|
|
3
|
+
*
|
|
4
|
+
* This module provides the core interfaces and implementations for verifying
|
|
5
|
+
* agent delegations in the MCP-I bouncer architecture.
|
|
6
|
+
*
|
|
7
|
+
* Architecture:
|
|
8
|
+
* - DelegationVerifier: Abstract interface (Dependency Inversion Principle)
|
|
9
|
+
* - Concrete adapters: CloudflareKVVerifier, AgentShieldAPIVerifier
|
|
10
|
+
* - Factory: createDelegationVerifier() selects adapter based on config
|
|
11
|
+
*
|
|
12
|
+
* Related: PHASE_1_XMCP_I_SERVER.md Epic 1 (Delegation Storage Layer)
|
|
13
|
+
*/
|
|
14
|
+
import { DelegationRecord } from '@kya-os/contracts/delegation';
|
|
15
|
+
/**
|
|
16
|
+
* Result of delegation verification
|
|
17
|
+
*/
|
|
18
|
+
export interface VerifyDelegationResult {
|
|
19
|
+
/** Whether delegation exists and is valid */
|
|
20
|
+
valid: boolean;
|
|
21
|
+
/** The delegation record (if found) */
|
|
22
|
+
delegation?: DelegationRecord;
|
|
23
|
+
/** Reason for invalid result */
|
|
24
|
+
reason?: string;
|
|
25
|
+
/** Whether result came from cache */
|
|
26
|
+
cached?: boolean;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Options for delegation verification
|
|
30
|
+
*/
|
|
31
|
+
export interface VerifyDelegationOptions {
|
|
32
|
+
/** Skip cache and force fresh lookup */
|
|
33
|
+
skipCache?: boolean;
|
|
34
|
+
/** Maximum cache age in milliseconds (default: 60000 = 1 minute) */
|
|
35
|
+
maxCacheAge?: number;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Abstract Delegation Verifier Interface
|
|
39
|
+
*
|
|
40
|
+
* Implementations:
|
|
41
|
+
* - CloudflareKVDelegationVerifier (local storage)
|
|
42
|
+
* - AgentShieldAPIDelegationVerifier (remote managed service)
|
|
43
|
+
*
|
|
44
|
+
* Follows Interface Segregation Principle (SOLID) - minimal required methods
|
|
45
|
+
*/
|
|
46
|
+
export interface DelegationVerifier {
|
|
47
|
+
/**
|
|
48
|
+
* Verify if agent has valid delegation for requested scopes
|
|
49
|
+
*
|
|
50
|
+
* @param agentDid - DID of the agent requesting access
|
|
51
|
+
* @param scopes - Required permission scopes
|
|
52
|
+
* @param options - Verification options
|
|
53
|
+
* @returns Verification result
|
|
54
|
+
*/
|
|
55
|
+
verify(agentDid: string, scopes: string[], options?: VerifyDelegationOptions): Promise<VerifyDelegationResult>;
|
|
56
|
+
/**
|
|
57
|
+
* Get delegation record by ID
|
|
58
|
+
*
|
|
59
|
+
* @param delegationId - Unique delegation identifier
|
|
60
|
+
* @returns Delegation record or null if not found
|
|
61
|
+
*/
|
|
62
|
+
get(delegationId: string): Promise<DelegationRecord | null>;
|
|
63
|
+
/**
|
|
64
|
+
* Store/update delegation record
|
|
65
|
+
*
|
|
66
|
+
* @param delegation - Delegation record to store
|
|
67
|
+
*/
|
|
68
|
+
put(delegation: DelegationRecord): Promise<void>;
|
|
69
|
+
/**
|
|
70
|
+
* Revoke delegation
|
|
71
|
+
*
|
|
72
|
+
* @param delegationId - Delegation ID to revoke
|
|
73
|
+
* @param reason - Optional revocation reason
|
|
74
|
+
*/
|
|
75
|
+
revoke(delegationId: string, reason?: string): Promise<void>;
|
|
76
|
+
/**
|
|
77
|
+
* Close any open connections (cleanup)
|
|
78
|
+
*/
|
|
79
|
+
close?(): Promise<void>;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Helper: Extract scopes from delegation (handles both simple and CRISP formats)
|
|
83
|
+
*
|
|
84
|
+
* @param delegation - Delegation record
|
|
85
|
+
* @returns Array of scope strings
|
|
86
|
+
*/
|
|
87
|
+
export declare function extractScopes(delegation: DelegationRecord): string[];
|
|
88
|
+
/**
|
|
89
|
+
* Helper: Check if delegation scopes satisfy required scopes
|
|
90
|
+
*
|
|
91
|
+
* @param delegationScopes - Scopes granted by delegation
|
|
92
|
+
* @param requiredScopes - Scopes required for operation
|
|
93
|
+
* @returns true if all required scopes are present
|
|
94
|
+
*/
|
|
95
|
+
export declare function checkScopes(delegationScopes: string[], requiredScopes: string[]): boolean;
|
|
96
|
+
/**
|
|
97
|
+
* Helper: Validate delegation record completeness
|
|
98
|
+
*
|
|
99
|
+
* @param delegation - Delegation to validate
|
|
100
|
+
* @returns Validation result with reason
|
|
101
|
+
*/
|
|
102
|
+
export declare function validateDelegation(delegation: DelegationRecord): {
|
|
103
|
+
valid: boolean;
|
|
104
|
+
reason?: string;
|
|
105
|
+
};
|
|
106
|
+
/**
|
|
107
|
+
* Configuration for delegation verifiers
|
|
108
|
+
*/
|
|
109
|
+
export interface DelegationVerifierConfig {
|
|
110
|
+
/** Verifier type */
|
|
111
|
+
type: 'cloudflare-kv' | 'agentshield-api' | 'memory';
|
|
112
|
+
/** Cloudflare KV namespace (for cloudflare-kv type) */
|
|
113
|
+
kvNamespace?: any;
|
|
114
|
+
/** AgentShield API configuration (for agentshield-api type) */
|
|
115
|
+
agentshield?: {
|
|
116
|
+
apiUrl: string;
|
|
117
|
+
apiKey: string;
|
|
118
|
+
};
|
|
119
|
+
/** Cache TTL in milliseconds (default: 60000 = 1 minute) */
|
|
120
|
+
cacheTtl?: number;
|
|
121
|
+
/** Enable debug logging */
|
|
122
|
+
debug?: boolean;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Factory: Create delegation verifier based on config
|
|
126
|
+
*
|
|
127
|
+
* Note: We import all adapters statically for better compatibility.
|
|
128
|
+
* Tree-shaking will remove unused adapters in production builds.
|
|
129
|
+
*
|
|
130
|
+
* @param config - Verifier configuration
|
|
131
|
+
* @returns DelegationVerifier instance
|
|
132
|
+
*/
|
|
133
|
+
export declare function createDelegationVerifier(config: DelegationVerifierConfig): DelegationVerifier;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Delegation Verification Module
|
|
4
|
+
*
|
|
5
|
+
* This module provides the core interfaces and implementations for verifying
|
|
6
|
+
* agent delegations in the MCP-I bouncer architecture.
|
|
7
|
+
*
|
|
8
|
+
* Architecture:
|
|
9
|
+
* - DelegationVerifier: Abstract interface (Dependency Inversion Principle)
|
|
10
|
+
* - Concrete adapters: CloudflareKVVerifier, AgentShieldAPIVerifier
|
|
11
|
+
* - Factory: createDelegationVerifier() selects adapter based on config
|
|
12
|
+
*
|
|
13
|
+
* Related: PHASE_1_XMCP_I_SERVER.md Epic 1 (Delegation Storage Layer)
|
|
14
|
+
*/
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.extractScopes = extractScopes;
|
|
17
|
+
exports.checkScopes = checkScopes;
|
|
18
|
+
exports.validateDelegation = validateDelegation;
|
|
19
|
+
exports.createDelegationVerifier = createDelegationVerifier;
|
|
20
|
+
const delegation_1 = require("@kya-os/contracts/delegation");
|
|
21
|
+
/**
|
|
22
|
+
* Helper: Extract scopes from delegation (handles both simple and CRISP formats)
|
|
23
|
+
*
|
|
24
|
+
* @param delegation - Delegation record
|
|
25
|
+
* @returns Array of scope strings
|
|
26
|
+
*/
|
|
27
|
+
function extractScopes(delegation) {
|
|
28
|
+
// Try simple scopes first (Phase 1 format)
|
|
29
|
+
if (delegation.constraints?.scopes && delegation.constraints.scopes.length > 0) {
|
|
30
|
+
return delegation.constraints.scopes;
|
|
31
|
+
}
|
|
32
|
+
// Fall back to CRISP scopes
|
|
33
|
+
if (delegation.constraints?.crisp?.scopes && delegation.constraints.crisp.scopes.length > 0) {
|
|
34
|
+
// Convert CRISP scopes to simple strings (use resource as scope name)
|
|
35
|
+
return delegation.constraints.crisp.scopes.map((s) => s.resource);
|
|
36
|
+
}
|
|
37
|
+
return [];
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Helper: Check if delegation scopes satisfy required scopes
|
|
41
|
+
*
|
|
42
|
+
* @param delegationScopes - Scopes granted by delegation
|
|
43
|
+
* @param requiredScopes - Scopes required for operation
|
|
44
|
+
* @returns true if all required scopes are present
|
|
45
|
+
*/
|
|
46
|
+
function checkScopes(delegationScopes, requiredScopes) {
|
|
47
|
+
// Check if delegation has all required scopes
|
|
48
|
+
return requiredScopes.every((required) => delegationScopes.includes(required));
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Helper: Validate delegation record completeness
|
|
52
|
+
*
|
|
53
|
+
* @param delegation - Delegation to validate
|
|
54
|
+
* @returns Validation result with reason
|
|
55
|
+
*/
|
|
56
|
+
function validateDelegation(delegation) {
|
|
57
|
+
// Check if delegation is currently valid (active + time bounds)
|
|
58
|
+
if (!(0, delegation_1.isDelegationCurrentlyValid)(delegation)) {
|
|
59
|
+
if (delegation.status === 'revoked') {
|
|
60
|
+
return { valid: false, reason: 'Delegation revoked' };
|
|
61
|
+
}
|
|
62
|
+
if (delegation.status === 'expired') {
|
|
63
|
+
return { valid: false, reason: 'Delegation expired' };
|
|
64
|
+
}
|
|
65
|
+
return { valid: false, reason: 'Delegation not currently valid' };
|
|
66
|
+
}
|
|
67
|
+
// Check required fields
|
|
68
|
+
if (!delegation.subjectDid) {
|
|
69
|
+
return { valid: false, reason: 'Missing subjectDid' };
|
|
70
|
+
}
|
|
71
|
+
// Check for scopes (either simple array or CRISP scopes)
|
|
72
|
+
const hasScopes = (delegation.constraints?.scopes && delegation.constraints.scopes.length > 0) ||
|
|
73
|
+
(delegation.constraints?.crisp?.scopes && delegation.constraints.crisp.scopes.length > 0);
|
|
74
|
+
if (!hasScopes) {
|
|
75
|
+
return { valid: false, reason: 'Missing scopes' };
|
|
76
|
+
}
|
|
77
|
+
return { valid: true };
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Factory: Create delegation verifier based on config
|
|
81
|
+
*
|
|
82
|
+
* Note: We import all adapters statically for better compatibility.
|
|
83
|
+
* Tree-shaking will remove unused adapters in production builds.
|
|
84
|
+
*
|
|
85
|
+
* @param config - Verifier configuration
|
|
86
|
+
* @returns DelegationVerifier instance
|
|
87
|
+
*/
|
|
88
|
+
function createDelegationVerifier(config) {
|
|
89
|
+
// Import adapters dynamically to avoid circular dependencies
|
|
90
|
+
// but keep them in the same file scope
|
|
91
|
+
let CloudflareKVDelegationVerifier;
|
|
92
|
+
let AgentShieldAPIDelegationVerifier;
|
|
93
|
+
let MemoryDelegationVerifier;
|
|
94
|
+
switch (config.type) {
|
|
95
|
+
case 'cloudflare-kv':
|
|
96
|
+
CloudflareKVDelegationVerifier = require('./delegation-verifier-kv').CloudflareKVDelegationVerifier;
|
|
97
|
+
return new CloudflareKVDelegationVerifier(config);
|
|
98
|
+
case 'agentshield-api':
|
|
99
|
+
AgentShieldAPIDelegationVerifier = require('./delegation-verifier-agentshield').AgentShieldAPIDelegationVerifier;
|
|
100
|
+
return new AgentShieldAPIDelegationVerifier(config);
|
|
101
|
+
case 'memory':
|
|
102
|
+
MemoryDelegationVerifier = require('./delegation-verifier-memory').MemoryDelegationVerifier;
|
|
103
|
+
return new MemoryDelegationVerifier(config);
|
|
104
|
+
default:
|
|
105
|
+
throw new Error(`Unknown delegation verifier type: ${config.type}`);
|
|
106
|
+
}
|
|
107
|
+
}
|
package/dist/runtime/index.d.ts
CHANGED
|
@@ -13,5 +13,12 @@ export { AuditLogger, defaultAuditLogger, logKeyRotationAudit, parseAuditLine, v
|
|
|
13
13
|
export { WellKnownManager, createWellKnownHandler, validateDIDDocument, validateAgentDocument, extractDIDFromPath, type DIDDocument, type VerificationMethod, type ServiceEndpoint, type AgentDocument, type WellKnownConfig, type WellKnownHandler, } from "./well-known";
|
|
14
14
|
export { DebugManager, createDebugEndpoint, type DebugVerificationResult, type DebugPageData, } from "./debug";
|
|
15
15
|
export { DemoManager, createDemoManager, DemoConsole, formatVerifyLink, type DemoConfig, } from "./demo";
|
|
16
|
+
export { type DelegationVerifier, type DelegationVerifierConfig, type VerifyDelegationResult, type VerifyDelegationOptions, createDelegationVerifier, checkScopes, validateDelegation, extractScopes, } from "./delegation-verifier";
|
|
17
|
+
export { type AuthHandshakeConfig, type VerifyOrHintsResult, type AgentReputation, type ResumeTokenStore, verifyOrHints, hasSensitiveScopes, MemoryResumeTokenStore, } from "./auth-handshake";
|
|
18
|
+
export { type ProofDestination, type ProofBatchQueueConfig, ProofBatchQueue, KTAProofDestination, AgentShieldProofDestination, createProofBatchQueue, } from "./proof-batch-queue";
|
|
19
|
+
export { CloudflareKVDelegationVerifier, } from "./delegation-verifier-kv";
|
|
20
|
+
export { AgentShieldAPIDelegationVerifier, } from "./delegation-verifier-agentshield";
|
|
21
|
+
export { MemoryDelegationVerifier, } from "./delegation-verifier-memory";
|
|
16
22
|
export type { HandshakeRequest, SessionContext, NonceCache, NonceCacheEntry, NonceCacheConfig, } from "@kya-os/contracts/handshake";
|
|
17
23
|
export type { ProofMeta, DetachedProof, CanonicalHashes, AuditRecord, } from "@kya-os/contracts/proof";
|
|
24
|
+
export type { DelegationRecord, DelegationStatus, DelegationChain, DelegationConstraints, } from "@kya-os/contracts/delegation";
|
package/dist/runtime/index.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* audit logging, and well-known endpoints.
|
|
7
7
|
*/
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.formatVerifyLink = exports.DemoConsole = exports.createDemoManager = exports.DemoManager = exports.createDebugEndpoint = exports.DebugManager = exports.extractDIDFromPath = exports.validateAgentDocument = exports.validateDIDDocument = exports.createWellKnownHandler = exports.WellKnownManager = exports.validateAuditRecord = exports.parseAuditLine = exports.logKeyRotationAudit = exports.defaultAuditLogger = exports.AuditLogger = exports.extractCanonicalData = exports.createProofResponse = exports.ProofGenerator = exports.validateHandshakeFormat = exports.createHandshakeRequest = exports.defaultSessionManager = exports.SessionManager = exports.IDENTITY_ERRORS = exports.ensureIdentity = exports.defaultIdentityManager = exports.IdentityManager = exports.RUNTIME_ERRORS = exports.RuntimeFactory = exports.createMCPIRuntime = exports.MCPIRuntime = void 0;
|
|
9
|
+
exports.MemoryDelegationVerifier = exports.AgentShieldAPIDelegationVerifier = exports.CloudflareKVDelegationVerifier = exports.createProofBatchQueue = exports.AgentShieldProofDestination = exports.KTAProofDestination = exports.ProofBatchQueue = exports.MemoryResumeTokenStore = exports.hasSensitiveScopes = exports.verifyOrHints = exports.extractScopes = exports.validateDelegation = exports.checkScopes = exports.createDelegationVerifier = exports.formatVerifyLink = exports.DemoConsole = exports.createDemoManager = exports.DemoManager = exports.createDebugEndpoint = exports.DebugManager = exports.extractDIDFromPath = exports.validateAgentDocument = exports.validateDIDDocument = exports.createWellKnownHandler = exports.WellKnownManager = exports.validateAuditRecord = exports.parseAuditLine = exports.logKeyRotationAudit = exports.defaultAuditLogger = exports.AuditLogger = exports.extractCanonicalData = exports.createProofResponse = exports.ProofGenerator = exports.validateHandshakeFormat = exports.createHandshakeRequest = exports.defaultSessionManager = exports.SessionManager = exports.IDENTITY_ERRORS = exports.ensureIdentity = exports.defaultIdentityManager = exports.IdentityManager = exports.RUNTIME_ERRORS = exports.RuntimeFactory = exports.createMCPIRuntime = exports.MCPIRuntime = void 0;
|
|
10
10
|
// Main runtime - now using core with Node.js providers
|
|
11
11
|
var mcpi_runtime_wrapper_1 = require("./mcpi-runtime-wrapper");
|
|
12
12
|
Object.defineProperty(exports, "MCPIRuntime", { enumerable: true, get: function () { return mcpi_runtime_wrapper_1.MCPIRuntimeWrapper; } });
|
|
@@ -56,3 +56,26 @@ Object.defineProperty(exports, "DemoManager", { enumerable: true, get: function
|
|
|
56
56
|
Object.defineProperty(exports, "createDemoManager", { enumerable: true, get: function () { return demo_1.createDemoManager; } });
|
|
57
57
|
Object.defineProperty(exports, "DemoConsole", { enumerable: true, get: function () { return demo_1.DemoConsole; } });
|
|
58
58
|
Object.defineProperty(exports, "formatVerifyLink", { enumerable: true, get: function () { return demo_1.formatVerifyLink; } });
|
|
59
|
+
// Delegation verification (NEW - Phase 1)
|
|
60
|
+
var delegation_verifier_1 = require("./delegation-verifier");
|
|
61
|
+
Object.defineProperty(exports, "createDelegationVerifier", { enumerable: true, get: function () { return delegation_verifier_1.createDelegationVerifier; } });
|
|
62
|
+
Object.defineProperty(exports, "checkScopes", { enumerable: true, get: function () { return delegation_verifier_1.checkScopes; } });
|
|
63
|
+
Object.defineProperty(exports, "validateDelegation", { enumerable: true, get: function () { return delegation_verifier_1.validateDelegation; } });
|
|
64
|
+
Object.defineProperty(exports, "extractScopes", { enumerable: true, get: function () { return delegation_verifier_1.extractScopes; } });
|
|
65
|
+
// Authorization handshake (NEW - Phase 1)
|
|
66
|
+
var auth_handshake_1 = require("./auth-handshake");
|
|
67
|
+
Object.defineProperty(exports, "verifyOrHints", { enumerable: true, get: function () { return auth_handshake_1.verifyOrHints; } });
|
|
68
|
+
Object.defineProperty(exports, "hasSensitiveScopes", { enumerable: true, get: function () { return auth_handshake_1.hasSensitiveScopes; } });
|
|
69
|
+
Object.defineProperty(exports, "MemoryResumeTokenStore", { enumerable: true, get: function () { return auth_handshake_1.MemoryResumeTokenStore; } });
|
|
70
|
+
// Proof batching (NEW - Phase 1)
|
|
71
|
+
var proof_batch_queue_1 = require("./proof-batch-queue");
|
|
72
|
+
Object.defineProperty(exports, "ProofBatchQueue", { enumerable: true, get: function () { return proof_batch_queue_1.ProofBatchQueue; } });
|
|
73
|
+
Object.defineProperty(exports, "KTAProofDestination", { enumerable: true, get: function () { return proof_batch_queue_1.KTAProofDestination; } });
|
|
74
|
+
Object.defineProperty(exports, "AgentShieldProofDestination", { enumerable: true, get: function () { return proof_batch_queue_1.AgentShieldProofDestination; } });
|
|
75
|
+
Object.defineProperty(exports, "createProofBatchQueue", { enumerable: true, get: function () { return proof_batch_queue_1.createProofBatchQueue; } });
|
|
76
|
+
var delegation_verifier_kv_1 = require("./delegation-verifier-kv");
|
|
77
|
+
Object.defineProperty(exports, "CloudflareKVDelegationVerifier", { enumerable: true, get: function () { return delegation_verifier_kv_1.CloudflareKVDelegationVerifier; } });
|
|
78
|
+
var delegation_verifier_agentshield_1 = require("./delegation-verifier-agentshield");
|
|
79
|
+
Object.defineProperty(exports, "AgentShieldAPIDelegationVerifier", { enumerable: true, get: function () { return delegation_verifier_agentshield_1.AgentShieldAPIDelegationVerifier; } });
|
|
80
|
+
var delegation_verifier_memory_1 = require("./delegation-verifier-memory");
|
|
81
|
+
Object.defineProperty(exports, "MemoryDelegationVerifier", { enumerable: true, get: function () { return delegation_verifier_memory_1.MemoryDelegationVerifier; } });
|
|
@@ -8,6 +8,8 @@ import { SessionContext, HandshakeRequest } from "@kya-os/contracts/handshake";
|
|
|
8
8
|
import { ToolRequest, ToolResponse } from "./proof";
|
|
9
9
|
import { WellKnownConfig } from "./well-known";
|
|
10
10
|
import { DemoManager } from "./demo";
|
|
11
|
+
import { DelegationVerifierConfig } from "./delegation-verifier";
|
|
12
|
+
import { NeedsAuthorizationError } from "@kya-os/contracts/runtime";
|
|
11
13
|
/**
|
|
12
14
|
* Runtime environment check
|
|
13
15
|
*/
|
|
@@ -39,6 +41,28 @@ export interface MCPIRuntimeConfig {
|
|
|
39
41
|
includePayloads?: boolean;
|
|
40
42
|
};
|
|
41
43
|
wellKnown?: WellKnownConfig;
|
|
44
|
+
delegation?: {
|
|
45
|
+
/** Enable delegation checks (default: false for backward compatibility) */
|
|
46
|
+
enabled?: boolean;
|
|
47
|
+
/** Delegation verifier configuration */
|
|
48
|
+
verifier?: DelegationVerifierConfig;
|
|
49
|
+
/** Authorization handshake configuration */
|
|
50
|
+
authorization?: {
|
|
51
|
+
/** Authorization URL base for consent flow */
|
|
52
|
+
authorizationUrl?: string;
|
|
53
|
+
/** KTA API configuration for reputation checks */
|
|
54
|
+
kta?: {
|
|
55
|
+
apiUrl: string;
|
|
56
|
+
apiKey?: string;
|
|
57
|
+
};
|
|
58
|
+
/** Minimum reputation score to bypass authorization (0-100) */
|
|
59
|
+
minReputationScore?: number;
|
|
60
|
+
/** Resume token TTL in milliseconds */
|
|
61
|
+
resumeTokenTtl?: number;
|
|
62
|
+
/** Require authorization for unknown agents */
|
|
63
|
+
requireAuthForUnknown?: boolean;
|
|
64
|
+
};
|
|
65
|
+
};
|
|
42
66
|
runtime?: {
|
|
43
67
|
showVerifyLink?: boolean;
|
|
44
68
|
identityBadge?: boolean;
|
|
@@ -57,6 +81,7 @@ export declare class MCPIRuntime {
|
|
|
57
81
|
private wellKnownManager?;
|
|
58
82
|
private debugManager?;
|
|
59
83
|
private demoManager?;
|
|
84
|
+
private delegationVerifier?;
|
|
60
85
|
private config;
|
|
61
86
|
private cachedIdentity?;
|
|
62
87
|
constructor(config?: MCPIRuntimeConfig);
|
|
@@ -70,11 +95,16 @@ export declare class MCPIRuntime {
|
|
|
70
95
|
validateHandshake(request: HandshakeRequest): Promise<SessionContext | null>;
|
|
71
96
|
/**
|
|
72
97
|
* Process tool call with identity-aware proof generation
|
|
98
|
+
*
|
|
99
|
+
* NEW (Phase 1): Includes delegation verification interceptor
|
|
73
100
|
*/
|
|
74
101
|
processToolCall(request: ToolRequest, session: SessionContext, toolHandler: (request: ToolRequest) => Promise<any>, options?: {
|
|
75
102
|
scopeId?: string;
|
|
76
103
|
delegationRef?: string;
|
|
77
|
-
|
|
104
|
+
requiresDelegation?: boolean;
|
|
105
|
+
requiredScopes?: string[];
|
|
106
|
+
agentDid?: string;
|
|
107
|
+
}): Promise<ToolResponse | NeedsAuthorizationError>;
|
|
78
108
|
/**
|
|
79
109
|
* Get well-known endpoint handler
|
|
80
110
|
*/
|
|
@@ -15,6 +15,8 @@ const audit_1 = require("./audit");
|
|
|
15
15
|
const well_known_1 = require("./well-known");
|
|
16
16
|
const debug_1 = require("./debug");
|
|
17
17
|
const demo_1 = require("./demo");
|
|
18
|
+
const delegation_verifier_1 = require("./delegation-verifier");
|
|
19
|
+
const auth_handshake_1 = require("./auth-handshake");
|
|
18
20
|
/**
|
|
19
21
|
* XMCP-I Runtime class
|
|
20
22
|
*/
|
|
@@ -25,6 +27,7 @@ class MCPIRuntime {
|
|
|
25
27
|
wellKnownManager;
|
|
26
28
|
debugManager;
|
|
27
29
|
demoManager;
|
|
30
|
+
delegationVerifier; // NEW - Phase 1
|
|
28
31
|
config;
|
|
29
32
|
cachedIdentity;
|
|
30
33
|
constructor(config = {}) {
|
|
@@ -38,6 +41,10 @@ class MCPIRuntime {
|
|
|
38
41
|
this.sessionManager = new session_1.SessionManager(config.session);
|
|
39
42
|
// Initialize audit logger
|
|
40
43
|
this.auditLogger = new audit_1.AuditLogger(config.audit);
|
|
44
|
+
// Initialize delegation verifier (NEW - Phase 1)
|
|
45
|
+
if (config.delegation?.enabled && config.delegation?.verifier) {
|
|
46
|
+
this.delegationVerifier = (0, delegation_verifier_1.createDelegationVerifier)(config.delegation.verifier);
|
|
47
|
+
}
|
|
41
48
|
}
|
|
42
49
|
/**
|
|
43
50
|
* Initialize the runtime (async setup)
|
|
@@ -87,13 +94,51 @@ class MCPIRuntime {
|
|
|
87
94
|
}
|
|
88
95
|
/**
|
|
89
96
|
* Process tool call with identity-aware proof generation
|
|
97
|
+
*
|
|
98
|
+
* NEW (Phase 1): Includes delegation verification interceptor
|
|
90
99
|
*/
|
|
91
100
|
async processToolCall(request, session, toolHandler, options = {}) {
|
|
92
101
|
if (!this.cachedIdentity) {
|
|
93
102
|
throw new Error("Runtime not initialized - call initialize() first");
|
|
94
103
|
}
|
|
104
|
+
// PHASE 1 INTERCEPTOR: Check delegation BEFORE executing tool
|
|
105
|
+
if (this.config.delegation?.enabled && options.requiresDelegation) {
|
|
106
|
+
if (!this.delegationVerifier) {
|
|
107
|
+
throw new Error("Delegation verifier not configured");
|
|
108
|
+
}
|
|
109
|
+
if (!options.agentDid) {
|
|
110
|
+
throw new Error("agentDid required when requiresDelegation=true");
|
|
111
|
+
}
|
|
112
|
+
const requiredScopes = options.requiredScopes || [];
|
|
113
|
+
// Build auth handshake config
|
|
114
|
+
const authConfig = {
|
|
115
|
+
delegationVerifier: this.delegationVerifier,
|
|
116
|
+
kta: this.config.delegation.authorization?.kta,
|
|
117
|
+
bouncer: {
|
|
118
|
+
authorizationUrl: this.config.delegation.authorization?.authorizationUrl ||
|
|
119
|
+
"https://agentshield.example.com/consent",
|
|
120
|
+
resumeTokenTtl: this.config.delegation.authorization?.resumeTokenTtl,
|
|
121
|
+
minReputationScore: this.config.delegation.authorization?.minReputationScore,
|
|
122
|
+
requireAuthForUnknown: this.config.delegation.authorization?.requireAuthForUnknown,
|
|
123
|
+
},
|
|
124
|
+
debug: this.config.identity?.environment === "development",
|
|
125
|
+
};
|
|
126
|
+
// Verify delegation or return authorization hints
|
|
127
|
+
const verifyResult = await (0, auth_handshake_1.verifyOrHints)(options.agentDid, requiredScopes, authConfig);
|
|
128
|
+
// If not authorized, return needs_authorization error
|
|
129
|
+
if (!verifyResult.authorized) {
|
|
130
|
+
if (!verifyResult.authError) {
|
|
131
|
+
throw new Error('Authorization failed but no authError was provided');
|
|
132
|
+
}
|
|
133
|
+
return verifyResult.authError;
|
|
134
|
+
}
|
|
135
|
+
// If authorized, log the delegation ID for audit trail
|
|
136
|
+
if (verifyResult.delegation) {
|
|
137
|
+
options.delegationRef = verifyResult.delegation.id;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
95
140
|
try {
|
|
96
|
-
// Execute the tool
|
|
141
|
+
// Execute the tool (only if delegation check passed)
|
|
97
142
|
let data = await toolHandler(request);
|
|
98
143
|
// Add identity badge if enabled
|
|
99
144
|
if (this.demoManager?.isIdentityBadgeEnabled()) {
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Proof Batch Queue
|
|
3
|
+
*
|
|
4
|
+
* Collects proofs in memory and submits them in batches to KTA and AgentShield.
|
|
5
|
+
* This prevents blocking tool execution while ensuring proofs are eventually submitted.
|
|
6
|
+
*
|
|
7
|
+
* Performance:
|
|
8
|
+
* - Batch size: 10 proofs (configurable)
|
|
9
|
+
* - Flush interval: 5 seconds (configurable)
|
|
10
|
+
* - Fire-and-forget submission (doesn't block tool execution)
|
|
11
|
+
*
|
|
12
|
+
* Retry Strategy:
|
|
13
|
+
* - Exponential backoff: 1s, 2s, 4s, 8s, 16s
|
|
14
|
+
* - Max retries: 5
|
|
15
|
+
* - Failed proofs logged and dropped after max retries
|
|
16
|
+
*
|
|
17
|
+
* Related: PHASE_1_XMCP_I_SERVER.md Epic 3 (Proof Batching)
|
|
18
|
+
*/
|
|
19
|
+
import { DetachedProof } from '@kya-os/contracts/proof';
|
|
20
|
+
/**
|
|
21
|
+
* Proof submission destination
|
|
22
|
+
*/
|
|
23
|
+
export interface ProofDestination {
|
|
24
|
+
/** Destination name (for logging) */
|
|
25
|
+
name: string;
|
|
26
|
+
/** Submit batch of proofs */
|
|
27
|
+
submit(proofs: DetachedProof[]): Promise<void>;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* KTA proof submission destination
|
|
31
|
+
*/
|
|
32
|
+
export declare class KTAProofDestination implements ProofDestination {
|
|
33
|
+
name: string;
|
|
34
|
+
private apiUrl;
|
|
35
|
+
private apiKey?;
|
|
36
|
+
constructor(apiUrl: string, apiKey?: string);
|
|
37
|
+
submit(proofs: DetachedProof[]): Promise<void>;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* AgentShield proof submission destination
|
|
41
|
+
*/
|
|
42
|
+
export declare class AgentShieldProofDestination implements ProofDestination {
|
|
43
|
+
name: string;
|
|
44
|
+
private apiUrl;
|
|
45
|
+
private apiKey;
|
|
46
|
+
constructor(apiUrl: string, apiKey: string);
|
|
47
|
+
submit(proofs: DetachedProof[]): Promise<void>;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Proof batch queue configuration
|
|
51
|
+
*/
|
|
52
|
+
export interface ProofBatchQueueConfig {
|
|
53
|
+
/** Destinations to submit proofs to */
|
|
54
|
+
destinations: ProofDestination[];
|
|
55
|
+
/** Maximum batch size (default: 10) */
|
|
56
|
+
maxBatchSize?: number;
|
|
57
|
+
/** Flush interval in milliseconds (default: 5000 = 5 seconds) */
|
|
58
|
+
flushIntervalMs?: number;
|
|
59
|
+
/** Maximum retries per batch (default: 5) */
|
|
60
|
+
maxRetries?: number;
|
|
61
|
+
/** Enable debug logging */
|
|
62
|
+
debug?: boolean;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Proof Batch Queue
|
|
66
|
+
*
|
|
67
|
+
* Collects proofs and submits them in batches to multiple destinations
|
|
68
|
+
*/
|
|
69
|
+
export declare class ProofBatchQueue {
|
|
70
|
+
private queue;
|
|
71
|
+
private pendingBatches;
|
|
72
|
+
private config;
|
|
73
|
+
private flushTimer?;
|
|
74
|
+
private retryTimer?;
|
|
75
|
+
private closed;
|
|
76
|
+
private stats;
|
|
77
|
+
constructor(config: ProofBatchQueueConfig);
|
|
78
|
+
/**
|
|
79
|
+
* Add proof to queue
|
|
80
|
+
*/
|
|
81
|
+
enqueue(proof: DetachedProof): void;
|
|
82
|
+
/**
|
|
83
|
+
* Flush queue immediately (submit all queued proofs)
|
|
84
|
+
*/
|
|
85
|
+
flush(): Promise<void>;
|
|
86
|
+
/**
|
|
87
|
+
* Submit batch to destination (with retries)
|
|
88
|
+
*/
|
|
89
|
+
private submitBatch;
|
|
90
|
+
/**
|
|
91
|
+
* Start flush timer
|
|
92
|
+
*/
|
|
93
|
+
private startFlushTimer;
|
|
94
|
+
/**
|
|
95
|
+
* Start retry timer
|
|
96
|
+
*/
|
|
97
|
+
private startRetryTimer;
|
|
98
|
+
/**
|
|
99
|
+
* Close queue and flush remaining proofs
|
|
100
|
+
*/
|
|
101
|
+
close(): Promise<void>;
|
|
102
|
+
/**
|
|
103
|
+
* Get queue statistics
|
|
104
|
+
*/
|
|
105
|
+
getStats(): {
|
|
106
|
+
queueSize: number;
|
|
107
|
+
pendingBatches: number;
|
|
108
|
+
queued: number;
|
|
109
|
+
submitted: number;
|
|
110
|
+
failed: number;
|
|
111
|
+
batchesSubmitted: number;
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Create proof batch queue from config
|
|
116
|
+
*/
|
|
117
|
+
export declare function createProofBatchQueue(config: ProofBatchQueueConfig): ProofBatchQueue;
|