Skip to content

Fix MCP fn_wrapper to pass None for optional params with UNSET defaults#21051

Merged
daniellok-db merged 2 commits intomlflow:masterfrom
yangbaechu:fix-mcp-click-unset-2
Feb 24, 2026
Merged

Fix MCP fn_wrapper to pass None for optional params with UNSET defaults#21051
daniellok-db merged 2 commits intomlflow:masterfrom
yangbaechu:fix-mcp-click-unset-2

Conversation

@yangbaechu
Copy link
Contributor

Related Issues/PRs

Resolve #21050

What changes are proposed in this pull request?

This PR fixes mlflow/mcp/server.py:fn_wrapper to handle Click 8.3.0+ click.core.Sentinel.UNSET defaults without breaking callback invocation.

When a parameter is missing from kwargs:

  • If param.default is Sentinel.UNSET, this PR sets kwargs[param.name] = None.
  • Otherwise, it sets kwargs[param.name] = param.default (existing behavior).

This avoids passing UNSET into callbacks while ensuring omitted optional params are still present for command.callback(**kwargs).

How is this PR tested?

  • Manual tests

Verified on Click 8.3.1.

Python-level

from mlflow.mcp.server import fn_wrapper
from mlflow.experiments import commands as experiments_cli

wrapped = fn_wrapper(experiments_cli.commands["get"])
wrapped(experiment_id="4", output="json")
  • Before this PR: raises TypeError: get_experiment() missing 1 required positional argument: 'experiment_name'
  • With this PR: succeeds and returns the expected JSON response.

End-to-end MCP tool calls

  • get_experiment succeeds when exactly one of experiment_id / experiment_name is provided, and fails with the expected Click validation error when both/neither are provided.
  • search_traces, list_scorers, and create_run behave as expected with optional params omitted / flags set / defaults applied.

Does this PR require documentation update?

  • No. You can skip the rest of this section.

Does this PR require updating the MLflow Skills repository?

  • No. You can skip the rest of this section.

Release Notes

Is this a user-facing change?

  • Yes. Give a description of this change to be included in the release notes for MLflow users.

Fixes MCP tool invocation on Click 8.3.0+ when optional parameters are omitted by treating Sentinel.UNSET defaults as None in fn_wrapper.

What component(s), interfaces, languages, and integrations does this PR affect?

Components

  • area/tracing: MLflow Tracing features, tracing APIs, and LLM tracing functionality

How should the PR be classified in the release notes? Choose one:

  • rn/bug-fix - A user-facing bug fix worth mentioning in the release notes

Should this PR be included in the next patch release?

Yes should be selected for bug fixes, documentation updates, and other small changes. No should be selected for new features and larger changes. If you're unsure about the release classification of this PR, leave this unchecked to let the maintainers decide.

What is a minor/patch release?
  • Minor release: a release that increments the second part of the version number (e.g., 1.2.0 -> 1.3.0).
    Bug fixes, doc updates and new features usually go into minor releases.
  • Patch release: a release that increments the third part of the version number (e.g., 1.2.0 -> 1.2.1).
    Bug fixes and doc updates usually go into patch releases.
  • Yes (this PR will be cherry-picked and included in the next patch release)
  • No (this PR will be included in the next minor release)

Signed-off-by: Yang Geonhee <abab_1212@naver.com>
@github-actions github-actions bot added community Community/external contribution size/XS area/tracing MLflow Tracing and its integrations rn/bug-fix Mention under Bug Fixes in Changelogs. labels Feb 21, 2026
@github-actions
Copy link
Contributor

🛠 DevTools 🛠

Install mlflow from this PR

# mlflow
pip install git+https://github.com/mlflow/mlflow.git@refs/pull/21051/merge
# mlflow-skinny
pip install git+https://github.com/mlflow/mlflow.git@refs/pull/21051/merge#subdirectory=libs/skinny

For Databricks, use the following command:

%sh curl -LsSf https://raw.githubusercontent.com/mlflow/mlflow/HEAD/dev/install-skinny.sh | sh -s pull/21051/merge

Copy link
Collaborator

@daniellok-db daniellok-db left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lg but let's add a test case

kwargs[param.name] = param.default
if param.name not in kwargs:
if param.default is click_unset:
kwargs[param.name] = None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the difference here? previously would kwargs simply not contain param.name? what error does that cause?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you include a test case that demonstrates where it was failing before?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes — kwargs simply didn't contain the param, so command.callback(**kwargs) raises e.g. TypeError: get_experiment() missing 1 required positional argument: 'experiment_name'.

Signed-off-by: Yang Geonhee <abab_1212@naver.com>
@yangbaechu
Copy link
Contributor Author

Added a unit test that simulates Sentinel.UNSET via monkeypatch to verify the fix regardless of Click version.

kwargs[param.name] = param.default
if param.name not in kwargs:
if param.default is click_unset:
kwargs[param.name] = None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you include a test case that demonstrates where it was failing before?

@github-actions
Copy link
Contributor

Documentation preview for 366ada2 is available at:

More info
  • Ignore this comment if this PR does not change the documentation.
  • The preview is updated when a new commit is pushed to this PR.
  • This comment was created by this workflow run.
  • The documentation was built by this workflow run.

@daniellok-db daniellok-db added this pull request to the merge queue Feb 24, 2026
Merged via the queue into mlflow:master with commit 3e31dbd Feb 24, 2026
51 of 52 checks passed
@github-actions github-actions bot added size/S Small PR (10-49 LoC) and removed size/XS labels Feb 24, 2026
daniellok-db pushed a commit to daniellok-db/mlflow that referenced this pull request Mar 5, 2026
…ts (mlflow#21051)

Signed-off-by: Yang Geonhee <abab_1212@naver.com>
daniellok-db pushed a commit to daniellok-db/mlflow that referenced this pull request Mar 5, 2026
…ts (mlflow#21051)

Signed-off-by: Yang Geonhee <abab_1212@naver.com>
daniellok-db pushed a commit that referenced this pull request Mar 5, 2026
…ts (#21051)

Signed-off-by: Yang Geonhee <abab_1212@naver.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/tracing MLflow Tracing and its integrations community Community/external contribution rn/bug-fix Mention under Bug Fixes in Changelogs. size/S Small PR (10-49 LoC) v3.10.1

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] MCP fn_wrapper incorrectly handles Click 8.3.0+ Sentinel.UNSET for omitted optional params

2 participants