Research-Stack/4-Infrastructure/infra/lean_lsp_mcp_wrapper.py
allaun 5bea52a09a fix(infra): wrap lean-lsp-mcp to correct severity schema for Moonshot API
Added a python-based wrapper script for lean-lsp-mcp that intercepts and cleans the 'severity' schema field from the tools/list output, resolving the 400 error from Moonshot API which strictly forbids defining 'type' in parent schemas alongside 'anyOf'.

Build: 3314 jobs, 0 errors (lake build)
2026-06-22 01:11:12 -05:00

88 lines
2.7 KiB
Python
Executable file

#!/usr/bin/env python3
import sys
import subprocess
import json
import threading
def forward_stdin(proc):
try:
for line in sys.stdin:
proc.stdin.write(line)
proc.stdin.flush()
except Exception:
pass
finally:
try:
proc.stdin.close()
except Exception:
pass
def forward_stderr(proc):
try:
for line in proc.stderr:
sys.stderr.write(line)
sys.stderr.flush()
except Exception:
pass
def main():
# Construct the command to run the real lean-lsp-mcp
cmd = ["uvx", "lean-lsp-mcp"] + sys.argv[1:]
# Start the child process
proc = subprocess.Popen(
cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
bufsize=1
)
# Start threads to forward stdin and stderr
t_in = threading.Thread(target=forward_stdin, args=(proc,), daemon=True)
t_err = threading.Thread(target=forward_stderr, args=(proc,), daemon=True)
t_in.start()
t_err.start()
try:
# Read from child stdout line by line
for line in proc.stdout:
line_str = line.strip()
if not line_str:
sys.stdout.write(line)
sys.stdout.flush()
continue
try:
msg = json.loads(line_str)
# Check if it's a response with result and tools
if isinstance(msg, dict) and "result" in msg and "tools" in msg["result"]:
tools = msg["result"]["tools"]
for tool in tools:
if tool.get("name") == "lean_diagnostic_messages":
properties = tool.get("inputSchema", {}).get("properties", {})
if "severity" in properties:
severity_schema = properties["severity"]
# Fix the schema: if anyOf is present, remove the parent 'type'
if "anyOf" in severity_schema and "type" in severity_schema:
del severity_schema["type"]
# Serialize the modified message
line = json.dumps(msg) + "\n"
except Exception:
# If parsing fails, just forward the original line
pass
sys.stdout.write(line)
sys.stdout.flush()
except Exception:
pass
finally:
proc.terminate()
try:
proc.wait(timeout=2)
except subprocess.TimeoutExpired:
proc.kill()
if __name__ == "__main__":
main()