MCP服务端在工具执行中请求用户输入的两类流程
介绍MCP服务端在工具执行期间向用户索取结构化输入的处理方式,涵盖旧版推送式请求与2026-07-28协议版本后的输入轮次机制,并说明处理器参数与轮次上限的作用。
当您需要响应服务器在工具执行期间对用户输入的请求时使用此选项 。 Eligaring 允许 MCP 服务器在操作期间请求用户提供结构化输入. 服务器可以交互请求缺失参数,请求澄清,或者收集额外的上下文,而不是要求所有输入前端. **这些段落显示的是服务器启动的流程,这是握手时代协议使用的流程.** 打开 `2026-07-28` 服务器通过返回请求来询问 —— 参见 [输入所需回合](#input-required-rounds)。一个 `elicitation_handler` 两者都一样,所以下面的例子 `mode="legacy"` 只为了锻炼被推压的形态. ## 处理器模板 ```python from pathlib import Path from fastmcp import Client from fastmcp.client.elicitation import ElicitResult, ElicitRequestParams, RequestContext async def elicitation_handler( message: str, response_type: type | None, params: ElicitRequestParams, context: RequestContext ) -> ElicitResult | object: """ Handle server requests for user input. Args: message: The prompt to display to the user response_type: Python dataclass type for form responses (None for URL requests or empty schemas) params: Original MCP elicitation parameters context: Request context with metadata Returns: - Data directly (implicitly accepts the elicitation) - ElicitResult for explicit control over the action """ # Present the message and collect input user_input = input(f"{message}: ") if not user_input: return ElicitResult(action="decline") # URL requests and empty-object schemas have no response type to construct, # so accepting is the whole response. if response_type is None: return ElicitResult(action="accept") # A scalar request, such as ctx.elicit(..., response_type=str), has one `value` field return response_type(value=user_input) client = Client( Path("my_mcp_server.py"), mode="legacy", elicitation_handler=elicitation_handler, ) ``` ## 如何运作 当服务器需要用户输入时, 它会用消息提示发送一个引出请求。 Form caused request 包括一个描述预期响应结构的 JSON schema , 以及 FastMCP 自动将这个chema 转换成 Python 数据类 。 一个请求一个单一的scalar 的服务器, 如字符串或数字, 得到一个数据类 。 `value` 字段,这是此页面上的实例构造。 URL 引出请求和空对象计划使用 `response_type=None`. 处理器得到四个参数: 向用户显示的提示信息 一个 Python 数据类类型, FastMCP 由窗体请求的 JSON 计划创建。 使用此数据构建您的响应, 并进行适当的输入。 对于 URL 请求或空对象计划, 这将是 `None`. 原始的 MCP 诱导参数。表单请求将原始的 JSON 计划带入 `params.requested_schema`; URL 请求携带 `params.url` 而不是没有计划。 请求包含元数据的内容 。 ## 反应行动 您可以直接返回数据, 无论是 `response_type` 或平原( 平原) `dict`,它隐含地接受诱导: ```python async def elicitation_handler(message, response_type, params, context): user_input = input(f"{message}: ") return response_type(value=user_input) # Implicit accept ``` 或返回一个 `ElicitResult` 对行动的明确控制: ```python from fastmcp.client.elicitation import ElicitResult async def elicitation_handler(message, response_type, params, context): user_input = input(f"{message}: ") if not user_input: return ElicitResult(action="decline") # User declined if user_input == "cancel": return ElicitResult(action="cancel") # Cancel entire operation return ElicitResult( action="accept", content=response_type(value=user_input) ) ``` **行动类型:** - **`accept`**: 用户提供了有效的输入。 将数据输入 `content` 字段键入。 - **`decline`**: 用户选择不提供所请求的信息。 Omit `content`. - **`cancel`**: 用户取消了整个操作。 Omit `content`. ## 示例 文件管理工具可能询问创建哪个目录 : ```python from pathlib import Path from fastmcp import Client from fastmcp.client.elicitation import ElicitResult async def elicitation_handler(message, response_type, params, context): print(f"Server asks: {message}") user_response = input("Your response: ") if not user_response: return ElicitResult(action="decline") # Use the response_type dataclass to create a properly structured response return response_type(value=user_response) client = Client( Path("my_mcp_server.py"), mode="legacy", elicitation_handler=elicitation_handler ) ``` ## 输入所需回合 关于协议版本 `2026-07-28` 后,服务器在返回最后结果之前可以请求输入。没有东西被打开:工具 *返回时* 描述它需要什么, 完成该回合作为普通答复, 客户端通过发布一个 **新设** `call_tool`, `get_prompt`,或 `read_resource` 请求带有答案。 `fastmcp.Client` 为您驱动该循环 —— 它使用您已经配置的调用符满足每回合的请求( 您的) `elicitation_handler`, `sampling_handler`,和根),并重复,直到呼叫到达终端结果。除上述处理器外,不需要额外的线条。 那个... `input_required_max_rounds` 参数上限客户端在放弃前将回答多少回合, 守护一个从未终止的服务器。 它默认为 `10`. ```python from fastmcp import Client client = Client( "https://example.com/mcp", mode="auto", elicitation_handler=elicitation_handler, input_required_max_rounds=5, ) ```
查看原始来源