Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:AI QL Tuui MCP Sampling

From Leeroopedia
Knowledge Sources
Domains MCP, LLM, Sampling
Last Updated 2026-10-24 00:00 GMT

Overview

A server-initiated request pattern where an MCP server asks the connected client to generate an LLM completion on its behalf, with the client controlling which model and parameters are used.

Description

MCP Sampling allows an MCP server to request LLM completions without having direct access to an LLM API. The server sends a sampling/createMessage request containing messages, an optional system prompt, temperature, and max tokens. The client (host application) receives this request, routes it to its configured LLM, and returns the generated response. This gives the client control over model selection, API keys, and cost management, while allowing servers to leverage LLM capabilities. The request/response is asynchronous and uses a UUID-based correlation channel.

Usage

Use this principle when designing MCP servers that need LLM capabilities but should not manage their own API keys, or when designing MCP clients that need to mediate LLM access for connected servers.

Theoretical Basis

The sampling roundtrip:

MCP Server → Client.setRequestHandler(SamplingRequestSchema):
  1. Server calls sampling/createMessage with { messages, systemPrompt?, temperature?, maxTokens? }
  2. Client handler forwards to host application via IPC
  3. Host application:
     a. Convert MCP SamplingMessage[] → OpenAI ChatCompletionMessage[]
     b. Call createCompletion(session, samplingParams)
     c. Extract assistant response
  4. Return SamplingResponse (CreateMessageResult) to server

The SamplingRequestParams includes:

  • messages: SamplingMessage[] — conversation context (role + content)
  • systemPrompt?: string — optional system prompt
  • temperature?: number — sampling temperature
  • maxTokens?: number — max response tokens

Content translation uses convertItem to map MCP content types to OpenAI format.

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment