Skip to content

Commit bf9ae94

Browse files
committed
chore: refactor app MCP configuration and implement AI chat functionality
1 parent 6aa3999 commit bf9ae94

File tree

3 files changed

+79
-56
lines changed

3 files changed

+79
-56
lines changed

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -269,14 +269,8 @@ 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
278272
executor = ToolExecutor()
279-
app_config = executor.get_app_mcp_config(api_key, app.name, app.desc)
273+
app_config = executor.get_app_mcp_config(app.id, app.name, app.desc)
280274
mcp_servers_config[str(app.id)] = app_config
281275

282276
if len(mcp_servers_config) > 0:

apps/common/utils/app_mcp.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import json
2+
import logging
3+
import os
4+
import sys
5+
6+
import uuid_utils.compat as uuid
7+
from asgiref.sync import sync_to_async
8+
from mcp.server.fastmcp import FastMCP
9+
10+
logging.basicConfig(level=logging.WARNING)
11+
logging.getLogger("mcp").setLevel(logging.ERROR)
12+
logging.getLogger("mcp.server").setLevel(logging.ERROR)
13+
14+
15+
def app_mcp_init(base_dir: str, application_id: str, name: str, description: str):
16+
import django
17+
18+
sys.path.insert(0, base_dir)
19+
20+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "maxkb.settings")
21+
22+
django.setup()
23+
mcp = FastMCP()
24+
25+
@sync_to_async
26+
def _get_chat_id():
27+
from application.models import ChatUserType
28+
from chat.serializers.chat import OpenChatSerializers
29+
from common.init import init_template
30+
31+
init_template.run()
32+
33+
return OpenChatSerializers(data={
34+
'application_id': application_id,
35+
'chat_user_id': str(uuid.uuid7()),
36+
'chat_user_type': ChatUserType.ANONYMOUS_USER,
37+
'debug': False
38+
}).open()
39+
40+
@sync_to_async
41+
def _chat_with_ai(chat_id: str, message: str) -> str:
42+
from application.models import ChatUserType
43+
from chat.serializers.chat import ChatSerializers
44+
45+
payload = {
46+
'message': message,
47+
'stream': False,
48+
're_chat': False
49+
}
50+
resp = ChatSerializers(data={
51+
'chat_id': chat_id,
52+
'chat_user_id': str(uuid.uuid7()),
53+
'chat_user_type': ChatUserType.ANONYMOUS_USER,
54+
'application_id': application_id,
55+
'debug': False,
56+
}).chat(payload)
57+
data = json.loads(str(resp.text))
58+
return str(data.get("data", {}).get("content") or data.get("response"))
59+
60+
@mcp.tool(description=f'{name} {description}')
61+
async def ai_chat(message: str) -> str:
62+
chat_id = await _get_chat_id()
63+
reply = await _chat_with_ai(chat_id, message)
64+
65+
return reply or "AI 未能生成回复"
66+
67+
mcp.run(transport='stdio')
68+
69+
70+
app_mcp_init(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])

apps/common/utils/tool_code.py

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

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()
236+
def get_app_mcp_config(self, application_id, name, description):
237+
cwd = os.path.dirname(os.path.abspath(__file__))
282238
app_config = {
283239
'command': sys.executable,
284240
'args': [
285-
'-c',
286-
f'import base64,gzip; exec(gzip.decompress(base64.b64decode(\'{compressed_and_base64_encoded_code_str}\')).decode())',
241+
f'{cwd}/app_mcp.py',
242+
BASE_DIR,
243+
str(application_id),
244+
name,
245+
description,
287246
],
288-
'cwd': _sandbox_path,
247+
'cwd': BASE_DIR,
289248
'env': {
290249
'LD_PRELOAD': f'{_sandbox_path}/lib/sandbox.so',
291250
},

0 commit comments

Comments
 (0)