@kya-os/mcp-i 1.5.3-canary.1 → 1.5.4

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 (50) hide show
  1. package/dist/auth/jwt.d.ts +1 -1
  2. package/dist/auth/oauth/router.js +3 -8
  3. package/dist/cli-adapter/index.js +1 -1
  4. package/dist/cli-adapter/kta-registration.d.ts +1 -1
  5. package/dist/cli-adapter/kta-registration.js +2 -2
  6. package/dist/compiler/config/injection.js +2 -2
  7. package/dist/compiler/get-webpack-config/get-entries.js +12 -8
  8. package/dist/providers/node-providers.d.ts +1 -1
  9. package/dist/providers/node-providers.js +4 -4
  10. package/dist/runtime/adapter-express.js +1 -1
  11. package/dist/runtime/adapter-nextjs.js +1 -1
  12. package/dist/runtime/audit.d.ts +287 -3
  13. package/dist/runtime/audit.js +169 -4
  14. package/dist/runtime/auth-handshake.d.ts +1 -1
  15. package/dist/runtime/auth-handshake.js +1 -1
  16. package/dist/runtime/debug.d.ts +2 -2
  17. package/dist/runtime/debug.js +3 -3
  18. package/dist/runtime/delegation/index.d.ts +7 -0
  19. package/dist/runtime/delegation/index.js +23 -0
  20. package/dist/runtime/delegation/vc-issuer.d.ts +119 -0
  21. package/dist/runtime/delegation/vc-issuer.js +220 -0
  22. package/dist/runtime/delegation/vc-verifier.d.ts +193 -0
  23. package/dist/runtime/delegation/vc-verifier.js +387 -0
  24. package/dist/runtime/http.js +1 -1
  25. package/dist/runtime/identity.d.ts +10 -2
  26. package/dist/runtime/identity.js +68 -11
  27. package/dist/runtime/mcpi-runtime.d.ts +4 -1
  28. package/dist/runtime/mcpi-runtime.js +2 -2
  29. package/dist/runtime/migrate-identity.d.ts +16 -0
  30. package/dist/runtime/migrate-identity.js +118 -0
  31. package/dist/runtime/proof.js +2 -2
  32. package/dist/runtime/stdio.js +1 -1
  33. package/dist/runtime/transports/http/index.js +3 -1
  34. package/dist/runtime/utils/time.d.ts +80 -0
  35. package/dist/runtime/utils/time.js +117 -0
  36. package/dist/runtime/utils/tools.js +22 -3
  37. package/dist/runtime/verifier-middleware.js +1 -1
  38. package/dist/runtime/well-known.d.ts +0 -4
  39. package/dist/runtime/well-known.js +12 -26
  40. package/dist/storage/delegation.js +2 -2
  41. package/dist/test/deterministic-keys.d.ts +1 -1
  42. package/dist/test/deterministic-keys.js +6 -6
  43. package/dist/test/examples/test-usage-example.d.ts +6 -6
  44. package/dist/test/examples/test-usage-example.js +5 -5
  45. package/dist/test/local-verification.d.ts +1 -1
  46. package/dist/test/local-verification.js +10 -10
  47. package/dist/test/mock-identity-provider.d.ts +4 -4
  48. package/dist/test/mock-identity-provider.js +7 -7
  49. package/dist/test/runtime-integration.d.ts +2 -2
  50. package/package.json +4 -3
@@ -0,0 +1,119 @@
1
+ /**
2
+ * Delegation Credential Issuer
3
+ *
4
+ * Issues W3C Verifiable Credentials for delegations with Ed25519 signatures.
5
+ * Follows the Python POC design (Delegation-Service.md:136-163) where
6
+ * delegations are issued AS W3C VCs.
7
+ *
8
+ * Related Spec: MCP-I §4.1, §4.2, W3C VC Data Model 1.1
9
+ * Python Reference: Delegation-Service.md
10
+ */
11
+ import { DelegationCredential, DelegationRecord } from '@kya-os/contracts/delegation';
12
+ import { CredentialStatus } from '@kya-os/contracts/vc';
13
+ import { AgentIdentity } from '../identity';
14
+ /**
15
+ * Options for issuing a delegation credential
16
+ */
17
+ export interface IssueDelegationOptions {
18
+ /** VC ID (optional, will be generated if not provided) */
19
+ id?: string;
20
+ /** Issuance date (optional, defaults to now) */
21
+ issuanceDate?: string;
22
+ /** Expiration date (optional, derived from constraints if not provided) */
23
+ expirationDate?: string;
24
+ /** Credential status for StatusList2021 (optional) */
25
+ credentialStatus?: CredentialStatus;
26
+ /** Additional context URIs (optional) */
27
+ additionalContexts?: string[];
28
+ }
29
+ /**
30
+ * Delegation Credential Issuer
31
+ *
32
+ * Issues W3C Verifiable Credentials for delegations.
33
+ * Per Python POC (Delegation-Service.md:136-146):
34
+ * - Every delegation MUST be issued as a VC
35
+ * - VC is signed with Ed25519 (Ed25519Signature2020)
36
+ * - StatusList2021 support for efficient revocation
37
+ */
38
+ export declare class DelegationCredentialIssuer {
39
+ private identity;
40
+ constructor(identity: AgentIdentity);
41
+ /**
42
+ * Issue a delegation credential
43
+ *
44
+ * Creates a W3C Verifiable Credential from a delegation record.
45
+ * Signs it with Ed25519 and returns the complete DelegationCredential.
46
+ *
47
+ * @param delegation - The delegation record to issue as a VC
48
+ * @param options - Issuance options
49
+ * @returns Signed DelegationCredential
50
+ */
51
+ issueDelegationCredential(delegation: DelegationRecord, options?: IssueDelegationOptions): Promise<DelegationCredential>;
52
+ /**
53
+ * Create a delegation record and issue it as a VC in one step
54
+ *
55
+ * Convenience method for creating a new delegation from scratch.
56
+ *
57
+ * @param params - Delegation parameters
58
+ * @param options - Issuance options
59
+ * @returns Signed DelegationCredential
60
+ */
61
+ createAndIssueDelegation(params: {
62
+ id: string;
63
+ issuerDid: string;
64
+ subjectDid: string;
65
+ controller?: string;
66
+ parentId?: string;
67
+ constraints: DelegationRecord['constraints'];
68
+ status?: DelegationRecord['status'];
69
+ metadata?: Record<string, any>;
70
+ }, options?: IssueDelegationOptions): Promise<DelegationCredential>;
71
+ /**
72
+ * Canonicalize VC for signing
73
+ *
74
+ * Uses JCS (JSON Canonicalization Scheme, RFC 8785) to create
75
+ * a deterministic representation of the VC.
76
+ *
77
+ * @param vc - The unsigned VC
78
+ * @returns Canonical JSON string
79
+ */
80
+ private canonicalizeVC;
81
+ /**
82
+ * Sign VC with Ed25519 (Ed25519Signature2020)
83
+ *
84
+ * Creates an Ed25519Signature2020 proof for the VC.
85
+ * Uses the same signing pattern as proof generation.
86
+ *
87
+ * @param vc - The unsigned VC
88
+ * @param canonicalVC - The canonical representation for signing
89
+ * @returns Proof object
90
+ */
91
+ private signVC;
92
+ /**
93
+ * Format base64 private key as PKCS#8 PEM for JOSE library
94
+ *
95
+ * Same as proof generator format.
96
+ */
97
+ private formatPrivateKeyAsPEM;
98
+ /**
99
+ * Get issuer DID
100
+ *
101
+ * @returns The DID of this issuer
102
+ */
103
+ getIssuerDid(): string;
104
+ /**
105
+ * Get issuer key ID
106
+ *
107
+ * @returns The key ID of this issuer
108
+ */
109
+ getIssuerKeyId(): string;
110
+ }
111
+ /**
112
+ * Create a delegation credential issuer from identity
113
+ *
114
+ * Convenience factory function.
115
+ *
116
+ * @param identity - Agent identity
117
+ * @returns DelegationCredentialIssuer instance
118
+ */
119
+ export declare function createDelegationIssuer(identity: AgentIdentity): DelegationCredentialIssuer;
@@ -0,0 +1,220 @@
1
+ "use strict";
2
+ /**
3
+ * Delegation Credential Issuer
4
+ *
5
+ * Issues W3C Verifiable Credentials for delegations with Ed25519 signatures.
6
+ * Follows the Python POC design (Delegation-Service.md:136-163) where
7
+ * delegations are issued AS W3C VCs.
8
+ *
9
+ * Related Spec: MCP-I §4.1, §4.2, W3C VC Data Model 1.1
10
+ * Python Reference: Delegation-Service.md
11
+ */
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ exports.DelegationCredentialIssuer = void 0;
14
+ exports.createDelegationIssuer = createDelegationIssuer;
15
+ const jose_1 = require("jose");
16
+ const json_canonicalize_1 = require("json-canonicalize");
17
+ const crypto_1 = require("crypto");
18
+ const delegation_1 = require("@kya-os/contracts/delegation");
19
+ /**
20
+ * Delegation Credential Issuer
21
+ *
22
+ * Issues W3C Verifiable Credentials for delegations.
23
+ * Per Python POC (Delegation-Service.md:136-146):
24
+ * - Every delegation MUST be issued as a VC
25
+ * - VC is signed with Ed25519 (Ed25519Signature2020)
26
+ * - StatusList2021 support for efficient revocation
27
+ */
28
+ class DelegationCredentialIssuer {
29
+ identity;
30
+ constructor(identity) {
31
+ this.identity = identity;
32
+ }
33
+ /**
34
+ * Issue a delegation credential
35
+ *
36
+ * Creates a W3C Verifiable Credential from a delegation record.
37
+ * Signs it with Ed25519 and returns the complete DelegationCredential.
38
+ *
39
+ * @param delegation - The delegation record to issue as a VC
40
+ * @param options - Issuance options
41
+ * @returns Signed DelegationCredential
42
+ */
43
+ async issueDelegationCredential(delegation, options = {}) {
44
+ // Step 1: Create unsigned VC
45
+ let unsignedVC = (0, delegation_1.wrapDelegationAsVC)(delegation, {
46
+ id: options.id,
47
+ issuanceDate: options.issuanceDate,
48
+ expirationDate: options.expirationDate,
49
+ credentialStatus: options.credentialStatus,
50
+ });
51
+ // Add additional contexts if provided
52
+ if (options.additionalContexts && options.additionalContexts.length > 0) {
53
+ const existingContexts = unsignedVC['@context'];
54
+ unsignedVC = {
55
+ ...unsignedVC,
56
+ '@context': [...existingContexts, ...options.additionalContexts],
57
+ };
58
+ }
59
+ // Step 2: Canonicalize VC (for signing)
60
+ const canonicalVC = this.canonicalizeVC(unsignedVC);
61
+ // Step 3: Sign with Ed25519
62
+ const proof = await this.signVC(unsignedVC, canonicalVC);
63
+ // Step 4: Return signed VC
64
+ return {
65
+ ...unsignedVC,
66
+ proof,
67
+ };
68
+ }
69
+ /**
70
+ * Create a delegation record and issue it as a VC in one step
71
+ *
72
+ * Convenience method for creating a new delegation from scratch.
73
+ *
74
+ * @param params - Delegation parameters
75
+ * @param options - Issuance options
76
+ * @returns Signed DelegationCredential
77
+ */
78
+ async createAndIssueDelegation(params, options = {}) {
79
+ const now = Date.now();
80
+ // Create delegation record
81
+ const delegation = {
82
+ id: params.id,
83
+ issuerDid: params.issuerDid,
84
+ subjectDid: params.subjectDid,
85
+ controller: params.controller,
86
+ vcId: options.id || `urn:uuid:${params.id}`,
87
+ parentId: params.parentId,
88
+ constraints: params.constraints,
89
+ signature: '', // Will be filled by VC proof
90
+ status: params.status || 'active',
91
+ createdAt: now,
92
+ metadata: params.metadata,
93
+ };
94
+ // Issue as VC
95
+ return this.issueDelegationCredential(delegation, options);
96
+ }
97
+ /**
98
+ * Canonicalize VC for signing
99
+ *
100
+ * Uses JCS (JSON Canonicalization Scheme, RFC 8785) to create
101
+ * a deterministic representation of the VC.
102
+ *
103
+ * @param vc - The unsigned VC
104
+ * @returns Canonical JSON string
105
+ */
106
+ canonicalizeVC(vc) {
107
+ // Per W3C VC spec, we canonicalize the VC without the proof
108
+ return (0, json_canonicalize_1.canonicalize)(vc);
109
+ }
110
+ /**
111
+ * Sign VC with Ed25519 (Ed25519Signature2020)
112
+ *
113
+ * Creates an Ed25519Signature2020 proof for the VC.
114
+ * Uses the same signing pattern as proof generation.
115
+ *
116
+ * @param vc - The unsigned VC
117
+ * @param canonicalVC - The canonical representation for signing
118
+ * @returns Proof object
119
+ */
120
+ async signVC(vc, canonicalVC) {
121
+ try {
122
+ // Import private key
123
+ const privateKeyPem = this.formatPrivateKeyAsPEM(this.identity.privateKey);
124
+ const privateKey = await (0, jose_1.importPKCS8)(privateKeyPem, 'EdDSA');
125
+ // Create verification method URI
126
+ const verificationMethod = `${this.identity.did}#${this.identity.kid}`;
127
+ // Create signing data (hash of canonical VC)
128
+ const dataToSign = (0, crypto_1.createHash)('sha256')
129
+ .update(canonicalVC, 'utf8')
130
+ .digest();
131
+ // Sign using jose (creates a detached JWS)
132
+ // We'll create a compact JWS and extract the signature
133
+ const jwt = await new jose_1.SignJWT({ digest: dataToSign.toString('base64') })
134
+ .setProtectedHeader({
135
+ alg: 'EdDSA',
136
+ typ: 'VC',
137
+ })
138
+ .setIssuedAt()
139
+ .sign(privateKey);
140
+ // Extract signature from JWT (third part of compact JWS)
141
+ const parts = jwt.split('.');
142
+ const signatureBase64url = parts[2];
143
+ // Create Ed25519Signature2020 proof
144
+ const proof = {
145
+ type: 'Ed25519Signature2020',
146
+ created: new Date().toISOString(),
147
+ verificationMethod,
148
+ proofPurpose: 'assertionMethod',
149
+ proofValue: signatureBase64url,
150
+ };
151
+ return proof;
152
+ }
153
+ catch (error) {
154
+ throw new Error(`Failed to sign delegation credential: ${error instanceof Error ? error.message : 'Unknown error'}`);
155
+ }
156
+ }
157
+ /**
158
+ * Format base64 private key as PKCS#8 PEM for JOSE library
159
+ *
160
+ * Same as proof generator format.
161
+ */
162
+ formatPrivateKeyAsPEM(base64PrivateKey) {
163
+ const keyData = Buffer.from(base64PrivateKey, 'base64');
164
+ // Ed25519 PKCS#8 header and footer
165
+ const header = '-----BEGIN PRIVATE KEY-----\n';
166
+ const footer = '\n-----END PRIVATE KEY-----';
167
+ // Wrap Ed25519 raw key in PKCS#8 structure (ASN.1 encoding)
168
+ const pkcs8Header = Buffer.from([
169
+ 0x30,
170
+ 0x2e, // SEQUENCE, length 46
171
+ 0x02,
172
+ 0x01,
173
+ 0x00, // INTEGER version 0
174
+ 0x30,
175
+ 0x05, // SEQUENCE, length 5
176
+ 0x06,
177
+ 0x03,
178
+ 0x2b,
179
+ 0x65,
180
+ 0x70, // OID for Ed25519
181
+ 0x04,
182
+ 0x22, // OCTET STRING, length 34
183
+ 0x04,
184
+ 0x20, // OCTET STRING, length 32 (the actual key)
185
+ ]);
186
+ const fullKey = Buffer.concat([pkcs8Header, keyData.subarray(0, 32)]);
187
+ const base64Key = fullKey.toString('base64');
188
+ // Format as PEM with line breaks every 64 characters
189
+ const formattedKey = base64Key.match(/.{1,64}/g)?.join('\n') || base64Key;
190
+ return header + formattedKey + footer;
191
+ }
192
+ /**
193
+ * Get issuer DID
194
+ *
195
+ * @returns The DID of this issuer
196
+ */
197
+ getIssuerDid() {
198
+ return this.identity.did;
199
+ }
200
+ /**
201
+ * Get issuer key ID
202
+ *
203
+ * @returns The key ID of this issuer
204
+ */
205
+ getIssuerKeyId() {
206
+ return this.identity.kid;
207
+ }
208
+ }
209
+ exports.DelegationCredentialIssuer = DelegationCredentialIssuer;
210
+ /**
211
+ * Create a delegation credential issuer from identity
212
+ *
213
+ * Convenience factory function.
214
+ *
215
+ * @param identity - Agent identity
216
+ * @returns DelegationCredentialIssuer instance
217
+ */
218
+ function createDelegationIssuer(identity) {
219
+ return new DelegationCredentialIssuer(identity);
220
+ }
@@ -0,0 +1,193 @@
1
+ /**
2
+ * Delegation Credential Verifier
3
+ *
4
+ * Progressive enhancement verification for W3C Delegation Credentials.
5
+ * Follows the Edge-Delegation-Verification.md pattern:
6
+ *
7
+ * Stage 1: Fast basic checks (no network, early rejection)
8
+ * Stage 2: Parallel advanced checks (signature, status)
9
+ * Stage 3: Combined results
10
+ *
11
+ * Related Spec: MCP-I §4.3, W3C VC Data Model 1.1
12
+ * Python Reference: Edge-Delegation-Verification.md
13
+ */
14
+ import { JWK } from 'jose';
15
+ import { DelegationCredential } from '@kya-os/contracts/delegation';
16
+ import { CredentialStatus } from '@kya-os/contracts/vc';
17
+ /**
18
+ * Verification result for delegation credentials
19
+ */
20
+ export interface DelegationVCVerificationResult {
21
+ /** Whether the delegation credential is valid */
22
+ valid: boolean;
23
+ /** Reason for invalid result (if valid=false) */
24
+ reason?: string;
25
+ /** Stage at which verification completed */
26
+ stage: 'basic' | 'signature' | 'status' | 'complete';
27
+ /** Whether result came from cache */
28
+ cached?: boolean;
29
+ /** Performance metrics */
30
+ metrics?: {
31
+ basicCheckMs?: number;
32
+ signatureCheckMs?: number;
33
+ statusCheckMs?: number;
34
+ totalMs: number;
35
+ };
36
+ /** Details about what was checked */
37
+ checks?: {
38
+ basicValid?: boolean;
39
+ signatureValid?: boolean;
40
+ statusValid?: boolean;
41
+ };
42
+ }
43
+ /**
44
+ * Options for verification
45
+ */
46
+ export interface VerifyDelegationVCOptions {
47
+ /** Skip cache and force fresh verification */
48
+ skipCache?: boolean;
49
+ /** Skip signature verification (faster, less secure) */
50
+ skipSignature?: boolean;
51
+ /** Skip status checking (faster, may miss revocations) */
52
+ skipStatus?: boolean;
53
+ /** DID resolver for fetching public keys */
54
+ didResolver?: DIDResolver;
55
+ /** Status list resolver for checking revocation */
56
+ statusListResolver?: StatusListResolver;
57
+ }
58
+ /**
59
+ * DID Resolver interface
60
+ */
61
+ export interface DIDResolver {
62
+ /**
63
+ * Resolve a DID to get the DID Document
64
+ * @param did - The DID to resolve
65
+ * @returns DID Document with verification methods
66
+ */
67
+ resolve(did: string): Promise<DIDDocument | null>;
68
+ }
69
+ /**
70
+ * DID Document (simplified)
71
+ */
72
+ export interface DIDDocument {
73
+ id: string;
74
+ verificationMethod?: VerificationMethod[];
75
+ authentication?: (string | VerificationMethod)[];
76
+ assertionMethod?: (string | VerificationMethod)[];
77
+ }
78
+ /**
79
+ * Verification Method
80
+ */
81
+ export interface VerificationMethod {
82
+ id: string;
83
+ type: string;
84
+ controller: string;
85
+ publicKeyJwk?: JWK;
86
+ publicKeyBase58?: string;
87
+ publicKeyMultibase?: string;
88
+ }
89
+ /**
90
+ * Status List Resolver interface
91
+ */
92
+ export interface StatusListResolver {
93
+ /**
94
+ * Check if a credential is revoked via StatusList2021
95
+ * @param status - The credential status entry
96
+ * @returns true if revoked, false otherwise
97
+ */
98
+ checkStatus(status: CredentialStatus): Promise<boolean>;
99
+ }
100
+ /**
101
+ * Delegation Credential Verifier
102
+ *
103
+ * Implements progressive enhancement pattern from Edge-Delegation-Verification.md:
104
+ * 1. Fast basic checks (no network) - early rejection
105
+ * 2. Parallel advanced checks (signature + status)
106
+ * 3. Combined results
107
+ */
108
+ export declare class DelegationCredentialVerifier {
109
+ private didResolver?;
110
+ private statusListResolver?;
111
+ private cache;
112
+ private cacheTtl;
113
+ constructor(options?: {
114
+ didResolver?: DIDResolver;
115
+ statusListResolver?: StatusListResolver;
116
+ cacheTtl?: number;
117
+ });
118
+ /**
119
+ * Verify a delegation credential with progressive enhancement
120
+ *
121
+ * Per Edge-Delegation-Verification.md:41-102
122
+ *
123
+ * @param vc - The delegation credential to verify
124
+ * @param options - Verification options
125
+ * @returns Verification result
126
+ */
127
+ verifyDelegationCredential(vc: DelegationCredential, options?: VerifyDelegationVCOptions): Promise<DelegationVCVerificationResult>;
128
+ /**
129
+ * Stage 1: Validate basic properties (no network calls)
130
+ *
131
+ * Fast path for early rejection of invalid delegations.
132
+ * Per Edge-Delegation-Verification.md:155-186
133
+ *
134
+ * @param vc - The delegation credential
135
+ * @returns Validation result
136
+ */
137
+ private validateBasicProperties;
138
+ /**
139
+ * Stage 2a: Verify signature
140
+ *
141
+ * Per Edge-Delegation-Verification.md:191-234
142
+ *
143
+ * @param vc - The delegation credential
144
+ * @param didResolver - Optional DID resolver
145
+ * @returns Verification result
146
+ */
147
+ private verifySignature;
148
+ /**
149
+ * Stage 2b: Check credential status via StatusList2021
150
+ *
151
+ * @param status - The credential status entry
152
+ * @param statusListResolver - Optional status list resolver
153
+ * @returns Status check result
154
+ */
155
+ private checkCredentialStatus;
156
+ /**
157
+ * Find verification method in DID document
158
+ *
159
+ * @param didDoc - The DID document
160
+ * @param verificationMethodId - The verification method ID
161
+ * @returns Verification method or undefined
162
+ */
163
+ private findVerificationMethod;
164
+ /**
165
+ * Get from cache
166
+ */
167
+ private getFromCache;
168
+ /**
169
+ * Set in cache
170
+ */
171
+ private setInCache;
172
+ /**
173
+ * Clear cache
174
+ */
175
+ clearCache(): void;
176
+ /**
177
+ * Clear cache entry for specific VC
178
+ */
179
+ clearCacheEntry(id: string): void;
180
+ }
181
+ /**
182
+ * Create a delegation credential verifier
183
+ *
184
+ * Convenience factory function.
185
+ *
186
+ * @param options - Verifier options
187
+ * @returns DelegationCredentialVerifier instance
188
+ */
189
+ export declare function createDelegationVerifier(options?: {
190
+ didResolver?: DIDResolver;
191
+ statusListResolver?: StatusListResolver;
192
+ cacheTtl?: number;
193
+ }): DelegationCredentialVerifier;