MCPcopy Index your code
hub / github.com/modelcontextprotocol/python-sdk / elicit_with_validation

Function elicit_with_validation

src/mcp/server/elicitation.py:85–120  ·  view source on GitHub ↗

Elicit information from the client/user with schema validation (form mode). This method can be used to interactively ask for additional information from the client within a tool's execution. The client might display the message to the user and collect a response according to the provide

(
    session: ServerSession,
    message: str,
    schema: type[ElicitSchemaModelT],
    related_request_id: RequestId | None = None,
)

Source from the content-addressed store, hash-verified

83
84
85async def elicit_with_validation(
86 session: ServerSession,
87 message: str,
88 schema: type[ElicitSchemaModelT],
89 related_request_id: RequestId | None = None,
90) -> ElicitationResult[ElicitSchemaModelT]:
91 """Elicit information from the client/user with schema validation (form mode).
92
93 This method can be used to interactively ask for additional information from the
94 client within a tool's execution. The client might display the message to the
95 user and collect a response according to the provided schema. If the client
96 is an agent, it might decide how to handle the elicitation -- either by asking
97 the user or automatically generating a response.
98
99 For sensitive data like credentials or OAuth flows, use elicit_url() instead.
100 """
101 json_schema = schema.model_json_schema(schema_generator=_ElicitationJsonSchema)
102 _validate_rendered_properties(json_schema)
103
104 result = await session.elicit_form(
105 message=message,
106 requested_schema=json_schema,
107 related_request_id=related_request_id,
108 )
109
110 if result.action == "accept" and result.content is not None:
111 # Validate and parse the content using the schema
112 validated_data = schema.model_validate(result.content)
113 return AcceptedElicitation(data=validated_data)
114 elif result.action == "decline":
115 return DeclinedElicitation()
116 elif result.action == "cancel": # pragma: no cover
117 return CancelledElicitation()
118 else: # pragma: no cover
119 # This should never happen, but handle it just in case
120 raise ValueError(f"Unexpected elicitation action: {result.action}")
121
122
123async def elicit_url(

Callers 1

elicitMethod · 0.90

Calls 5

AcceptedElicitationClass · 0.85
DeclinedElicitationClass · 0.85
elicit_formMethod · 0.45

Tested by

no test coverage detected