Spaces:
Running
Running
Deploy Gradio MCP client from GitHub Codespaces
Browse files
README.md
CHANGED
|
@@ -1,231 +1,19 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
- 🔐 **Secure Authentication**: Integrates with Hugging Face Hub for model access
|
| 21 |
-
- ⚡ **GPU Acceleration**: Supports GPU execution for faster inference
|
| 22 |
-
- 🛡️ **Error Handling**: Comprehensive error messages and graceful failure handling
|
| 23 |
-
- 🔄 **Connection Management**: Proper initialization and cleanup of long-lived connections
|
| 24 |
-
|
| 25 |
-
## Prerequisites
|
| 26 |
-
|
| 27 |
-
- Python 3.10+ (ZeroGPU supports Python 3.10.13 and 3.12.12)
|
| 28 |
-
- Hugging Face account with API access
|
| 29 |
-
- HF_TOKEN environment variable or local Hugging Face authentication
|
| 30 |
-
|
| 31 |
-
## Installation
|
| 32 |
-
|
| 33 |
-
1. **Clone the repository**
|
| 34 |
-
```bash
|
| 35 |
-
git clone <repository-url>
|
| 36 |
-
cd mcp-client-agent
|
| 37 |
-
```
|
| 38 |
-
|
| 39 |
-
2. **Install dependencies**
|
| 40 |
-
```bash
|
| 41 |
-
pip install -r requirements.txt
|
| 42 |
-
```
|
| 43 |
-
|
| 44 |
-
3. **Set up Hugging Face authentication**
|
| 45 |
-
```bash
|
| 46 |
-
# Option 1: Set environment variable
|
| 47 |
-
export HF_TOKEN=your_hugging_face_token
|
| 48 |
-
|
| 49 |
-
# Option 2: Use Hugging Face CLI
|
| 50 |
-
huggingface-cli login
|
| 51 |
-
```
|
| 52 |
-
|
| 53 |
-
## Configuration
|
| 54 |
-
|
| 55 |
-
Configure the application through environment variables:
|
| 56 |
-
|
| 57 |
-
| Variable | Default | Description |
|
| 58 |
-
|----------|---------|-------------|
|
| 59 |
-
| `HF_TOKEN` | _(required)_ | Hugging Face API token for model inference |
|
| 60 |
-
| `MCP_SERVER_URL` | `https://zlysunshine-mcp-sentiment.hf.space/gradio_api/mcp/` | URL of the remote MCP server |
|
| 61 |
-
| `MODEL_ID` | `Qwen/Qwen3-Next-80B-A3B-Thinking` | Language model to use for the agent |
|
| 62 |
-
|
| 63 |
-
### Example Configuration
|
| 64 |
-
|
| 65 |
-
```bash
|
| 66 |
-
export HF_TOKEN="hf_xxxxxxxxxxxxxxxxxxxx"
|
| 67 |
-
export MCP_SERVER_URL="https://your-mcp-server.hf.space/gradio_api/mcp/"
|
| 68 |
-
export MODEL_ID="meta-llama/Llama-3-70b-chat-hf"
|
| 69 |
-
```
|
| 70 |
-
|
| 71 |
-
## Usage
|
| 72 |
-
|
| 73 |
-
### Running Locally
|
| 74 |
-
|
| 75 |
-
```bash
|
| 76 |
-
python app.py
|
| 77 |
-
```
|
| 78 |
-
|
| 79 |
-
The application will start a Gradio server at `http://localhost:7860`
|
| 80 |
-
|
| 81 |
-
### Running on Hugging Face Spaces
|
| 82 |
-
|
| 83 |
-
**Step-by-step deployment guide:**
|
| 84 |
-
|
| 85 |
-
1. **Create a new Space**
|
| 86 |
-
- Go to [huggingface.co/spaces](https://huggingface.co/spaces)
|
| 87 |
-
- Click "Create new Space"
|
| 88 |
-
- Choose a name for your Space (e.g., `mcp-client-agent`)
|
| 89 |
-
- Select **Gradio** as the Space SDK
|
| 90 |
-
- Choose "Public" or "Private" visibility
|
| 91 |
-
- Click "Create Space"
|
| 92 |
-
|
| 93 |
-
2. **Upload the repository content**
|
| 94 |
-
- Clone or download this repository
|
| 95 |
-
- Push the code to your Space repository:
|
| 96 |
-
```bash
|
| 97 |
-
git clone https://huggingface.co/spaces/[your-username]/[your-space-name]
|
| 98 |
-
cd [your-space-name]
|
| 99 |
-
# Copy files from mcp-client-agent
|
| 100 |
-
cp /path/to/mcp-client-agent/* .
|
| 101 |
-
git add .
|
| 102 |
-
git commit -m "Initial commit: MCP client agent"
|
| 103 |
-
git push
|
| 104 |
-
```
|
| 105 |
-
|
| 106 |
-
3. **Configure secrets**
|
| 107 |
-
- Go to your Space's Settings → Secrets and variables
|
| 108 |
-
- Add a new secret:
|
| 109 |
-
- **Name**: `HF_TOKEN`
|
| 110 |
-
- **Value**: Your Hugging Face user access token
|
| 111 |
-
- (Optional) Add other environment variables:
|
| 112 |
-
- `MCP_SERVER_URL`: Custom MCP server endpoint
|
| 113 |
-
- `MODEL_ID`: Alternative model for inference
|
| 114 |
-
|
| 115 |
-
4. **Optional: Configure Space settings**
|
| 116 |
-
- **Hardware**: Recommend at least CPU, or GPU for faster inference
|
| 117 |
-
- **Persistent storage**: Enable if you need to save data
|
| 118 |
-
- **Environment variables**: Add any additional config (see Configuration section)
|
| 119 |
-
|
| 120 |
-
5. **Deployment**
|
| 121 |
-
- The Space will automatically build and deploy when you push code
|
| 122 |
-
- Monitor the build logs in the Space's "Build" tab
|
| 123 |
-
- Once deployed, your app will be live at `https://huggingface.co/spaces/[your-username]/[your-space-name]`
|
| 124 |
-
|
| 125 |
-
**Important notes:**
|
| 126 |
-
- ZeroGPU: If you have access to ZeroGPU, the `@spaces.GPU(duration=20)` decorator in `app.py` will automatically use it for faster inference
|
| 127 |
-
- Token security: Never commit your HF_TOKEN to the repository—always use Secrets
|
| 128 |
-
- Server connectivity: Ensure the MCP server URL is accessible from the Space environment
|
| 129 |
-
|
| 130 |
-
### Interacting with the Agent
|
| 131 |
-
|
| 132 |
-
1. Open the chat interface
|
| 133 |
-
2. Ask questions that require using the MCP server's tools
|
| 134 |
-
3. The agent will automatically:
|
| 135 |
-
- Analyze your question
|
| 136 |
-
- Choose appropriate tools from the MCP server
|
| 137 |
-
- Execute the tools
|
| 138 |
-
- Return the results
|
| 139 |
-
|
| 140 |
-
**Example queries:**
|
| 141 |
-
- "You must use the sentiment_analysis tool to analyze: 'This MCP course is excellent, but deployment was frustrating.'"
|
| 142 |
-
- "Call the sentiment tool for: 'I am happy that the application finally works.'"
|
| 143 |
-
|
| 144 |
-
## How It Works
|
| 145 |
-
|
| 146 |
-
### Architecture
|
| 147 |
-
|
| 148 |
-
```
|
| 149 |
-
User Input
|
| 150 |
-
↓
|
| 151 |
-
Gradio Chat Interface
|
| 152 |
-
↓
|
| 153 |
-
CodeAgent (Smolagents)
|
| 154 |
-
↓
|
| 155 |
-
MCP Client ←→ Remote MCP Server
|
| 156 |
-
↓
|
| 157 |
-
Tool Discovery & Execution
|
| 158 |
-
↓
|
| 159 |
-
LLM Processing
|
| 160 |
-
↓
|
| 161 |
-
Response to User
|
| 162 |
-
```
|
| 163 |
-
|
| 164 |
-
### Key Components
|
| 165 |
-
|
| 166 |
-
1. **Gradio Interface**: Provides the web-based chat UI
|
| 167 |
-
2. **CodeAgent**: Orchestrates tool selection and execution
|
| 168 |
-
3. **MCPClient**: Connects to and communicates with MCP servers
|
| 169 |
-
4. **InferenceClientModel**: Interfaces with Hugging Face models for inference
|
| 170 |
-
5. **Tool Discovery**: Automatically discovers available tools from the MCP server
|
| 171 |
-
|
| 172 |
-
### Process Flow
|
| 173 |
-
|
| 174 |
-
1. User submits a message through the chat interface
|
| 175 |
-
2. The agent receives the message and available tools
|
| 176 |
-
3. The LLM decides which tools are needed to answer the question
|
| 177 |
-
4. Tools are executed with appropriate parameters
|
| 178 |
-
5. Results are processed and returned to the user
|
| 179 |
-
6. Response is displayed in the chat interface
|
| 180 |
-
|
| 181 |
-
## Dependencies
|
| 182 |
-
|
| 183 |
-
- **gradio[mcp]**: Web UI framework with MCP support
|
| 184 |
-
- **smolagents[mcp]**: AI agent library with MCP integration
|
| 185 |
-
- **spaces**: Hugging Face Spaces integration
|
| 186 |
-
- **huggingface_hub**: Hugging Face API client
|
| 187 |
-
|
| 188 |
-
See `requirements.txt` for version details.
|
| 189 |
-
|
| 190 |
-
## Troubleshooting
|
| 191 |
-
|
| 192 |
-
### "No Hugging Face token was found"
|
| 193 |
-
- Ensure `HF_TOKEN` environment variable is set
|
| 194 |
-
- Or run `huggingface-cli login` to save credentials locally
|
| 195 |
-
|
| 196 |
-
### "The MCP server connected successfully but returned no tools"
|
| 197 |
-
- Verify the `MCP_SERVER_URL` is correct and accessible
|
| 198 |
-
- Check that the MCP server is running and serving tools
|
| 199 |
-
|
| 200 |
-
### "The MCP client could not complete the request"
|
| 201 |
-
- Check the error details in the response
|
| 202 |
-
- Verify internet connectivity
|
| 203 |
-
- Ensure the MCP server is still running
|
| 204 |
-
- Check that your HF_TOKEN has inference API access
|
| 205 |
-
|
| 206 |
-
## Development
|
| 207 |
-
|
| 208 |
-
### Running with Custom MCP Server
|
| 209 |
-
|
| 210 |
-
```python
|
| 211 |
-
export MCP_SERVER_URL="http://your-server:8000/mcp/"
|
| 212 |
-
python app.py
|
| 213 |
-
```
|
| 214 |
-
|
| 215 |
-
### Adjusting Agent Parameters
|
| 216 |
-
|
| 217 |
-
Edit `app.py` to modify:
|
| 218 |
-
- `max_steps`: Maximum reasoning steps (default: 4)
|
| 219 |
-
- `max_tokens`: Maximum token output (default: 1200)
|
| 220 |
-
- `timeout`: Request timeout in seconds (default: 120)
|
| 221 |
-
|
| 222 |
-
## License
|
| 223 |
-
|
| 224 |
-
See [LICENSE](LICENSE) for details.
|
| 225 |
-
|
| 226 |
-
## Resources
|
| 227 |
-
|
| 228 |
-
- [Model Context Protocol Documentation](https://modelcontextprotocol.io/)
|
| 229 |
-
- [Smolagents Documentation](https://huggingface.co/docs/smolagents)
|
| 230 |
-
- [Gradio Documentation](https://www.gradio.app/docs)
|
| 231 |
-
- [Hugging Face Spaces](https://huggingface.co/spaces)
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Gradio MCP Client Agent
|
| 3 |
+
emoji: 🤖
|
| 4 |
+
colorFrom: purple
|
| 5 |
+
colorTo: blue
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: "6.20.0"
|
| 8 |
+
python_version: "3.10"
|
| 9 |
+
app_file: app.py
|
| 10 |
+
pinned: false
|
| 11 |
+
short_description: A Gradio agent that consumes tools from a remote MCP server.
|
| 12 |
+
tags:
|
| 13 |
+
- mcp
|
| 14 |
+
- gradio
|
| 15 |
+
- smolagents
|
| 16 |
+
- agent
|
| 17 |
+
---
|
| 18 |
+
|
| 19 |
+
# -mcp-client-agent
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|