yuxh1996 commited on
Commit
afb386c
Β·
1 Parent(s): f4114e2

Implement dual-mode authentication strategy

Browse files

- Space demo: Uses provided API key for demonstration (limited usage)
- MCP clients: Must provide own API key for production usage
- Check SPACE_ID environment variable to detect Hugging Face Space
- Update documentation to clarify API key requirements
- Add comprehensive authentication strategy logging
- Ensure security: client API keys required for production use

Features:
βœ… Space demo works with pre-configured API key
βœ… MCP clients must provide their own API key
βœ… Clear documentation for both usage modes
βœ… Production-ready authentication strategy

Files changed (2) hide show
  1. README.md +10 -34
  2. app.py +58 -17
README.md CHANGED
@@ -96,9 +96,9 @@ This Gradio app serves as both a **web interface** and a **full MCP server**, pr
96
 
97
  ## πŸ”§ MCP Client Configuration
98
 
99
- ### Method 1: With API Key in Headers (Recommended)
100
 
101
- Add this to your Claude Desktop configuration file:
102
 
103
  ```json
104
  {
@@ -119,23 +119,7 @@ Add this to your Claude Desktop configuration file:
119
  }
120
  ```
121
 
122
- ### Method 2: With Environment Variable (Fallback)
123
-
124
- ```json
125
- {
126
- "mcpServers": {
127
- "a1d-gradio": {
128
- "command": "npx",
129
- "args": [
130
- "mcp-remote",
131
- "http://localhost:7860/gradio_api/mcp/sse"
132
- ]
133
- }
134
- }
135
- }
136
- ```
137
-
138
- *Note: Requires `A1D_API_KEY` environment variable to be set.*
139
 
140
  ### For Other MCP Clients
141
  - **Server URL:** `http://localhost:7860/gradio_api/mcp/sse`
@@ -169,7 +153,9 @@ To deploy this on your own Hugging Face Space:
169
 
170
  ### πŸ”— MCP Client Configuration for Hosted Version
171
 
172
- **Method 1: With API Key in Headers (Recommended)**
 
 
173
 
174
  ```json
175
  {
@@ -190,21 +176,11 @@ To deploy this on your own Hugging Face Space:
190
  }
191
  ```
192
 
193
- **Method 2: Environment Variable (if Space has API key configured)**
194
 
195
- ```json
196
- {
197
- "mcpServers": {
198
- "a1d-hosted": {
199
- "command": "npx",
200
- "args": [
201
- "mcp-remote",
202
- "https://aigchacker-a1d-mcp-server.hf.space/gradio_api/mcp/sse"
203
- ]
204
- }
205
- }
206
- }
207
- ```
208
 
209
  ## πŸ’‘ How to Use
210
 
 
96
 
97
  ## πŸ”§ MCP Client Configuration
98
 
99
+ ### ⚠️ API Key Required for Client Usage
100
 
101
+ When using with your own MCP client (Claude Desktop, Cursor, etc.), you **must** provide your API key:
102
 
103
  ```json
104
  {
 
119
  }
120
  ```
121
 
