File size: 23,615 Bytes
aaa3e82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98727e9
37495c1
aaa3e82
 
 
 
 
98727e9
 
 
37495c1
 
98727e9
37495c1
98727e9
 
 
 
aaa3e82
afb386c
 
 
98727e9
 
afb386c
c1359fb
 
37495c1
 
 
 
 
 
 
98727e9
 
 
 
 
 
 
 
 
 
 
 
c1359fb
37495c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1359fb
afb386c
aaa3e82
37495c1
aaa3e82
98727e9
37495c1
c1359fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98727e9
 
aaa3e82
afb386c
aaa3e82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f4114e2
 
 
 
 
 
afb386c
 
 
f4114e2
 
 
 
afb386c
f4114e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afb386c
 
 
 
 
 
 
 
 
 
f4114e2
 
 
 
 
 
 
 
 
afb386c
 
f4114e2
 
 
 
 
 
 
 
 
 
 
afb386c
 
 
 
f4114e2
 
aaa3e82
f4114e2
 
aaa3e82
 
 
f4114e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aaa3e82
f4114e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aaa3e82
f4114e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aaa3e82
f4114e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aaa3e82
f4114e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aaa3e82
f4114e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aaa3e82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0466140
afb386c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0466140
 
 
 
 
afb386c
 
 
 
 
 
0466140
aaa3e82
 
 
0466140
aaa3e82
 
 
0466140
 
aaa3e82
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
"""
A1D MCP Server - Gradio Application
Universal AI Tools for image and video processing
"""

import gradio as gr
import os
from typing import Optional, Tuple, Union
from utils import A1DAPIClient, validate_url, validate_scale, prepare_request_data, format_response_with_preview
from config import GRADIO_CONFIG, TOOLS_CONFIG
from mcp_handler import get_api_key_from_headers


