Skip to content

Commit 56f9132

Browse files
committed
Revert "chore: refactor app MCP configuration and implement AI chat functionality"
This reverts commit bf9ae94.
1 parent bf9ae94 commit 56f9132

File tree

3 files changed

+56
-79
lines changed

3 files changed

+56
-79
lines changed

apps/application/chat_pipeline/step/chat_step/impl/base_chat_step.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,14 @@ def _handle_mcp_request(self, mcp_enable, tool_enable, mcp_source, mcp_servers,
269269
self.context['application_ids'] = application_ids
270270
for application_id in application_ids:
271271
app = QuerySet(Application).filter(id=application_id).first()
272+
app_key = QuerySet(ApplicationApiKey).filter(application_id=application_id, is_active=True).first()
273+
# TODO 处理api
274+
if app_key is not None:
275+
api_key = app_key.secret_key
276+
else:
277+
continue
272278
executor = ToolExecutor()
273-
app_config = executor.get_app_mcp_config(app.id, app.name, app.desc)
279+
app_config = executor.get_app_mcp_config(api_key, app.name, app.desc)
274280
mcp_servers_config[str(app.id)] = app_config
275281

276282
if len(mcp_servers_config) > 0:

apps/common/utils/app_mcp.py

Lines changed: 0 additions & 70 deletions
This file was deleted.

apps/common/utils/tool_code.py

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -233,18 +233,59 @@ def get_tool_mcp_config(self, code, params, name, description):
233233
}
234234
return tool_config
235235

236-
def get_app_mcp_config(self, application_id, name, description):
237-
cwd = os.path.dirname(os.path.abspath(__file__))
236+
def get_app_mcp_config(self, api_key, name, description):
237+
chat_path = CONFIG.get_chat_path()
238+
_code = f'''
239+
import requests
240+
from typing import Optional
241+
242+
def _get_chat_id() -> Optional[str]:
243+
url = f"http://127.0.0.1:8080{chat_path}/api/open"
244+
headers = {{
245+
'accept': '*/*',
246+
'Authorization': f'Bearer {api_key}'
247+
}}
248+
try:
249+
resp = requests.get(url, headers=headers, timeout=10)
250+
resp.raise_for_status()
251+
return resp.json().get("data")
252+
except Exception as e:
253+
raise e
254+
255+
256+
def _chat_with_ai(chat_id: str, message: str) -> Optional[str]:
257+
url = f"http://127.0.0.1:8080{chat_path}/api/chat_message/{{chat_id}}"
258+
headers = {{"Content-Type": "application/json", "Authorization": f'Bearer {api_key}'}}
259+
payload = {{
260+
"message": message,
261+
"re_chat": False,
262+
"stream": False
263+
}}
264+
try:
265+
resp = requests.post(url, json=payload, headers=headers, timeout=600)
266+
resp.raise_for_status()
267+
data = resp.json()
268+
return str(data.get("data", {{}}).get("content") or data.get("response"))
269+
except Exception as e:
270+
raise e
271+
272+
def ai_chat(message: str) -> str:
273+
chat_id = _get_chat_id()
274+
reply = _chat_with_ai(chat_id, message)
275+
return reply or "AI 未能生成回复"
276+
277+
'''
278+
_code = self.generate_mcp_server_code(_code, {}, name, description)
279+
# print(_code)
280+
maxkb_logger.debug(f"Python code of mcp app: {_code}")
281+
compressed_and_base64_encoded_code_str = base64.b64encode(gzip.compress(_code.encode())).decode()
238282
app_config = {
239283
'command': sys.executable,
240284
'args': [
241-
f'{cwd}/app_mcp.py',
242-
BASE_DIR,
243-
str(application_id),
244-
name,
245-
description,
285+
'-c',
286+
f'import base64,gzip; exec(gzip.decompress(base64.b64decode(\'{compressed_and_base64_encoded_code_str}\')).decode())',
246287
],
247-
'cwd': BASE_DIR,
288+
'cwd': _sandbox_path,
248289
'env': {
249290
'LD_PRELOAD': f'{_sandbox_path}/lib/sandbox.so',
250291
},

0 commit comments

Comments
 (0)