AI代理服务器

一个结合了模型上下文协议(MCP)服务器能力和独立命令行代理的双模式AI代理系统,支持API交互、文件管理、网页抓取、数据处理和AI任务执行。

  • by elcaiseri
  • Python
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 elcaiseri/mcp-ai-agent-server

使用说明

MCP AI代理服务器

一个双模AI代理系统,将模型上下文协议(MCP)服务器功能与由LangChain支持的独立CLI代理相结合。

🎯 两种操作模式

1. MCP服务器模式

作为MCP服务器运行,可以连接到任何MCP客户端(如Claude Desktop)或使用MCP检查器进行检查。

2. CLI代理模式

作为独立的命令行代理运行,用于直接交互和任务执行。

✨ 能力

  • 🌐 API交互:获取天气数据、新闻文章等
  • 📁 文件管理:读取、写入、搜索和组织文件
  • 🔍 Web剪贴:从网站提取数据
  • 📊 数据处理:分析和转换数据
  • 💡 AI驱动的任务:使用LangChain进行智能决策

🛠️ 可用工具

  1. 天气工具:获取任何位置的当前天气
  2. 新闻工具:按主题获取最新新闻文章
  3. 文件管理器:创建、读取、更新、删除文件
  4. 网页抓取器:下载和解析web内容
  5. 计算器:执行复杂的计算
  6. 搜索工具:搜索文件和数据
  7. AI 代理:LangChain MCP客户端驱动的推理和任务执行

📦 安装

先决条件

  • Python 3.10+
  • 紫外线 包管理器
  • Node.js(用于MCP检查器)

设置

  1. 克隆存储库:
git clone https://github.com/elcaiseri/mcp-ai-agent-server.git
cd mcp-ai-agent-server
  1. 使用uv安装依赖项:
uv pip install -e .
  1. 设置环境变量:
cp .env.example .env
# Edit .env with your API keys

中所需的API密钥 .env:

OPENAI_API_KEY=your_openai_api_key
OPENWEATHER_API_KEY=your_openweather_api_key
NEWS_API_KEY=your_news_api_key

🚀 用法

模式1:带检查器的MCP服务器

MCP服务器可以使用官方的MCP Inspector工具进行测试和调试。

使用inspector启动服务器:

npx @modelcontextprotocol/inspector uv run python -m src.server

这将:

  • 启动MCP服务器
  • 在浏览器中打开MCP检查器
  • 允许您以交互方式测试所有可用工具
  • 实时查看请求/响应日志

连接到MCP客户端:

添加到您的MCP客户端配置中(例如,Claude Desktop):

{
  "mcpServers": {
    "ai-agent": {
      "command": "uv",
      "args": ["run", "python", "-m", "src.server"],
      "cwd": "/path/to/mcp-ai-agent-server"
    }
  }
}

模式2:独立CLI代理

直接从命令行运行代理以进行交互式会话。

启动CLI代理:

uv run python -m src.client

交互示例:

> What's the weather in Tokyo?
> Fetch the latest news about AI
> Create a file called notes.txt with today's summary
> Search for all Python files in the current directory

CLI代理使用LangChain MCP客户端:

  • 理解自然语言命令
  • 自动选择合适的工具
  • 将多个操作链接在一起
  • 提供对话式回应

📚 工具示例

天气工具

# Get current weather for a location
get_weather(location="New York")
# Returns: temperature, conditions, humidity, wind speed

新闻工具

# Get latest news on a topic
fetch_news(topic="technology", limit=5)
# Returns: list of articles with title, description, URL

文件操作

# Create a file
create_file(path="data/output.txt", content="Hello World")

# Read a file
read_file(path="data/input.txt")

# Search files
search_files(directory=".", pattern="*.py")

网络抓取

# Fetch webpage content
fetch_webpage(url="https://example.com")
# Returns: parsed HTML content and text

AI代理任务

# Execute complex multi-step tasks
execute_agent_task(task="Analyze the weather in Tokyo and write a report")

🏗️ 建筑

mcp-ai-agent-server/
├── src/
│   ├── server.py          # Main MCP server implementation
│   ├── client.py          # Standalone CLI agent implementation
│   ├── tools/             # Individual tool implementations
│   │   ├── weather.py
│   │   ├── news.py
│   │   ├── file_manager.py
│   │   ├── web_fetcher.py
│   │   └── calculator.py
│   └── utils/             # Utilities
│       └── config.py
├── tests/                 # Test suite
├── pyproject.toml         # Project configuration
├── .env.example           # Environment template
└── README.md

🔧 发展

运行测试

uv run pytest

代码格式化

uv run black src/
uv run ruff check src/

许可证

MIT许可证

🤝 贡献

欢迎投稿!请随时提交拉取请求。