# Initialize API client
def get_api_client():
    """Get API client with current API key"""
    # Try to get API key from multiple sources
    api_key = None
    user_agent = ""
    request_path = ""

    # 1. Try from request headers (for MCP clients)
    try:
        request = gr.request()
        if request and hasattr(request, 'headers'):
            headers = dict(request.headers)
            api_key = get_api_key_from_headers(headers)
            user_agent = headers.get('user-agent', '')
            request_path = getattr(request, 'url', {}).path if hasattr(
                request, 'url') else ""
            print(f"πŸ” Request headers found - User-Agent: {user_agent}")
            print(f"πŸ” Request path: {request_path}")
            print(
                f"πŸ” API key from headers: {'Found' if api_key else 'Not found'}")
    except Exception as e:
        print(f"πŸ” No request context available: {e}")

    # 2. Check if running on Hugging Face Space
    is_space = os.getenv("SPACE_ID") is not None
    space_api_key = os.getenv("A1D_API_KEY")
    print(
        f"πŸ” Environment check - Is Space: {is_space}, Space API key: {'Found' if space_api_key else 'Not found'}")

    # 3. Determine if this is a web browser request or MCP client request
    is_web_request = False
    is_mcp_request = False

    # Check if this is an MCP request
    if request_path and ('/mcp/' in request_path or '/gradio_api/mcp' in request_path):
        is_mcp_request = True
        print("πŸ” Detected MCP API request")

    if user_agent:
        user_agent_lower = user_agent.lower()
        # Web browsers typically have 'mozilla' in user agent
        is_web_request = ('mozilla' in user_agent_lower or
                          'chrome' in user_agent_lower or
                          'safari' in user_agent_lower or
                          'edge' in user_agent_lower)
        print(f"πŸ” Request type detection - Is web request: {is_web_request}")
    else:
        # If no user agent, assume it's NOT a web request (likely MCP client)
        is_web_request = False
        print("πŸ” No User-Agent found - assuming MCP client request")

    # 4. STRICT RULE: MCP requests MUST have API key
    if is_mcp_request and not api_key:
        error_msg = (
            "πŸ”‘ API key is REQUIRED for MCP requests!\n\n"
            "This is an MCP API endpoint. You must provide your API key.\n"
            "Get your API key at https://a1d.ai\n\n"
            "Configuration example:\n"
            '{\n'
            '  "mcpServers": {\n'
            '    "a1d": {\n'
            '      "command": "npx",\n'
            '      "args": [\n'
            '        "mcp-remote@latest",\n'
            '        "https://aigchacker-a1d-mcp-server.hf.space/gradio_api/mcp/sse",\n'
            '        "--header",\n'
            '        "API_KEY:${MCP_API_KEY}"\n'
            '      ],\n'
            '      "env": {\n'
            '        "MCP_API_KEY": "your_a1d_api_key_here"\n'
            '      }\n'
            '    }\n'
            '  }\n'
            '}'
        )
        print(f"❌ MCP API key validation failed: {error_msg}")
        raise ValueError(error_msg)

    # 5. Use Space API key ONLY for web browser requests on Hugging Face Space
    if not api_key and is_space and space_api_key and is_web_request and not is_mcp_request:
        print("πŸ“‘ Using API key from Space environment variable (web demo)")
        return A1DAPIClient(space_api_key)

    # 6. For all other cases, user API key is mandatory
    if not api_key:
        error_msg = (
            "πŸ”‘ API key is required!\n\n"
            "Please provide API_KEY in request headers.\n"
            "Get your API key at https://a1d.ai\n\n"
            "Configuration example:\n"
            '{\n'
            '  "mcpServers": {\n'
            '    "a1d": {\n'
            '      "command": "npx",\n'
            '      "args": [\n'
            '        "mcp-remote@latest",\n'
            '        "https://aigchacker-a1d-mcp-server.hf.space/gradio_api/mcp/sse",\n'
            '        "--header",\n'
            '        "API_KEY:${MCP_API_KEY}"\n'
            '      ],\n'
            '      "env": {\n'
            '        "MCP_API_KEY": "your_a1d_api_key_here"\n'
            '      }\n'
            '    }\n'
            '  }\n'
            '}'
        )
        print(f"❌ API key validation failed: {error_msg}")
        raise ValueError(error_msg)

    print("πŸ”‘ Using API key from MCP client headers")
    return A1DAPIClient(api_key)


def remove_bg(image_url: str) -> Tuple[str, Optional[str]]:
    """Remove background from images using AI.

    Args:
        image_url: The URL of the image to remove background from

    Returns:
        Tuple of (result_message, media_url_for_preview)
    """
    try:
        if not validate_url(image_url):
            return "❌ Error: Invalid image URL format", None

        client = get_api_client()
        data = prepare_request_data("remove_bg", image_url=image_url)

        # Use the new method that waits for result
        response = client.make_request_with_result(
            TOOLS_CONFIG["remove_bg"]["api_endpoint"],
            data,
            timeout=120  # 2 minutes timeout
        )

        return format_response_with_preview(response, "remove_bg")

    except Exception as e:
        return f"❌ Error: {str(e)}", None


def image_upscaler(image_url: str, scale: int = 2) -> Tuple[str, Optional[str]]:
    """Upscale images using AI with specified scale factor.

    Args:
        image_url: The URL of the image to upscale
        scale: Scale factor for upscaling (2, 4, 8, or 16). Default: 2

    Returns:
        Tuple of (result_message, media_url_for_preview)
    """
    try:
        if not validate_url(image_url):
            return "❌ Error: Invalid image URL format", None

        if not validate_scale(scale):
            return "❌ Error: Scale must be 2, 4, 8, or 16", None

        client = get_api_client()
        data = prepare_request_data(
            "image_upscaler", image_url=image_url, scale=scale)

        response = client.make_request_with_result(
            TOOLS_CONFIG["image_upscaler"]["api_endpoint"],
            data,
            timeout=120
        )

        return format_response_with_preview(response, "image_upscaler")

    except Exception as e:
        return f"❌ Error: {str(e)}", None


