Python: [BREAKING] simplify ag-ui run logic, fix mcp bugs, fix anthropic client issues in ag-ui (#3322)

* Refactor ag-ui to simplify flow

* Refactoring

* Fix backend tool

* Update tests

* Improvements

* Fix mypy

* Fixes

* Fix json serialize errors
This commit is contained in:
Evan Mattson
2026-01-23 14:10:46 +09:00
committed by GitHub
Unverified
parent 9f893a32a6
commit 5436354a83
42 changed files with 2789 additions and 5395 deletions
@@ -89,8 +89,10 @@ def _merge_options(base: dict[str, Any], override: dict[str, Any]) -> dict[str,
if value is None:
continue
if key == "tools" and result.get("tools"):
# Combine tool lists
result["tools"] = list(result["tools"]) + list(value)
# Combine tool lists, avoiding duplicates by name
existing_names = {getattr(t, "name", None) for t in result["tools"]}
unique_new = [t for t in value if getattr(t, "name", None) not in existing_names]
result["tools"] = list(result["tools"]) + unique_new
elif key == "logit_bias" and result.get("logit_bias"):
# Merge logit_bias dicts
result["logit_bias"] = {**result["logit_bias"], **value}
+26 -1
View File
@@ -1553,7 +1553,7 @@ async def _auto_invoke_function(
runtime_kwargs: dict[str, Any] = {
key: value
for key, value in (custom_args or {}).items()
if key not in {"_function_middleware_pipeline", "middleware"}
if key not in {"_function_middleware_pipeline", "middleware", "conversation_id"}
}
try:
args = tool.input_model.model_validate(parsed_args)
@@ -1689,6 +1689,11 @@ async def _try_execute_function_calls(
tool_map = _get_tool_map(tools)
approval_tools = [tool_name for tool_name, tool in tool_map.items() if tool.approval_mode == "always_require"]
logger.debug(
"_try_execute_function_calls: tool_map keys=%s, approval_tools=%s",
list(tool_map.keys()),
approval_tools,
)
declaration_only = [tool_name for tool_name, tool in tool_map.items() if tool.declaration_only]
additional_tool_names = [tool.name for tool in config.additional_tools] if config.additional_tools else []
# check if any are calling functions that need approval
@@ -1696,7 +1701,15 @@ async def _try_execute_function_calls(
approval_needed = False
declaration_only_flag = False
for fcc in function_calls:
fcc_name = getattr(fcc, "name", None)
logger.debug(
"Checking function call: type=%s, name=%s, in approval_tools=%s",
fcc.type,
fcc_name,
fcc_name in approval_tools,
)
if fcc.type == "function_call" and fcc.name in approval_tools: # type: ignore[attr-defined]
logger.debug("Approval needed for function: %s", fcc.name)
approval_needed = True
break
if fcc.type == "function_call" and (fcc.name in declaration_only or fcc.name in additional_tool_names): # type: ignore[attr-defined]
@@ -1706,6 +1719,7 @@ async def _try_execute_function_calls(
raise KeyError(f'Error: Requested function "{fcc.name}" not found.') # type: ignore[attr-defined]
if approval_needed:
# approval can only be needed for Function Call Content, not Approval Responses.
logger.debug("Returning function_approval_request contents")
return (
[
Content.from_function_approval_request(id=fcc.call_id, function_call=fcc) # type: ignore[attr-defined, arg-type]
@@ -2148,6 +2162,17 @@ def _handle_function_calls_streaming_response(
# we load the tools here, since middleware might have changed them compared to before calling func.
tools = _extract_tools(options)
fc_count = len(function_calls) if function_calls else 0
logger.debug(
"Streaming: tools extracted=%s, function_calls=%d",
tools is not None,
fc_count,
)
if tools:
for t in tools if isinstance(tools, list) else [tools]:
t_name = getattr(t, "name", "unknown")
t_approval = getattr(t, "approval_mode", None)
logger.debug(" Tool %s: approval_mode=%s", t_name, t_approval)
if function_calls and tools:
# Use the stored middleware pipeline instead of extracting from kwargs
# because kwargs may have been modified by the underlying function
@@ -12,11 +12,6 @@ _IMPORTS = [
"AGUIChatClient",
"AGUIEventConverter",
"AGUIHttpService",
"ConfirmationStrategy",
"DefaultConfirmationStrategy",
"TaskPlannerConfirmationStrategy",
"RecipeConfirmationStrategy",
"DocumentWriterConfirmationStrategy",
]
@@ -5,11 +5,6 @@ from agent_framework_ag_ui import (
AGUIChatClient,
AGUIEventConverter,
AGUIHttpService,
ConfirmationStrategy,
DefaultConfirmationStrategy,
DocumentWriterConfirmationStrategy,
RecipeConfirmationStrategy,
TaskPlannerConfirmationStrategy,
__version__,
add_agent_framework_fastapi_endpoint,
)
@@ -19,11 +14,6 @@ __all__ = [
"AGUIEventConverter",
"AGUIHttpService",
"AgentFrameworkAgent",
"ConfirmationStrategy",
"DefaultConfirmationStrategy",
"DocumentWriterConfirmationStrategy",
"RecipeConfirmationStrategy",
"TaskPlannerConfirmationStrategy",
"__version__",
"add_agent_framework_fastapi_endpoint",
]