使用说明

项目简介

CLAP Agent MCP Server Examples 是 CLAP (Cognitive Layer Agent Package) 框架的一部分,展示了如何使用 'fastmcp' 库创建符合 Model Context Protocol (MCP) 规范的服务器。这些示例服务器旨在为 CLAP Agent 或其他 MCP 客户端提供远程工具和资源,实现功能扩展和上下文信息共享。

主要功能点

  • 工具注册与调用: 通过装饰器轻松将 Python 函数注册为 MCP 工具,并支持远程调用执行。
  • 资源托管: 支持托管静态和动态资源,允许客户端通过标准化的 MCP 协议访问。
  • Prompt 模板: 定义和管理 Prompt 模板,为客户端提供可定制的 LLM 交互模式。
  • SSE 传输协议: 使用 Server-Sent Events (SSE) 作为默认传输协议,实现简单高效的客户端-服务器通信。
  • 示例丰富: 提供多个示例服务器 (simple_mcp.py, simple2_mcp.py, example_server.py),演示不同功能配置。

安装步骤

  1. 确保已安装 Python 3.10 或更高版本。
  2. 安装 'clap-agents' 库,其中包含了运行 MCP 服务器示例所需的依赖:
    pip install clap-agents

服务器配置

MCP 服务器配置用于 MCP 客户端 (如 CLAP Agent) 连接和使用远程功能。以下 JSON 配置信息描述了如何配置连接到示例 MCP 服务器:

[
  {
    "server name": "adder_server",
    "command": "python",
    "args": ["examples/simple_mcp.py"] // 启动一个提供 'add' 工具的服务器,默认监听 http://localhost:8000/sse
  },
  {
    "server name": "subtract_server",
    "command": "python",
    "args": ["examples/simple2_mcp.py"] // 启动一个提供 'sub' 工具的服务器,默认监听 http://localhost:8001/sse
  },
  {
    "server name": "example_server",
    "command": "python",
    "args": ["examples/example_server.py"] // 启动一个提供 'add' 工具、资源和 prompt 的综合示例服务器,默认监听 http://localhost:8000/sse
  }
]

配置说明:

  • server name: 为 MCP 服务器指定一个逻辑名称,客户端将使用此名称引用服务器。
  • command: 启动 MCP 服务器的命令,通常为 'python'。
  • args: 命令参数列表,用于指定要运行的服务器脚本文件路径 ('examples/simple_mcp.py' 等)。

注意: 客户端需要根据实际运行的服务器地址 (例如,如果服务器不在本地运行,则需要修改为服务器的 IP 地址或域名) 和端口进行配置。示例服务器默认使用 SSE 协议,端口通常在脚本中指定 (如 'simple_mcp.py' 默认使用 8000 端口)。

基本使用方法

  1. 启动 MCP 服务器: 在不同的终端窗口中,分别运行示例服务器脚本。例如,启动 'simple_mcp.py' 服务器,在终端中执行:

    python examples/simple_mcp.py

    这将启动一个简单的 MCP 服务器,并输出服务器地址 (如 'Starting minimal MCP server on http://localhost:8000/sse')。

  2. 配置 MCP 客户端: 在 CLAP Agent 或其他 MCP 客户端的配置中,添加上述 '服务器配置' 中相应的服务器信息。确保 'server name' 与配置一致,并根据实际服务器地址和端口更新 URL。

  3. 客户端调用远程功能: 客户端 (如 CLAP Agent) 启动后,将自动连接到配置的 MCP 服务器。Agent 可以发现并调用服务器提供的工具和资源,具体使用方法请参考 CLAP Agent 框架的相关文档和示例代码 ('examples/mcp_test_agent.py', 'examples/mcp_team_agent.py', 'examples/google_mcp.py')。

通过以上步骤,您可以快速体验 CLAP Agent MCP Server Examples,并将其集成到您的 LLM 应用中,实现强大的远程功能扩展。

信息

分类

AI与计算