@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
@@ -9,7 +9,7 @@
9
9
  */
10
10
  export interface AgentIdentity {
11
11
  did: string;
12
- keyId: string;
12
+ kid: string;
13
13
  privateKey: string;
14
14
  publicKey: string;
15
15
  createdAt: string;
@@ -21,7 +21,7 @@ export interface AgentIdentity {
21
21
  export interface DevIdentityFile {
22
22
  version: string;
23
23
  did: string;
24
- keyId: string;
24
+ kid: string;
25
25
  privateKey: string;
26
26
  publicKey: string;
27
27
  createdAt: string;
@@ -74,6 +74,14 @@ export declare class IdentityManager {
74
74
  * Requirements: 4.1, 4.4
75
75
  */
76
76
  private generateDevIdentity;
77
+ /**
78
+ * Generate multibase-encoded key identifier (z-prefix base58btc)
79
+ */
80
+ private generateMultibaseKid;
81
+ /**
82
+ * Simple base58 encoding (matching well-known.ts implementation)
83
+ */
84
+ private encodeBase58;
77
85
  /**
78
86
  * Save development identity to .mcpi/identity.json
79
87
  */
@@ -73,9 +73,31 @@ class IdentityManager {
73
73
  if ((0, fs_1.existsSync)(identityPath)) {
74
74
  const content = await (0, promises_1.readFile)(identityPath, "utf-8");
75
75
  const devIdentity = JSON.parse(content);
76
+ // Handle backward compatibility: support both 'kid' and old 'keyId' format
77
+ let kid = devIdentity.kid || devIdentity.keyId;
78
+ // If we have old keyId format, migrate to multibase format
79
+ if (devIdentity.keyId && !devIdentity.kid) {
80
+ // Check if it's the old format (key-[hex])
81
+ if (kid.startsWith('key-')) {
82
+ // Generate new multibase kid from public key
83
+ kid = this.generateMultibaseKid(devIdentity.publicKey);
84
+ // Save migrated identity
85
+ const migratedIdentity = {
86
+ did: devIdentity.did,
87
+ kid,
88
+ privateKey: devIdentity.privateKey,
89
+ publicKey: devIdentity.publicKey,
90
+ createdAt: devIdentity.createdAt,
91
+ lastRotated: devIdentity.lastRotated,
92
+ };
93
+ await this.saveDevIdentity(migratedIdentity);
94
+ console.error(`✅ Migrated identity to new multibase kid format: ${kid}`);
95
+ return migratedIdentity;
96
+ }
97
+ }
76
98
  return {
77
99
  did: devIdentity.did,
78
- keyId: devIdentity.keyId,
100
+ kid,
79
101
  privateKey: devIdentity.privateKey,
80
102
  publicKey: devIdentity.publicKey,
81
103
  createdAt: devIdentity.createdAt,
@@ -83,9 +105,9 @@ class IdentityManager {
83
105
  };
84
106
  }
85
107
  }
86
- catch {
108
+ catch (error) {
87
109
  // If file exists but is corrupted, we'll regenerate
88
- console.warn(`Warning: Could not load identity from ${identityPath}, generating new one`);
110
+ console.warn(`Warning: Could not load identity from ${identityPath}, generating new one`, error instanceof Error ? error.message : error);
89
111
  }
90
112
  // Generate new identity
91
113
  return await this.generateDevIdentity();
@@ -104,14 +126,16 @@ class IdentityManager {
104
126
  }
105
127
  const privateKey = Buffer.from(privateKeyJwk.d, "base64url").toString("base64");
106
128
  const publicKey = Buffer.from(privateKeyJwk.x, "base64url").toString("base64");
107
- // Generate key ID (first 8 chars of public key hash)
108
- const keyId = `key-${(0, crypto_1.createHash)("sha256").update(publicKey).digest("hex").substring(0, 8)}`;
129
+ // Generate multibase-encoded key ID
130
+ const kid = this.generateMultibaseKid(publicKey);
109
131
  // Generate DID (for dev, use localhost)
110
- const did = `did:web:localhost:3000:agents:${keyId}`;
132
+ // Extract a short identifier for the DID path (first 8 chars of hash for readability)
133
+ const shortId = (0, crypto_1.createHash)("sha256").update(publicKey).digest("hex").substring(0, 8);
134
+ const did = `did:web:localhost:3000:agents:${shortId}`;
111
135
  const now = new Date().toISOString();
112
136
  const identity = {
113
137
  did,
114
- keyId,
138
+ kid: kid, // Using kid but keeping field name for now for compatibility
115
139
  privateKey,
116
140
  publicKey,
117
141
  createdAt: now,
@@ -120,6 +144,38 @@ class IdentityManager {
120
144
  await this.saveDevIdentity(identity);
121
145
  return identity;
122
146
  }
147
+ /**
148
+ * Generate multibase-encoded key identifier (z-prefix base58btc)
149
+ */
150
+ generateMultibaseKid(base64PublicKey) {
151
+ const publicKeyBytes = Buffer.from(base64PublicKey, "base64");
152
+ // Ed25519 public key prefix (0xed01) + key bytes
153
+ const prefixedKey = Buffer.concat([
154
+ Buffer.from([0xed, 0x01]), // Ed25519 multicodec prefix
155
+ publicKeyBytes,
156
+ ]);
157
+ // Convert to base58btc
158
+ const base58 = this.encodeBase58(prefixedKey);
159
+ return `z${base58}`; // 'z' prefix indicates base58btc
160
+ }
161
+ /**
162
+ * Simple base58 encoding (matching well-known.ts implementation)
163
+ */
164
+ encodeBase58(buffer) {
165
+ const alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
166
+ let num = BigInt("0x" + buffer.toString("hex"));
167
+ let result = "";
168
+ while (num > 0n) {
169
+ const remainder = num % 58n;
170
+ result = alphabet[Number(remainder)] + result;
171
+ num = num / 58n;
172
+ }
173
+ // Handle leading zeros
174
+ for (let i = 0; i < buffer.length && buffer[i] === 0; i++) {
175
+ result = "1" + result;
176
+ }
177
+ return result;
178
+ }
123
179
  /**
124
180
  * Save development identity to .mcpi/identity.json
125
181
  */
@@ -127,10 +183,11 @@ class IdentityManager {
127
183
  const identityPath = this.config.devIdentityPath;
128
184
  // Ensure directory exists
129
185
  await (0, promises_1.mkdir)((0, path_1.dirname)(identityPath), { recursive: true });
186
+ // Use 'kid' in the saved file (conforming to new schema)
130
187
  const devIdentity = {
131
188
  version: "1.0",
132
189
  did: identity.did,
133
- keyId: identity.keyId,
190
+ kid: identity.kid, // Save as 'kid' in file
134
191
  privateKey: identity.privateKey,
135
192
  publicKey: identity.publicKey,
136
193
  createdAt: identity.createdAt,
@@ -141,7 +198,7 @@ class IdentityManager {
141
198
  });
142
199
  console.error(`✅ Identity saved to ${identityPath}`);
143
200
  console.error(` DID: ${identity.did}`);
144
- console.error(` Key ID: ${identity.keyId}`);
201
+ console.error(` Key ID: ${identity.kid}`);
145
202
  }
146
203
  /**
147
204
  * Load production identity from environment variables
@@ -185,7 +242,7 @@ class IdentityManager {
185
242
  .digest("base64");
186
243
  return {
187
244
  did: env.AGENT_DID,
188
- keyId: env.AGENT_KEY_ID,
245
+ kid: env.AGENT_KEY_ID,
189
246
  privateKey: env.AGENT_PRIVATE_KEY,
190
247
  publicKey,
191
248
  createdAt: new Date().toISOString(), // We don't have creation time in prod
@@ -198,7 +255,7 @@ class IdentityManager {
198
255
  try {
199
256
  // Basic validation
200
257
  if (!identity.did ||
201
- !identity.keyId ||
258
+ !identity.kid ||
202
259
  !identity.privateKey ||
203
260
  !identity.publicKey) {
204
261
  return false;
@@ -163,7 +163,7 @@ export declare class MCPIRuntime {
163
163
  getStats(): {
164
164
  identity: {
165
165
  did: string | undefined;
166
- keyId: string | undefined;
166
+ kid: string | undefined;
167
167
  environment: "development" | "production";
168
168
  };
169
169
  session: {
@@ -179,6 +179,9 @@ export declare class MCPIRuntime {
179
179
  enabled: boolean;
180
180
  sessionsLogged: number;
181
181
  includePayloads: boolean;
182
+ totalRecordsLogged: number;
183
+ currentLogSize: number;
184
+ lastRotationTime: number;
182
185
  };
183
186
  runtime: {
184
187
  initialized: boolean;
@@ -93,7 +93,7 @@ class MCPIRuntime {
93
93
  });
94
94
  console.error(`✅ XMCP-I Runtime initialized`);
95
95
  console.error(` DID: ${this.cachedIdentity.did}`);
96
- console.error(` Key ID: ${this.cachedIdentity.keyId}`);
96
+ console.error(` Key ID: ${this.cachedIdentity.kid}`);
97
97
  // Show verify link in development (default true)
98
98
  const showVerifyLink = this.config.runtime?.showVerifyLink !== false;
99
99
  demo_1.DemoConsole.printVerifyLink(showVerifyLink, this.config.identity?.environment || "development");
@@ -281,7 +281,7 @@ class MCPIRuntime {
281
281
  return {
282
282
  identity: {
283
283
  did: this.cachedIdentity?.did,
284
- keyId: this.cachedIdentity?.keyId,
284
+ kid: this.cachedIdentity?.kid,
285
285
  environment: this.config.identity?.environment || "development",
286
286
  },
287
287
  session: this.sessionManager.getStats(),
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Identity Migration Utility
3
+ *
4
+ * Migrates old identity files from keyId format to kid multibase format
5
+ */
6
+ /**
7
+ * Migrate an identity file from old keyId format to new kid format
8
+ * @param identityPath Path to the identity file
9
+ * @returns true if migration was performed, false if already migrated
10
+ */
11
+ export declare function migrateIdentityFile(identityPath: string): Promise<boolean>;
12
+ /**
13
+ * Generate multibase-encoded key identifier (z-prefix base58btc)
14
+ */
15
+ declare function generateMultibaseKid(base64PublicKey: string): string;
16
+ export { generateMultibaseKid };
@@ -0,0 +1,118 @@
1
+ "use strict";
2
+ /**
3
+ * Identity Migration Utility
4
+ *
5
+ * Migrates old identity files from keyId format to kid multibase format
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.migrateIdentityFile = migrateIdentityFile;
9
+ exports.generateMultibaseKid = generateMultibaseKid;
10
+ const promises_1 = require("fs/promises");
11
+ const fs_1 = require("fs");
12
+ /**
13
+ * Migrate an identity file from old keyId format to new kid format
14
+ * @param identityPath Path to the identity file
15
+ * @returns true if migration was performed, false if already migrated
16
+ */
17
+ async function migrateIdentityFile(identityPath) {
18
+ if (!(0, fs_1.existsSync)(identityPath)) {
19
+ throw new Error(`Identity file not found: ${identityPath}`);
20
+ }
21
+ const content = await (0, promises_1.readFile)(identityPath, "utf-8");
22
+ const identity = JSON.parse(content);
23
+ // Check if already migrated (has kid field)
24
+ if (identity.kid) {
25
+ console.log("Identity file already migrated to kid format");
26
+ return false;
27
+ }
28
+ // Check if has old format keyId
29
+ if (!identity.keyId) {
30
+ throw new Error("Identity file has neither kid nor keyId field");
31
+ }
32
+ // Check if it's the old format (key-[hex])
33
+ if (identity.keyId.startsWith('key-')) {
34
+ // Generate multibase kid from public key
35
+ const kid = generateMultibaseKid(identity.publicKey);
36
+ // Create migrated identity
37
+ const migratedIdentity = {
38
+ version: identity.version || "1.0",
39
+ did: identity.did,
40
+ kid, // New field
41
+ privateKey: identity.privateKey,
42
+ publicKey: identity.publicKey,
43
+ createdAt: identity.createdAt,
44
+ lastRotated: identity.lastRotated || new Date().toISOString(),
45
+ };
46
+ // Save migrated identity
47
+ await (0, promises_1.writeFile)(identityPath, JSON.stringify(migratedIdentity, null, 2), {
48
+ mode: 0o600,
49
+ });
50
+ console.log(`✅ Migrated identity file to new multibase kid format`);
51
+ console.log(` Old keyId: ${identity.keyId}`);
52
+ console.log(` New kid: ${kid}`);
53
+ return true;
54
+ }
55
+ // Already in multibase format, just rename field
56
+ const renamedIdentity = {
57
+ version: identity.version || "1.0",
58
+ did: identity.did,
59
+ kid: identity.keyId, // Rename field from keyId to kid
60
+ privateKey: identity.privateKey,
61
+ publicKey: identity.publicKey,
62
+ createdAt: identity.createdAt,
63
+ lastRotated: identity.lastRotated,
64
+ };
65
+ await (0, promises_1.writeFile)(identityPath, JSON.stringify(renamedIdentity, null, 2), {
66
+ mode: 0o600,
67
+ });
68
+ console.log(`✅ Renamed keyId field to kid`);
69
+ return true;
70
+ }
71
+ /**
72
+ * Generate multibase-encoded key identifier (z-prefix base58btc)
73
+ */
74
+ function generateMultibaseKid(base64PublicKey) {
75
+ const publicKeyBytes = Buffer.from(base64PublicKey, "base64");
76
+ // Ed25519 public key prefix (0xed01) + key bytes
77
+ const prefixedKey = Buffer.concat([
78
+ Buffer.from([0xed, 0x01]), // Ed25519 multicodec prefix
79
+ publicKeyBytes,
80
+ ]);
81
+ // Convert to base58btc
82
+ const base58 = encodeBase58(prefixedKey);
83
+ return `z${base58}`; // 'z' prefix indicates base58btc
84
+ }
85
+ /**
86
+ * Simple base58 encoding
87
+ */
88
+ function encodeBase58(buffer) {
89
+ const alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
90
+ let num = BigInt("0x" + buffer.toString("hex"));
91
+ let result = "";
92
+ while (num > 0n) {
93
+ const remainder = num % 58n;
94
+ result = alphabet[Number(remainder)] + result;
95
+ num = num / 58n;
96
+ }
97
+ // Handle leading zeros
98
+ for (let i = 0; i < buffer.length && buffer[i] === 0; i++) {
99
+ result = "1" + result;
100
+ }
101
+ return result;
102
+ }
103
+ // CLI usage
104
+ if (require.main === module) {
105
+ const args = process.argv.slice(2);
106
+ if (args.length !== 1) {
107
+ console.error("Usage: ts-node migrate-identity.ts <path-to-identity.json>");
108
+ process.exit(1);
109
+ }
110
+ migrateIdentityFile(args[0])
111
+ .then((migrated) => {
112
+ process.exit(migrated ? 0 : 1);
113
+ })
114
+ .catch((error) => {
115
+ console.error("Migration failed:", error.message);
116
+ process.exit(1);
117
+ });
118
+ }
@@ -30,7 +30,7 @@ class ProofGenerator {
30
30
  // Create proof metadata
31
31
  const meta = {
32
32
  did: this.identity.did,
33
- kid: this.identity.keyId,
33
+ kid: this.identity.kid,
34
34
  ts: Math.floor(Date.now() / 1000),
35
35
  nonce: session.nonce,
36
36
  audience: session.audience,
@@ -118,7 +118,7 @@ class ProofGenerator {
118
118
  const jwt = await new jose_1.SignJWT(payload)
119
119
  .setProtectedHeader({
120
120
  alg: "EdDSA",
121
- kid: this.identity.keyId,
121
+ kid: this.identity.kid,
122
122
  })
123
123
  .sign(privateKey);
124
124
  // Return full compact JWS (NOT detached)