tooluniverse.mcp_tool_registry 模块¶
ToolUniverse 的 MCP 工具注册系统
该模块提供将本地工具注册为MCP工具的功能,并通过ToolUniverse集成实现这些工具在远程服务器上的自动加载。
Usage¶
Server Side (Tool Provider): .. code-block:: python
- from tooluniverse.mcp_tool_registry import (
register_mcp_tool, start_mcp_server
)
- @注册_mcp_工具(
tool_type_name=”我的分析工具”, config={
description”: “执行自定义数据分析
}, mcp_config={
“server_name”: “Custom Analysis Server”,
“host”: “0.0.0.0”, “port”: 8001
请提供具体的英文文本内容,我将为您翻译成符合要求的中文。
) 类 MyAnalysisTool:
`python def run(self, arguments): `return {“result”: “分析完成”}
# 启动带有已注册工具的MCP服务器 start_mcp_server() ```
客户端(工具消费者):
`python
from tooluniverse import ToolUniverse
`
# 自动发现并从远程服务器加载MCP工具 tu = ToolUniverse() tu.load_mcp_tools(server_urls=[”http://localhost:8001”])
# Use the remote tool result = tu.run_tool(“my_analysis_tool”, {“data”: “input”}) ```
- tooluniverse.mcp_tool_registry.register_mcp_tool(tool_type_name=None, config=None, mcp_config=None)[源代码]¶
Decorator to register a tool class for MCP server exposure.
This decorator registers tools both globally (via register_tool) and for MCP server management. The global registration allows ToolUniverse to properly instantiate tools, while MCP registration controls server exposure. The parameters and behavior are identical to register_tool, with an optional mcp_config parameter for server configuration.
- 参数:
tool_type_name (
str, optional) – 工具类型的自定义名称。与 register_tool 相同。config (
dict, optional) – 工具配置字典。与 register_tool 相同。mcp_config (
dict, optional) – 额外的 MCP 服务器配置。可包括: - server_name:MCP 服务器名称 - host:服务器主机(默认值:”localhost”) - port:服务器端口(默认值:8000) - transport:”http” 或 “stdio”(默认值:”http”) - auto_start:工具注册时是否自动启动服务器
- 返回:
Decorator function that registers the tool class for MCP server only.
- 返回类型:
function
- tooluniverse.mcp_tool_registry.register_mcp_tool_from_config(tool_class, config)[源代码]¶
使用配置将现有工具类注册为 MCP 工具。
This function provides a programmatic way to register tools as MCP tools without using decorators, useful for dynamic tool registration. Just like register_mcp_tool decorator, this registers tools for MCP exposure only.
- 参数:
示例
`python class ExistingTool: ``python def run(self, arguments): `return {“status”: “已处理”}
- register_mcp_tool_from_config(ExistingTool, {
“name”: “existing_tool”, “description”: “通过 MCP 暴露的现有工具”, “mcp_config”: {“port”: 8002}
- tooluniverse.mcp_tool_registry.get_mcp_tool_configs()[源代码]¶
Get the MCP tool configurations for ToolUniverse.
- tooluniverse.mcp_tool_registry.get_registered_tools()[源代码]¶
获取所有已注册MCP工具及其信息的列表。
- 返回值
包含工具信息(包括名称、描述和端口)的字典列表。
- tooluniverse.mcp_tool_registry.start_mcp_server(port=None, **kwargs)[源代码]¶
启动已注册工具的 MCP 服务器。
- 参数:
port (
int, optional) – 用于启动服务器的特定端口。如果为 None,则启动所有已注册工具的服务器。**kwargs – 传递给 SMCP 服务器的附加参数
- tooluniverse.mcp_tool_registry.stop_mcp_server(port=None)[源代码]¶
停止 MCP 服务器。
- 参数:
port (
int, optional) – 要停止服务器的特定端口。如果为 None,则停止所有服务器。
- tooluniverse.mcp_tool_registry.load_mcp_tools_to_tooluniverse(tu, server_urls=None)[源代码]¶
将 MCP 工具从服务器加载到 ToolUniverse 实例中。
- 参数:
示例
`python from tooluniverse import ToolUniverse from tooluniverse.mcp_tool_registry import load_mcp_tools_to_tooluniverse `tu = 工具宇宙()
# 从特定服务器加载 load_mcp_tools_to_tooluniverse(tu, [
请提供需要翻译的英文文本,我将为您提供符合要求的中文翻译。