-
Notifications
You must be signed in to change notification settings - Fork 414
Expand file tree
/
Copy pathsdk_integration.py
More file actions
771 lines (674 loc) · 30.8 KB
/
Copy pathsdk_integration.py
File metadata and controls
771 lines (674 loc) · 30.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
"""Claude Code Python SDK integration."""
import asyncio
import os
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, AsyncIterator, Callable, Dict, List, Optional
import structlog
from claude_agent_sdk import (
AssistantMessage,
ClaudeAgentOptions,
ClaudeSDKClient,
ClaudeSDKError,
CLIConnectionError,
CLIJSONDecodeError,
CLINotFoundError,
Message,
PermissionResultAllow,
PermissionResultDeny,
ProcessError,
ResultMessage,
TextBlock,
ThinkingBlock,
ToolPermissionContext,
ToolUseBlock,
UserMessage,
)
from claude_agent_sdk._errors import MessageParseError
from claude_agent_sdk._internal.message_parser import parse_message
from claude_agent_sdk.types import StreamEvent
from ..config.settings import Settings
from ..security.validators import SecurityValidator
from .exceptions import (
ClaudeMCPError,
ClaudeParsingError,
ClaudeProcessError,
ClaudeTimeoutError,
)
from .monitor import _is_claude_internal_path, check_bash_directory_boundary
logger = structlog.get_logger()
# Fallback message when Claude produces no text but did use tools.
TASK_COMPLETED_MSG = "✅ Task completed. Tools used: {tools_summary}"
@dataclass
class ClaudeResponse:
"""Response from Claude Code SDK."""
content: str
session_id: str
cost: float
duration_ms: int
num_turns: int
is_error: bool = False
error_type: Optional[str] = None
tools_used: List[Dict[str, Any]] = field(default_factory=list)
interrupted: bool = False
@dataclass
class StreamUpdate:
"""Streaming update from Claude SDK."""
type: str # 'assistant', 'user', 'system', 'result', 'stream_delta'
content: Optional[str] = None
tool_calls: Optional[List[Dict[str, Any]]] = None
metadata: Optional[Dict[str, Any]] = None
progress: Optional[Dict[str, Any]] = None
def get_tool_names(self) -> List[str]:
"""Return tool names from the stream payload."""
names: List[str] = []
if self.tool_calls:
for tool_call in self.tool_calls:
name = tool_call.get("name") if isinstance(tool_call, dict) else None
if isinstance(name, str) and name:
names.append(name)
if self.metadata:
tool_name = self.metadata.get("tool_name")
if isinstance(tool_name, str) and tool_name:
names.append(tool_name)
metadata_tools = self.metadata.get("tools")
if isinstance(metadata_tools, list):
for tool in metadata_tools:
if isinstance(tool, dict):
name = tool.get("name")
elif isinstance(tool, str):
name = tool
else:
name = None
if isinstance(name, str) and name:
names.append(name)
# Preserve insertion order while de-duplicating.
return list(dict.fromkeys(names))
def is_error(self) -> bool:
"""Check whether this stream update represents an error."""
if self.type == "error":
return True
if self.metadata:
if self.metadata.get("is_error") is True:
return True
status = self.metadata.get("status")
if isinstance(status, str) and status.lower() == "error":
return True
error_val = self.metadata.get("error")
if isinstance(error_val, str) and error_val:
return True
error_msg_val = self.metadata.get("error_message")
if isinstance(error_msg_val, str) and error_msg_val:
return True
if self.progress:
status = self.progress.get("status")
if isinstance(status, str) and status.lower() == "error":
return True
return False
def get_error_message(self) -> str:
"""Get the best available error message from the stream payload."""
if self.metadata:
for key in ("error_message", "error", "message"):
value = self.metadata.get(key)
if isinstance(value, str) and value.strip():
return value
if isinstance(self.content, str) and self.content.strip():
return self.content
if self.progress:
value = self.progress.get("error")
if isinstance(value, str) and value.strip():
return value
return "Unknown error"
def get_progress_percentage(self) -> Optional[int]:
"""Extract progress percentage if present."""
def _to_int(value: Any) -> Optional[int]:
if isinstance(value, (int, float)):
return int(value)
if isinstance(value, str) and value.strip():
try:
return int(float(value))
except ValueError:
return None
return None
if self.progress:
for key in ("percentage", "percent", "progress"):
percentage = _to_int(self.progress.get(key))
if percentage is not None:
return max(0, min(100, percentage))
step = _to_int(self.progress.get("step"))
total_steps = _to_int(self.progress.get("total_steps"))
if step is not None and total_steps and total_steps > 0:
return max(0, min(100, int((step / total_steps) * 100)))
if self.metadata:
percentage = _to_int(self.metadata.get("progress_percentage"))
if percentage is not None:
return max(0, min(100, percentage))
return None
def _make_can_use_tool_callback(
security_validator: SecurityValidator,
working_directory: Path,
approved_directory: Path,
) -> Any:
"""Create a can_use_tool callback for SDK-level tool permission validation.
The callback validates file path boundaries and bash directory boundaries
*before* the SDK executes the tool, providing preventive security enforcement.
"""
_FILE_TOOLS = {"Write", "Edit", "Read", "create_file", "edit_file", "read_file"}
_BASH_TOOLS = {"Bash", "bash", "shell"}
async def can_use_tool(
tool_name: str,
tool_input: Dict[str, Any],
context: ToolPermissionContext,
) -> Any:
# File path validation
if tool_name in _FILE_TOOLS:
file_path = tool_input.get("file_path") or tool_input.get("path")
if file_path:
# Allow Claude Code internal paths (~/.claude/plans/, etc.)
if _is_claude_internal_path(file_path):
return PermissionResultAllow()
valid, _resolved, error = security_validator.validate_path(
file_path, working_directory
)
if not valid:
logger.warning(
"can_use_tool denied file operation",
tool_name=tool_name,
file_path=file_path,
error=error,
)
return PermissionResultDeny(message=error or "Invalid file path")
# Bash directory boundary validation
if tool_name in _BASH_TOOLS:
command = tool_input.get("command", "")
if command:
valid, error = check_bash_directory_boundary(
command, working_directory, approved_directory
)
if not valid:
logger.warning(
"can_use_tool denied bash command",
tool_name=tool_name,
command=command,
error=error,
)
return PermissionResultDeny(
message=error or "Bash directory boundary violation"
)
return PermissionResultAllow()
return can_use_tool
class ClaudeSDKManager:
"""Manage Claude Code SDK integration."""
def __init__(
self,
config: Settings,
security_validator: Optional[SecurityValidator] = None,
):
"""Initialize SDK manager with configuration."""
self.config = config
self.security_validator = security_validator
# Set up environment for Claude Code SDK if API key is provided
# If no API key is provided, the SDK will use existing CLI authentication
if config.anthropic_api_key_str:
os.environ["ANTHROPIC_API_KEY"] = config.anthropic_api_key_str
logger.info("Using provided API key for Claude SDK authentication")
else:
logger.info("No API key provided, using existing Claude CLI authentication")
def _is_retryable_error(self, exc: BaseException) -> bool:
"""Return True for transient errors that warrant a retry.
asyncio.TimeoutError is intentional (user-configured timeout) — not retried.
Only non-MCP CLIConnectionError is considered transient.
"""
if isinstance(exc, CLIConnectionError):
msg = str(exc).lower()
return "mcp" not in msg # "server" alone is too broad
return False
async def execute_command(
self,
prompt: str,
working_directory: Path,
session_id: Optional[str] = None,
continue_session: bool = False,
stream_callback: Optional[Callable[[StreamUpdate], None]] = None,
interrupt_event: Optional[asyncio.Event] = None,
images: Optional[List[Dict[str, str]]] = None,
) -> ClaudeResponse:
"""Execute Claude Code command via SDK."""
start_time = asyncio.get_event_loop().time()
logger.info(
"Starting Claude SDK command",
working_directory=str(working_directory),
session_id=session_id,
continue_session=continue_session,
)
try:
# Capture stderr from Claude CLI for better error diagnostics
stderr_lines: List[str] = []
def _stderr_callback(line: str) -> None:
stderr_lines.append(line)
logger.debug("Claude CLI stderr", line=line)
# Build system prompt, loading CLAUDE.md from working directory if present
base_prompt = (
f"All file operations must stay within {working_directory}. "
"Use relative paths."
)
claude_md_path = Path(working_directory) / "CLAUDE.md"
if claude_md_path.exists():
base_prompt += "\n\n" + claude_md_path.read_text(encoding="utf-8")
logger.info(
"Loaded CLAUDE.md into system prompt",
path=str(claude_md_path),
)
# When DISABLE_TOOL_VALIDATION=true, pass [] (not None) for
# allowed/disallowed tools. claude-agent-sdk subprocess_cli.py
# calls list(options.allowed_tools) unconditionally and crashes
# with "'NoneType' object is not iterable". Empty list is treated
# by the CLI as "no --allowedTools flag" → no restriction, which
# is the intent of DISABLE_TOOL_VALIDATION=true.
if self.config.disable_tool_validation:
sdk_allowed_tools = []
sdk_disallowed_tools = []
else:
sdk_allowed_tools = self.config.claude_allowed_tools
sdk_disallowed_tools = self.config.claude_disallowed_tools
# Build Claude Agent options
options = ClaudeAgentOptions(
max_turns=self.config.claude_max_turns,
model=self.config.claude_model or None,
max_budget_usd=self.config.claude_max_cost_per_request,
cwd=str(working_directory),
allowed_tools=sdk_allowed_tools,
disallowed_tools=sdk_disallowed_tools,
cli_path=self.config.claude_cli_path or None,
include_partial_messages=stream_callback is not None,
sandbox={
"enabled": self.config.sandbox_enabled,
"autoAllowBashIfSandboxed": True,
"excludedCommands": self.config.sandbox_excluded_commands or [],
},
system_prompt=base_prompt,
setting_sources=["project"],
stderr=_stderr_callback,
)
# Pass MCP server configuration if enabled
if self.config.enable_mcp and self.config.mcp_config_path:
options.mcp_servers = self._load_mcp_config(self.config.mcp_config_path)
logger.info(
"MCP servers configured",
mcp_config_path=str(self.config.mcp_config_path),
)
# Wire can_use_tool callback for preventive tool validation
if self.security_validator:
options.can_use_tool = _make_can_use_tool_callback(
security_validator=self.security_validator,
working_directory=working_directory,
approved_directory=self.config.approved_directory,
)
# Resume previous session if we have a session_id
if session_id and continue_session:
options.resume = session_id
logger.info(
"Resuming previous session",
session_id=session_id,
)
# Collect messages via ClaudeSDKClient
messages: List[Message] = []
interrupted = False
async def _run_client() -> None:
client = ClaudeSDKClient(options)
try:
await client.connect()
if images:
content_blocks: List[Dict[str, Any]] = []
for img in images:
media_type = img.get("media_type", "image/png")
content_blocks.append(
{
"type": "image",
"source": {
"type": "base64",
"media_type": media_type,
"data": img["data"],
},
}
)
content_blocks.append({"type": "text", "text": prompt})
multimodal_msg = {
"type": "user",
"message": {
"role": "user",
"content": content_blocks,
},
}
async def _multimodal_prompt() -> AsyncIterator[Dict[str, Any]]:
yield multimodal_msg
await client.query(_multimodal_prompt())
else:
await client.query(prompt)
async for raw_data in client._query.receive_messages():
try:
message = parse_message(raw_data)
except MessageParseError as e:
logger.debug(
"Skipping unparseable message",
error=str(e),
)
continue
messages.append(message)
if isinstance(message, ResultMessage):
break
# Handle streaming callback
if stream_callback:
try:
await self._handle_stream_message(
message, stream_callback
)
except Exception as callback_error:
logger.warning(
"Stream callback failed",
error=str(callback_error),
error_type=type(callback_error).__name__,
)
finally:
await client.disconnect()
# Execute with timeout and retry, racing against optional interrupt
max_attempts = max(1, self.config.claude_retry_max_attempts)
last_exc: Optional[BaseException] = None
for attempt in range(max_attempts):
# Reset message accumulator each attempt so that a failed attempt
# does not pollute the next one with partial/duplicate messages.
# _run_client() closes over `messages` by reference (late-binding
# closure), so clearing it here is seen by every new call.
messages.clear()
if attempt > 0:
delay = min(
self.config.claude_retry_base_delay
* (self.config.claude_retry_backoff_factor ** (attempt - 1)),
self.config.claude_retry_max_delay,
)
logger.warning(
"Retrying Claude SDK command",
attempt=attempt + 1,
max_attempts=max_attempts,
delay_seconds=delay,
)
await asyncio.sleep(delay)
run_task = asyncio.create_task(_run_client())
interrupt_watcher: Optional["asyncio.Task[None]"] = None
if interrupt_event is not None:
async def _cancel_on_interrupt() -> None:
nonlocal interrupted
await interrupt_event.wait()
interrupted = True
run_task.cancel()
interrupt_watcher = asyncio.create_task(_cancel_on_interrupt())
# Note: asyncio.TimeoutError is intentionally NOT retried —
# it reflects a user-configured hard limit.
try:
await asyncio.wait_for(
asyncio.shield(run_task),
timeout=self.config.claude_timeout_seconds,
)
break # success — exit retry loop
except asyncio.CancelledError:
if not interrupted:
raise
# Interrupt cancelled the task — wait for cleanup
try:
await run_task
except asyncio.CancelledError:
pass
break # user interrupted — don't retry
except asyncio.TimeoutError:
run_task.cancel()
try:
await run_task
except asyncio.CancelledError:
pass
raise # timeout — don't retry
except CLIConnectionError as exc:
if self._is_retryable_error(exc) and attempt < max_attempts - 1:
last_exc = exc
logger.warning(
"Transient connection error, will retry",
attempt=attempt + 1,
error=str(exc),
)
continue
raise # non-retryable or attempts exhausted
finally:
if interrupt_watcher is not None:
interrupt_watcher.cancel()
else:
if last_exc is not None:
raise last_exc
# Extract cost, tools, and session_id from result message
cost = 0.0
tools_used: List[Dict[str, Any]] = []
claude_session_id = None
result_content = None
for message in messages:
if isinstance(message, ResultMessage):
cost = getattr(message, "total_cost_usd", 0.0) or 0.0
claude_session_id = getattr(message, "session_id", None)
result_content = getattr(message, "result", None)
current_time = asyncio.get_event_loop().time()
for msg in messages:
if isinstance(msg, AssistantMessage):
msg_content = getattr(msg, "content", [])
if msg_content and isinstance(msg_content, list):
for block in msg_content:
if isinstance(block, ToolUseBlock):
tools_used.append(
{
"name": getattr(
block, "name", "unknown"
),
"timestamp": current_time,
"input": getattr(block, "input", {}),
}
)
break
# Fallback: extract session_id from StreamEvent messages if
# ResultMessage didn't provide one (can happen with some CLI versions)
if not claude_session_id:
for message in messages:
msg_session_id = getattr(message, "session_id", None)
if msg_session_id and not isinstance(message, ResultMessage):
claude_session_id = msg_session_id
logger.info(
"Got session ID from stream event (fallback)",
session_id=claude_session_id,
)
break
# Calculate duration
duration_ms = int((asyncio.get_event_loop().time() - start_time) * 1000)
# Use Claude's session_id if available, otherwise fall back
final_session_id = claude_session_id or session_id or ""
if claude_session_id and claude_session_id != session_id:
logger.info(
"Got session ID from Claude",
claude_session_id=claude_session_id,
previous_session_id=session_id,
)
# Use ResultMessage.result if available, fall back to message extraction
if result_content is not None:
content = str(result_content).strip()
else:
content_parts = []
for msg in messages:
if isinstance(msg, AssistantMessage):
msg_content = getattr(msg, "content", [])
if msg_content and isinstance(msg_content, list):
for block in msg_content:
if hasattr(block, "text"):
content_parts.append(block.text)
elif msg_content:
content_parts.append(str(msg_content))
content = "\n".join(content_parts).strip()
if not content and tools_used:
tool_names = [
tool.get("name", "")
for tool in tools_used
if isinstance(tool.get("name"), str) and tool.get("name")
]
unique_tool_names = list(dict.fromkeys(tool_names))
tools_summary = ", ".join(unique_tool_names) or "unknown"
content = TASK_COMPLETED_MSG.format(tools_summary=tools_summary)
return ClaudeResponse(
content=content,
session_id=final_session_id,
cost=cost,
duration_ms=duration_ms,
num_turns=len(
[
m
for m in messages
if isinstance(m, (UserMessage, AssistantMessage))
]
),
tools_used=tools_used,
interrupted=interrupted,
)
except asyncio.TimeoutError:
logger.error(
"Claude SDK command timed out",
timeout_seconds=self.config.claude_timeout_seconds,
)
raise ClaudeTimeoutError(
f"Claude SDK timed out after {self.config.claude_timeout_seconds}s"
)
except CLINotFoundError as e:
logger.error("Claude CLI not found", error=str(e))
error_msg = (
"Claude Code not found. Please ensure Claude is installed:\n"
" npm install -g @anthropic-ai/claude-code\n\n"
"If already installed, try one of these:\n"
" 1. Add Claude to your PATH\n"
" 2. Create a symlink: ln -s $(which claude) /usr/local/bin/claude\n"
" 3. Set CLAUDE_CLI_PATH environment variable"
)
raise ClaudeProcessError(error_msg)
except ProcessError as e:
error_str = str(e)
# Include captured stderr for better diagnostics
captured_stderr = "\n".join(stderr_lines[-20:]) if stderr_lines else ""
if captured_stderr:
error_str = f"{error_str}\nStderr: {captured_stderr}"
logger.error(
"Claude process failed",
error=error_str,
exit_code=getattr(e, "exit_code", None),
stderr=captured_stderr or None,
)
# Check if the process error is MCP-related
if "mcp" in error_str.lower():
raise ClaudeMCPError(f"MCP server error: {error_str}")
raise ClaudeProcessError(f"Claude process error: {error_str}")
except CLIConnectionError as e:
error_str = str(e)
logger.error("Claude connection error", error=error_str)
# Check if the connection error is MCP-related
if "mcp" in error_str.lower() or "server" in error_str.lower():
raise ClaudeMCPError(f"MCP server connection failed: {error_str}")
raise ClaudeProcessError(f"Failed to connect to Claude: {error_str}")
except CLIJSONDecodeError as e:
logger.error("Claude SDK JSON decode error", error=str(e))
raise ClaudeParsingError(f"Failed to decode Claude response: {str(e)}")
except ClaudeSDKError as e:
logger.error("Claude SDK error", error=str(e))
raise ClaudeProcessError(f"Claude SDK error: {str(e)}")
except Exception as e:
exceptions = getattr(e, "exceptions", None)
if exceptions is not None:
# ExceptionGroup from TaskGroup operations (Python 3.11+)
logger.error(
"Task group error in Claude SDK",
error=str(e),
error_type=type(e).__name__,
exception_count=len(exceptions),
exceptions=[str(ex) for ex in exceptions[:3]],
)
raise ClaudeProcessError(
f"Claude SDK task error: {exceptions[0] if exceptions else e}"
)
logger.error(
"Unexpected error in Claude SDK",
error=str(e),
error_type=type(e).__name__,
)
raise ClaudeProcessError(f"Unexpected error: {str(e)}")
async def _handle_stream_message(
self, message: Message, stream_callback: Callable[[StreamUpdate], None]
) -> None:
"""Handle streaming message from claude-agent-sdk."""
try:
if isinstance(message, AssistantMessage):
# Extract content from assistant message
content = getattr(message, "content", [])
text_parts = []
tool_calls = []
if content and isinstance(content, list):
for block in content:
if isinstance(block, ToolUseBlock):
tool_calls.append(
{
"name": block.name,
"input": block.input,
"id": block.id,
}
)
elif isinstance(block, TextBlock):
text_parts.append(block.text)
elif isinstance(block, ThinkingBlock):
text_parts.append(block.thinking)
if text_parts or tool_calls:
update = StreamUpdate(
type="assistant",
content=("\n".join(text_parts) if text_parts else None),
tool_calls=tool_calls if tool_calls else None,
)
await stream_callback(update)
elif content:
# Fallback for non-list content
update = StreamUpdate(
type="assistant",
content=str(content),
)
await stream_callback(update)
elif isinstance(message, StreamEvent):
event = message.event or {}
if event.get("type") == "content_block_delta":
delta = event.get("delta", {})
if delta.get("type") == "text_delta":
text = delta.get("text", "")
if text:
update = StreamUpdate(
type="stream_delta",
content=text,
)
await stream_callback(update)
elif isinstance(message, UserMessage):
content = getattr(message, "content", "")
if content:
update = StreamUpdate(
type="user",
content=content,
)
await stream_callback(update)
except Exception as e:
logger.warning("Stream callback failed", error=str(e))
def _load_mcp_config(self, config_path: Path) -> Dict[str, Any]:
"""Load MCP server configuration from a JSON file.
The new claude-agent-sdk expects mcp_servers as a dict, not a file path.
"""
import json
try:
with open(config_path) as f:
config_data = json.load(f)
return config_data.get("mcpServers", {})
except (json.JSONDecodeError, OSError) as e:
logger.error(
"Failed to load MCP config", path=str(config_path), error=str(e)
)
return {}