mirror of
https://github.com/anthropics/claude-code-sdk-python.git
synced 2025-07-07 14:45:00 +00:00
refactor: consolidate strict_mcp_config example into quick_start.py
- Remove separate strict_mcp_config_example.py file - Add with_strict_mcp_config_example() to quick_start.py - Use simpler query() function for consistency - Add comment to main() about uncommenting MCP example if servers configured
This commit is contained in:
parent
6a6991f48c
commit
b73eac8c57
2 changed files with 31 additions and 41 deletions
|
@ -65,11 +65,42 @@ async def with_tools_example():
|
|||
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__":
|
||||
|
|
|
@ -1,41 +0,0 @@
|
|||
"""Example demonstrating how to use strict MCP config with Claude SDK.
|
||||
|
||||
This example shows how to use the strict_mcp_config option to ensure
|
||||
only your programmatically specified MCP servers are used, ignoring
|
||||
any global or project-level MCP configurations.
|
||||
"""
|
||||
|
||||
from claude_code_sdk import ClaudeCodeSDK, ClaudeCodeOptions
|
||||
|
||||
async def main():
|
||||
# Create options with strict MCP config enabled
|
||||
# This ensures ONLY the MCP servers specified here will be used
|
||||
options = ClaudeCodeOptions(
|
||||
mcp_servers={
|
||||
"my-custom-server": {
|
||||
"command": "npx",
|
||||
"args": ["@modelcontextprotocol/server-memory"],
|
||||
}
|
||||
},
|
||||
strict_mcp_config=True, # Ignore all file-based MCP configurations
|
||||
)
|
||||
|
||||
# Create SDK instance
|
||||
sdk = ClaudeCodeSDK()
|
||||
|
||||
# Query Claude with strict MCP config
|
||||
async with await sdk.query(
|
||||
"List the available MCP tools from the memory server",
|
||||
options=options
|
||||
) as session:
|
||||
async for message in session.stream():
|
||||
if message.type == "assistant":
|
||||
print(f"Claude: {message.message.content}")
|
||||
elif message.type == "result":
|
||||
print(f"\nResult: {message.subtype}")
|
||||
if message.result:
|
||||
print(f"Final output: {message.result}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
import asyncio
|
||||
asyncio.run(main())
|
Loading…
Add table
Add a link
Reference in a new issue