@kya-os/mcp-i 0.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.
Files changed (229) hide show
  1. package/README.md +406 -71
  2. package/dist/149.js +1 -0
  3. package/dist/189.js +1 -0
  4. package/dist/261.js +1 -0
  5. package/dist/28.js +1 -0
  6. package/dist/295.js +1 -0
  7. package/dist/460.js +1 -0
  8. package/dist/570.js +1 -0
  9. package/dist/634.js +1 -0
  10. package/dist/647.js +1 -0
  11. package/dist/67.js +1 -0
  12. package/dist/739.js +1 -0
  13. package/dist/742.js +1 -0
  14. package/dist/904.js +1 -0
  15. package/dist/938.js +1 -0
  16. package/dist/auth/api-key.d.ts +16 -0
  17. package/dist/auth/api-key.js +82 -0
  18. package/dist/auth/jwt.d.ts +43 -0
  19. package/dist/auth/jwt.js +51 -0
  20. package/dist/auth/oauth/factory.d.ts +12 -0
  21. package/dist/auth/oauth/factory.js +36 -0
  22. package/dist/auth/oauth/index.d.ts +5 -0
  23. package/dist/auth/oauth/index.js +27 -0
  24. package/dist/auth/oauth/providers/proxy-provider.d.ts +13 -0
  25. package/dist/auth/oauth/providers/proxy-provider.js +159 -0
  26. package/dist/auth/oauth/router.d.ts +4 -0
  27. package/dist/auth/oauth/router.js +294 -0
  28. package/dist/auth/oauth/storage/memory-storage.d.ts +12 -0
  29. package/dist/auth/oauth/storage/memory-storage.js +40 -0
  30. package/dist/auth/oauth/types.d.ts +112 -0
  31. package/dist/auth/oauth/types.js +2 -0
  32. package/dist/cache/__tests__/cloudflare-kv-nonce-cache.test.d.ts +4 -0
  33. package/dist/cache/__tests__/cloudflare-kv-nonce-cache.test.js +176 -0
  34. package/dist/cache/__tests__/concurrency.test.d.ts +5 -0
  35. package/dist/cache/__tests__/concurrency.test.js +300 -0
  36. package/dist/cache/__tests__/dynamodb-nonce-cache.test.d.ts +4 -0
  37. package/dist/cache/__tests__/dynamodb-nonce-cache.test.js +176 -0
  38. package/dist/cache/__tests__/memory-nonce-cache.test.d.ts +4 -0
  39. package/dist/cache/__tests__/memory-nonce-cache.test.js +132 -0
  40. package/dist/cache/__tests__/nonce-cache-factory-simple.test.d.ts +4 -0
  41. package/dist/cache/__tests__/nonce-cache-factory-simple.test.js +133 -0
  42. package/dist/cache/__tests__/nonce-cache-factory.test.d.ts +4 -0
  43. package/dist/cache/__tests__/nonce-cache-factory.test.js +252 -0
  44. package/dist/cache/__tests__/redis-nonce-cache.test.d.ts +4 -0
  45. package/dist/cache/__tests__/redis-nonce-cache.test.js +95 -0
  46. package/dist/cache/cloudflare-kv-nonce-cache.d.ts +14 -0
  47. package/dist/cache/cloudflare-kv-nonce-cache.js +93 -0
  48. package/dist/cache/dynamodb-nonce-cache.d.ts +15 -0
  49. package/dist/cache/dynamodb-nonce-cache.js +92 -0
  50. package/dist/cache/index.d.ts +16 -0
  51. package/dist/cache/index.js +32 -0
  52. package/dist/cache/memory-nonce-cache.d.ts +44 -0
  53. package/dist/cache/memory-nonce-cache.js +105 -0
  54. package/dist/cache/nonce-cache-factory.d.ts +20 -0
  55. package/dist/cache/nonce-cache-factory.js +208 -0
  56. package/dist/cache/redis-nonce-cache.d.ts +14 -0
  57. package/dist/cache/redis-nonce-cache.js +53 -0
  58. package/dist/compiler/compiler-context.d.ts +23 -0
  59. package/dist/compiler/compiler-context.js +24 -0
  60. package/dist/compiler/config/constants.d.ts +41 -0
  61. package/dist/compiler/config/constants.js +45 -0
  62. package/dist/compiler/config/index.d.ts +252 -0
  63. package/dist/compiler/config/index.js +15 -0
  64. package/dist/compiler/config/injection.d.ts +26 -0
  65. package/dist/compiler/config/injection.js +58 -0
  66. package/dist/compiler/config/schemas/experimental/index.d.ts +91 -0
  67. package/dist/compiler/config/schemas/experimental/index.js +16 -0
  68. package/dist/compiler/config/schemas/experimental/oauth.d.ts +74 -0
  69. package/dist/compiler/config/schemas/experimental/oauth.js +25 -0
  70. package/dist/compiler/config/schemas/index.d.ts +6 -0
  71. package/dist/compiler/config/schemas/index.js +17 -0
  72. package/dist/compiler/config/schemas/paths.d.ts +9 -0
  73. package/dist/compiler/config/schemas/paths.js +12 -0
  74. package/dist/compiler/config/schemas/transport/http.d.ts +82 -0
  75. package/dist/compiler/config/schemas/transport/http.js +33 -0
  76. package/dist/compiler/config/schemas/transport/stdio.d.ts +9 -0
  77. package/dist/compiler/config/schemas/transport/stdio.js +15 -0
  78. package/dist/compiler/config/schemas/webpack.d.ts +3 -0
  79. package/dist/compiler/config/schemas/webpack.js +15 -0
  80. package/dist/compiler/config/types.d.ts +1 -0
  81. package/dist/compiler/config/types.js +2 -0
  82. package/dist/compiler/config/utils.d.ts +20 -0
  83. package/dist/compiler/config/utils.js +36 -0
  84. package/dist/compiler/generate-env-code.d.ts +1 -0
  85. package/dist/compiler/generate-env-code.js +8 -0
  86. package/dist/compiler/generate-import-code.d.ts +1 -0
  87. package/dist/compiler/generate-import-code.js +24 -0
  88. package/dist/compiler/get-webpack-config/get-entries.d.ts +3 -0
  89. package/dist/compiler/get-webpack-config/get-entries.js +29 -0
  90. package/dist/compiler/get-webpack-config/get-externals.d.ts +7 -0
  91. package/dist/compiler/get-webpack-config/get-externals.js +88 -0
  92. package/dist/compiler/get-webpack-config/get-injected-variables.d.ts +8 -0
  93. package/dist/compiler/get-webpack-config/get-injected-variables.js +25 -0
  94. package/dist/compiler/get-webpack-config/index.d.ts +4 -0
  95. package/dist/compiler/get-webpack-config/index.js +101 -0
  96. package/dist/compiler/get-webpack-config/plugins.d.ts +8 -0
  97. package/dist/compiler/get-webpack-config/plugins.js +132 -0
  98. package/dist/compiler/get-webpack-config/resolve-tsconfig-paths.d.ts +9 -0
  99. package/dist/compiler/get-webpack-config/resolve-tsconfig-paths.js +40 -0
  100. package/dist/compiler/index.d.ts +6 -0
  101. package/dist/compiler/index.js +194 -0
  102. package/dist/compiler/on-first-build.d.ts +3 -0
  103. package/dist/compiler/on-first-build.js +58 -0
  104. package/dist/compiler/parse-xmcp-config.d.ts +9 -0
  105. package/dist/compiler/parse-xmcp-config.js +155 -0
  106. package/dist/compiler/start-http-server.d.ts +1 -0
  107. package/dist/compiler/start-http-server.js +34 -0
  108. package/dist/index.d.ts +12 -54
  109. package/dist/index.js +22 -190
  110. package/dist/index.js.LICENSE.txt +49 -0
  111. package/dist/runtime/__tests__/audit.test.d.ts +4 -0
  112. package/dist/runtime/__tests__/audit.test.js +328 -0
  113. package/dist/runtime/__tests__/identity.test.d.ts +4 -0
  114. package/dist/runtime/__tests__/identity.test.js +164 -0
  115. package/dist/runtime/__tests__/mcpi-runtime.test.d.ts +4 -0
  116. package/dist/runtime/__tests__/mcpi-runtime.test.js +372 -0
  117. package/dist/runtime/__tests__/proof.test.d.ts +4 -0
  118. package/dist/runtime/__tests__/proof.test.js +302 -0
  119. package/dist/runtime/__tests__/session.test.d.ts +4 -0
  120. package/dist/runtime/__tests__/session.test.js +254 -0
  121. package/dist/runtime/__tests__/well-known.test.d.ts +4 -0
  122. package/dist/runtime/__tests__/well-known.test.js +312 -0
  123. package/dist/runtime/adapter-express.js +2 -0
  124. package/dist/runtime/adapter-express.js.LICENSE.txt +252 -0
  125. package/dist/runtime/adapter-nextjs.js +2 -0
  126. package/dist/runtime/adapter-nextjs.js.LICENSE.txt +53 -0
  127. package/dist/runtime/adapters/express/index.d.ts +2 -0
  128. package/dist/runtime/adapters/express/index.js +48 -0
  129. package/dist/runtime/adapters/nextjs/index.d.ts +8 -0
  130. package/dist/runtime/adapters/nextjs/index.js +18 -0
  131. package/dist/runtime/audit.d.ts +93 -0
  132. package/dist/runtime/audit.js +212 -0
  133. package/dist/runtime/debug.d.ts +118 -0
  134. package/dist/runtime/debug.js +612 -0
  135. package/dist/runtime/delegation-hooks.d.ts +85 -0
  136. package/dist/runtime/delegation-hooks.js +116 -0
  137. package/dist/runtime/demo.d.ts +71 -0
  138. package/dist/runtime/demo.js +135 -0
  139. package/dist/runtime/headers.d.ts +1 -0
  140. package/dist/runtime/headers.js +9 -0
  141. package/dist/runtime/http.js +2 -0
  142. package/dist/runtime/http.js.LICENSE.txt +252 -0
  143. package/dist/runtime/identity.d.ts +105 -0
  144. package/dist/runtime/identity.js +232 -0
  145. package/dist/runtime/index.d.ts +16 -0
  146. package/dist/runtime/index.js +56 -0
  147. package/dist/runtime/mcpi-runtime.d.ts +164 -0
  148. package/dist/runtime/mcpi-runtime.js +352 -0
  149. package/dist/runtime/proof.d.ts +87 -0
  150. package/dist/runtime/proof.js +223 -0
  151. package/dist/runtime/session.d.ts +88 -0
  152. package/dist/runtime/session.js +216 -0
  153. package/dist/runtime/stdio.js +2 -0
  154. package/dist/runtime/stdio.js.LICENSE.txt +1 -0
  155. package/dist/runtime/templates/home.d.ts +2 -0
  156. package/dist/runtime/templates/home.js +50 -0
  157. package/dist/runtime/transports/http/base-streamable-http.d.ts +25 -0
  158. package/dist/runtime/transports/http/base-streamable-http.js +16 -0
  159. package/dist/runtime/transports/http/http-context.d.ts +9 -0
  160. package/dist/runtime/transports/http/http-context.js +8 -0
  161. package/dist/runtime/transports/http/index.d.ts +1 -0
  162. package/dist/runtime/transports/http/index.js +55 -0
  163. package/dist/runtime/transports/http/setup-cors.d.ts +4 -0
  164. package/dist/runtime/transports/http/setup-cors.js +24 -0
  165. package/dist/runtime/transports/http/stateless-streamable-http.d.ts +39 -0
  166. package/dist/runtime/transports/http/stateless-streamable-http.js +331 -0
  167. package/dist/runtime/transports/stdio/index.d.ts +1 -0
  168. package/dist/runtime/transports/stdio/index.js +51 -0
  169. package/dist/runtime/utils/server.d.ts +42 -0
  170. package/dist/runtime/utils/server.js +39 -0
  171. package/dist/runtime/utils/tools.d.ts +8 -0
  172. package/dist/runtime/utils/tools.js +115 -0
  173. package/dist/runtime/verifier-middleware.d.ts +76 -0
  174. package/dist/runtime/verifier-middleware.js +322 -0
  175. package/dist/runtime/well-known.d.ts +151 -0
  176. package/dist/runtime/well-known.js +258 -0
  177. package/dist/storage/config.d.ts +28 -0
  178. package/dist/storage/config.js +79 -0
  179. package/dist/storage/delegation.d.ts +59 -0
  180. package/dist/storage/delegation.js +130 -0
  181. package/dist/storage/merkle-verifier.d.ts +84 -0
  182. package/dist/storage/merkle-verifier.js +261 -0
  183. package/dist/test/__tests__/nonce-cache-integration.test.d.ts +1 -0
  184. package/dist/test/__tests__/nonce-cache-integration.test.js +116 -0
  185. package/dist/test/__tests__/nonce-cache.test.d.ts +1 -0
  186. package/dist/test/__tests__/nonce-cache.test.js +122 -0
  187. package/dist/test/__tests__/runtime-integration.test.d.ts +4 -0
  188. package/dist/test/__tests__/runtime-integration.test.js +192 -0
  189. package/dist/test/__tests__/test-infrastructure.test.d.ts +4 -0
  190. package/dist/test/__tests__/test-infrastructure.test.js +178 -0
  191. package/dist/test/deterministic-keys.d.ts +31 -0
  192. package/dist/test/deterministic-keys.js +108 -0
  193. package/dist/test/examples/test-usage-example.d.ts +140 -0
  194. package/dist/test/examples/test-usage-example.js +175 -0
  195. package/dist/test/index.d.ts +11 -0
  196. package/dist/test/index.js +27 -0
  197. package/dist/test/local-verification.d.ts +28 -0
  198. package/dist/test/local-verification.js +342 -0
  199. package/dist/test/mock-identity-provider.d.ts +96 -0
  200. package/dist/test/mock-identity-provider.js +243 -0
  201. package/dist/test/runtime-integration.d.ts +63 -0
  202. package/dist/test/runtime-integration.js +140 -0
  203. package/dist/test/test-environment.d.ts +26 -0
  204. package/dist/test/test-environment.js +50 -0
  205. package/dist/types/declarations.d.ts +1 -0
  206. package/dist/types/declarations.js +6 -0
  207. package/dist/types/middleware.d.ts +2 -0
  208. package/dist/types/middleware.js +2 -0
  209. package/dist/types/tool.d.ts +80 -0
  210. package/dist/types/tool.js +2 -0
  211. package/dist/utils/cli-icons.d.ts +3 -0
  212. package/dist/utils/cli-icons.js +7 -0
  213. package/dist/utils/constants.d.ts +6 -0
  214. package/dist/utils/constants.js +13 -0
  215. package/dist/utils/context.d.ts +33 -0
  216. package/dist/utils/context.js +58 -0
  217. package/dist/utils/file-watcher.d.ts +19 -0
  218. package/dist/utils/file-watcher.js +49 -0
  219. package/dist/utils/fs-utils.d.ts +2 -0
  220. package/dist/utils/fs-utils.js +22 -0
  221. package/dist/utils/path-validation.d.ts +3 -0
  222. package/dist/utils/path-validation.js +56 -0
  223. package/dist/utils/spawn-process.d.ts +9 -0
  224. package/dist/utils/spawn-process.js +50 -0
  225. package/dist/utils/subscribable.d.ts +12 -0
  226. package/dist/utils/subscribable.js +44 -0
  227. package/package.json +99 -21
  228. package/dist/index.d.ts.map +0 -1
  229. package/dist/index.js.map +0 -1
