from pathlib import Path
from urllib.parse import urlparse, unquote
from dedalus_mcp import get_context, tool
def file_uri_to_path(uri: str) -> Path:
parsed = urlparse(uri)
if parsed.scheme != "file":
raise ValueError(f"Unsupported root scheme: {parsed.scheme!r}")
return Path(unquote(parsed.path)).expanduser().resolve()
@tool(description="Find project roots by marker files")
async def find_projects() -> list[dict]:
ctx = get_context()
server = ctx.server
if server is None:
raise RuntimeError("No active server in context")
roots = await server.roots.refresh(ctx.session)
projects: list[dict] = []
for root in roots:
root_path = file_uri_to_path(str(root.uri))
for marker in ["package.json", "pyproject.toml", "Cargo.toml"]:
if (root_path / marker).exists():
projects.append(
{
"root": root.name,
"path": str(root_path),
"type": marker,
}
)
return projects