Spaces:
Sleeping
Sleeping
Implement Gradio app for MCP tool listing and calling; add usage guide and JSON payload example
Browse files- .github/copilot-instructions.md +3 -0
- .gitignore +50 -0
- app.py +52 -3
- gradio-api.md +72 -0
- payload.json +7 -0
- utputFormat +7 -0
.github/copilot-instructions.md
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
- All terminal commands should be in Windows PowerShell syntax.
|
| 2 |
+
- Always activate the virtual environment before running any Python scripts with `.\.venv\Scripts\Activate.ps1;`
|
| 3 |
+
- When reading files, always read the first 1000 lines of the file.
|
.gitignore
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Python .gitignore
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.py[cod]
|
| 4 |
+
*.pyo
|
| 5 |
+
*.pyd
|
| 6 |
+
*.pdb
|
| 7 |
+
*.egg
|
| 8 |
+
*.egg-info/
|
| 9 |
+
dist/
|
| 10 |
+
build/
|
| 11 |
+
.eggs/
|
| 12 |
+
.env
|
| 13 |
+
.venv
|
| 14 |
+
env/
|
| 15 |
+
venv/
|
| 16 |
+
ENV/
|
| 17 |
+
*.env
|
| 18 |
+
*.venv
|
| 19 |
+
*.log
|
| 20 |
+
*.sqlite3
|
| 21 |
+
.DS_Store
|
| 22 |
+
.idea/
|
| 23 |
+
.vscode/
|
| 24 |
+
*.db
|
| 25 |
+
*.bak
|
| 26 |
+
*.swp
|
| 27 |
+
*.swo
|
| 28 |
+
*.~
|
| 29 |
+
*.tmp
|
| 30 |
+
coverage/
|
| 31 |
+
htmlcov/
|
| 32 |
+
.tox/
|
| 33 |
+
.nox/
|
| 34 |
+
.pytest_cache/
|
| 35 |
+
.mypy_cache/
|
| 36 |
+
.pyre/
|
| 37 |
+
.pytype/
|
| 38 |
+
.cache/
|
| 39 |
+
|
| 40 |
+
# Jupyter Notebook
|
| 41 |
+
.ipynb_checkpoints
|
| 42 |
+
|
| 43 |
+
# VS Code settings
|
| 44 |
+
.vscode/
|
| 45 |
+
|
| 46 |
+
# macOS
|
| 47 |
+
.DS_Store
|
| 48 |
+
|
| 49 |
+
# Windows
|
| 50 |
+
Thumbs.db
|
app.py
CHANGED
|
@@ -1,7 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
def
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
-
demo = gr.Interface(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
demo.launch()
|
|
|
|
| 1 |
+
|
| 2 |
+
# Gradio app to list available MCP tools from a server URL
|
| 3 |
+
# Use githubRepo tool to fetch documentation on mcp from https://github.com/modelcontextprotocol/python-sdk
|
| 4 |
+
# Docs for using curl to access gradio app: https://github.com/gradio-app/gradio/blob/main/guides/09_gradio-clients-and-lite/03_querying-gradio-apps-with-curl.md
|
| 5 |
+
|
| 6 |
import gradio as gr
|
| 7 |
+
import asyncio
|
| 8 |
+
import traceback
|
| 9 |
+
import json
|
| 10 |
+
from mcp import ClientSession
|
| 11 |
+
from mcp.client.streamable_http import streamablehttp_client
|
| 12 |
+
|
| 13 |
+
DEFAULT_MCP_URL = "https://mcp.composio.dev/composio/server/ca1477ae-dffb-404c-9021-ef25ff440221/mcp"
|
| 14 |
+
DEFAULT_TOOL_NAME = "SALESFORCE_RETRIEVE_LEAD_BY_ID"
|
| 15 |
+
DEFAULT_JSON_ARGS = '{"id": "00QgK0000005yjVUAQ"}'
|
| 16 |
+
|
| 17 |
+
async def call_tool_from_server(url, tool_name, json_args):
|
| 18 |
+
try:
|
| 19 |
+
async with streamablehttp_client(url) as (read, write, _):
|
| 20 |
+
async with ClientSession(read, write) as session:
|
| 21 |
+
await session.initialize()
|
| 22 |
+
if not tool_name:
|
| 23 |
+
# List tools if no tool name provided
|
| 24 |
+
tools = await session.list_tools()
|
| 25 |
+
return {"available_tools": [tool.name for tool in tools.tools]}
|
| 26 |
+
# Parse JSON args
|
| 27 |
+
try:
|
| 28 |
+
args = json.loads(json_args) if json_args else {}
|
| 29 |
+
except Exception as e:
|
| 30 |
+
return {"error": f"Invalid JSON arguments: {e}"}
|
| 31 |
+
# Call the tool
|
| 32 |
+
result = await session.call_tool(tool_name, args)
|
| 33 |
+
return result
|
| 34 |
+
except Exception as e:
|
| 35 |
+
tb = traceback.format_exc()
|
| 36 |
+
return {"error": str(e), "traceback": tb}
|
| 37 |
+
|
| 38 |
+
def call_tool(url, tool_name, json_args):
|
| 39 |
+
return asyncio.run(call_tool_from_server(url, tool_name, json_args))
|
| 40 |
|
| 41 |
+
def gradio_tool_lister_and_caller(url, tool_name, json_args):
|
| 42 |
+
"""Gradio interface function for tool lister/caller."""
|
| 43 |
+
return call_tool(url, tool_name, json_args)
|
| 44 |
|
| 45 |
+
demo = gr.Interface(
|
| 46 |
+
fn=gradio_tool_lister_and_caller,
|
| 47 |
+
inputs=[
|
| 48 |
+
gr.Textbox(label="MCP Server URL", value=DEFAULT_MCP_URL),
|
| 49 |
+
gr.Textbox(label="Tool Name", value=DEFAULT_TOOL_NAME),
|
| 50 |
+
gr.Textbox(label="Tool Arguments (JSON)", value=DEFAULT_JSON_ARGS, lines=4),
|
| 51 |
+
],
|
| 52 |
+
outputs=gr.JSON(label="Result"),
|
| 53 |
+
title="MCP Tool Lister and Caller",
|
| 54 |
+
description="Enter an MCP server URL, tool name, and JSON arguments to call a tool. Leave tool name blank to list available tools.",
|
| 55 |
+
)
|
| 56 |
demo.launch()
|
gradio-api.md
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Using Gradio API with curl.exe on Windows
|
| 2 |
+
|
| 3 |
+
This guide explains how to interact with any Gradio app as an API using `curl.exe` from Windows PowerShell. It covers both public Hugging Face Spaces and local Gradio apps.
|
| 4 |
+
|
| 5 |
+
## Prerequisites
|
| 6 |
+
- Windows with PowerShell
|
| 7 |
+
- `curl.exe` (included in Windows 10+)
|
| 8 |
+
- The URL of your Gradio app (public or local, e.g., http://127.0.0.1:7860/)
|
| 9 |
+
|
| 10 |
+
## Step 1: Prepare Your Input
|
| 11 |
+
Create a file named `payload.json` in your working directory. For a local MCP Gradio app, the payload might look like:
|
| 12 |
+
|
| 13 |
+
```json
|
| 14 |
+
{
|
| 15 |
+
"data": [
|
| 16 |
+
"https://mcp.composio.dev/composio/server/ca1477ae-dffb-404c-9021-ef25ff440221/mcp",
|
| 17 |
+
"SALESFORCE_RETRIEVE_LEAD_BY_ID",
|
| 18 |
+
"{\"id\": \"00QgK0000005yjVUAQ\"}"
|
| 19 |
+
]
|
| 20 |
+
}
|
| 21 |
+
```
|
| 22 |
+
|
| 23 |
+
## Step 2: Make the POST Request (Local Gradio)
|
| 24 |
+
Send your input to the local Gradio API endpoint using this command:
|
| 25 |
+
|
| 26 |
+
```powershell
|
| 27 |
+
curl.exe -X POST http://127.0.0.1:7860/gradio_api/call/predict -s -H "Content-Type: application/json" -d '@payload.json'
|
| 28 |
+
```
|
| 29 |
+
|
| 30 |
+
This returns a JSON response with an `event_id`, e.g.:
|
| 31 |
+
|
| 32 |
+
```json
|
| 33 |
+
{"event_id":"YOUR_EVENT_ID"}
|
| 34 |
+
```
|
| 35 |
+
|
| 36 |
+
## Step 3: Fetch the Result (Local Gradio)
|
| 37 |
+
Use the `event_id` from the previous step to fetch the result:
|
| 38 |
+
|
| 39 |
+
```powershell
|
| 40 |
+
curl.exe -N http://127.0.0.1:7860/gradio_api/call/predict/YOUR_EVENT_ID
|
| 41 |
+
```
|
| 42 |
+
|
| 43 |
+
You will see output like:
|
| 44 |
+
|
| 45 |
+
```
|
| 46 |
+
event: complete
|
| 47 |
+
data: [ ... ]
|
| 48 |
+
```
|
| 49 |
+
|
| 50 |
+
## Example for Public Gradio Apps
|
| 51 |
+
For public Spaces, use the `/call/predict` endpoint and adjust the payload as needed:
|
| 52 |
+
|
| 53 |
+
```powershell
|
| 54 |
+
curl.exe -X POST "https://abidlabs-en2fr.hf.space/call/predict" -H "Content-Type: application/json" -d '@payload.json'
|
| 55 |
+
```
|
| 56 |
+
|
| 57 |
+
Then fetch the result as above, replacing the URL and event ID.
|
| 58 |
+
|
| 59 |
+
## Notes
|
| 60 |
+
- For Gradio apps with multiple inputs, adjust the `data` array in `payload.json` accordingly.
|
| 61 |
+
- For private Spaces, add your Hugging Face token with `-H "Authorization: Bearer YOUR_TOKEN"` to both requests.
|
| 62 |
+
- For file inputs, use a dictionary with a `path` key pointing to a file URL.
|
| 63 |
+
- If authentication is required, first POST to `/login` and use the returned cookies in subsequent requests.
|
| 64 |
+
|
| 65 |
+
## Troubleshooting
|
| 66 |
+
- Always use single quotes around `@payload.json` in PowerShell to avoid variable expansion errors.
|
| 67 |
+
- If you get JSON errors, check your `payload.json` formatting.
|
| 68 |
+
- If you get a 404 or "Not Found" error, check the endpoint path (e.g., `/gradio_api/call/predict` for local apps).
|
| 69 |
+
|
| 70 |
+
---
|
| 71 |
+
|
| 72 |
+
This guide is based on the official Gradio documentation and adapted for both public and local Windows PowerShell use cases.
|
payload.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"data": [
|
| 3 |
+
"https://mcp.composio.dev/composio/server/ca1477ae-dffb-404c-9021-ef25ff440221/mcp",
|
| 4 |
+
"SALESFORCE_RETRIEVE_LEAD_BY_ID",
|
| 5 |
+
"{\"id\": \"00QgK0000005yjVUAQ\"}"
|
| 6 |
+
]
|
| 7 |
+
}
|
utputFormat
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
HTTP/1.1 422 Unprocessable Content
|
| 2 |
+
date: Sun, 10 Aug 2025 03:02:52 GMT
|
| 3 |
+
server: uvicorn
|
| 4 |
+
content-length: 158
|
| 5 |
+
content-type: application/json
|
| 6 |
+
|
| 7 |
+
{"detail":[{"type":"json_invalid","loc":["body",1],"msg":"JSON decode error","input":{},"ctx":{"error":"Expecting property name enclosed in double quotes"}}]}
|