|
10 | 10 | import traceback
|
11 | 11 | import subprocess
|
12 | 12 | import websockets
|
| 13 | +from core.handle.mcpHandle import call_mcp_tool |
13 | 14 | from core.utils.util import (
|
14 | 15 | extract_json_from_string,
|
15 | 16 | check_vad_update,
|
@@ -146,6 +147,8 @@ def __init__(
|
146 | 147 | ) # 在原来第一道关闭的基础上加60秒,进行二道关闭
|
147 | 148 |
|
148 | 149 | self.audio_format = "opus"
|
| 150 | + # {"mcp":true} 表示启用MCP功能 |
| 151 | + self.features = None |
149 | 152 |
|
150 | 153 | async def handle_connection(self, ws):
|
151 | 154 | try:
|
@@ -579,6 +582,12 @@ def chat(self, query, tool_call=False):
|
579 | 582 | functions = None
|
580 | 583 | if self.intent_type == "function_call" and hasattr(self, "func_handler"):
|
581 | 584 | functions = self.func_handler.get_functions()
|
| 585 | + if hasattr(self, "mcp_client"): |
| 586 | + mcp_tools = self.mcp_client.get_available_tools() |
| 587 | + if mcp_tools is not None and len(mcp_tools) > 0: |
| 588 | + if functions is None: |
| 589 | + functions = [] |
| 590 | + functions.extend(mcp_tools) |
582 | 591 | response_message = []
|
583 | 592 |
|
584 | 593 | try:
|
@@ -696,9 +705,23 @@ def chat(self, query, tool_call=False):
|
696 | 705 | "arguments": function_arguments,
|
697 | 706 | }
|
698 | 707 |
|
699 |
| - # 处理MCP工具调用 |
| 708 | + # 处理Server端MCP工具调用 |
700 | 709 | if self.mcp_manager.is_mcp_tool(function_name):
|
701 | 710 | result = self._handle_mcp_tool_call(function_call_data)
|
| 711 | + elif hasattr(self, "mcp_client") and self.mcp_client.has_tool(function_name): |
| 712 | + # 如果是小智端MCP工具调用 |
| 713 | + self.logger.bind(tag=TAG).debug( |
| 714 | + f"调用小智端MCP工具: {function_name}, 参数: {function_arguments}" |
| 715 | + ) |
| 716 | + try: |
| 717 | + result = asyncio.run_coroutine_threadsafe(call_mcp_tool(self, self.mcp_client, function_name, function_arguments), self.loop).result() |
| 718 | + self.logger.bind(tag=TAG).debug(f"MCP工具调用结果: {result}") |
| 719 | + result = ActionResponse(action=Action.REQLLM, result=result, response="") |
| 720 | + except Exception as e: |
| 721 | + self.logger.bind(tag=TAG).error(f"MCP工具调用失败: {e}") |
| 722 | + result = ActionResponse( |
| 723 | + action=Action.REQLLM, result="MCP工具调用失败", response="" |
| 724 | + ) |
702 | 725 | else:
|
703 | 726 | # 处理系统函数
|
704 | 727 | result = self.func_handler.handle_llm_function_call(
|
|
0 commit comments