claude-code-sdk-python/src/claude_code_sdk
Rushil Patel bc01cd7e9a
feat: enable custom transports (#91)
## Summary

This PR exposes the `Transport` interface in the public API, enabling
users to pass custom transport implementations to the `query()`
function. Previously, transport selection was internal and users had no
way to provide custom implementations.

**Primary Benefits:**
- 🔌 **Remote claude code** - Connect to Claude Code CLIs running
remotely
- The concrete use case here is to be able to implement a custom
transport that can communicate to claude code instances running in
remote sandboxes. Currently the the sdk only works with Claude Codes
running in a local subprocess limiting the scenarios in which this SDK
can be used.

## Changes

### Public API Changes
- **Exposed the previously internal `Transport` abstract base class** in
`claude_code_sdk.__init__.py`
- **Added `transport` parameter** to `query()` function signature
- **Updated docstring** with transport parameter documentation

### Internal Changes
- **Modified `InternalClient.process_query()`** to accept optional
transport parameter
- **Added transport selection logic** - use provided transport or
default to `SubprocessCLITransport`
- **Updated `__all__` exports** to include `Transport`

### Testing
- **Updated existing tests** to work with new transport parameter
- **Maintained backward compatibility** - all existing code continues to
work unchanged

## Testing

### Existing Tests
-  All existing unit tests pass with new transport parameter
-  Integration tests updated to mock new transport interface
-  Subprocess buffering tests continue to work with exposed transport

### New Functionality Testing
-  Verified custom transport can be passed to `query()`
-  Confirmed default behavior unchanged when no transport provided  
-  Validated transport lifecycle ( connect → receive → disconnect)
-  Tested transport interface compliance with abstract base class

## Example Usage

### Basic Custom Transport
```python
from claude_code_sdk import query, ClaudeCodeOptions, Transport

class MyCustomTransport(Transport):
    # Implement abstract methods: connect, disconnect, 
    # send_request, receive_messages, is_connected
    pass

transport = MyCustomTransport()
async for message in query(
    prompt="Hello",
    transport=transport
):
    print(message)
```

## Related
- #85 

🤖 Generated with [Claude Code](https://claude.ai/code)

---------

Signed-off-by: Rushil Patel <rpatel@codegen.com>
2025-08-19 13:28:42 -07:00
..
_internal feat: enable custom transports (#91) 2025-08-19 13:28:42 -07:00
__init__.py feat: enable custom transports (#91) 2025-08-19 13:28:42 -07:00
_errors.py Remove hardcoded timeout for control messages to match Typescript SDK 2025-07-19 20:43:07 -07:00
client.py Make streaming implementation trio-compatible (#84) 2025-07-22 23:31:42 -07:00
py.typed Initial Python SDK import 2025-06-12 00:16:19 -07:00
query.py feat: enable custom transports (#91) 2025-08-19 13:28:42 -07:00
types.py Add support for plan permission mode (#116) 2025-08-07 13:06:05 -07:00