Skip to content

Commit 246744c

Browse files
Merge pull request #826 from MervinPraison/claude/issue-823-20250711_111524
feat: Implement MCP HTTP Stream transport
2 parents 0adc3e5 + bca039a commit 246744c

File tree

6 files changed

+679
-11
lines changed

6 files changed

+679
-11
lines changed

src/praisonai-agents/praisonaiagents/mcp/mcp.py

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,45 @@ def __init__(self, command_or_string=None, args=None, *, command=None, timeout=6
187187
self.timeout = timeout
188188
self.debug = debug
189189

190-
# Check if this is an SSE URL
190+
# Check if this is an HTTP URL
191191
if isinstance(command_or_string, str) and re.match(r'^https?://', command_or_string):
192-
# Import the SSE client implementation
193-
from .mcp_sse import SSEMCPClient
194-
self.sse_client = SSEMCPClient(command_or_string, debug=debug, timeout=timeout)
195-
self._tools = list(self.sse_client.tools)
196-
self.is_sse = True
197-
self.is_npx = False
198-
return
192+
# Determine transport type based on URL or kwargs
193+
if command_or_string.endswith('/sse') and 'transport_type' not in kwargs:
194+
# Legacy SSE URL - use SSE transport for backward compatibility
195+
from .mcp_sse import SSEMCPClient
196+
self.sse_client = SSEMCPClient(command_or_string, debug=debug, timeout=timeout)
197+
self._tools = list(self.sse_client.tools)
198+
self.is_sse = True
199+
self.is_http_stream = False
200+
self.is_npx = False
201+
return
202+
else:
203+
# Use HTTP Stream transport for all other HTTP URLs
204+
from .mcp_http_stream import HTTPStreamMCPClient
205+
# Extract transport options from kwargs
206+
transport_options = {}
207+
if 'responseMode' in kwargs:
208+
transport_options['responseMode'] = kwargs.pop('responseMode')
209+
if 'headers' in kwargs:
210+
transport_options['headers'] = kwargs.pop('headers')
211+
if 'cors' in kwargs:
212+
transport_options['cors'] = kwargs.pop('cors')
213+
if 'session' in kwargs:
214+
transport_options['session'] = kwargs.pop('session')
215+
if 'resumability' in kwargs:
216+
transport_options['resumability'] = kwargs.pop('resumability')
217+
218+
self.http_stream_client = HTTPStreamMCPClient(
219+
command_or_string,
220+
debug=debug,
221+
timeout=timeout,
222+
options=transport_options
223+
)
224+
self._tools = list(self.http_stream_client.tools)
225+
self.is_sse = False
226+
self.is_http_stream = True
227+
self.is_npx = False
228+
return
199229

200230
# Handle the single string format for stdio client
201231
if isinstance(command_or_string, str) and args is None:
@@ -219,6 +249,7 @@ def __init__(self, command_or_string=None, args=None, *, command=None, timeout=6
219249

220250
# Set up stdio client
221251
self.is_sse = False
252+
self.is_http_stream = False
222253

223254
# Ensure UTF-8 encoding in environment for Docker compatibility
224255
env = kwargs.get('env', {})
@@ -275,6 +306,9 @@ def _generate_tool_functions(self) -> List[Callable]:
275306
"""
276307
if self.is_sse:
277308
return list(self.sse_client.tools)
309+
310+
if self.is_http_stream:
311+
return list(self.http_stream_client.tools)
278312

279313
tool_functions = []
280314

@@ -448,6 +482,10 @@ def to_openai_tool(self):
448482
if self.is_sse and hasattr(self, 'sse_client') and self.sse_client.tools:
449483
# Return all tools from SSE client
450484
return self.sse_client.to_openai_tools()
485+
486+
if self.is_http_stream and hasattr(self, 'http_stream_client') and self.http_stream_client.tools:
487+
# Return all tools from HTTP Stream client
488+
return self.http_stream_client.to_openai_tools()
451489

452490
# For simplicity, we'll convert the first tool only if multiple exist
453491
# More complex implementations could handle multiple tools

0 commit comments

Comments
 (0)