Skip to main content
Glama
optuna

Optuna MCP Server

Official
by optuna

get_trial_user_attrs

Retrieve user-specific attributes for a given trial number. Enables analysis and optimization of hyperparameters in the Optuna MCP Server, supporting interactive chat-based exploration of trial data.

Instructions

Get user attributes in a trial

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
trial_numberYes

Implementation Reference

  • The handler function for the 'get_trial_user_attrs' tool. It fetches the trial from Optuna storage using the trial_number and returns a TrialResponse with the user_attrs.
    @mcp.tool(structured_output=True)
    def get_trial_user_attrs(trial_number: int) -> TrialResponse:
        """Get user attributes in a trial"""
        if mcp.study is None:
            raise McpError(
                ErrorData(
                    code=INTERNAL_ERROR,
                    message="No study has been created. Please create a study first.",
                )
            )
    
        storage = mcp.study._storage
        trial_id = storage.get_trial_id_from_study_id_trial_number(
            mcp.study._study_id, trial_number
        )
        trial = storage.get_trial(trial_id)
        return TrialResponse(
            trial_number=trial_number,
            user_attrs=trial.user_attrs,
        )
  • Pydantic model defining the output structure for the tool, including the user_attrs field.
    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."
        )
  • The register_tools function is called here to register all tools, including 'get_trial_user_attrs', with the MCP server.
    mcp = register_tools(mcp)

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