|
| 1 | +# Spaces as MCP servers |
| 2 | + |
| 3 | +You can **expose any public Space that has a visible `MCP` badge into a callable tool** that will be available in any MCP-compatible client, you can add as many Spaces as you want and without writing a single line of code. |
| 4 | + |
| 5 | +## Setup your MCP Client |
| 6 | + |
| 7 | +From your [Hub MCP settings](https://huggingface.co/settings/mcp), select your MCP client (VSCode, Cursor, Claude Code, ...) then follow the setup instructions. |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +> **Important:** You need a valid Hugging Face token with **READ** permissions to use MCP tools. If you don't have one, [create a new "Read" access token here](https://huggingface.co/settings/tokens/new?tokenType=read). |
| 12 | +
|
| 13 | +## Add an existing Space to your MCP tools |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +1. Browse compatible [Spaces](https://huggingface.co/spaces?filter=mcp-server) to find Spaces that are usable via MCP. You can also look for the grey **MCP** badge on any Spaces card. |
| 19 | +2. Click the badge and choose **Add to MCP tools** then confirm when asked. |
| 20 | +3. The Space should be listed in your MCP Server settings in the Spaces Tools section. |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +## Use Spaces from your MCP client |
| 25 | + |
| 26 | +If your MCP client is configured correctly, the Spaces you added will be available instantly without changing anything (if it doesn't restart your client and it should appear). Most MCP clients will list what tools are currently loaded so you can make sure the Space is available. |
| 27 | + |
| 28 | +> **Note:** For ZeroGPU Spaces, your quota will be used when the tool is called, if you run out of quota you can [subscribe to PRO](https://huggingface.co/subscribe/pro?from=ZeroGPU) to get 25 minutes of daily quota (x8 more quota than free users). For example your PRO account lets you generate up to 600 images per day using FLUX.1-schnell. |
| 29 | +
|
| 30 | +## Build your own MCP-compatible Gradio Space |
| 31 | + |
| 32 | +To create your own MCP-enabled Space, you need to [Create a new Gradio Space](https://huggingface.co/new-space?sdk=gradio) then make sure to enable MCP support in the code. Get started with [Gradio Spaces](https://huggingface.co/docs/hub/en/spaces-sdks-gradio) and make sure to check the [detailed MCP guide](https://www.gradio.app/guides/building-mcp-server-with-gradio) for more details. |
| 33 | + |
| 34 | + |
| 35 | +First, install Gradio with MCP support: |
| 36 | +```bash |
| 37 | +pip install "gradio[mcp]" |
| 38 | +``` |
| 39 | + |
| 40 | +Then create your app with clear type hints and docstrings: |
| 41 | + |
| 42 | +```python |
| 43 | +import gradio as gr |
| 44 | + |
| 45 | +def letter_counter(word: str, letter: str) -> int: |
| 46 | + """Count occurrences of a letter in a word. |
| 47 | + |
| 48 | + Args: |
| 49 | + word: The word to search in |
| 50 | + letter: The letter to count |
| 51 | + |
| 52 | + Returns: |
| 53 | + Number of times the letter appears in the word |
| 54 | + """ |
| 55 | + return word.lower().count(letter.lower()) |
| 56 | + |
| 57 | +demo = gr.Interface(fn=letter_counter, |
| 58 | + inputs=["text", "text"], |
| 59 | + outputs="number") |
| 60 | +demo.launch(mcp_server=True) # exposes an MCP schema automatically |
| 61 | +``` |
| 62 | + |
| 63 | +Push the app to a **Gradio Space** and it will automatically receive the **MCP** badge. Anyone can then add it as a tool with a single click. |
| 64 | + |
| 65 | +> **Note:** It's also quite easy to convert an existing Gradio Space to MCP server. Duplicate it from the context menu then just add the `mcp_server=True` parameter to your `launch()` method, and ensure your functions have clear type hints and docstrings - you can use AI tools to automate this quite easily ([example](https://huggingface.co/spaces/Lightricks/ltx-video-distilled/discussions/22/files) of AI generated docstrings). |
| 66 | +
|
| 67 | +## Be creative by mixing Spaces! |
| 68 | + |
| 69 | +As Hugging Face Spaces is the largest directory of AI apps, you can find many creative tools that can be used as MCP tools. Mixing and matching different Spaces can lead to powerful and creative workflows. |
| 70 | + |
| 71 | + |
| 72 | +<figure> |
| 73 | + <video controls> |
| 74 | + <source src="https://cdn-uploads.huggingface.co/production/uploads/5f17f0a0925b9863e28ad517/jmYCdqYh32iwCgxM9p0Ig.mp4" type="video/mp4"> |
| 75 | + </video> |
| 76 | + <figcaption> |
| 77 | + This video demonstrates the use of |
| 78 | + <a href="https://huggingface.co/spaces/Lightricks/ltx-video-distilled">Lightricks/ltx-video-distilled</a> and |
| 79 | + <a href="https://huggingface.co/spaces/ResembleAI/Chatterbox">ResembleAI/Chatterbox</a> in Claude Code to generate a video with audio. |
| 80 | + </figcaption> |
| 81 | +</figure> |
0 commit comments