xmcp
Version:
The TypeScript MCP framework
36 lines (35 loc) • 1.08 kB
TypeScript
type DefaultContext = Object;
type GetContext<T extends DefaultContext> = () => T;
type SetContext<T extends DefaultContext> = (data: Partial<T>) => void;
export interface Context<T extends DefaultContext> {
/** Run a callback with a specific context. */
provider: <R>(initialValue: T, callback: () => R) => R;
/** Get the context. */
getContext: GetContext<T>;
/** Partially update the context. */
setContext: SetContext<T>;
}
interface CreateContextOptions {
name: string;
/** Allow reads outside the async scope to use the last provided value. */
fallback?: boolean;
}
/**
* Create context allows you to create scoped variables for functions.
* Similar to React's context API.
* Usage:
* ```ts
* interface MyContext {
* value: string;
* }
*
* const context = createContext({ name: "my-context" });
*
* context.provider({
* value: "hello",
* }, () => {
* // Do something with the context
* })```
*/
export declare function createContext<T extends Object>({ name, fallback, }: CreateContextOptions): Context<T>;
export {};