clawd-code-sdk 1.2.10__tar.gz → 1.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/PKG-INFO +1 -1
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/pyproject.toml +1 -1
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/_internal/transport/subprocess_utils.py +7 -12
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/client.py +7 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/models/control.py +5 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/models/messages.py +18 -1
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/models/options.py +50 -0
- clawd_code_sdk-1.3.0/tests/test_mcp_delivery.py +88 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/test_transport.py +39 -1
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/.gitignore +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/LICENSE +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/README.md +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/__init__.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/_bundled/.gitignore +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/_errors.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/_internal/__init__.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/_internal/mcp_compat.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/_internal/query.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/_internal/sdk_mcp_bridge.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/_internal/session_mutations.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/_internal/sessions.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/_internal/transport/__init__.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/_internal/transport/subprocess_cli.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/_version.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/anthropic_stream.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/instrumentation.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/list_sessions.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/mcp_utils.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/models/__init__.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/models/agents.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/models/anthropic_types.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/models/base.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/models/content_blocks.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/models/context_usage.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/models/hooks.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/models/input_types.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/models/mcp.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/models/output_types.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/models/permissions.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/models/prompt_requests.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/models/prompts.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/models/server_info.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/models/session.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/models/settings.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/models/system_messages.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/models/thinking.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/models/usage.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/py.typed +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/session.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/storage/ARCHITECTURE.md +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/storage/__init__.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/storage/helpers.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/storage/models.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/storage/replay.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/__init__.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/conftest.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/e2e/__init__.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/e2e/test_agents_and_settings.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/e2e/test_dynamic_control.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/e2e/test_hook_events.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/e2e/test_hooks.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/e2e/test_include_partial_messages.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/e2e/test_mcp_resources.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/e2e/test_mcp_tools.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/e2e/test_sdk_mcp_resources.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/e2e/test_sdk_mcp_tools.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/e2e/test_slash_commands.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/e2e/test_stderr_callback.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/e2e/test_storage_parsing.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/e2e/test_structured_output.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/e2e/test_subagent_invocation.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/e2e/test_tool_permissions.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/mcp_server.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/mock_claude_server.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/test_api_content_blocks.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/test_apply_flag_settings.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/test_changelog.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/test_client.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/test_elicitation_mcp_conversion.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/test_errors.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/test_get_usage.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/test_image.png +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/test_integration.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/test_line_framing.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/test_message_parser.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/test_result_error.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/test_sandbox_settings.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/test_sdk_0_3_types.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/test_sdk_mcp_integration.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/test_session.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/test_session_mutations.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/test_session_title.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/test_sessions.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/test_steer.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/test_streaming_client.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/test_subprocess_buffering.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/test_tool_callbacks.py +0 -0
- {clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/tests/test_transport_close.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: clawd-code-sdk
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.3.0
|
|
4
4
|
Summary: Python SDK for Claude Code
|
|
5
5
|
Project-URL: Documentation, https://github.com/phil65/claude-agent-sdk-python
|
|
6
6
|
Project-URL: Homepage, https://github.com/phil65/claude-agent-sdk-python
|
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
from contextlib import suppress
|
|
6
|
-
import dataclasses
|
|
7
6
|
import functools
|
|
8
7
|
import logging
|
|
9
8
|
from pathlib import Path
|
|
@@ -222,17 +221,13 @@ def to_cli_args(options: ClaudeAgentOptions) -> list[str]:
|
|
|
222
221
|
cmd.extend(["--add-dir", str(directory)])
|
|
223
222
|
|
|
224
223
|
match options.mcp_servers:
|
|
225
|
-
case dict()
|
|
226
|
-
servers_for_cli
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
for name, cfg in servers.items()
|
|
233
|
-
}
|
|
234
|
-
dct = anyenv.dump_json({"mcpServers": servers_for_cli})
|
|
235
|
-
cmd.extend(["--mcp-config", dct])
|
|
224
|
+
case dict():
|
|
225
|
+
if servers_for_cli := options.argv_mcp_servers():
|
|
226
|
+
dct = anyenv.dump_json({"mcpServers": servers_for_cli})
|
|
227
|
+
cmd.extend(["--mcp-config", dct])
|
|
228
|
+
if options.initialize_mcp_servers() is not None:
|
|
229
|
+
# The CLI reads the initialize request (with the other servers) at launch
|
|
230
|
+
cmd.append("--await-initialize")
|
|
236
231
|
case str() | Path() as path if str(path):
|
|
237
232
|
cmd.extend(["--mcp-config", str(path)])
|
|
238
233
|
|
|
@@ -227,6 +227,7 @@ class ClaudeSDKClient:
|
|
|
227
227
|
prompt_suggestions=options.prompt_suggestions,
|
|
228
228
|
exclude_dynamic_sections=options.exclude_dynamic_sections,
|
|
229
229
|
sdk_mcp_servers=list(query.sdk_mcp_servers.keys()) or None,
|
|
230
|
+
mcp_servers=(mcp_servers := options.initialize_mcp_servers()),
|
|
230
231
|
agent_progress_summaries=options.agent_progress_summaries,
|
|
231
232
|
title=title,
|
|
232
233
|
skills=options.skills,
|
|
@@ -238,6 +239,12 @@ class ClaudeSDKClient:
|
|
|
238
239
|
)
|
|
239
240
|
response = await query._send_control_request(request, timeout=initialize_timeout)
|
|
240
241
|
self._initialization_result = info = ClaudeCodeServerInfo.model_validate(response)
|
|
242
|
+
if mcp_servers is not None and info.mcp_servers_applied is not True:
|
|
243
|
+
# Absent on CLIs that predate the field: they never read `mcpServers`
|
|
244
|
+
raise CLIConnectionError(
|
|
245
|
+
"Claude Code did not take the MCP servers sent in the initialize request. "
|
|
246
|
+
'Update Claude Code (2.1.261 or newer) or set mcp_servers_delivery="argv".'
|
|
247
|
+
)
|
|
241
248
|
blocks_on_failure = any(
|
|
242
249
|
m.on_failure not in (None, "continue")
|
|
243
250
|
for matchers in (options.hooks or {}).values()
|
|
@@ -110,6 +110,11 @@ class SDKControlInitializeRequest(_ControlBase):
|
|
|
110
110
|
hooks: dict[HookEvent, Any] | None = None
|
|
111
111
|
agents: dict[str, AgentWireDefinition] | None = None
|
|
112
112
|
sdk_mcp_servers: list[str] | None = Field(default=None, serialization_alias="sdkMcpServers")
|
|
113
|
+
mcp_servers: dict[str, dict[str, Any]] | None = Field(
|
|
114
|
+
default=None, serialization_alias="mcpServers"
|
|
115
|
+
)
|
|
116
|
+
"""Servers for Claude Code to run or connect to, as one ``--mcp-config`` item would hold
|
|
117
|
+
them; read only at launch under ``--await-initialize`` (``mcp_servers_applied``)."""
|
|
113
118
|
system_prompt: list[str] | None = Field(default=None, serialization_alias="systemPrompt")
|
|
114
119
|
append_system_prompt: str | None = Field(default=None, serialization_alias="appendSystemPrompt")
|
|
115
120
|
exclude_dynamic_sections: bool | None = Field(
|
|
@@ -474,19 +474,36 @@ class SubagentStats(TypedDict):
|
|
|
474
474
|
by_type: dict[str, Any]
|
|
475
475
|
|
|
476
476
|
|
|
477
|
+
class AutoModeRuleMeta(TypedDict):
|
|
478
|
+
"""The auto-mode rule that decided a tool call."""
|
|
479
|
+
|
|
480
|
+
list: Literal["allow", "soft_deny"]
|
|
481
|
+
name: str
|
|
482
|
+
|
|
483
|
+
|
|
477
484
|
class ToolPermissionDecisionMeta(TypedDict):
|
|
478
485
|
"""How a tool call's permission was decided."""
|
|
479
486
|
|
|
480
487
|
decision: str
|
|
481
488
|
source: str
|
|
482
489
|
reason_type: NotRequired[str]
|
|
490
|
+
plugin_approval: NotRequired[str]
|
|
491
|
+
auto_mode_rule: NotRequired[AutoModeRuleMeta]
|
|
483
492
|
|
|
484
493
|
|
|
485
494
|
class ToolResultMeta(TypedDict):
|
|
486
|
-
"""Per-tool-result metadata attached to user messages (
|
|
495
|
+
"""Per-tool-result metadata attached to user messages (internal, display only)."""
|
|
487
496
|
|
|
488
497
|
id: str
|
|
498
|
+
non_execution_kind: NotRequired[str]
|
|
499
|
+
"""Why an ``is_error`` result holds no output of the tool's own: ``user-rejected``,
|
|
500
|
+
``permission-rule``, ``automode-*``, ``interrupted`` or ``cancelled`` (CLI 2.1.330+)."""
|
|
501
|
+
user_feedback: NotRequired[str]
|
|
502
|
+
remedy: NotRequired[Any]
|
|
503
|
+
"""How to get past a refusal (shape not published)."""
|
|
489
504
|
permission_decision: NotRequired[ToolPermissionDecisionMeta]
|
|
505
|
+
built_in_tool: NotRequired[str]
|
|
506
|
+
run_input: NotRequired[str]
|
|
490
507
|
|
|
491
508
|
|
|
492
509
|
class InvalidConfigFile(TypedDict):
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
+
import dataclasses
|
|
5
6
|
from dataclasses import dataclass, field
|
|
6
7
|
import json
|
|
7
8
|
import logging
|
|
@@ -153,6 +154,19 @@ class ClaudeAgentOptions:
|
|
|
153
154
|
# MCP
|
|
154
155
|
mcp_servers: dict[str, McpServerConfig] | str | Path = field(default_factory=dict)
|
|
155
156
|
"""MCP servers for the agent."""
|
|
157
|
+
mcp_servers_delivery: Literal["argv", "initialize"] = "initialize"
|
|
158
|
+
"""How the ``mcp_servers`` that Claude Code runs or connects to (stdio, SSE, HTTP) reach it.
|
|
159
|
+
|
|
160
|
+
- ``"initialize"`` (default): in the initialize request over stdin (the CLI is started
|
|
161
|
+
with ``--await-initialize``), so server ``env`` and ``headers`` stay off the command
|
|
162
|
+
line, where other users of the machine can read them. Needs Claude Code 2.1.261 or
|
|
163
|
+
newer; connecting fails if the CLI does not take them (``mcp_servers_applied``).
|
|
164
|
+
- ``"argv"``: in one ``--mcp-config`` flag, for older Claude Code. Deprecated upstream.
|
|
165
|
+
|
|
166
|
+
In-process servers (``McpSdkServerConfigWithInstance``) and an ``mcp_servers`` file path
|
|
167
|
+
are not affected, and a launch with ``--sdk-url`` in ``extra_args`` always uses
|
|
168
|
+
``--mcp-config``, as that session cannot read the request at launch.
|
|
169
|
+
"""
|
|
156
170
|
chrome: bool = False
|
|
157
171
|
"""Add the chrome-tools MCP server (-> Claude Code browser extension) to the agent."""
|
|
158
172
|
strict_mcp_config: bool = False
|
|
@@ -453,6 +467,33 @@ class ClaudeAgentOptions:
|
|
|
453
467
|
raise CLIConnectionError(f"{source} must contain a JSON object")
|
|
454
468
|
return loaded
|
|
455
469
|
|
|
470
|
+
def initialize_mcp_servers(self) -> dict[str, dict[str, Any]] | None:
|
|
471
|
+
"""The servers to send in the initialize request, or None to send none there."""
|
|
472
|
+
if self.mcp_servers_delivery != "initialize" or "sdk-url" in self.extra_args:
|
|
473
|
+
return None
|
|
474
|
+
if not isinstance(self.mcp_servers, dict):
|
|
475
|
+
return None
|
|
476
|
+
servers = {
|
|
477
|
+
name: _serialize_mcp_server(config)
|
|
478
|
+
for name, config in self.mcp_servers.items()
|
|
479
|
+
if config.type != "sdk"
|
|
480
|
+
}
|
|
481
|
+
return servers or None
|
|
482
|
+
|
|
483
|
+
def argv_mcp_servers(self) -> dict[str, dict[str, Any]]:
|
|
484
|
+
"""The servers for ``--mcp-config``.
|
|
485
|
+
|
|
486
|
+
All of them, or only the in-process ones when the others go in the initialize request.
|
|
487
|
+
"""
|
|
488
|
+
if not isinstance(self.mcp_servers, dict):
|
|
489
|
+
return {}
|
|
490
|
+
in_initialize = self.initialize_mcp_servers() is not None
|
|
491
|
+
return {
|
|
492
|
+
name: _serialize_mcp_server(config)
|
|
493
|
+
for name, config in self.mcp_servers.items()
|
|
494
|
+
if not in_initialize or config.type == "sdk"
|
|
495
|
+
}
|
|
496
|
+
|
|
456
497
|
def get_json_schema(self) -> dict[str, Any] | None:
|
|
457
498
|
from pydantic import TypeAdapter
|
|
458
499
|
|
|
@@ -476,3 +517,12 @@ class ClaudeAgentOptions:
|
|
|
476
517
|
return config
|
|
477
518
|
case _ as unreachable:
|
|
478
519
|
assert_never(unreachable)
|
|
520
|
+
|
|
521
|
+
|
|
522
|
+
def _serialize_mcp_server(config: McpServerConfig) -> dict[str, Any]:
|
|
523
|
+
"""A server config as the CLI takes it: no live instance, no unset fields."""
|
|
524
|
+
return {
|
|
525
|
+
key: value
|
|
526
|
+
for key, value in dataclasses.asdict(config).items()
|
|
527
|
+
if key != "instance" and value is not None
|
|
528
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"""MCP servers sent in the initialize request (`mcp_servers_delivery="initialize"`)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
from typing import TYPE_CHECKING, Any
|
|
7
|
+
|
|
8
|
+
import anyio
|
|
9
|
+
import pytest
|
|
10
|
+
|
|
11
|
+
from clawd_code_sdk import ClaudeSDKClient
|
|
12
|
+
from clawd_code_sdk._errors import CLIConnectionError
|
|
13
|
+
from clawd_code_sdk._internal.transport import Transport
|
|
14
|
+
from clawd_code_sdk.models import ClaudeAgentOptions, McpStdioServerConfig
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
if TYPE_CHECKING:
|
|
18
|
+
from collections.abc import AsyncIterator
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class _InitializeTransport(Transport):
|
|
22
|
+
"""Answers the initialize request with `answer` and records what was sent."""
|
|
23
|
+
|
|
24
|
+
def __init__(self, answer: dict[str, Any]) -> None:
|
|
25
|
+
self.answer = answer
|
|
26
|
+
self.sent: list[dict[str, Any]] = []
|
|
27
|
+
self._send, self._receive = anyio.create_memory_object_stream[dict[str, Any]](10)
|
|
28
|
+
|
|
29
|
+
async def connect(self) -> None:
|
|
30
|
+
pass
|
|
31
|
+
|
|
32
|
+
async def write(self, data: str) -> None:
|
|
33
|
+
message = json.loads(data)
|
|
34
|
+
self.sent.append(message)
|
|
35
|
+
if message.get("type") == "control_request":
|
|
36
|
+
response = {"subtype": "success", "request_id": message["request_id"]}
|
|
37
|
+
await self._send.send(
|
|
38
|
+
{
|
|
39
|
+
"type": "control_response",
|
|
40
|
+
"response": response | {"response": self.answer},
|
|
41
|
+
}
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
async def read_messages(self) -> AsyncIterator[dict[str, Any]]:
|
|
45
|
+
async for message in self._receive:
|
|
46
|
+
yield message
|
|
47
|
+
|
|
48
|
+
async def close(self) -> None:
|
|
49
|
+
self._send.close()
|
|
50
|
+
|
|
51
|
+
async def end_input(self) -> None:
|
|
52
|
+
pass
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _options() -> ClaudeAgentOptions:
|
|
56
|
+
server = McpStdioServerConfig(command="/srv", env={"TOKEN": "s3cret"})
|
|
57
|
+
return ClaudeAgentOptions(mcp_servers={"stdio": server})
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
async def test_servers_are_sent_and_must_be_applied() -> None:
|
|
61
|
+
transport = _InitializeTransport({"mcp_servers_applied": True})
|
|
62
|
+
client = ClaudeSDKClient(_options(), transport=transport)
|
|
63
|
+
await client.connect()
|
|
64
|
+
await client.disconnect()
|
|
65
|
+
[initialize] = [m for m in transport.sent if m.get("type") == "control_request"]
|
|
66
|
+
servers = initialize["request"]["mcpServers"]
|
|
67
|
+
assert servers == {
|
|
68
|
+
"stdio": {"type": "stdio", "command": "/srv", "args": [], "env": {"TOKEN": "s3cret"}}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
@pytest.mark.parametrize("answer", [{}, {"mcp_servers_applied": False}])
|
|
73
|
+
async def test_connecting_fails_when_the_cli_does_not_take_them(answer: dict[str, Any]) -> None:
|
|
74
|
+
client = ClaudeSDKClient(_options(), transport=_InitializeTransport(answer))
|
|
75
|
+
with pytest.raises(CLIConnectionError, match="mcp_servers_delivery"):
|
|
76
|
+
await client.connect()
|
|
77
|
+
await client.disconnect()
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
async def test_argv_delivery_sends_none_in_initialize() -> None:
|
|
81
|
+
options = _options()
|
|
82
|
+
options.mcp_servers_delivery = "argv"
|
|
83
|
+
transport = _InitializeTransport({})
|
|
84
|
+
client = ClaudeSDKClient(options, transport=transport)
|
|
85
|
+
await client.connect()
|
|
86
|
+
await client.disconnect()
|
|
87
|
+
[initialize] = [m for m in transport.sent if m.get("type") == "control_request"]
|
|
88
|
+
assert "mcpServers" not in initialize["request"]
|
|
@@ -20,6 +20,8 @@ from clawd_code_sdk.models import (
|
|
|
20
20
|
AgentDefinition,
|
|
21
21
|
ClaudeAgentOptions,
|
|
22
22
|
ClaudeCodeSettings,
|
|
23
|
+
McpHttpServerConfig,
|
|
24
|
+
McpSdkServerConfigWithInstance,
|
|
23
25
|
McpSSEServerConfig,
|
|
24
26
|
McpStdioServerConfig,
|
|
25
27
|
Network,
|
|
@@ -190,9 +192,10 @@ class TestSubprocessCLITransport:
|
|
|
190
192
|
args=["--option", "value"],
|
|
191
193
|
)
|
|
192
194
|
}
|
|
193
|
-
opts = ClaudeAgentOptions(mcp_servers=mcp_servers)
|
|
195
|
+
opts = ClaudeAgentOptions(mcp_servers=mcp_servers, mcp_servers_delivery="argv")
|
|
194
196
|
transport = self._make_transport(options=opts)
|
|
195
197
|
cmd = transport._build_command()
|
|
198
|
+
assert "--await-initialize" not in cmd
|
|
196
199
|
# Find the --mcp-config flag and its value
|
|
197
200
|
assert "--mcp-config" in cmd
|
|
198
201
|
mcp_idx = cmd.index("--mcp-config")
|
|
@@ -204,6 +207,41 @@ class TestSubprocessCLITransport:
|
|
|
204
207
|
assert server["command"] == "/path/to/server"
|
|
205
208
|
assert server["args"] == ["--option", "value"]
|
|
206
209
|
|
|
210
|
+
def test_mcp_servers_go_in_the_initialize_request_by_default(self):
|
|
211
|
+
"""Server env and headers stay off the command line; SDK servers keep their entry."""
|
|
212
|
+
secret_env = {"TOKEN": "s3cret"}
|
|
213
|
+
mcp_servers = {
|
|
214
|
+
"stdio": McpStdioServerConfig(command="/srv", env=secret_env),
|
|
215
|
+
"web": McpHttpServerConfig(url="https://mcp.test", headers={"auth": "s3cret"}),
|
|
216
|
+
"local": McpSdkServerConfigWithInstance(name="local", instance=MagicMock()),
|
|
217
|
+
}
|
|
218
|
+
opts = ClaudeAgentOptions(mcp_servers=mcp_servers)
|
|
219
|
+
cmd = self._make_transport(options=opts)._build_command()
|
|
220
|
+
assert "--await-initialize" in cmd
|
|
221
|
+
assert "s3cret" not in " ".join(cmd)
|
|
222
|
+
argv = json.loads(cmd[cmd.index("--mcp-config") + 1])["mcpServers"]
|
|
223
|
+
assert argv == {"local": {"type": "sdk", "name": "local"}}
|
|
224
|
+
sent = opts.initialize_mcp_servers()
|
|
225
|
+
assert sent is not None
|
|
226
|
+
assert sent["stdio"]["env"] == secret_env
|
|
227
|
+
assert sent["web"] == {
|
|
228
|
+
"type": "http",
|
|
229
|
+
"url": "https://mcp.test",
|
|
230
|
+
"headers": {"auth": "s3cret"},
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
def test_sdk_url_and_sdk_only_servers_stay_on_the_command_line(self):
|
|
234
|
+
stdio = {"stdio": McpStdioServerConfig(command="/srv")}
|
|
235
|
+
opts = ClaudeAgentOptions(mcp_servers=stdio, extra_args={"sdk-url": "wss://x"})
|
|
236
|
+
cmd = self._make_transport(options=opts)._build_command()
|
|
237
|
+
assert "--await-initialize" not in cmd
|
|
238
|
+
assert "stdio" in json.loads(cmd[cmd.index("--mcp-config") + 1])["mcpServers"]
|
|
239
|
+
local = {"local": McpSdkServerConfigWithInstance(name="local", instance=MagicMock())}
|
|
240
|
+
opts = ClaudeAgentOptions(mcp_servers=local)
|
|
241
|
+
cmd = self._make_transport(options=opts)._build_command()
|
|
242
|
+
assert "--await-initialize" not in cmd
|
|
243
|
+
assert opts.initialize_mcp_servers() is None
|
|
244
|
+
|
|
207
245
|
def test_build_command_with_mcp_servers_as_file_path(self):
|
|
208
246
|
"""Test building CLI command with mcp_servers as file path."""
|
|
209
247
|
# Test with string path
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/_internal/sdk_mcp_bridge.py
RENAMED
|
File without changes
|
{clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/_internal/session_mutations.py
RENAMED
|
File without changes
|
|
File without changes
|
{clawd_code_sdk-1.2.10 → clawd_code_sdk-1.3.0}/src/clawd_code_sdk/_internal/transport/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|