模型上下文协议服务器与代理CLI

该项目包含样本MCP服务器、交互式代理CLI和多代理编排示例,用于集成MCP工具和LLM API。

  • by williamguomd
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 williamguomd/GenAIPractise

使用说明

GenAI实践-MCP服务器和代理CLI

此项目包含

  • MCP(模型上下文协议)服务器示例
  • 交互式Agent CLI
  • 多代理编排示例--TODO

项目结构

.
├── agent/                      # Agent CLI application
│   ├── __init__.py
│   ├── agent_cli.py            # Main CLI interface
│   ├── mcp_manager.py          # MCP server integration
│   ├── llm_client.py           # LLM API client
│   ├── llm_service.py           # LangChain-based LLM service
│   ├── prompt_loader.py        # YAML prompt template loader
│   ├── video_evaluation.py     # Video evaluation using LLM
│   ├── video_processor.py      # Video frame extraction and encoding
│   ├── prompt/                 # Prompt templates
│   │   └── tennis_evaluation_prompt.yaml
│   └── video/                  # Video files for evaluation
│       └── tennis.mov
├── mcp_servers/                # Directory containing all MCP servers
│   ├── __init__.py
│   ├── start_mcp_server.sh     # Shell script to start MCP servers
│   └── example_server/         # Example MCP server
│       ├── __init__.py
│       └── server.py
├── main.py                     # Main entry point
├── pyproject.toml              # Project configuration
├── install_agent.sh            # Installation script for agent CLI
├── mcp.json                    # MCP configuration
└── README.md

设置

  1. 安装依赖项:

    uv sync
    
  2. 运行特定的MCP服务器:

    使用shell脚本(推荐):

    ./mcp_servers/start_mcp_server.sh [server_name]
    

    例子:

    ./mcp_servers/start_mcp_server.sh example_server
    

    如果未提供服务器名称,则默认为 example_server.

    或者直接使用紫外线:

    uv run python -m mcp_servers.example_server.server
    

代理CLI

Agent CLI是一个交互式命令行界面,与MCP工具和LLM API集成。

安装

  1. 运行安装脚本:

    ./install_agent.sh
    
  2. 添加 ~/.local/bin 到您的路径(如果还没有):

    echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.zshrc
    source ~/.zshrc
    
  3. 设置您的OpenAI API配置:

    创建一个 .env 文件,并添加您的API密钥和模型:

    OPENAI_API_KEY=your-actual-api-key-here
    OPENAI_MODEL=gpt-4o
    

    或者将它们设置为环境变量:

    export OPENAI_API_KEY="your-api-key-here"
    export OPENAI_MODEL="gpt-4o"
    

    这 .env 文件已在 .gitignore 以确保API密钥的安全。

  4. 在中配置MCP服务器 ~/.agent/mcp.json (安装时从项目根目录复制)

用法

启动代理:

agent

您将看到一个交互式提示:

agent>

命令

  • /tools -列出所有可用的MCP工具
  • /help -显示帮助消息
  • /exit 或 /quit -退出代理
  • /clear -清除屏幕

特性

  • 交互式REPL:类似于Python的交互式shell agent> 提示
  • MCP集成:自动从配置的MCP服务器中发现并列出工具
  • LLM集成:直接向LLM发送请求并获得响应
  • 丰富的用户界面:漂亮的终端界面,支持颜色和标记

MCP配置

代理从以下位置读取MCP服务器配置 ~/.agent/mcp.json (在安装过程中从项目根目录复制)。例子:

{
  "mcp_servers": [
    {
      "name": "example_server",
      "command": "mcp_servers/start_mcp_server.sh",
      "args": ["example_server"],
      "cwd": "/path/to/project"
    }
  ]
}

视频评估

该项目包括一个视频评估示例,该示例使用LLM视觉模型来分析网球视频。

用法

在默认视频上运行视频评估:

python -m agent.video_evaluation

这将:

  1. 从中提取帧 video/tennis.mov
  2. 将帧编码为base64图像
  3. 向LLM发送帧和提示(通过配置 .env)
  4. 解析并显示评估结果

配置

视频评估使用:

  • 提示模板: agent/prompt/tennis_evaluation_prompt.yaml
  • 视频文件: video/tennis.mov (默认)
  • API配置:来自 .env 文件(OPENAI_API_KEY 和 OPENAI_MODEL)

定制

您可以通过以下方式自定义评估:

  • 在中修改提示模板 agent/prompt/tennis_evaluation_prompt.yaml
  • 在中更改视频文件路径 agent/video_evaluation.py main() 函数
  • 调整 max_frames 参数在 VideoEvaluator (默认值:10帧)

添加新的MCP服务器

  1. 在下创建新目录 mcp_servers/
  2. 添加服务器实现
  3. 遵循MCP协议规范
  4. 更新 mcp.json 包括您的新服务器