set_sampler
Configure the sampler for optimization studies on the Optuna MCP Server. Choose from TPESampler, NSGAIISampler, RandomSampler, or GPSampler to define the sampling strategy based on study objectives.
Instructions
Set the sampler for the study. The sampler must be one of the following: - TPESampler - NSGAIISampler - RandomSampler - GPSampler
The default sampler for single-objective optimization is TPESampler.
The default sampler for multi-objective optimization is NSGAIISampler.
GPSampler is a Gaussian process-based sampler suitable for low-dimensional numerical optimization problems.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| name | Yes |
Implementation Reference
- optuna_mcp/server.py:190-219 (handler)The handler function for the 'set_sampler' tool. It creates an instance of the specified Optuna sampler and assigns it to the current study's sampler. Requires a study to be created first. Returns a StudyResponse with the study name and sampler name.@mcp.tool(structured_output=True) def set_sampler( name: SamplerName, ) -> StudyResponse: """Set the sampler for the study. The sampler must be one of the following: - TPESampler - NSGAIISampler - RandomSampler - GPSampler The default sampler for single-objective optimization is TPESampler. The default sampler for multi-objective optimization is NSGAIISampler. GPSampler is a Gaussian process-based sampler suitable for low-dimensional numerical optimization problems. """ sampler = getattr(optuna.samplers, name)() if mcp.study is None: raise McpError( ErrorData( code=INTERNAL_ERROR, message="No study has been created. Please create a study first.", ) ) mcp.study.sampler = sampler return StudyResponse( study_name=mcp.study.study_name, sampler_name=name, )
- optuna_mcp/server.py:20-20 (schema)Type alias defining the valid sampler names accepted by the set_sampler tool.SamplerName = typing.Literal["TPESampler", "NSGAIISampler", "RandomSampler", "GPSampler"]
- optuna_mcp/server.py:71-82 (schema)Pydantic model defining the structured output schema returned by set_sampler and other study-related tools.class StudyResponse(BaseModel): study_name: str sampler_name: SamplerName | None = Field( default=None, description="The name of the sampler used in the study." ) directions: list[DirectionName] | None = Field( default=None, description="The optimization directions for each objective." ) metric_names: list[str] | None = Field( default=None, description="The metric names for each objective." )
- optuna_mcp/server.py:685-686 (registration)In the main function, register_tools is called on the OptunaMCP instance, which defines and registers all tools including set_sampler via decorators.mcp = OptunaMCP("Optuna", storage=args.storage) mcp = register_tools(mcp)