Lee-W commented on code in PR #74026:
URL: https://github.com/apache/airflow/pull/74026#discussion_r4398444582


##########
providers/common/ai/src/airflow/providers/common/ai/example_dags/example_claude_agent_sdk.py:
##########
@@ -0,0 +1,88 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""
+Answer a question about a database with the Claude Agent SDK's agent loop, in 
an Airflow task.
+
+The agent loop is the SDK's own ``query()``, which runs the bundled Claude 
Code CLI as a
+subprocess. Airflow supplies the API key, from a connection, and the tools:
+:class:`~airflow.providers.common.ai.tools.claude_agent_sdk.AirflowTools` 
serves a
+``SQLToolset`` to the CLI through an in-process MCP server, with read-only SQL 
validation,
+bounded results and Airflow's secret masker applied to every result. 
``AirflowTools.build_options``
+also turns off the CLI's built-in tools and every settings source on the 
worker, so the only
+tools the model can call are Airflow's.
+
+Before running:
+
+1. Install this provider's ``claude-agent-sdk`` extra:
+   ``pip install "apache-airflow-providers-common-ai[claude-agent-sdk]"``.
+2. Create a connection (``LLM_CONN_ID``, default ``anthropic_default``) whose 
password is
+   an Anthropic API key.
+3. Create a database connection (``DB_CONN_ID``, default ``sql_default``) 
whose hook is a
+   ``DbApiHook`` (e.g. SQLite, Postgres, MySQL).
+"""
+
+from __future__ import annotations
+
+import asyncio
+import os
+
+from claude_agent_sdk import query

Review Comment:
   The SDK, adapter and toolset imports now live inside the task, like the 
sibling examples.
   



##########
providers/common/ai/tests/unit/common/ai/tools/test_claude_agent_sdk.py:
##########
@@ -0,0 +1,637 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+from __future__ import annotations
+
+import asyncio
+import copy
+import importlib
+import json
+import sys
+from collections.abc import AsyncIterator
+from typing import Any
+from unittest import mock
+
+import pytest
+
+pytest.importorskip("claude_agent_sdk")
+
+from claude_agent_sdk import AssistantMessage, Message, ResultMessage, 
TextBlock, UserMessage
+from pydantic_ai.exceptions import ModelRetry
+
+from airflow.providers.common.ai.tools import AirflowTool, ToolCallError, 
ToolResult, collect_tools
+from airflow.providers.common.ai.tools.claude_agent_sdk import AirflowTools
+from airflow.providers.common.ai.toolsets.sql import SQLToolset
+from airflow.providers.common.ai.utils.query_results import 
DEFAULT_MAX_RESULT_BYTES
+
+from unit.common.ai.tools.test__from_toolset import _scripted
+from unit.common.ai.toolsets.test_sql import _make_mock_db_hook
+
+MODULE = "airflow.providers.common.ai.tools.claude_agent_sdk"
+
+
+def _tool(outcome: ToolResult | Exception, ran: list[dict[str, Any]] | None = 
None) -> AirflowTool:
+    """A ``lookup`` tool that returns or raises ``outcome``, recording its 
calls in ``ran``."""
+
+    async def function(arguments: dict[str, Any]) -> ToolResult:
+        if ran is not None:
+            ran.append(arguments)
+        if isinstance(outcome, Exception):
+            raise outcome
+        return outcome
+
+    return AirflowTool(
+        name="lookup",
+        description="Look something up.",
+        parameters={"type": "object", "properties": {"key": {"type": 
"string"}}, "required": ["key"]},
+        function=function,
+    )
+
+
+def _build_tools(*sources: Any, server_name: str = "airflow") -> 
tuple[AirflowTools, dict[str, Any]]:
+    """
+    Build ``AirflowTools``, with ``create_sdk_mcp_server`` mocked to capture 
its SDK tools.
+
+    The ``@tool`` decorator itself is not mocked: it is only a plain wrapper
+    (``SdkMcpTool(handler=handler, ...)``), so the handlers it returns run the 
same code
+    they would through the real MCP server. Returns the instance and its 
handlers by name.
+    """
+    captured: dict[str, Any] = {}
+
+    def _capture_server(*, name: str, version: str = "1.0.0", tools: Any = 
None) -> Any:
+        captured["sdk_tools"] = tools
+        return mock.sentinel.server
+
+    with mock.patch(f"{MODULE}.create_sdk_mcp_server", autospec=True) as 
mock_create_server:
+        mock_create_server.side_effect = _capture_server
+        tools = AirflowTools(*sources, server_name=server_name)
+    handlers = {sdk_tool.name: sdk_tool.handler for sdk_tool in 
captured["sdk_tools"]}
+    return tools, handlers
+
+
+def _assistant_message() -> AssistantMessage:
+    return AssistantMessage(content=[TextBlock(text="")], model="claude-test", 
stop_reason="tool_use")
+
+
+def _user_message() -> UserMessage:
+    return UserMessage(content="")
+
+
+def _result_message(**overrides: Any) -> ResultMessage:
+    fields: dict[str, Any] = {
+        "subtype": "success",
+        "duration_ms": 1,
+        "duration_api_ms": 1,
+        "is_error": False,
+        "num_turns": 1,
+        "session_id": "session-1",
+    }
+    fields.update(overrides)
+    return ResultMessage(**fields)
+
+
+async def _fake_cli(
+    handlers: dict[str, Any],
+    turns: list[list[tuple[str, dict[str, Any]]]],
+    *,
+    final: ResultMessage | None = None,
+) -> AsyncIterator[Message]:
+    """
+    Stand in for the CLI driving the tools ``AirflowTools`` served through one 
or more turns.
+
+    For each turn, yields an ``AssistantMessage`` with a ``tool_use`` stop 
reason, then
+    awaits every one of that turn's calls -- as the CLI waits for a turn's 
tool results
+    before sending the next request -- before yielding the ``UserMessage`` 
carrying them.
+    Ends with a ``ResultMessage``.
+    """
+    for turn in turns:
+        yield _assistant_message()
+        for tool_name, args in turn:
+            await handlers[tool_name](args)
+        yield _user_message()
+    yield final if final is not None else _result_message()
+
+
+async def _call_real_server(tools: AirflowTools, name: str, args: dict[str, 
Any]) -> dict[str, Any]:
+    """Call a tool through the real in-process MCP server, with the JSON-RPC 
the SDK feeds its bridge."""
+    return await _request_real_server(tools, "tools/call", {"name": name, 
"arguments": args})
+
+
+async def _request_real_server(tools: AirflowTools, method: str, params: 
dict[str, Any]) -> dict[str, Any]:
+    """Send one JSON-RPC request to the real in-process MCP server and return 
its ``result``."""
+    moved = (
+        "claude_agent_sdk's private path _internal.sdk_mcp_bridge.SdkMcpBridge 
has changed; "
+        "update _request_real_server in this test module."
+    )
+    try:
+        bridge_module = 
importlib.import_module("claude_agent_sdk._internal.sdk_mcp_bridge")
+    except ImportError:
+        pytest.fail(moved)
+    bridge_cls = getattr(bridge_module, "SdkMcpBridge", None)
+    if bridge_cls is None:
+        pytest.fail(moved)
+    bridge = bridge_cls("airflow", tools.server["instance"])
+    try:
+        await bridge.handle(
+            {
+                "jsonrpc": "2.0",
+                "id": 0,
+                "method": "initialize",
+                "params": {
+                    "protocolVersion": "2025-06-18",
+                    "capabilities": {},
+                    "clientInfo": {"name": "test", "version": "1"},
+                },
+            }
+        )
+        await bridge.handle({"jsonrpc": "2.0", "method": 
"notifications/initialized"})
+        reply = await bridge.handle(
+            {
+                "jsonrpc": "2.0",
+                "id": 1,
+                "method": method,
+                "params": params,
+            }
+        )
+        return reply["result"]
+    finally:
+        await bridge.aclose()
+
+
+class TestTools:
+    def test_each_toolset_tool_becomes_an_mcp_tool_with_its_schema(self):

Review Comment:
   Added a `tools/list` check through the real server that asserts name, 
description, `inputSchema` and the `_meta` max-size entry. The import is at the 
top now, and the mock uses `create_autospec`.
   



##########
providers/common/ai/docs/frameworks/claude_agent_sdk.rst:
##########
@@ -0,0 +1,219 @@
+ .. Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+
+ ..   http://www.apache.org/licenses/LICENSE-2.0
+
+ .. Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+
+.. _howto/frameworks:claude_agent_sdk:
+
+Claude Agent SDK
+=================
+
+If your agent is a loop on the `Claude Agent SDK
+<https://platform.claude.com/docs/en/api/agent-sdk/overview>`__'s ``query()``, 
which runs
+the Claude Code CLI as a subprocess, you can run it in an Airflow task as it 
is and give
+it the toolsets this provider ships.
+:class:`~airflow.providers.common.ai.tools.claude_agent_sdk.AirflowTools` 
serves
+``SQLToolset``, ``HookToolset`` and most other toolsets to the CLI through an 
in-process
+MCP server. If you have no loop yet, ``AgentOperator`` runs one for you, with 
durable
+replay and human review; see :doc:`index`.
+
+.. note::
+
+    ``AirflowTools`` and the framework-neutral tool interface it builds on are
+    experimental. They may change in a minor release of this provider.
+    See :ref:`howto/stability`.
+
+Run a loop in a task
+---------------------
+
+This task answers a question about a database. The client's API key comes from 
an
+Airflow connection:
+
+.. exampleinclude:: 
/../../ai/src/airflow/providers/common/ai/example_dags/example_claude_agent_sdk.py
+    :language: python
+    :start-after: [START example_claude_agent_sdk]
+    :end-before: [END example_claude_agent_sdk]
+
+The model can call ``list_tables``, ``get_schema`` and ``query``, the tools 
``SQLToolset``
+serves. Each call writes a group like this one to the task log. This output is
+illustrative, not captured from a real run; the tool name and timing vary with 
what the

Review Comment:
   Replaced with output from a real run calling `list_tables`, `get_schema` and 
`query`, and a real `is_error` result.
   



##########
providers/common/ai/docs/frameworks/claude_agent_sdk.rst:
##########
@@ -0,0 +1,219 @@
+ .. Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+
+ ..   http://www.apache.org/licenses/LICENSE-2.0
+
+ .. Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+
+.. _howto/frameworks:claude_agent_sdk:
+
+Claude Agent SDK
+=================
+
+If your agent is a loop on the `Claude Agent SDK
+<https://platform.claude.com/docs/en/api/agent-sdk/overview>`__'s ``query()``, 
which runs
+the Claude Code CLI as a subprocess, you can run it in an Airflow task as it 
is and give
+it the toolsets this provider ships.
+:class:`~airflow.providers.common.ai.tools.claude_agent_sdk.AirflowTools` 
serves
+``SQLToolset``, ``HookToolset`` and most other toolsets to the CLI through an 
in-process
+MCP server. If you have no loop yet, ``AgentOperator`` runs one for you, with 
durable
+replay and human review; see :doc:`index`.
+
+.. note::
+
+    ``AirflowTools`` and the framework-neutral tool interface it builds on are

Review Comment:
   Changed to the sibling wording: only `AirflowTools` is experimental, and the 
framework-neutral tool interface is stable.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to