karthiks
commited on
Commit
·
651d216
1
Parent(s):
89d4183
Simple app v1
Browse files- README.md +43 -1
- app.py +28 -0
- requirements.txt +1 -0
README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
---
|
| 2 |
-
title: Gradio
|
| 3 |
emoji: 😻
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: red
|
|
@@ -11,3 +11,45 @@ short_description: Building an MCP Server with Gradio
|
|
| 11 |
---
|
| 12 |
|
| 13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Gradio MCP Demo
|
| 3 |
emoji: 😻
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: red
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
| 14 |
+
|
| 15 |
+
# Gradio MCP Demo
|
| 16 |
+
|
| 17 |
+
Notice that we have:
|
| 18 |
+
|
| 19 |
+
1. included a detailed docstring for our function, and
|
| 20 |
+
2. set mcp_server=True in .launch(). This is all that's needed for your Gradio app to serve as an MCP server!
|
| 21 |
+
|
| 22 |
+
Now, when you run this app, it will:
|
| 23 |
+
|
| 24 |
+
1. Start the regular Gradio web interface
|
| 25 |
+
2. Start the MCP server
|
| 26 |
+
3. Print the MCP server URL in the console
|
| 27 |
+
|
| 28 |
+
The MCP server will be accessible at: `http://your-server:port/gradio_api/mcp/sse`
|
| 29 |
+
|
| 30 |
+
Gradio automatically converts the `letter_counter` function into an MCP tool that can be used by LLMs. The docstring of the function and the type hints of arguments will be used to generate the description of the tool and its parameters. The name of the function will be used as the name of your tool.
|
| 31 |
+
|
| 32 |
+
Now, all you need to do is add this URL endpoint to your MCP Client (e.g. Claude Desktop, Cursor, or Cline), which typically means pasting this config in the settings:
|
| 33 |
+
|
| 34 |
+
```json
|
| 35 |
+
{
|
| 36 |
+
"mcpServers": {
|
| 37 |
+
"gradio": {
|
| 38 |
+
"url": "http://your-server:port/gradio_api/mcp/sse"
|
| 39 |
+
}
|
| 40 |
+
}
|
| 41 |
+
}
|
| 42 |
+
```
|
| 43 |
+
|
| 44 |
+
By the way, you can find the exact config to copy-paste by going to the "View API" link in the footer of your Gradio app, and then clicking on "MCP"
|
| 45 |
+

|
| 46 |
+
|
| 47 |
+
## References
|
| 48 |
+
|
| 49 |
+
- [Building an MCP Server with Gradio](https://www.gradio.app/guides/building-mcp-server-with-gradio)
|
| 50 |
+
- MCP Tools for Gradio UI
|
| 51 |
+
- [MCP only Tools](https://www.gradio.app/guides/building-mcp-server-with-gradio#adding-mcp-only-tools) that aren't UI evented.
|
| 52 |
+
- [Gradio with FastMCP](https://www.gradio.app/guides/building-mcp-server-with-gradio#gradio-with-fast-mcp)
|
| 53 |
+
- The MCP protocol is still in its infancy and you might see issues connecting to an MCP Server that you've built. We generally recommend using the [MCP Inspector Tool](https://github.com/modelcontextprotocol/inspector) to try connecting and debugging your MCP Server.
|
| 54 |
+
- [Spaces As MCP Servers](https://huggingface.co/docs/hub/spaces-mcp-servers)
|
| 55 |
+
- [Docker Spaces](https://huggingface.co/docs/hub/spaces-sdks-docker)
|
app.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
def letter_counter(word, letter):
|
| 4 |
+
"""
|
| 5 |
+
Count the number of occurrences of a letter in a word or text.
|
| 6 |
+
|
| 7 |
+
Args:
|
| 8 |
+
word (str): The input text to search through
|
| 9 |
+
letter (str): The letter to search for
|
| 10 |
+
|
| 11 |
+
Returns:
|
| 12 |
+
str: A message indicating how many times the letter appears
|
| 13 |
+
"""
|
| 14 |
+
word = word.lower()
|
| 15 |
+
letter = letter.lower()
|
| 16 |
+
count = word.count(letter)
|
| 17 |
+
return count
|
| 18 |
+
|
| 19 |
+
demo = gr.Interface(
|
| 20 |
+
fn=letter_counter,
|
| 21 |
+
inputs=[gr.Textbox("strawberry"), gr.Textbox("r")],
|
| 22 |
+
outputs=[gr.Number()],
|
| 23 |
+
title="Letter Counter",
|
| 24 |
+
description="Enter text and a letter to count how many times the letter appears in the text."
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
if __name__ == "__main__":
|
| 28 |
+
demo.launch(mcp_server=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
gradio[mcp]=5.40.0
|