mirror of
https://github.com/mcp-use/mcp-use.git
synced 2026-09-14 18:06:01 +08:00
be9942a4d4
* feat: server first implementation * docs: docs for server and images * chore: locked langchain dependencies in pyproject.toml (#321) * python/release/1.3.13 * catch uvicorn logs && log session id * fix debug logic * docs: logging update * fix: icon of elicitation * fix installation paths for local clones * docs: minor restructuring * docs: reorganization, killed non existing pages * docs: beta tag on server * chore: example servers * feat: inspector autopopulated and client only * feat: enhance MCP logging system with customizable formats and improved middleware * feat: implement weather alert and forecast tools in FastMCP example server * mcp path configurable * fix inspector * export as FastMCP too * chore: update fastmcp migration example * lock inspector version * type runner * unhide docs and remove dangers * migration diff * docs and context example * export context * logging improvement * pretty JSON RPC * autoConnect url param in inspecotr * move logging * type check * Server Router * show response logs * fallback tool name to function name in router registration * compatibility export * Introduced ty and prek as type checker and precommit engine respectively - moved ruff config in pyproject toml * add ty check * ty runs only on staged files * ty configuration fix * move startup info in logging * remove bug from action * update claude rules * fix resource serialization * comments on inspector errors * abstracted runner transports * remove redunant call for network * fix ip getter * add inspector mcp path * response limit logging * disable ty check in action * moved transport type to types * support sse * show transport type in logs * change default transport * refactored tests to use mcp server * removed fastmcp * file restructuring * test table * sampling, elicitation, tool disabling enabling * deprecte sse server side * simplified report * fix action * remove sse deprecated test * move tests files to allow for server integration tests * docs * reorg integration test files * api-reference-doxs * fix broken links * telemetry * remove feature useage from scarf
69 lines
2.5 KiB
Plaintext
69 lines
2.5 KiB
Plaintext
---
|
|
title: "Sampling"
|
|
description: "Request LLM completions from the client"
|
|
icon: "pipette"
|
|
---
|
|
|
|
Sampling allows your MCP server tools to request LLM completions from the client during execution. This enables tools to leverage the client's LLM capabilities for tasks like sentiment analysis, text generation, or decision-making.
|
|
|
|
## How It Works
|
|
|
|
When a tool needs an LLM response, it can use the `Context` to send a sampling request to the client. The client's configured LLM processes the request and returns the result to the tool.
|
|
|
|
## Basic Usage
|
|
|
|
Use the `sample()` method on the context object to request LLM completions:
|
|
|
|
```python
|
|
from mcp.types import SamplingMessage, TextContent
|
|
from mcp_use.server import Context, MCPServer
|
|
|
|
server = MCPServer(name="My Server")
|
|
|
|
@server.tool()
|
|
async def analyze_sentiment(text: str, ctx: Context) -> str:
|
|
"""Analyze the sentiment of text using the client's LLM."""
|
|
prompt = f"""Analyze the sentiment of the following text as positive, negative, or neutral.
|
|
Just output a single word - 'positive', 'negative', or 'neutral'.
|
|
|
|
Text to analyze: {text}"""
|
|
|
|
message = SamplingMessage(role="user", content=TextContent(type="text", text=prompt))
|
|
|
|
# Request LLM analysis
|
|
response = await ctx.sample(messages=[message])
|
|
|
|
if isinstance(response.content, TextContent):
|
|
return response.content.text.strip()
|
|
return ""
|
|
```
|
|
|
|
## Use Case Example
|
|
|
|
A content moderation server that uses the client's LLM to analyze user-generated content:
|
|
|
|
```python
|
|
@server.tool()
|
|
async def moderate_content(content: str, ctx: Context) -> dict:
|
|
"""Moderate user content for policy violations."""
|
|
prompt = f"Does this content violate community guidelines? Answer yes or no: {content}"
|
|
message = SamplingMessage(role="user", content=TextContent(type="text", text=prompt))
|
|
response = await ctx.sample(messages=[message])
|
|
decision = response.content.text.strip().lower()
|
|
return {"allowed": decision == "no", "reason": "AI review"}
|
|
```
|
|
|
|
This allows the tool to leverage sophisticated LLM reasoning without having to integrate an LLM provider directly.
|
|
|
|
## Important Notes
|
|
|
|
- The client must provide a `sampling_callback` to support sampling requests
|
|
- If no callback is configured, sampling requests will fail with an error
|
|
- Sampling requests are processed by the client's configured LLM, not the server
|
|
|
|
## Next Steps
|
|
|
|
- See [Context API](/python/api-reference/mcp_use_server_context) for more context methods
|
|
- Learn about [Elicitation](/python/server/elicitation) for requesting user input
|
|
|