From affe3c9d92426c992b076482b3e39087be3ce70d Mon Sep 17 00:00:00 2001 From: Lina Tawfik Date: Thu, 3 Jul 2025 16:57:11 -0700 Subject: [PATCH] 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. --- examples/mcp_servers.py | 50 ++++++++++++++++++++++++++++++++++++++ examples/quick_start.py | 54 ----------------------------------------- examples/using_tools.py | 43 ++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 54 deletions(-) create mode 100644 examples/mcp_servers.py create mode 100644 examples/using_tools.py diff --git a/examples/mcp_servers.py b/examples/mcp_servers.py new file mode 100644 index 0000000..dbf3121 --- /dev/null +++ b/examples/mcp_servers.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 +"""Example demonstrating MCP (Model Context Protocol) server configuration.""" + +import anyio + +from claude_code_sdk import ( + AssistantMessage, + ClaudeCodeOptions, + ResultMessage, + TextBlock, + query, +) + + +async def with_strict_mcp_config_example(): + """Example using strict MCP configuration.""" + print("=== Strict MCP Config Example ===") + + # This ensures ONLY the MCP servers specified here will be used, + # ignoring any global or project-level MCP configurations + options = ClaudeCodeOptions( + mcp_servers={ + "memory-server": { + "command": "npx", + "args": ["@modelcontextprotocol/server-memory"], + } + }, + strict_mcp_config=True, # Ignore all file-based MCP configurations + ) + + async for message in query( + prompt="List the available MCP tools from the memory server", + options=options, + ): + if isinstance(message, AssistantMessage): + for block in message.content: + if isinstance(block, TextBlock): + print(f"Claude: {block.text}") + elif isinstance(message, ResultMessage): + print(f"\nResult: {message.subtype}") + print() + + +async def main(): + """Run the example.""" + await with_strict_mcp_config_example() + + +if __name__ == "__main__": + anyio.run(main) \ No newline at end of file diff --git a/examples/quick_start.py b/examples/quick_start.py index a6dd715..cc1f2cc 100644 --- a/examples/quick_start.py +++ b/examples/quick_start.py @@ -43,64 +43,10 @@ async def with_options_example(): print() -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 with_strict_mcp_config_example(): - """Example using strict MCP configuration.""" - print("=== Strict MCP Config Example ===") - - # This ensures ONLY the MCP servers specified here will be used, - # ignoring any global or project-level MCP configurations - options = ClaudeCodeOptions( - mcp_servers={ - "memory-server": { - "command": "npx", - "args": ["@modelcontextprotocol/server-memory"], - } - }, - strict_mcp_config=True, # Ignore all file-based MCP configurations - ) - - async for message in query( - prompt="List the available MCP tools from the memory server", - options=options, - ): - if isinstance(message, AssistantMessage): - for block in message.content: - if isinstance(block, TextBlock): - print(f"Claude: {block.text}") - elif isinstance(message, ResultMessage): - print(f"\nResult: {message.subtype}") - print() - - async def main(): """Run all examples.""" await basic_example() await with_options_example() - await with_tools_example() - # Note: Uncomment the line below if you have MCP servers configured - # await with_strict_mcp_config_example() if __name__ == "__main__": diff --git a/examples/using_tools.py b/examples/using_tools.py new file mode 100644 index 0000000..eef0c0a --- /dev/null +++ b/examples/using_tools.py @@ -0,0 +1,43 @@ +#!/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) \ No newline at end of file