mcp-use
Version:
Opinionated MCP Framework for TypeScript (@modelcontextprotocol/sdk compatible) - Build MCP Agents, Clients and Servers with support for ChatGPT Apps, Code Mode, OAuth, Notifications, Sampling, Observability and more.
133 lines • 5.73 kB
TypeScript
/**
* Tool Execution Helpers
*
* Helper functions for tool execution context enhancement.
* Extracted from tool-registration.ts to reduce duplication and improve maintainability.
*/
import type { z } from "zod";
import type { Context } from "hono";
import type { CreateMessageRequest, CreateMessageResult, ElicitRequestFormParams, ElicitRequestURLParams, ElicitResult, ElicitRequest } from "@modelcontextprotocol/sdk/types.js";
import type { SampleOptions, ElicitOptions, ElicitFormParams, ElicitUrlParams } from "../types/index.js";
import type { SessionData } from "../sessions/session-manager.js";
export type { SessionData };
/**
* Result of session context lookup
*/
export interface SessionContextResult {
requestContext: Context | undefined;
session: SessionData | undefined;
progressToken: number | undefined;
sendNotification: ((notification: {
method: string;
params: Record<string, any>;
}) => Promise<void>) | undefined;
}
/**
* Find session context from sessions map
* Combines session lookup, context matching, and metadata extraction in one pass
*/
export declare function findSessionContext(sessions: Map<string, SessionData>, initialRequestContext: Context | undefined, extraProgressToken?: number, extraSendNotification?: (notification: {
method: string;
params: Record<string, any>;
}) => Promise<void>): SessionContextResult;
/**
* Send a progress notification
*/
export declare function sendProgressNotification(sendNotification: ((notification: {
method: string;
params: Record<string, any>;
}) => Promise<void>) | undefined, progressToken: number | undefined, progress: number, total: number | undefined, message: string | undefined): Promise<void>;
/**
* Wrap a promise with a timeout
*/
export declare function withTimeout<T>(promise: Promise<T>, timeout: number | undefined, errorMessage: string): Promise<T>;
/**
* Parsed elicit parameters
*/
export interface ParsedElicitParams {
sdkParams: ElicitRequestFormParams | ElicitRequestURLParams;
zodSchema: z.ZodObject<any> | null;
options: ElicitOptions | undefined;
}
/**
* Parse elicit() method parameters handling multiple overload signatures
*/
export declare function parseElicitParams(messageOrParams: string | ElicitFormParams | ElicitUrlParams, schemaOrUrlOrOptions?: z.ZodObject<any> | string | ElicitOptions, maybeOptions?: ElicitOptions): ParsedElicitParams;
/**
* Create the sample() method for enhanced context
*/
export declare function createSampleMethod(createMessage: (params: CreateMessageRequest["params"], options?: {
timeout?: number;
}) => Promise<CreateMessageResult>, progressToken: number | undefined, sendNotification: ((notification: {
method: string;
params: Record<string, unknown>;
}) => Promise<void>) | undefined): {
(prompt: string, options?: SampleOptions): Promise<CreateMessageResult>;
(sampleParams: CreateMessageRequest["params"], options?: SampleOptions): Promise<CreateMessageResult>;
};
/**
* Create the elicit() method for enhanced context
*/
export declare function createElicitMethod(elicitInput: (params: ElicitRequest["params"], options?: {
timeout?: number;
}) => Promise<ElicitResult>): (messageOrParams: string | ElicitFormParams | ElicitUrlParams, schemaOrUrlOrOptions?: z.ZodObject<any> | string | ElicitOptions, maybeOptions?: ElicitOptions) => Promise<ElicitResult>;
/**
* Create the reportProgress() method for enhanced context
*/
export declare function createReportProgressMethod(progressToken: number | undefined, sendNotification: ((notification: {
method: string;
params: Record<string, any>;
}) => Promise<void>) | undefined): ((progress: number, total?: number, message?: string) => Promise<void>) | undefined;
/**
* RFC 5424 log levels with numeric values for comparison
*/
declare const LOG_LEVELS: {
readonly debug: 0;
readonly info: 1;
readonly notice: 2;
readonly warning: 3;
readonly error: 4;
readonly critical: 5;
readonly alert: 6;
readonly emergency: 7;
};
type LogLevel = keyof typeof LOG_LEVELS;
/**
* Valid log levels according to RFC 5424
*/
export declare const VALID_LOG_LEVELS: readonly LogLevel[];
/**
* Check if a log level is valid
*/
export declare function isValidLogLevel(level: string): level is LogLevel;
/**
* Check if a message level meets the minimum log level threshold
*/
export declare function shouldLogMessage(messageLevel: string, minLevel: string | undefined): boolean;
/**
* Create enhanced context with sample, elicit, reportProgress, log, client, session, and notification methods
*/
export declare function createEnhancedContext(baseContext: Context | undefined, createMessage: (params: CreateMessageRequest["params"], options?: {
timeout?: number;
}) => Promise<CreateMessageResult>, elicitInput: (params: ElicitRequest["params"], options?: {
timeout?: number;
}) => Promise<ElicitResult>, progressToken: number | undefined, sendNotification: ((notification: {
method: string;
params: Record<string, unknown>;
}) => Promise<void>) | undefined, minLogLevel?: string, clientCapabilities?: Record<string, unknown>, sessionId?: string, sessions?: Map<string, SessionData>): Context & {
sample: ReturnType<typeof createSampleMethod>;
elicit: ReturnType<typeof createElicitMethod>;
reportProgress: (params: {
progress: number;
total?: number;
}) => Promise<void>;
log: (level: string, data: unknown, logger?: string) => Promise<void>;
client: {
capabilities?: Record<string, unknown>;
};
session: {
id?: string;
};
sendNotification: typeof sendNotification;
};
//# sourceMappingURL=tool-execution-helpers.d.ts.map