This example demonstrates how to use the sampling capability of the MCP SDK with an OpenAI-compatible client. It shows how to:
- Connect to an MCP server
- Fetch available tools
- Use OpenAI's API for chat completions
- Call MCP tools from the client
- Python 3.13+
- uv for dependency management
- An OpenAI API key (set in a
.envfile or as an environment variable)
- Install dependencies:
cd examples/clients/simple-sampling-client/
uv sync-
Set environment variables in a
.envfile. A sample.envfile is provided as.env.example. -
Start the MCP server in a separate terminal:
cd examples/snippets/servers/
uv run server sampling streamable-http- Run the sampling client in previous terminal:
uv run mcp-simple-sampling-clientYou will be prompted to enter a message. Type your message and press Enter. The assistant will respond using the sampling capability and may call MCP tools as needed.
Type exit or quit to stop the client.
For more details, see the source code in mcp_simple_sampling_client/main.py.