media-agent-mcp 2.6.6__py3-none-any.whl → 2.6.9__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -26,10 +26,6 @@ from dotenv import load_dotenv
26
26
  import uvicorn
27
27
  import anyio
28
28
  from functools import wraps
29
- import os
30
- import sys
31
- import signal
32
- import subprocess
33
29
 
34
30
  def async_retry(max_retries=3, delay=2):
35
31
  def decorator(func):
@@ -85,131 +81,6 @@ from media_agent_mcp.async_wrapper import (
85
81
  logging.basicConfig(level=logging.INFO)
86
82
  logger = logging.getLogger(__name__)
87
83
 
88
- # Swallow ClosedResourceError from AnyIO (e.g., SSE client disconnected)
89
- class IgnoreClosedResourceErrorMiddleware:
90
- def __init__(self, app):
91
- self.app = app
92
-
93
- async def __call__(self, scope, receive, send):
94
- try:
95
- await self.app(scope, receive, send)
96
- except anyio.ClosedResourceError:
97
- logger.warning("SSE client disconnected (ClosedResourceError). Ignoring.")
98
- return
99
-
100
- # Enhanced middleware that supports reconnection for SSE clients
101
- class ReconnectableSSEMiddleware:
102
- def __init__(self, app):
103
- self.app = app
104
- self.connections = {} # 存储活跃连接
105
-
106
- async def __call__(self, scope, receive, send):
107
- # 为每个连接生成唯一ID
108
- connection_id = scope.get('client', ('unknown', 0))[0] + ':' + str(scope.get('client', ('unknown', 0))[1])
109
-
110
- # 包装send函数以跟踪连接状态
111
- original_send = send
112
-
113
- async def wrapped_send(message):
114
- if message.get('type') == 'http.response.start':
115
- # 记录新连接
116
- self.connections[connection_id] = {'active': True}
117
- logger.info(f"New SSE connection established: {connection_id}")
118
- elif message.get('type') == 'http.response.body' and message.get('more_body', False) is False:
119
- # 连接结束
120
- if connection_id in self.connections:
121
- self.connections[connection_id]['active'] = False
122
- logger.info(f"SSE connection closed normally: {connection_id}")
123
-
124
- # 调用原始send
125
- await original_send(message)
126
-
127
- try:
128
- # 使用包装后的send函数
129
- await self.app(scope, receive, wrapped_send)
130
- except anyio.ClosedResourceError:
131
- # 客户端断开连接
132
- if connection_id in self.connections:
133
- self.connections[connection_id]['active'] = False
134
-
135
- logger.warning(f"SSE client disconnected (ClosedResourceError): {connection_id}. Client can reconnect.")
136
- # 不抛出异常,允许客户端重连
137
- return
138
-
139
- # 在出现错误时重启应用的中间件
140
- class RestartOnErrorMiddleware:
141
- def __init__(self, app):
142
- self.app = app
143
- self.restart_cooldown = 5 # 重启冷却时间(秒)
144
- self.script_path = os.path.abspath(sys.argv[0])
145
- self.args = sys.argv[1:]
146
-
147
- async def __call__(self, scope, receive, send):
148
- try:
149
- await self.app(scope, receive, send)
150
- except anyio.ClosedResourceError:
151
- logger.warning("检测到 ClosedResourceError,准备重启应用...")
152
- # 等待一段时间,确保日志输出
153
- await asyncio.sleep(1)
154
-
155
- # 先终止当前进程的服务器,释放端口
156
- logger.info("正在关闭当前服务器以释放端口...")
157
- # 发送SIGTERM信号给当前进程,但不立即退出
158
- # 这会触发uvicorn的关闭流程,释放端口
159
- os.kill(os.getpid(), signal.SIGUSR1)
160
-
161
- # 等待端口释放
162
- await asyncio.sleep(2)
163
-
164
- # 启动新进程
165
- logger.info(f"启动新进程: {self.script_path} {' '.join(self.args)}")
166
- subprocess.Popen([sys.executable, self.script_path] + self.args)
167
-
168
- # 等待冷却时间
169
- logger.info(f"等待 {self.restart_cooldown} 秒后终止当前进程...")
170
- await asyncio.sleep(self.restart_cooldown)
171
-
172
- # 终止当前进程
173
- logger.info("终止当前进程")
174
- os.kill(os.getpid(), signal.SIGTERM)
175
- return
176
-
177
- # 重置超过窗口期的错误计数
178
- if current_time - self.last_error_time > self.error_window:
179
- self.error_count = 0
180
-
181
- self.last_error_time = current_time
182
- self.error_count += 1
183
-
184
- logger.warning(f"SSE client disconnected (ClosedResourceError). Error count: {self.error_count}/{self.max_errors}")
185
-
186
- # 如果错误次数超过阈值,重启应用
187
- if self.error_count >= self.max_errors:
188
- logger.error(f"Too many connection errors ({self.error_count}). Restarting application...")
189
-
190
- # 启动一个新进程来重启应用
191
- # 使用当前脚本的路径和参数
192
- args = sys.argv.copy()
193
-
194
- # 确保我们不会立即重启(可能导致无限循环)
195
- logger.info(f"Cooling down for {self.restart_cooldown} seconds before restart...")
196
- await asyncio.sleep(self.restart_cooldown)
197
-
198
- # 使用subprocess启动新进程
199
- subprocess.Popen(args, env=os.environ.copy())
200
-
201
- # 发送SIGTERM信号给当前进程
202
- logger.info("Sending termination signal to current process...")
203
- os.kill(os.getpid(), signal.SIGTERM)
204
-
205
- # 等待进程终止
206
- await asyncio.sleep(1)
207
-
208
- # 如果进程还在运行,强制退出
209
- sys.exit(0)
210
-
211
- return
212
-
213
84
  # Initialize FastMCP server (will be configured in main function)
214
85
  load_dotenv()
215
86
  mcp = FastMCP("Media-Agent-MCP-Async")
@@ -654,7 +525,7 @@ def main():
654
525
  mcp.settings.port = args.port
655
526
  # Use uvicorn to run SSE app with extended keep-alive timeout (5 minutes)
656
527
  uvicorn.run(
657
- RestartOnErrorMiddleware(mcp.sse_app()),
528
+ IgnoreClosedResourceErrorMiddleware(mcp.sse_app()),
658
529
  host=args.host,
659
530
  port=args.port,
660
531
  timeout_keep_alive=300
media_agent_mcp/server.py CHANGED
@@ -21,12 +21,6 @@ import json
21
21
  from dotenv import load_dotenv
22
22
  import uvicorn
23
23
  import anyio
24
- import uuid
25
- import os
26
- import sys
27
- import signal
28
- import subprocess
29
- import asyncio
30
24
 
31
25
  from mcp.server.fastmcp import FastMCP
32
26
 
@@ -56,83 +50,6 @@ class IgnoreClosedResourceErrorMiddleware:
56
50
  await self.app(scope, receive, send)
57
51
  except anyio.ClosedResourceError:
58
52
  logger.warning("SSE client disconnected (ClosedResourceError). Ignoring.")
59
- return
60
-
61
- # Enhanced middleware that supports reconnection for SSE clients
62
- class ReconnectableSSEMiddleware:
63
- def __init__(self, app):
64
- self.app = app
65
- self.connections = {} # 存储活跃连接
66
-
67
- async def __call__(self, scope, receive, send):
68
- # 为每个连接生成唯一ID
69
- connection_id = scope.get('client', ('unknown', 0))[0] + ':' + str(scope.get('client', ('unknown', 0))[1])
70
-
71
- # 包装send函数以跟踪连接状态
72
- original_send = send
73
-
74
- async def wrapped_send(message):
75
- if message.get('type') == 'http.response.start':
76
- # 记录新连接
77
- self.connections[connection_id] = {'active': True}
78
- logger.info(f"New SSE connection established: {connection_id}")
79
- elif message.get('type') == 'http.response.body' and message.get('more_body', False) is False:
80
- # 连接结束
81
- if connection_id in self.connections:
82
- self.connections[connection_id]['active'] = False
83
- logger.info(f"SSE connection closed normally: {connection_id}")
84
-
85
- # 调用原始send
86
- await original_send(message)
87
-
88
- try:
89
- # 使用包装后的send函数
90
- await self.app(scope, receive, wrapped_send)
91
- except anyio.ClosedResourceError:
92
- # 客户端断开连接
93
- if connection_id in self.connections:
94
- self.connections[connection_id]['active'] = False
95
-
96
- logger.warning(f"SSE client disconnected (ClosedResourceError): {connection_id}. Client can reconnect.")
97
- # 不抛出异常,允许客户端重连
98
- return
99
-
100
- # 在出现错误时重启应用的中间件
101
- class RestartOnErrorMiddleware:
102
- def __init__(self, app):
103
- self.app = app
104
- self.restart_cooldown = 5 # 重启冷却时间(秒)
105
- self.script_path = os.path.abspath(sys.argv[0])
106
- self.args = sys.argv[1:]
107
-
108
- async def __call__(self, scope, receive, send):
109
- try:
110
- await self.app(scope, receive, send)
111
- except anyio.ClosedResourceError:
112
- logger.warning("检测到 ClosedResourceError,准备重启应用...")
113
- # 等待一段时间,确保日志输出
114
- await asyncio.sleep(1)
115
-
116
- # 启动新进程
117
- logger.info(f"启动新进程: {self.script_path} {' '.join(self.args)}")
118
- subprocess.Popen([sys.executable, self.script_path] + self.args)
119
-
120
- # 等待冷却时间
121
- logger.info(f"等待 {self.restart_cooldown} 秒后终止当前进程...")
122
- await asyncio.sleep(self.restart_cooldown)
123
-
124
- # 终止当前进程
125
- logger.info("终止当前进程")
126
- os.kill(os.getpid(), signal.SIGTERM)
127
- return
128
-
129
-
130
-
131
-
132
-
133
-
134
-
135
-
136
53
  return
137
54
 
138
55
  # Initialize FastMCP server (will be configured in main function)
@@ -673,7 +590,7 @@ def main():
673
590
  # Configure and run the server
674
591
  if args.transport == 'sse':
675
592
  # SSE transport
676
- uvicorn.run(RestartOnErrorMiddleware(mcp.create_sse_app()), host=args.host, port=args.port)
593
+ uvicorn.run(IgnoreClosedResourceErrorMiddleware(mcp.create_sse_app()), host=args.host, port=args.port)
677
594
  else:
678
595
  # STDIO transport (default)
679
596
  mcp.run()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: media-agent-mcp
3
- Version: 2.6.6
3
+ Version: 2.6.9
4
4
  Summary: A Model Context Protocol server for media processing with AI tools
5
5
  Author-email: Media Agent Team <team@mediaagent.com>
6
6
  Keywords: mcp,ai,media,video,image,processing
@@ -12,7 +12,6 @@ Classifier: Programming Language :: Python :: 3.12
12
12
  Requires-Python: >=3.12
13
13
  Description-Content-Type: text/markdown
14
14
  Requires-Dist: httpx>=0.28.1
15
- Requires-Dist: mcp[cli]>=1.12.2
16
15
  Requires-Dist: opencv-python>=4.12.0.88
17
16
  Requires-Dist: pillow>=11.3.0
18
17
  Requires-Dist: requests>=2.32.4
@@ -29,6 +28,7 @@ Requires-Dist: Flask>=3.0.0
29
28
  Requires-Dist: pydub>=0.25.1
30
29
  Requires-Dist: audioop-lts; python_version >= "3.13"
31
30
  Requires-Dist: google-genai>=1.33.0
31
+ Requires-Dist: mcp==1.11.0
32
32
 
33
33
  # Media Agent MCP
34
34
 
@@ -1,7 +1,7 @@
1
1
  media_agent_mcp/__init__.py,sha256=4kV5u8kHrsdjpMHDNUhc8h4U6AwOyw1mNptFPd2snrQ,365
2
- media_agent_mcp/async_server.py,sha256=suhjcxjolzojUnLGEeKwsyf2OsT-O0xK6K4xqm1w5CA,25324
2
+ media_agent_mcp/async_server.py,sha256=OXbeOpzWq-8v_b6vnerT1cIyk3bXTiFy9Rp9gl7tK6E,19907
3
3
  media_agent_mcp/async_wrapper.py,sha256=hiiBhhz9WeVDfSBWVh6ovhf5jeP5ZbsieBbz9P-KPn0,15351
4
- media_agent_mcp/server.py,sha256=TAH88BDip7m3R0kNDz89RSuK6Vt8f-v8KeM5bgeSx1M,23104
4
+ media_agent_mcp/server.py,sha256=hZ_m7HodF_ct_q3FXuCrjE6_tw6rXOim38q2sSdTI9I,19976
5
5
  media_agent_mcp/ai_models/__init__.py,sha256=2kHzTYwjQw89U4QGDq0e2WqJScqDkDNlDaWHGak5JeY,553
6
6
  media_agent_mcp/ai_models/omni_human.py,sha256=s_Ja4gEmHG_bgii1x4SoeV73lz0Zg_3iBRvu36goxVA,4643
7
7
  media_agent_mcp/ai_models/openaiedit.py,sha256=uu4d2BgXSrjWRdNPs_SryI9muxO93pItVtEze9nDhjc,9776
@@ -44,8 +44,8 @@ media_agent_mcp/video/__init__.py,sha256=tfz22XEeFSeuKa3AggYCE0vCDt4IwXRCKW6avof
44
44
  media_agent_mcp/video/processor.py,sha256=twfqmN5DbVryjDawZUcqTUcnglcBJYpUbAnApqHgD0c,12787
45
45
  media_agent_mcp/video/stack.py,sha256=pyoJiJ9NhU1tjy2l3kARI9sWFoC00Fj97psxYOBi2NU,1736
46
46
  media_agent_mcp/video/subtitle.py,sha256=TlrWVhWJqYTUJpnVz7eccwMAn8ixfrRzRxS6ETMY-DM,16323
47
- media_agent_mcp-2.6.6.dist-info/METADATA,sha256=LVviMD07NXIxSmeG6yXohs0hoVSoVdKSO0f03fEZNVQ,11310
48
- media_agent_mcp-2.6.6.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
49
- media_agent_mcp-2.6.6.dist-info/entry_points.txt,sha256=qhOUwR-ORVf9GO7emhhl7Lgd6MISgqbZr8bEuSH_VdA,70
50
- media_agent_mcp-2.6.6.dist-info/top_level.txt,sha256=WEa0YfchpTxZgiKn8gdxYgs-dir5HepJaTOrxAGx9nY,16
51
- media_agent_mcp-2.6.6.dist-info/RECORD,,
47
+ media_agent_mcp-2.6.9.dist-info/METADATA,sha256=v9oWNaTmOcNrbn60b79PdNO8miLvXn5xda48kGYflf8,11305
48
+ media_agent_mcp-2.6.9.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
49
+ media_agent_mcp-2.6.9.dist-info/entry_points.txt,sha256=qhOUwR-ORVf9GO7emhhl7Lgd6MISgqbZr8bEuSH_VdA,70
50
+ media_agent_mcp-2.6.9.dist-info/top_level.txt,sha256=WEa0YfchpTxZgiKn8gdxYgs-dir5HepJaTOrxAGx9nY,16
51
+ media_agent_mcp-2.6.9.dist-info/RECORD,,