forked from MervinPraison/PraisonAI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp_http_stream.py
More file actions
737 lines (611 loc) · 28.1 KB
/
mcp_http_stream.py
File metadata and controls
737 lines (611 loc) · 28.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
"""
HTTP Stream client implementation for MCP (Model Context Protocol).
This module provides the necessary classes and functions to connect to an MCP server
over HTTP Stream transport, implementing the Streamable HTTP transport protocol.
"""
import asyncio
import atexit
import logging
import threading
import inspect
import json
import time
import uuid
import weakref
from typing import List, Dict, Any, Optional, Callable, Iterable, Union
from urllib.parse import urlparse, urljoin
try:
from mcp import ClientSession
MCP_AVAILABLE = True
except ImportError:
MCP_AVAILABLE = False
ClientSession = None
try:
import aiohttp
except ImportError:
aiohttp = None
logger = logging.getLogger("mcp-http-stream")
# Global event loop for async operations
_event_loop = None
# Global registry of active clients for cleanup
_active_clients = weakref.WeakSet()
_cleanup_registered = False
def get_event_loop():
"""Get or create a global event loop."""
global _event_loop
if _event_loop is None or _event_loop.is_closed():
_event_loop = asyncio.new_event_loop()
asyncio.set_event_loop(_event_loop)
return _event_loop
def _cleanup_all_clients():
"""Clean up all active clients at program exit."""
if not _active_clients:
return
# Create a copy to avoid modification during iteration
clients_to_cleanup = list(_active_clients)
for client in clients_to_cleanup:
try:
if hasattr(client, '_force_cleanup'):
client._force_cleanup()
except Exception:
# Ignore exceptions during cleanup
pass
def _register_cleanup():
"""Register the cleanup function to run at program exit."""
global _cleanup_registered
if not _cleanup_registered:
atexit.register(_cleanup_all_clients)
_cleanup_registered = True
class HTTPStreamMCPTool:
"""A wrapper for an MCP tool that can be used with praisonaiagents."""
def __init__(self, name: str, description: str, session: ClientSession, input_schema: Optional[Dict[str, Any]] = None, timeout: int = 60):
self.name = name
self.__name__ = name # Required for Agent to recognize it as a tool
self.__qualname__ = name # Required for Agent to recognize it as a tool
self.__doc__ = description # Required for Agent to recognize it as a tool
self.description = description
self.session = session
self.input_schema = input_schema or {}
self.timeout = timeout
# Create a signature based on input schema
params = []
if input_schema and 'properties' in input_schema:
for param_name, prop_schema in input_schema['properties'].items():
# Determine type annotation based on schema
prop_type = prop_schema.get('type', 'string') if isinstance(prop_schema, dict) else 'string'
if prop_type == 'string':
annotation = str
elif prop_type == 'integer':
annotation = int
elif prop_type == 'number':
annotation = float
elif prop_type == 'boolean':
annotation = bool
elif prop_type == 'array':
annotation = list
elif prop_type == 'object':
annotation = dict
else:
annotation = Any
params.append(
inspect.Parameter(
name=param_name,
kind=inspect.Parameter.POSITIONAL_OR_KEYWORD,
default=inspect.Parameter.empty if param_name in input_schema.get('required', []) else None,
annotation=annotation
)
)
self.__signature__ = inspect.Signature(params)
def __call__(self, **kwargs):
"""Synchronous wrapper for the async call."""
logger.debug(f"Tool {self.name} called with args: {kwargs}")
# Use the global event loop
loop = get_event_loop()
# Run the async call in the event loop
future = asyncio.run_coroutine_threadsafe(self._async_call(**kwargs), loop)
try:
# Wait for the result with a timeout
return future.result(timeout=self.timeout)
except Exception as e:
logger.error(f"Error calling tool {self.name}: {e}")
return f"Error: {str(e)}"
async def _async_call(self, **kwargs):
"""Call the tool with the provided arguments."""
logger.debug(f"Async calling tool {self.name} with args: {kwargs}")
try:
result = await self.session.call_tool(self.name, kwargs)
# Extract text from result
if hasattr(result, 'content') and result.content:
if hasattr(result.content[0], 'text'):
return result.content[0].text
return str(result.content[0])
return str(result)
except Exception as e:
logger.error(f"Error in _async_call for {self.name}: {e}")
raise
def _fix_array_schemas(self, schema):
"""
Fix array schemas by adding missing 'items' attribute required by OpenAI.
This ensures compatibility with OpenAI's function calling format which
requires array types to specify the type of items they contain.
Args:
schema: The schema dictionary to fix
Returns:
dict: The fixed schema
"""
if not isinstance(schema, dict):
return schema
# Create a copy to avoid modifying the original
fixed_schema = schema.copy()
# Fix array types at the current level
if fixed_schema.get("type") == "array" and "items" not in fixed_schema:
# Add a default items schema for arrays without it
fixed_schema["items"] = {"type": "string"}
# Recursively fix nested schemas
if "properties" in fixed_schema:
fixed_properties = {}
for prop_name, prop_schema in fixed_schema["properties"].items():
fixed_properties[prop_name] = self._fix_array_schemas(prop_schema)
fixed_schema["properties"] = fixed_properties
# Fix items schema if it exists
if "items" in fixed_schema:
fixed_schema["items"] = self._fix_array_schemas(fixed_schema["items"])
return fixed_schema
def to_openai_tool(self):
"""Convert the tool to OpenAI format."""
# Fix array schemas to include 'items' attribute
fixed_schema = self._fix_array_schemas(self.input_schema)
return {
"type": "function",
"function": {
"name": self.name,
"description": self.description,
"parameters": fixed_schema
}
}
class HTTPStreamTransport:
"""
HTTP Stream Transport implementation for MCP.
This transport provides a single endpoint for all MCP communication,
supporting both batch (JSON) and streaming (SSE) response modes.
Per MCP Protocol Revision 2025-11-25:
- Includes MCP-Protocol-Version header on all requests
- Handles session management via Mcp-Session-Id header
- Supports SSE resumability via Last-Event-ID
"""
# Default protocol version for backward compatibility
DEFAULT_PROTOCOL_VERSION = '2025-03-26'
def __init__(self, base_url: str, session_id: Optional[str] = None, options: Optional[Dict[str, Any]] = None):
self.base_url = base_url
self.session_id = session_id
self.options = options or {}
self.response_mode = self.options.get('responseMode', 'batch')
self.protocol_version = self.options.get('protocol_version', self.DEFAULT_PROTOCOL_VERSION)
# Track last event ID for resumability
self.last_event_id: Optional[str] = None
self._retry_delay_ms: int = 3000 # Default retry delay
self.headers = {
'Content-Type': 'application/json',
'Accept': 'application/json, text/event-stream',
'Mcp-Protocol-Version': self.protocol_version # Required per spec
}
if session_id:
self.headers['Mcp-Session-Id'] = session_id
# Add custom headers if provided
if 'headers' in self.options:
self.headers.update(self.options['headers'])
self._session = None
self._sse_task = None
self._message_queue = asyncio.Queue()
self._pending_requests = {}
self._closing = False
self._closed = False
async def __aenter__(self):
self._session = aiohttp.ClientSession()
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
# Prevent double closing
if self._closed:
return
# Set closing flag to stop listener gracefully
self._closing = True
self._closed = True
if self._sse_task:
self._sse_task.cancel()
try:
await self._sse_task
except asyncio.CancelledError:
pass
if self._session:
await self._session.close()
def __del__(self):
"""Lightweight cleanup during garbage collection."""
# Note: We cannot safely run async cleanup in __del__
# The best practice is to use async context managers or explicit close() calls
pass
async def send_request(self, request: Dict[str, Any]) -> Union[Dict[str, Any], None]:
"""Send a request to the HTTP Stream endpoint."""
if not self._session:
raise RuntimeError("Transport not initialized. Use async context manager.")
try:
async with self._session.post(self.base_url, json=request, headers=self.headers) as response:
# Update session ID if provided in response
if 'Mcp-Session-Id' in response.headers:
self.session_id = response.headers['Mcp-Session-Id']
self.headers['Mcp-Session-Id'] = self.session_id
# Handle different response types
content_type = response.headers.get('Content-Type', '')
if 'text/event-stream' in content_type:
# Stream mode - process SSE events
return await self._process_sse_response(response)
else:
# Batch mode - return JSON response
return await response.json()
except Exception as e:
logger.error(f"Error sending request: {e}")
raise
async def _process_sse_response(self, response):
"""Process SSE response stream with resumability support."""
buffer = ""
async for chunk in response.content:
buffer += chunk.decode('utf-8')
# Process complete SSE events
while "\n\n" in buffer:
event, buffer = buffer.split("\n\n", 1)
lines = event.strip().split("\n")
# Parse SSE event fields per spec
event_id = None
data_lines = []
retry_value = None
for line in lines:
if line.startswith("id:"):
# Track event ID for resumability
event_id = line[3:].strip()
elif line.startswith("data:"):
data_lines.append(line[5:].strip())
elif line.startswith("retry:"):
# Handle retry field per SSE spec
try:
retry_value = int(line[6:].strip())
except ValueError:
pass
# Update last event ID for resumability
if event_id:
self.last_event_id = event_id
# Update retry delay if provided
if retry_value is not None:
self._retry_delay_ms = retry_value
# Process data if present
data = '\n'.join(data_lines) if data_lines else None
if data:
try:
message = json.loads(data)
# Process the message
if 'id' in message and message['id'] in self._pending_requests:
# This is a response to a pending request
self._pending_requests[message['id']].set_result(message)
else:
# This is a server-initiated message
await self._message_queue.put(message)
except json.JSONDecodeError:
logger.error(f"Failed to parse SSE event: {data}")
async def terminate_session(self) -> bool:
"""
Terminate the session via HTTP DELETE.
Per MCP spec: Clients that no longer need a particular session
SHOULD send an HTTP DELETE to the MCP endpoint with the
Mcp-Session-Id header to explicitly terminate the session.
Returns:
True if session was terminated, False if server doesn't support it
"""
if not self._session or not self.session_id:
return False
try:
headers = {
'Mcp-Session-Id': self.session_id,
'Mcp-Protocol-Version': self.protocol_version
}
async with self._session.delete(self.base_url, headers=headers) as response:
if response.status == 405:
# Server doesn't allow client-initiated session termination
logger.debug("Server does not allow session termination via DELETE")
return False
elif response.status in (200, 202, 204):
# Session terminated successfully
self.session_id = None
self.headers.pop('Mcp-Session-Id', None)
return True
else:
logger.warning(f"Unexpected response to DELETE: {response.status}")
return False
except Exception as e:
logger.error(f"Error terminating session: {e}")
return False
async def start_sse_listener(self):
"""Start listening for SSE events from the server."""
if self._sse_task is None or self._sse_task.done():
self._sse_task = asyncio.create_task(self._sse_listener())
async def _sse_listener(self):
"""Background task to listen for SSE events with resumability support."""
while True:
try:
# Check if we should stop
if hasattr(self, '_closing') and self._closing:
break
url = self.base_url
# Build headers per MCP spec
headers = {
'Accept': 'text/event-stream',
'Cache-Control': 'no-cache',
'Mcp-Protocol-Version': self.protocol_version # Required per spec
}
if self.session_id:
headers['Mcp-Session-Id'] = self.session_id
# Include Last-Event-ID for resumability per spec
if self.last_event_id:
headers['Last-Event-ID'] = self.last_event_id
async with self._session.get(url, headers=headers) as response:
# Handle session expiration (HTTP 404)
if response.status == 404:
logger.warning("Session expired (HTTP 404), need to reinitialize")
self.session_id = None
self.headers.pop('Mcp-Session-Id', None)
break
buffer = ""
async for chunk in response.content:
# Check if we should stop
if hasattr(self, '_closing') and self._closing:
break
buffer += chunk.decode('utf-8')
# Process complete SSE events
while "\n\n" in buffer:
event, buffer = buffer.split("\n\n", 1)
lines = event.strip().split("\n")
# Parse SSE event fields
event_id = None
data_lines = []
retry_value = None
for line in lines:
if line.startswith("id:"):
event_id = line[3:].strip()
elif line.startswith("data:"):
data_lines.append(line[5:].strip())
elif line.startswith("retry:"):
try:
retry_value = int(line[6:].strip())
except ValueError:
pass
# Update last event ID for resumability
if event_id:
self.last_event_id = event_id
# Update retry delay if provided
if retry_value is not None:
self._retry_delay_ms = retry_value
# Process data
data = '\n'.join(data_lines) if data_lines else None
if data:
try:
message = json.loads(data)
await self._message_queue.put(message)
except json.JSONDecodeError:
logger.error(f"Failed to parse SSE event: {data}")
except asyncio.CancelledError:
# Proper shutdown
break
except Exception as e:
if not (hasattr(self, '_closing') and self._closing):
logger.error(f"SSE listener error: {e}")
# Use retry delay from server or default
await asyncio.sleep(self._retry_delay_ms / 1000.0)
else:
break
def read_stream(self):
"""Create a read stream for the ClientSession."""
async def _read():
while True:
message = await self._message_queue.get()
yield message
return _read()
def write_stream(self):
"""Create a write stream for the ClientSession."""
async def _write(message):
if hasattr(message, 'to_dict'):
message = message.to_dict()
response = await self.send_request(message)
return response
return _write
class HTTPStreamMCPClient:
"""A client for connecting to an MCP server over HTTP Stream transport."""
def __init__(self, server_url: str, debug: bool = False, timeout: int = 60, options: Optional[Dict[str, Any]] = None):
"""
Initialize an HTTP Stream MCP client.
Args:
server_url: The URL of the HTTP Stream MCP server
debug: Whether to enable debug logging
timeout: Timeout in seconds for operations (default: 60)
options: Additional configuration options for the transport
"""
# Check if MCP is available
if not MCP_AVAILABLE:
raise ImportError(
"MCP (Model Context Protocol) package is not installed. "
"Install it with: pip install praisonaiagents[mcp]"
)
# Check if aiohttp is available
if aiohttp is None:
raise ImportError(
"aiohttp is required for HTTP Stream transport. "
"Install it with: pip install praisonaiagents[mcp]"
)
# Parse URL to extract base URL and endpoint
parsed = urlparse(server_url)
# If the URL already has a path, use it; otherwise use default /mcp endpoint
if parsed.path and parsed.path != '/':
self.base_url = server_url
else:
# Default endpoint is /mcp
self.base_url = urljoin(server_url, '/mcp')
self.debug = debug
self.timeout = timeout
self.options = options or {}
self.session = None
self.tools = []
self.transport = None
self._closed = False
# Set up logging
if debug:
logger.setLevel(logging.DEBUG)
else:
# Set to WARNING by default to hide INFO messages
logger.setLevel(logging.WARNING)
# Register this client for cleanup and setup exit handler
_active_clients.add(self)
_register_cleanup()
self._initialize()
def _initialize(self):
"""Initialize the connection and tools."""
# Use the global event loop
loop = get_event_loop()
# Start a background thread to run the event loop
def run_event_loop():
asyncio.set_event_loop(loop)
loop.run_forever()
self.loop_thread = threading.Thread(target=run_event_loop, daemon=True)
self.loop_thread.start()
# Run the initialization in the event loop
future = asyncio.run_coroutine_threadsafe(self._async_initialize(), loop)
self.tools = future.result(timeout=self.timeout)
async def _async_initialize(self):
"""Asynchronously initialize the connection and tools."""
logger.debug(f"Connecting to MCP server at {self.base_url}")
# Create HTTP Stream transport
self.transport = HTTPStreamTransport(self.base_url, options=self.options)
await self.transport.__aenter__()
# Create read and write streams
read_stream = self.transport.read_stream()
write_stream = self.transport.write_stream()
# Start SSE listener if in stream mode
if self.options.get('responseMode', 'batch') == 'stream':
await self.transport.start_sse_listener()
# Create client session
self._session_context = ClientSession(read_stream, write_stream)
self.session = await self._session_context.__aenter__()
# Initialize
await self.session.initialize()
# List available tools
logger.debug("Listing tools...")
response = await self.session.list_tools()
tools_data = response.tools
logger.debug(f"Found {len(tools_data)} tools: {[tool.name for tool in tools_data]}")
# Create tool wrappers
tools = []
for tool in tools_data:
input_schema = tool.inputSchema if hasattr(tool, 'inputSchema') else None
wrapper = HTTPStreamMCPTool(
name=tool.name,
description=tool.description if hasattr(tool, 'description') else f"Call the {tool.name} tool",
session=self.session,
input_schema=input_schema,
timeout=self.timeout
)
tools.append(wrapper)
# Set up cleanup finalizer now that transport and session are created
self._finalizer = weakref.finalize(self, self._static_cleanup,
self.transport, self._session_context)
return tools
def __iter__(self):
"""Return an iterator over the tools."""
return iter(self.tools)
def to_openai_tools(self):
"""Convert all tools to OpenAI format."""
return [tool.to_openai_tool() for tool in self.tools]
async def __aenter__(self):
"""Async context manager entry."""
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
"""Async context manager exit."""
await self.aclose()
async def aclose(self):
"""Async cleanup method to close all resources."""
if self._closed:
return
self._closed = True
try:
if hasattr(self, '_session_context') and self._session_context:
await self._session_context.__aexit__(None, None, None)
except Exception:
pass
try:
if self.transport:
await self.transport.__aexit__(None, None, None)
except Exception:
pass
def close(self):
"""Synchronous cleanup method to close all resources."""
if self._closed:
return
try:
# Use the global event loop for non-blocking cleanup
loop = get_event_loop()
if not loop.is_closed():
# Schedule cleanup without blocking - add callback for fallback
future = asyncio.run_coroutine_threadsafe(self.aclose(), loop)
# Add a completion callback for fallback cleanup if async fails
def _cleanup_callback(fut):
try:
fut.result() # This will raise if aclose() failed
except Exception:
# If async cleanup failed, try force cleanup
try:
self._force_cleanup()
except Exception:
pass
future.add_done_callback(_cleanup_callback)
else:
# Event loop is closed, use force cleanup immediately
self._force_cleanup()
except Exception:
# If async scheduling fails, try force cleanup
self._force_cleanup()
def _force_cleanup(self):
"""Force cleanup of resources synchronously (for emergencies)."""
if self._closed:
return
self._closed = True
# Force close transport session if it exists
try:
if self.transport and hasattr(self.transport, '_session') and self.transport._session:
session = self.transport._session
if not session.closed:
# Force close the aiohttp session
if hasattr(session, '_connector') and session._connector:
try:
# Close connector directly
session._connector.close()
except Exception:
pass
except Exception:
pass
@staticmethod
def _static_cleanup(transport, session_context):
"""Static cleanup method for weakref finalizer."""
try:
# This is called by weakref finalizer, so we can't do async operations
# Just ensure any session is closed if possible
if transport and hasattr(transport, '_session') and transport._session:
session = transport._session
if not session.closed and hasattr(session, '_connector'):
try:
session._connector.close()
except Exception:
pass
except Exception:
pass
def __del__(self):
"""Cleanup when object is garbage collected."""
try:
if not self._closed:
self._force_cleanup()
except Exception:
# Never raise exceptions in __del__
pass