claude-code-sdk-python/examples/mcp_servers.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

50 lines
No EOL
1.3 KiB
Python

#!/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)