1
+ #!/usr/bin/env python3
2
+ """Example demonstrating MCP (Model Context Protocol) server configuration."""
3
+
4
+ import anyio
5
+
6
+ from claude_code_sdk import (
7
+ AssistantMessage ,
8
+ ClaudeCodeOptions ,
9
+ ResultMessage ,
10
+ SystemMessage ,
11
+ TextBlock ,
12
+ query ,
13
+ )
14
+
15
+
16
+ async def basic_mcp_server_example ():
17
+ """Example with basic MCP server configuration."""
18
+ print ("=== Basic MCP Server Example ===" )
19
+
20
+ options = ClaudeCodeOptions (
21
+ mcp_servers = {
22
+ "memory-server" : {
23
+ "command" : "npx" ,
24
+ "args" : ["@modelcontextprotocol/server-memory" ],
25
+ }
26
+ }
27
+ )
28
+
29
+ async for message in query (
30
+ prompt = "What MCP tools are available from the memory server?" ,
31
+ options = options ,
32
+ ):
33
+ if isinstance (message , SystemMessage ) and message .subtype == "init" :
34
+ if "mcp_servers" in message .data :
35
+ print (f"Loaded { len (message .data ['mcp_servers' ])} MCP servers" )
36
+ elif isinstance (message , AssistantMessage ):
37
+ for block in message .content :
38
+ if isinstance (block , TextBlock ):
39
+ print (f"Claude: { block .text } " )
40
+
41
+
42
+ async def strict_mcp_config_example ():
43
+ """Example using strict MCP configuration to ignore file-based configs."""
44
+ print ("\n === Strict MCP Config Example ===" )
45
+
46
+ # This ensures ONLY the MCP servers specified here will be used,
47
+ # ignoring any global or project-level MCP configurations
48
+ options = ClaudeCodeOptions (
49
+ mcp_servers = {
50
+ "my-custom-server" : {
51
+ "command" : "node" ,
52
+ "args" : ["my-mcp-server.js" ],
53
+ }
54
+ },
55
+ strict_mcp_config = True , # Ignore all file-based MCP configurations
56
+ )
57
+
58
+ async for message in query (
59
+ prompt = "List the available MCP tools" ,
60
+ options = options ,
61
+ ):
62
+ if isinstance (message , SystemMessage ) and message .subtype == "init" :
63
+ if "mcp_servers" in message .data :
64
+ servers = message .data ["mcp_servers" ]
65
+ print (f"Loaded exactly { len (servers )} MCP servers (strict mode)" )
66
+ for server in servers :
67
+ print (f" - { server ['name' ]} " )
68
+ elif isinstance (message , AssistantMessage ):
69
+ for block in message .content :
70
+ if isinstance (block , TextBlock ):
71
+ print (f"Claude: { block .text } " )
72
+
73
+
74
+ async def multiple_mcp_servers_example ():
75
+ """Example with multiple MCP servers."""
76
+ print ("\n === Multiple MCP Servers Example ===" )
77
+
78
+ options = ClaudeCodeOptions (
79
+ mcp_servers = {
80
+ "filesystem" : {
81
+ "command" : "npx" ,
82
+ "args" : ["-y" , "@modelcontextprotocol/server-filesystem" , "/tmp" ],
83
+ },
84
+ "github" : {
85
+ "command" : "npx" ,
86
+ "args" : ["-y" , "@modelcontextprotocol/server-github" ],
87
+ "env" : {
88
+ "GITHUB_PERSONAL_ACCESS_TOKEN" : "your-token-here"
89
+ }
90
+ }
91
+ }
92
+ )
93
+
94
+ async for message in query (
95
+ prompt = "What MCP servers and tools do I have access to?" ,
96
+ options = options ,
97
+ ):
98
+ if isinstance (message , AssistantMessage ):
99
+ for block in message .content :
100
+ if isinstance (block , TextBlock ):
101
+ print (f"Claude: { block .text } " )
102
+
103
+
104
+ async def main ():
105
+ """Run MCP examples."""
106
+ print ("MCP (Model Context Protocol) Server Examples\n " )
107
+ print ("Note: These examples require MCP servers to be installed." )
108
+ print ("See: https://github.com/modelcontextprotocol\n " )
109
+
110
+ # Run basic example
111
+ await basic_mcp_server_example ()
112
+
113
+ # Uncomment to run other examples:
114
+ # await strict_mcp_config_example()
115
+ # await multiple_mcp_servers_example()
116
+
117
+
118
+ if __name__ == "__main__" :
119
+ anyio .run (main )
0 commit comments