-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmcp_setup.py
295 lines (260 loc) · 9.9 KB
/
mcp_setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env python3
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "python-dotenv"
# ]
# ///
import json
import os
import sys
import subprocess
from pathlib import Path
import argparse
import dotenv
# Package Configuration
PACKAGES_TO_INSTALL = {
# npm packages that will be run with npx
"npm": [
"@modelcontextprotocol/server-filesystem",
"@modelcontextprotocol/server-memory",
"@modelcontextprotocol/server-brave-search",
"@modelcontextprotocol/server-github",
"@patruff/server-terminator",
"@patruff/server-flux",
"@patruff/server-gmail-drive",
"@abhiz123/todoist-mcp-server",
"@patruff/server-lightrag",
"@patruff/server-codesnip"
],
# Python packages that will be run with uvx
"python": [
"mcp-server-sqlite",
"mcp-server-time",
"python-dotenv"
]
}
# API Keys Configuration
API_KEYS = {
"GIT_PAT_TOKEN": "",
"REPLICATE_API_TOKEN": "",
"BRAVE_API_KEY": "",
"TODOIST_API_TOKEN": "",
"LIGHTRAG_API_URL": "http://127.0.0.1:8020"
}
# MCP Server Configs that require API keys or credentials
MCP_API_REQUIREMENTS = {
"github": ["GIT_PAT_TOKEN"],
"terminator": ["GIT_PAT_TOKEN"],
"flux": ["REPLICATE_API_TOKEN"],
"brave-search": ["BRAVE_API_KEY"],
"gmail-drive": ["GMAIL_DRIVE_CREDENTIALS"],
"todoist": ["TODOIST_API_TOKEN"],
"lightrag": ["LIGHTRAG_API_URL"]
}
def load_env_config():
"""Load configuration from .env file if it exists"""
script_dir = Path(__file__).parent
env_path = script_dir / ".env"
if env_path.exists():
print(f"Loading configuration from {env_path}")
dotenv.load_dotenv(env_path)
env_keys = {
"GIT_PAT_TOKEN": os.getenv("GIT_PAT_TOKEN"),
"REPLICATE_API_TOKEN": os.getenv("REPLICATE_API_TOKEN"),
"BRAVE_API_KEY": os.getenv("BRAVE_API_KEY"),
"TODOIST_API_TOKEN": os.getenv("TODOIST_API_TOKEN"),
"LIGHTRAG_API_URL": os.getenv("LIGHTRAG_API_URL")
}
return {k: v for k, v in env_keys.items() if v is not None}
else:
print("No .env file found, using default configuration")
return {}
def install_package(package, package_type):
"""Install a package using npm or pip"""
try:
if package_type == "npm":
print(f"Installing NPM package: {package}")
subprocess.run(["npm", "install", "-g", package], check=True, shell=(os.name == 'nt'))
else:
print(f"Installing Python package: {package}")
subprocess.run([sys.executable, "-m", "pip", "install", package], check=True)
print(f"Successfully installed {package}")
return True
except subprocess.CalledProcessError as e:
print(f"Failed to install {package}: {e}")
return False
except Exception as e:
print(f"Error installing {package}: {e}")
return False
def get_config_path():
"""Get Claude desktop config path"""
if os.name == 'nt': # Windows
return Path(os.environ["APPDATA"]) / "Claude" / "claude_desktop_config.json"
if sys.platform == 'darwin': # macOS
return Path.home() / "Library" / "Application Support" / "Claude" / "claude_desktop_config.json"
# Linux
return Path.home() / ".config" / "Claude" / "claude_desktop_config.json"
def check_gmail_drive_credentials():
"""Check if Gmail/Drive credentials exist"""
home_dir = Path.home()
return all(
(home_dir / f).exists() for f in [
".gdrive-server-credentials.json",
".gmail-server-credentials.json",
"gcp-oauth.keys.json"
]
)
def check_api_keys(mcp_name, api_keys):
"""Check if required API keys are available for an MCP"""
if mcp_name not in MCP_API_REQUIREMENTS:
return True
if mcp_name == "gmail-drive":
return check_gmail_drive_credentials()
required_keys = MCP_API_REQUIREMENTS[mcp_name]
return all(api_keys.get(key) for key in required_keys)
def update_config(api_keys):
"""Update Claude desktop configuration"""
config_path = get_config_path()
try:
config_path.parent.mkdir(parents=True, exist_ok=True)
config = {
"mcpServers": {
# NPX-based servers
"filesystem": {
"command": "npx",
"args": ["@modelcontextprotocol/server-filesystem", str(Path.home() / "anthropicFun")]
},
"codesnip": {
"command": "npx",
"args": ["@patruff/server-codesnip"]
},
"memory": {
"command": "npx",
"args": ["@modelcontextprotocol/server-memory"]
},
"lightrag": {
"command": "npx",
"args": ["@patruff/server-lightrag"],
"env": {
"LIGHTRAG_API_URL": api_keys.get("LIGHTRAG_API_URL", "http://127.0.0.1:8020")
}
},
# UVX-based servers
"sqlite": {
"command": "uvx",
"args": ["mcp-server-sqlite", "--db-path", "~/test.db"]
},
"time": {
"command": "uvx",
"args": ["mcp-server-time"]
}
}
}
# Add Todoist configuration if API key exists
if check_api_keys("todoist", api_keys):
config["mcpServers"]["todoist"] = {
"command": "npx",
"args": ["@abhiz123/todoist-mcp-server"],
"env": {
"TODOIST_API_TOKEN": api_keys.get("TODOIST_API_TOKEN", "")
}
}
# Add gmail-drive configuration if credentials exist
if check_gmail_drive_credentials():
config["mcpServers"]["gmail-drive"] = {
"command": "npx",
"args": ["@patruff/server-gmail-drive"]
}
# Add API-dependent servers if keys are available
if check_api_keys("brave-search", api_keys):
config["mcpServers"]["brave-search"] = {
"command": "npx",
"args": ["@modelcontextprotocol/server-brave-search"],
"env": {
"BRAVE_API_KEY": api_keys.get("BRAVE_API_KEY", "")
}
}
if check_api_keys("github", api_keys):
config["mcpServers"]["github"] = {
"command": "npx",
"args": ["@modelcontextprotocol/server-github"],
"env": {
"GITHUB_PERSONAL_ACCESS_TOKEN": api_keys.get("GIT_PAT_TOKEN", "")
}
}
if check_api_keys("terminator", api_keys):
config["mcpServers"]["terminator"] = {
"command": "npx",
"args": ["@patruff/server-terminator"],
"env": {
"GITHUB_PERSONAL_ACCESS_TOKEN": api_keys.get("GIT_PAT_TOKEN", "")
}
}
if check_api_keys("flux", api_keys):
config["mcpServers"]["flux"] = {
"command": "npx",
"args": ["@patruff/server-flux"],
"env": {
"REPLICATE_API_TOKEN": api_keys.get("REPLICATE_API_TOKEN", "")
}
}
# Load and merge existing config if it exists
if config_path.exists():
try:
with open(config_path) as f:
existing_config = json.load(f)
if "mcpServers" in existing_config:
existing_config["mcpServers"].update(config["mcpServers"])
config = existing_config
except json.JSONDecodeError:
print(f"Warning: Existing config was invalid, using default config")
# Save updated config
with open(config_path, 'w') as f:
json.dump(config, f, indent=2)
print(f"Configuration updated successfully at {config_path}")
return True
except Exception as e:
print(f"Error updating config: {e}")
return False
def main():
parser = argparse.ArgumentParser(description="Claude MCP Setup Script")
parser.add_argument("--skip-prompts", action="store_true", help="Skip API key prompts")
parser.add_argument("--skip-auth", action="store_true", help="Skip Gmail/Drive authentication")
args = parser.parse_args()
# Load API keys from .env file first
api_keys = load_env_config()
# Fill in any missing keys from default config
for key in API_KEYS:
if key not in api_keys:
api_keys[key] = API_KEYS[key]
# Only prompt for missing keys if not skipping prompts
if not args.skip_prompts:
for key in API_KEYS:
if not api_keys.get(key):
value = input(f"Enter value for {key} (press Enter to skip): ")
if value:
api_keys[key] = value
# Install packages
success = True
for pkg_type, packages in PACKAGES_TO_INSTALL.items():
for package in packages:
if not install_package(package, pkg_type):
success = False
print(f"Warning: Failed to install {package}")
# Update configuration
if not update_config(api_keys):
success = False
print("Warning: Failed to update configuration")
elif api_keys:
print("\nMCP Servers requiring API keys:")
for mcp, required_keys in MCP_API_REQUIREMENTS.items():
status = "Configured" if check_api_keys(mcp, api_keys) else "Missing API key(s)"
print(f"- {mcp}: {status}")
if success:
print("\nSetup completed successfully!")
else:
print("\nSetup completed with some warnings. Please check the messages above.")
sys.exit(1)
if __name__ == "__main__":
main()