mirror of
https://github.com/anthropics/claude-code-sdk-python.git
synced 2025-07-09 15:45:01 +00:00

- Simplify quick_start.py to just basic examples (~45 lines) - Move tools example to dedicated using_tools.py file - Move MCP configuration example to mcp_servers.py file - No new content added - just reorganized existing examples This makes the quick start truly quick and provides dedicated files for users who need specific advanced features.
43 lines
No EOL
1.1 KiB
Python
43 lines
No EOL
1.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Example demonstrating how to use tools with Claude Code SDK."""
|
|
|
|
import anyio
|
|
|
|
from claude_code_sdk import (
|
|
AssistantMessage,
|
|
ClaudeCodeOptions,
|
|
ResultMessage,
|
|
TextBlock,
|
|
query,
|
|
)
|
|
|
|
|
|
async def with_tools_example():
|
|
"""Example using tools."""
|
|
print("=== With Tools Example ===")
|
|
|
|
options = ClaudeCodeOptions(
|
|
allowed_tools=["Read", "Write"],
|
|
system_prompt="You are a helpful file assistant.",
|
|
)
|
|
|
|
async for message in query(
|
|
prompt="Create a file called hello.txt with 'Hello, World!' in it",
|
|
options=options,
|
|
):
|
|
if isinstance(message, AssistantMessage):
|
|
for block in message.content:
|
|
if isinstance(block, TextBlock):
|
|
print(f"Claude: {block.text}")
|
|
elif isinstance(message, ResultMessage) and message.total_cost_usd > 0:
|
|
print(f"\nCost: ${message.total_cost_usd:.4f}")
|
|
print()
|
|
|
|
|
|
async def main():
|
|
"""Run the example."""
|
|
await with_tools_example()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
anyio.run(main) |