@@ -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