@@ -0,0 +1,151 @@
1
+ /**
2
+ * Well-Known Endpoints for XMCP-I Runtime
3
+ *
4
+ * Handles /.well-known/did.json and /.well-known/agent.json endpoints
5
+ * according to requirements 7.1, 7.2, 7.3, 7.4, 7.5.
6
+ */
7
+ import { AgentIdentity } from "./identity";
8
+ /**
9
+ * DID Document structure (W3C DID Core specification)
10
+ */
11
+ export interface DIDDocument {
12
+ "@context": string[];
13
+ id: string;
14
+ verificationMethod: VerificationMethod[];
15
+ authentication: string[];
16
+ assertionMethod: string[];
17
+ keyAgreement?: string[];
18
+ capabilityInvocation?: string[];
19
+ capabilityDelegation?: string[];
20
+ service?: ServiceEndpoint[];
21
+ }
22
+ /**
23
+ * Verification Method for DID Document
24
+ */
25
+ export interface VerificationMethod {
26
+ id: string;
27
+ type: string;
28
+ controller: string;
29
+ publicKeyMultibase?: string;
30
+ publicKeyJwk?: any;
31
+ }
32
+ /**
33
+ * Service Endpoint for DID Document
34
+ */
35
+ export interface ServiceEndpoint {
36
+ id: string;
37
+ type: string;
38
+ serviceEndpoint: string;
39
+ }
40
+ /**
41
+ * Agent Document structure (XMCP-I specific)
42
+ */
43
+ export interface AgentDocument {
44
+ id: string;
45
+ capabilities: {
46
+ "mcp-i": ["handshake", "signing", "verification"];
47
+ };
48
+ registry?: {
49
+ kta?: string;
50
+ mcp?: string;
51
+ };
52
+ metadata?: {
53
+ name?: string;
54
+ description?: string;
55
+ version?: string;
56
+ };
57
+ }
58
+ /**
59
+ * Well-known endpoint configuration
60
+ */
61
+ export interface WellKnownConfig {
62
+ environment: "development" | "production";
63
+ baseUrl?: string;
64
+ agentMetadata?: {
65
+ name?: string;
66
+ description?: string;
67
+ version?: string;
68
+ };
69
+ registryUrls?: {
70
+ kta?: string;
71
+ mcp?: string;
72
+ };
73
+ }
74
+ /**
75
+ * Well-known endpoints manager
76
+ */
77
+ export declare class WellKnownManager {
78
+ private identity;
79
+ private config;
80
+ constructor(identity: AgentIdentity, config: WellKnownConfig);
81
+ /**
82
+ * Generate DID document for /.well-known/did.json
83
+ * Requirements: 7.1, 7.5
84
+ */
85
+ generateDIDDocument(): DIDDocument;
86
+ /**
87
+ * Generate agent document for /.well-known/agent.json
88
+ * Requirements: 7.2, 7.3
89
+ */
90
+ generateAgentDocument(): AgentDocument;
91
+ /**
92
+ * Get HTTP headers for DID document
93
+ * Requirements: 7.4, 7.5
94
+ */
95
+ getDIDDocumentHeaders(): Record<string, string>;
96
+ /**
97
+ * Get HTTP headers for agent document
98
+ * Requirements: 7.2, 7.5
99
+ */
100
+ getAgentDocumentHeaders(): Record<string, string>;
101
+ /**
102
+ * Encode public key as multibase (base58btc with 'z' prefix for Ed25519)
103
+ */
104
+ private encodePublicKeyMultibase;
105
+ /**
106
+ * Simple base58 encoding (use proper library in production)
107
+ */
108
+ private encodeBase58;
109
+ /**
110
+ * Update configuration
111
+ */
112
+ updateConfig(config: Partial<WellKnownConfig>): void;
113
+ /**
114
+ * Get current configuration
115
+ */
116
+ getConfig(): WellKnownConfig;
117
+ }
118
+ /**
119
+ * Express/HTTP handler for well-known endpoints
120
+ */
121
+ export interface WellKnownHandler {
122
+ handleDIDDocument(): {
123
+ status: number;
124
+ headers: Record<string, string>;
125
+ body: string;
126
+ };
127
+ handleAgentDocument(): {
128
+ status: number;
129
+ headers: Record<string, string>;
130
+ body: string;
131
+ };
132
+ }
133
+ /**
134
+ * Create well-known endpoint handler
135
+ */
136
+ export declare function createWellKnownHandler(identity: AgentIdentity, config: WellKnownConfig): WellKnownHandler;
137
+ /**
138
+ * Utility functions
139
+ */
140
+ /**
141
+ * Validate DID document structure
142
+ */
143
+ export declare function validateDIDDocument(doc: any): doc is DIDDocument;
144
+ /**
145
+ * Validate agent document structure
146
+ */
147
+ export declare function validateAgentDocument(doc: any): doc is AgentDocument;
148
+ /**
149
+ * Extract DID from URL path (for did:web resolution)
150
+ */
151
+ export declare function extractDIDFromPath(path: string, baseUrl: string): string | null;
@@ -0,0 +1,258 @@
1
+ "use strict";
2
+ /**
3
+ * Well-Known Endpoints for XMCP-I Runtime
4
+ *
5
+ * Handles /.well-known/did.json and /.well-known/agent.json endpoints
6
+ * according to requirements 7.1, 7.2, 7.3, 7.4, 7.5.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.WellKnownManager = void 0;
10
+ exports.createWellKnownHandler = createWellKnownHandler;
11
+ exports.validateDIDDocument = validateDIDDocument;
12
+ exports.validateAgentDocument = validateAgentDocument;
13
+ exports.extractDIDFromPath = extractDIDFromPath;
14
+ /**
15
+ * Well-known endpoints manager
16
+ */
17
+ class WellKnownManager {
18
+ identity;
19
+ config;
20
+ constructor(identity, config) {
21
+ this.identity = identity;
22
+ this.config = config;
23
+ }
24
+ /**
25
+ * Generate DID document for /.well-known/did.json
26
+ * Requirements: 7.1, 7.5
27
+ */
28
+ generateDIDDocument() {
29
+ const keyId = `#${this.identity.keyId}`;
30
+ // Convert base64 public key to multibase format
31
+ const publicKeyMultibase = this.encodePublicKeyMultibase(this.identity.publicKey);
32
+ const didDocument = {
33
+ "@context": [
34
+ "https://www.w3.org/ns/did/v1",
35
+ "https://w3id.org/security/suites/ed25519-2020/v1",
36
+ ],
37
+ id: this.identity.did,
38
+ verificationMethod: [
39
+ {
40
+ id: keyId,
41
+ type: "Ed25519VerificationKey2020",
42
+ controller: this.identity.did,
43
+ publicKeyMultibase,
44
+ },
45
+ ],
46
+ authentication: [keyId],
47
+ assertionMethod: [keyId],
48
+ };
49
+ return didDocument;
50
+ }
51
+ /**
52
+ * Generate agent document for /.well-known/agent.json
53
+ * Requirements: 7.2, 7.3
54
+ */
55
+ generateAgentDocument() {
56
+ const agentDocument = {
57
+ id: this.identity.did,
58
+ capabilities: {
59
+ "mcp-i": ["handshake", "signing", "verification"], // Exact capability triplet
60
+ },
61
+ };
62
+ // Add registry URLs if configured
63
+ if (this.config.registryUrls) {
64
+ agentDocument.registry = { ...this.config.registryUrls };
65
+ }
66
+ // Add metadata if configured
67
+ if (this.config.agentMetadata) {
68
+ agentDocument.metadata = { ...this.config.agentMetadata };
69
+ }
70
+ return agentDocument;
71
+ }
72
+ /**
73
+ * Get HTTP headers for DID document
74
+ * Requirements: 7.4, 7.5
75
+ */
76
+ getDIDDocumentHeaders() {
77
+ const headers = {
78
+ "Content-Type": "application/did+json",
79
+ };
80
+ // Add caching headers based on environment
81
+ if (this.config.environment === "production") {
82
+ headers["Cache-Control"] = "public, max-age=300"; // 5 minutes
83
+ }
84
+ else {
85
+ headers["Cache-Control"] = "no-store";
86
+ }
87
+ return headers;
88
+ }
89
+ /**
90
+ * Get HTTP headers for agent document
91
+ * Requirements: 7.2, 7.5
92
+ */
93
+ getAgentDocumentHeaders() {
94
+ const headers = {
95
+ "Content-Type": "application/json",
96
+ };
97
+ // Always no-store for agent document in dev, public cache in prod
98
+ if (this.config.environment === "production") {
99
+ headers["Cache-Control"] = "public, max-age=300"; // 5 minutes
100
+ }
101
+ else {
102
+ headers["Cache-Control"] = "no-store";
103
+ }
104
+ return headers;
105
+ }
106
+ /**
107
+ * Encode public key as multibase (base58btc with 'z' prefix for Ed25519)
108
+ */
109
+ encodePublicKeyMultibase(base64PublicKey) {
110
+ // For Ed25519, we use base58btc encoding with 'z' prefix
111
+ // This is a simplified implementation - in production, use proper multibase library
112
+ const publicKeyBytes = Buffer.from(base64PublicKey, "base64");
113
+ // Ed25519 public key prefix (0xed01) + key bytes
114
+ const prefixedKey = Buffer.concat([
115
+ Buffer.from([0xed, 0x01]), // Ed25519 multicodec prefix
116
+ publicKeyBytes,
117
+ ]);
118
+ // Convert to base58btc (simplified - use proper base58 library in production)
119
+ const base58 = this.encodeBase58(prefixedKey);
120
+ return `z${base58}`; // 'z' prefix indicates base58btc
121
+ }
122
+ /**
123
+ * Simple base58 encoding (use proper library in production)
124
+ */
125
+ encodeBase58(buffer) {
126
+ const alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
127
+ let num = BigInt("0x" + buffer.toString("hex"));
128
+ let result = "";
129
+ while (num > 0) {
130
+ const remainder = num % 58n;
131
+ result = alphabet[Number(remainder)] + result;
132
+ num = num / 58n;
133
+ }
134
+ // Handle leading zeros
135
+ for (let i = 0; i < buffer.length && buffer[i] === 0; i++) {
136
+ result = "1" + result;
137
+ }
138
+ return result;
139
+ }
140
+ /**
141
+ * Update configuration
142
+ */
143
+ updateConfig(config) {
144
+ this.config = { ...this.config, ...config };
145
+ }
146
+ /**
147
+ * Get current configuration
148
+ */
149
+ getConfig() {
150
+ return { ...this.config };
151
+ }
152
+ }
153
+ exports.WellKnownManager = WellKnownManager;
154
+ /**
155
+ * Create well-known endpoint handler
156
+ */
157
+ function createWellKnownHandler(identity, config) {
158
+ const manager = new WellKnownManager(identity, config);
159
+ return {
160
+ handleDIDDocument() {
161
+ try {
162
+ const didDocument = manager.generateDIDDocument();
163
+ const headers = manager.getDIDDocumentHeaders();
164
+ return {
165
+ status: 200,
166
+ headers,
167
+ body: JSON.stringify(didDocument, null, 2),
168
+ };
169
+ }
170
+ catch (error) {
171
+ return {
172
+ status: 500,
173
+ headers: { "Content-Type": "application/json" },
174
+ body: JSON.stringify({
175
+ error: "Failed to generate DID document",
176
+ message: error instanceof Error ? error.message : "Unknown error",
177
+ }),
178
+ };
179
+ }
180
+ },
181
+ handleAgentDocument() {
182
+ try {
183
+ const agentDocument = manager.generateAgentDocument();
184
+ const headers = manager.getAgentDocumentHeaders();
185
+ return {
186
+ status: 200,
187
+ headers,
188
+ body: JSON.stringify(agentDocument, null, 2),
189
+ };
190
+ }
191
+ catch (error) {
192
+ return {
193
+ status: 500,
194
+ headers: { "Content-Type": "application/json" },
195
+ body: JSON.stringify({
196
+ error: "Failed to generate agent document",
197
+ message: error instanceof Error ? error.message : "Unknown error",
198
+ }),
199
+ };
200
+ }
201
+ },
202
+ };
203
+ }
204
+ /**
205
+ * Utility functions
206
+ */
207
+ /**
208
+ * Validate DID document structure
209
+ */
210
+ function validateDIDDocument(doc) {
211
+ return (typeof doc === "object" &&
212
+ doc !== null &&
213
+ Array.isArray(doc["@context"]) &&
214
+ typeof doc.id === "string" &&
215
+ doc.id.startsWith("did:") &&
216
+ Array.isArray(doc.verificationMethod) &&
217
+ Array.isArray(doc.authentication) &&
218
+ Array.isArray(doc.assertionMethod));
219
+ }
220
+ /**
221
+ * Validate agent document structure
222
+ */
223
+ function validateAgentDocument(doc) {
224
+ return (typeof doc === "object" &&
225
+ doc !== null &&
226
+ typeof doc.id === "string" &&
227
+ doc.id.startsWith("did:") &&
228
+ typeof doc.capabilities === "object" &&
229
+ Array.isArray(doc.capabilities["mcp-i"]) &&
230
+ doc.capabilities["mcp-i"].length === 3 &&
231
+ doc.capabilities["mcp-i"].includes("handshake") &&
232
+ doc.capabilities["mcp-i"].includes("signing") &&
233
+ doc.capabilities["mcp-i"].includes("verification"));
234
+ }
235
+ /**
236
+ * Extract DID from URL path (for did:web resolution)
237
+ */
238
+ function extractDIDFromPath(path, baseUrl) {
239
+ try {
240
+ // For did:web, the DID is constructed from the domain and path
241
+ // Example: /.well-known/did.json -> did:web:example.com
242
+ // Example: /agents/my-agent/.well-known/did.json -> did:web:example.com:agents:my-agent
243
+ const url = new URL(baseUrl);
244
+ const domain = url.hostname;
245
+ // Remove /.well-known/did.json from path
246
+ const cleanPath = path.replace("/.well-known/did.json", "");
247
+ if (cleanPath === "") {
248
+ return `did:web:${domain}`;
249
+ }
250
+ // Convert path segments to DID path components
251
+ const pathComponents = cleanPath.split("/").filter(Boolean);
252
+ const didPath = pathComponents.join(":");
253
+ return `did:web:${domain}:${didPath}`;
254
+ }
255
+ catch {
256
+ return null;
257
+ }
258
+ }
@@ -0,0 +1,28 @@
1
+ import { StorageMode, StorageConfig } from "@kya-os/contracts/registry";
2
+ /**
3
+ * Storage configuration utilities for XMCP-I
4
+ */
5
+ /**
6
+ * Detects and validates the storage mode from environment variables
7
+ */
8
+ export declare function detectStorageMode(): StorageMode;
9
+ /**
10
+ * Creates a complete storage configuration with defaults
11
+ */
12
+ export declare function createStorageConfig(overrides?: Partial<StorageConfig>): StorageConfig;
13
+ /**
14
+ * Validates storage mode configuration
15
+ */
16
+ export declare function validateStorageMode(mode: string): mode is StorageMode;
17
+ /**
18
+ * Gets storage mode description for logging/debugging
19
+ */
20
+ export declare function getStorageModeDescription(mode: StorageMode): string;
21
+ /**
22
+ * Determines if encryption is required for the given storage mode
23
+ */
24
+ export declare function isEncryptionRequired(mode: StorageMode): boolean;
25
+ /**
26
+ * Determines if receipt verification is available for the given storage mode
27
+ */
28
+ export declare function isReceiptVerificationAvailable(_mode: StorageMode): boolean;
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.detectStorageMode = detectStorageMode;
4
+ exports.createStorageConfig = createStorageConfig;
5
+ exports.validateStorageMode = validateStorageMode;
6
+ exports.getStorageModeDescription = getStorageModeDescription;
7
+ exports.isEncryptionRequired = isEncryptionRequired;
8
+ exports.isReceiptVerificationAvailable = isReceiptVerificationAvailable;
9
+ const registry_1 = require("@kya-os/contracts/registry");
10
+ /**
11
+ * Storage configuration utilities for XMCP-I
12
+ */
13
+ /**
14
+ * Detects and validates the storage mode from environment variables
15
+ */
16
+ function detectStorageMode() {
17
+ const envValue = process.env[registry_1.STORAGE_MODE_ENV_VAR];
18
+ if (!envValue) {
19
+ return registry_1.DEFAULT_STORAGE_MODE;
20
+ }
21
+ const result = registry_1.StorageModeSchema.safeParse(envValue);
22
+ if (!result.success) {
23
+ console.warn(`Invalid ${registry_1.STORAGE_MODE_ENV_VAR}="${envValue}". Valid values: ktaEncrypted, hybridReceiptsOnly, selfHostedAuthoritative. Using default: ${registry_1.DEFAULT_STORAGE_MODE}`);
24
+ return registry_1.DEFAULT_STORAGE_MODE;
25
+ }
26
+ return result.data;
27
+ }
28
+ /**
29
+ * Creates a complete storage configuration with defaults
30
+ */
31
+ function createStorageConfig(overrides) {
32
+ const mode = detectStorageMode();
33
+ const config = {
34
+ mode,
35
+ encryptionEnabled: mode === "ktaEncrypted",
36
+ receiptVerificationEnabled: true,
37
+ ktaBaseURL: "https://knowthat.ai",
38
+ ...overrides,
39
+ };
40
+ const result = registry_1.StorageConfigSchema.safeParse(config);
41
+ if (!result.success) {
42
+ throw new Error(`Invalid storage configuration: ${result.error.message}`);
43
+ }
44
+ return result.data;
45
+ }
46
+ /**
47
+ * Validates storage mode configuration
48
+ */
49
+ function validateStorageMode(mode) {
50
+ return registry_1.StorageModeSchema.safeParse(mode).success;
51
+ }
52
+ /**
53
+ * Gets storage mode description for logging/debugging
54
+ */
55
+ function getStorageModeDescription(mode) {
56
+ switch (mode) {
57
+ case "ktaEncrypted":
58
+ return "KTA stores encrypted credential/delegation objects";
59
+ case "hybridReceiptsOnly":
60
+ return "KTA stores receipts only; objects stored by issuer/recipient";
61
+ case "selfHostedAuthoritative":
62
+ return "Customer stores object + log; KTA mirrors receipt/pointer";
63
+ default:
64
+ return "Unknown storage mode";
65
+ }
66
+ }
67
+ /**
68
+ * Determines if encryption is required for the given storage mode
69
+ */
70
+ function isEncryptionRequired(mode) {
71
+ return mode === "ktaEncrypted";
72
+ }
73
+ /**
74
+ * Determines if receipt verification is available for the given storage mode
75
+ */
76
+ function isReceiptVerificationAvailable(_mode) {
77
+ // All modes support receipt verification
78
+ return true;
79
+ }
@@ -0,0 +1,59 @@
1
+ import { Delegation, DelegationRequest, DelegationResponse, Receipt, StorageConfig } from "@kya-os/contracts/registry";
2
+ /**
3
+ * Delegation management for XMCP-I runtime
4
+ */
5
+ export interface DelegationManager {
6
+ /**
7
+ * Issue a new delegation
8
+ */
9
+ issue(request: DelegationRequest): Promise<DelegationResponse>;
10
+ /**
11
+ * Revoke an existing delegation
12
+ */
13
+ revoke(delegationRef: string): Promise<Receipt>;
14
+ /**
15
+ * Check if a delegation is active
16
+ */
17
+ isActive(delegationRef: string): Promise<boolean>;
18
+ /**
19
+ * Get delegation details
20
+ */
21
+ get(delegationRef: string): Promise<Delegation | null>;
22
+ /**
23
+ * List active delegations for a subject
24
+ */
25
+ listBySubject(subject: string): Promise<Delegation[]>;
26
+ }
27
+ /**
28
+ * Default delegation manager implementation
29
+ */
30
+ export declare class DefaultDelegationManager implements DelegationManager {
31
+ private config;
32
+ constructor(config?: Partial<StorageConfig>);
33
+ issue(request: DelegationRequest): Promise<DelegationResponse>;
34
+ revoke(_delegationRef: string): Promise<Receipt>;
35
+ isActive(delegationRef: string): Promise<boolean>;
36
+ get(delegationRef: string): Promise<Delegation | null>;
37
+ listBySubject(_subject: string): Promise<Delegation[]>;
38
+ /**
39
+ * Encrypt delegation payload for audience
40
+ */
41
+ private encryptDelegation;
42
+ }
43
+ /**
44
+ * Create delegation manager instance
45
+ */
46
+ export declare function createDelegationManager(config?: Partial<StorageConfig>): DelegationManager;
47
+ /**
48
+ * Delegation context for runtime use
49
+ */
50
+ export interface DelegationContext {
51
+ delegationRef?: string;
52
+ scopes: string[];
53
+ audience?: string;
54
+ expiresAt: number;
55
+ }
56
+ /**
57
+ * Extract delegation context from proof metadata
58
+ */
59
+ export declare function extractDelegationContext(delegationRef?: string): DelegationContext | null;