File size: 2,454 Bytes
4958545
 
 
 
 
f61d443
4958545
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f61d443
4958545
 
 
f61d443
4958545
 
 
 
 
 
 
 
 
 
 
f61d443
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

# Gradio app to list available MCP tools from a server URL
# Use githubRepo tool to fetch documentation on mcp from https://github.com/modelcontextprotocol/python-sdk
# 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

import gradio as gr
import asyncio
import traceback
import json
from mcp import ClientSession
from mcp.client.streamable_http import streamablehttp_client

DEFAULT_MCP_URL = "https://mcp.composio.dev/composio/server/ca1477ae-dffb-404c-9021-ef25ff440221/mcp"
DEFAULT_TOOL_NAME = "SALESFORCE_RETRIEVE_LEAD_BY_ID"
DEFAULT_JSON_ARGS = '{"id": "00QgK0000005yjVUAQ"}'

async def call_tool_from_server(url, tool_name, json_args):
    try:
        async with streamablehttp_client(url) as (read, write, _):
            async with ClientSession(read, write) as session:
                await session.initialize()
                if not tool_name:
                    # List tools if no tool name provided
                    tools = await session.list_tools()
                    return {"available_tools": [tool.name for tool in tools.tools]}
                # Parse JSON args
                try:
                    args = json.loads(json_args) if json_args else {}
                except Exception as e:
                    return {"error": f"Invalid JSON arguments: {e}"}
                # Call the tool
                result = await session.call_tool(tool_name, args)
                return result
    except Exception as e:
        tb = traceback.format_exc()
        return {"error": str(e), "traceback": tb}

def call_tool(url, tool_name, json_args):
    return asyncio.run(call_tool_from_server(url, tool_name, json_args))

def gradio_tool_lister_and_caller(url, tool_name, json_args):
    """Gradio interface function for tool lister/caller."""
    return call_tool(url, tool_name, json_args)

demo = gr.Interface(
    fn=gradio_tool_lister_and_caller,
    inputs=[
        gr.Textbox(label="MCP Server URL", value=DEFAULT_MCP_URL),
        gr.Textbox(label="Tool Name", value=DEFAULT_TOOL_NAME),
        gr.Textbox(label="Tool Arguments (JSON)", value=DEFAULT_JSON_ARGS, lines=4),
    ],
    outputs=gr.JSON(label="Result"),
    title="MCP Tool Lister and Caller",
    description="Enter an MCP server URL, tool name, and JSON arguments to call a tool. Leave tool name blank to list available tools.",
)
demo.launch()