def video_upscaler(video_url: str) -> Tuple[str, Optional[str]]:
    """Upscale videos using AI.

    Args:
        video_url: The URL of the video to upscale

    Returns:
        Tuple of (result_message, media_url_for_preview)
    """
    try:
        if not validate_url(video_url):
            return "❌ Error: Invalid video URL format", None

        client = get_api_client()
        data = prepare_request_data("video_upscaler", video_url=video_url)

        response = client.make_request_with_result(
            TOOLS_CONFIG["video_upscaler"]["api_endpoint"],
            data,
            timeout=300  # 5 minutes for video processing
        )

        return format_response_with_preview(response, "video_upscaler")

    except Exception as e:
        return f"❌ Error: {str(e)}", None


def image_vectorization(image_url: str) -> Tuple[str, Optional[str]]:
    """Convert images to vector format using AI.

    Args:
        image_url: The URL of the image to vectorize

    Returns:
        Tuple of (result_message, media_url_for_preview)
    """
    try:
        if not validate_url(image_url):
            return "❌ Error: Invalid image URL format", None

        client = get_api_client()
        data = prepare_request_data("image_vectorization", image_url=image_url)

        response = client.make_request_with_result(
            TOOLS_CONFIG["image_vectorization"]["api_endpoint"],
            data,
            timeout=120
        )

        return format_response_with_preview(response, "image_vectorization")

    except Exception as e:
        return f"❌ Error: {str(e)}", None


def image_extends(image_url: str) -> Tuple[str, Optional[str]]:
    """Extend images using AI.

    Args:
        image_url: The URL of the image to extend

    Returns:
        Tuple of (result_message, media_url_for_preview)
    """
    try:
        if not validate_url(image_url):
            return "❌ Error: Invalid image URL format", None

        client = get_api_client()
        data = prepare_request_data("image_extends", image_url=image_url)

        response = client.make_request_with_result(
            TOOLS_CONFIG["image_extends"]["api_endpoint"],
            data,
            timeout=120
        )

        return format_response_with_preview(response, "image_extends")

    except Exception as e:
        return f"❌ Error: {str(e)}", None


def image_generator(prompt: str) -> Tuple[str, Optional[str]]:
    """Generate images using AI from text prompts.

    Args:
        prompt: Text prompt to generate image from

    Returns:
        Tuple of (result_message, media_url_for_preview)
    """
    try:
        if not prompt or not prompt.strip():
            return "❌ Error: Prompt is required and cannot be empty", None

        client = get_api_client()
        data = prepare_request_data("image_generator", prompt=prompt.strip())

        response = client.make_request_with_result(
            TOOLS_CONFIG["image_generator"]["api_endpoint"],
            data,
            timeout=120
        )

        return format_response_with_preview(response, "image_generator")

    except Exception as e:
        return f"❌ Error: {str(e)}", None


# Wrapper functions for Gradio interface
def remove_bg_wrapper(image_url: str):
    """Wrapper for remove_bg that returns message and media for Gradio

    Args:
        image_url: The URL of the image to remove background from. Must be a valid HTTP/HTTPS URL pointing to an image file.

    Returns:
        Tuple of (result_message, media_url_for_preview)
    """
    message, media_url = remove_bg(image_url)
    return message, media_url if media_url else None


def image_upscaler_wrapper(image_url: str, scale: int):
    """Wrapper for image_upscaler that returns message and media for Gradio

    Args:
        image_url: The URL of the image to upscale. Must be a valid HTTP/HTTPS URL pointing to an image file.
        scale: Scale factor for upscaling. Choose from 2, 4, 8, or 16. Higher values produce larger images but take longer to process.

    Returns:
        Tuple of (result_message, media_url_for_preview)
    """
    message, media_url = image_upscaler(image_url, scale)
    return message, media_url if media_url else None


