/** * Client Sampling Utility * * This module provides utilities for MCP clients that don't natively support sampling. * It offers a simple configuration-based API to add sampling capability to any MCP client. * * @see https://modelcontextprotocol.io/specification/2025-06-18/client/sampling */ import type { Client } from "@modelcontextprotocol/sdk/client/index.js"; import { type CreateMessageRequest, type CreateMessageResultWithTools } from "@modelcontextprotocol/sdk/types.js"; import { type AISDKMessage } from "./utils.js"; /** * AI SDK handler function type */ export type AISDKHandler = (params: { systemPrompt?: string; messages: AISDKMessage[]; modelPreferences?: CreateMessageRequest["params"]["modelPreferences"]; tools?: CreateMessageRequest["params"]["tools"]; helpers?: { tool: (...args: any[]) => any; jsonSchema: (...args: any[]) => any; }; }) => Promise; /** * Client sampling configuration */ export interface ClientSamplingConfig { /** * AI SDK handler function that generates completions */ handler: AISDKHandler; /** * AI SDK helpers for tool conversion (tool and jsonSchema from "ai" package) * Required for proper tool support when MCP server sends tools in createMessage request */ helpers?: { tool: (...args: any[]) => any; jsonSchema: (...args: any[]) => any; }; /** * Optional model selection map for preference-based routing */ modelMap?: { hints?: Record; priorities?: { speed?: string; intelligence?: string; cost?: string; }; default?: string; }; } /** * Create a configured client sampling handler * * This is the main entry point for adding sampling support to MCP clients. * It takes a configuration object and returns a ready-to-use sampling handler. * * @param config - Client sampling configuration * @returns MCP sampling handler function * * @example * ```typescript * import { createClientSampling } from "@mcpc/mcp-sampling-ai-provider"; * import { generateText } from "ai"; * * const samplingHandler = createClientSampling({ * handler: async (params) => { * const result = await generateText({ * model: "openai/gpt-5-mini", * messages: params.messages, * }); * return { * model: "openai/gpt-5-mini", * role: "assistant", * content: { type: "text", text: result.text }, * stopReason: result.finishReason === "stop" ? "endTurn" : "maxTokens", * }; * }, * }); * * server.setRequestHandler(CreateMessageRequestSchema, samplingHandler); * ``` */ export declare function createClientSampling(config: ClientSamplingConfig): (request: CreateMessageRequest) => Promise; /** * Helper to select model based on MCP model preferences * * @param preferences - MCP model preferences * @param modelMap - Map of hint names/priorities to your model identifiers * @returns Selected model identifier * * @example * ```typescript * const model = selectModelFromPreferences(modelPreferences, { * hints: { * "gpt-4o": "openai/gpt-4o", * "claude": "anthropic/claude-3-5-sonnet-20241022", * }, * priorities: { * speed: "openai/gpt-4o-mini", * intelligence: "openai/gpt-4o", * cost: "openai/gpt-4o-mini", * }, * default: "openai/gpt-5-mini", * }); * ``` */ export declare function selectModelFromPreferences(preferences: CreateMessageRequest["params"]["modelPreferences"] | undefined, modelMap: { /** * Map hint names to model identifiers */ hints?: Record; /** * Map priority types to model identifiers */ priorities?: { speed?: string; intelligence?: string; cost?: string; }; /** * Default model if no match found */ default: string; }): string; /** * Add sampling support to an existing MCP client * * This function configures an existing MCP Client instance to handle sampling requests * from the server. It sets up the necessary request handler for createMessage requests. * * @param client - Existing MCP Client instance * @param config - Sampling handler configuration * * @example * ```typescript * import { Client } from "@modelcontextprotocol/sdk/client/index.js"; * import { setupClientSampling } from "@mcpc/mcp-sampling-ai-provider"; * import { generateText } from "ai"; * * const client = new Client( * { name: "my-client", version: "1.0.0" }, * { capabilities: { sampling: {} } } * ); * * setupClientSampling(client, { * handler: async (params) => { * const result = await generateText({ * model: "openai/gpt-5-mini", * messages: params.messages, * }); * return { * model: "openai/gpt-5-mini", * role: "assistant", * content: { type: "text", text: result.text }, * stopReason: result.finishReason === "stop" ? "endTurn" : "maxTokens", * }; * }, * }); * * await client.connect(transport); * ``` */ export declare function setupClientSampling(client: Client, config: ClientSamplingConfig): void; //# sourceMappingURL=client-sampling.d.ts.map