diff --git a/docs/en/start/install.md b/docs/en/start/install.md index 17e61eb..2265bb0 100644 --- a/docs/en/start/install.md +++ b/docs/en/start/install.md @@ -150,3 +150,4 @@ Add options in the `.env` file from the diagram below in nonebot2 project. | MARSHOAI_SEND_THINKING | `bool` | `true` | Send thinking chain or not | | MARSHOAI_STREAM | `bool` | `false`| 是否通过流式方式请求 API **开启此项后暂无法使用函数调用,无法在 Bot 用户侧聊天界面呈现出流式效果** | | MARSHOAI_ENABLE_MCP | `bool` | `false`| Enable MCP feature or not | +| MARSHOAI_ENABLE_MCP_RESULT_LOGGING | `bool` | `false`| Whether to output MCP return results in the log | diff --git a/docs/zh/start/install.md b/docs/zh/start/install.md index 2052909..aed50c4 100644 --- a/docs/zh/start/install.md +++ b/docs/zh/start/install.md @@ -151,4 +151,5 @@ GitHub Models API 的限制较多,不建议使用,建议通过修改`MARSHOA | MARSHOAI_SEND_THINKING | `bool` | `true` | 是否发送思维链(部分模型不支持) | | MARSHOAI_STREAM | `bool` | `false`| 是否通过流式方式请求 API **开启此项后暂无法使用函数调用,无法在 Bot 用户侧聊天界面呈现出流式效果** | | MARSHOAI_ENABLE_MCP | `bool` | `false`| 是否启用 MCP 功能 | +| MARSHOAI_ENABLE_MCP_RESULT_LOGGING | `bool` | `false`| 是否在日志中输出 MCP 返回结果 | diff --git a/nonebot_plugin_marshoai/__init__.py b/nonebot_plugin_marshoai/__init__.py index 6822757..f7b98ba 100755 --- a/nonebot_plugin_marshoai/__init__.py +++ b/nonebot_plugin_marshoai/__init__.py @@ -33,7 +33,7 @@ from nonebot import get_driver, logger # type: ignore from .config import config from .dev import * # noqa: F403 -from .extensions.mcp_extension.client import get_mcp_list, initialize_servers +from .extensions.mcp_extension.client import initialize_servers from .marsho import * # noqa: F403 from .metadata import metadata @@ -49,8 +49,8 @@ driver = get_driver() @driver.on_startup async def _(): if config.marshoai_enable_mcp: + logger.info("MCP 初始化开始~🐾") await initialize_servers() - print(await get_mcp_list()) logger.info("MarshoAI 已经加载~🐾") logger.info(f"Marsho 的插件数据存储于 : {str(store.get_plugin_data_dir())} 哦~🐾") if config.marshoai_token == "": diff --git a/nonebot_plugin_marshoai/config.py b/nonebot_plugin_marshoai/config.py index 31d22c4..c38b6bb 100644 --- a/nonebot_plugin_marshoai/config.py +++ b/nonebot_plugin_marshoai/config.py @@ -72,6 +72,7 @@ class ConfigModel(BaseModel): marshoai_plugins: list[str] = [] """marsho插件的名称列表,从pip安装的使用包名,从本地导入的使用路径""" marshoai_enable_mcp: bool = False + marshoai_enable_mcp_result_logging: bool = False yaml = YAML() diff --git a/nonebot_plugin_marshoai/extensions/mcp_extension/client.py b/nonebot_plugin_marshoai/extensions/mcp_extension/client.py index 1be46b6..70ec5be 100644 --- a/nonebot_plugin_marshoai/extensions/mcp_extension/client.py +++ b/nonebot_plugin_marshoai/extensions/mcp_extension/client.py @@ -1,6 +1,7 @@ import asyncio from typing import Any, Optional +from mcp.types import TextContent from nonebot import logger from .config import get_mcp_server_config @@ -29,7 +30,7 @@ async def initialize_servers() -> None: async def handle_mcp_tool( tool: str, arguments: Optional[dict[str, Any]] = None -) -> Optional[str]: +) -> Optional[str | list]: """ 处理 MCP Tool 调用 """ @@ -50,7 +51,14 @@ async def handle_mcp_tool( logger.info( f"工具 {tool} 执行进度: {progress}/{total} ({percentage:.1f}%)" ) - + if isinstance(result, list): + content_string: str = "" + # Assuming result is a dict with ContentBlock keys or values + # Adjust as needed based on actual structure + for content in result: + if isinstance(content, TextContent): + content_string += content.text + return content_string return f"Tool execution result: {result}" except Exception as e: error_msg = f"Error executing tool: {str(e)}" diff --git a/nonebot_plugin_marshoai/handler.py b/nonebot_plugin_marshoai/handler.py index 416c80e..80b5ca9 100644 --- a/nonebot_plugin_marshoai/handler.py +++ b/nonebot_plugin_marshoai/handler.py @@ -195,6 +195,8 @@ class MarshoHandler: ) else: func_return = await handle_mcp_tool(tool_name, function_args) + if config.marshoai_enable_mcp_result_logging: + logger.info(f"MCP工具 {tool_clean_name} 返回结果: {func_return}") tool_msg.append( ToolMessage(tool_call_id=tool_call.id, content=func_return).as_dict() # type: ignore ) diff --git a/nonebot_plugin_marshoai/metadata.py b/nonebot_plugin_marshoai/metadata.py index 85efda0..ac1ea1a 100755 --- a/nonebot_plugin_marshoai/metadata.py +++ b/nonebot_plugin_marshoai/metadata.py @@ -5,7 +5,7 @@ from .constants import USAGE metadata = PluginMetadata( name="Marsho AI 插件", - description="接入 Azure API 或其他 API 的 AI 聊天插件,支持图片处理,外部函数调用,兼容包括 DeepSeek-R1, QwQ-32B 在内的多个模型", + description="接入 Azure API 或其他 API 的 AI 聊天插件,支持图片处理,外部函数调用,MCP,兼容包括 DeepSeek-R1, QwQ-32B 在内的多个模型", usage=USAGE, type="application", config=ConfigModel,