添加 MCP 返回结果日志记录功能,更新相关配置和文档,改善 MCP 结果返回

This commit is contained in:
2025-09-05 22:00:50 +08:00
parent b2914be3c1
commit 3dbe00e1d6
7 changed files with 18 additions and 5 deletions

View File

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

View File

@ -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 返回结果 |

View File

@ -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 == "":

View File

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

View File

@ -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)}"

View File

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

View File

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