添加 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

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