-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathvscode.py
More file actions
122 lines (98 loc) · 4.04 KB
/
Copy pathvscode.py
File metadata and controls
122 lines (98 loc) · 4.04 KB
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
import json
import logging
import os
import traceback
from typing import Any, Dict
from mcpm.clients.base import JSONClientManager
logger = logging.getLogger(__name__)
class VSCodeManager(JSONClientManager):
"""Manages VSCode MCP server configurations"""
# Client information
client_key = "vscode"
display_name = "VSCode"
download_url = "https://code.visualstudio.com/"
configure_key_name = "servers"
def __init__(self, config_path_override: str | None = None):
super().__init__(config_path_override=config_path_override)
if config_path_override:
self.config_path = config_path_override
else:
# Set config path based on detected platform
if self._system == "Windows":
self.config_path = os.path.join(os.environ.get("APPDATA", ""), "Code", "User", "mcp.json")
elif self._system == "Darwin":
self.config_path = os.path.expanduser("~/Library/Application Support/Code/User/mcp.json")
else:
# Linux
self.config_path = os.path.expanduser("~/.config/Code/User/mcp.json")
def _load_config(self) -> Dict[str, Any]:
"""Load client configuration file
{
"servers": {
"server_name": {
...
}
},
"inputs": []
}
Returns:
Dict containing the client configuration with at least {"servers": {}, "inputs": []}
"""
# Create empty config with the correct structure
empty_config = {self.configure_key_name: {}, "inputs": []}
if not os.path.exists(self.config_path):
logger.warning(f"Client config file not found at: {self.config_path}")
return empty_config
try:
with open(self.config_path, "r", encoding="utf-8") as f:
config = json.load(f)
# Ensure servers section exists
if self.configure_key_name not in config:
config[self.configure_key_name] = {}
# Ensure inputs array exists
if "inputs" not in config:
config["inputs"] = []
return config
except json.JSONDecodeError:
logger.error(f"Error parsing client config file: {self.config_path}")
return empty_config
def _save_config(self, config: Dict[str, Any]) -> bool:
"""Save configuration to client config file
Args:
config: Configuration to save (should include "servers" and optionally "inputs")
Returns:
bool: Success or failure
"""
try:
# Create directory if it doesn't exist
os.makedirs(os.path.dirname(self.config_path), exist_ok=True)
# Ensure inputs array exists if not present
if "inputs" not in config:
config["inputs"] = []
with open(self.config_path, "w", encoding="utf-8") as f:
json.dump(config, f, indent=2)
return True
except Exception as e:
logger.error(f"Error saving client config: {str(e)}")
traceback.print_exc()
return False
def to_client_format(self, server_config) -> dict:
"""Convert ServerConfig to VSCode-specific format
VSCode expects a "type" field in addition to command and args.
"""
from mcpm.core.schema import STDIOServerConfig
if isinstance(server_config, STDIOServerConfig):
result = {
"type": "stdio",
"command": server_config.command,
"args": server_config.args,
}
# Add environment variables if present
import os
non_empty_env = server_config.get_filtered_env_vars(os.environ)
if non_empty_env:
result["env"] = non_empty_env
return result
else:
# For other server types, use the default implementation
return super().to_client_format(server_config)