Skip to content

Commit f0a3391

Browse files
committed
refactor: simplify error handling in MCP server code execution
1 parent 5061708 commit f0a3391

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
import json
1111
import os
1212
import re
13+
import sys
1314
import time
15+
import traceback
1416
from functools import reduce
1517
from typing import List, Dict
1618

@@ -143,7 +145,7 @@ def mcp_response_generator(chat_model, message_list, mcp_servers):
143145
except StopAsyncIteration:
144146
break
145147
except Exception as e:
146-
maxkb_logger.error(f'Exception: {e}')
148+
maxkb_logger.error(f'Exception: {e}', traceback.format_exc())
147149
finally:
148150
loop.close()
149151

@@ -285,9 +287,10 @@ def _handle_mcp_request(self, mcp_enable, tool_enable, mcp_source, mcp_servers,
285287
code_path = f'{executor.sandbox_path}/execute/{tool_id}.py'
286288
with open(code_path, 'w') as f:
287289
f.write(code)
290+
os.system(f"chown sandbox:root {code_path}")
288291

289292
tool_config = {
290-
'command': 'python',
293+
'command': sys.executable,
291294
'args': [code_path],
292295
'transport': 'stdio',
293296
}

apps/common/utils/tool_code.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -120,19 +120,16 @@ 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"""
123-
try:
124-
import os
125-
import sys
126-
path_to_exclude = ['/opt/py3/lib/python3.11/site-packages', '/opt/maxkb-app/apps']
127-
sys.path = [p for p in sys.path if p not in path_to_exclude]
128-
sys.path += {python_paths}
129-
env = dict(os.environ)
130-
for key in list(env.keys()):
131-
if key in os.environ and (key.startswith('MAXKB') or key.startswith('POSTGRES') or key.startswith('PG') or key.startswith('REDIS') or key == 'PATH'):
132-
del os.environ[key]
133-
exec({dedent(code)!a})
134-
except Exception as e:
135-
pass
123+
import os
124+
import sys
125+
path_to_exclude = ['/opt/py3/lib/python3.11/site-packages', '/opt/maxkb-app/apps']
126+
sys.path = [p for p in sys.path if p not in path_to_exclude]
127+
sys.path += {python_paths}
128+
env = dict(os.environ)
129+
for key in list(env.keys()):
130+
if key in os.environ and (key.startswith('MAXKB') or key.startswith('POSTGRES') or key.startswith('PG') or key.startswith('REDIS') or key == 'PATH'):
131+
del os.environ[key]
132+
exec({dedent(code)!a})
136133
"""
137134

138135
def _exec_sandbox(self, _code, _id):

0 commit comments

Comments
 (0)