def video_upscaler_wrapper(video_url: str):
    """Wrapper for video_upscaler that returns message and media for Gradio

    Args:
        video_url: The URL of the video to upscale. Must be a valid HTTP/HTTPS URL pointing to a video file (MP4, AVI, MOV, etc.).

    Returns:
        Tuple of (result_message, media_url_for_preview)
    """
    message, media_url = video_upscaler(video_url)
    return message, media_url if media_url else None


def image_vectorization_wrapper(image_url: str):
    """Wrapper for image_vectorization that returns message and media for Gradio

    Args:
        image_url: The URL of the image to convert to vector format. Must be a valid HTTP/HTTPS URL pointing to an image file.

    Returns:
        Tuple of (result_message, media_url_for_preview)
    """
    message, media_url = image_vectorization(image_url)
    return message, media_url if media_url else None


def image_extends_wrapper(image_url: str):
    """Wrapper for image_extends that returns message and media for Gradio

    Args:
        image_url: The URL of the image to extend. Must be a valid HTTP/HTTPS URL pointing to an image file.

    Returns:
        Tuple of (result_message, media_url_for_preview)
    """
    message, media_url = image_extends(image_url)
    return message, media_url if media_url else None


def image_generator_wrapper(prompt: str):
    """Wrapper for image_generator that returns message and media for Gradio

    Args:
        prompt: Text description of the image to generate. Be descriptive and specific for better results. Example: "A beautiful sunset over mountains with vibrant orange and purple colors".

    Returns:
        Tuple of (result_message, media_url_for_preview)
    """
    message, media_url = image_generator(prompt)
    return message, media_url if media_url else None


# MCP Documentation Component
def create_mcp_docs():
    """Create MCP documentation component"""
    return gr.Markdown("""
## πŸ”§ MCP Client Configuration

### ⚠️ API Key Required for Client Usage

When using with your own MCP client (Claude Desktop, Cursor, etc.), you **must** provide your API key:

```json
{
  "mcpServers": {
    "a1d": {
      "command": "npx",
      "args": [
        "mcp-remote@latest",
        "https://aigchacker-a1d-mcp-server.hf.space/gradio_api/mcp/sse",
        "--header",
        "API_KEY:${MCP_API_KEY}"
      ],
      "env": {
        "MCP_API_KEY": "your_a1d_api_key_here"
      }
    }
  }
}
```

**πŸ”‘ API key is mandatory for client usage.** Get your API key at [A1D.ai](https://a1d.ai/home/api).

---

### 🌐 Using the Hosted Demo on Hugging Face Space

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.

---

### πŸ”‘ How to Get Your A1D API Key:

1. **Visit A1D Website**: Go to [https://a1d.ai](https://a1d.ai)
2. **Sign Up/Login**: Create an account or login to your existing account
3. **Access Dashboard**: Navigate to your user dashboard
4. **Generate API Key**: Look for "API Keys" or "Developer" section
5. **Copy Your Key**: Copy the generated API key
6. **Replace in Config**: Replace `your_a1d_api_key_here` with your actual API key

---

### πŸ“‹ Available MCP Tools:
- `remove_bg_wrapper` - Remove background from images
- `image_upscaler_wrapper` - Upscale images (2x/4x/8x/16x)
- `video_upscaler_wrapper` - Upscale videos
- `image_vectorization_wrapper` - Convert images to vector format
- `image_extends_wrapper` - Extend images using AI
- `image_generator_wrapper` - Generate images from text prompts

### 🌐 MCP Endpoints:
- **SSE Endpoint**: `https://aigchacker-a1d-mcp-server.hf.space/gradio_api/mcp/sse`
- **Schema**: `https://aigchacker-a1d-mcp-server.hf.space/gradio_api/mcp/schema`

### πŸ’‘ Usage Summary:
- **Hosted Demo**: Works directly in browser with provided API key
- **MCP Client**: Requires your own API key for production use
""")

# Create Gradio interfaces for each tool


