使用说明

项目简介

Mainframe Orchestra 是一个用于构建 LLM 应用的 Agent 框架,它通过 Model Context Protocol (MCP) 适配器,可以轻松集成各种 MCP 服务器提供的工具和服务。借助 MCP,Orchestra Agent 可以安全、可扩展地利用外部上下文信息和功能,例如访问文件系统、调用 Web 服务、操作浏览器等,从而构建更强大的 LLM 应用。

主要功能点

  • MCP 集成: 通过 'MCPOrchestra' 适配器,连接并使用兼容 MCP 协议的服务器,如 FastMCP, Playwright, Slack, Filesystem 等。
  • 工具扩展: 允许 Agent 使用来自 MCP 服务器的工具,无需在 Orchestra 框架内重新实现,极大地扩展了 Agent 的能力边界。
  • 灵活配置: 支持多种 MCP 服务器连接方式,包括本地启动和远程连接,方便用户根据需求配置。
  • 易于使用: 通过简单的 API 调用即可将 MCP 服务器的工具集成到 Orchestra Agent 中。

安装步骤

  1. 确保已安装 Python 环境。
  2. 使用 pip 安装 'mainframe-orchestra' 包:
    pip install mainframe-orchestra
  3. 根据需要安装额外的依赖包,例如使用 FastMCP 示例可能需要安装 'fastmcp'。

服务器配置

MCP 客户端(例如 Orchestra Agent)需要配置 MCP 服务器的连接信息才能使用其提供的工具。以下是一个连接 FastMCP 服务器的配置示例,通常在客户端代码中配置:

{
  "server_name": "calculator",  // MCP 服务器的名称,客户端用于标识连接
  "command": "python",         // 启动 MCP 服务器的命令,例如 python 或 node
  "args": ["mcp_fast_calc.py"], // 启动命令的参数,指向 MCP 服务器脚本
  "start_server": true,        // 是否在客户端启动时自动启动 MCP 服务器
  "server_startup_delay": 2.0  // 服务器启动后的等待时间(秒),确保服务器完全启动
}

基本使用方法

  1. 创建 MCPOrchestra 客户端:

    from mainframe_orchestra import MCPOrchestra
    import asyncio
    
    async def main():
        async with MCPOrchestra() as mcp_client:
            # ... 后续步骤
  2. 连接 MCP 服务器: 使用 'mcp_client.connect()' 方法,传入服务器配置信息,建立连接。

    await mcp_client.connect(
        server_name="calculator",
        command="python",
        args=["mcp_fast_calc.py"],
        start_server=True,
        server_startup_delay=2.0
    )
  3. 获取 MCP 服务器工具: 使用 'mcp_client.get_tools()' 或 'mcp_client.get_server_tools(server_name)' 获取工具列表。

    calculator_tools = mcp_client.get_tools() # 获取所有连接的 MCP 服务器的工具
    # 或
    calculator_tools = mcp_client.get_server_tools("calculator") # 获取指定服务器的工具
  4. 创建 Agent 并使用 MCP 工具: 将获取的工具列表添加到 Agent 的 'tools' 参数中。

    from mainframe_orchestra import Agent, OpenaiModels
    
    agent = Agent(
        agent_id="calculator_agent",
        role="Math Assistant",
        goal="Help users perform mathematical calculations",
        tools=calculator_tools,
        llm=OpenaiModels.gpt_4o
    )
  5. 创建 Task 并执行: 像使用普通 Orchestra 工具一样使用 MCP 工具。

    from mainframe_orchestra import Task
    
    result = await Task.create(
        agent=agent,
        instruction="You need to calculate the sum of 42 and 58, and then multiply the result by 5. Respond with the final answer."
    )
    print(result)

更详细的使用方法和示例代码,请参考仓库 'examples/python/mcp' 目录下的示例脚本。

信息

分类

AI与计算