mirror of
https://github.com/FuQuan233/nonebot-plugin-llmchat.git
synced 2026-06-29 16:52:02 +00:00
✨ 添加MCP服务器全局工作目录配置
This commit is contained in:
parent
38af060cb2
commit
b6af4ec334
4 changed files with 254 additions and 224 deletions
|
|
@ -123,6 +123,7 @@ _✨ 支持多API预设、MCP协议、内置工具、联网搜索、视觉模型
|
|||
| LLMCHAT__DEFAULT_PRESET | 否 | off | 默认使用的预设名称,配置为off则为关闭 |
|
||||
| LLMCHAT__RANDOM_TRIGGER_PROB | 否 | 0.05 | 默认随机触发概率 [0, 1] |
|
||||
| LLMCHAT__DEFAULT_PROMPT | 否 | 你的回答应该尽量简洁、幽默、可以使用一些语气词、颜文字。你应该拒绝回答任何政治相关的问题。 | 默认提示词 |
|
||||
| LLMCHAT__MCP_SERVER_CWD | 否 | 无 | command类型MCP服务器全局工作目录(cwd) |
|
||||
| LLMCHAT__BLACKLIST_USER_IDS | 否 | [] | 黑名单用户ID列表,机器人将不会处理黑名单用户的消息 |
|
||||
| LLMCHAT__IGNORE_PREFIXES | 否 | [] | 需要忽略的消息前缀列表,匹配到这些前缀的消息不会处理 |
|
||||
| LLMCHAT__MCP_SERVERS | 否 | {} | MCP服务器配置,具体见下表 |
|
||||
|
|
@ -165,7 +166,7 @@ LLMCHAT__MCP_SERVERS同样为一个dict,key为服务器名称,value配置的
|
|||
| 配置项 | 必填 | 默认值 | 说明 |
|
||||
|:-----:|:----:|:----:|:----:|
|
||||
| command | stdio服务器必填 | 无 | stdio服务器MCP命令 |
|
||||
| arg | 否 | [] | stdio服务器MCP命令参数 |
|
||||
| args | 否 | [] | stdio服务器MCP命令参数 |
|
||||
| env | 否 | {} | stdio服务器环境变量 |
|
||||
| url | sse服务器必填 | 无 | sse服务器地址 |
|
||||
| headers | 否 | {} | sse模式下http请求头,用于认证或其他设置 |
|
||||
|
|
|
|||
|
|
@ -349,6 +349,7 @@ async def process_messages(context_id: int, is_group: bool = True):
|
|||
logger.info(
|
||||
f"开始处理{chat_type}消息 {context_type}:{context_id} 当前队列长度:{state.queue.qsize()}"
|
||||
)
|
||||
try:
|
||||
while not state.queue.empty():
|
||||
event = await state.queue.get()
|
||||
if is_group:
|
||||
|
|
@ -358,7 +359,10 @@ async def process_messages(context_id: int, is_group: bool = True):
|
|||
logger.debug(f"从队列获取消息 用户:{context_id} 消息ID:{event.message_id}")
|
||||
group_id = None
|
||||
past_events_snapshot = []
|
||||
mcp_client = MCPClient.get_instance(plugin_config.mcp_servers)
|
||||
mcp_client = MCPClient.get_instance(
|
||||
plugin_config.mcp_servers,
|
||||
plugin_config.mcp_server_cwd,
|
||||
)
|
||||
try:
|
||||
# 构建系统提示,分成多行以满足行长限制
|
||||
chat_type = "群聊" if is_group else "私聊"
|
||||
|
|
@ -470,7 +474,7 @@ async def process_messages(context_id: int, is_group: bool = True):
|
|||
}
|
||||
|
||||
if preset.request_with_reasoning_content:
|
||||
llm_reply["reasoning_content"] = message.reasoning_content# pyright: ignore[reportGeneralTypeIssues]
|
||||
llm_reply["reasoning_content"] = message.reasoning_content # pyright: ignore[reportGeneralTypeIssues]
|
||||
|
||||
# 发送LLM调用工具时的回复,一般没有
|
||||
if message.content:
|
||||
|
|
@ -555,7 +559,7 @@ async def process_messages(context_id: int, is_group: bool = True):
|
|||
llm_reply["images"] = reply_images # pyright: ignore[reportGeneralTypeIssues]
|
||||
|
||||
if preset.request_with_reasoning_content:
|
||||
llm_reply["reasoning_content"] = reasoning_content# pyright: ignore[reportGeneralTypeIssues]
|
||||
llm_reply["reasoning_content"] = reasoning_content # pyright: ignore[reportGeneralTypeIssues]
|
||||
|
||||
new_messages.append(llm_reply)
|
||||
|
||||
|
|
@ -600,10 +604,11 @@ async def process_messages(context_id: int, is_group: bool = True):
|
|||
state.past_events.extendleft(reversed(past_events_snapshot))
|
||||
await handler.send(Message(f"服务暂时不可用,请稍后再试\n{e!s}"))
|
||||
finally:
|
||||
state.processing = False
|
||||
state.queue.task_done()
|
||||
# 不再需要每次都清理MCPClient,因为它现在是单例
|
||||
# await mcp_client.cleanup()
|
||||
finally:
|
||||
state.processing = False
|
||||
|
||||
|
||||
# 预设切换命令
|
||||
|
|
|
|||
|
|
@ -48,6 +48,10 @@ class ScopedConfig(BaseModel):
|
|||
"你的回答应该尽量简洁、幽默、可以使用一些语气词、颜文字。你应该拒绝回答任何政治相关的问题。",
|
||||
description="默认提示词",
|
||||
)
|
||||
mcp_server_cwd: str | None = Field(
|
||||
None,
|
||||
description="command类型MCP服务器的全局工作目录(cwd)"
|
||||
)
|
||||
mcp_servers: dict[str, MCPServerConfig] = Field({}, description="MCP服务器配置")
|
||||
blacklist_user_ids: set[int] = Field(set(), description="黑名单用户ID列表")
|
||||
ignore_prefixes: list[str] = Field(
|
||||
|
|
|
|||
|
|
@ -18,12 +18,20 @@ class MCPClient:
|
|||
_SESSION_TTL_SECONDS = 600
|
||||
_SESSION_CLEANUP_INTERVAL_SECONDS = 60
|
||||
|
||||
def __new__(cls, server_config: dict[str, MCPServerConfig] | None = None):
|
||||
def __new__(
|
||||
cls,
|
||||
server_config: dict[str, MCPServerConfig] | None = None,
|
||||
default_command_cwd: str | None = None,
|
||||
):
|
||||
if cls._instance is None:
|
||||
cls._instance = super().__new__(cls)
|
||||
return cls._instance
|
||||
|
||||
def __init__(self, server_config: dict[str, MCPServerConfig] | None = None):
|
||||
def __init__(
|
||||
self,
|
||||
server_config: dict[str, MCPServerConfig] | None = None,
|
||||
default_command_cwd: str | None = None,
|
||||
):
|
||||
if self._initialized:
|
||||
return
|
||||
|
||||
|
|
@ -32,6 +40,7 @@ class MCPClient:
|
|||
|
||||
logger.info(f"正在初始化MCPClient单例,共有{len(server_config)}个服务器配置")
|
||||
self.server_config = server_config
|
||||
self.default_command_cwd = default_command_cwd
|
||||
self.sessions = {}
|
||||
self.exit_stack = AsyncExitStack()
|
||||
self._session_exit_stacks: dict[str, AsyncExitStack] = {}
|
||||
|
|
@ -47,12 +56,16 @@ class MCPClient:
|
|||
logger.debug("MCPClient单例初始化成功")
|
||||
|
||||
@classmethod
|
||||
def get_instance(cls, server_config: dict[str, MCPServerConfig] | None = None):
|
||||
def get_instance(
|
||||
cls,
|
||||
server_config: dict[str, MCPServerConfig] | None = None,
|
||||
default_command_cwd: str | None = None,
|
||||
):
|
||||
"""获取MCPClient实例"""
|
||||
if cls._instance is None:
|
||||
if server_config is None:
|
||||
raise ValueError("server_config must be provided for first initialization")
|
||||
cls._instance = cls(server_config)
|
||||
cls._instance = cls(server_config, default_command_cwd)
|
||||
return cls._instance
|
||||
|
||||
@classmethod
|
||||
|
|
@ -79,8 +92,15 @@ class MCPClient:
|
|||
sse_client(url=config.url, headers=config.headers)
|
||||
)
|
||||
elif config.command:
|
||||
stdio_params: dict[str, Any] = {
|
||||
"command": config.command,
|
||||
"args": config.args or [],
|
||||
"env": config.env or {},
|
||||
}
|
||||
if self.default_command_cwd:
|
||||
stdio_params["cwd"] = self.default_command_cwd
|
||||
transport = await session_stack.enter_async_context(
|
||||
cast(Any, stdio_client(StdioServerParameters(**config.model_dump())))
|
||||
cast(Any, stdio_client(StdioServerParameters(**stdio_params)))
|
||||
)
|
||||
else:
|
||||
raise ValueError("Server config must have either url or command")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue