You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: www/docs/pages/clients/advanced-sampling.mdx
+18Lines changed: 18 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,6 +6,24 @@ Learn how to implement MCP clients that can handle sampling requests from server
6
6
7
7
Sampling allows MCP clients to respond to LLM completion requests from servers. When a server needs to generate content, answer questions, or perform reasoning tasks, it can send a sampling request to the client, which then processes it using an LLM and returns the result.
8
8
9
+
:::danger[Critical Security Requirement]
10
+
Per the [MCP specification](https://modelcontextprotocol.io/specification/2025-06-18/client/sampling#user-interaction-model), sampling implementations **SHOULD** always include a human in the loop with the ability to deny sampling requests.
11
+
12
+
**You MUST implement approval flows that:**
13
+
- Present each sampling request to the user for review before execution
14
+
- Allow users to view and edit prompts before sending to the LLM
15
+
- Display generated responses for user approval before returning to the server
16
+
- Provide clear UI to accept or reject requests at each stage
17
+
18
+
**Without human approval, your implementation:**
19
+
- Allows servers to make unauthorized LLM requests without user consent
20
+
- May expose sensitive information through unreviewed prompts
21
+
- Creates uncontrolled API costs from automated sampling
22
+
- Violates user trust and security best practices
23
+
24
+
The examples below show basic handler implementation. **You must add approval logic** before using in production.
25
+
:::
26
+
9
27
## Implementing a Sampling Handler
10
28
11
29
Create a sampling handler by implementing the `SamplingHandler` interface:
Copy file name to clipboardExpand all lines: www/docs/pages/servers/advanced-sampling.mdx
+19Lines changed: 19 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,6 +6,25 @@ Learn how to implement MCP servers that can request LLM completions from clients
6
6
7
7
Sampling allows MCP servers to request LLM completions from clients, enabling bidirectional communication where servers can leverage client-side LLM capabilities. This is particularly useful for tools that need to generate content, answer questions, or perform reasoning tasks.
8
8
9
+
:::info[User Consent Required]
10
+
Per the [MCP specification](https://modelcontextprotocol.io/specification/2025-06-18/client/sampling#user-interaction-model), clients **SHOULD** implement human-in-the-loop approval for sampling requests.
11
+
12
+
When you request sampling from a client:
13
+
- The user will typically be prompted to review and approve your request
14
+
- The user may modify your prompts before sending to their LLM
15
+
- The user may reject your request entirely
16
+
- Response times may be longer due to user interaction
17
+
18
+
**Design your tools accordingly:**
19
+
- Provide clear descriptions of why sampling is needed
20
+
- Use descriptive system prompts explaining the purpose
21
+
- Handle rejection errors gracefully
22
+
- Consider timeouts for user approval delays
23
+
- Don't assume immediate or automatic approval
24
+
25
+
Well-designed sampling requests improve user trust and approval rates.
26
+
:::
27
+
9
28
## Enabling Sampling
10
29
11
30
To enable sampling in your server, call `EnableSampling()` during server setup:
Copy file name to clipboardExpand all lines: www/docs/pages/transports/http.mdx
+66Lines changed: 66 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -693,6 +693,24 @@ The headers are automatically populated by the transport layer and are available
693
693
694
694
StreamableHTTP transport now supports bidirectional sampling, allowing servers to request LLM completions from clients. This enables advanced scenarios where servers can leverage client-side LLM capabilities.
695
695
696
+
:::warning[Security: Human-in-the-Loop Required]
697
+
Per the [MCP specification](https://modelcontextprotocol.io/specification/2025-06-18/client/sampling), implementations **SHOULD** always include a human in the loop with the ability to deny sampling requests.
698
+
699
+
**Your sampling handler implementation MUST:**
700
+
- Present sampling requests to users for review before execution
701
+
- Allow users to view and edit prompts before sending to the LLM
702
+
- Display generated responses for approval before returning to the server
703
+
- Provide clear UI to accept or reject sampling requests
704
+
705
+
Failing to implement approval flows creates serious security and trust risks, including:
706
+
- Servers making unauthorized LLM requests on behalf of users
707
+
- Exposure of sensitive data through unreviewed prompts
708
+
- Uncontrolled API costs from automated sampling
709
+
- Lack of user consent for AI interactions
710
+
711
+
See the [example implementation](#example-with-approval-flow) below for a reference approval pattern.
712
+
:::
713
+
696
714
### Requirements for Sampling
697
715
698
716
To enable sampling with StreamableHTTP transport, the client **must** use the `WithContinuousListening()` option:
@@ -777,6 +795,54 @@ mcpServer.AddTool(mcp.Tool{
777
795
- Without continuous listening, the transport operates in stateless request/response mode only
778
796
- Network interruptions may require reconnection and re-establishment of the sampling channel
779
797
798
+
### Example with Approval Flow
799
+
800
+
Here's a reference implementation showing proper human-in-the-loop approval:
801
+
802
+
```go
803
+
typeApprovalSamplingHandlerstruct {
804
+
llmClient LLMClient// Your actual LLM client
805
+
ui UserInterface// Your UI for presenting requests to users
0 commit comments