Overview
With MCP Sampling, servers can:- Create messages: Request LLM completions during tool execution
- Use tools: Provide tools for the LLM to use during sampling
- Manage conversations: Build multi-turn interactions within a single tool call
- Delegate reasoning: Offload complex logic to the LLM
Quick Start
Basic Sampling Configuration
Configure sampling models infastagent.config.yaml:
Using a Sampling-Enabled Tool
How It Works
Server requests sampling
The server calls
ctx.session.create_message() with a prompt and optional toolsCreating Sampling-Enabled Tools
Basic Sampling Tool
Sampling with Tools
Provide tools for the LLM to use during sampling:Complete Example: Secret Code Tool
This example demonstrates the full sampling-with-tools flow:Model Selection
Per-Server Configuration
Configure different models for different servers:Model Aliases
Use model aliases defined in your configuration:Use Cases
Text Analysis
Tools that analyze, summarize, or extract information from text using LLM reasoning
Code Generation
Generate code snippets, configurations, or scripts based on requirements
Data Transformation
Transform data between formats using LLM understanding of structure
Decision Making
Tools that need to make complex decisions based on context and criteria
Validation
Validate inputs, outputs, or configurations using LLM reasoning
Mathematical Reasoning
Solve math problems by providing calculator tools to the LLM
Best Practices
Choose Appropriate Models
Choose Appropriate Models
Match the model to the task complexity:
Limit Token Usage
Limit Token Usage
Set appropriate
max_tokens for sampling requests:Handle Tool Loops
Handle Tool Loops
Implement proper tool execution loops:
Error Handling
Error Handling
Handle sampling failures gracefully:
Advanced Patterns
Multi-Turn Reasoning
Conditional Tool Provision
Troubleshooting
Sampling model not configured
Sampling model not configured
Ensure model is specified in config:
Tool execution loops infinitely
Tool execution loops infinitely
Add iteration limits:
Model lacks capability
Model lacks capability
Switch to more capable model:
Related Topics
MCP Servers
Learn about MCP server configuration
Prompts
Use prompts in sampling requests
Model Configuration
Configure models and aliases
Tool Development
Create custom MCP tools
