Add support for Elicitation (#625)
Co-authored-by: David Soria Parra <davidsp@anthropic.com>
This commit is contained in:
parent
a3bcabdce2
commit
69e6572d48
1 changed files with 643 additions and 8 deletions
|
|
@ -34,6 +34,7 @@ from mcp.server.auth.provider import OAuthAuthorizationServerProvider
|
|||
from mcp.server.auth.settings import (
|
||||
AuthSettings,
|
||||
)
|
||||
from mcp.server.elicitation import ElicitationResult, ElicitSchemaModelT, elicit_with_validation
|
||||
from mcp.server.fastmcp.exceptions import ResourceError
|
||||
from mcp.server.fastmcp.prompts import Prompt, PromptManager
|
||||
from mcp.server.fastmcp.resources import FunctionResource, Resource, ResourceManager
|
||||
|
|
@ -972,6 +973,37 @@ class Context(BaseModel, Generic[ServerSessionT, LifespanContextT, RequestT]):
|
|||
assert self._fastmcp is not None, "Context is not available outside of a request"
|
||||
return await self._fastmcp.read_resource(uri)
|
||||
|
||||
async def elicit(
|
||||
self,
|
||||
message: str,
|
||||
schema: type[ElicitSchemaModelT],
|
||||
) -> ElicitationResult[ElicitSchemaModelT]:
|
||||
"""Elicit information from the client/user.
|
||||
|
||||
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 provided schema. Or in case a
|
||||
client is an agent, it might decide how to handle the elicitation -- either by asking
|
||||
the user or automatically generating a response.
|
||||
|
||||
Args:
|
||||
schema: A Pydantic model class defining the expected response structure, according to the specification,
|
||||
only primive types are allowed.
|
||||
message: Optional message to present to the user. If not provided, will use
|
||||
a default message based on the schema
|
||||
|
||||
Returns:
|
||||
An ElicitationResult containing the action taken and the data if accepted
|
||||
|
||||
Note:
|
||||
Check the result.action to determine if the user accepted, declined, or cancelled.
|
||||
The result.data will only be populated if action is "accept" and validation succeeded.
|
||||
"""
|
||||
|
||||
return await elicit_with_validation(
|
||||
session=self.request_context.session, message=message, schema=schema, related_request_id=self.request_id
|
||||
)
|
||||
|
||||
async def log(
|
||||
self,
|
||||
level: Literal["debug", "info", "warning", "error"],
|
||||
|
|
|
|||
Loading…
Reference in a new issue