FastestMCP.Sampling (fastest_mcp v0.1.2)

Copy Markdown View Source

Elixir-native helpers for MCP sampling.

This module sits one level above FastestMCP.Context.sample/3. It keeps the low-level sampling call available, but adds a friendlier surface for:

  • prompt-oriented sampling requests
  • message-list sampling requests
  • normalization of the returned payload into a small response struct
  • preparation of runtime tools for model-facing sampling calls

Example

response =
  FastestMCP.Sampling.run!(ctx, "Summarize the active session in one sentence.")

FastestMCP.Sampling.text(response)

If you want the model to see local tools during sampling, prepare them first:

tools = FastestMCP.Sampling.prepare_tools(server_name)
response = FastestMCP.Sampling.run!(ctx, prompt: "Use tools if needed", tools: tools)

The module is small on purpose. It does not invent a second interaction model; it just makes the existing runtime sampling path easier to call from handlers.

Summary

Functions

Normalizes server tools or sampling tools into the sampling request format.

Normalizes raw sampling output into a response struct.

Runs the main entrypoint for this module.

Runs a sampling request and raises on failure.

Extracts or requests plain text for this interaction.

Types

run_opts()

@type run_opts() :: keyword()

Functions

prepare_tools(server_or_tools, opts \\ [])

Normalizes server tools or sampling tools into the sampling request format.

response(response)

Normalizes raw sampling output into a response struct.

run(context, prompt_or_messages, opts \\ [])

Runs the main entrypoint for this module.

run!(context, prompt_or_messages, opts \\ [])

Runs a sampling request and raises on failure.

text(raw)

Extracts or requests plain text for this interaction.