Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c43a8f5

Browse files
feat(python): support Conda and UV virtual environments
- Added Python environment configuration interface and implementation classes for system, Conda, and UV environments - Modified Python runtime manager to support environment configuration - Updated plugin instance initialization flow to handle Python environments - Improved error handling and logging for better debuggability
1 parent fbb0d8a commit c43a8f5

File tree

12 files changed

+611
-302
lines changed

12 files changed

+611
-302
lines changed

‎CMakeLists.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
cmake_minimum_required(VERSION 3.22)
2-
project(MCPServer.cpp LANGUAGES CXX C VERSION 1.0.6.0)
2+
project(MCPServer.cpp LANGUAGES CXX C VERSION 1.0.7.0)
33

44
# Add option for Python support
55
option(ENABLE_PYTHON_PLUGINS "Enable Python plugin support" ON)

‎README_zh.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ MCPServer++ 现在通过新的 Python SDK 支持 Python 插件,这使得插件
214214

215215
### 请求示例
216216

217-
``json
217+
```json
218218
{
219219
"jsonrpc": "2.0",
220220
"id": 1,
@@ -225,7 +225,7 @@ MCPServer++ 现在通过新的 Python SDK 支持 Python 插件,这使得插件
225225

226226
### 响应示例
227227

228-
``json
228+
```json
229229
{
230230
"jsonrpc": "2.0",
231231
"id": 1,

‎cmake/EnablePython.cmake‎

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,47 @@
11
include_guard(GLOBAL)
22

33
function(target_enable_python target_name)
4-
if(ENABLE_PYTHON_PLUGINS)
5-
find_package(Python COMPONENTS Interpreter Development)
6-
if(Python_FOUND)
7-
target_include_directories(${target_name} PUBLIC ${Python_INCLUDE_DIRS})
8-
else()
9-
message(WARNING "Python not found. Disabling Python plugin support.")
10-
set(ENABLE_PYTHON_PLUGINS OFF)
4+
if(ENABLE_PYTHON_PLUGINS)
5+
find_package(Python COMPONENTS Interpreter Development)
6+
if(Python_FOUND)
7+
target_include_directories(${target_name} PUBLIC ${Python_INCLUDE_DIRS})
8+
if(WIN32)
9+
# On Windows, we need to link to python library
10+
# But we should avoid linking conflicts, so we use the imported targets
11+
if(TARGET Python::Python)
12+
target_link_libraries(${target_name} PUBLIC Python::Python)
13+
else()
14+
target_link_libraries(${target_name} PUBLIC ${Python_LIBRARIES})
15+
endif()
16+
endif()
17+
else()
18+
message(WARNING "Python not found. Disabling Python plugin support.")
19+
set(ENABLE_PYTHON_PLUGINS OFF)
20+
endif()
1121
endif()
12-
endif()
1322
endfunction()
1423

1524
function(enable_python)
16-
if(ENABLE_PYTHON_PLUGINS)
17-
find_package(Python COMPONENTS Interpreter Development)
18-
if(Python_FOUND)
19-
message(STATUS "Python found: ${Python_VERSION}")
20-
message(STATUS "Python executable: ${Python_EXECUTABLE}")
21-
message(STATUS "Python include dirs: ${Python_INCLUDE_DIRS}")
22-
message(STATUS "Python libraries: ${Python_LIBRARIES}")
23-
else()
24-
message(WARNING "Python not found. Disabling Python plugin support.")
25-
set(ENABLE_PYTHON_PLUGINS OFF)
25+
if(ENABLE_PYTHON_PLUGINS)
26+
find_package(Python COMPONENTS Interpreter Development)
27+
if(Python_FOUND)
28+
message(STATUS "Python found: ${Python_VERSION}")
29+
message(STATUS "Python executable: ${Python_EXECUTABLE}")
30+
message(STATUS "Python include dirs: ${Python_INCLUDE_DIRS}")
31+
message(STATUS "Python libraries: ${Python_LIBRARIES}")
32+
33+
if(WIN32)
34+
# On Windows, link to python library for the main executable
35+
# But we should avoid linking conflicts, so we use the imported targets
36+
if(TARGET Python::Python)
37+
target_link_libraries(mcp-server++ PRIVATE Python::Python)
38+
else()
39+
target_link_libraries(mcp-server++ PRIVATE ${Python_LIBRARIES})
40+
endif()
41+
endif()
42+
else()
43+
message(WARNING "Python not found. Disabling Python plugin support.")
44+
set(ENABLE_PYTHON_PLUGINS OFF)
45+
endif()
2646
endif()
27-
endif()
2847
endfunction()

‎config.ini‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,12 @@ plugin_install_dir=plugins_install
6565
plugin_enable_dir=plugins
6666
tools_enable_dir=configs
6767
tools_install_dir=plugins_install
68+
69+
[python_environment]
70+
;Default Python environment to use (system, conda, uv)
71+
default=system
72+
;Conda environment installation prefix
73+
conda_prefix=/opt/conda
74+
;UV virtual environment path
75+
uv_venv_path=./venv
76+
;Note: Python version is automatically detected in the range 3.6-3.13

‎config.ini.example‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,12 @@ plugin_install_dir=plugins_install
6666
plugin_enable_dir=plugins
6767
tools_enable_dir=configs
6868
tools_install_dir=plugins_install
69+
70+
[python_environment]
71+
;Default Python environment to use (system, conda, uv)
72+
default=system
73+
;Conda environment installation prefix
74+
conda_prefix=/opt/conda
75+
;UV virtual environment path
76+
uv_venv_path=./venv
77+
;Note: Python version is automatically detected in the range 3.6-3.13

‎config/config.hpp‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ namespace mcp {
3737
return get_config_file_path();
3838
}
3939

40+
enum class ConfigMode{
41+
NONE, //Do not load config,use default settings
42+
STATIC, //Load config from static file
43+
DYNAMIC //Load config dynamically
44+
};
4045

4146
/**
4247
* Server-specific configuration structure
@@ -163,6 +168,42 @@ namespace mcp {
163168
}
164169
};
165170

171+
172+
/**
173+
* Python environment configuration structure
174+
* Contains settings for Python interpreter environment selection
175+
*/
176+
struct PythonEnvConfig {
177+
std::string default_env; // Default Python environment type (system, conda, uv)
178+
std::string conda_prefix;// Conda environment installation prefix
179+
std::string uv_venv_path;// UV virtual environment path
180+
181+
/**
182+
* Load Python environment configuration from INI file
183+
* @param ini Reference to IniManager instance
184+
* @return Populated PythonEnvConfig structure
185+
*/
186+
static PythonEnvConfig load(inicpp::IniManager &ini) {
187+
try {
188+
PythonEnvConfig config;
189+
190+
// Get python_environment section, create if not exists
191+
auto python_section = ini["python_environment"];
192+
193+
// Load configuration values with defaults
194+
config.default_env = python_section["default"].String().empty() ? "system" : python_section["default"].String();
195+
config.conda_prefix = python_section["conda_prefix"].String().empty() ? "/opt/conda" : python_section["conda_prefix"].String();
196+
config.uv_venv_path = python_section["uv_venv_path"].String().empty() ? "./venv" : python_section["uv_venv_path"].String();
197+
198+
return config;
199+
} catch (const std::exception &e) {
200+
MCP_ERROR("Failed to load Python environment config: {}", e.what());
201+
std::cerr << "Failed to load Python environment config: " << e.what() << std::endl;
202+
throw;
203+
}
204+
}
205+
};
206+
166207
/**
167208
* Global application configuration structure
168209
* Contains top-level configuration and nested server settings
@@ -171,6 +212,7 @@ namespace mcp {
171212
std::string title; // Configuration file title/description
172213
ServerConfig server; // Nested server configuration
173214
PluginHubConfig plugin_hub;// Nested plugin hub configuration
215+
PythonEnvConfig python_env;// Nested Python environment configuration
174216

175217
/**
176218
* Loads complete configuration from INI file
@@ -185,6 +227,7 @@ namespace mcp {
185227
config.title = ini[""]["title"].String().empty() ? "MCP Server Configuration" : ini[""]["title"].String();
186228
config.server = ServerConfig::load(ini);
187229
config.plugin_hub = PluginHubConfig::load(ini);
230+
config.python_env = PythonEnvConfig::load(ini);
188231

189232
return config;
190233
} catch (const std::exception &e) {

‎plugins/sdk/pybind_module_plugin.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
namespace py = pybind11;
2727
namespace fs = std::filesystem;
2828

29+
using namespace mcp::business;
30+
2931

3032
static std::unordered_map<std::string, std::unique_ptr<PythonPluginInstance>> g_plugin_instances;
3133

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /