feat: Introduce unified tooling system for plugins and MCP
- Added a new `tooling` module to define a unified model for tool declarations, invocations, and execution results, facilitating compatibility between plugins, legacy actions, and MCP tools. - Implemented `ToolProvider` interface for various tool providers including built-in tools, MCP tools, and plugin runtime tools. - Enhanced `MCPManager` and `MCPConnection` to support unified tool invocation and execution results. - Updated `ComponentRegistry` and related classes to accommodate the new tool specifications and descriptions. - Refactored existing components to utilize the new tooling system, ensuring backward compatibility with legacy actions. - Improved error handling and logging for tool invocations across different providers.
This commit is contained in:
64
src/maisaka/tool_provider.py
Normal file
64
src/maisaka/tool_provider.py
Normal file
@@ -0,0 +1,64 @@
|
||||
"""Maisaka 内置工具 Provider。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Awaitable, Callable
|
||||
from typing import Dict, Optional
|
||||
|
||||
from src.core.tooling import ToolExecutionContext, ToolExecutionResult, ToolInvocation, ToolProvider, ToolSpec
|
||||
|
||||
from .builtin_tools import get_builtin_tool_specs
|
||||
|
||||
BuiltinToolHandler = Callable[[ToolInvocation, Optional[ToolExecutionContext]], Awaitable[ToolExecutionResult]]
|
||||
|
||||
|
||||
class MaisakaBuiltinToolProvider(ToolProvider):
|
||||
"""Maisaka 内置工具提供者。"""
|
||||
|
||||
provider_name = "maisaka_builtin"
|
||||
provider_type = "builtin"
|
||||
|
||||
def __init__(self, handlers: Optional[Dict[str, BuiltinToolHandler]] = None) -> None:
|
||||
"""初始化内置工具 Provider。
|
||||
|
||||
Args:
|
||||
handlers: 工具名到异步处理器的映射。
|
||||
"""
|
||||
|
||||
self._handlers = dict(handlers or {})
|
||||
|
||||
async def list_tools(self) -> list[ToolSpec]:
|
||||
"""列出全部内置工具。"""
|
||||
|
||||
return list(get_builtin_tool_specs())
|
||||
|
||||
async def invoke(
|
||||
self,
|
||||
invocation: ToolInvocation,
|
||||
context: Optional[ToolExecutionContext] = None,
|
||||
) -> ToolExecutionResult:
|
||||
"""执行指定内置工具。
|
||||
|
||||
Args:
|
||||
invocation: 工具调用请求。
|
||||
context: 执行上下文。
|
||||
|
||||
Returns:
|
||||
ToolExecutionResult: 工具执行结果。
|
||||
"""
|
||||
|
||||
handler = self._handlers.get(invocation.tool_name)
|
||||
if handler is None:
|
||||
return ToolExecutionResult(
|
||||
tool_name=invocation.tool_name,
|
||||
success=False,
|
||||
error_message=f"未找到内置工具处理器:{invocation.tool_name}",
|
||||
)
|
||||
return await handler(invocation, context)
|
||||
|
||||
async def close(self) -> None:
|
||||
"""关闭 Provider。
|
||||
|
||||
内置 Provider 无需释放额外资源。
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user