使用说明

项目简介

mcp-agent 是一个轻量级的 Python 框架,旨在帮助开发者使用 Model Context Protocol (MCP) 构建高效的 AI 代理。该框架受到 Anthropic 的研究成果 "Building Effective Agents" 和 Model Context Protocol 的启发,旨在简化 AI 代理的开发流程,并提供可组合的模式来构建强大的代理应用。

主要功能点

  • 简化 MCP 服务器连接: 'mcp-agent' 框架处理了与 MCP 服务器连接和生命周期管理的复杂性,让开发者可以专注于代理的核心业务逻辑。
  • 实现有效代理模式: 框架实现了 "Building Effective Agents" 中描述的各种代理模式,例如 Augmented LLM, Parallel, Router, Intent-Classifier, Evaluator-Optimizer 和 Orchestrator-Workers 等,并以可组合的方式提供,方便开发者灵活地组合和扩展这些模式。
  • 支持多代理编排: 框架实现了 OpenAI 的 Swarm 模式,支持模型无关的多代理编排,使得构建复杂的协作式 AI 系统成为可能。
  • 轻量级和可定制: 'mcp-agent' 是一个轻量级的库,更像是一个代理模式库而非重型框架,开发者可以轻松地定制和扩展框架的各个组件,例如模型提供商、日志记录、编排器等。
  • 与 MCP 服务器生态系统互操作: 随着越来越多的服务支持 MCP 协议,'mcp-agent' 可以帮助开发者构建能够开箱即用利用这些服务的强大 AI 代理。

安装步骤

推荐使用 'uv' 包管理器来安装 'mcp-agent':

uv add "mcp-agent"

或者使用 'pip':

pip install mcp-agent

服务器配置

'mcp-agent' 框架本身是用于构建 MCP 客户端应用的,它主要用于连接和管理外部的 MCP 服务器,例如 'fetch' 和 'filesystem' 服务器。在 'mcp_agent.config.yaml' 配置文件中,您可以配置要连接的 MCP 服务器及其启动方式。

以下是一个 'mcp' 配置部分的示例,展示了如何配置 'fetch' 和 'filesystem' 服务器:

mcp:
  servers:
    fetch:
      command: "uvx"  # 启动 fetch 服务器的命令
      args: ["mcp-server-fetch"]  # 传递给 fetch 服务器的参数
      description: "Fetch content at URLs from the world wide web" # 服务器描述 (可选)
    filesystem:
      command: "npx"  # 启动 filesystem 服务器的命令
      args:
        [
          "-y",
          "@modelcontextprotocol/server-filesystem",
          "<add_your_directories>", #  **重要**:  请替换为你要允许文件系统服务器访问的目录路径,例如:  "/path/to/your/directory"
        ] # 传递给 filesystem 服务器的参数,需要配置允许访问的目录
      description: "提供对本地文件系统的访问" # 服务器描述 (可选)

基本使用方法

以下代码展示了如何使用 'mcp-agent' 创建一个简单的 "finder" 代理,它可以读取本地文件系统和获取 URL 内容:

import asyncio
import os

from mcp_agent.app import MCPApp
from mcp_agent.agents.agent import Agent
from mcp_agent.workflows.llm.augmented_llm_openai import OpenAIAugmentedLLM

app = MCPApp(name="hello_world_agent")

async def example_usage():
    async with app.run() as mcp_agent_app:
        logger = mcp_agent_app.logger
        # This agent can read the filesystem or fetch URLs
        finder_agent = Agent(
            name="finder",
            instruction="""You can read local files or fetch URLs.
                Return the requested information when asked.""",
            server_names=["fetch", "filesystem"], # MCP servers this Agent can use
        )

        async with finder_agent:
            # Automatically initializes the MCP servers and adds their tools for LLM use
            tools = await finder_agent.list_tools()
            logger.info(f"Tools available:", data=tools)

            # Attach an OpenAI LLM to the agent (defaults to GPT-4o)
            llm = await finder_agent.attach_llm(OpenAIAugmentedLLM)

            # This will perform a file lookup and read using the filesystem server
            result = await llm.generate_str(
                message="Show me what's in README.md verbatim"
            )
            logger.info(f"README.md contents: {result}")

            # Uses the fetch server to fetch the content from URL
            result = await llm.generate_str(
                message="Print the first two paragraphs from https://www.anthropic.com/research/building-effective-agents"
            )
            logger.info(f"Blog intro: {result}")

            # Multi-turn interactions by default
            result = await llm.generate_str("Summarize that in a 128-char tweet")
            logger.info(f"Tweet: {result}")

if __name__ == "__main__":
    asyncio.run(example_usage())

这段代码演示了如何:

  1. 创建一个 'MCPApp' 实例来管理应用程序的生命周期和配置。
  2. 定义一个 'Agent' ,并指定它可以使用的 MCP 服务器 ('fetch' 和 'filesystem')。
  3. 初始化 'Agent' 并列出可用的工具。
  4. 将 OpenAI 的 LLM 连接到 'Agent' 。
  5. 使用 LLM 和工具来执行文件读取和 URL 获取等任务。

更多详细的使用方法和示例,请参考仓库中的 'examples' 目录。

信息

分类

AI与计算