|
| 1 | +""" |
| 2 | +Elicitation provider factory for smart provider selection. |
| 3 | +
|
| 4 | +This module implements the factory pattern for creating the appropriate |
| 5 | +elicitation provider based on capabilities and preferences. It automatically |
| 6 | +detects MCP elicitation capability and selects the best available provider. |
| 7 | +""" |
| 8 | + |
| 9 | +from mcp.server.fastmcp import Context |
| 10 | +from pydantic import BaseModel |
| 11 | + |
| 12 | +from mcp_as_a_judge.elicitation.fallback_provider import FallbackElicitationProvider |
| 13 | +from mcp_as_a_judge.elicitation.interface import ElicitationResult |
| 14 | +from mcp_as_a_judge.elicitation.mcp_provider import MCPElicitationProvider |
| 15 | + |
| 16 | + |
| 17 | +class ElicitationProviderFactory: |
| 18 | + """ |
| 19 | + Unified elicitation provider that automatically selects the best available method. |
| 20 | +
|
| 21 | + Similar to the messaging provider, this checks for elicitation capability and |
| 22 | + provides appropriate fallbacks when not available. |
| 23 | + """ |
| 24 | + |
| 25 | + def __init__(self, prefer_elicitation: bool = True): |
| 26 | + """ |
| 27 | + Initialize the elicitation provider factory. |
| 28 | +
|
| 29 | + Args: |
| 30 | + prefer_elicitation: Whether to prefer MCP elicitation when available |
| 31 | + """ |
| 32 | + self.prefer_elicitation = prefer_elicitation |
| 33 | + self._mcp_provider = MCPElicitationProvider() |
| 34 | + self._fallback_provider = FallbackElicitationProvider() |
| 35 | + |
| 36 | + async def elicit_user_input( |
| 37 | + self, message: str, schema: type[BaseModel], ctx: Context |
| 38 | + ) -> ElicitationResult: |
| 39 | + """ |
| 40 | + Elicit user input using the best available method. |
| 41 | +
|
| 42 | + Args: |
| 43 | + message: Message to display to the user |
| 44 | + schema: Pydantic model schema defining expected fields |
| 45 | + ctx: MCP context |
| 46 | +
|
| 47 | + Returns: |
| 48 | + ElicitationResult with success status and data/message |
| 49 | + """ |
| 50 | + |
| 51 | + # Check if MCP elicitation is available and preferred |
| 52 | + if self.prefer_elicitation and self._mcp_provider.check_capability(ctx): |
| 53 | + result = await self._mcp_provider.elicit(message, schema, ctx) |
| 54 | + |
| 55 | + # If MCP elicitation succeeds, return the result |
| 56 | + if result.success: |
| 57 | + return result |
| 58 | + |
| 59 | + # Use fallback provider |
| 60 | + return await self._fallback_provider.elicit(message, schema, ctx) |
| 61 | + |
| 62 | + def get_available_providers(self, ctx: Context) -> dict[str, dict[str, object]]: |
| 63 | + """Get information about all available providers. |
| 64 | + |
| 65 | + Args: |
| 66 | + ctx: MCP context for capability checking |
| 67 | + |
| 68 | + Returns: |
| 69 | + Dictionary with provider availability information |
| 70 | + """ |
| 71 | + return { |
| 72 | + "mcp_elicitation": { |
| 73 | + "available": self._mcp_provider.check_capability(ctx), |
| 74 | + "provider_type": self._mcp_provider.provider_type, |
| 75 | + }, |
| 76 | + "fallback_elicitation": { |
| 77 | + "available": True, # Always available |
| 78 | + "provider_type": self._fallback_provider.provider_type, |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | +# Global elicitation provider instance |
| 84 | +elicitation_provider = ElicitationProviderFactory(prefer_elicitation=True) |
0 commit comments