Skip to main content
Glama
optuna

Optuna MCP Server

Official
by optuna

ask

Suggest optimized parameters for hyperparameter tuning using a defined search space. Integrates with the Optuna MCP Server to enhance model performance through structured parameter distribution.

Instructions

Suggest new parameters using Optuna

    search_space must be a string that can be evaluated to a dictionary to specify Optuna's distributions.

    Example:
        {"x": {"name": "FloatDistribution", "attributes": {"step": null, "low": -10.0, "high": 10.0, "log": false}}}
    

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
search_spaceYes

Implementation Reference

  • The core handler function for the 'ask' tool. It parses the search_space into Optuna distributions and calls study.ask() to suggest new trial parameters, returning a TrialResponse.
    @mcp.tool(structured_output=True)
    def ask(search_space: dict) -> TrialResponse:
        """Suggest new parameters using Optuna
    
        search_space must be a string that can be evaluated to a dictionary to specify Optuna's distributions.
    
        Example:
            {"x": {"name": "FloatDistribution", "attributes": {"step": null, "low": -10.0, "high": 10.0, "log": false}}}
        """
        try:
            distributions = {
                name: optuna.distributions.json_to_distribution(json.dumps(dist))
                for name, dist in search_space.items()
            }
        except Exception as e:
            raise McpError(ErrorData(code=INTERNAL_ERROR, message=f"Error: {e}")) from e
    
        if mcp.study is None:
            raise McpError(
                ErrorData(
                    code=INTERNAL_ERROR,
                    message="No study has been created. Please create a study first.",
                )
            )
    
        trial = mcp.study.ask(fixed_distributions=distributions)
    
        return TrialResponse(
            trial_number=trial.number,
            params=trial.params,
        )
  • Pydantic BaseModel defining the structured output schema for the 'ask' tool response, including trial_number, params, etc.
    class TrialResponse(BaseModel):
        trial_number: int
        params: dict[str, typing.Any] | None = Field(
            default=None, description="The parameter values suggested by the trial."
        )
        values: list[float] | None = Field(
            default=None, description="The objective values of the trial."
        )
        user_attrs: dict[str, typing.Any] | None = Field(
            default=None, description="User-defined attributes for the trial."
        )
        system_attrs: dict[str, typing.Any] | None = Field(
            default=None, description="System-defined attributes for the trial."
        )
  • Registration of all tools including 'ask' by calling register_tools on the OptunaMCP instance before running the server.
    mcp = OptunaMCP("Optuna", storage=args.storage)
    mcp = register_tools(mcp)
    mcp.run()
  • The @mcp.tool decorator that registers the 'ask' function as an MCP tool with structured output.
    @mcp.tool(structured_output=True)

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/optuna/optuna-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server