Active function execution based on tools/tool_choice: - If tool_choice specifies a function name (string or {type:'function', function:{name}}), attempt to execute that function. - If tool_choice is 'auto' and only one tool is provided, execute that tool. - Argument source: Attempt
(
messages: List[Message],
tools: Optional[List[Dict[str, Any]]],
tool_choice: Optional[Union[str, Dict[str, Any]]],
)
| 10 | |
| 11 | |
| 12 | async def maybe_execute_tools( |
| 13 | messages: List[Message], |
| 14 | tools: Optional[List[Dict[str, Any]]], |
| 15 | tool_choice: Optional[Union[str, Dict[str, Any]]], |
| 16 | ) -> Optional[List[Dict[str, Any]]]: |
| 17 | """ |
| 18 | Active function execution based on tools/tool_choice: |
| 19 | - If tool_choice specifies a function name (string or {type:'function', function:{name}}), attempt to execute that function. |
| 20 | - If tool_choice is 'auto' and only one tool is provided, execute that tool. |
| 21 | - Argument source: Attempt to extract JSON from the text of the most recent user message; if fails, use empty parameters. |
| 22 | - Returns [{name, arguments, result}]; returns None if no executable is found. |
| 23 | """ |
| 24 | try: |
| 25 | # Track runtime-declared tools and optional MCP endpoints |
| 26 | mcp_ep: Optional[str] = None |
| 27 | # support per-request MCP endpoint via request-level message or tool spec extension (if present later) |
| 28 | # current: read from env only in registry when not provided |
| 29 | register_runtime_tools(tools, mcp_ep) |
| 30 | # If tool result messages already exist (role='tool'), follow the conversational calling loop, |
| 31 | # driven by the client, the server does not actively execute again. |
| 32 | for m in messages: |
| 33 | if getattr(m, "role", None) == "tool": |
| 34 | return None |
| 35 | chosen_name: Optional[str] = None |
| 36 | if isinstance(tool_choice, dict): |
| 37 | fn_raw = tool_choice.get("function") |
| 38 | if isinstance(fn_raw, dict): |
| 39 | name_raw = fn_raw.get("name") |
| 40 | if isinstance(name_raw, str): |
| 41 | chosen_name = name_raw |
| 42 | elif isinstance(tool_choice, str): |
| 43 | lc = tool_choice.lower() |
| 44 | if lc in ("none", "no", "off"): |
| 45 | return None |
| 46 | if lc in ("auto", "required", "any"): |
| 47 | if isinstance(tools, list) and len(tools) == 1: |
| 48 | first_tool = tools[0] |
| 49 | func_raw = first_tool.get("function", {}) |
| 50 | if isinstance(func_raw, dict): |
| 51 | name_from_func = func_raw.get("name") |
| 52 | if isinstance(name_from_func, str): |
| 53 | chosen_name = name_from_func |
| 54 | if not chosen_name: |
| 55 | name_from_tool = first_tool.get("name") |
| 56 | if isinstance(name_from_tool, str): |
| 57 | chosen_name = name_from_tool |
| 58 | else: |
| 59 | chosen_name = tool_choice |
| 60 | elif tool_choice is None: |
| 61 | # Do not execute actively |
| 62 | return None |
| 63 | |
| 64 | if not chosen_name: |
| 65 | return None |
| 66 | |
| 67 | user_text = get_latest_user_text(messages) |
| 68 | args_json = extract_json_from_text(user_text) or "{}" |
| 69 | result_str = await execute_tool_call(chosen_name, args_json) |