UnisKB/apps/system_manage/mcp_runtime.py

181 lines
10 KiB
Python

"""Standard component input/output adapters used by both assistants."""
import asyncio
import json
from copy import deepcopy
from rest_framework.exceptions import ValidationError
from system_manage import ai_hub as hub, mcp_registry as registry, mcp_scheduler as scheduler
def connections(raw):
try:
servers = json.loads(raw) if isinstance(raw, str) else deepcopy(raw)
except (TypeError, ValueError):
raise ValidationError('MCP 服务配置必须为有效 JSON')
if not isinstance(servers, dict) or not servers or len(servers) > 16:
raise ValidationError('MCP 服务配置无效')
from urllib.parse import urlsplit
for config in servers.values():
if not isinstance(config, dict) or config.get('transport') not in ('sse', 'streamable_http'):
raise ValidationError('MCP 仅支持 SSE 和 Streamable HTTP')
address = urlsplit(config.get('url', ''))
if address.scheme not in ('http', 'https') or not address.hostname or address.username or address.password:
raise ValidationError('MCP 服务地址无效')
return servers
async def discover_async(servers):
from langchain_mcp_adapters.client import MultiServerMCPClient
rows = []
for key, config in connections(servers).items():
client = MultiServerMCPClient({key: config})
async with client.session(key) as session:
response = await session.list_tools()
rows += [{'server': key, 'name': tool.name, 'description': tool.description or '',
'args_schema': tool.inputSchema} for tool in response.tools]
return rows
def discover(servers):
try:
return asyncio.run(asyncio.wait_for(discover_async(servers), timeout=20))
except ValidationError:
raise
except Exception:
raise ValidationError('MCP 工具发现失败,请检查服务状态和认证') from None
def call_mcp(servers, server, tool, params, timeout=60, retries=0):
from langchain_mcp_adapters.client import MultiServerMCPClient
if not isinstance(params, dict):
raise ValidationError('工具参数必须为 JSON 对象')
config = connections(servers)
if server not in config:
raise ValidationError('所选 MCP 服务已不存在,请重新获取工具')
timeout = hub.integer(timeout, 1, 600, '调用超时')
retries = hub.integer(retries, 0, 2, '重试次数')
async def once():
client = MultiServerMCPClient({server: config[server]})
async with client.session(server) as session:
response = await session.call_tool(tool, params)
if response.isError:
# Remote messages may contain secrets. They are not safe workflow errors.
raise ValidationError('MCP 工具返回执行失败')
contents = [item.model_dump(mode='json', exclude_none=True) for item in response.content]
texts = [item['text'] for item in contents if item.get('type') == 'text']
structured = getattr(response, 'structuredContent', None)
if not texts and structured is not None:
texts = [json.dumps(structured, ensure_ascii=False)]
return {'result': texts, 'text': '\n'.join(texts), 'structured': structured,
'content': contents, 'mcp_tool': tool, 'mcp_server': server}
async def run():
for attempt in range(retries + 1):
try:
value = await asyncio.wait_for(once(), timeout=timeout)
return {**value, 'attempts': attempt + 1}
except ValidationError:
# A tool-level business error is never retried.
raise
except Exception:
if attempt == retries:
raise ValidationError('MCP 调用失败或超时,请检查服务后重试') from None
await asyncio.sleep(min(attempt + 1, 2))
return asyncio.run(run())
def invoke(workspace, user, key, params, *, parent=None, application_id=None, chat_id=None, timeout=60, retries=0, priority=5,
scope=None, progress=None, checkpoint=None, connection_overrides=None):
metadata, obj = registry.resolve(workspace, user, key, check=not bool(parent))
if not isinstance(params, dict):
raise ValidationError('组件参数必须为 JSON 对象')
if parent:
from system_manage.models.ai_hub import AITask
task = AITask.objects.filter(pk=parent, owner_id=user.id, workspace_id=workspace, status='RUNNING').first()
if task and task.snapshot.get('resource_versions', {}).get(key, metadata['version']) != metadata['version']:
raise ValidationError('组件版本已变化,请重新提交流程任务')
def execute(active_task=None):
execution_parent = parent or active_task
if metadata['kind'] == 'mcp':
raw = connection_overrides if connection_overrides is not None else (params.get('servers') if key == 'mcp-custom' else obj.code)
value = call_mcp(raw, params.get('server'), params.get('tool'), params.get('arguments', {}), timeout, retries)
elif key == 'knowledge-search':
from system_manage.mcp_business import search_knowledge
value = search_knowledge(workspace, user, params, application_id, scope)
elif key == 'mindmap-save':
from system_manage.mcp_business import save_mindmap
value = save_mindmap(workspace, user, params, execution_parent, scope)
elif key == 'markdown-parser':
from markdown_it import MarkdownIt
text = hub.text(params.get('text', ''), 'Markdown 内容')
tokens = MarkdownIt().enable('table').parse(text)
if len(tokens) > 50000:
raise ValidationError('Markdown 结构过于复杂,请拆分文档')
def token_data(token):
return {'type': token.type, 'tag': token.tag, 'nesting': token.nesting,
'content': token.content, 'lines': token.map,
'children': [token_data(child) for child in token.children or []]}
headings = [{'level': int(token.tag[1:]), 'text': tokens[index+1].content,
'line': token.map[0]+1} for index, token in enumerate(tokens)
if token.type == 'heading_open' and index+1 < len(tokens)]
value = {'text': text, 'structured': {'tokens': [token_data(token) for token in tokens], 'headings': headings}}
elif key == 'mindmap-engine':
text = hub.text(params.get('text', ''), 'Markdown 内容')
structure = params.get('heading_structure') or {}
value = {'text': text, 'structured': hub.markdown_tree(text, structure.get('excluded_lines', []))}
elif key == 'file-reader' or metadata['kind'] == 'parser':
from system_manage.models.ai_hub import AITask
from system_manage.mcp_business import parse_document
task = AITask.objects.filter(id=execution_parent, owner_id=user.id, workspace_id=workspace).first() if execution_parent else None
if task and task.file_data:
name, raw = task.file_name, bytes(task.file_data)
value = parse_document(workspace, user, name, raw, obj, params.get('options'), progress,
repair_headings=(scope or {}).get('source') in ('mindmap', 'mindmap-workbench') or
str(task.snapshot.get('source', '')).startswith('mindmap'))
else:
from knowledge.models import File, FileSourceType
docs = params.get('documents', [])
if not isinstance(docs, list) or not 1 <= len(docs) <= 10 or not application_id:
raise ValidationError('请选择开始节点的文档变量或提交任务文件')
texts = []
for doc in docs:
from django.db.models import Q
file_scope = Q(source_type=FileSourceType.APPLICATION, source_id=str(application_id))
if chat_id:
file_scope |= Q(source_type=FileSourceType.CHAT, source_id=str(chat_id))
file = File.objects.filter(file_scope, id=doc.get('file_id')).first()
if not file:
raise ValidationError('文档不存在或不属于当前应用')
raw = file.get_bytes()
if len(raw) > hub.MAX_FILE:
raise ValidationError('文件不能超过20MB')
parsed = parse_document(workspace, user, file.file_name, raw, obj, params.get('options'), progress)
texts.append(parsed['text'])
value = {'text': '\n\n'.join(texts), 'structured': None}
elif metadata['kind'] == 'model':
from system_manage.mcp_business import organize_document
value = organize_document(obj, params, timeout, progress, checkpoint, execution_parent=execution_parent)
else:
raise ValidationError('组件不支持当前调用方式')
value.setdefault('result', [value.get('text', '')])
if execution_parent and (key in ('file-reader', 'mindmap-engine', 'knowledge-search') or metadata['kind'] == 'parser'):
from django.db import transaction
from system_manage.models.ai_hub import AITask
with transaction.atomic():
current = AITask.objects.select_for_update().get(pk=execution_parent, owner_id=user.id, workspace_id=workspace)
updates = {}
if key == 'mindmap-engine': updates = {'mindmap_markdown': value['text'], 'tree': value['structured']}
elif key == 'knowledge-search': updates = {'references': value['references']}
else: updates = {'original': value.get('original', value['text']), 'parsed_markdown': value['text'], 'source': value['structured']}
current.result = {**current.result, **updates}
current.save(update_fields=['result', 'updated_at'])
value.update(component_id=key, component_version=metadata['version'])
if len(json.dumps(value, ensure_ascii=False).encode()) > 24 * 1024 * 1024:
raise ValidationError('组件输出超过24MB')
return value
trace = {**(scope or {}), 'application_id': str(application_id) if application_id else None,
'chat_id': str(chat_id) if chat_id else None, 'tool': params.get('tool'), 'server': params.get('server')}
return scheduler.inline(workspace, user, key, execute, parent=parent, priority_value=priority, trace=trace, task_context=True)