UNPKG

@kya-os/mcp-i

Version:

The TypeScript MCP framework with identity features built-in

141 lines (140 loc) 3.62 kB
"use strict"; /** * Global Tool Protection Registry * * Provides a global registry for tool protection configuration that can be * accessed from compiled bundle code where the MCPIRuntime instance is not directly available. * * This is used by the compiler-generated tool handlers to check if a tool requires delegation * BEFORE executing the handler function. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.toolProtectionRegistry = void 0; exports.isToolProtected = isToolProtected; exports.getToolProtection = getToolProtection; /** * Global registry for tool protection configuration */ class ToolProtectionRegistry { config = new Map(); delegationVerifier = null; authConfig = null; debug = false; currentSession = null; /** * Register tool protection configuration */ register(toolName, config) { this.config.set(toolName, config); if (this.debug) { console.error(`[ToolProtectionRegistry] Registered protection for tool: ${toolName}`, config); } } /** * Bulk register multiple tool protections */ registerAll(configs) { for (const [toolName, config] of Object.entries(configs)) { this.register(toolName, config); } } /** * Get tool protection configuration */ get(toolName) { return this.config.get(toolName) || null; } /** * Check if a tool requires delegation */ requiresDelegation(toolName) { const config = this.get(toolName); return config?.requiresDelegation || false; } /** * Get required scopes for a tool */ getRequiredScopes(toolName) { const config = this.get(toolName); return config?.requiredScopes || []; } /** * Set delegation verifier (for runtime verification) */ setDelegationVerifier(verifier) { this.delegationVerifier = verifier; } /** * Set auth config (for authorization handshake) */ setAuthConfig(config) { this.authConfig = config; } /** * Get delegation verifier */ getDelegationVerifier() { return this.delegationVerifier; } /** * Get auth config */ getAuthConfig() { return this.authConfig; } /** * Enable debug logging */ setDebug(enabled) { this.debug = enabled; } /** * Set current session for tool execution context */ setCurrentSession(session) { this.currentSession = session; } /** * Get current session (for extracting client DID during tool execution) */ getCurrentSession() { return this.currentSession; } /** * Get current agent DID from session */ getCurrentAgentDid() { return this.currentSession?.agentDid || null; } /** * Clear all registrations */ clear() { this.config.clear(); this.delegationVerifier = null; this.authConfig = null; this.currentSession = null; } /** * Get all registered tool names */ getProtectedTools() { return Array.from(this.config.keys()); } } /** * Global singleton instance */ exports.toolProtectionRegistry = new ToolProtectionRegistry(); /** * Helper to check if a tool is protected */ function isToolProtected(toolName) { return exports.toolProtectionRegistry.requiresDelegation(toolName); } /** * Helper to get tool protection config */ function getToolProtection(toolName) { return exports.toolProtectionRegistry.get(toolName); }