122
+ **πŸ”‘ API key is mandatory for client usage.** Get your API key at [A1D.ai](https://a1d.ai/home/api).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
 
124
  ### For Other MCP Clients
125
  - **Server URL:** `http://localhost:7860/gradio_api/mcp/sse`
 
153
 
154
  ### πŸ”— MCP Client Configuration for Hosted Version
155
 
156
+ #### ⚠️ API Key Required for Client Usage
157
+
158
+ When using with your own MCP client (Claude Desktop, Cursor, etc.), you **must** provide your API key:
159
 
160
  ```json
161
  {
 
176
  }
177
  ```
178
 
179
+ **πŸ”‘ API key is mandatory for client usage.** Get your API key at [A1D.ai](https://a1d.ai/home/api).
180
 
181
+ #### 🌐 Using the Hosted Demo on Hugging Face Space
182
+
183
+ The hosted demo at [https://huggingface.co/spaces/aigchacker/a1d-mcp-server](https://huggingface.co/spaces/aigchacker/a1d-mcp-server) uses our provided API key for demonstration purposes only, with limited usage. For production use, please obtain your own API key.
 
 
 
 
 
 
 
 
 
 
184
 
185
  ## πŸ’‘ How to Use
186
 
app.py CHANGED
@@ -25,14 +25,21 @@ def get_api_client():
25
  except:
26
  pass
27
 
28
- # 2. Fallback to environment variable
29
- if not api_key:
30
- api_key = os.getenv("A1D_API_KEY")
 
 
 
 
 
31
 
 
32
  if not api_key:
33
  raise ValueError(
34
- "API key is required. Set A1D_API_KEY environment variable or provide API_KEY in request headers.")
35
 
 
36
  return A1DAPIClient(api_key)
37
 
38
 
@@ -296,13 +303,14 @@ def create_mcp_docs():
296
  return gr.Markdown("""
297
  ## πŸ”§ MCP Client Configuration
298
 
299
- ### For Claude Desktop:
300
- Add this configuration to your Claude Desktop settings:
 
301
 
302
  ```json
303
  {
304
  "mcpServers": {
305
- "a1d-hosted": {
306
  "command": "npx",
307
  "args": [
308
  "mcp-remote@latest",
@@ -318,6 +326,16 @@ Add this configuration to your Claude Desktop settings:
318
  }
319
  ```
320
 
 
 
 
 
 
 
 
 
 
 
321
  ### πŸ”‘ How to Get Your A1D API Key:
322
 
323
  1. **Visit A1D Website**: Go to [https://a1d.ai](https://a1d.ai)
@@ -327,6 +345,8 @@ Add this configuration to your Claude Desktop settings:
327
  5. **Copy Your Key**: Copy the generated API key
328
  6. **Replace in Config**: Replace `your_a1d_api_key_here` with your actual API key
329
 
 
 
330
  ### πŸ“‹ Available MCP Tools:
331
  - `remove_bg_wrapper` - Remove background from images
332
  - `image_upscaler_wrapper` - Upscale images (2x/4x/8x/16x)
@@ -338,6 +358,10 @@ Add this configuration to your Claude Desktop settings:
338
  ### 🌐 MCP Endpoints:
339
  - **SSE Endpoint**: `https://aigchacker-a1d-mcp-server.hf.space/gradio_api/mcp/sse`
340
  - **Schema**: `https://aigchacker-a1d-mcp-server.hf.space/gradio_api/mcp/schema`
 
 
 
 
341
  """)
342
 
343
  # Create Gradio interfaces for each tool
@@ -535,22 +559,39 @@ def create_gradio_app():
535
 
536
 
537
  if __name__ == "__main__":
538
- # Check for API key
539
- if not os.getenv("A1D_API_KEY"):
540
- print("❌ Error: A1D_API_KEY environment variable is required")
541
- print("Please set your API key: export A1D_API_KEY=your_api_key_here")
542
- exit(1)
543
-
544
  print("πŸš€ Starting A1D MCP Server...")
545
- print("=" * 50)
546
- print("βœ… API key found")
547
- print(f"🎯 Title: {GRADIO_CONFIG['title']}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
548
  print(
549
  f"🌐 Server: http://{GRADIO_CONFIG['server_name']}:{GRADIO_CONFIG['server_port']}")
550
  print(
551
  f"πŸ”§ MCP Endpoint: http://{GRADIO_CONFIG['server_name']}:{GRADIO_CONFIG['server_port']}/gradio_api/mcp/sse")
552
  print(f"πŸ“‹ Available Tools: {len(TOOLS_CONFIG)} AI tools")
553
- print("=" * 50)
 
 
 
 
 
554
 
555
  # Create and launch the app
556
  demo = create_gradio_app()
 
25
  except:
26
  pass
27
 
28
+ # 2. Check if running on Hugging Face Space
29
+ is_space = os.getenv("SPACE_ID") is not None
30
+ space_api_key = os.getenv("A1D_API_KEY")
31
+
32
+ # 3. Use environment variable only if on Hugging Face Space
33
+ if not api_key and is_space and space_api_key:
34
+ print("πŸ“‘ Using API key from Space environment variable")
35
+ return A1DAPIClient(space_api_key)
36
 
37
+ # 4. If not on Space or no API key provided, require user API key
38
  if not api_key:
39
  raise ValueError(
40
+ "API key is required. Please provide API_KEY in request headers. Get your API key at https://a1d.ai")
41
 
42
+ print("πŸ”‘ Using API key from MCP client headers")
43
  return A1DAPIClient(api_key)
44
 
45
 
 
303
  return gr.Markdown("""
304
  ## πŸ”§ MCP Client Configuration
305
 
306
+ ### ⚠️ API Key Required for Client Usage
307
+
308
+ When using with your own MCP client (Claude Desktop, Cursor, etc.), you **must** provide your API key:
309
 
310
  ```json
311
  {
312
  "mcpServers": {
313
+ "a1d": {
314
  "command": "npx",
315
  "args": [
316
  "mcp-remote@latest",
 
326
  }
327
  ```
328
 
329
+ **πŸ”‘ API key is mandatory for client usage.** Get your API key at [A1D.ai](https://a1d.ai/home/api).
330
+
331
+ ---
332
+
333
+ ### 🌐 Using the Hosted Demo on Hugging Face Space
334
+
335
+ The hosted demo at [https://huggingface.co/spaces/aigchacker/a1d-mcp-server](https://huggingface.co/spaces/aigchacker/a1d-mcp-server) uses our provided API key for demonstration purposes only, with limited usage. For production use, please obtain your own API key.
336
+
337
+ ---
338
+
339
  ### πŸ”‘ How to Get Your A1D API Key:
340
 
341
  1. **Visit A1D Website**: Go to [https://a1d.ai](https://a1d.ai)
 
345
  5. **Copy Your Key**: Copy the generated API key
346
  6. **Replace in Config**: Replace `your_a1d_api_key_here` with your actual API key
347
 
348
+ ---
349
+
350
  ### πŸ“‹ Available MCP Tools:
351
  - `remove_bg_wrapper` - Remove background from images
352
  - `image_upscaler_wrapper` - Upscale images (2x/4x/8x/16x)
 
358
  ### 🌐 MCP Endpoints:
359
  - **SSE Endpoint**: `https://aigchacker-a1d-mcp-server.hf.space/gradio_api/mcp/sse`
360
  - **Schema**: `https://aigchacker-a1d-mcp-server.hf.space/gradio_api/mcp/schema`
361
+
362
+ ### πŸ’‘ Usage Summary:
363
+ - **Hosted Demo**: Works directly in browser with provided API key
364
+ - **MCP Client**: Requires your own API key for production use
365
  """)
366
 
367
  # Create Gradio interfaces for each tool
 
559
 
560
 
561
  if __name__ == "__main__":
 
 
 
 
 
 
562
  print("πŸš€ Starting A1D MCP Server...")
563
+ print("=" * 70)
564
+
565
+ # Check environment and show configuration
566
+ is_space = os.getenv("SPACE_ID") is not None
567
+ space_api_key = os.getenv("A1D_API_KEY")
568
+
569
+ if is_space and space_api_key:
570
+ print("βœ… Running on Hugging Face Space with demo API key")
571
+ print("🌐 Web demo: Works directly with provided API key")
572
+ print("πŸ”‘ MCP clients: Must provide their own API key")
573
+ elif is_space:
574
+ print("⚠️ Running on Hugging Face Space without API key")
575
+ print("πŸ”‘ All users must provide their own API key")
576
+ else:
577
+ print("πŸ–₯️ Running locally")
578
+ if space_api_key:
579
+ print("βœ… Local API key found")
580
+ else:
581
+ print("πŸ”‘ Users must provide their own API key")
582
+
583
+ print(f"\n🎯 Title: {GRADIO_CONFIG['title']}")
584
  print(
585
  f"🌐 Server: http://{GRADIO_CONFIG['server_name']}:{GRADIO_CONFIG['server_port']}")
586
  print(
587
  f"πŸ”§ MCP Endpoint: http://{GRADIO_CONFIG['server_name']}:{GRADIO_CONFIG['server_port']}/gradio_api/mcp/sse")
588
  print(f"πŸ“‹ Available Tools: {len(TOOLS_CONFIG)} AI tools")
589
+
590
+ print(f"\nπŸ“– Authentication Strategy:")
591
+ print(" - Space demo: Uses provided API key (limited usage)")
592
+ print(" - MCP clients: Must provide own API key (production usage)")
593
+ print(" - Get API key at: https://a1d.ai")
594
+ print("=" * 70)
595
 
596
  # Create and launch the app
597
  demo = create_gradio_app()