Update app.py
Browse files
app.py
CHANGED
|
@@ -7,8 +7,8 @@ import aiohttp
|
|
| 7 |
from typing import Dict, Any, Optional, List
|
| 8 |
from google import genai
|
| 9 |
from google.genai import types
|
| 10 |
-
from
|
| 11 |
-
from mcp import
|
| 12 |
from mcp.client.streamable_http import streamablehttp_client
|
| 13 |
import nest_asyncio
|
| 14 |
|
|
@@ -21,7 +21,6 @@ class MCPTrainClient:
|
|
| 21 |
def __init__(self):
|
| 22 |
self.session = None
|
| 23 |
self.exit_stack = None
|
| 24 |
-
self.tools = []
|
| 25 |
self.mcp_server_url = "https://mgokg-db-timetable-api.hf.space/gradio_api/mcp/"
|
| 26 |
|
| 27 |
async def connect(self) -> str:
|
|
@@ -32,23 +31,18 @@ class MCPTrainClient:
|
|
| 32 |
|
| 33 |
self.exit_stack = AsyncExitStack()
|
| 34 |
|
| 35 |
-
# Connect using Streamable HTTP transport
|
| 36 |
-
transport = await self.exit_stack.enter_async_context(
|
| 37 |
-
streamablehttp_client(self.mcp_server_url)
|
| 38 |
-
)
|
| 39 |
-
|
| 40 |
-
self.read_stream, self.write_stream = transport
|
| 41 |
self.session = await self.exit_stack.enter_async_context(
|
| 42 |
-
|
| 43 |
)
|
| 44 |
|
|
|
|
| 45 |
await self.session.initialize()
|
| 46 |
|
| 47 |
# List available tools
|
| 48 |
response = await self.session.list_tools()
|
| 49 |
-
|
| 50 |
|
| 51 |
-
tool_names = [tool.name for tool in self.tools]
|
| 52 |
return f"✅ Connected to MCP server. Available tools: {', '.join(tool_names)}"
|
| 53 |
|
| 54 |
except Exception as e:
|
|
@@ -80,7 +74,6 @@ class MCPTrainClient:
|
|
| 80 |
await self.exit_stack.aclose()
|
| 81 |
self.exit_stack = None
|
| 82 |
self.session = None
|
| 83 |
-
self.tools = []
|
| 84 |
|
| 85 |
|
| 86 |
class GeminiMCPIntegration:
|
|
|
|
| 7 |
from typing import Dict, Any, Optional, List
|
| 8 |
from google import genai
|
| 9 |
from google.genai import types
|
| 10 |
+
from mcp import ClientSession, StdioServerParameters
|
| 11 |
+
from mcp.client.stdio import stdio_client
|
| 12 |
from mcp.client.streamable_http import streamablehttp_client
|
| 13 |
import nest_asyncio
|
| 14 |
|
|
|
|
| 21 |
def __init__(self):
|
| 22 |
self.session = None
|
| 23 |
self.exit_stack = None
|
|
|
|
| 24 |
self.mcp_server_url = "https://mgokg-db-timetable-api.hf.space/gradio_api/mcp/"
|
| 25 |
|
| 26 |
async def connect(self) -> str:
|
|
|
|
| 31 |
|
| 32 |
self.exit_stack = AsyncExitStack()
|
| 33 |
|
| 34 |
+
# Connect using Streamable HTTP transport - FIXED VERSION
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
self.session = await self.exit_stack.enter_async_context(
|
| 36 |
+
streamablehttp_client(self.mcp_server_url)
|
| 37 |
)
|
| 38 |
|
| 39 |
+
# Initialize the session
|
| 40 |
await self.session.initialize()
|
| 41 |
|
| 42 |
# List available tools
|
| 43 |
response = await self.session.list_tools()
|
| 44 |
+
tool_names = [tool.name for tool in response.tools]
|
| 45 |
|
|
|
|
| 46 |
return f"✅ Connected to MCP server. Available tools: {', '.join(tool_names)}"
|
| 47 |
|
| 48 |
except Exception as e:
|
|
|
|
| 74 |
await self.exit_stack.aclose()
|
| 75 |
self.exit_stack = None
|
| 76 |
self.session = None
|
|
|
|
| 77 |
|
| 78 |
|
| 79 |
class GeminiMCPIntegration:
|