Commit graph

79 commits

Author SHA1 Message Date
Dickson Tsai
aecba20ccc
Add extra_args field to ClaudeCodeOptions for forward compatibility
- Add extra_args dict field to pass arbitrary CLI flags
- Support both valued flags (--flag value) and boolean flags (--flag)
- Add comprehensive tests for the new functionality
- Allows SDK to work with future CLI flags without code changes

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-04 15:07:56 -07:00
Sam Fu
8f4b7777b9
remove mcp_tools field from ClaudeCodeOptions (#110)
Some checks are pending
Lint / lint (push) Waiting to run
Test / test (3.10) (push) Waiting to run
Test / test (3.11) (push) Waiting to run
Test / test (3.12) (push) Waiting to run
Test / test (3.13) (push) Waiting to run
the `mcp_tools` field in `ClaudeCodeOptions` seems to have been there
since the initial commit but I couldn't find any references in the repo,
so I believe it may be vestigial and unused
2025-08-04 11:14:01 -07:00
github-actions[bot]
6255850eb2
chore: bump version to 0.0.19 (#105)
Some checks failed
Lint / lint (push) Has been cancelled
Test / test (3.10) (push) Has been cancelled
Test / test (3.11) (push) Has been cancelled
Test / test (3.12) (push) Has been cancelled
Test / test (3.13) (push) Has been cancelled
This PR updates the version to 0.0.19 after publishing to PyPI.

## Changes
- Updated version in `pyproject.toml`
- Updated version in `src/claude_code_sdk/__init__.py`

## Release Information
- Published to PyPI: https://pypi.org/project/claude-code-sdk/0.0.19/
- Install with: `pip install claude-code-sdk==0.0.19`

🤖 Generated by GitHub Actions

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2025-07-31 22:37:33 -07:00
Dickson Tsai
0cb5efa923
Support --add-dir flag (#104) 2025-07-31 20:52:30 -07:00
Dickson Tsai
fbda510ee4
Fix subprocess deadlock with MCP servers via stderr redirection (#103)
Some checks are pending
Lint / lint (push) Waiting to run
Test / test (3.10) (push) Waiting to run
Test / test (3.11) (push) Waiting to run
Test / test (3.12) (push) Waiting to run
Test / test (3.13) (push) Waiting to run
## Summary

Fixes a critical deadlock issue that occurs when MCP servers produce
verbose stderr output. The SDK would hang indefinitely when the stderr
pipe buffer filled up.

## The Problem

The deadlock occurred due to sequential reading of subprocess streams:
1. SDK reads stdout completely before reading stderr
2. When stderr pipe buffer fills (64KB on Linux, 16KB on macOS),
subprocess blocks on write
3. Subprocess can't continue to stdout, parent waits for stdout →
**DEADLOCK** 🔒

## The Solution  

Redirect stderr to a temporary file instead of a pipe:
- **No pipe buffer** = no possibility of deadlock
- Temp file can grow as needed (no 64KB limit)
- Still capture stderr for error reporting (last 100 lines)
- Works consistently across all async backends

## Implementation Details

- `stderr=tempfile.NamedTemporaryFile()` instead of `stderr=PIPE`
- Use `deque(maxlen=100)` to keep only recent stderr lines in memory
- Temp file is automatically cleaned up on disconnect
- Add `[stderr truncated, showing last 100 lines]` message when buffer
is full

## Testing

- Verified no deadlock with 150+ lines of stderr output
- Confirmed stderr is still captured for error reporting
- All existing tests pass
- Works with asyncio, trio, and other anyio backends

## Impact

- Fixes consistent hangs in production with MCP servers
- No functional regression - stderr handling is preserved
- Simpler than concurrent reading alternatives
- More robust than pipe-based solutions

Fixes the issue reported in Slack where SDK would hang indefinitely when
receiving messages from MCP servers with verbose logging.

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

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-07-31 11:42:20 -07:00
github-actions[bot]
0de87a2a96
chore: bump version to 0.0.18 (#102)
Some checks are pending
Lint / lint (push) Waiting to run
Test / test (3.10) (push) Waiting to run
Test / test (3.11) (push) Waiting to run
Test / test (3.12) (push) Waiting to run
Test / test (3.13) (push) Waiting to run
This PR updates the version to 0.0.18 after publishing to PyPI.

## Changes
- Updated version in `pyproject.toml`
- Updated version in `src/claude_code_sdk/__init__.py`

## Release Information
- Published to PyPI: https://pypi.org/project/claude-code-sdk/0.0.18/
- Install with: `pip install claude-code-sdk==0.0.18`

🤖 Generated by GitHub Actions

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2025-07-31 11:02:29 -07:00
Dickson Tsai
fa3962de3f
Improve UserMessage types to include ToolResultBlock (#101)
Fixes https://github.com/anthropics/claude-code-sdk-python/issues/90
2025-07-31 07:51:39 -07:00
Dickson Tsai
df94948edc
Add settings option to ClaudeCodeOptions (#100)
Some checks are pending
Lint / lint (push) Waiting to run
Test / test (3.10) (push) Waiting to run
Test / test (3.11) (push) Waiting to run
Test / test (3.12) (push) Waiting to run
Test / test (3.13) (push) Waiting to run
## Summary
- Add `settings` field to `ClaudeCodeOptions` to expose the `--settings`
CLI flag
- Allow SDK users to specify custom settings configuration path

## Changes
- Added `settings: str | None = None` field to `ClaudeCodeOptions`
dataclass
- Added CLI argument conversion logic in `SubprocessCLITransport` to
pass `--settings` flag to Claude Code CLI

## Test plan
- [x] All existing tests pass
- [x] Linting passes (`python -m ruff check`)
- [x] Type checking passes (`python -m mypy src/`)

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

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-07-30 23:59:21 -07:00
github-actions[bot]
472aa23aae
chore: bump version to 0.0.17 (#88)
Some checks failed
Lint / lint (push) Has been cancelled
Test / test (3.10) (push) Has been cancelled
Test / test (3.11) (push) Has been cancelled
Test / test (3.12) (push) Has been cancelled
Test / test (3.13) (push) Has been cancelled
This PR updates the version to 0.0.17 after publishing to PyPI.

## Changes
- Updated version in `pyproject.toml`
- Updated version in `src/claude_code_sdk/__init__.py`

## Release Information
- Published to PyPI: https://pypi.org/project/claude-code-sdk/0.0.17/
- Install with: `pip install claude-code-sdk==0.0.17`

🤖 Generated by GitHub Actions

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2025-07-23 22:52:20 -07:00
Dickson Tsai
5f8351fce9
Make streaming implementation trio-compatible (#84)
Some checks are pending
Lint / lint (push) Waiting to run
Test / test (3.10) (push) Waiting to run
Test / test (3.11) (push) Waiting to run
Test / test (3.12) (push) Waiting to run
Test / test (3.13) (push) Waiting to run
## Summary
- Replace asyncio.create_task() with anyio task group for trio
compatibility
- Update client.py docstring example to use anyio.sleep
- Add trio example demonstrating multi-turn conversation

## Details
The SDK already uses anyio for most async operations, but one line was
using asyncio.create_task() which broke trio compatibility. This PR
fixes that by using anyio's task group API with proper lifecycle
management.

### Changes:
1. **subprocess_cli.py**: Replace asyncio.create_task() with anyio task
group, ensuring proper cleanup on disconnect
2. **client.py**: Update docstring example to use anyio.sleep instead of
asyncio.sleep
3. **streaming_mode_trio.py**: Add new example showing how to use the
SDK with trio

## Test plan
- [x] All existing tests pass
- [x] Manually tested with trio runtime (created test script that
successfully runs multi-turn conversation)
- [x] Linting and type checking pass

🤖 Generated with [Claude Code](https://claude.ai/code)
2025-07-22 23:31:42 -07:00
Dickson Tsai
b25cdf81c9
Add changelog and changelog format check (#77)
Some checks failed
Lint / lint (push) Has been cancelled
Test / test (3.10) (push) Has been cancelled
Test / test (3.11) (push) Has been cancelled
Test / test (3.12) (push) Has been cancelled
Test / test (3.13) (push) Has been cancelled
2025-07-21 10:53:01 -07:00
github-actions[bot]
1d8ba02062
chore: bump version to 0.0.16 (#83)
This PR updates the version to 0.0.16 after publishing to PyPI.

## Changes
- Updated version in `pyproject.toml`
- Updated version in `src/claude_code_sdk/__init__.py`

## Release Information
- Published to PyPI: https://pypi.org/project/claude-code-sdk/0.0.16/
- Install with: `pip install claude-code-sdk==0.0.16`

🤖 Generated by GitHub Actions

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2025-07-21 09:55:50 -07:00
Dickson Tsai
21428b4f4d
Fix "Publish to PyPI" workflow: Add commit signing and improve diff (#82)
* Adds commit signing
* Converts sed pattern matching to a script to ensure we don't update
version values unrelated to PyPI
* Remove the `Publish to Test PyPI first'` step since it no longer works
2025-07-21 09:53:24 -07:00
Dickson Tsai
c4384ead71
Merge pull request #75 from anthropics/dickson/streaming
Some checks failed
Lint / lint (push) Has been cancelled
Test / test (3.10) (push) Has been cancelled
Test / test (3.11) (push) Has been cancelled
Test / test (3.12) (push) Has been cancelled
Test / test (3.13) (push) Has been cancelled
Implement streaming
2025-07-19 22:38:15 -07:00
Dickson Tsai
e852710d8c
Remove hardcoded timeout for control messages to match Typescript SDK 2025-07-19 20:43:07 -07:00
Dickson Tsai
5325dea9fd
Lint 2025-07-19 20:19:40 -07:00
Dickson Tsai
3e7da418ce
Fix json error handling 2025-07-19 20:16:45 -07:00
Dickson Tsai
8e652d7d87
Fix lint and test 2025-07-19 20:04:58 -07:00
Dickson Tsai
b57e05afa5
Improve examples 2025-07-19 19:57:17 -07:00
Dickson Tsai
3b56577b2f
CLAUDE.md 2025-07-19 19:12:15 -07:00
Dickson Tsai
739a5723f9
PR feedback 2025-07-19 19:12:07 -07:00
Dickson Tsai
9520852839
Fix types 2025-07-19 18:47:07 -07:00
Dickson Tsai
a813a4d665
Fix lint 2025-07-19 15:26:30 -07:00
Dickson Tsai
e65c2f417a
Fix test 2025-07-19 15:25:34 -07:00
Dickson Tsai
c95c077b9b
Ruff 2025-07-19 15:21:02 -07:00
Dickson Tsai
712948c2e7
Fix test 2025-07-19 15:20:02 -07:00
Dickson Tsai
eeb0be9955
Close stdin for query() 2025-07-19 15:01:43 -07:00
Dickson Tsai
489677d614
Add tests 2025-07-19 13:57:52 -07:00
Dickson Tsai
6c25bf7d37
Fix examples 2025-07-19 13:44:53 -07:00
Dickson Tsai
361c7e0be3
Working examples 2025-07-19 13:23:53 -07:00
Dickson Tsai
6dd12b0df8
Implement proper client and bidi streaming 2025-07-19 10:43:23 -07:00
Dickson Tsai
f4cff21590
Finalize streaming impl 2025-07-18 00:16:18 -07:00
Dickson Tsai
4c67548d87
Initial implementation of bidi streaming 2025-07-17 13:44:31 -07:00
Lina Tawfik
343ec4812c
Merge pull request #42 from RomainGehrig/fix/cwd-not-exists
Some checks failed
Test / test (3.13) (push) Has been cancelled
Lint / lint (push) Has been cancelled
Test / test (3.10) (push) Has been cancelled
Test / test (3.11) (push) Has been cancelled
Test / test (3.12) (push) Has been cancelled
Explicit error if the cwd does not exist
2025-07-05 14:29:03 -07:00
Lina Tawfik
5a4f617309
Merge pull request #49 from anthropics/add-claude-issue-triage
Some checks failed
Test / test (3.12) (push) Has been cancelled
Test / test (3.13) (push) Has been cancelled
Lint / lint (push) Has been cancelled
Test / test (3.10) (push) Has been cancelled
Test / test (3.11) (push) Has been cancelled
Add Claude issue triage workflow
2025-07-03 11:21:18 -07:00
Lina Tawfik
7a9f606e5f
Add Claude issue triage workflow
- Uses claude-code-base-action for automated issue triage
- Analyzes new issues and applies appropriate labels
- Uses GitHub MCP server for issue operations
- Requires ANTHROPIC_API_KEY secret to be configured

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-03 11:15:41 -07:00
Lina Tawfik
de27b6bf89
Merge pull request #65 from anthropics/chore/bump-version-0.0.14-signed
Some checks failed
Lint / lint (push) Has been cancelled
Test / test (3.10) (push) Has been cancelled
Test / test (3.11) (push) Has been cancelled
Test / test (3.12) (push) Has been cancelled
Test / test (3.13) (push) Has been cancelled
chore: bump version to 0.0.14
2025-07-01 20:40:21 -07:00
Lina Tawfik
e4de22eba4
Merge pull request #60 from anthropics/fix-fastapi-streaming-issue-4
Fix FastAPI SSE streaming compatibility
2025-07-01 20:40:12 -07:00
Lina Tawfik
1c792f155f
chore: bump version to 0.0.14
This PR updates the version to 0.0.14 after publishing to PyPI.

## Changes
- Updated version in `pyproject.toml`
- Updated version in `src/claude_code_sdk/__init__.py`

## Release Information
- Published to PyPI: https://pypi.org/project/claude-code-sdk/0.0.14/
- Install with: `pip install claude-code-sdk==0.0.14`

## Next Steps
After merging this PR, a release tag will be created automatically.

Signed-off-by: Lina <lina@anthropic.com>
2025-07-01 18:31:56 -07:00
Lina Tawfik
be040c6161
Fix linting issues - use lowercase variable names 2025-07-01 16:27:32 -07:00
Lina Tawfik
4460dfbe3b
Improve FastAPI SSE streaming fix with safety limits
Add critical safety mechanisms to the sequential stderr reading approach:
- 10MB memory limit on stderr collection with truncation message
- 30 second timeout protection to prevent hanging
- Proper exit code based error detection instead of string matching
- Debug logging for stderr on successful runs
- Better exception handling for edge cases

This addresses potential production issues while maintaining FastAPI compatibility.

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-01 16:24:47 -07:00
Lina Tawfik
67193350e2
Remove implementation-specific test
The test was checking source code for specific strings rather than
testing behavior. The existing subprocess tests already verify the
functionality works correctly.
2025-07-01 15:23:55 -07:00
Lina Tawfik
9ef9da9ab6
Fix linting issues 2025-07-01 15:12:26 -07:00
Romain Gehrig
d336a22ab4
Explicit error if the cwd does not exist
Previously was raised as a CLINotFoundError
2025-07-01 16:33:30 +02:00
Lina Tawfik
404c50bce0
Fix FastAPI SSE streaming compatibility (fixes #4)
Remove anyio.create_task_group() from receive_messages() to fix the
RuntimeError "Attempted to exit cancel scope in a different task than
it was entered in" when using the SDK with FastAPI's SSE streaming.

The issue occurred because FastAPI can move async generators between
different asyncio tasks during the streaming lifecycle, which conflicts
with anyio's cancel scope tracking.

Changes:
- Remove task group usage from receive_messages()
- Read stderr sequentially after stdout completes
- Add test to ensure no task groups are used
- Fix existing test expectation for buffer overflow

This is a minimal fix that maintains compatibility while solving the
core issue. The trade-off is that stderr is read after stdout instead
of concurrently, but this is unlikely to cause issues in practice.

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-01 00:04:50 -07:00
Lina Tawfik
4af210ee8f
Merge pull request #53 from anthropics/fix/json-streaming-buffer
Some checks are pending
Lint / lint (push) Waiting to run
Test / test (3.10) (push) Waiting to run
Test / test (3.11) (push) Waiting to run
Test / test (3.12) (push) Waiting to run
Test / test (3.13) (push) Waiting to run
fix: handle JSON messages split across multiple stream reads
2025-06-30 23:03:27 -07:00
Lina Tawfik
8233533148
Add comment explaining JSON buffer accumulation logic 2025-06-30 23:00:54 -07:00
Lina Tawfik
1791031d20
chore: Remove obvious comments from code
Removed redundant comments that simply restate what the code is doing.
Kept only comments that provide valuable context or explain complex behavior.

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-06-30 22:27:48 -07:00
Lina Tawfik
3ab62b617d
fix: Pass proper Exception to CLIJSONDecodeError
The CLIJSONDecodeError constructor expects an Exception as the second
argument, not None. Changed to pass a ValueError with details about
the buffer size limit being exceeded.

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-06-30 22:02:02 -07:00
Lina Tawfik
977bf6438b
style: apply ruff formatting 2025-06-30 21:58:37 -07:00