Skip to content

Commit f56c1d9

Browse files
committed
Fix argparser errors not redirected
When an MCP request to OpenStack is received with incorrect arguments we fail to return the error in the `stderr` field so it's always an empty string. This is because argparse ignores the `stderr` argument we pass to the shell itself. With this patch we fix it by ensuring we redirect the whole stderr when running the command.
1 parent 930223c commit f56c1d9

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

src/rhos_ls_mcps/osc.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import json
2727
import logging
2828
import os
29+
import sys
2930
import shlex
3031
from typing import TYPE_CHECKING, Any, Optional
3132

@@ -374,7 +375,7 @@ async def _initialize_api_versions(self, mcp_argv: list[str]) -> None:
374375
"""
375376
versions_varg = ["versions", "show", "--format", "json"]
376377
# Run in this process to later on share the loaded plugins and commands with command runs
377-
response, stdout, stderr = self._do_run(mcp_argv + versions_varg)
378+
response, stdout, stderr = self._do_run(mcp_argv + versions_varg, redirect=False)
378379
if response:
379380
raise ToolError(
380381
f"Failed to get API versions ({response}):\n{stdout}\n{stderr}"
@@ -405,8 +406,11 @@ async def _initialize_api_versions(self, mcp_argv: list[str]) -> None:
405406
# pass them all on the command line.
406407
self.parser.set_defaults(**version_defaults)
407408

408-
def _do_run(self, cmd: list[str]) -> tuple[int, str, str]:
409+
def _do_run(self, cmd: list[str], redirect: bool = True) -> tuple[int, str, str]:
409410
self._clean_stds()
411+
if redirect:
412+
old_stderr = sys.stderr
413+
sys.stderr = self.stderr
410414
try:
411415
return_code = super().run(cmd)
412416
except (SystemExit, Exception) as e:
@@ -416,6 +420,8 @@ def _do_run(self, cmd: list[str]) -> tuple[int, str, str]:
416420
f"Failure running command: {cmd} with code: {return_code} and message: {msg}"
417421
)
418422
finally:
423+
if redirect:
424+
sys.stderr = old_stderr
419425
stdout = self.stdout.getvalue()
420426
stderr = self.stderr.getvalue()
421427
self._clean_stds()

0 commit comments

Comments
 (0)