Spaces:
Sleeping
Sleeping
# 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() | |