@kya-os/mcp-i
Version:
The TypeScript MCP framework with identity features built-in
89 lines (88 loc) • 3.31 kB
TypeScript
/**
* 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.
*/
import type { SessionContext } from '@kya-os/contracts/handshake';
import type { Ed25519PrivateJWK } from '@kya-os/mcp-i-core';
export interface RequestContext {
session?: SessionContext;
requestId?: string;
startTime?: number;
/** Delegation VC credential ID (set after delegation verification succeeds) */
delegationRef?: string;
/** Reference chain string — format: vc_id_1>del_id_1>... */
delegationChain?: string;
/** Scopes granted by the active delegation */
delegationScopes?: string[];
/** JWS-compact DelegationCredential VC for the active delegation (emitted on KYA-OS-Delegation-Credential when present) */
delegationCredential?: string;
/** Agent Ed25519 private key JWK for delegation proof signing */
delegationPrivateKeyJwk?: Ed25519PrivateJWK;
/** Key ID used in delegation proof JWT header */
delegationAgentKid?: string;
}
/**
* Run a function with request context
*/
export declare function runWithContext<T>(context: RequestContext, fn: () => T | Promise<T>): T | Promise<T>;
/**
* Get the current request context
*/
export declare function getContext(): RequestContext | undefined;
/**
* Get the current session from context
*/
export declare function getCurrentSession(): SessionContext | undefined;
/**
* Get the current agent DID from session
*/
export declare function getCurrentAgentDid(): string | undefined;
/**
* Set session in current context
*
* NOTE: This only works if called within a runWithContext block
*/
export declare function setSession(session: SessionContext): void;
/**
* Update delegation context fields in the current request context.
* Called after delegation is verified for a protected tool call.
*
* NOTE: This only works if called within a runWithContext block
*/
export declare function setDelegationContext(fields: {
delegationRef: string;
delegationChain: string;
delegationScopes: string[];
delegationCredential?: string;
delegationPrivateKeyJwk?: Ed25519PrivateJWK;
delegationAgentKid?: string;
}): void;
/**
* Convenience helper that wires delegation metadata from a verified delegation
* result and agent identity into AsyncLocalStorage in a single call.
*
* Avoids duplicating the base64-to-base64url JWK conversion and the
* setDelegationContext call across mcpi-runtime.ts and utils/tools.ts.
*/
export declare function setDelegationContextFromIdentity(params: {
delegationId: string;
delegationChain: string;
delegationScopes: string[];
/**
* JWS-compact DelegationCredential VC for the active delegation. Threaded
* through to KYA-OS-Delegation-Credential on outbound requests. Sourced
* from the verified delegation result (e.g. credential.credential_jwt).
* Omit when the verifier did not return a presentable VC.
*/
delegationCredential?: string;
identity: {
privateKey: string;
publicKey: string;
kid: string;
} | null | undefined;
}): void;