media-agent-mcp 2.6.5__py3-none-any.whl → 2.6.6__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.
- media_agent_mcp/async_server.py +79 -1
- media_agent_mcp/server.py +72 -35
- {media_agent_mcp-2.6.5.dist-info → media_agent_mcp-2.6.6.dist-info}/METADATA +1 -1
- {media_agent_mcp-2.6.5.dist-info → media_agent_mcp-2.6.6.dist-info}/RECORD +7 -7
- {media_agent_mcp-2.6.5.dist-info → media_agent_mcp-2.6.6.dist-info}/WHEEL +0 -0
- {media_agent_mcp-2.6.5.dist-info → media_agent_mcp-2.6.6.dist-info}/entry_points.txt +0 -0
- {media_agent_mcp-2.6.5.dist-info → media_agent_mcp-2.6.6.dist-info}/top_level.txt +0 -0
media_agent_mcp/async_server.py
CHANGED
@@ -26,6 +26,10 @@ 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
|
29
33
|
|
30
34
|
def async_retry(max_retries=3, delay=2):
|
31
35
|
def decorator(func):
|
@@ -132,6 +136,80 @@ class ReconnectableSSEMiddleware:
|
|
132
136
|
# 不抛出异常,允许客户端重连
|
133
137
|
return
|
134
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
|
+
|
135
213
|
# Initialize FastMCP server (will be configured in main function)
|
136
214
|
load_dotenv()
|
137
215
|
mcp = FastMCP("Media-Agent-MCP-Async")
|
@@ -576,7 +654,7 @@ def main():
|
|
576
654
|
mcp.settings.port = args.port
|
577
655
|
# Use uvicorn to run SSE app with extended keep-alive timeout (5 minutes)
|
578
656
|
uvicorn.run(
|
579
|
-
|
657
|
+
RestartOnErrorMiddleware(mcp.sse_app()),
|
580
658
|
host=args.host,
|
581
659
|
port=args.port,
|
582
660
|
timeout_keep_alive=300
|
media_agent_mcp/server.py
CHANGED
@@ -22,6 +22,11 @@ from dotenv import load_dotenv
|
|
22
22
|
import uvicorn
|
23
23
|
import anyio
|
24
24
|
import uuid
|
25
|
+
import os
|
26
|
+
import sys
|
27
|
+
import signal
|
28
|
+
import subprocess
|
29
|
+
import asyncio
|
25
30
|
|
26
31
|
from mcp.server.fastmcp import FastMCP
|
27
32
|
|
@@ -57,46 +62,78 @@ class IgnoreClosedResourceErrorMiddleware:
|
|
57
62
|
class ReconnectableSSEMiddleware:
|
58
63
|
def __init__(self, app):
|
59
64
|
self.app = app
|
60
|
-
self.connections = {}
|
65
|
+
self.connections = {} # 存储活跃连接
|
61
66
|
|
62
67
|
async def __call__(self, scope, receive, send):
|
63
|
-
#
|
64
|
-
connection_id = str(
|
68
|
+
# 为每个连接生成唯一ID
|
69
|
+
connection_id = scope.get('client', ('unknown', 0))[0] + ':' + str(scope.get('client', ('unknown', 0))[1])
|
65
70
|
|
66
|
-
#
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
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}")
|
76
84
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
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
|
83
95
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
# For non-HTTP connections, just pass through
|
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:
|
99
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
|
+
return
|
100
137
|
|
101
138
|
# Initialize FastMCP server (will be configured in main function)
|
102
139
|
load_dotenv()
|
@@ -636,7 +673,7 @@ def main():
|
|
636
673
|
# Configure and run the server
|
637
674
|
if args.transport == 'sse':
|
638
675
|
# SSE transport
|
639
|
-
uvicorn.run(
|
676
|
+
uvicorn.run(RestartOnErrorMiddleware(mcp.create_sse_app()), host=args.host, port=args.port)
|
640
677
|
else:
|
641
678
|
# STDIO transport (default)
|
642
679
|
mcp.run()
|
@@ -1,7 +1,7 @@
|
|
1
1
|
media_agent_mcp/__init__.py,sha256=4kV5u8kHrsdjpMHDNUhc8h4U6AwOyw1mNptFPd2snrQ,365
|
2
|
-
media_agent_mcp/async_server.py,sha256=
|
2
|
+
media_agent_mcp/async_server.py,sha256=suhjcxjolzojUnLGEeKwsyf2OsT-O0xK6K4xqm1w5CA,25324
|
3
3
|
media_agent_mcp/async_wrapper.py,sha256=hiiBhhz9WeVDfSBWVh6ovhf5jeP5ZbsieBbz9P-KPn0,15351
|
4
|
-
media_agent_mcp/server.py,sha256=
|
4
|
+
media_agent_mcp/server.py,sha256=TAH88BDip7m3R0kNDz89RSuK6Vt8f-v8KeM5bgeSx1M,23104
|
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.
|
48
|
-
media_agent_mcp-2.6.
|
49
|
-
media_agent_mcp-2.6.
|
50
|
-
media_agent_mcp-2.6.
|
51
|
-
media_agent_mcp-2.6.
|
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,,
|
File without changes
|
File without changes
|
File without changes
|