def create_gradio_app():
    """Create the main Gradio application with all tools"""

    # Background Removal Interface with MCP docs
    with gr.Blocks(title="🎭 Background Removal") as remove_bg_interface:
        gr.Markdown("# 🎭 Background Removal")
        gr.Markdown("Remove background from images using AI")

        with gr.Row():
            with gr.Column():
                bg_input = gr.Textbox(
                    label="Image URL",
                    placeholder="https://example.com/image.jpg",
                    info="Enter the URL of the image to remove background from"
                )
                bg_button = gr.Button("Remove Background", variant="primary")
            with gr.Column():
                bg_result = gr.Textbox(label="Result")
                bg_preview = gr.Image(label="Preview")

        bg_button.click(
            fn=remove_bg_wrapper,
            inputs=[bg_input],
            outputs=[bg_result, bg_preview]
        )

        # Add MCP documentation
        create_mcp_docs()

    # Image Upscaler Interface with MCP docs
    with gr.Blocks(title="πŸ” Image Upscaler") as image_upscaler_interface:
        gr.Markdown("# πŸ” Image Upscaler")
        gr.Markdown("Upscale images using AI with specified scale factor")

        with gr.Row():
            with gr.Column():
                up_input = gr.Textbox(
                    label="Image URL",
                    placeholder="https://example.com/image.jpg",
                    info="Enter the URL of the image to upscale"
                )
                up_scale = gr.Dropdown(
                    choices=[2, 4, 8, 16],
                    value=2,
                    label="Scale Factor",
                    info="Choose the upscaling factor"
                )
                up_button = gr.Button("Upscale Image", variant="primary")
            with gr.Column():
                up_result = gr.Textbox(label="Result")
                up_preview = gr.Image(label="Preview")

        up_button.click(
            fn=image_upscaler_wrapper,
            inputs=[up_input, up_scale],
            outputs=[up_result, up_preview]
        )

        # Add MCP documentation
        create_mcp_docs()

    # Video Upscaler Interface with MCP docs
    with gr.Blocks(title="🎬 Video Upscaler") as video_upscaler_interface:
        gr.Markdown("# 🎬 Video Upscaler")
        gr.Markdown("Upscale videos using AI")

        with gr.Row():
            with gr.Column():
                vid_input = gr.Textbox(
                    label="Video URL",
                    placeholder="https://example.com/video.mp4",
                    info="Enter the URL of the video to upscale"
                )
                vid_button = gr.Button("Upscale Video", variant="primary")
            with gr.Column():
                vid_result = gr.Textbox(label="Result")
                vid_preview = gr.Video(label="Preview")

        vid_button.click(
            fn=video_upscaler_wrapper,
            inputs=[vid_input],
            outputs=[vid_result, vid_preview]
        )

        # Add MCP documentation
        create_mcp_docs()

    # Image Vectorization Interface with MCP docs
    with gr.Blocks(title="πŸ“ Image Vectorization") as image_vectorization_interface:
        gr.Markdown("# πŸ“ Image Vectorization")
        gr.Markdown("Convert images to vector format using AI")

        with gr.Row():
            with gr.Column():
                vec_input = gr.Textbox(
                    label="Image URL",
                    placeholder="https://example.com/image.jpg",
                    info="Enter the URL of the image to convert to vector format"
                )
                vec_button = gr.Button("Vectorize Image", variant="primary")
            with gr.Column():
                vec_result = gr.Textbox(label="Result")
                vec_preview = gr.Image(label="Preview")

        vec_button.click(
            fn=image_vectorization_wrapper,
            inputs=[vec_input],
            outputs=[vec_result, vec_preview]
        )

        # Add MCP documentation
        create_mcp_docs()

    # Image Extension Interface with MCP docs
    with gr.Blocks(title="πŸ–ΌοΈ Image Extension") as image_extends_interface:
        gr.Markdown("# πŸ–ΌοΈ Image Extension")
        gr.Markdown("Extend images using AI")

        with gr.Row():
            with gr.Column():
                ext_input = gr.Textbox(
                    label="Image URL",
                    placeholder="https://example.com/image.jpg",
                    info="Enter the URL of the image to extend"
                )
                ext_button = gr.Button("Extend Image", variant="primary")
            with gr.Column():
                ext_result = gr.Textbox(label="Result")
                ext_preview = gr.Image(label="Preview")

        ext_button.click(
            fn=image_extends_wrapper,
            inputs=[ext_input],
            outputs=[ext_result, ext_preview]
        )

        # Add MCP documentation
        create_mcp_docs()

    # Image Generator Interface with MCP docs
    with gr.Blocks(title="🎨 Image Generator") as image_generator_interface:
        gr.Markdown("# 🎨 Image Generator")
        gr.Markdown("Generate images using AI from text prompts")

        with gr.Row():
            with gr.Column():
                gen_input = gr.Textbox(
                    label="Text Prompt",
                    placeholder="A beautiful sunset over mountains",
                    info="Enter a text description to generate an image",
                    lines=3
                )
                gen_button = gr.Button("Generate Image", variant="primary")
            with gr.Column():
                gen_result = gr.Textbox(label="Result")
                gen_preview = gr.Image(label="Preview")

        gen_button.click(
            fn=image_generator_wrapper,
            inputs=[gen_input],
            outputs=[gen_result, gen_preview]
        )

        # Add MCP documentation
        create_mcp_docs()

    # Create tabbed interface
    demo = gr.TabbedInterface(
        [
            remove_bg_interface,
            image_upscaler_interface,
            video_upscaler_interface,
            image_vectorization_interface,
            image_extends_interface,
            image_generator_interface
        ],
        [
            "Background Removal",
            "Image Upscaler",
            "Video Upscaler",
            "Image Vectorization",
            "Image Extension",
            "Image Generator"
        ],
        title=GRADIO_CONFIG["title"],
        theme=GRADIO_CONFIG["theme"]
    )

    return demo


