使用说明
通量图像生成器MCP代理
一个强大的AI代理,通过模型上下文协议(MCP)使用Flux模型生成图像。该项目将谷歌的ADK(代理开发工具包)与定制的MCP服务器集成在一起,通过对话界面提供图像生成功能。
🌟 概述
该项目演示了如何构建一个AI代理,该代理可以:
- 使用Flux-Schnell模型生成图像
- 管理生成的图像(列表、删除、获取信息)
- 通过对话界面与用户互动
- 利用MCP(模型上下文协议)进行工具集成
🏗️ 建筑
该项目由三个主要部分组成:
- MCP服务器 (
flux_server.py)-通过MCP显示图像生成工具 - 代理定义 (
flux_agent/agent.py)-使用MCP工具集定义AI代理 - 跑者 (
run_agents.py)-交互式CLI,用于与代理聊天
┌─────────────────┐
│ User Input │
└────────┬────────┘
│
▼
┌─────────────────┐
│ run_agents.py │ ◄── Interactive CLI
│ (Runner) │
└────────┬────────┘
│
▼
┌─────────────────┐
│ LlmAgent │ ◄── Google ADK Agent
│ (Gemini 2.5) │
└────────┬────────┘
│
▼
┌─────────────────┐
│ MCP Toolset │ ◄── MCP Client
└────────┬────────┘
│
▼
┌─────────────────┐
│ flux_server.py │ ◄── MCP Server
│ (Flux Tools) │
└─────────────────┘
📁 项目结构
mcp-test/
├── .env # Environment variables (GOOGLE_API_KEY)
├── requirements.txt # Python dependencies
├── flux_server.py # MCP server with Flux image generation tools
├── run_agents.py # Interactive CLI runner
├── inspect_agent.py # Utility to inspect LlmAgent attributes
├── flux_agent/
│ ├── agent.py # Agent definition (root_agent)
│ └── .env # Agent-specific environment variables
└── images/ # Generated images storage
🔧 组件
1.MCP服务器(flux_server.py)
MCP服务器提供四种工具:
-
generate_flux_image-使用Flux模型生成图像- 参数:提示、宽度、高度、种子、随机化设置、num_inference_steps
- 返回:保存图像的文件路径
-
list_generated_images-列出所有生成的图像- 返回:图像文件路径列表
-
delete_image-删除特定图像- 参数:文件名
- 返回:确认消息
-
get_image_info-获取图像元数据- 参数:文件名
- 返回:带宽度、高度、格式、模式、大小的字典
2.代理定义(flux_agent/agent.py)
定义 root_agent 使用:
- 模型:Gemini 2.5 Flash Lite
- 工具:MCP工具集已连接到flux_server.py
- 指令:指导代理解释用户请求并生成图像
3.跑步者(run_agents.py)
交互式CLI:
- 初始化代理和会话服务
- 在循环中处理用户输入
- 实时流式传输代理响应
- 处理会话管理
🚀 安装说明
先决条件
- Python 3.8或更高版本
- Google API密钥(适用于Gemini模型)
安装
-
克隆或导航到项目目录:
cd /Users/ashokrajkoribilli/Projects/mcp-test -
安装依赖项:
pip install -r requirements.txt -
设置环境变量:
创建一个
.env使用您的Google API密钥在项目根目录中创建文件:echo "GOOGLE_API_KEY=your_api_key_here" > .env⚠️ 安全说明:电流
.env文件包含一个公开的API密钥。用您自己的密钥替换它,并且永远不要将API密钥提交给版本控制。
📖 用法
运行交互代理
python run_agents.py
交互示例
示例1:生成图像
You: Generate an image of a sunset over mountains
Assistant: I've generated an image of a sunset over mountains. The image has been saved to: ./images/flux_20251204_113045.png
示例2:列出生成的图像
You: Show me all the images I've generated
Assistant: Here are all the images you've generated:
- ./images/flux_20251204_113045.png
- ./images/flux_20251203_091523.png
示例3:自定义尺寸
You: Create a wide landscape image of a futuristic city, 1024x512
Assistant: I'll create a wide landscape image of a futuristic city for you with dimensions 1024x512.
[Agent uses generate_flux_image with width=1024, height=512]
Image saved to: ./images/flux_20251204_113102.png
退出代理
类型 exit 或 quit 要停止交互会话,请按 Ctrl+C.
🔍 运作原理
MCP通信流程
- 用户输入:用户在CLI中键入请求
- 代理处理:LlmAgent(Gemini)处理请求
- 工具选择:代理决定使用MCP工具
- MCP服务器调用:McpToolset通过stdio与flux_server.py通信
- 图像生成:Flux服务器使用Gradio客户端生成图像
- 回应:文件路径返回给代理,然后返回给用户
关键技术
- 谷歌ADK:用于构建AI代理的代理开发工具包
- MCP(模型上下文协议):工具集成协议
- Gemini 2.5 Flash精简版:谷歌代理情报法学硕士
- 通量快速:快速图像生成模型
- Gradio客户端:与Flux模型API的接口
📦 依赖项
该项目使用以下主要依赖项(来自 requirements.txt):
google-adk-谷歌代理开发工具包modelcontextprotocol-MCP实施gradio_client-基于Gradio模型的客户端Pillow-图像处理库anyio-异步I/O支持python-dotenv-环境变量管理
🛠️ 故障排除
常见问题
1.“AttributeError:'LlmAgent'对象没有属性'run'”
- 解决方案:使用
Runner类而不是直接调用代理 - 正确的图案如所示
run_agents.py
2.“未设置GOOGLE_API_KEY”
- 解决方案:创建一个
.env使用您的Google API密钥文件 - 从以下位置获取API密钥:https://aistudio.google.com/app/apikey
3.“与MCP服务器的连接超时”
- 解决方案:增加超时时间
StdioConnectionParams(默认值:120秒) - 检查
flux_server.py在正确的路径上
4.“找不到模块”错误
- 解决方案:确保安装了所有依赖项:
pip install -r requirements.txt
5.图像生成速度慢
- 解决方案:减少
num_inference_steps(速度默认为4) - 较低的值=生成速度更快,较高的值=质量更好
🔐 安全说明
- 永不承诺
.env文件 使用API密钥进行版本控制 - 添加
.env你的.gitignore文件 - 电流
.env包含公开的API密钥-立即替换它 - 使用环境特定
.env不同部署的文件
📝 其他文件
inspect_agent.py
用于检查的实用程序脚本 LlmAgent 类属性:
python inspect_agent.py
这有助于理解代理的可用方法和属性。
🚀 高级用法
独立运行MCP服务器
您可以独立运行MCP服务器进行测试:
python flux_server.py
服务器将以stdio模式启动并等待MCP协议消息。
自定义代理
编辑 flux_agent/agent.py 致:
- 更改模型(例如
gemini-2.0-pro) - 修改代理指令
- 在工具集中添加/删除工具
- 调整工具过滤器
添加更多工具
要向MCP服务器添加更多工具,请执行以下操作:
- 在中定义新函数
flux_server.py - 用
@mcp.tool() - 将工具名称添加到
tool_filter在flux_agent/agent.py
📚 了解更多
📄 许可证
该项目用于教育和示范目的。
🤝 贡献
请随意分叉此项目,并将其适应您自己的用例!
使用Google ADK和MCP创建 🚀
助理:我生成了一张山上日落的图像。图像已保存到:。/图片/flux_20251204_113045.png
**Example 2: List generated images**
你:给我看看我生成的所有图像 助理:以下是您生成的所有图像:
- ./images/flux_20251204_113045.png
- ./images/flux_20251203_091523.png
**Example 3: Custom dimensions**
你:创造一个未来城市的广阔景观图像,1024x512 助理:我将为您创建一个未来城市的宽景观图像,尺寸为1024x512。
图像保存到:。/图片/flux_20251204_113102.png
### Exiting the Agent
Type `exit` or `quit` to stop the interactive session, or press `Ctrl+C`.
## 🔍 How It Works
### MCP Communication Flow
1. **User Input**: User types a request in the CLI
2. **Agent Processing**: LlmAgent (Gemini) processes the request
3. **Tool Selection**: Agent decides to use MCP tools
4. **MCP Server Call**: McpToolset communicates with flux_server.py via stdio
5. **Image Generation**: Flux server generates image using Gradio Client
6. **Response**: File path is returned to agent, then to user
### Key Technologies
- **Google ADK**: Agent Development Kit for building AI agents
- **MCP (Model Context Protocol)**: Protocol for tool integration
- **Gemini 2.5 Flash Lite**: Google's LLM for agent intelligence
- **Flux Schnell**: Fast image generation model
- **Gradio Client**: Interface to Flux model API
## 📦 Dependencies
The project uses the following main dependencies (from `requirements.txt`):
- `google-adk` - Google Agent Development Kit
- `modelcontextprotocol` - MCP implementation
- `gradio_client` - Client for Gradio-based models
- `Pillow` - Image processing library
- `anyio` - Async I/O support
- `python-dotenv` - Environment variable management
## 🛠️ Troubleshooting
### Common Issues
**1. "AttributeError: 'LlmAgent' object has no attribute 'run'"**
- **Solution**: Use `Runner` class instead of calling agent directly
- The correct pattern is shown in `run_agents.py`
**2. "GOOGLE_API_KEY is not set"**
- **Solution**: Create a `.env` file with your Google API key
- Get your API key from: https://aistudio.google.com/app/apikey
**3. "Connection timeout to MCP server"**
- **Solution**: Increase timeout in `StdioConnectionParams` (default: 120s)
- Check that `flux_server.py` is in the correct path
**4. "Module not found" errors**
- **Solution**: Ensure all dependencies are installed:
```bash
pip install -r requirements.txt
5.图像生成速度慢
- 解决方案:减少
num_inference_steps(速度默认为4) - 较低的值=生成速度更快,较高的值=质量更好
🔐 安全说明
- 永不承诺
.env文件 使用API密钥进行版本控制 - 添加
.env你的.gitignore文件 - 电流
.env包含公开的API密钥-立即替换它 - 使用环境特定
.env不同部署的文件
📝 其他文件
inspect_agent.py
用于检查的实用程序脚本 LlmAgent 类属性:
python inspect_agent.py
这有助于理解代理的可用方法和属性。
🚀 高级用法
独立运行MCP服务器
您可以独立运行MCP服务器进行测试:
python flux_server.py
服务器将以stdio模式启动并等待MCP协议消息。
自定义代理
编辑 flux_agent/agent.py 致:
- 更改模型(例如
gemini-2.0-pro) - 修改代理指令
- 在工具集中添加/删除工具
- 调整工具过滤器
添加更多工具
要向MCP服务器添加更多工具,请执行以下操作:
- 在中定义新函数
flux_server.py - 用
@mcp.tool() - 将工具名称添加到
tool_filter在flux_agent/agent.py
📚 了解更多
📄 许可证
该项目用于教育和示范目的。
🤝 贡献
请随意分叉此项目,并将其适应您自己的用例!
使用Google ADK和MCP创建 🚀