@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,380 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session Management — Platform-agnostic Protocol Reference
|
|
3
|
+
*
|
|
4
|
+
* Handles handshake enforcement, session management, and nonce validation
|
|
5
|
+
* according to MCP-I requirements 4.5–4.9 and 19.1–19.2.
|
|
6
|
+
*
|
|
7
|
+
* Platform adapters inject a CryptoProvider for all random byte generation.
|
|
8
|
+
* The static generateNonce() uses globalThis.crypto (available Node 20+ and
|
|
9
|
+
* Cloudflare Workers) to remain synchronous without platform-specific imports.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
HandshakeRequest,
|
|
14
|
+
SessionContext,
|
|
15
|
+
NonceCache,
|
|
16
|
+
} from "@kya-os/contracts/handshake";
|
|
17
|
+
import { CryptoProvider } from "../providers/base";
|
|
18
|
+
import { MemoryNonceCacheProvider } from "../providers/memory";
|
|
19
|
+
import { logger } from "../logging";
|
|
20
|
+
import {
|
|
21
|
+
SESSION_ID_PREFIX,
|
|
22
|
+
LEGACY_SESSION_ID_PREFIX,
|
|
23
|
+
recordWireForm,
|
|
24
|
+
} from "../wire-format";
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Session management configuration
|
|
28
|
+
*/
|
|
29
|
+
export interface SessionConfig {
|
|
30
|
+
/** Clock-skew tolerance in seconds. Default: 120. */
|
|
31
|
+
timestampSkewSeconds?: number;
|
|
32
|
+
/** Idle session TTL in minutes. Default: 30. */
|
|
33
|
+
sessionTtlMinutes?: number;
|
|
34
|
+
/** Optional absolute session lifetime in minutes. */
|
|
35
|
+
absoluteSessionLifetime?: number;
|
|
36
|
+
/** Nonce cache implementation. Default: MemoryNonceCacheProvider. */
|
|
37
|
+
nonceCache?: NonceCache;
|
|
38
|
+
/** Optional server DID to include in session context. */
|
|
39
|
+
serverDid?: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Handshake validation result
|
|
44
|
+
*/
|
|
45
|
+
export interface HandshakeResult {
|
|
46
|
+
success: boolean;
|
|
47
|
+
session?: SessionContext;
|
|
48
|
+
error?: {
|
|
49
|
+
code: string;
|
|
50
|
+
message: string;
|
|
51
|
+
remediation?: string;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Platform-agnostic session manager.
|
|
57
|
+
* Requires a CryptoProvider for session ID and client ID generation.
|
|
58
|
+
*/
|
|
59
|
+
export class SessionManager {
|
|
60
|
+
private config: Required<
|
|
61
|
+
Omit<SessionConfig, "absoluteSessionLifetime" | "serverDid">
|
|
62
|
+
> & {
|
|
63
|
+
absoluteSessionLifetime?: number;
|
|
64
|
+
serverDid?: string;
|
|
65
|
+
};
|
|
66
|
+
private cryptoProvider: CryptoProvider;
|
|
67
|
+
private sessions = new Map<string, SessionContext>();
|
|
68
|
+
|
|
69
|
+
constructor(cryptoProvider: CryptoProvider, config: SessionConfig = {}) {
|
|
70
|
+
this.cryptoProvider = cryptoProvider;
|
|
71
|
+
this.config = {
|
|
72
|
+
timestampSkewSeconds: config.timestampSkewSeconds ?? 120,
|
|
73
|
+
sessionTtlMinutes: config.sessionTtlMinutes ?? 30,
|
|
74
|
+
nonceCache: config.nonceCache ?? new MemoryNonceCacheProvider(),
|
|
75
|
+
...(config.absoluteSessionLifetime !== undefined && {
|
|
76
|
+
absoluteSessionLifetime: config.absoluteSessionLifetime,
|
|
77
|
+
}),
|
|
78
|
+
...(config.serverDid !== undefined && { serverDid: config.serverDid }),
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
if (this.config.nonceCache instanceof MemoryNonceCacheProvider) {
|
|
82
|
+
logger.warn(
|
|
83
|
+
"[SessionManager] Using MemoryNonceCacheProvider — not suitable for " +
|
|
84
|
+
"multi-instance deployments. Use Redis, DynamoDB, or Cloudflare KV " +
|
|
85
|
+
"for production."
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Set server DID for session creation (called after identity is loaded).
|
|
92
|
+
*/
|
|
93
|
+
setServerDid(serverDid: string): void {
|
|
94
|
+
this.config.serverDid = serverDid;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Validate handshake and create or retrieve session.
|
|
99
|
+
* Requirements: 4.5, 4.6, 4.7, 4.8, 4.9
|
|
100
|
+
*/
|
|
101
|
+
async validateHandshake(request: HandshakeRequest): Promise<HandshakeResult> {
|
|
102
|
+
try {
|
|
103
|
+
const now = Math.floor(Date.now() / 1000);
|
|
104
|
+
const timeDiff = Math.abs(now - request.timestamp);
|
|
105
|
+
|
|
106
|
+
if (timeDiff > this.config.timestampSkewSeconds) {
|
|
107
|
+
return {
|
|
108
|
+
success: false,
|
|
109
|
+
error: {
|
|
110
|
+
code: "XMCP_I_EHANDSHAKE",
|
|
111
|
+
message: `Timestamp outside acceptable range (±${this.config.timestampSkewSeconds}s)`,
|
|
112
|
+
remediation: `Check NTP sync on client and server. Current server time: ${now}, received: ${request.timestamp}, diff: ${timeDiff}s. Adjust timestampSkewSeconds if needed.`,
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const nonceExists = await this.config.nonceCache.has(
|
|
118
|
+
request.nonce,
|
|
119
|
+
request.agentDid
|
|
120
|
+
);
|
|
121
|
+
if (nonceExists) {
|
|
122
|
+
return {
|
|
123
|
+
success: false,
|
|
124
|
+
error: {
|
|
125
|
+
code: "XMCP_I_EHANDSHAKE",
|
|
126
|
+
message: "Nonce already used (replay attack prevention)",
|
|
127
|
+
remediation: "Generate a new unique nonce for each request",
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const nonceTtlSeconds = this.config.sessionTtlMinutes * 60 + 60;
|
|
133
|
+
await this.config.nonceCache.add(
|
|
134
|
+
request.nonce,
|
|
135
|
+
nonceTtlSeconds,
|
|
136
|
+
request.agentDid
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
const sessionId = await this.generateSessionId();
|
|
140
|
+
const clientInfo = await this.buildClientInfo(request);
|
|
141
|
+
|
|
142
|
+
const session: SessionContext = {
|
|
143
|
+
sessionId,
|
|
144
|
+
audience: request.audience,
|
|
145
|
+
nonce: request.nonce,
|
|
146
|
+
timestamp: request.timestamp,
|
|
147
|
+
createdAt: now,
|
|
148
|
+
lastActivity: now,
|
|
149
|
+
ttlMinutes: this.config.sessionTtlMinutes,
|
|
150
|
+
identityState: "anonymous",
|
|
151
|
+
agentDid: request.agentDid,
|
|
152
|
+
...(this.config.serverDid && { serverDid: this.config.serverDid }),
|
|
153
|
+
...(clientInfo && { clientInfo }),
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
this.sessions.set(sessionId, session);
|
|
157
|
+
|
|
158
|
+
return { success: true, session };
|
|
159
|
+
} catch (error) {
|
|
160
|
+
return {
|
|
161
|
+
success: false,
|
|
162
|
+
error: {
|
|
163
|
+
code: "XMCP_I_EHANDSHAKE",
|
|
164
|
+
message: `Handshake validation failed: ${error instanceof Error ? error.message : "Unknown error"}`,
|
|
165
|
+
},
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Get session by ID and update last activity.
|
|
172
|
+
*/
|
|
173
|
+
async getSession(sessionId: string): Promise<SessionContext | null> {
|
|
174
|
+
// C4 (#2918) dual-accept telemetry: a client presenting a pre-flip
|
|
175
|
+
// `mcpi_…` id is the (informational) old-form session signal; it drains
|
|
176
|
+
// within a session TTL. The lookup itself stays prefix-agnostic.
|
|
177
|
+
if (sessionId.startsWith(LEGACY_SESSION_ID_PREFIX)) {
|
|
178
|
+
recordWireForm("session_id", "old");
|
|
179
|
+
}
|
|
180
|
+
const session = this.sessions.get(sessionId);
|
|
181
|
+
if (!session) return null;
|
|
182
|
+
|
|
183
|
+
const now = Math.floor(Date.now() / 1000);
|
|
184
|
+
const idleTimeSeconds = now - session.lastActivity;
|
|
185
|
+
const maxIdleSeconds = session.ttlMinutes * 60;
|
|
186
|
+
|
|
187
|
+
if (idleTimeSeconds > maxIdleSeconds) {
|
|
188
|
+
this.sessions.delete(sessionId);
|
|
189
|
+
return null;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (this.config.absoluteSessionLifetime !== undefined) {
|
|
193
|
+
const sessionAgeSeconds = now - session.createdAt;
|
|
194
|
+
const maxAgeSeconds = this.config.absoluteSessionLifetime * 60;
|
|
195
|
+
if (sessionAgeSeconds > maxAgeSeconds) {
|
|
196
|
+
this.sessions.delete(sessionId);
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
session.lastActivity = now;
|
|
202
|
+
this.sessions.set(sessionId, session);
|
|
203
|
+
return session;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Generate a unique session ID.
|
|
208
|
+
*
|
|
209
|
+
* C4 (#2918) dual-accept window: emits the canonical `kyaos_` prefix
|
|
210
|
+
* ({@link SESSION_ID_PREFIX}). Reads stay lenient — a client still
|
|
211
|
+
* presenting a pre-flip `mcpi_…` id resolves unchanged (the store keys on
|
|
212
|
+
* the full opaque id) and old ids drain within a session TTL.
|
|
213
|
+
* Uses injected CryptoProvider for platform-agnostic random bytes.
|
|
214
|
+
*/
|
|
215
|
+
private async generateSessionId(): Promise<string> {
|
|
216
|
+
const bytes = await this.cryptoProvider.randomBytes(16);
|
|
217
|
+
// Apply UUID v4 version and variant bits
|
|
218
|
+
bytes[6] = (bytes[6] & 0x0f) | 0x40;
|
|
219
|
+
bytes[8] = (bytes[8] & 0x3f) | 0x80;
|
|
220
|
+
const hex = Array.from(bytes)
|
|
221
|
+
.map((b) => b.toString(16).padStart(2, "0"))
|
|
222
|
+
.join("");
|
|
223
|
+
const uuid = `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20, 32)}`;
|
|
224
|
+
return `${SESSION_ID_PREFIX}${uuid}`;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Generate a deterministic client identifier.
|
|
229
|
+
*/
|
|
230
|
+
private async generateClientId(): Promise<string> {
|
|
231
|
+
const bytes = await this.cryptoProvider.randomBytes(6);
|
|
232
|
+
const hex = Array.from(bytes)
|
|
233
|
+
.map((b) => b.toString(16).padStart(2, "0"))
|
|
234
|
+
.join("");
|
|
235
|
+
return `client_${hex}`;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Normalize string fields from handshake metadata.
|
|
240
|
+
*/
|
|
241
|
+
private normalizeClientInfoString(value: unknown): string | undefined {
|
|
242
|
+
if (typeof value !== "string") return undefined;
|
|
243
|
+
const trimmed = value.trim();
|
|
244
|
+
return trimmed.length > 0 ? trimmed : undefined;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Build MCP client metadata for the session.
|
|
249
|
+
*/
|
|
250
|
+
private async buildClientInfo(
|
|
251
|
+
request: HandshakeRequest
|
|
252
|
+
): Promise<SessionContext["clientInfo"] | undefined> {
|
|
253
|
+
const hasMetadata =
|
|
254
|
+
!!request.clientInfo ||
|
|
255
|
+
typeof request.clientProtocolVersion === "string" ||
|
|
256
|
+
request.clientCapabilities !== undefined;
|
|
257
|
+
|
|
258
|
+
if (!hasMetadata) return undefined;
|
|
259
|
+
|
|
260
|
+
const source = request.clientInfo;
|
|
261
|
+
|
|
262
|
+
return {
|
|
263
|
+
name: this.normalizeClientInfoString(source?.name) ?? "unknown",
|
|
264
|
+
title: this.normalizeClientInfoString(source?.title),
|
|
265
|
+
version: this.normalizeClientInfoString(source?.version),
|
|
266
|
+
platform: this.normalizeClientInfoString(source?.platform),
|
|
267
|
+
vendor: this.normalizeClientInfoString(source?.vendor),
|
|
268
|
+
persistentId: this.normalizeClientInfoString(source?.persistentId),
|
|
269
|
+
clientId:
|
|
270
|
+
this.normalizeClientInfoString(source?.clientId) ??
|
|
271
|
+
(await this.generateClientId()),
|
|
272
|
+
protocolVersion: this.normalizeClientInfoString(
|
|
273
|
+
request.clientProtocolVersion
|
|
274
|
+
),
|
|
275
|
+
capabilities: request.clientCapabilities,
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Generate a cryptographically secure nonce.
|
|
281
|
+
* Uses globalThis.crypto (sync Web Crypto — available in Node 20+ and Workers).
|
|
282
|
+
*/
|
|
283
|
+
static generateNonce(): string {
|
|
284
|
+
const buffer = new Uint8Array(16);
|
|
285
|
+
globalThis.crypto.getRandomValues(buffer);
|
|
286
|
+
let binaryStr = "";
|
|
287
|
+
for (let i = 0; i < buffer.length; i++) {
|
|
288
|
+
binaryStr += String.fromCharCode(buffer[i]);
|
|
289
|
+
}
|
|
290
|
+
return btoa(binaryStr)
|
|
291
|
+
.replace(/\+/g, "-")
|
|
292
|
+
.replace(/\//g, "_")
|
|
293
|
+
.replace(/=/g, "");
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Cleanup expired sessions and nonces.
|
|
298
|
+
*/
|
|
299
|
+
async cleanup(): Promise<void> {
|
|
300
|
+
const now = Math.floor(Date.now() / 1000);
|
|
301
|
+
|
|
302
|
+
for (const [sessionId, session] of this.sessions.entries()) {
|
|
303
|
+
const idleTimeSeconds = now - session.lastActivity;
|
|
304
|
+
const maxIdleSeconds = session.ttlMinutes * 60;
|
|
305
|
+
let expired = idleTimeSeconds > maxIdleSeconds;
|
|
306
|
+
|
|
307
|
+
if (!expired && this.config.absoluteSessionLifetime !== undefined) {
|
|
308
|
+
const sessionAgeSeconds = now - session.createdAt;
|
|
309
|
+
const maxAgeSeconds = this.config.absoluteSessionLifetime * 60;
|
|
310
|
+
expired = sessionAgeSeconds > maxAgeSeconds;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
if (expired) {
|
|
314
|
+
this.sessions.delete(sessionId);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
await this.config.nonceCache.cleanup();
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Get session statistics.
|
|
323
|
+
*/
|
|
324
|
+
getStats(): {
|
|
325
|
+
activeSessions: number;
|
|
326
|
+
config: {
|
|
327
|
+
timestampSkewSeconds: number;
|
|
328
|
+
sessionTtlMinutes: number;
|
|
329
|
+
absoluteSessionLifetime?: number;
|
|
330
|
+
cacheType: string;
|
|
331
|
+
};
|
|
332
|
+
} {
|
|
333
|
+
return {
|
|
334
|
+
activeSessions: this.sessions.size,
|
|
335
|
+
config: {
|
|
336
|
+
timestampSkewSeconds: this.config.timestampSkewSeconds,
|
|
337
|
+
sessionTtlMinutes: this.config.sessionTtlMinutes,
|
|
338
|
+
absoluteSessionLifetime: this.config.absoluteSessionLifetime,
|
|
339
|
+
cacheType: this.config.nonceCache.constructor.name,
|
|
340
|
+
},
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Clear all sessions (useful for testing).
|
|
346
|
+
*/
|
|
347
|
+
clearSessions(): void {
|
|
348
|
+
this.sessions.clear();
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Create a handshake request with a secure nonce.
|
|
354
|
+
*/
|
|
355
|
+
export function createHandshakeRequest(audience: string): HandshakeRequest {
|
|
356
|
+
return {
|
|
357
|
+
nonce: SessionManager.generateNonce(),
|
|
358
|
+
audience,
|
|
359
|
+
timestamp: Math.floor(Date.now() / 1000),
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Validate handshake request format.
|
|
365
|
+
*/
|
|
366
|
+
export function validateHandshakeFormat(
|
|
367
|
+
request: unknown
|
|
368
|
+
): request is HandshakeRequest {
|
|
369
|
+
return (
|
|
370
|
+
typeof request === "object" &&
|
|
371
|
+
request !== null &&
|
|
372
|
+
typeof (request as Record<string, unknown>).nonce === "string" &&
|
|
373
|
+
((request as Record<string, unknown>).nonce as string).length > 0 &&
|
|
374
|
+
typeof (request as Record<string, unknown>).audience === "string" &&
|
|
375
|
+
((request as Record<string, unknown>).audience as string).length > 0 &&
|
|
376
|
+
typeof (request as Record<string, unknown>).timestamp === "number" &&
|
|
377
|
+
((request as Record<string, unknown>).timestamp as number) > 0 &&
|
|
378
|
+
Number.isInteger((request as Record<string, unknown>).timestamp)
|
|
379
|
+
);
|
|
380
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for OAuthRequiredError
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { describe, it, expect } from "vitest";
|
|
6
|
+
import { OAuthRequiredError } from "../oauth-required-error.js";
|
|
7
|
+
|
|
8
|
+
describe("OAuthRequiredError", () => {
|
|
9
|
+
it("should create error with required fields", () => {
|
|
10
|
+
const error = new OAuthRequiredError({
|
|
11
|
+
toolName: "checkout",
|
|
12
|
+
requiredScopes: ["cart:write", "payment:process"],
|
|
13
|
+
provider: "github",
|
|
14
|
+
oauthUrl: "https://github.com/login/oauth/authorize",
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
expect(error).toBeInstanceOf(Error);
|
|
18
|
+
expect(error.name).toBe("OAuthRequiredError");
|
|
19
|
+
expect(error.toolName).toBe("checkout");
|
|
20
|
+
expect(error.requiredScopes).toEqual(["cart:write", "payment:process"]);
|
|
21
|
+
expect(error.provider).toBe("github");
|
|
22
|
+
expect(error.oauthUrl).toBe("https://github.com/login/oauth/authorize");
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("should include optional fields when provided", () => {
|
|
26
|
+
const error = new OAuthRequiredError({
|
|
27
|
+
toolName: "checkout",
|
|
28
|
+
requiredScopes: ["cart:write"],
|
|
29
|
+
provider: "github",
|
|
30
|
+
oauthUrl: "https://github.com/login/oauth/authorize",
|
|
31
|
+
resumeToken: "resume-token-123",
|
|
32
|
+
userDid: "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK",
|
|
33
|
+
sessionId: "session-456",
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
expect(error.resumeToken).toBe("resume-token-123");
|
|
37
|
+
expect(error.userDid).toBe(
|
|
38
|
+
"did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK"
|
|
39
|
+
);
|
|
40
|
+
expect(error.sessionId).toBe("session-456");
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("should have descriptive error message", () => {
|
|
44
|
+
const error = new OAuthRequiredError({
|
|
45
|
+
toolName: "checkout",
|
|
46
|
+
requiredScopes: ["cart:write", "payment:process"],
|
|
47
|
+
provider: "github",
|
|
48
|
+
oauthUrl: "https://github.com/login/oauth/authorize",
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
expect(error.message).toContain('OAuth required for tool "checkout"');
|
|
52
|
+
expect(error.message).toContain('provider "github"');
|
|
53
|
+
expect(error.message).toContain("cart:write");
|
|
54
|
+
expect(error.message).toContain("payment:process");
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("should handle single scope", () => {
|
|
58
|
+
const error = new OAuthRequiredError({
|
|
59
|
+
toolName: "read-repo",
|
|
60
|
+
requiredScopes: ["repo:read"],
|
|
61
|
+
provider: "github",
|
|
62
|
+
oauthUrl: "https://github.com/login/oauth/authorize",
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
expect(error.message).toContain("repo:read");
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("should handle empty scopes array", () => {
|
|
69
|
+
const error = new OAuthRequiredError({
|
|
70
|
+
toolName: "public-tool",
|
|
71
|
+
requiredScopes: [],
|
|
72
|
+
provider: "github",
|
|
73
|
+
oauthUrl: "https://github.com/login/oauth/authorize",
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
expect(error.requiredScopes).toEqual([]);
|
|
77
|
+
expect(error.message).toContain("Required scopes:");
|
|
78
|
+
});
|
|
79
|
+
});
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for Tool Protection Types
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { describe, it, expect } from "vitest";
|
|
6
|
+
import {
|
|
7
|
+
DelegationRequiredError,
|
|
8
|
+
ToolProtectionServiceError,
|
|
9
|
+
type InterceptedToolCall,
|
|
10
|
+
} from "../tool-protection.js";
|
|
11
|
+
|
|
12
|
+
describe("DelegationRequiredError", () => {
|
|
13
|
+
it("should create error with required fields", () => {
|
|
14
|
+
const error = new DelegationRequiredError(
|
|
15
|
+
"checkout",
|
|
16
|
+
["cart:write", "payment:process"],
|
|
17
|
+
"https://consent.example.com/authorize"
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
expect(error).toBeInstanceOf(Error);
|
|
21
|
+
expect(error.name).toBe("DelegationRequiredError");
|
|
22
|
+
expect(error.toolName).toBe("checkout");
|
|
23
|
+
expect(error.requiredScopes).toEqual(["cart:write", "payment:process"]);
|
|
24
|
+
expect(error.consentUrl).toBe("https://consent.example.com/authorize");
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("should include optional intercepted call", () => {
|
|
28
|
+
const interceptedCall: InterceptedToolCall = {
|
|
29
|
+
toolName: "checkout",
|
|
30
|
+
args: { cartId: "cart-123" },
|
|
31
|
+
sessionId: "session-456",
|
|
32
|
+
timestamp: Date.now(),
|
|
33
|
+
expiresAt: Date.now() + 300000,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const error = new DelegationRequiredError(
|
|
37
|
+
"checkout",
|
|
38
|
+
["cart:write"],
|
|
39
|
+
"https://consent.example.com/authorize",
|
|
40
|
+
interceptedCall
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
expect(error.interceptedCall).toEqual(interceptedCall);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("should include optional resume token", () => {
|
|
47
|
+
const error = new DelegationRequiredError(
|
|
48
|
+
"checkout",
|
|
49
|
+
["cart:write"],
|
|
50
|
+
"https://consent.example.com/authorize",
|
|
51
|
+
undefined,
|
|
52
|
+
"resume-token-123"
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
expect(error.resumeToken).toBe("resume-token-123");
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("should include both intercepted call and resume token", () => {
|
|
59
|
+
const interceptedCall: InterceptedToolCall = {
|
|
60
|
+
toolName: "checkout",
|
|
61
|
+
args: { cartId: "cart-123" },
|
|
62
|
+
sessionId: "session-456",
|
|
63
|
+
timestamp: Date.now(),
|
|
64
|
+
expiresAt: Date.now() + 300000,
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const error = new DelegationRequiredError(
|
|
68
|
+
"checkout",
|
|
69
|
+
["cart:write"],
|
|
70
|
+
"https://consent.example.com/authorize",
|
|
71
|
+
interceptedCall,
|
|
72
|
+
"resume-token-123"
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
expect(error.interceptedCall).toEqual(interceptedCall);
|
|
76
|
+
expect(error.resumeToken).toBe("resume-token-123");
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("should have descriptive error message", () => {
|
|
80
|
+
const error = new DelegationRequiredError(
|
|
81
|
+
"checkout",
|
|
82
|
+
["cart:write"],
|
|
83
|
+
"https://consent.example.com/authorize"
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
expect(error.message).toBe('Delegation required for tool "checkout"');
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("should handle empty scopes array", () => {
|
|
90
|
+
const error = new DelegationRequiredError(
|
|
91
|
+
"public-tool",
|
|
92
|
+
[],
|
|
93
|
+
"https://consent.example.com/authorize"
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
expect(error.requiredScopes).toEqual([]);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
describe("ToolProtectionServiceError", () => {
|
|
101
|
+
it("should create error with message", () => {
|
|
102
|
+
const error = new ToolProtectionServiceError("Service unavailable");
|
|
103
|
+
|
|
104
|
+
expect(error).toBeInstanceOf(Error);
|
|
105
|
+
expect(error.name).toBe("ToolProtectionServiceError");
|
|
106
|
+
expect(error.message).toBe("Service unavailable");
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("should include optional cause", () => {
|
|
110
|
+
const cause = new Error("Network error");
|
|
111
|
+
const error = new ToolProtectionServiceError("Service unavailable", cause);
|
|
112
|
+
|
|
113
|
+
expect(error.cause).toBe(cause);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("should handle error without cause", () => {
|
|
117
|
+
const error = new ToolProtectionServiceError("Service unavailable");
|
|
118
|
+
|
|
119
|
+
expect(error.cause).toBeUndefined();
|
|
120
|
+
});
|
|
121
|
+
});
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OAuth Required Error
|
|
3
|
+
*
|
|
4
|
+
* Error thrown when a tool requires an IDP token (OAuth) but none is available.
|
|
5
|
+
* This triggers the OAuth flow to obtain the necessary token.
|
|
6
|
+
*
|
|
7
|
+
* @package @kya-os/mcp-i-core
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export interface OAuthRequiredErrorOptions {
|
|
11
|
+
/** Name of the tool requiring OAuth */
|
|
12
|
+
toolName: string;
|
|
13
|
+
|
|
14
|
+
/** Required OAuth scopes */
|
|
15
|
+
requiredScopes: string[];
|
|
16
|
+
|
|
17
|
+
/** OAuth provider name (e.g., "github", "google") */
|
|
18
|
+
provider: string;
|
|
19
|
+
|
|
20
|
+
/** OAuth authorization URL */
|
|
21
|
+
oauthUrl: string;
|
|
22
|
+
|
|
23
|
+
/** Optional resume token for continuing after OAuth */
|
|
24
|
+
resumeToken?: string;
|
|
25
|
+
|
|
26
|
+
/** Optional user DID */
|
|
27
|
+
userDid?: string;
|
|
28
|
+
|
|
29
|
+
/** Optional session ID */
|
|
30
|
+
sessionId?: string;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Auth type from tool protection config (e.g., "password", "oauth")
|
|
34
|
+
* Used to determine correct consent flow when catching the error
|
|
35
|
+
*/
|
|
36
|
+
authType?: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Error thrown when a tool requires OAuth but IDP token is not available
|
|
41
|
+
*
|
|
42
|
+
* This error should trigger the OAuth flow to obtain the necessary IDP token.
|
|
43
|
+
*/
|
|
44
|
+
export class OAuthRequiredError extends Error {
|
|
45
|
+
public readonly toolName: string;
|
|
46
|
+
public readonly requiredScopes: string[];
|
|
47
|
+
public readonly provider: string;
|
|
48
|
+
public readonly oauthUrl: string;
|
|
49
|
+
public readonly resumeToken?: string;
|
|
50
|
+
public readonly userDid?: string;
|
|
51
|
+
public readonly sessionId?: string;
|
|
52
|
+
public readonly authType?: string;
|
|
53
|
+
|
|
54
|
+
constructor(options: OAuthRequiredErrorOptions) {
|
|
55
|
+
const { toolName, requiredScopes, provider, oauthUrl } = options;
|
|
56
|
+
super(
|
|
57
|
+
`OAuth required for tool "${toolName}" with provider "${provider}". ` +
|
|
58
|
+
`Required scopes: ${requiredScopes.join(", ")}`
|
|
59
|
+
);
|
|
60
|
+
this.name = "OAuthRequiredError";
|
|
61
|
+
this.toolName = toolName;
|
|
62
|
+
this.requiredScopes = requiredScopes;
|
|
63
|
+
this.provider = provider;
|
|
64
|
+
this.oauthUrl = oauthUrl;
|
|
65
|
+
this.resumeToken = options.resumeToken;
|
|
66
|
+
this.userDid = options.userDid;
|
|
67
|
+
this.sessionId = options.sessionId;
|
|
68
|
+
this.authType = options.authType;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|