Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/claude_agent_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from dataclasses import replace
from typing import Any

from . import Transport
from ._errors import CLIConnectionError
from .types import ClaudeAgentOptions, HookEvent, HookMatcher, Message, ResultMessage

Expand Down Expand Up @@ -51,12 +52,17 @@ class ClaudeSDKClient:
exist.
"""

def __init__(self, options: ClaudeAgentOptions | None = None):
def __init__(
self,
options: ClaudeAgentOptions | None = None,
transport: Transport | None = None,
):
"""Initialize Claude SDK client."""
if options is None:
options = ClaudeAgentOptions()
self.options = options
self._transport: Any | None = None
self._custom_transport = transport
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is self._custom_transport necessary?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If _transport is assigned directly, the test case TestClaudeSDKClientEdgeCases.test_double_connect will fail. If there is no custom transport, client.connect() should ensure that the self._transport is renewed.

self._transport: Transport | None = None
self._query: Any | None = None
os.environ["CLAUDE_CODE_ENTRYPOINT"] = "sdk-py-client"

Expand Down Expand Up @@ -115,10 +121,14 @@ async def _empty_stream() -> AsyncIterator[dict[str, Any]]:
else:
options = self.options

self._transport = SubprocessCLITransport(
prompt=actual_prompt,
options=options,
)
# Use provided custom transport or create subprocess transport
if self._custom_transport:
self._transport = self._custom_transport
else:
self._transport = SubprocessCLITransport(
prompt=actual_prompt,
options=options,
)
await self._transport.connect()

# Extract SDK MCP servers from options
Expand Down
Loading