@kya-os/mcp-i
Version:
The TypeScript MCP framework with identity features built-in
57 lines (56 loc) • 1.54 kB
JavaScript
;
/**
* Request Context Management
*
* Uses AsyncLocalStorage to track request-scoped data like session information
* across async operations without needing to pass it through every function.
*
* This enables the compiler path to access session data (like agentDid) that
* was set during handshake processing.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.runWithContext = runWithContext;
exports.getContext = getContext;
exports.getCurrentSession = getCurrentSession;
exports.getCurrentAgentDid = getCurrentAgentDid;
exports.setSession = setSession;
const async_hooks_1 = require("async_hooks");
const asyncLocalStorage = new async_hooks_1.AsyncLocalStorage();
/**
* Run a function with request context
*/
function runWithContext(context, fn) {
return asyncLocalStorage.run(context, fn);
}
/**
* Get the current request context
*/
function getContext() {
return asyncLocalStorage.getStore();
}
/**
* Get the current session from context
*/
function getCurrentSession() {
return getContext()?.session;
}
/**
* Get the current agent DID from session
*/
function getCurrentAgentDid() {
return getCurrentSession()?.agentDid;
}
/**
* Set session in current context
*
* NOTE: This only works if called within a runWithContext block
*/
function setSession(session) {
const context = getContext();
if (context) {
context.session = session;
}
else {
console.warn('[RequestContext] Attempted to set session outside of request context');
}
}