Kuk1 commited on
Commit
3f50b76
·
1 Parent(s): 93a6044

Add SSE support for cloud deployment

Browse files
Files changed (2) hide show
  1. mcp.json +4 -19
  2. src/core/mcp_client.py +34 -12
mcp.json CHANGED
@@ -1,25 +1,10 @@
1
  {
2
- "$schema": "https://json.schemastore.org/mcp.json",
3
- "$comment": "Echo Universal Host - MCP Server Configuration",
4
  "mcpServers": {
5
  "agentbell-voice": {
6
- "command": "python",
7
- "args": [
8
- "services/mcp_server/server.py"
9
- ],
10
- "env": {
11
- "PYTHONUNBUFFERED": "1",
12
- "PYTHONPATH": "."
13
- },
14
- "_description": "AgentBell Voice MCP Server - Provides voice notification tools"
15
- },
16
- "memory": {
17
- "command": "npx",
18
- "args": [
19
- "-y",
20
- "@modelcontextprotocol/server-memory"
21
- ],
22
- "_description": "Simple in-memory key-value store for testing MCP connections"
23
  }
24
  }
25
  }
 
1
  {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$comment": "MCP Server Configuration for Echo Client (Hugging Face Deployment)",
4
  "mcpServers": {
5
  "agentbell-voice": {
6
+ "url": "https://mcp-1st-birthday-agentbell-mcp-server.hf.space/sse",
7
+ "_description": "AgentBell Voice Companion (Cloud-hosted SSE)"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  }
9
  }
10
  }
src/core/mcp_client.py CHANGED
@@ -7,6 +7,7 @@ from contextlib import AsyncExitStack
7
 
8
  from mcp import ClientSession, StdioServerParameters
9
  from mcp.client.stdio import stdio_client
 
10
  import mcp.types as types
11
 
12
  import sys
@@ -21,15 +22,20 @@ logger = logging.getLogger("mcp_host")
21
 
22
  class MCPConnection:
23
  """
24
- Manages the connection to an MCP Server via Stdio.
25
  Acts as a generic MCP Host.
26
  """
27
- def __init__(self, server_command: str, server_args: List[str], env: Optional[Dict[str, str]] = None):
28
- self.server_params = StdioServerParameters(
29
- command=server_command,
30
- args=server_args,
31
- env=env or os.environ.copy()
32
- )
 
 
 
 
 
33
  self.session: Optional[ClientSession] = None
34
  self.exit_stack = AsyncExitStack()
35
  self.tools: List[Any] = []
@@ -60,13 +66,24 @@ class MCPConnection:
60
 
61
  async def connect(self):
62
  """Establishes the connection to the MCP Server."""
63
- logger.info(f"Connecting to MCP Server: {self.server_params.command} {self.server_params.args}")
 
 
 
 
 
64
 
65
  try:
66
- # 1. Start the stdio transport (NO stream interception - it breaks MCP SDK)
67
- read, write = await self.exit_stack.enter_async_context(
68
- stdio_client(self.server_params)
69
- )
 
 
 
 
 
 
70
 
71
  # 2. Create the session
72
  self.session = await self.exit_stack.enter_async_context(
@@ -185,6 +202,11 @@ def create_connection_from_config(server_name: str = "agentbell-voice") -> MCPCo
185
 
186
  server_config = config["mcpServers"][server_name]
187
 
 
 
 
 
 
188
  # Fix command: Use sys.executable if command is 'python' or 'python3'
189
  # This ensures the subprocess uses the same virtual environment as the host
190
  cmd = server_config["command"]
 
7
 
8
  from mcp import ClientSession, StdioServerParameters
9
  from mcp.client.stdio import stdio_client
10
+ from mcp.client.sse import sse_client
11
  import mcp.types as types
12
 
13
  import sys
 
22
 
23
  class MCPConnection:
24
  """
25
+ Manages the connection to an MCP Server via Stdio or SSE.
26
  Acts as a generic MCP Host.
27
  """
28
+ def __init__(self, server_command: str = None, server_args: List[str] = None, env: Optional[Dict[str, str]] = None, url: str = None):
29
+ self.server_params = None
30
+ self.url = url
31
+
32
+ if server_command:
33
+ self.server_params = StdioServerParameters(
34
+ command=server_command,
35
+ args=server_args or [],
36
+ env=env or os.environ.copy()
37
+ )
38
+
39
  self.session: Optional[ClientSession] = None
40
  self.exit_stack = AsyncExitStack()
41
  self.tools: List[Any] = []
 
66
 
67
  async def connect(self):
68
  """Establishes the connection to the MCP Server."""
69
+ if self.url:
70
+ logger.info(f"Connecting to MCP Server via SSE: {self.url}")
71
+ elif self.server_params:
72
+ logger.info(f"Connecting to MCP Server via Stdio: {self.server_params.command} {self.server_params.args}")
73
+ else:
74
+ raise ValueError("No connection parameters (command or url) provided.")
75
 
76
  try:
77
+ if self.url:
78
+ # 1. Start SSE transport
79
+ read, write = await self.exit_stack.enter_async_context(
80
+ sse_client(self.url)
81
+ )
82
+ else:
83
+ # 1. Start stdio transport
84
+ read, write = await self.exit_stack.enter_async_context(
85
+ stdio_client(self.server_params)
86
+ )
87
 
88
  # 2. Create the session
89
  self.session = await self.exit_stack.enter_async_context(
 
202
 
203
  server_config = config["mcpServers"][server_name]
204
 
205
+ # Check for SSE config (url)
206
+ if "url" in server_config:
207
+ return MCPConnection(url=server_config["url"])
208
+
209
+ # Fallback to Stdio config (command)
210
  # Fix command: Use sys.executable if command is 'python' or 'python3'
211
  # This ensures the subprocess uses the same virtual environment as the host
212
  cmd = server_config["command"]