Skip to content

Commit 541f1f2

Browse files
committed
feat: refactor MCP code generation and update cleanup logic
1 parent 1837512 commit 541f1f2

File tree

2 files changed

+5
-11
lines changed

2 files changed

+5
-11
lines changed

apps/application/flow/step_node/ai_chat_step_node/impl/base_chat_node.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ def _handle_mcp_request(self, mcp_enable, tool_enable, mcp_source, mcp_servers,
281281
for tool_id in tool_ids:
282282
tool = QuerySet(Tool).filter(id=tool_id).first()
283283
executor = ToolExecutor()
284-
code = executor.get_exec_code(tool.code)
284+
code = executor.generate_mcp_server_code(tool.code)
285285
code_path = f'{executor.sandbox_path}/execute/{tool_id}.py'
286286
with open(code_path, 'w') as f:
287287
f.write(code)
@@ -340,8 +340,8 @@ def get_details(self, index: int, **kwargs):
340340
# 清理工具代码文件,延时删除,避免文件被占用
341341
for tool_id in self.context.get('tool_ids'):
342342
code_path = f'{executor.sandbox_path}/execute/{tool_id}.py'
343-
if os.path.exists(code_path):
344-
os.remove(code_path)
343+
# if os.path.exists(code_path):
344+
# os.remove(code_path)
345345
return {
346346
'name': self.node.properties.get('stepName'),
347347
"index": index,

apps/common/utils/tool_code.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,27 +116,21 @@ def _generate_mcp_server_code(self, _code):
116116

117117
return "\n".join(code_parts)
118118

119-
def get_exec_code(self, code_str):
119+
def generate_mcp_server_code(self, code_str):
120120
python_paths = CONFIG.get_sandbox_python_package_paths().split(',')
121121
code = self._generate_mcp_server_code(code_str)
122122
return f"""
123123
try:
124124
import os
125125
import sys
126-
import pickle
127126
path_to_exclude = ['/opt/py3/lib/python3.11/site-packages', '/opt/maxkb-app/apps']
128127
sys.path = [p for p in sys.path if p not in path_to_exclude]
129128
sys.path += {python_paths}
130129
env = dict(os.environ)
131130
for key in list(env.keys()):
132131
if key in os.environ and (key.startswith('MAXKB') or key.startswith('POSTGRES') or key.startswith('PG') or key.startswith('REDIS') or key == 'PATH'):
133132
del os.environ[key]
134-
locals_v={'{}'}
135-
globals_v=globals()
136-
exec({dedent(code)!a}, globals_v, locals_v)
137-
f_name, f = locals_v.popitem()
138-
for local in locals_v:
139-
globals_v[local] = locals_v[local]
133+
exec({dedent(code)!a})
140134
except Exception as e:
141135
pass
142136
"""

0 commit comments

Comments
 (0)