Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import httpx
|
| 3 |
+
import json
|
| 4 |
+
import os
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
|
| 7 |
+
# Load environment variables from .env file
|
| 8 |
+
load_dotenv()
|
| 9 |
+
|
| 10 |
+
# Configuration
|
| 11 |
+
MODAL_MCP_ENDPOINT = os.getenv("MODAL_MCP_ENDPOINT", "") # Should be like "https://username--mcp-server-app-execute-tool.modal.run"
|
| 12 |
+
TOOL_NAME = "pydocstyle"
|
| 13 |
+
AGENT_ID = "gradio-pydocstyle-ui"
|
| 14 |
+
SESSION_ID = "default_session"
|
| 15 |
+
|
| 16 |
+
# Example Python code with potential pydocstyle issues
|
| 17 |
+
EXAMPLE_CODE = """
|
| 18 |
+
# No module docstring here
|
| 19 |
+
|
| 20 |
+
def example_function_missing_docstring():
|
| 21 |
+
pass
|
| 22 |
+
|
| 23 |
+
class ExampleClassMissingDocstring:
|
| 24 |
+
def method_missing_docstring(self):
|
| 25 |
+
pass
|
| 26 |
+
|
| 27 |
+
def well_documented_function():
|
| 28 |
+
\"\"\"This function is well documented.\"\"\"
|
| 29 |
+
return True
|
| 30 |
+
"""
|
| 31 |
+
|
| 32 |
+
async def call_mcp_tool(tool_params_json: str) -> str:
|
| 33 |
+
"""Calls the specified MCP tool on the Modal server with JSON parameters."""
|
| 34 |
+
if not MODAL_MCP_ENDPOINT:
|
| 35 |
+
return json.dumps({"error": "MODAL_MCP_ENDPOINT environment variable is not set. Please set it in your .env file."})
|
| 36 |
+
|
| 37 |
+
print(f"Attempting to use Modal Endpoint for Pydocstyle: {MODAL_MCP_ENDPOINT}")
|
| 38 |
+
|
| 39 |
+
try:
|
| 40 |
+
tool_params = json.loads(tool_params_json)
|
| 41 |
+
except json.JSONDecodeError as e:
|
| 42 |
+
return json.dumps({"error": f"Invalid JSON input for tool parameters: {str(e)}"})
|
| 43 |
+
|
| 44 |
+
payload = {
|
| 45 |
+
"tool_name": TOOL_NAME,
|
| 46 |
+
"tool_params": tool_params, # Expects {'code': '...'}
|
| 47 |
+
"agent_id": AGENT_ID,
|
| 48 |
+
"session_id": SESSION_ID,
|
| 49 |
+
"context": {}
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
print(f"Calling MCP tool: {TOOL_NAME} at {MODAL_MCP_ENDPOINT} with params: {tool_params}")
|
| 53 |
+
|
| 54 |
+
try:
|
| 55 |
+
async with httpx.AsyncClient(timeout=60.0) as client:
|
| 56 |
+
response = await client.post(MODAL_MCP_ENDPOINT, json=payload)
|
| 57 |
+
|
| 58 |
+
response.raise_for_status() # Will raise an exception for 4XX/5XX responses
|
| 59 |
+
|
| 60 |
+
# The Modal endpoint returns a tuple: [response_dict, status_code]
|
| 61 |
+
# We need to parse the response content if it's a string, or access the first element if it's already a list/tuple
|
| 62 |
+
raw_response_data = response.json()
|
| 63 |
+
print(raw_response_data) # For debugging the raw response from Modal
|
| 64 |
+
|
| 65 |
+
if isinstance(raw_response_data, list) and len(raw_response_data) > 0:
|
| 66 |
+
# Assuming the actual result is the first element of the list (the dict part of the tuple)
|
| 67 |
+
mcp_result = raw_response_data[0]
|
| 68 |
+
elif isinstance(raw_response_data, dict): # If it directly returns the dict (less likely based on current MCP server)
|
| 69 |
+
mcp_result = raw_response_data
|
| 70 |
+
else:
|
| 71 |
+
return json.dumps({"error": "Unexpected response format from MCP server", "raw_response": str(raw_response_data)})
|
| 72 |
+
|
| 73 |
+
return json.dumps(mcp_result, indent=2)
|
| 74 |
+
|
| 75 |
+
except httpx.HTTPStatusError as e:
|
| 76 |
+
error_message = f"HTTP error occurred: {e.response.status_code} - {e.response.text}"
|
| 77 |
+
print(error_message)
|
| 78 |
+
try:
|
| 79 |
+
# Attempt to parse error response from server if JSON
|
| 80 |
+
error_details = e.response.json()
|
| 81 |
+
return json.dumps({"error": "HTTP Error from MCP Server", "details": error_details, "status_code": e.response.status_code}, indent=2)
|
| 82 |
+
except json.JSONDecodeError:
|
| 83 |
+
return json.dumps({"error": error_message}, indent=2)
|
| 84 |
+
except httpx.RequestError as e:
|
| 85 |
+
error_message = f"Request error occurred: {str(e)}"
|
| 86 |
+
print(error_message)
|
| 87 |
+
return json.dumps({"error": error_message}, indent=2)
|
| 88 |
+
except json.JSONDecodeError as e:
|
| 89 |
+
# This might happen if the response from Modal isn't valid JSON, though raise_for_status should catch HTTP errors first.
|
| 90 |
+
error_message = f"Failed to decode JSON response from MCP server: {str(e)}"
|
| 91 |
+
print(error_message)
|
| 92 |
+
return json.dumps({"error": error_message, "raw_response_text": response.text if 'response' in locals() else 'N/A'}, indent=2)
|
| 93 |
+
except Exception as e:
|
| 94 |
+
error_message = f"An unexpected error occurred: {str(e)}"
|
| 95 |
+
print(error_message)
|
| 96 |
+
return json.dumps({"error": error_message}, indent=2)
|
| 97 |
+
|
| 98 |
+
# Gradio Interface
|
| 99 |
+
iface = gr.Interface(
|
| 100 |
+
fn=call_mcp_tool,
|
| 101 |
+
inputs=gr.Code(
|
| 102 |
+
value=json.dumps({"code": EXAMPLE_CODE}, indent=2),
|
| 103 |
+
language="json",
|
| 104 |
+
label="Pydocstyle Tool Parameters (JSON - must include 'code' key)"
|
| 105 |
+
),
|
| 106 |
+
outputs=gr.Code(language="json", label="Pydocstyle Analysis Output"),
|
| 107 |
+
title="Pydocstyle Documentation Checker (via MCP on Modal)",
|
| 108 |
+
description=(
|
| 109 |
+
"Enter Python code as a JSON object under the 'code' key to check for documentation style issues using Pydocstyle. "
|
| 110 |
+
"The application sends the code to a Pydocstyle tool running on a Modal Labs MCP server and displays the results."
|
| 111 |
+
"Ensure your MODAL_MCP_ENDPOINT is set in a .env file (e.g., MODAL_MCP_ENDPOINT=https://your-modal-app-url/execute_tool)."
|
| 112 |
+
),
|
| 113 |
+
allow_flagging="never"
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
if __name__ == "__main__":
|
| 117 |
+
if not MODAL_MCP_ENDPOINT:
|
| 118 |
+
print("ERROR: MODAL_MCP_ENDPOINT is not set. Please create a .env file in the mcp_deploy directory with this variable.")
|
| 119 |
+
print("Example .env content: MODAL_MCP_ENDPOINT=https://your-username--mcp-server-app-execute-tool.modal.run")
|
| 120 |
+
else:
|
| 121 |
+
print(f"Using Modal MCP Endpoint: {MODAL_MCP_ENDPOINT}")
|
| 122 |
+
iface.launch(server_name="0.0.0.0")
|