Join the conversation

Join the community of Machine Learners and AI enthusiasts.

Sign Up
abidlabsΒ 
posted an update 3 days ago
Post
2238
Hi folks! Excited to share a new feature from the Gradio team along with a tutorial.

If you don't already know, Gradio is an open-source Python library used to build interfaces for machine learning models. Beyond just creating UIs, Gradio also exposes API capabilities and now, Gradio apps can be launched Model Context Protocol (MCP) servers for LLMs.

If you already know how to use Gradio, there are only two additional things you need to do:
* Add standard docstrings to your function (these will be used to generate the descriptions for your tools for the LLM)
* Set mcp_server=True in launch()


Here's a complete example (make sure you already have the latest version of Gradio installed):


import gradio as gr

def letter_counter(word, letter):
    """Count the occurrences of a specific letter in a word.
    
    Args:
        word: The word or phrase to analyze
        letter: The letter to count occurrences of
        
    Returns:
        The number of times the letter appears in the word
    """
    return word.lower().count(letter.lower())

demo = gr.Interface(
    fn=letter_counter,
    inputs=["text", "text"],
    outputs="number",
    title="Letter Counter",
    description="Count how many times a letter appears in a word"
)

demo.launch(mcp_server=True)



This is a very simple example, but you can add the ability to generate Ghibli images or speak emotions to any LLM that supports MCP. Once you have an MCP running locally, you can copy-paste the same app to host it on [Hugging Face Spaces](https://huggingface.co/spaces/) as well.

All free and open-source of course! Full tutorial: https://www.gradio.app/guides/building-mcp-server-with-gradio

Itss great stuff : to be able to make gradio be server ; i wuld lie to now more about accessing the gradio interface as a server also as i do use gradio a lot now days :

I wouild also have expected the understanding that UI are generally in MCP terms the CLIENT !!!!" connecting to the servers ... as gradio is a great host as a web app :

How do you utilise the gradio asa tool from a llm ?
How do you query the gradio UI for a Completion ? ( is this the same question ) = as a agent ? base url and api key ?

also for the mcp do you have to garnish the functions with @tool ? and why not ?
which functions will be the tools ?

Β·

Thanks @LeroyDyer , yes you can actually use the Gradio Chatbot as an MCP Client as well, although that is not the focus of this guide. Here's a guide that shows Gradio chatbot as an MCP Client and Gradio apps an MCP server:

https://www.gradio.app/guides/building-an-mcp-client-with-gradio