lfx-nightly 0.2.0.dev0__py3-none-any.whl → 0.2.0.dev26__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.
- lfx/_assets/component_index.json +1 -1
- lfx/base/agents/agent.py +13 -1
- lfx/base/agents/altk_base_agent.py +380 -0
- lfx/base/agents/altk_tool_wrappers.py +565 -0
- lfx/base/agents/events.py +2 -1
- lfx/base/composio/composio_base.py +159 -224
- lfx/base/data/base_file.py +88 -21
- lfx/base/data/storage_utils.py +192 -0
- lfx/base/data/utils.py +178 -14
- lfx/base/embeddings/embeddings_class.py +113 -0
- lfx/base/models/groq_constants.py +74 -58
- lfx/base/models/groq_model_discovery.py +265 -0
- lfx/base/models/model.py +1 -1
- lfx/base/models/model_utils.py +100 -0
- lfx/base/models/openai_constants.py +7 -0
- lfx/base/models/watsonx_constants.py +32 -8
- lfx/base/tools/run_flow.py +601 -129
- lfx/cli/commands.py +6 -3
- lfx/cli/common.py +2 -2
- lfx/cli/run.py +1 -1
- lfx/cli/script_loader.py +53 -11
- lfx/components/Notion/create_page.py +1 -1
- lfx/components/Notion/list_database_properties.py +1 -1
- lfx/components/Notion/list_pages.py +1 -1
- lfx/components/Notion/list_users.py +1 -1
- lfx/components/Notion/page_content_viewer.py +1 -1
- lfx/components/Notion/search.py +1 -1
- lfx/components/Notion/update_page_property.py +1 -1
- lfx/components/__init__.py +19 -5
- lfx/components/{agents → altk}/__init__.py +5 -9
- lfx/components/altk/altk_agent.py +193 -0
- lfx/components/apify/apify_actor.py +1 -1
- lfx/components/composio/__init__.py +70 -18
- lfx/components/composio/apollo_composio.py +11 -0
- lfx/components/composio/bitbucket_composio.py +11 -0
- lfx/components/composio/canva_composio.py +11 -0
- lfx/components/composio/coda_composio.py +11 -0
- lfx/components/composio/composio_api.py +10 -0
- lfx/components/composio/discord_composio.py +1 -1
- lfx/components/composio/elevenlabs_composio.py +11 -0
- lfx/components/composio/exa_composio.py +11 -0
- lfx/components/composio/firecrawl_composio.py +11 -0
- lfx/components/composio/fireflies_composio.py +11 -0
- lfx/components/composio/gmail_composio.py +1 -1
- lfx/components/composio/googlebigquery_composio.py +11 -0
- lfx/components/composio/googlecalendar_composio.py +1 -1
- lfx/components/composio/googledocs_composio.py +1 -1
- lfx/components/composio/googlemeet_composio.py +1 -1
- lfx/components/composio/googlesheets_composio.py +1 -1
- lfx/components/composio/googletasks_composio.py +1 -1
- lfx/components/composio/heygen_composio.py +11 -0
- lfx/components/composio/mem0_composio.py +11 -0
- lfx/components/composio/peopledatalabs_composio.py +11 -0
- lfx/components/composio/perplexityai_composio.py +11 -0
- lfx/components/composio/serpapi_composio.py +11 -0
- lfx/components/composio/slack_composio.py +3 -574
- lfx/components/composio/slackbot_composio.py +1 -1
- lfx/components/composio/snowflake_composio.py +11 -0
- lfx/components/composio/tavily_composio.py +11 -0
- lfx/components/composio/youtube_composio.py +2 -2
- lfx/components/cuga/__init__.py +34 -0
- lfx/components/cuga/cuga_agent.py +730 -0
- lfx/components/data/__init__.py +78 -28
- lfx/components/data_source/__init__.py +58 -0
- lfx/components/{data → data_source}/api_request.py +26 -3
- lfx/components/{data → data_source}/csv_to_data.py +15 -10
- lfx/components/{data → data_source}/json_to_data.py +15 -8
- lfx/components/{data → data_source}/news_search.py +1 -1
- lfx/components/{data → data_source}/rss.py +1 -1
- lfx/components/{data → data_source}/sql_executor.py +1 -1
- lfx/components/{data → data_source}/url.py +1 -1
- lfx/components/{data → data_source}/web_search.py +1 -1
- lfx/components/datastax/astradb_cql.py +1 -1
- lfx/components/datastax/astradb_graph.py +1 -1
- lfx/components/datastax/astradb_tool.py +1 -1
- lfx/components/datastax/astradb_vectorstore.py +1 -1
- lfx/components/datastax/hcd.py +1 -1
- lfx/components/deactivated/json_document_builder.py +1 -1
- lfx/components/docling/__init__.py +0 -3
- lfx/components/elastic/elasticsearch.py +1 -1
- lfx/components/elastic/opensearch_multimodal.py +1575 -0
- lfx/components/files_and_knowledge/__init__.py +47 -0
- lfx/components/{data → files_and_knowledge}/directory.py +1 -1
- lfx/components/{data → files_and_knowledge}/file.py +246 -18
- lfx/components/{knowledge_bases → files_and_knowledge}/retrieval.py +2 -2
- lfx/components/{data → files_and_knowledge}/save_file.py +142 -22
- lfx/components/flow_controls/__init__.py +58 -0
- lfx/components/{logic → flow_controls}/conditional_router.py +1 -1
- lfx/components/{logic → flow_controls}/loop.py +43 -9
- lfx/components/flow_controls/run_flow.py +108 -0
- lfx/components/glean/glean_search_api.py +1 -1
- lfx/components/groq/groq.py +35 -28
- lfx/components/helpers/__init__.py +102 -0
- lfx/components/input_output/__init__.py +3 -1
- lfx/components/input_output/chat.py +4 -3
- lfx/components/input_output/chat_output.py +4 -4
- lfx/components/input_output/text.py +1 -1
- lfx/components/input_output/text_output.py +1 -1
- lfx/components/{data → input_output}/webhook.py +1 -1
- lfx/components/knowledge_bases/__init__.py +59 -4
- lfx/components/langchain_utilities/character.py +1 -1
- lfx/components/langchain_utilities/csv_agent.py +84 -16
- lfx/components/langchain_utilities/json_agent.py +67 -12
- lfx/components/langchain_utilities/language_recursive.py +1 -1
- lfx/components/llm_operations/__init__.py +46 -0
- lfx/components/{processing → llm_operations}/batch_run.py +1 -1
- lfx/components/{processing → llm_operations}/lambda_filter.py +1 -1
- lfx/components/{logic → llm_operations}/llm_conditional_router.py +1 -1
- lfx/components/{processing/llm_router.py → llm_operations/llm_selector.py} +3 -3
- lfx/components/{processing → llm_operations}/structured_output.py +1 -1
- lfx/components/logic/__init__.py +126 -0
- lfx/components/mem0/mem0_chat_memory.py +11 -0
- lfx/components/models/__init__.py +64 -9
- lfx/components/models_and_agents/__init__.py +49 -0
- lfx/components/{agents → models_and_agents}/agent.py +2 -2
- lfx/components/models_and_agents/embedding_model.py +423 -0
- lfx/components/models_and_agents/language_model.py +398 -0
- lfx/components/{agents → models_and_agents}/mcp_component.py +53 -44
- lfx/components/{helpers → models_and_agents}/memory.py +1 -1
- lfx/components/nvidia/system_assist.py +1 -1
- lfx/components/olivya/olivya.py +1 -1
- lfx/components/ollama/ollama.py +17 -3
- lfx/components/processing/__init__.py +9 -57
- lfx/components/processing/converter.py +1 -1
- lfx/components/processing/dataframe_operations.py +1 -1
- lfx/components/processing/parse_json_data.py +2 -2
- lfx/components/processing/parser.py +1 -1
- lfx/components/processing/split_text.py +1 -1
- lfx/components/qdrant/qdrant.py +1 -1
- lfx/components/redis/redis.py +1 -1
- lfx/components/twelvelabs/split_video.py +10 -0
- lfx/components/twelvelabs/video_file.py +12 -0
- lfx/components/utilities/__init__.py +43 -0
- lfx/components/{helpers → utilities}/calculator_core.py +1 -1
- lfx/components/{helpers → utilities}/current_date.py +1 -1
- lfx/components/{processing → utilities}/python_repl_core.py +1 -1
- lfx/components/vectorstores/local_db.py +9 -0
- lfx/components/youtube/youtube_transcripts.py +118 -30
- lfx/custom/custom_component/component.py +57 -1
- lfx/custom/custom_component/custom_component.py +68 -6
- lfx/graph/edge/base.py +43 -20
- lfx/graph/graph/base.py +4 -1
- lfx/graph/state/model.py +15 -2
- lfx/graph/utils.py +6 -0
- lfx/graph/vertex/base.py +4 -1
- lfx/graph/vertex/param_handler.py +10 -7
- lfx/helpers/__init__.py +12 -0
- lfx/helpers/flow.py +117 -0
- lfx/inputs/input_mixin.py +24 -1
- lfx/inputs/inputs.py +13 -1
- lfx/interface/components.py +161 -83
- lfx/log/logger.py +5 -3
- lfx/services/database/__init__.py +5 -0
- lfx/services/database/service.py +25 -0
- lfx/services/deps.py +87 -22
- lfx/services/manager.py +19 -6
- lfx/services/mcp_composer/service.py +998 -157
- lfx/services/session.py +5 -0
- lfx/services/settings/base.py +51 -7
- lfx/services/settings/constants.py +8 -0
- lfx/services/storage/local.py +76 -46
- lfx/services/storage/service.py +152 -29
- lfx/template/field/base.py +3 -0
- lfx/utils/ssrf_protection.py +384 -0
- lfx/utils/validate_cloud.py +26 -0
- {lfx_nightly-0.2.0.dev0.dist-info → lfx_nightly-0.2.0.dev26.dist-info}/METADATA +38 -22
- {lfx_nightly-0.2.0.dev0.dist-info → lfx_nightly-0.2.0.dev26.dist-info}/RECORD +182 -150
- {lfx_nightly-0.2.0.dev0.dist-info → lfx_nightly-0.2.0.dev26.dist-info}/WHEEL +1 -1
- lfx/components/agents/altk_agent.py +0 -366
- lfx/components/agents/cuga_agent.py +0 -1013
- lfx/components/docling/docling_remote_vlm.py +0 -284
- lfx/components/logic/run_flow.py +0 -71
- lfx/components/models/embedding_model.py +0 -195
- lfx/components/models/language_model.py +0 -144
- /lfx/components/{data → data_source}/mock_data.py +0 -0
- /lfx/components/{knowledge_bases → files_and_knowledge}/ingestion.py +0 -0
- /lfx/components/{logic → flow_controls}/data_conditional_router.py +0 -0
- /lfx/components/{logic → flow_controls}/flow_tool.py +0 -0
- /lfx/components/{logic → flow_controls}/listen.py +0 -0
- /lfx/components/{logic → flow_controls}/notify.py +0 -0
- /lfx/components/{logic → flow_controls}/pass_message.py +0 -0
- /lfx/components/{logic → flow_controls}/sub_flow.py +0 -0
- /lfx/components/{processing → models_and_agents}/prompt.py +0 -0
- /lfx/components/{helpers → processing}/create_list.py +0 -0
- /lfx/components/{helpers → processing}/output_parser.py +0 -0
- /lfx/components/{helpers → processing}/store_message.py +0 -0
- /lfx/components/{helpers → utilities}/id_generator.py +0 -0
- {lfx_nightly-0.2.0.dev0.dist-info → lfx_nightly-0.2.0.dev26.dist-info}/entry_points.txt +0 -0
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
"""MCP Composer service for proxying and orchestrating MCP servers."""
|
|
2
2
|
|
|
3
3
|
import asyncio
|
|
4
|
+
import json
|
|
4
5
|
import os
|
|
6
|
+
import platform
|
|
5
7
|
import re
|
|
6
8
|
import select
|
|
7
9
|
import socket
|
|
8
10
|
import subprocess
|
|
11
|
+
import tempfile
|
|
12
|
+
import typing
|
|
9
13
|
from collections.abc import Callable
|
|
10
14
|
from functools import wraps
|
|
15
|
+
from pathlib import Path
|
|
11
16
|
from typing import Any
|
|
12
17
|
|
|
13
18
|
from lfx.log.logger import logger
|
|
@@ -72,16 +77,373 @@ class MCPComposerService(Service):
|
|
|
72
77
|
self._start_locks: dict[
|
|
73
78
|
str, asyncio.Lock
|
|
74
79
|
] = {} # Lock to prevent concurrent start operations for the same project
|
|
80
|
+
self._active_start_tasks: dict[
|
|
81
|
+
str, asyncio.Task
|
|
82
|
+
] = {} # Track active start tasks to cancel them when new request arrives
|
|
83
|
+
self._port_to_project: dict[int, str] = {} # Track which project is using which port
|
|
84
|
+
self._pid_to_project: dict[int, str] = {} # Track which PID belongs to which project
|
|
85
|
+
self._last_errors: dict[str, str] = {} # Track last error message per project for UI display
|
|
75
86
|
|
|
76
|
-
def
|
|
77
|
-
"""
|
|
87
|
+
def get_last_error(self, project_id: str) -> str | None:
|
|
88
|
+
"""Get the last error message for a project, if any."""
|
|
89
|
+
return self._last_errors.get(project_id)
|
|
90
|
+
|
|
91
|
+
def set_last_error(self, project_id: str, error_message: str) -> None:
|
|
92
|
+
"""Set the last error message for a project."""
|
|
93
|
+
self._last_errors[project_id] = error_message
|
|
94
|
+
|
|
95
|
+
def clear_last_error(self, project_id: str) -> None:
|
|
96
|
+
"""Clear the last error message for a project."""
|
|
97
|
+
self._last_errors.pop(project_id, None)
|
|
98
|
+
|
|
99
|
+
def _is_port_available(self, port: int, host: str = "localhost") -> bool:
|
|
100
|
+
"""Check if a port is available by trying to bind to it.
|
|
101
|
+
|
|
102
|
+
Args:
|
|
103
|
+
port: Port number to check
|
|
104
|
+
host: Host to check (default: localhost)
|
|
105
|
+
|
|
106
|
+
Returns:
|
|
107
|
+
True if port is available (not in use), False if in use
|
|
108
|
+
|
|
109
|
+
Raises:
|
|
110
|
+
ValueError: If port is not in valid range (0-65535)
|
|
111
|
+
"""
|
|
112
|
+
import errno
|
|
113
|
+
|
|
114
|
+
# Validate port range before attempting bind
|
|
115
|
+
max_port = 65535
|
|
116
|
+
if not isinstance(port, int) or port < 0 or port > max_port:
|
|
117
|
+
msg = f"Invalid port number: {port}. Port must be between 0 and {max_port}."
|
|
118
|
+
raise ValueError(msg)
|
|
119
|
+
|
|
120
|
+
# Check both IPv4 and IPv6 to ensure port is truly available
|
|
121
|
+
# MCP Composer tries to bind on both, so we need to check both
|
|
122
|
+
|
|
123
|
+
# Check IPv4
|
|
78
124
|
try:
|
|
79
125
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
|
80
|
-
|
|
81
|
-
sock.bind((
|
|
82
|
-
return True # Port is available
|
|
126
|
+
# Don't use SO_REUSEADDR here as it can give false positives
|
|
127
|
+
sock.bind((host, port))
|
|
83
128
|
except OSError:
|
|
84
|
-
return False # Port is in use
|
|
129
|
+
return False # Port is in use on IPv4
|
|
130
|
+
|
|
131
|
+
# Check IPv6 (if supported on this system)
|
|
132
|
+
try:
|
|
133
|
+
with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as sock:
|
|
134
|
+
# Don't use SO_REUSEADDR here as it can give false positives
|
|
135
|
+
# Use ::1 for localhost on IPv6
|
|
136
|
+
ipv6_host = "::1" if host in ("localhost", "127.0.0.1") else host
|
|
137
|
+
sock.bind((ipv6_host, port))
|
|
138
|
+
except OSError as e:
|
|
139
|
+
# Check if it's "address already in use" error
|
|
140
|
+
# errno.EADDRINUSE is 48 on macOS, 98 on Linux, 10048 on Windows (WSAEADDRINUSE)
|
|
141
|
+
# We check both the standard errno and Windows-specific error code
|
|
142
|
+
if e.errno in (errno.EADDRINUSE, 10048):
|
|
143
|
+
return False # Port is in use on IPv6
|
|
144
|
+
# For other errors (e.g., IPv6 not supported, EADDRNOTAVAIL), continue
|
|
145
|
+
# IPv6 might not be supported on this system, which is okay
|
|
146
|
+
|
|
147
|
+
return True # Port is available on both IPv4 and IPv6 (or IPv6 not supported)
|
|
148
|
+
|
|
149
|
+
async def _kill_process_on_port(self, port: int) -> bool:
|
|
150
|
+
"""Kill the process using the specified port.
|
|
151
|
+
|
|
152
|
+
Cross-platform implementation supporting Windows, macOS, and Linux.
|
|
153
|
+
|
|
154
|
+
Args:
|
|
155
|
+
port: The port number to check
|
|
156
|
+
|
|
157
|
+
Returns:
|
|
158
|
+
True if a process was found and killed, False otherwise
|
|
159
|
+
"""
|
|
160
|
+
try:
|
|
161
|
+
await logger.adebug(f"Checking for processes using port {port}...")
|
|
162
|
+
os_type = platform.system()
|
|
163
|
+
|
|
164
|
+
# Platform-specific command to find PID
|
|
165
|
+
if os_type == "Windows":
|
|
166
|
+
# Use netstat on Windows - use full path to avoid PATH issues
|
|
167
|
+
netstat_cmd = os.path.join(os.environ.get("SYSTEMROOT", "C:\\Windows"), "System32", "netstat.exe") # noqa: PTH118
|
|
168
|
+
result = await asyncio.to_thread(
|
|
169
|
+
subprocess.run,
|
|
170
|
+
[netstat_cmd, "-ano"],
|
|
171
|
+
capture_output=True,
|
|
172
|
+
text=True,
|
|
173
|
+
check=False,
|
|
174
|
+
)
|
|
175
|
+
|
|
176
|
+
if result.returncode == 0:
|
|
177
|
+
# Parse netstat output to find PID
|
|
178
|
+
# Format: TCP 0.0.0.0:PORT 0.0.0.0:0 LISTENING PID
|
|
179
|
+
windows_pids: list[int] = []
|
|
180
|
+
for line in result.stdout.split("\n"):
|
|
181
|
+
if f":{port}" in line and "LISTENING" in line:
|
|
182
|
+
parts = line.split()
|
|
183
|
+
if parts:
|
|
184
|
+
try:
|
|
185
|
+
pid = int(parts[-1])
|
|
186
|
+
windows_pids.append(pid)
|
|
187
|
+
except (ValueError, IndexError):
|
|
188
|
+
continue
|
|
189
|
+
|
|
190
|
+
await logger.adebug(f"Found {len(windows_pids)} process(es) using port {port}: {windows_pids}")
|
|
191
|
+
|
|
192
|
+
for pid in windows_pids:
|
|
193
|
+
try:
|
|
194
|
+
await logger.adebug(f"Attempting to kill process {pid} on port {port}...")
|
|
195
|
+
# Use taskkill on Windows - use full path to avoid PATH issues
|
|
196
|
+
taskkill_cmd = os.path.join( # noqa: PTH118
|
|
197
|
+
os.environ.get("SYSTEMROOT", "C:\\Windows"), "System32", "taskkill.exe"
|
|
198
|
+
)
|
|
199
|
+
kill_result = await asyncio.to_thread(
|
|
200
|
+
subprocess.run,
|
|
201
|
+
[taskkill_cmd, "/F", "/PID", str(pid)],
|
|
202
|
+
capture_output=True,
|
|
203
|
+
check=False,
|
|
204
|
+
)
|
|
205
|
+
|
|
206
|
+
if kill_result.returncode == 0:
|
|
207
|
+
await logger.adebug(f"Successfully killed process {pid} on port {port}")
|
|
208
|
+
return True
|
|
209
|
+
await logger.awarning(
|
|
210
|
+
f"taskkill returned {kill_result.returncode} for process {pid} on port {port}"
|
|
211
|
+
)
|
|
212
|
+
except Exception as e: # noqa: BLE001
|
|
213
|
+
await logger.aerror(f"Error killing PID {pid}: {e}")
|
|
214
|
+
|
|
215
|
+
return False
|
|
216
|
+
else:
|
|
217
|
+
# Use lsof on Unix-like systems (macOS, Linux)
|
|
218
|
+
result = await asyncio.to_thread(
|
|
219
|
+
subprocess.run,
|
|
220
|
+
["lsof", "-ti", f":{port}"],
|
|
221
|
+
capture_output=True,
|
|
222
|
+
text=True,
|
|
223
|
+
check=False,
|
|
224
|
+
)
|
|
225
|
+
|
|
226
|
+
await logger.adebug(f"lsof returned code {result.returncode} for port {port}")
|
|
227
|
+
|
|
228
|
+
# Extract PIDs from lsof output
|
|
229
|
+
lsof_output = result.stdout.strip()
|
|
230
|
+
lsof_errors = result.stderr.strip()
|
|
231
|
+
|
|
232
|
+
if lsof_output:
|
|
233
|
+
await logger.adebug(f"lsof stdout: {lsof_output}")
|
|
234
|
+
if lsof_errors:
|
|
235
|
+
await logger.adebug(f"lsof stderr: {lsof_errors}")
|
|
236
|
+
|
|
237
|
+
if result.returncode == 0 and lsof_output:
|
|
238
|
+
unix_pids = lsof_output.split("\n")
|
|
239
|
+
await logger.adebug(f"Found {len(unix_pids)} process(es) using port {port}: {unix_pids}")
|
|
240
|
+
|
|
241
|
+
for pid_str in unix_pids:
|
|
242
|
+
try:
|
|
243
|
+
pid = int(pid_str.strip())
|
|
244
|
+
await logger.adebug(f"Attempting to kill process {pid} on port {port}...")
|
|
245
|
+
|
|
246
|
+
# Try to kill the process
|
|
247
|
+
kill_result = await asyncio.to_thread(
|
|
248
|
+
subprocess.run,
|
|
249
|
+
["kill", "-9", str(pid)],
|
|
250
|
+
capture_output=True,
|
|
251
|
+
check=False,
|
|
252
|
+
)
|
|
253
|
+
|
|
254
|
+
if kill_result.returncode == 0:
|
|
255
|
+
await logger.adebug(f"Successfully sent kill signal to process {pid} on port {port}")
|
|
256
|
+
return True
|
|
257
|
+
await logger.awarning(
|
|
258
|
+
f"kill command returned {kill_result.returncode} for process {pid} on port {port}"
|
|
259
|
+
)
|
|
260
|
+
except (ValueError, ProcessLookupError) as e:
|
|
261
|
+
await logger.aerror(f"Error processing PID {pid_str}: {e}")
|
|
262
|
+
|
|
263
|
+
# If we get here, we found processes but couldn't kill any
|
|
264
|
+
return False
|
|
265
|
+
await logger.adebug(f"No process found using port {port}")
|
|
266
|
+
return False
|
|
267
|
+
except Exception as e: # noqa: BLE001
|
|
268
|
+
await logger.aerror(f"Error finding/killing process on port {port}: {e}")
|
|
269
|
+
return False
|
|
270
|
+
return False
|
|
271
|
+
|
|
272
|
+
async def _kill_zombie_mcp_processes(self, port: int) -> bool:
|
|
273
|
+
"""Kill zombie MCP Composer processes that may be stuck.
|
|
274
|
+
|
|
275
|
+
On Windows, sometimes MCP Composer processes start but fail to bind to port.
|
|
276
|
+
These processes become "zombies" that need to be killed before retry.
|
|
277
|
+
|
|
278
|
+
Args:
|
|
279
|
+
port: The port that should be used
|
|
280
|
+
|
|
281
|
+
Returns:
|
|
282
|
+
True if zombie processes were found and killed
|
|
283
|
+
"""
|
|
284
|
+
try:
|
|
285
|
+
os_type = platform.system()
|
|
286
|
+
if os_type != "Windows":
|
|
287
|
+
return False
|
|
288
|
+
|
|
289
|
+
await logger.adebug(f"Looking for zombie MCP Composer processes on Windows for port {port}...")
|
|
290
|
+
|
|
291
|
+
# First, try to find and kill any process using the port directly
|
|
292
|
+
# Use full path to netstat on Windows to avoid PATH issues
|
|
293
|
+
netstat_cmd = os.path.join(os.environ.get("SYSTEMROOT", "C:\\Windows"), "System32", "netstat.exe") # noqa: PTH118
|
|
294
|
+
netstat_result = await asyncio.to_thread(
|
|
295
|
+
subprocess.run,
|
|
296
|
+
[netstat_cmd, "-ano"],
|
|
297
|
+
capture_output=True,
|
|
298
|
+
text=True,
|
|
299
|
+
check=False,
|
|
300
|
+
)
|
|
301
|
+
|
|
302
|
+
killed_any = False
|
|
303
|
+
if netstat_result.returncode == 0:
|
|
304
|
+
# Parse netstat output to find PIDs using our port
|
|
305
|
+
pids_on_port: list[int] = []
|
|
306
|
+
for line in netstat_result.stdout.split("\n"):
|
|
307
|
+
if f":{port}" in line and "LISTENING" in line:
|
|
308
|
+
parts = line.split()
|
|
309
|
+
if parts:
|
|
310
|
+
try:
|
|
311
|
+
pid = int(parts[-1])
|
|
312
|
+
# Only kill if not tracked by us
|
|
313
|
+
if pid not in self._pid_to_project:
|
|
314
|
+
pids_on_port.append(pid)
|
|
315
|
+
else:
|
|
316
|
+
project = self._pid_to_project[pid]
|
|
317
|
+
await logger.adebug(
|
|
318
|
+
f"Process {pid} on port {port} is tracked, skipping (project: {project})"
|
|
319
|
+
)
|
|
320
|
+
except (ValueError, IndexError):
|
|
321
|
+
continue
|
|
322
|
+
|
|
323
|
+
if pids_on_port:
|
|
324
|
+
await logger.adebug(
|
|
325
|
+
f"Found {len(pids_on_port)} untracked process(es) on port {port}: {pids_on_port}"
|
|
326
|
+
)
|
|
327
|
+
for pid in pids_on_port:
|
|
328
|
+
try:
|
|
329
|
+
await logger.adebug(f"Killing process {pid} on port {port}...")
|
|
330
|
+
# Use full path to taskkill on Windows to avoid PATH issues
|
|
331
|
+
taskkill_cmd = os.path.join( # noqa: PTH118
|
|
332
|
+
os.environ.get("SYSTEMROOT", "C:\\Windows"), "System32", "taskkill.exe"
|
|
333
|
+
)
|
|
334
|
+
kill_result = await asyncio.to_thread(
|
|
335
|
+
subprocess.run,
|
|
336
|
+
[taskkill_cmd, "/F", "/PID", str(pid)],
|
|
337
|
+
capture_output=True,
|
|
338
|
+
check=False,
|
|
339
|
+
)
|
|
340
|
+
if kill_result.returncode == 0:
|
|
341
|
+
await logger.adebug(f"Successfully killed process {pid} on port {port}")
|
|
342
|
+
killed_any = True
|
|
343
|
+
else:
|
|
344
|
+
stderr_output = (
|
|
345
|
+
kill_result.stderr.decode()
|
|
346
|
+
if isinstance(kill_result.stderr, bytes)
|
|
347
|
+
else kill_result.stderr
|
|
348
|
+
)
|
|
349
|
+
await logger.awarning(f"Failed to kill process {pid} on port {port}: {stderr_output}")
|
|
350
|
+
except Exception as e: # noqa: BLE001
|
|
351
|
+
await logger.adebug(f"Error killing process {pid}: {e}")
|
|
352
|
+
|
|
353
|
+
# Also look for any orphaned mcp-composer processes (without checking port)
|
|
354
|
+
# This catches processes that failed to bind but are still running
|
|
355
|
+
# Use PowerShell instead of deprecated wmic.exe for Windows 10/11 compatibility
|
|
356
|
+
try:
|
|
357
|
+
# Use PowerShell to get Python processes with command line info
|
|
358
|
+
# Build PowerShell command to find MCP Composer processes
|
|
359
|
+
ps_filter = (
|
|
360
|
+
f"$_.Name -eq 'python.exe' -and $_.CommandLine -like '*mcp-composer*' "
|
|
361
|
+
f"-and ($_.CommandLine -like '*--port {port}*' -or $_.CommandLine -like '*--port={port}*')"
|
|
362
|
+
)
|
|
363
|
+
ps_cmd = (
|
|
364
|
+
f"Get-WmiObject Win32_Process | Where-Object {{ {ps_filter} }} | "
|
|
365
|
+
"Select-Object ProcessId,CommandLine | ConvertTo-Json"
|
|
366
|
+
)
|
|
367
|
+
powershell_cmd = ["powershell.exe", "-NoProfile", "-Command", ps_cmd]
|
|
368
|
+
|
|
369
|
+
ps_result = await asyncio.to_thread(
|
|
370
|
+
subprocess.run,
|
|
371
|
+
powershell_cmd,
|
|
372
|
+
capture_output=True,
|
|
373
|
+
text=True,
|
|
374
|
+
check=False,
|
|
375
|
+
timeout=5,
|
|
376
|
+
)
|
|
377
|
+
|
|
378
|
+
if ps_result.returncode == 0 and ps_result.stdout.strip():
|
|
379
|
+
try:
|
|
380
|
+
# PowerShell may return single object or array
|
|
381
|
+
processes = json.loads(ps_result.stdout)
|
|
382
|
+
if isinstance(processes, dict):
|
|
383
|
+
processes = [processes]
|
|
384
|
+
elif not isinstance(processes, list):
|
|
385
|
+
processes = []
|
|
386
|
+
|
|
387
|
+
for proc in processes:
|
|
388
|
+
try:
|
|
389
|
+
pid = int(proc.get("ProcessId", 0))
|
|
390
|
+
if pid <= 0 or pid in self._pid_to_project:
|
|
391
|
+
continue
|
|
392
|
+
|
|
393
|
+
await logger.adebug(
|
|
394
|
+
f"Found orphaned MCP Composer process {pid} for port {port}, killing it"
|
|
395
|
+
)
|
|
396
|
+
# Use full path to taskkill on Windows to avoid PATH issues
|
|
397
|
+
taskkill_cmd = os.path.join( # noqa: PTH118
|
|
398
|
+
os.environ.get("SYSTEMROOT", "C:\\Windows"), "System32", "taskkill.exe"
|
|
399
|
+
)
|
|
400
|
+
kill_result = await asyncio.to_thread(
|
|
401
|
+
subprocess.run,
|
|
402
|
+
[taskkill_cmd, "/F", "/PID", str(pid)],
|
|
403
|
+
capture_output=True,
|
|
404
|
+
check=False,
|
|
405
|
+
)
|
|
406
|
+
if kill_result.returncode == 0:
|
|
407
|
+
await logger.adebug(f"Successfully killed orphaned process {pid}")
|
|
408
|
+
killed_any = True
|
|
409
|
+
|
|
410
|
+
except (ValueError, KeyError) as e:
|
|
411
|
+
await logger.adebug(f"Error processing PowerShell result: {e}")
|
|
412
|
+
continue
|
|
413
|
+
|
|
414
|
+
except json.JSONDecodeError as e:
|
|
415
|
+
await logger.adebug(f"Failed to parse PowerShell output: {e}")
|
|
416
|
+
|
|
417
|
+
except asyncio.TimeoutError:
|
|
418
|
+
await logger.adebug("PowerShell command timed out while checking for orphaned processes")
|
|
419
|
+
except Exception as e: # noqa: BLE001
|
|
420
|
+
await logger.adebug(f"Error using PowerShell to find orphaned processes: {e}")
|
|
421
|
+
|
|
422
|
+
if killed_any:
|
|
423
|
+
# Give Windows time to clean up
|
|
424
|
+
await logger.adebug("Waiting 3 seconds for Windows to release port...")
|
|
425
|
+
await asyncio.sleep(3)
|
|
426
|
+
|
|
427
|
+
return killed_any # noqa: TRY300
|
|
428
|
+
|
|
429
|
+
except Exception as e: # noqa: BLE001
|
|
430
|
+
await logger.adebug(f"Error killing zombie processes: {e}")
|
|
431
|
+
return False
|
|
432
|
+
|
|
433
|
+
def _is_port_used_by_another_project(self, port: int, current_project_id: str) -> tuple[bool, str | None]:
|
|
434
|
+
"""Check if a port is being used by another project.
|
|
435
|
+
|
|
436
|
+
Args:
|
|
437
|
+
port: The port to check
|
|
438
|
+
current_project_id: The current project ID
|
|
439
|
+
|
|
440
|
+
Returns:
|
|
441
|
+
Tuple of (is_used_by_other, other_project_id)
|
|
442
|
+
"""
|
|
443
|
+
other_project_id = self._port_to_project.get(port)
|
|
444
|
+
if other_project_id and other_project_id != current_project_id:
|
|
445
|
+
return True, other_project_id
|
|
446
|
+
return False, None
|
|
85
447
|
|
|
86
448
|
async def start(self):
|
|
87
449
|
"""Check if the MCP Composer service is enabled."""
|
|
@@ -125,47 +487,300 @@ class MCPComposerService(Service):
|
|
|
125
487
|
composer_info = self.project_composers[project_id]
|
|
126
488
|
process = composer_info.get("process")
|
|
127
489
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
490
|
+
try:
|
|
491
|
+
if process:
|
|
492
|
+
try:
|
|
493
|
+
# Check if process is still running before trying to terminate
|
|
494
|
+
if process.poll() is None:
|
|
495
|
+
await logger.adebug(f"Terminating MCP Composer process {process.pid} for project {project_id}")
|
|
496
|
+
process.terminate()
|
|
134
497
|
|
|
135
|
-
|
|
136
|
-
try:
|
|
137
|
-
await asyncio.wait_for(self._wait_for_process_exit(process), timeout=3.0)
|
|
138
|
-
await logger.adebug(f"MCP Composer for project {project_id} terminated gracefully")
|
|
139
|
-
except asyncio.TimeoutError:
|
|
140
|
-
await logger.aerror(
|
|
141
|
-
f"MCP Composer for project {project_id} did not terminate gracefully, force killing"
|
|
142
|
-
)
|
|
143
|
-
process.kill()
|
|
144
|
-
# Wait a bit more for force kill to complete
|
|
498
|
+
# Wait longer for graceful shutdown
|
|
145
499
|
try:
|
|
146
|
-
await asyncio.wait_for(
|
|
500
|
+
await asyncio.wait_for(asyncio.to_thread(process.wait), timeout=2.0)
|
|
501
|
+
await logger.adebug(f"MCP Composer for project {project_id} terminated gracefully")
|
|
147
502
|
except asyncio.TimeoutError:
|
|
148
503
|
await logger.aerror(
|
|
149
|
-
f"
|
|
504
|
+
f"MCP Composer for project {project_id} did not terminate gracefully, force killing"
|
|
150
505
|
)
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
506
|
+
await asyncio.to_thread(process.kill)
|
|
507
|
+
await asyncio.to_thread(process.wait)
|
|
508
|
+
else:
|
|
509
|
+
await logger.adebug(f"MCP Composer process for project {project_id} was already terminated")
|
|
155
510
|
|
|
156
|
-
|
|
157
|
-
# Process already terminated
|
|
158
|
-
await logger.adebug(f"MCP Composer process for project {project_id} was already terminated")
|
|
159
|
-
except Exception as e: # noqa: BLE001
|
|
160
|
-
await logger.aerror(f"Error stopping MCP Composer for project {project_id}: {e}")
|
|
511
|
+
await logger.adebug(f"MCP Composer stopped for project {project_id}")
|
|
161
512
|
|
|
162
|
-
|
|
163
|
-
|
|
513
|
+
except ProcessLookupError:
|
|
514
|
+
# Process already terminated
|
|
515
|
+
await logger.adebug(f"MCP Composer process for project {project_id} was already terminated")
|
|
516
|
+
except Exception as e: # noqa: BLE001
|
|
517
|
+
await logger.aerror(f"Error stopping MCP Composer for project {project_id}: {e}")
|
|
518
|
+
finally:
|
|
519
|
+
# Always clean up tracking, even if stopping failed
|
|
520
|
+
port = composer_info.get("port")
|
|
521
|
+
if port and self._port_to_project.get(port) == project_id:
|
|
522
|
+
self._port_to_project.pop(port, None)
|
|
523
|
+
await logger.adebug(f"Released port {port} from project {project_id}")
|
|
524
|
+
|
|
525
|
+
# Clean up PID tracking
|
|
526
|
+
if process and process.pid:
|
|
527
|
+
self._pid_to_project.pop(process.pid, None)
|
|
528
|
+
await logger.adebug(f"Released PID {process.pid} tracking for project {project_id}")
|
|
529
|
+
|
|
530
|
+
# Remove from tracking
|
|
531
|
+
self.project_composers.pop(project_id, None)
|
|
532
|
+
await logger.adebug(f"Removed tracking for project {project_id}")
|
|
164
533
|
|
|
165
534
|
async def _wait_for_process_exit(self, process):
|
|
166
535
|
"""Wait for a process to exit."""
|
|
167
536
|
await asyncio.to_thread(process.wait)
|
|
168
537
|
|
|
538
|
+
async def _read_process_output_and_extract_error(
|
|
539
|
+
self,
|
|
540
|
+
process: subprocess.Popen,
|
|
541
|
+
oauth_server_url: str | None,
|
|
542
|
+
timeout: float = 2.0,
|
|
543
|
+
stdout_file=None,
|
|
544
|
+
stderr_file=None,
|
|
545
|
+
) -> tuple[str, str, str]:
|
|
546
|
+
"""Read process output and extract user-friendly error message.
|
|
547
|
+
|
|
548
|
+
Args:
|
|
549
|
+
process: The subprocess to read from
|
|
550
|
+
oauth_server_url: OAuth server URL for error messages
|
|
551
|
+
timeout: Timeout for reading output
|
|
552
|
+
stdout_file: Optional file handle for stdout (Windows)
|
|
553
|
+
stderr_file: Optional file handle for stderr (Windows)
|
|
554
|
+
|
|
555
|
+
Returns:
|
|
556
|
+
Tuple of (stdout, stderr, error_message)
|
|
557
|
+
"""
|
|
558
|
+
stdout_content = ""
|
|
559
|
+
stderr_content = ""
|
|
560
|
+
|
|
561
|
+
try:
|
|
562
|
+
# On Windows with temp files, read from files instead of pipes
|
|
563
|
+
if stdout_file and stderr_file:
|
|
564
|
+
# Close file handles to flush and allow reading
|
|
565
|
+
try:
|
|
566
|
+
stdout_file.close()
|
|
567
|
+
stderr_file.close()
|
|
568
|
+
except Exception as e: # noqa: BLE001
|
|
569
|
+
await logger.adebug(f"Error closing temp files: {e}")
|
|
570
|
+
|
|
571
|
+
# Read from temp files using asyncio.to_thread
|
|
572
|
+
try:
|
|
573
|
+
|
|
574
|
+
def read_file(filepath):
|
|
575
|
+
return Path(filepath).read_bytes()
|
|
576
|
+
|
|
577
|
+
stdout_bytes = await asyncio.to_thread(read_file, stdout_file.name)
|
|
578
|
+
stdout_content = stdout_bytes.decode("utf-8", errors="replace") if stdout_bytes else ""
|
|
579
|
+
except Exception as e: # noqa: BLE001
|
|
580
|
+
await logger.adebug(f"Error reading stdout file: {e}")
|
|
581
|
+
|
|
582
|
+
try:
|
|
583
|
+
|
|
584
|
+
def read_file(filepath):
|
|
585
|
+
return Path(filepath).read_bytes()
|
|
586
|
+
|
|
587
|
+
stderr_bytes = await asyncio.to_thread(read_file, stderr_file.name)
|
|
588
|
+
stderr_content = stderr_bytes.decode("utf-8", errors="replace") if stderr_bytes else ""
|
|
589
|
+
except Exception as e: # noqa: BLE001
|
|
590
|
+
await logger.adebug(f"Error reading stderr file: {e}")
|
|
591
|
+
|
|
592
|
+
# Clean up temp files
|
|
593
|
+
try:
|
|
594
|
+
Path(stdout_file.name).unlink()
|
|
595
|
+
Path(stderr_file.name).unlink()
|
|
596
|
+
except Exception as e: # noqa: BLE001
|
|
597
|
+
await logger.adebug(f"Error removing temp files: {e}")
|
|
598
|
+
else:
|
|
599
|
+
# Use asyncio.to_thread to avoid blocking the event loop
|
|
600
|
+
# Process returns bytes, decode with error handling
|
|
601
|
+
stdout_bytes, stderr_bytes = await asyncio.to_thread(process.communicate, timeout=timeout)
|
|
602
|
+
stdout_content = stdout_bytes.decode("utf-8", errors="replace") if stdout_bytes else ""
|
|
603
|
+
stderr_content = stderr_bytes.decode("utf-8", errors="replace") if stderr_bytes else ""
|
|
604
|
+
|
|
605
|
+
except subprocess.TimeoutExpired:
|
|
606
|
+
process.kill()
|
|
607
|
+
error_msg = self._extract_error_message("", "", oauth_server_url)
|
|
608
|
+
return "", "", error_msg
|
|
609
|
+
|
|
610
|
+
error_msg = self._extract_error_message(stdout_content, stderr_content, oauth_server_url)
|
|
611
|
+
return stdout_content, stderr_content, error_msg
|
|
612
|
+
|
|
613
|
+
async def _read_stream_non_blocking(self, stream, stream_name: str) -> str:
|
|
614
|
+
"""Read from a stream without blocking and log the content.
|
|
615
|
+
|
|
616
|
+
Args:
|
|
617
|
+
stream: The stream to read from (stdout or stderr)
|
|
618
|
+
stream_name: Name of the stream for logging ("stdout" or "stderr")
|
|
619
|
+
|
|
620
|
+
Returns:
|
|
621
|
+
The content read from the stream (empty string if nothing available)
|
|
622
|
+
"""
|
|
623
|
+
if not stream:
|
|
624
|
+
return ""
|
|
625
|
+
|
|
626
|
+
try:
|
|
627
|
+
# On Windows, select.select() doesn't work with pipes (only sockets)
|
|
628
|
+
# Use platform-specific approach
|
|
629
|
+
os_type = platform.system()
|
|
630
|
+
|
|
631
|
+
if os_type == "Windows":
|
|
632
|
+
# On Windows, select.select() doesn't work with pipes
|
|
633
|
+
# Skip stream reading during monitoring - output will be captured when process terminates
|
|
634
|
+
# This prevents blocking on peek() which can cause the monitoring loop to hang
|
|
635
|
+
return ""
|
|
636
|
+
# On Unix-like systems, use select
|
|
637
|
+
if select.select([stream], [], [], 0)[0]:
|
|
638
|
+
line_bytes = stream.readline()
|
|
639
|
+
if line_bytes:
|
|
640
|
+
# Decode bytes with error handling
|
|
641
|
+
line = line_bytes.decode("utf-8", errors="replace") if isinstance(line_bytes, bytes) else line_bytes
|
|
642
|
+
stripped = line.strip()
|
|
643
|
+
if stripped:
|
|
644
|
+
# Log errors at error level, everything else at debug
|
|
645
|
+
if stream_name == "stderr" and ("ERROR" in stripped or "error" in stripped):
|
|
646
|
+
await logger.aerror(f"MCP Composer {stream_name}: {stripped}")
|
|
647
|
+
else:
|
|
648
|
+
await logger.adebug(f"MCP Composer {stream_name}: {stripped}")
|
|
649
|
+
return stripped
|
|
650
|
+
except Exception as e: # noqa: BLE001
|
|
651
|
+
await logger.adebug(f"Error reading {stream_name}: {e}")
|
|
652
|
+
return ""
|
|
653
|
+
|
|
654
|
+
async def _ensure_port_available(self, port: int, current_project_id: str) -> None:
|
|
655
|
+
"""Ensure a port is available, only killing untracked processes.
|
|
656
|
+
|
|
657
|
+
Args:
|
|
658
|
+
port: The port number to ensure is available
|
|
659
|
+
current_project_id: The project ID requesting the port
|
|
660
|
+
|
|
661
|
+
Raises:
|
|
662
|
+
MCPComposerPortError: If port cannot be made available
|
|
663
|
+
MCPComposerConfigError: If port is invalid
|
|
664
|
+
"""
|
|
665
|
+
try:
|
|
666
|
+
is_port_available = self._is_port_available(port)
|
|
667
|
+
await logger.adebug(f"Port {port} availability check: {is_port_available}")
|
|
668
|
+
except (ValueError, OverflowError, TypeError) as e:
|
|
669
|
+
# Port validation failed - invalid port number or type
|
|
670
|
+
# ValueError: from our validation
|
|
671
|
+
# OverflowError: from socket.bind() when port > 65535
|
|
672
|
+
# TypeError: when port is not an integer
|
|
673
|
+
error_msg = f"Invalid port number: {port}. Port must be an integer between 0 and 65535."
|
|
674
|
+
await logger.aerror(f"Invalid port for project {current_project_id}: {e}")
|
|
675
|
+
raise MCPComposerConfigError(error_msg, current_project_id) from e
|
|
676
|
+
|
|
677
|
+
if not is_port_available:
|
|
678
|
+
# Check if the port is being used by a tracked project
|
|
679
|
+
is_used_by_other, other_project_id = self._is_port_used_by_another_project(port, current_project_id)
|
|
680
|
+
|
|
681
|
+
if is_used_by_other and other_project_id:
|
|
682
|
+
# Port is being used by another tracked project
|
|
683
|
+
# Check if we can take ownership (e.g., the other project is failing)
|
|
684
|
+
other_composer = self.project_composers.get(other_project_id)
|
|
685
|
+
if other_composer and other_composer.get("process"):
|
|
686
|
+
other_process = other_composer["process"]
|
|
687
|
+
# If the other process is still running and healthy, don't kill it
|
|
688
|
+
if other_process.poll() is None:
|
|
689
|
+
await logger.aerror(
|
|
690
|
+
f"Port {port} requested by project {current_project_id} is already in use by "
|
|
691
|
+
f"project {other_project_id}. Will not kill active MCP Composer process."
|
|
692
|
+
)
|
|
693
|
+
port_error_msg = (
|
|
694
|
+
f"Port {port} is already in use by another project. "
|
|
695
|
+
f"Please choose a different port (e.g., {port + 1}) "
|
|
696
|
+
f"or disable OAuth on the other project first."
|
|
697
|
+
)
|
|
698
|
+
raise MCPComposerPortError(port_error_msg, current_project_id)
|
|
699
|
+
|
|
700
|
+
# Process died but port tracking wasn't cleaned up - allow takeover
|
|
701
|
+
await logger.adebug(
|
|
702
|
+
f"Port {port} was tracked to project {other_project_id} but process died. "
|
|
703
|
+
f"Allowing project {current_project_id} to take ownership."
|
|
704
|
+
)
|
|
705
|
+
# Clean up the old tracking
|
|
706
|
+
await self._do_stop_project_composer(other_project_id)
|
|
707
|
+
|
|
708
|
+
# Check if port is used by a process owned by the current project (e.g., stuck in startup loop)
|
|
709
|
+
port_owner_project = self._port_to_project.get(port)
|
|
710
|
+
if port_owner_project == current_project_id:
|
|
711
|
+
# Port is owned by current project - safe to kill
|
|
712
|
+
await logger.adebug(
|
|
713
|
+
f"Port {port} is in use by current project {current_project_id} (likely stuck in startup). "
|
|
714
|
+
f"Killing process to retry."
|
|
715
|
+
)
|
|
716
|
+
killed = await self._kill_process_on_port(port)
|
|
717
|
+
if killed:
|
|
718
|
+
await logger.adebug(
|
|
719
|
+
f"Successfully killed own process on port {port}. Waiting for port to be released..."
|
|
720
|
+
)
|
|
721
|
+
await asyncio.sleep(2)
|
|
722
|
+
is_port_available = self._is_port_available(port)
|
|
723
|
+
if not is_port_available:
|
|
724
|
+
await logger.aerror(f"Port {port} is still in use after killing own process.")
|
|
725
|
+
port_error_msg = f"Port {port} is still in use after killing process"
|
|
726
|
+
raise MCPComposerPortError(port_error_msg)
|
|
727
|
+
else:
|
|
728
|
+
# Port is in use by unknown process - don't kill it (security concern)
|
|
729
|
+
await logger.aerror(
|
|
730
|
+
f"Port {port} is in use by an unknown process (not owned by Langflow). "
|
|
731
|
+
f"Will not kill external application for security reasons."
|
|
732
|
+
)
|
|
733
|
+
port_error_msg = (
|
|
734
|
+
f"Port {port} is already in use by another application. "
|
|
735
|
+
f"Please choose a different port (e.g., {port + 1}) or free up the port manually."
|
|
736
|
+
)
|
|
737
|
+
raise MCPComposerPortError(port_error_msg, current_project_id)
|
|
738
|
+
|
|
739
|
+
await logger.adebug(f"Port {port} is available, proceeding with MCP Composer startup")
|
|
740
|
+
|
|
741
|
+
async def _log_startup_error_details(
|
|
742
|
+
self,
|
|
743
|
+
project_id: str,
|
|
744
|
+
cmd: list[str],
|
|
745
|
+
host: str,
|
|
746
|
+
port: int,
|
|
747
|
+
stdout: str = "",
|
|
748
|
+
stderr: str = "",
|
|
749
|
+
error_msg: str = "",
|
|
750
|
+
exit_code: int | None = None,
|
|
751
|
+
pid: int | None = None,
|
|
752
|
+
) -> None:
|
|
753
|
+
"""Log detailed startup error information.
|
|
754
|
+
|
|
755
|
+
Args:
|
|
756
|
+
project_id: The project ID
|
|
757
|
+
cmd: The command that was executed
|
|
758
|
+
host: Target host
|
|
759
|
+
port: Target port
|
|
760
|
+
stdout: Standard output from the process
|
|
761
|
+
stderr: Standard error from the process
|
|
762
|
+
error_msg: User-friendly error message
|
|
763
|
+
exit_code: Process exit code (if terminated)
|
|
764
|
+
pid: Process ID (if still running)
|
|
765
|
+
"""
|
|
766
|
+
await logger.aerror(f"MCP Composer startup failed for project {project_id}:")
|
|
767
|
+
if exit_code is not None:
|
|
768
|
+
await logger.aerror(f" - Process died with exit code: {exit_code}")
|
|
769
|
+
if pid is not None:
|
|
770
|
+
await logger.aerror(f" - Process is running (PID: {pid}) but failed to bind to port {port}")
|
|
771
|
+
await logger.aerror(f" - Target: {host}:{port}")
|
|
772
|
+
|
|
773
|
+
# Obfuscate secrets in command before logging
|
|
774
|
+
safe_cmd = self._obfuscate_command_secrets(cmd)
|
|
775
|
+
await logger.aerror(f" - Command: {' '.join(safe_cmd)}")
|
|
776
|
+
|
|
777
|
+
if stderr.strip():
|
|
778
|
+
await logger.aerror(f" - Error output: {stderr.strip()}")
|
|
779
|
+
if stdout.strip():
|
|
780
|
+
await logger.aerror(f" - Standard output: {stdout.strip()}")
|
|
781
|
+
if error_msg:
|
|
782
|
+
await logger.aerror(f" - Error message: {error_msg}")
|
|
783
|
+
|
|
169
784
|
def _validate_oauth_settings(self, auth_config: dict[str, Any]) -> None:
|
|
170
785
|
"""Validate that all required OAuth settings are present and non-empty.
|
|
171
786
|
|
|
@@ -205,6 +820,18 @@ class MCPComposerService(Service):
|
|
|
205
820
|
config_error_msg = f"Invalid OAuth configuration: {'; '.join(error_parts)}"
|
|
206
821
|
raise MCPComposerConfigError(config_error_msg)
|
|
207
822
|
|
|
823
|
+
@staticmethod
|
|
824
|
+
def _normalize_config_value(value: Any) -> Any:
|
|
825
|
+
"""Normalize a config value (None or empty string becomes None).
|
|
826
|
+
|
|
827
|
+
Args:
|
|
828
|
+
value: The value to normalize
|
|
829
|
+
|
|
830
|
+
Returns:
|
|
831
|
+
None if value is None or empty string, otherwise the value
|
|
832
|
+
"""
|
|
833
|
+
return None if (value is None or value == "") else value
|
|
834
|
+
|
|
208
835
|
def _has_auth_config_changed(self, existing_auth: dict[str, Any] | None, new_auth: dict[str, Any] | None) -> bool:
|
|
209
836
|
"""Check if auth configuration has changed in a way that requires restart."""
|
|
210
837
|
if not existing_auth and not new_auth:
|
|
@@ -230,12 +857,8 @@ class MCPComposerService(Service):
|
|
|
230
857
|
|
|
231
858
|
# Compare relevant fields
|
|
232
859
|
for field in fields_to_check:
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
# Convert None and empty string to None for comparison
|
|
237
|
-
old_normalized = None if (old_val is None or old_val == "") else old_val
|
|
238
|
-
new_normalized = None if (new_val is None or new_val == "") else new_val
|
|
860
|
+
old_normalized = self._normalize_config_value(existing_auth.get(field))
|
|
861
|
+
new_normalized = self._normalize_config_value(new_auth.get(field))
|
|
239
862
|
|
|
240
863
|
if old_normalized != new_normalized:
|
|
241
864
|
return True
|
|
@@ -252,23 +875,30 @@ class MCPComposerService(Service):
|
|
|
252
875
|
List of command arguments with secrets replaced with ***REDACTED***
|
|
253
876
|
"""
|
|
254
877
|
safe_cmd = []
|
|
255
|
-
|
|
878
|
+
i = 0
|
|
256
879
|
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
skip_next = False
|
|
260
|
-
safe_cmd.append("***REDACTED***")
|
|
261
|
-
continue
|
|
880
|
+
while i < len(cmd):
|
|
881
|
+
arg = cmd[i]
|
|
262
882
|
|
|
883
|
+
# Check if this is --env followed by a secret key
|
|
263
884
|
if arg == "--env" and i + 2 < len(cmd):
|
|
264
|
-
# Check if next env var is a secret
|
|
265
885
|
env_key = cmd[i + 1]
|
|
886
|
+
env_value = cmd[i + 2]
|
|
887
|
+
|
|
266
888
|
if any(secret in env_key.lower() for secret in ["secret", "key", "token"]):
|
|
267
|
-
|
|
268
|
-
|
|
889
|
+
# Redact the value
|
|
890
|
+
safe_cmd.extend([arg, env_key, "***REDACTED***"])
|
|
891
|
+
i += 3 # Skip all three: --env, key, and value
|
|
269
892
|
continue
|
|
270
893
|
|
|
894
|
+
# Not a secret, keep as-is
|
|
895
|
+
safe_cmd.extend([arg, env_key, env_value])
|
|
896
|
+
i += 3
|
|
897
|
+
continue
|
|
898
|
+
|
|
899
|
+
# Regular argument
|
|
271
900
|
safe_cmd.append(arg)
|
|
901
|
+
i += 1
|
|
272
902
|
|
|
273
903
|
return safe_cmd
|
|
274
904
|
|
|
@@ -321,11 +951,75 @@ class MCPComposerService(Service):
|
|
|
321
951
|
project_id: str,
|
|
322
952
|
sse_url: str,
|
|
323
953
|
auth_config: dict[str, Any] | None,
|
|
324
|
-
|
|
954
|
+
max_retries: int = 3,
|
|
955
|
+
max_startup_checks: int = 40,
|
|
325
956
|
startup_delay: float = 2.0,
|
|
326
957
|
) -> None:
|
|
327
958
|
"""Start an MCP Composer instance for a specific project.
|
|
328
959
|
|
|
960
|
+
Args:
|
|
961
|
+
project_id: The project ID
|
|
962
|
+
sse_url: The SSE URL to connect to
|
|
963
|
+
auth_config: Authentication configuration
|
|
964
|
+
max_retries: Maximum number of retry attempts (default: 3)
|
|
965
|
+
max_startup_checks: Number of checks per retry attempt (default: 40)
|
|
966
|
+
startup_delay: Delay between checks in seconds (default: 2.0)
|
|
967
|
+
|
|
968
|
+
Raises:
|
|
969
|
+
MCPComposerError: Various specific errors if startup fails
|
|
970
|
+
"""
|
|
971
|
+
# Cancel any active start operation for this project
|
|
972
|
+
if project_id in self._active_start_tasks:
|
|
973
|
+
active_task = self._active_start_tasks[project_id]
|
|
974
|
+
if not active_task.done():
|
|
975
|
+
await logger.adebug(f"Cancelling previous MCP Composer start operation for project {project_id}")
|
|
976
|
+
active_task.cancel()
|
|
977
|
+
try:
|
|
978
|
+
await active_task
|
|
979
|
+
except asyncio.CancelledError:
|
|
980
|
+
await logger.adebug(f"Previous start operation for project {project_id} cancelled successfully")
|
|
981
|
+
finally:
|
|
982
|
+
# Clean up the cancelled task from tracking
|
|
983
|
+
del self._active_start_tasks[project_id]
|
|
984
|
+
|
|
985
|
+
# Create and track the current task
|
|
986
|
+
current_task = asyncio.current_task()
|
|
987
|
+
if not current_task:
|
|
988
|
+
await logger.awarning(
|
|
989
|
+
f"Could not get current task for project {project_id}. "
|
|
990
|
+
f"Concurrent start operations may not be properly cancelled."
|
|
991
|
+
)
|
|
992
|
+
else:
|
|
993
|
+
self._active_start_tasks[project_id] = current_task
|
|
994
|
+
|
|
995
|
+
try:
|
|
996
|
+
await self._do_start_project_composer(
|
|
997
|
+
project_id, sse_url, auth_config, max_retries, max_startup_checks, startup_delay
|
|
998
|
+
)
|
|
999
|
+
finally:
|
|
1000
|
+
# Clean up the task reference when done
|
|
1001
|
+
if project_id in self._active_start_tasks and self._active_start_tasks[project_id] == current_task:
|
|
1002
|
+
del self._active_start_tasks[project_id]
|
|
1003
|
+
|
|
1004
|
+
async def _do_start_project_composer(
|
|
1005
|
+
self,
|
|
1006
|
+
project_id: str,
|
|
1007
|
+
sse_url: str,
|
|
1008
|
+
auth_config: dict[str, Any] | None,
|
|
1009
|
+
max_retries: int = 3,
|
|
1010
|
+
max_startup_checks: int = 40,
|
|
1011
|
+
startup_delay: float = 2.0,
|
|
1012
|
+
) -> None:
|
|
1013
|
+
"""Internal method to start an MCP Composer instance.
|
|
1014
|
+
|
|
1015
|
+
Args:
|
|
1016
|
+
project_id: The project ID
|
|
1017
|
+
sse_url: The SSE URL to connect to
|
|
1018
|
+
auth_config: Authentication configuration
|
|
1019
|
+
max_retries: Maximum number of retry attempts (default: 3)
|
|
1020
|
+
max_startup_checks: Number of checks per retry attempt (default: 40)
|
|
1021
|
+
startup_delay: Delay between checks in seconds (default: 2.0)
|
|
1022
|
+
|
|
329
1023
|
Raises:
|
|
330
1024
|
MCPComposerError: Various specific errors if startup fails
|
|
331
1025
|
"""
|
|
@@ -366,11 +1060,13 @@ class MCPComposerService(Service):
|
|
|
366
1060
|
composer_info = self.project_composers[project_id]
|
|
367
1061
|
process = composer_info.get("process")
|
|
368
1062
|
existing_auth = composer_info.get("auth_config", {})
|
|
1063
|
+
existing_port = composer_info.get("port")
|
|
369
1064
|
|
|
370
1065
|
# Check if process is still running
|
|
371
1066
|
if process and process.poll() is None:
|
|
372
1067
|
# Process is running - only restart if config changed
|
|
373
1068
|
auth_changed = self._has_auth_config_changed(existing_auth, auth_config)
|
|
1069
|
+
|
|
374
1070
|
if auth_changed:
|
|
375
1071
|
await logger.adebug(f"Config changed for project {project_id}, restarting MCP Composer")
|
|
376
1072
|
await self._do_stop_project_composer(project_id)
|
|
@@ -383,28 +1079,130 @@ class MCPComposerService(Service):
|
|
|
383
1079
|
# Process died or never started properly, restart it
|
|
384
1080
|
await logger.adebug(f"MCP Composer process died for project {project_id}, restarting")
|
|
385
1081
|
await self._do_stop_project_composer(project_id)
|
|
1082
|
+
# Also kill any process that might be using the old port
|
|
1083
|
+
if existing_port:
|
|
1084
|
+
try:
|
|
1085
|
+
await asyncio.wait_for(self._kill_process_on_port(existing_port), timeout=5.0)
|
|
1086
|
+
except asyncio.TimeoutError:
|
|
1087
|
+
await logger.aerror(f"Timeout while killing process on port {existing_port}")
|
|
386
1088
|
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
1089
|
+
# Retry loop: try starting the process multiple times
|
|
1090
|
+
last_error = None
|
|
1091
|
+
try:
|
|
1092
|
+
# Before first attempt, try to kill any zombie MCP Composer processes
|
|
1093
|
+
# This is a best-effort operation - don't fail startup if it errors
|
|
1094
|
+
try:
|
|
1095
|
+
await logger.adebug(
|
|
1096
|
+
f"Checking for zombie MCP Composer processes on port {project_port} before startup..."
|
|
1097
|
+
)
|
|
1098
|
+
zombies_killed = await self._kill_zombie_mcp_processes(project_port)
|
|
1099
|
+
if zombies_killed:
|
|
1100
|
+
await logger.adebug(f"Killed zombie processes, port {project_port} should now be free")
|
|
1101
|
+
except Exception as zombie_error: # noqa: BLE001
|
|
1102
|
+
# Log but continue - zombie cleanup is optional
|
|
1103
|
+
await logger.awarning(
|
|
1104
|
+
f"Failed to check/kill zombie processes (non-fatal): {zombie_error}. Continuing with startup..."
|
|
1105
|
+
)
|
|
392
1106
|
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
1107
|
+
# Ensure port is available (only kill untracked processes)
|
|
1108
|
+
try:
|
|
1109
|
+
await self._ensure_port_available(project_port, project_id)
|
|
1110
|
+
except (MCPComposerPortError, MCPComposerConfigError) as e:
|
|
1111
|
+
# Port/config error before starting - store and raise immediately (no retries)
|
|
1112
|
+
self._last_errors[project_id] = e.message
|
|
1113
|
+
raise
|
|
1114
|
+
for retry_attempt in range(1, max_retries + 1):
|
|
1115
|
+
try:
|
|
1116
|
+
await logger.adebug(
|
|
1117
|
+
f"Starting MCP Composer for project {project_id} (attempt {retry_attempt}/{max_retries})"
|
|
1118
|
+
)
|
|
404
1119
|
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
1120
|
+
# Re-check port availability before each attempt to prevent race conditions
|
|
1121
|
+
if retry_attempt > 1:
|
|
1122
|
+
await logger.adebug(f"Re-checking port {project_port} availability before retry...")
|
|
1123
|
+
await self._ensure_port_available(project_port, project_id)
|
|
1124
|
+
|
|
1125
|
+
process = await self._start_project_composer_process(
|
|
1126
|
+
project_id,
|
|
1127
|
+
project_host,
|
|
1128
|
+
project_port,
|
|
1129
|
+
sse_url,
|
|
1130
|
+
auth_config,
|
|
1131
|
+
max_startup_checks,
|
|
1132
|
+
startup_delay,
|
|
1133
|
+
)
|
|
1134
|
+
|
|
1135
|
+
except MCPComposerError as e:
|
|
1136
|
+
last_error = e
|
|
1137
|
+
await logger.aerror(
|
|
1138
|
+
f"MCP Composer startup attempt {retry_attempt}/{max_retries} failed "
|
|
1139
|
+
f"for project {project_id}: {e.message}"
|
|
1140
|
+
)
|
|
1141
|
+
|
|
1142
|
+
# For config/port errors, don't retry - fail immediately
|
|
1143
|
+
if isinstance(e, (MCPComposerConfigError, MCPComposerPortError)):
|
|
1144
|
+
await logger.aerror(
|
|
1145
|
+
f"Configuration or port error for project {project_id}, not retrying: {e.message}"
|
|
1146
|
+
)
|
|
1147
|
+
raise # Re-raise to exit retry loop immediately
|
|
1148
|
+
|
|
1149
|
+
# Clean up any partially started process before retrying
|
|
1150
|
+
if project_id in self.project_composers:
|
|
1151
|
+
await self._do_stop_project_composer(project_id)
|
|
1152
|
+
|
|
1153
|
+
# If not the last attempt, wait and try to clean up zombie processes
|
|
1154
|
+
if retry_attempt < max_retries:
|
|
1155
|
+
await logger.adebug(f"Waiting 2 seconds before retry attempt {retry_attempt + 1}...")
|
|
1156
|
+
await asyncio.sleep(2)
|
|
1157
|
+
|
|
1158
|
+
# On Windows, try to kill any zombie MCP Composer processes for this port
|
|
1159
|
+
# This is a best-effort operation - don't fail retry if it errors
|
|
1160
|
+
try:
|
|
1161
|
+
msg = f"Checking for zombie MCP Composer processes on port {project_port}"
|
|
1162
|
+
await logger.adebug(msg)
|
|
1163
|
+
zombies_killed = await self._kill_zombie_mcp_processes(project_port)
|
|
1164
|
+
if zombies_killed:
|
|
1165
|
+
await logger.adebug(f"Killed zombie processes, port {project_port} should be free")
|
|
1166
|
+
except Exception as retry_zombie_error: # noqa: BLE001
|
|
1167
|
+
# Log but continue - zombie cleanup is optional
|
|
1168
|
+
msg = f"Failed to check/kill zombie processes during retry: {retry_zombie_error}"
|
|
1169
|
+
await logger.awarning(msg)
|
|
1170
|
+
|
|
1171
|
+
else:
|
|
1172
|
+
# Success! Store the composer info and register the port and PID
|
|
1173
|
+
self.project_composers[project_id] = {
|
|
1174
|
+
"process": process,
|
|
1175
|
+
"host": project_host,
|
|
1176
|
+
"port": project_port,
|
|
1177
|
+
"sse_url": sse_url,
|
|
1178
|
+
"auth_config": auth_config,
|
|
1179
|
+
}
|
|
1180
|
+
self._port_to_project[project_port] = project_id
|
|
1181
|
+
self._pid_to_project[process.pid] = project_id
|
|
1182
|
+
# Clear any previous error on success
|
|
1183
|
+
self.clear_last_error(project_id)
|
|
1184
|
+
|
|
1185
|
+
await logger.adebug(
|
|
1186
|
+
f"MCP Composer started for project {project_id} on port {project_port} "
|
|
1187
|
+
f"(PID: {process.pid}) after {retry_attempt} attempt(s)"
|
|
1188
|
+
)
|
|
1189
|
+
return # Success!
|
|
1190
|
+
|
|
1191
|
+
# All retries failed, raise the last error
|
|
1192
|
+
if last_error:
|
|
1193
|
+
await logger.aerror(
|
|
1194
|
+
f"MCP Composer failed to start for project {project_id} after {max_retries} attempts"
|
|
1195
|
+
)
|
|
1196
|
+
# Store the error message for later retrieval
|
|
1197
|
+
self._last_errors[project_id] = last_error.message
|
|
1198
|
+
raise last_error
|
|
1199
|
+
|
|
1200
|
+
except asyncio.CancelledError:
|
|
1201
|
+
# Operation was cancelled, clean up any started process
|
|
1202
|
+
await logger.adebug(f"MCP Composer start operation for project {project_id} was cancelled")
|
|
1203
|
+
if project_id in self.project_composers:
|
|
1204
|
+
await self._do_stop_project_composer(project_id)
|
|
1205
|
+
raise # Re-raise to propagate cancellation
|
|
408
1206
|
|
|
409
1207
|
async def _start_project_composer_process(
|
|
410
1208
|
self,
|
|
@@ -413,14 +1211,34 @@ class MCPComposerService(Service):
|
|
|
413
1211
|
port: int,
|
|
414
1212
|
sse_url: str,
|
|
415
1213
|
auth_config: dict[str, Any] | None = None,
|
|
416
|
-
max_startup_checks: int =
|
|
1214
|
+
max_startup_checks: int = 40,
|
|
417
1215
|
startup_delay: float = 2.0,
|
|
418
1216
|
) -> subprocess.Popen:
|
|
419
|
-
"""Start the MCP Composer subprocess for a specific project.
|
|
1217
|
+
"""Start the MCP Composer subprocess for a specific project.
|
|
1218
|
+
|
|
1219
|
+
Args:
|
|
1220
|
+
project_id: The project ID
|
|
1221
|
+
host: Host to bind to
|
|
1222
|
+
port: Port to bind to
|
|
1223
|
+
sse_url: SSE URL to connect to
|
|
1224
|
+
auth_config: Authentication configuration
|
|
1225
|
+
max_startup_checks: Number of port binding checks (default: 40)
|
|
1226
|
+
startup_delay: Delay between checks in seconds (default: 2.0)
|
|
1227
|
+
|
|
1228
|
+
Returns:
|
|
1229
|
+
The started subprocess
|
|
1230
|
+
|
|
1231
|
+
Raises:
|
|
1232
|
+
MCPComposerStartupError: If startup fails
|
|
1233
|
+
"""
|
|
420
1234
|
settings = get_settings_service().settings
|
|
421
1235
|
cmd = [
|
|
422
1236
|
"uvx",
|
|
423
1237
|
f"mcp-composer{settings.mcp_composer_version}",
|
|
1238
|
+
"--port",
|
|
1239
|
+
str(port),
|
|
1240
|
+
"--host",
|
|
1241
|
+
host,
|
|
424
1242
|
"--mode",
|
|
425
1243
|
"sse",
|
|
426
1244
|
"--sse-url",
|
|
@@ -442,6 +1260,8 @@ class MCPComposerService(Service):
|
|
|
442
1260
|
cmd.extend(["--env", "ENABLE_OAUTH", "True"])
|
|
443
1261
|
|
|
444
1262
|
# Map auth config to environment variables for OAuth
|
|
1263
|
+
# Note: oauth_host and oauth_port are passed both via --host/--port CLI args
|
|
1264
|
+
# (for server binding) and as environment variables (for OAuth flow)
|
|
445
1265
|
oauth_env_mapping = {
|
|
446
1266
|
"oauth_host": "OAUTH_HOST",
|
|
447
1267
|
"oauth_port": "OAUTH_PORT",
|
|
@@ -462,68 +1282,102 @@ class MCPComposerService(Service):
|
|
|
462
1282
|
if value is not None and str(value).strip():
|
|
463
1283
|
cmd.extend(["--env", env_key, str(value)])
|
|
464
1284
|
|
|
1285
|
+
# Log the command being executed (with secrets obfuscated)
|
|
1286
|
+
safe_cmd = self._obfuscate_command_secrets(cmd)
|
|
1287
|
+
await logger.adebug(f"Starting MCP Composer with command: {' '.join(safe_cmd)}")
|
|
1288
|
+
|
|
465
1289
|
# Start the subprocess with both stdout and stderr captured
|
|
466
|
-
|
|
1290
|
+
# On Windows, use temp files to avoid pipe buffering issues that can cause process to hang
|
|
1291
|
+
stdout_handle: int | typing.IO[bytes] = subprocess.PIPE
|
|
1292
|
+
stderr_handle: int | typing.IO[bytes] = subprocess.PIPE
|
|
1293
|
+
stdout_file = None
|
|
1294
|
+
stderr_file = None
|
|
1295
|
+
|
|
1296
|
+
if platform.system() == "Windows":
|
|
1297
|
+
# Create temp files for stdout/stderr on Windows to avoid pipe deadlocks
|
|
1298
|
+
# Note: We intentionally don't use context manager as we need files to persist
|
|
1299
|
+
# for the subprocess and be cleaned up manually later
|
|
1300
|
+
stdout_file = tempfile.NamedTemporaryFile( # noqa: SIM115
|
|
1301
|
+
mode="w+b", delete=False, prefix=f"mcp_composer_{project_id}_stdout_", suffix=".log"
|
|
1302
|
+
)
|
|
1303
|
+
stderr_file = tempfile.NamedTemporaryFile( # noqa: SIM115
|
|
1304
|
+
mode="w+b", delete=False, prefix=f"mcp_composer_{project_id}_stderr_", suffix=".log"
|
|
1305
|
+
)
|
|
1306
|
+
stdout_handle = stdout_file
|
|
1307
|
+
stderr_handle = stderr_file
|
|
1308
|
+
stdout_name = stdout_file.name
|
|
1309
|
+
stderr_name = stderr_file.name
|
|
1310
|
+
await logger.adebug(f"Using temp files for MCP Composer logs: stdout={stdout_name}, stderr={stderr_name}")
|
|
1311
|
+
|
|
1312
|
+
process = subprocess.Popen(cmd, env=env, stdout=stdout_handle, stderr=stderr_handle) # noqa: ASYNC220, S603
|
|
467
1313
|
|
|
468
1314
|
# Monitor the process startup with multiple checks
|
|
469
1315
|
process_running = False
|
|
470
1316
|
port_bound = False
|
|
471
1317
|
|
|
472
|
-
await logger.adebug(
|
|
1318
|
+
await logger.adebug(
|
|
1319
|
+
f"MCP Composer process started with PID {process.pid}, monitoring startup for project {project_id}..."
|
|
1320
|
+
)
|
|
473
1321
|
|
|
474
|
-
|
|
475
|
-
|
|
1322
|
+
try:
|
|
1323
|
+
for check in range(max_startup_checks):
|
|
1324
|
+
await asyncio.sleep(startup_delay)
|
|
476
1325
|
|
|
477
|
-
|
|
478
|
-
|
|
1326
|
+
# Check if process is still running
|
|
1327
|
+
poll_result = process.poll()
|
|
479
1328
|
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
await
|
|
488
|
-
|
|
489
|
-
await logger.aerror(f"MCP Composer stderr:\n{stderr_content}")
|
|
490
|
-
safe_cmd = self._obfuscate_command_secrets(cmd)
|
|
491
|
-
await logger.aerror(f"Command that failed: {' '.join(safe_cmd)}")
|
|
492
|
-
|
|
493
|
-
# Extract meaningful error message
|
|
494
|
-
startup_error_msg = self._extract_error_message(stdout_content, stderr_content, oauth_server_url)
|
|
495
|
-
raise MCPComposerStartupError(startup_error_msg, project_id)
|
|
496
|
-
except subprocess.TimeoutExpired:
|
|
497
|
-
process.kill()
|
|
498
|
-
await logger.aerror(
|
|
499
|
-
f"MCP Composer process {process.pid} terminated unexpectedly for project {project_id}"
|
|
1329
|
+
startup_error_msg = None
|
|
1330
|
+
if poll_result is not None:
|
|
1331
|
+
# Process terminated, get the error output
|
|
1332
|
+
(
|
|
1333
|
+
stdout_content,
|
|
1334
|
+
stderr_content,
|
|
1335
|
+
startup_error_msg,
|
|
1336
|
+
) = await self._read_process_output_and_extract_error(
|
|
1337
|
+
process, oauth_server_url, stdout_file=stdout_file, stderr_file=stderr_file
|
|
500
1338
|
)
|
|
501
|
-
|
|
502
|
-
|
|
1339
|
+
await self._log_startup_error_details(
|
|
1340
|
+
project_id, cmd, host, port, stdout_content, stderr_content, startup_error_msg, poll_result
|
|
1341
|
+
)
|
|
1342
|
+
raise MCPComposerStartupError(startup_error_msg, project_id)
|
|
503
1343
|
|
|
504
|
-
|
|
505
|
-
|
|
1344
|
+
# Process is still running, check if port is bound
|
|
1345
|
+
port_bound = not self._is_port_available(port)
|
|
506
1346
|
|
|
507
|
-
|
|
1347
|
+
if port_bound:
|
|
1348
|
+
await logger.adebug(
|
|
1349
|
+
f"MCP Composer for project {project_id} bound to port {port} "
|
|
1350
|
+
f"(check {check + 1}/{max_startup_checks})"
|
|
1351
|
+
)
|
|
1352
|
+
process_running = True
|
|
1353
|
+
break
|
|
508
1354
|
await logger.adebug(
|
|
509
|
-
f"MCP Composer for project {project_id} bound to port {port} "
|
|
1355
|
+
f"MCP Composer for project {project_id} not yet bound to port {port} "
|
|
510
1356
|
f"(check {check + 1}/{max_startup_checks})"
|
|
511
1357
|
)
|
|
512
|
-
|
|
513
|
-
|
|
1358
|
+
|
|
1359
|
+
# Try to read any available stderr/stdout without blocking to see what's happening
|
|
1360
|
+
await self._read_stream_non_blocking(process.stderr, "stderr")
|
|
1361
|
+
await self._read_stream_non_blocking(process.stdout, "stdout")
|
|
1362
|
+
|
|
1363
|
+
except asyncio.CancelledError:
|
|
1364
|
+
# Operation was cancelled, kill the process and cleanup
|
|
514
1365
|
await logger.adebug(
|
|
515
|
-
f"MCP Composer for project {project_id}
|
|
516
|
-
f"(check {check + 1}/{max_startup_checks})"
|
|
1366
|
+
f"MCP Composer process startup cancelled for project {project_id}, terminating process {process.pid}"
|
|
517
1367
|
)
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
1368
|
+
try:
|
|
1369
|
+
process.terminate()
|
|
1370
|
+
# Wait for graceful termination with timeout
|
|
521
1371
|
try:
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
1372
|
+
await asyncio.wait_for(asyncio.to_thread(process.wait), timeout=2.0)
|
|
1373
|
+
except asyncio.TimeoutError:
|
|
1374
|
+
# Force kill if graceful termination times out
|
|
1375
|
+
await logger.adebug(f"Process {process.pid} did not terminate gracefully, force killing")
|
|
1376
|
+
await asyncio.to_thread(process.kill)
|
|
1377
|
+
await asyncio.to_thread(process.wait)
|
|
1378
|
+
except Exception as e: # noqa: BLE001
|
|
1379
|
+
await logger.adebug(f"Error terminating process during cancellation: {e}")
|
|
1380
|
+
raise # Re-raise to propagate cancellation
|
|
527
1381
|
|
|
528
1382
|
# After all checks
|
|
529
1383
|
if not process_running or not port_bound:
|
|
@@ -532,56 +1386,43 @@ class MCPComposerService(Service):
|
|
|
532
1386
|
|
|
533
1387
|
if poll_result is not None:
|
|
534
1388
|
# Process died
|
|
535
|
-
startup_error_msg =
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
await logger.aerror(f" - Process died with exit code: {poll_result}")
|
|
542
|
-
await logger.aerror(f" - Target: {host}:{port}")
|
|
543
|
-
# Obfuscate secrets in command before logging
|
|
544
|
-
safe_cmd = self._obfuscate_command_secrets(cmd)
|
|
545
|
-
await logger.aerror(f" - Command: {' '.join(safe_cmd)}")
|
|
546
|
-
if stderr_content.strip():
|
|
547
|
-
await logger.aerror(f" - Error output: {stderr_content.strip()}")
|
|
548
|
-
if stdout_content.strip():
|
|
549
|
-
await logger.aerror(f" - Standard output: {stdout_content.strip()}")
|
|
550
|
-
await logger.aerror(f" - Error message: {startup_error_msg}")
|
|
551
|
-
except subprocess.TimeoutExpired:
|
|
552
|
-
await logger.aerror(f"MCP Composer for project {project_id} died but couldn't read output")
|
|
553
|
-
process.kill()
|
|
554
|
-
|
|
1389
|
+
stdout_content, stderr_content, startup_error_msg = await self._read_process_output_and_extract_error(
|
|
1390
|
+
process, oauth_server_url, stdout_file=stdout_file, stderr_file=stderr_file
|
|
1391
|
+
)
|
|
1392
|
+
await self._log_startup_error_details(
|
|
1393
|
+
project_id, cmd, host, port, stdout_content, stderr_content, startup_error_msg, poll_result
|
|
1394
|
+
)
|
|
555
1395
|
raise MCPComposerStartupError(startup_error_msg, project_id)
|
|
556
1396
|
# Process running but port not bound
|
|
557
|
-
await logger.aerror(f"MCP Composer startup failed for project {project_id}:")
|
|
558
|
-
await logger.aerror(f" - Process is running (PID: {process.pid}) but failed to bind to port {port}")
|
|
559
1397
|
await logger.aerror(
|
|
560
1398
|
f" - Checked {max_startup_checks} times over {max_startup_checks * startup_delay} seconds"
|
|
561
1399
|
)
|
|
562
|
-
await logger.aerror(f" - Target: {host}:{port}")
|
|
563
1400
|
|
|
564
1401
|
# Get any available output before terminating
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
process
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
if stdout_content.strip():
|
|
573
|
-
await logger.aerror(f" - Process stdout: {stdout_content.strip()}")
|
|
574
|
-
except Exception: # noqa: BLE001
|
|
575
|
-
process.kill()
|
|
576
|
-
await logger.aerror(" - Could not retrieve process output before termination")
|
|
577
|
-
|
|
1402
|
+
process.terminate()
|
|
1403
|
+
stdout_content, stderr_content, startup_error_msg = await self._read_process_output_and_extract_error(
|
|
1404
|
+
process, oauth_server_url, stdout_file=stdout_file, stderr_file=stderr_file
|
|
1405
|
+
)
|
|
1406
|
+
await self._log_startup_error_details(
|
|
1407
|
+
project_id, cmd, host, port, stdout_content, stderr_content, startup_error_msg, pid=process.pid
|
|
1408
|
+
)
|
|
578
1409
|
raise MCPComposerStartupError(startup_error_msg, project_id)
|
|
579
1410
|
|
|
580
|
-
# Close the pipes if everything is successful
|
|
581
|
-
if
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
1411
|
+
# Close the pipes/files if everything is successful
|
|
1412
|
+
if stdout_file and stderr_file:
|
|
1413
|
+
# Clean up temp files on success
|
|
1414
|
+
try:
|
|
1415
|
+
stdout_file.close()
|
|
1416
|
+
stderr_file.close()
|
|
1417
|
+
Path(stdout_file.name).unlink()
|
|
1418
|
+
Path(stderr_file.name).unlink()
|
|
1419
|
+
except Exception as e: # noqa: BLE001
|
|
1420
|
+
await logger.adebug(f"Error cleaning up temp files on success: {e}")
|
|
1421
|
+
else:
|
|
1422
|
+
if process.stdout:
|
|
1423
|
+
process.stdout.close()
|
|
1424
|
+
if process.stderr:
|
|
1425
|
+
process.stderr.close()
|
|
585
1426
|
|
|
586
1427
|
return process
|
|
587
1428
|
|