@kya-os/mcp-i-runtime 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +4 -0
- package/dist/auth/handshake.d.ts +136 -0
- package/dist/auth/handshake.d.ts.map +1 -0
- package/dist/auth/handshake.js +276 -0
- package/dist/auth/handshake.js.map +1 -0
- package/dist/auth/index.d.ts +6 -0
- package/dist/auth/index.d.ts.map +1 -0
- package/dist/auth/index.js +12 -0
- package/dist/auth/index.js.map +1 -0
- package/dist/auth/types.d.ts +52 -0
- package/dist/auth/types.d.ts.map +1 -0
- package/dist/auth/types.js +10 -0
- package/dist/auth/types.js.map +1 -0
- package/dist/cache/oauth-config-cache.d.ts +69 -0
- package/dist/cache/oauth-config-cache.d.ts.map +1 -0
- package/dist/cache/oauth-config-cache.js +76 -0
- package/dist/cache/oauth-config-cache.js.map +1 -0
- package/dist/cache/tool-protection-cache.d.ts +102 -0
- package/dist/cache/tool-protection-cache.d.ts.map +1 -0
- package/dist/cache/tool-protection-cache.js +131 -0
- package/dist/cache/tool-protection-cache.js.map +1 -0
- package/dist/compliance/index.d.ts +8 -0
- package/dist/compliance/index.d.ts.map +1 -0
- package/dist/compliance/index.js +24 -0
- package/dist/compliance/index.js.map +1 -0
- package/dist/compliance/schema-registry.d.ts +50 -0
- package/dist/compliance/schema-registry.d.ts.map +1 -0
- package/dist/compliance/schema-registry.js +438 -0
- package/dist/compliance/schema-registry.js.map +1 -0
- package/dist/compliance/schema-verifier.d.ts +110 -0
- package/dist/compliance/schema-verifier.d.ts.map +1 -0
- package/dist/compliance/schema-verifier.js +518 -0
- package/dist/compliance/schema-verifier.js.map +1 -0
- package/dist/config/remote-config.d.ts +188 -0
- package/dist/config/remote-config.d.ts.map +1 -0
- package/dist/config/remote-config.js +268 -0
- package/dist/config/remote-config.js.map +1 -0
- package/dist/config.d.ts +182 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +148 -0
- package/dist/config.js.map +1 -0
- package/dist/identity/idp-token-resolver.d.ts +69 -0
- package/dist/identity/idp-token-resolver.d.ts.map +1 -0
- package/dist/identity/idp-token-resolver.js +136 -0
- package/dist/identity/idp-token-resolver.js.map +1 -0
- package/dist/identity/idp-token-storage.interface.d.ts +80 -0
- package/dist/identity/idp-token-storage.interface.d.ts.map +1 -0
- package/dist/identity/idp-token-storage.interface.js +14 -0
- package/dist/identity/idp-token-storage.interface.js.map +1 -0
- package/dist/identity/user-did-manager.d.ts +228 -0
- package/dist/identity/user-did-manager.d.ts.map +1 -0
- package/dist/identity/user-did-manager.js +356 -0
- package/dist/identity/user-did-manager.js.map +1 -0
- package/dist/index.d.ts +69 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +193 -0
- package/dist/index.js.map +1 -0
- package/dist/logging/index.d.ts +5 -0
- package/dist/logging/index.d.ts.map +1 -0
- package/dist/logging/index.js +10 -0
- package/dist/logging/index.js.map +1 -0
- package/dist/logging/logger.d.ts +33 -0
- package/dist/logging/logger.d.ts.map +1 -0
- package/dist/logging/logger.js +104 -0
- package/dist/logging/logger.js.map +1 -0
- package/dist/proof/generator.d.ts +109 -0
- package/dist/proof/generator.d.ts.map +1 -0
- package/dist/proof/generator.js +236 -0
- package/dist/proof/generator.js.map +1 -0
- package/dist/proof/index.d.ts +5 -0
- package/dist/proof/index.d.ts.map +1 -0
- package/dist/proof/index.js +11 -0
- package/dist/proof/index.js.map +1 -0
- package/dist/providers/base.d.ts +95 -0
- package/dist/providers/base.d.ts.map +1 -0
- package/dist/providers/base.js +47 -0
- package/dist/providers/base.js.map +1 -0
- package/dist/providers/memory.d.ts +42 -0
- package/dist/providers/memory.d.ts.map +1 -0
- package/dist/providers/memory.js +121 -0
- package/dist/providers/memory.js.map +1 -0
- package/dist/runtime/audit-logger.d.ts +37 -0
- package/dist/runtime/audit-logger.d.ts.map +1 -0
- package/dist/runtime/audit-logger.js +9 -0
- package/dist/runtime/audit-logger.js.map +1 -0
- package/dist/runtime/base.d.ts +392 -0
- package/dist/runtime/base.d.ts.map +1 -0
- package/dist/runtime/base.js +1527 -0
- package/dist/runtime/base.js.map +1 -0
- package/dist/runtime/dif-core.d.ts +33 -0
- package/dist/runtime/dif-core.d.ts.map +1 -0
- package/dist/runtime/dif-core.js +14 -0
- package/dist/runtime/dif-core.js.map +1 -0
- package/dist/runtime/ext-apps-constants.d.ts +20 -0
- package/dist/runtime/ext-apps-constants.d.ts.map +1 -0
- package/dist/runtime/ext-apps-constants.js +23 -0
- package/dist/runtime/ext-apps-constants.js.map +1 -0
- package/dist/services/access-control.service.d.ts +138 -0
- package/dist/services/access-control.service.d.ts.map +1 -0
- package/dist/services/access-control.service.js +721 -0
- package/dist/services/access-control.service.js.map +1 -0
- package/dist/services/authorization/authorization-registry.d.ts +29 -0
- package/dist/services/authorization/authorization-registry.d.ts.map +1 -0
- package/dist/services/authorization/authorization-registry.js +58 -0
- package/dist/services/authorization/authorization-registry.js.map +1 -0
- package/dist/services/authorization/types.d.ts +53 -0
- package/dist/services/authorization/types.d.ts.map +1 -0
- package/dist/services/authorization/types.js +10 -0
- package/dist/services/authorization/types.js.map +1 -0
- package/dist/services/batch-delegation.service.d.ts +53 -0
- package/dist/services/batch-delegation.service.d.ts.map +1 -0
- package/dist/services/batch-delegation.service.js +95 -0
- package/dist/services/batch-delegation.service.js.map +1 -0
- package/dist/services/crypto.service.d.ts +69 -0
- package/dist/services/crypto.service.d.ts.map +1 -0
- package/dist/services/crypto.service.js +225 -0
- package/dist/services/crypto.service.js.map +1 -0
- package/dist/services/delegation-error-formatter.d.ts +124 -0
- package/dist/services/delegation-error-formatter.d.ts.map +1 -0
- package/dist/services/delegation-error-formatter.js +214 -0
- package/dist/services/delegation-error-formatter.js.map +1 -0
- package/dist/services/errors.d.ts +49 -0
- package/dist/services/errors.d.ts.map +1 -0
- package/dist/services/errors.js +66 -0
- package/dist/services/errors.js.map +1 -0
- package/dist/services/index.d.ts +9 -0
- package/dist/services/index.d.ts.map +1 -0
- package/dist/services/index.js +15 -0
- package/dist/services/index.js.map +1 -0
- package/dist/services/oauth-config.service.d.ts +53 -0
- package/dist/services/oauth-config.service.d.ts.map +1 -0
- package/dist/services/oauth-config.service.js +141 -0
- package/dist/services/oauth-config.service.js.map +1 -0
- package/dist/services/oauth-provider-registry.d.ts +88 -0
- package/dist/services/oauth-provider-registry.d.ts.map +1 -0
- package/dist/services/oauth-provider-registry.js +128 -0
- package/dist/services/oauth-provider-registry.js.map +1 -0
- package/dist/services/oauth-service.d.ts +122 -0
- package/dist/services/oauth-service.d.ts.map +1 -0
- package/dist/services/oauth-service.js +442 -0
- package/dist/services/oauth-service.js.map +1 -0
- package/dist/services/oauth-token-retrieval.service.d.ts +49 -0
- package/dist/services/oauth-token-retrieval.service.d.ts.map +1 -0
- package/dist/services/oauth-token-retrieval.service.js +150 -0
- package/dist/services/oauth-token-retrieval.service.js.map +1 -0
- package/dist/services/policy.service.d.ts +130 -0
- package/dist/services/policy.service.d.ts.map +1 -0
- package/dist/services/policy.service.js +399 -0
- package/dist/services/policy.service.js.map +1 -0
- package/dist/services/proof-verifier.d.ts +103 -0
- package/dist/services/proof-verifier.d.ts.map +1 -0
- package/dist/services/proof-verifier.js +190 -0
- package/dist/services/proof-verifier.js.map +1 -0
- package/dist/services/provider-resolver.d.ts +89 -0
- package/dist/services/provider-resolver.d.ts.map +1 -0
- package/dist/services/provider-resolver.js +196 -0
- package/dist/services/provider-resolver.js.map +1 -0
- package/dist/services/provider-validator.d.ts +55 -0
- package/dist/services/provider-validator.d.ts.map +1 -0
- package/dist/services/provider-validator.js +135 -0
- package/dist/services/provider-validator.js.map +1 -0
- package/dist/services/session-registration.service.d.ts +80 -0
- package/dist/services/session-registration.service.d.ts.map +1 -0
- package/dist/services/session-registration.service.js +174 -0
- package/dist/services/session-registration.service.js.map +1 -0
- package/dist/services/storage.service.d.ts +116 -0
- package/dist/services/storage.service.d.ts.map +1 -0
- package/dist/services/storage.service.js +411 -0
- package/dist/services/storage.service.js.map +1 -0
- package/dist/services/tool-context-builder.d.ts +74 -0
- package/dist/services/tool-context-builder.d.ts.map +1 -0
- package/dist/services/tool-context-builder.js +215 -0
- package/dist/services/tool-context-builder.js.map +1 -0
- package/dist/services/tool-protection.service.d.ts +209 -0
- package/dist/services/tool-protection.service.d.ts.map +1 -0
- package/dist/services/tool-protection.service.js +752 -0
- package/dist/services/tool-protection.service.js.map +1 -0
- package/dist/session/index.d.ts +5 -0
- package/dist/session/index.d.ts.map +1 -0
- package/dist/session/index.js +11 -0
- package/dist/session/index.js.map +1 -0
- package/dist/session/manager.d.ts +118 -0
- package/dist/session/manager.d.ts.map +1 -0
- package/dist/session/manager.js +285 -0
- package/dist/session/manager.js.map +1 -0
- package/dist/types/oauth-required-error.d.ts +46 -0
- package/dist/types/oauth-required-error.d.ts.map +1 -0
- package/dist/types/oauth-required-error.js +42 -0
- package/dist/types/oauth-required-error.js.map +1 -0
- package/dist/types/tool-protection.d.ts +144 -0
- package/dist/types/tool-protection.d.ts.map +1 -0
- package/dist/types/tool-protection.js +48 -0
- package/dist/types/tool-protection.js.map +1 -0
- package/dist/utils/base58.d.ts +31 -0
- package/dist/utils/base58.d.ts.map +1 -0
- package/dist/utils/base58.js +103 -0
- package/dist/utils/base58.js.map +1 -0
- package/dist/utils/base64.d.ts +32 -0
- package/dist/utils/base64.d.ts.map +1 -0
- package/dist/utils/base64.js +172 -0
- package/dist/utils/base64.js.map +1 -0
- package/dist/utils/cors.d.ts +52 -0
- package/dist/utils/cors.d.ts.map +1 -0
- package/dist/utils/cors.js +79 -0
- package/dist/utils/cors.js.map +1 -0
- package/dist/utils/did-helpers.d.ts +168 -0
- package/dist/utils/did-helpers.d.ts.map +1 -0
- package/dist/utils/did-helpers.js +222 -0
- package/dist/utils/did-helpers.js.map +1 -0
- package/dist/utils/index.d.ts +8 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +24 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/storage-keys.d.ts +120 -0
- package/dist/utils/storage-keys.d.ts.map +1 -0
- package/dist/utils/storage-keys.js +217 -0
- package/dist/utils/storage-keys.js.map +1 -0
- package/dist/wire-format.d.ts +62 -0
- package/dist/wire-format.d.ts.map +1 -0
- package/dist/wire-format.js +89 -0
- package/dist/wire-format.js.map +1 -0
- package/package.json +83 -0
- package/src/__tests__/cache/tool-protection-cache.test.ts +640 -0
- package/src/__tests__/config/provider-runtime-config.test.ts +309 -0
- package/src/__tests__/config.test.ts +307 -0
- package/src/__tests__/identity/user-did-manager.test.ts +232 -0
- package/src/__tests__/index.test.ts +56 -0
- package/src/__tests__/integration/full-flow.test.ts +779 -0
- package/src/__tests__/integration.test.ts +281 -0
- package/src/__tests__/providers/base.test.ts +173 -0
- package/src/__tests__/providers/memory.test.ts +319 -0
- package/src/__tests__/regression/phase2-regression.test.ts +406 -0
- package/src/__tests__/runtime/audit-logger.test.ts +154 -0
- package/src/__tests__/runtime/base-extensions.test.ts +595 -0
- package/src/__tests__/runtime/base.test.ts +1328 -0
- package/src/__tests__/runtime/delegation-flow.test.ts +159 -0
- package/src/__tests__/runtime/process-tool-call-security.test.ts +1024 -0
- package/src/__tests__/runtime/proof-client-did.test.ts +376 -0
- package/src/__tests__/runtime/provider-selection.test.ts +292 -0
- package/src/__tests__/runtime/route-interception.test.ts +670 -0
- package/src/__tests__/runtime/tool-protection-enforcement.test.ts +989 -0
- package/src/__tests__/services/access-control-retry.test.ts +595 -0
- package/src/__tests__/services/agentshield-integration.test.ts +709 -0
- package/src/__tests__/services/cache-busting.test.ts +125 -0
- package/src/__tests__/services/oauth-service-errors.test.ts +704 -0
- package/src/__tests__/services/oauth-service-pkce.test.ts +556 -0
- package/src/__tests__/services/oauth-service-secret-resolution.test.ts +349 -0
- package/src/__tests__/services/provider-resolver-edge-cases.test.ts +591 -0
- package/src/__tests__/services/tool-protection-failsafe.test.ts +683 -0
- package/src/__tests__/services/tool-protection-merged-config.test.ts +664 -0
- package/src/__tests__/services/tool-protection-oauth-provider.test.ts +300 -0
- package/src/__tests__/services/tool-protection.service.test.ts +1201 -0
- package/src/__tests__/utils/mock-providers.ts +340 -0
- package/src/__tests__/wire-format.test.ts +123 -0
- package/src/auth/__tests__/resume-token.test.ts +42 -0
- package/src/auth/handshake.ts +476 -0
- package/src/auth/index.ts +15 -0
- package/src/auth/types.ts +68 -0
- package/src/cache/__tests__/oauth-config-cache.test.ts +215 -0
- package/src/cache/oauth-config-cache.d.ts +69 -0
- package/src/cache/oauth-config-cache.d.ts.map +1 -0
- package/src/cache/oauth-config-cache.js.map +1 -0
- package/src/cache/oauth-config-cache.ts +123 -0
- package/src/cache/tool-protection-cache.ts +184 -0
- package/src/compliance/EXAMPLE.md +412 -0
- package/src/compliance/__tests__/schema-verifier.test.ts +791 -0
- package/src/compliance/index.ts +8 -0
- package/src/compliance/schema-registry.ts +460 -0
- package/src/compliance/schema-verifier.ts +708 -0
- package/src/config/__tests__/merged-config.spec.ts +445 -0
- package/src/config/__tests__/remote-config-edge-cases.test.ts +424 -0
- package/src/config/__tests__/remote-config-helpers.test.ts +280 -0
- package/src/config/__tests__/remote-config.spec.ts +265 -0
- package/src/config/remote-config.ts +368 -0
- package/src/config.ts +312 -0
- package/src/identity/__tests__/idp-token-resolver.test.ts +340 -0
- package/src/identity/__tests__/user-did-manager.test.ts +487 -0
- package/src/identity/idp-token-resolver.ts +181 -0
- package/src/identity/idp-token-storage.interface.ts +102 -0
- package/src/identity/user-did-manager.ts +592 -0
- package/src/index.ts +331 -0
- package/src/logging/__tests__/logger.test.ts +366 -0
- package/src/logging/index.ts +10 -0
- package/src/logging/logger.ts +112 -0
- package/src/proof/__tests__/crypto-parity.test.ts +202 -0
- package/src/proof/__tests__/proof-generator.test.ts +484 -0
- package/src/proof/generator.ts +347 -0
- package/src/proof/index.ts +12 -0
- package/src/providers/base.d.ts +91 -0
- package/src/providers/base.d.ts.map +1 -0
- package/src/providers/base.js.map +1 -0
- package/src/providers/base.ts +100 -0
- package/src/providers/memory.ts +142 -0
- package/src/runtime/__tests__/dif-esm-bridge.test.ts +33 -0
- package/src/runtime/__tests__/ext-apps-consent.test.ts +334 -0
- package/src/runtime/audit-logger.ts +39 -0
- package/src/runtime/base.ts +2100 -0
- package/src/runtime/dif-core.ts +39 -0
- package/src/runtime/ext-apps-constants.ts +20 -0
- package/src/services/__tests__/access-control.integration.test.ts +441 -0
- package/src/services/__tests__/access-control.proof-response-validation.test.ts +578 -0
- package/src/services/__tests__/access-control.service.test.ts +1132 -0
- package/src/services/__tests__/batch-delegation.service.test.ts +351 -0
- package/src/services/__tests__/crypto.service.test.ts +531 -0
- package/src/services/__tests__/delegation-error-formatter.test.ts +643 -0
- package/src/services/__tests__/oauth-config.service.test.ts +359 -0
- package/src/services/__tests__/oauth-provider-registry.test.ts +142 -0
- package/src/services/__tests__/oauth-token-retrieval.service.test.ts +365 -0
- package/src/services/__tests__/proof-verifier.integration.test.ts +494 -0
- package/src/services/__tests__/proof-verifier.test.ts +489 -0
- package/src/services/__tests__/provider-resolution.integration.test.ts +202 -0
- package/src/services/__tests__/provider-resolver.test.ts +213 -0
- package/src/services/__tests__/provider-validator.test.ts +370 -0
- package/src/services/__tests__/session-registration.service.test.ts +312 -0
- package/src/services/__tests__/storage.service.test.ts +358 -0
- package/src/services/__tests__/tool-context-builder.test.ts +438 -0
- package/src/services/access-control.service.ts +1048 -0
- package/src/services/authorization/authorization-registry.ts +67 -0
- package/src/services/authorization/types.ts +71 -0
- package/src/services/batch-delegation.service.ts +136 -0
- package/src/services/crypto.service.ts +296 -0
- package/src/services/delegation-error-formatter.ts +299 -0
- package/src/services/errors.ts +76 -0
- package/src/services/index.ts +34 -0
- package/src/services/oauth-config.service.d.ts +53 -0
- package/src/services/oauth-config.service.d.ts.map +1 -0
- package/src/services/oauth-config.service.js.map +1 -0
- package/src/services/oauth-config.service.ts +192 -0
- package/src/services/oauth-provider-registry.d.ts +57 -0
- package/src/services/oauth-provider-registry.d.ts.map +1 -0
- package/src/services/oauth-provider-registry.js.map +1 -0
- package/src/services/oauth-provider-registry.ts +141 -0
- package/src/services/oauth-service.ts +652 -0
- package/src/services/oauth-token-retrieval.service.ts +245 -0
- package/src/services/policy.service.ts +527 -0
- package/src/services/proof-verifier.ts +295 -0
- package/src/services/provider-resolver.d.ts +48 -0
- package/src/services/provider-resolver.d.ts.map +1 -0
- package/src/services/provider-resolver.js.map +1 -0
- package/src/services/provider-resolver.ts +236 -0
- package/src/services/provider-validator.ts +170 -0
- package/src/services/session-registration.service.ts +259 -0
- package/src/services/storage.service.ts +566 -0
- package/src/services/tool-context-builder.ts +285 -0
- package/src/services/tool-protection.service.ts +971 -0
- package/src/session/__tests__/session-manager.test.ts +327 -0
- package/src/session/index.ts +10 -0
- package/src/session/manager.ts +380 -0
- package/src/types/__tests__/oauth-required-error.test.ts +79 -0
- package/src/types/__tests__/tool-protection.test.ts +121 -0
- package/src/types/oauth-required-error.ts +71 -0
- package/src/types/tool-protection.ts +179 -0
- package/src/utils/__tests__/base58.test.ts +281 -0
- package/src/utils/__tests__/base64.test.ts +239 -0
- package/src/utils/__tests__/cors.test.ts +184 -0
- package/src/utils/__tests__/did-helpers.test.ts +156 -0
- package/src/utils/__tests__/storage-keys.test.ts +372 -0
- package/src/utils/base58.ts +109 -0
- package/src/utils/base64.ts +185 -0
- package/src/utils/cors.ts +93 -0
- package/src/utils/did-helpers.ts +229 -0
- package/src/utils/index.ts +8 -0
- package/src/utils/storage-keys.ts +285 -0
- package/src/wire-format.ts +114 -0
- package/tsconfig.json +21 -0
- package/vitest.config.mts +57 -0
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transport-aware Logger for MCP-I
|
|
3
|
+
*
|
|
4
|
+
* Provides a lightweight, dependency-free logging interface that:
|
|
5
|
+
* - Maps log levels correctly for Cloudflare Workers (so wrangler tail shows correct severities)
|
|
6
|
+
* - Routes all logs to stderr for stdio transport (so stdout remains protocol-only)
|
|
7
|
+
* - Supports runtime configuration of log level and transport mode
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export type Level = "debug" | "info" | "warn" | "error";
|
|
11
|
+
|
|
12
|
+
const SEVERITY: Record<Level, number> = {
|
|
13
|
+
debug: 10,
|
|
14
|
+
info: 20,
|
|
15
|
+
warn: 30,
|
|
16
|
+
error: 40,
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export interface Logger {
|
|
20
|
+
configure: (
|
|
21
|
+
opts?: { level?: Level; transport?: string; forceStderr?: boolean }
|
|
22
|
+
) => void;
|
|
23
|
+
debug: (...args: any[]) => void;
|
|
24
|
+
info: (...args: any[]) => void;
|
|
25
|
+
warn: (...args: any[]) => void;
|
|
26
|
+
error: (...args: any[]) => void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Create a default console logger with transport-aware behavior
|
|
31
|
+
*
|
|
32
|
+
* - Cloudflare Workers: Maps levels to console.debug/info/warn/error
|
|
33
|
+
* - stdio transport: Routes all levels to console.error (stderr) to keep stdout protocol-only
|
|
34
|
+
*/
|
|
35
|
+
export function createDefaultConsoleLogger(): Logger {
|
|
36
|
+
let level: Level = "info";
|
|
37
|
+
let forceStderr = false;
|
|
38
|
+
|
|
39
|
+
function shouldLog(l: Level): boolean {
|
|
40
|
+
return SEVERITY[l] >= SEVERITY[level];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function write(l: Level, ...args: any[]): void {
|
|
44
|
+
if (!shouldLog(l)) return;
|
|
45
|
+
|
|
46
|
+
if (forceStderr) {
|
|
47
|
+
// stdio transport: all logs go to stderr (console.error)
|
|
48
|
+
// This ensures stdout remains protocol-only
|
|
49
|
+
console.error(...args);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Cloudflare Workers: map to appropriate console method
|
|
54
|
+
switch (l) {
|
|
55
|
+
case "debug":
|
|
56
|
+
if (typeof console.debug === "function") {
|
|
57
|
+
console.debug(...args);
|
|
58
|
+
} else {
|
|
59
|
+
console.log(...args);
|
|
60
|
+
}
|
|
61
|
+
break;
|
|
62
|
+
case "info":
|
|
63
|
+
if (typeof console.info === "function") {
|
|
64
|
+
console.info(...args);
|
|
65
|
+
} else {
|
|
66
|
+
console.log(...args);
|
|
67
|
+
}
|
|
68
|
+
break;
|
|
69
|
+
case "warn":
|
|
70
|
+
if (typeof console.warn === "function") {
|
|
71
|
+
console.warn(...args);
|
|
72
|
+
} else {
|
|
73
|
+
console.error(...args);
|
|
74
|
+
}
|
|
75
|
+
break;
|
|
76
|
+
case "error":
|
|
77
|
+
console.error(...args);
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
configure(opts = {}) {
|
|
84
|
+
if (opts.level) level = opts.level;
|
|
85
|
+
|
|
86
|
+
// Handle transport-based stderr routing
|
|
87
|
+
// - stdio transport: force all logs to stderr (stdout is protocol-only)
|
|
88
|
+
// - sse/http transports: use normal console routing
|
|
89
|
+
// - explicit forceStderr overrides transport-based behavior
|
|
90
|
+
if (opts.forceStderr === true) {
|
|
91
|
+
forceStderr = true;
|
|
92
|
+
} else if (opts.forceStderr === false) {
|
|
93
|
+
forceStderr = false;
|
|
94
|
+
} else if (opts.transport === "stdio") {
|
|
95
|
+
forceStderr = true;
|
|
96
|
+
} else if (opts.transport === "sse" || opts.transport === "http") {
|
|
97
|
+
// Reset to normal console routing for non-stdio transports
|
|
98
|
+
forceStderr = false;
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
debug: (...a) => write("debug", ...a),
|
|
102
|
+
info: (...a) => write("info", ...a),
|
|
103
|
+
warn: (...a) => write("warn", ...a),
|
|
104
|
+
error: (...a) => write("error", ...a),
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Default singleton logger instance
|
|
110
|
+
* Configure via logger.configure() before use
|
|
111
|
+
*/
|
|
112
|
+
export const logger = createDefaultConsoleLogger();
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Crypto Parity Test Suite — Critical Safety Gate
|
|
3
|
+
*
|
|
4
|
+
* Verifies that the CryptoProvider.hash() abstraction produces byte-identical
|
|
5
|
+
* output to the original Node createHash("sha256") implementation.
|
|
6
|
+
*
|
|
7
|
+
* This test MUST pass 100% before merging Phase 2 to main.
|
|
8
|
+
* If output diverges, existing proofs fail verification in production.
|
|
9
|
+
*
|
|
10
|
+
* Per spec Section 5: Phase 2 — Parity Tests (Critical)
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { describe, it, expect } from "vitest";
|
|
14
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* We import the NodeCryptoProvider from mcp-i, NOT from mcp-i-core,
|
|
18
|
+
* because mcp-i-core is platform-agnostic and has no Node provider.
|
|
19
|
+
* These tests run in a Node environment, so the import is correct here.
|
|
20
|
+
*
|
|
21
|
+
* In the test environment, the mcp-i package is available as a workspace dep.
|
|
22
|
+
*/
|
|
23
|
+
import { NodeCryptoProvider } from "../../../../mcp-i/src/providers/node-providers";
|
|
24
|
+
|
|
25
|
+
const nodeCrypto = new NodeCryptoProvider();
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Reference hash using Node's built-in createHash.
|
|
29
|
+
* This is the "ground truth" format we must match exactly.
|
|
30
|
+
*/
|
|
31
|
+
function referenceHash(data: Uint8Array): string {
|
|
32
|
+
const hex = createHash("sha256").update(Buffer.from(data)).digest("hex");
|
|
33
|
+
return `sha256:${hex}`;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
describe("SHA-256 parity — NodeCryptoProvider vs Node createHash", () => {
|
|
37
|
+
it("should produce byte-identical output for 100 known string inputs", async () => {
|
|
38
|
+
const inputs = Array.from({ length: 100 }, (_, i) =>
|
|
39
|
+
new TextEncoder().encode(`test-input-${i}-${Math.random()}`)
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
for (const input of inputs) {
|
|
43
|
+
const expected = referenceHash(input);
|
|
44
|
+
const actual = await nodeCrypto.hash(input);
|
|
45
|
+
expect(actual).toBe(expected);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("should match format exactly as sha256:<hex>", async () => {
|
|
50
|
+
const data = new TextEncoder().encode("format-check");
|
|
51
|
+
const result = await nodeCrypto.hash(data);
|
|
52
|
+
|
|
53
|
+
expect(result).toMatch(/^sha256:[a-f0-9]{64}$/);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("should match reference for empty input edge case", async () => {
|
|
57
|
+
const data = new Uint8Array(0);
|
|
58
|
+
const expected = referenceHash(data);
|
|
59
|
+
const actual = await nodeCrypto.hash(data);
|
|
60
|
+
expect(actual).toBe(expected);
|
|
61
|
+
// SHA-256("") = e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
|
|
62
|
+
expect(actual).toBe(
|
|
63
|
+
"sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
|
|
64
|
+
);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("should match reference for large payload (>1MB)", async () => {
|
|
68
|
+
const data = randomBytes(1_100_000);
|
|
69
|
+
const expected = referenceHash(data);
|
|
70
|
+
const actual = await nodeCrypto.hash(data);
|
|
71
|
+
expect(actual).toBe(expected);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("should match reference for known fixed test vectors", async () => {
|
|
75
|
+
const vectors: Array<{ input: string; expected: string }> = [
|
|
76
|
+
{
|
|
77
|
+
input: "hello",
|
|
78
|
+
expected:
|
|
79
|
+
"sha256:2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824",
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
input: "world",
|
|
83
|
+
expected:
|
|
84
|
+
"sha256:486ea46224d1bb4fb680f34f7c9ad96a8f24ec88be73ea8e5a6c65260e9cb8a7",
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
input: "",
|
|
88
|
+
expected:
|
|
89
|
+
"sha256:e3b0c44298fc1c149afbf4c8996fb924" +
|
|
90
|
+
"27ae41e4649b934ca495991b7852b855",
|
|
91
|
+
},
|
|
92
|
+
];
|
|
93
|
+
|
|
94
|
+
for (const { input, expected } of vectors) {
|
|
95
|
+
const data = new TextEncoder().encode(input);
|
|
96
|
+
const actual = await nodeCrypto.hash(data);
|
|
97
|
+
expect(actual).toBe(expected);
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("should produce different hashes for different inputs (collision resistance)", async () => {
|
|
102
|
+
const hashes = new Set<string>();
|
|
103
|
+
for (let i = 0; i < 50; i++) {
|
|
104
|
+
const data = new TextEncoder().encode(`unique-${i}`);
|
|
105
|
+
const hash = await nodeCrypto.hash(data);
|
|
106
|
+
hashes.add(hash);
|
|
107
|
+
}
|
|
108
|
+
expect(hashes.size).toBe(50);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("should be deterministic — same input always produces same output", async () => {
|
|
112
|
+
const data = new TextEncoder().encode("determinism-check");
|
|
113
|
+
const results = await Promise.all(
|
|
114
|
+
Array.from({ length: 10 }, () => nodeCrypto.hash(data))
|
|
115
|
+
);
|
|
116
|
+
const unique = new Set(results);
|
|
117
|
+
expect(unique.size).toBe(1);
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
describe("randomBytes parity — NodeCryptoProvider", () => {
|
|
122
|
+
it("should produce the requested number of bytes", async () => {
|
|
123
|
+
for (const n of [1, 16, 32, 64, 128, 256]) {
|
|
124
|
+
const bytes = await nodeCrypto.randomBytes(n);
|
|
125
|
+
expect(bytes).toBeInstanceOf(Uint8Array);
|
|
126
|
+
expect(bytes.length).toBe(n);
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("should produce different output on each call (entropy check)", async () => {
|
|
131
|
+
const results = await Promise.all(
|
|
132
|
+
Array.from({ length: 10 }, () => nodeCrypto.randomBytes(32))
|
|
133
|
+
);
|
|
134
|
+
const hexResults = results.map((r) =>
|
|
135
|
+
Array.from(r)
|
|
136
|
+
.map((b) => b.toString(16).padStart(2, "0"))
|
|
137
|
+
.join("")
|
|
138
|
+
);
|
|
139
|
+
const unique = new Set(hexResults);
|
|
140
|
+
expect(unique.size).toBe(10);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it("should pass basic uniformity check for 1000 calls (chi-squared)", async () => {
|
|
144
|
+
// Generate 1000 single bytes and check distribution
|
|
145
|
+
const counts = new Array<number>(256).fill(0);
|
|
146
|
+
for (let i = 0; i < 1000; i++) {
|
|
147
|
+
const bytes = await nodeCrypto.randomBytes(1);
|
|
148
|
+
counts[bytes[0]]++;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Expected count per bucket: 1000/256 ≈ 3.9
|
|
152
|
+
// Chi-squared test: with df=255, chi-sq > 310 would be suspicious at p=0.01
|
|
153
|
+
// We use a lenient threshold: no single bucket should exceed 40 (10x expected)
|
|
154
|
+
const max = Math.max(...counts);
|
|
155
|
+
expect(max).toBeLessThan(40);
|
|
156
|
+
|
|
157
|
+
// And at least 100 different byte values should appear (high coverage)
|
|
158
|
+
const nonZero = counts.filter((c) => c > 0).length;
|
|
159
|
+
expect(nonZero).toBeGreaterThan(100);
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
describe("Ed25519 sign/verify roundtrip — cross-runtime compatibility", () => {
|
|
164
|
+
it("should generate and verify with the same provider", async () => {
|
|
165
|
+
const keyPair = await nodeCrypto.generateKeyPair();
|
|
166
|
+
const data = new TextEncoder().encode("cross-runtime-test");
|
|
167
|
+
|
|
168
|
+
const signature = await nodeCrypto.sign(data, keyPair.privateKey);
|
|
169
|
+
const isValid = await nodeCrypto.verify(
|
|
170
|
+
data,
|
|
171
|
+
signature,
|
|
172
|
+
keyPair.publicKey
|
|
173
|
+
);
|
|
174
|
+
|
|
175
|
+
expect(isValid).toBe(true);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it("should produce consistent signatures (Ed25519 is deterministic)", async () => {
|
|
179
|
+
const keyPair = await nodeCrypto.generateKeyPair();
|
|
180
|
+
const data = new TextEncoder().encode("deterministic-signing");
|
|
181
|
+
|
|
182
|
+
const sig1 = await nodeCrypto.sign(data, keyPair.privateKey);
|
|
183
|
+
const sig2 = await nodeCrypto.sign(data, keyPair.privateKey);
|
|
184
|
+
|
|
185
|
+
expect(sig1).toEqual(sig2);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it("should reject signature from a different key", async () => {
|
|
189
|
+
const keyPair1 = await nodeCrypto.generateKeyPair();
|
|
190
|
+
const keyPair2 = await nodeCrypto.generateKeyPair();
|
|
191
|
+
const data = new TextEncoder().encode("wrong-key-test");
|
|
192
|
+
|
|
193
|
+
const signature = await nodeCrypto.sign(data, keyPair1.privateKey);
|
|
194
|
+
const isValid = await nodeCrypto.verify(
|
|
195
|
+
data,
|
|
196
|
+
signature,
|
|
197
|
+
keyPair2.publicKey
|
|
198
|
+
);
|
|
199
|
+
|
|
200
|
+
expect(isValid).toBe(false);
|
|
201
|
+
});
|
|
202
|
+
});
|