add_trials
Add multiple trials to a study in the Optuna MCP Server for hyperparameter optimization. Specify parameters, distributions, values, and trial states for enhanced automated analysis.
Instructions
Add multiple trials to the study.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| trials | Yes |
Implementation Reference
- optuna_mcp/server.py:401-414 (handler)The handler function for the 'add_trials' MCP tool. It converts input TrialToAdd objects to FrozenTrial instances using the _create_trial helper and adds them to the Optuna study.@mcp.tool(structured_output=True) def add_trials(trials: list[TrialToAdd]) -> str: """Add multiple trials to the study.""" frozen_trials = [_create_trial(trial) for trial in trials] if mcp.study is None: raise McpError( ErrorData( code=INTERNAL_ERROR, message="No study has been created. Please create a study first.", ) ) mcp.study.add_trials(frozen_trials) return f"{len(trials)} trials were added."
- optuna_mcp/server.py:35-69 (schema)Dataclass defining the input schema for trials to be added via the 'add_trials' tool.@dataclass class TrialToAdd: """ A trial to be added to an Optuna study. Attributes: params: The parameter values for the trial. distributions: The distributions used for the parameters. A key is the parameter name and a value is a distribution. The distribution is a dictionary that can be converted to a JSON string, e.g., { "name": "IntDistribution", "attributes": {"step": null, "low": 1, "high": 9, "log": false} }. The name of the distribution must be one of the following: - FloatDistribution - IntDistribution - CategoricalDistribution values: The objective values for the trial, or None if not set. If the state is "COMPLETE", this must be set. state: The state of the trial. - "COMPLETE": The trial completed successfully. - "PRUNED": The trial was pruned. - "FAIL": The trial failed. user_attrs: User-defined attributes for the trial, or None if not set. system_attrs: System-defined attributes for the trial, or None if not set. """ params: dict[str, typing.Any] distributions: dict[str, typing.Any] values: list[float] | None state: typing.Literal["COMPLETE", "PRUNED", "FAIL"] user_attrs: dict[str, typing.Any] | None system_attrs: dict[str, typing.Any] | None
- optuna_mcp/server.py:373-385 (helper)Helper function used by 'add_trials' (and 'add_trial') to convert TrialToAdd dataclass instances into Optuna FrozenTrial objects.def _create_trial(trial: TrialToAdd) -> optuna.trial.FrozenTrial: """Create a trial from the given parameters.""" return optuna.trial.create_trial( params=trial.params, distributions={ k: optuna.distributions.json_to_distribution(json.dumps(d)) for k, d in trial.distributions.items() }, values=trial.values, state=optuna.trial.TrialState[trial.state], user_attrs=trial.user_attrs, system_attrs=trial.system_attrs, )
- optuna_mcp/server.py:685-687 (registration)The registration of all tools, including 'add_trials', occurs via the call to register_tools(mcp) in the main function.mcp = OptunaMCP("Optuna", storage=args.storage) mcp = register_tools(mcp) mcp.run()