mirror of
https://github.com/anthropics/claude-code-sdk-python.git
synced 2025-07-07 14:45:00 +00:00
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.
This commit is contained in:
parent
b123767bc1
commit
affe3c9d92
3 changed files with 93 additions and 54 deletions
50
examples/mcp_servers.py
Normal file
50
examples/mcp_servers.py
Normal file
|
@ -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)
|
|
@ -43,64 +43,10 @@ async def with_options_example():
|
||||||
print()
|
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():
|
async def main():
|
||||||
"""Run all examples."""
|
"""Run all examples."""
|
||||||
await basic_example()
|
await basic_example()
|
||||||
await with_options_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__":
|
if __name__ == "__main__":
|
||||||
|
|
43
examples/using_tools.py
Normal file
43
examples/using_tools.py
Normal file
|
@ -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)
|
Loading…
Add table
Add a link
Reference in a new issue