Skip to content

datarobot_genai.drmcp.test_utils.elicitation_test_tool

elicitation_test_tool

Test tool for elicitation testing.

This module registers a test tool that can be used to test elicitation support. It should be imported in tests that need it.

get_user_greeting async

get_user_greeting(ctx: Context, username: str | None = None) -> dict

Get a personalized greeting for a user.

This tool demonstrates FastMCP's built-in elicitation by requiring a username parameter. If username is not provided, it uses ctx.elicit() to request it from the user.

Parameters:

Name Type Description Default
ctx Context

FastMCP context (automatically injected)

required
username str | None

The username to greet. If None, elicitation will be triggered.

None
Returns
Dictionary with greeting message or error if elicitation was declined/cancelled
Source code in datarobot_genai/drmcp/test_utils/elicitation_test_tool.py
@mcp.tool(
    name="get_user_greeting",
    description=(
        "Get a personalized greeting for a user. "
        "Requires a username - if not provided, will request it via elicitation."
    ),
    tags={"test", "elicitation"},
)
async def get_user_greeting(ctx: Context, username: str | None = None) -> dict:
    """
    Get a personalized greeting for a user.

    This tool demonstrates FastMCP's built-in elicitation by requiring a username parameter.
    If username is not provided, it uses ctx.elicit() to request it from the user.

    Args:
        ctx: FastMCP context (automatically injected)
        username: The username to greet. If None, elicitation will be triggered.

    Returns
    -------
        Dictionary with greeting message or error if elicitation was declined/cancelled
    """
    if not username:
        # Use elicitation to request username from the client
        try:
            result = await ctx.elicit(
                message="Username is required to generate a personalized greeting",
                response_type=str,
            )

            if isinstance(result, AcceptedElicitation):
                username = result.data
            elif isinstance(result, DeclinedElicitation):
                return {
                    "status": "error",
                    "error": "Username declined by user",
                    "message": "Cannot generate greeting without username",
                }
            elif isinstance(result, CancelledElicitation):
                return {
                    "status": "error",
                    "error": "Operation cancelled",
                    "message": "Greeting request was cancelled",
                }
        except Exception:
            # Elicitation not supported by client - return graceful skip
            return {
                "status": "skipped",
                "message": (
                    "Elicitation not supported by client. "
                    "Username parameter is required when client does not support elicitation."
                ),
                "elicitation_supported": False,
            }

    return {
        "status": "success",
        "message": f"Hello, {username}! Welcome to the DataRobot MCP server.",
        "username": username,
    }