if __name__ == "__main__":
    print("πŸš€ Starting A1D MCP Server...")
    print("=" * 70)

    # Check environment and show configuration
    is_space = os.getenv("SPACE_ID") is not None
    space_api_key = os.getenv("A1D_API_KEY")

    if is_space and space_api_key:
        print("βœ… Running on Hugging Face Space with demo API key")
        print("🌐 Web demo: Works directly with provided API key")
        print("πŸ”‘ MCP clients: Must provide their own API key")
    elif is_space:
        print("⚠️  Running on Hugging Face Space without API key")
        print("πŸ”‘ All users must provide their own API key")
    else:
        print("πŸ–₯️  Running locally")
        if space_api_key:
            print("βœ… Local API key found")
        else:
            print("πŸ”‘ Users must provide their own API key")

    print(f"\n🎯 Title: {GRADIO_CONFIG['title']}")
    print(
        f"🌐 Server: http://{GRADIO_CONFIG['server_name']}:{GRADIO_CONFIG['server_port']}")
    print(
        f"πŸ”§ MCP Endpoint: http://{GRADIO_CONFIG['server_name']}:{GRADIO_CONFIG['server_port']}/gradio_api/mcp/sse")
    print(f"πŸ“‹ Available Tools: {len(TOOLS_CONFIG)} AI tools")

    print(f"\nπŸ“– Authentication Strategy:")
    print("   - Space demo: Uses provided API key (limited usage)")
    print("   - MCP clients: Must provide own API key (production usage)")
    print("   - Get API key at: https://a1d.ai")
    print("=" * 70)

    # Create and launch the app
    demo = create_gradio_app()

    # Launch the Gradio app with MCP server enabled
    demo.launch(
        server_name=GRADIO_CONFIG["server_name"],
        server_port=GRADIO_CONFIG["server_port"],
        share=GRADIO_CONFIG["share"],
        mcp_server=True  # Enable MCP server functionality
    )