Skip to content

Commit eab52f5

Browse files
committed
fix: fix stdio problem on windows platform
1 parent 91e52ee commit eab52f5

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

pypreader_mcp/find_symbol.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
help="Logging level for the server.",
2929
)
3030

31+
arg_parser.add_argument(
32+
"--output_path",
33+
type=str,
34+
help="Path to write the output"
35+
)
36+
3137
args = arg_parser.parse_args()
3238

3339

@@ -144,4 +150,5 @@ def find_symbol(package_name: str, symbol_name: str) -> str:
144150

145151
if __name__ == "__main__":
146152
result = find_symbol(args.package_name, args.symbol_name)
147-
print(result)
153+
with open(args.output_path, "w") as f:
154+
f.write(result)

pypreader_mcp/server.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -152,22 +152,33 @@ def get_source_code_by_symbol(package_name: str, symbol_name: str) -> str:
152152
str: The definition of the symbol a string of code.
153153
"""
154154
symbol_name = symbol_name.strip()
155-
code = subprocess.run(
156-
[
157-
PYTHON_PATH,
158-
os.path.join(PYP_READER_MCP_DIR_PATH, "find_symbol.py"),
159-
"--package_name",
160-
package_name,
161-
"--symbol_name",
162-
symbol_name,
163-
"--logging_level",
164-
LOGGING_LEVEL,
165-
],
166-
capture_output=True,
167-
text=True,
168-
check=True,
169-
)
170-
return code.stdout.strip()
155+
output_path = os.path.join(os.path.abspath(os.path.dirname(__file__)),"output.txt")
156+
157+
cmd = [
158+
PYTHON_PATH,
159+
os.path.join(PYP_READER_MCP_DIR_PATH, "find_symbol.py"),
160+
"--package_name", package_name,
161+
"--symbol_name", symbol_name,
162+
"--logging_level", LOGGING_LEVEL,
163+
"--output_path", output_path,
164+
]
165+
166+
# For Windows platform compatible
167+
# Key points: Close stdin, stdout, and stderr to prevent inheritance and blocking
168+
with open(os.devnull, 'w') as devnull:
169+
result = subprocess.run(
170+
cmd,
171+
stdin=devnull,
172+
stdout=devnull, # close stdout
173+
stderr=subprocess.PIPE, # Optional: Record error information
174+
check=True,
175+
)
176+
177+
try:
178+
with open(output_path, 'r', encoding='utf-8') as f:
179+
return f.read().strip()
180+
except FileNotFoundError:
181+
return f"Error: Output file not found. stderr: {result.stderr.decode()}"
171182

172183

173184
def main() -> None:

0 commit comments

Comments
 (0)