Implementing MCP Servers in Python: An AI Shopping Assistant with Gradio
Gradio's MCP integration turns Python functions into LLM tools: launching an app with mcp_server=True exposes each endpoint as an MCP tool, with names, descriptions and input schemas generated from function docstrings, plus progress streaming and file upload handling. The walkthrough builds a virtual try-on shopping assistant by wrapping the IDM-VTON diffusion Space, called through the Gradio API client, in a Gradio Interface. That server is registered alongside the Playwright MCP server in VS Code's mcp.json, so the AI chat can browse a clothing store and render the user wearing selected garments.
Related posts
- How to Build an MCP Server with Gradio
Hugging Face mediumAdeptGradio apps become Model Context Protocol servers by passing mcp_server=True to launch(), which auto-converts each Python function into an MCP tool, using its docstring and type hints to build the description and input schema. The server is exposed over SSE at /gradio_api/mcp/sse. Claude Desktop, which lacks SSE support, can reach it through mcp-remote via npx. Newer features include decorators for MCP resources and prompts, MCP-only functions via gr.api(), automatic file and base64 conversion, per-tool latency and success metrics, and free hosting on Hugging Face Spaces with bearer-token auth for private Spaces.
- Tiny Agents in Python: a MCP-powered agent in ~70 lines of code
Hugging Face mediumAdeptHugging Face's huggingface_hub Python SDK now includes an MCP client and a minimal Agent class, letting an LLM pull tools from MCP servers (stdio, SSE or streamable HTTP) and call them during inference. Install it with `pip install "huggingface_hub[mcp]"`, then run agents from the `tiny-agents` CLI, configured by an agent.json that sets model, inference provider and MCP servers, plus an optional PROMPT.md. The MCPClient connects to servers, converts their tool schemas into OpenAI-style function definitions, and streams chat completions while rebuilding tool calls. It then routes each call to the right MCP session. The Agent is essentially a while loop on top of the client, with special exit-loop tools to end a turn.
- Five Big Improvements to Gradio MCP Servers
Hugging Face mediumAdeptGradio 5.38.0 adds five features for building MCP servers. A "File Upload" MCP server lets agents send local files to remote Gradio apps, and progress notifications now stream to MCP clients. `gr.load_openapi` turns an OpenAPI spec into an MCP server in one line, `gr.Header`-typed arguments extract auth headers and list them in the connection docs, and the `api_description` parameter overrides auto-generated tool descriptions. Short code examples cover the OpenAPI loader, header-based auth and custom descriptions.
- Wire It, Run It, Deploy It: AI Workflows in Gradio
Hugging Face mediumAdeptGradio's gr.Workflow turns AI pipelines into a graph of typed nodes (reference inputs, operator steps, subject outputs) served as a drag-and-drop canvas where every node is runnable and intermediate results are visible. Operators can be Python functions, Hugging Face Inference Providers models, other Gradio Spaces, or Hub dataset rows, and fan-out lets one input feed several operators in parallel. Each output automatically becomes a REST endpoint callable via gradio_client or curl, and fn nodes decorated with @spaces.GPU can run local models on ZeroGPU. Example Spaces include an image editor, a media studio chaining FLUX, background removal and TTS, and a dataset profiler.
- Tiny Agents: an MCP-powered agent in 50 lines of code
Hugging Face mediumAdeptAn LLM agent built on the Model Context Protocol reduces to a simple while loop around an MCP-aware inference client: connect to MCP servers, convert their tools into OpenAI-style function definitions, pass them to the chat-completion call, execute returned tool calls via the MCP SDK, and feed results back. The TypeScript implementation in huggingface.js extends InferenceClient into an McpClient that holds one session per server and maps tool names to sessions. An Agent class adds a system prompt plus two control-flow tools (task_complete, ask_question). The loop alternates tool calls and results until an exit tool is called, the model answers twice without tools, or a turn limit is reached. The default setup uses Qwen2.5-72B on Nebius, and the model, provider and MCP servers can be swapped.