Skip to content

Port bug fixes and features from Python SDK#13

Open
Savid wants to merge 2 commits intomasterfrom
feat/python-sdk-parity-port
Open

Port bug fixes and features from Python SDK#13
Savid wants to merge 2 commits intomasterfrom
feat/python-sdk-parity-port

Conversation

@Savid
Copy link
Copy Markdown
Member

@Savid Savid commented Apr 10, 2026

Summary

Syncs the Go SDK with upstream changes from the Python Claude Agent SDK since commit 4e8e7c9fde85d92a53d7a7ca890cd0131c3f754a (38 commits, filtering to 6 meaningful changes).

Bug Fixes

  • Fix thinking flag CLI args (python#796) — ThinkingConfigAdaptive and ThinkingConfigDisabled were incorrectly mapped to --max-thinking-tokens 32000 / --max-thinking-tokens 0. The CLI (v2.1.96+) now expects --thinking adaptive and --thinking disabled. Only ThinkingConfigEnabled (with explicit budget) still uses --max-thinking-tokens.

  • Fix --setting-sources passed when empty (python#778) — When SettingSources was nil or empty, the SDK passed --setting-sources "" to the CLI, which caused the CLI's arg parser to consume the next flag as the value. Now only emits the flag when len > 0, consistent with AllowedTools, DisallowedTools, and Betas.

Features

  • ExcludeDynamicSections in SystemPromptPreset (python#797) — When true, the CLI strips per-user dynamic sections (working directory, auto-memory, git status) from the system prompt so it's byte-identical across users, enabling cross-user prompt caching. Sent as excludeDynamicSections in the initialize control request. New WithExcludeDynamicSections(bool) option.

  • WithSessionID option (python#750) — Adds SessionID field mapped to --session-id CLI flag, allowing users to specify a custom session ID (distinct from --resume which resumes an existing session).

  • ToolUseID in PermissionContext (python#754) — Exposes the tool_use_id field (already sent by the CLI and extracted for deduplication) in the PermissionContext passed to CanUseTool callbacks. Enables permission logic based on specific tool call IDs.

  • MCP maxResultSizeChars via _meta (python#756) — SDK MCP tools can now declare WithMaxResultSizeChars(n) to override the CLI's default 50K character hard limit for tool output. The value is forwarded as _meta: {"anthropic/maxResultSizeChars": n} in tools/list responses.

Already present in Go SDK (no changes needed)

These Python SDK additions were already implemented in the Go SDK:

  • "auto" PermissionMode
  • AgentDefinition background/effort/permissionMode fields
  • control_cancel_request handling
  • get_context_usage() / GetContextUsage()

Not applicable

  • Python Annotated type for per-parameter @tool descriptions (Python-specific)
  • String prompt in connect() fix (Go SDK already handles this correctly)

Files changed (15)

File Change
internal/cli/command.go Thinking flags, session-id flag, setting-sources guard
internal/cli/command_test.go New tests for all CLI flag changes
internal/cli/cli_test.go Updated existing thinking config tests
internal/config/options.go SessionID field, updated doc comment
internal/config/preset.go ExcludeDynamicSections field on SystemPromptPreset
internal/protocol/session.go excludeDynamicSections in initialize payload, ToolUseID in parsePermissionContext
internal/protocol/session_test.go Extended permission context metadata test
internal/permission/permission.go ToolUseID field on Context
internal/mcp/sdk_server.go AddToolWithOptions, _meta in ListTools
internal/mcp/sdk_server_test.go Tests for maxResultSizeChars in ListTools
options.go WithSessionID, WithExcludeDynamicSections options
sdk_mcp_tool.go MaxResultSizeChars field, WithMaxResultSizeChars option
sdk_mcp_tool_test.go Tests for maxResultSizeChars option and server integration
sdk_mcp_server.go Pass MaxResultSizeChars through to internal server
examples/extended_thinking/main.go Updated comment

Test plan

  • go test -race ./... — all tests pass
  • golangci-lint run --new-from-rev=origin/master — 0 issues
  • New tests for thinking config (table-driven, 3 modes + negative)
  • New tests for setting-sources empty/non-empty
  • New tests for session-id flag
  • Extended existing permission context metadata test for ToolUseID
  • New tests for maxResultSizeChars (internal + public + integration)

🤖 Generated with Claude Code

Syncs the Go SDK with changes from the Python Claude Agent SDK since
commit 4e8e7c9, covering 2 bug fixes and 4 new features.

Bug fixes:
- Use --thinking adaptive/disabled flags instead of --max-thinking-tokens
  for non-budget thinking modes (aligns with CLI v2.1.96+)
- Only pass --setting-sources when non-empty to avoid CLI arg parser issues

Features:
- Add ExcludeDynamicSections to SystemPromptPreset for cross-user prompt caching
- Add WithSessionID option (--session-id CLI flag)
- Expose ToolUseID in PermissionContext for tool permission callbacks
- Add WithMaxResultSizeChars for MCP SDK tools to bypass 50K char truncation

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@Savid Savid force-pushed the feat/python-sdk-parity-port branch from 42c747d to 2f1f18d Compare April 10, 2026 07:34
CLI v2.1.98 does not thread --permission-prompt-tool through to the
streaming mode options object. The flag value is only included in the
--print (one-shot) path, but streaming mode (--input-format stream-json)
constructs its options without permissionPromptToolName, causing the
permission handler to use the default auto-approve path instead of the
control protocol.

This is confirmed as a CLI bug affecting both the Go and Python SDKs.
Tests are left failing until the CLI fix lands.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@Savid Savid force-pushed the feat/python-sdk-parity-port branch from 2f1f18d to d3df7ec Compare April 10, 2026 07:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant