Skip to content

Instantly share code, notes, and snippets.

@amotl
Last active April 3, 2025 19:02
Show Gist options
  • Save amotl/a761566e69870520c556f32c889ebeab to your computer and use it in GitHub Desktop.
Save amotl/a761566e69870520c556f32c889ebeab to your computer and use it in GitHub Desktop.
Problem with `uv run` not running to completion.
#!/usr/bin/env python3
"""
Prerequisite:
docker run --rm --name=cratedb \
--publish=4200:4200 --publish=5432:5432 \
--env=CRATE_HEAP_SIZE=2g crate/crate:nightly \
-Cdiscovery.type=single-node
Variants:
uv run uv_run_stuck_mcp.py --direct
uv run uv_run_stuck_mcp.py --uv
python uv_run_stuck_mcp.py --direct
python uv_run_stuck_mcp.py --uv
Problem:
`python uv_run_stuck_mcp.py --uv` stalls and does run to completion.
Is UV-12108 [1] the root cause?
[1] https://github.com/astral-sh/uv/issues/12108
"""
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "mcp",
# "pyyaml",
# "where",
# ]
# ///
import asyncio
import shlex
import sys
import yaml
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
async def run(runtime: str):
# Create server parameters for stdio connection.
if runtime == "direct":
cmd = "mcp-alchemy"
elif runtime == "uv":
cmd = "uv run --with='mcp-alchemy @ git+https://github.com/runekaagaard/mcp-alchemy.git@b85aae6' mcp-alchemy"
else:
raise NotImplementedError(f"Unknown runtime: {runtime}")
command = shlex.split(cmd)
server_params = StdioServerParameters(
command=command[0],
args=command[1:],
env={"DB_URL": "crate://crate@localhost:4200/?schema=testdrive"},
)
# Start server, and inquire available tools.
async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
# Initialize the connection.
await session.initialize()
# Inquire list of tools.
response = await session.list_tools()
for tool in response.tools:
print(yaml.dump(tool.model_dump(mode="json")))
if __name__ == "__main__":
try:
runtime = sys.argv[1].replace("--", "")
except Exception:
print(
"ERROR: Missing command line option flag. Use `--direct` or `--uv`",
file=sys.stderr,
)
sys.exit(1)
if runtime not in ["direct", "uv"]:
print(
"ERROR: Invalid line option flag. Use `--direct` or `--uv`", file=sys.stderr
)
sys.exit(1)
asyncio.run(run(runtime))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment