claude-code-sdk-python/examples/using_tools.py
Lina Tawfik affe3c9d92
refactor: reorganize examples for better clarity
- 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.
2025-07-03 16:59:36 -07:00

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)