mcp.txt/index.html
2026-03-01 21:58:50 -08:00

90 lines
2.5 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<title>mcp.txt file browser</title>
</head>
<body>
<h1>mcp.txt file browser</h1>
<p>Path: <code id="current-path">/</code></p>
<div id="listing"></div>
<hr>
<pre id="file-content" hidden></pre>
<script>
let currentPath = ".";
async function navigate(path) {
currentPath = path;
document.getElementById("current-path").textContent = path === "." ? "/" : "/" + path;
document.getElementById("file-content").hidden = true;
const res = await fetch("/api/list?path=" + encodeURIComponent(path));
const data = await res.json();
if (data.error) {
document.getElementById("listing").textContent = "Error: " + data.error;
return;
}
const listing = document.getElementById("listing");
listing.innerHTML = "";
const ul = document.createElement("ul");
// parent directory link
if (path !== ".") {
const li = document.createElement("li");
const a = document.createElement("a");
a.href = "#";
a.textContent = "..";
const parent = path.includes("/") ? path.substring(0, path.lastIndexOf("/")) : ".";
a.onclick = (e) => { e.preventDefault(); navigate(parent); };
li.appendChild(a);
ul.appendChild(li);
}
for (const entry of data.entries) {
const li = document.createElement("li");
const a = document.createElement("a");
a.href = "#";
a.textContent = entry.name + (entry.type === "directory" ? "/" : "");
const entryPath = path === "." ? entry.name : path + "/" + entry.name;
if (entry.type === "directory") {
a.onclick = (e) => { e.preventDefault(); navigate(entryPath); };
} else {
a.onclick = (e) => { e.preventDefault(); readFile(entryPath); };
li.append(" (" + entry.size + " bytes)");
}
li.prepend(a);
ul.appendChild(li);
}
if (data.entries.length === 0) {
listing.textContent = "(empty directory)";
} else {
listing.appendChild(ul);
}
}
async function readFile(path) {
const pre = document.getElementById("file-content");
pre.hidden = false;
pre.textContent = "Loading...";
const res = await fetch("/api/read?path=" + encodeURIComponent(path));
const data = await res.json();
if (data.error) {
pre.textContent = "Error: " + data.error;
return;
}
pre.textContent = "=== " + path + " ===\n\n" + data.content;
}
navigate(".");
</script>
</body>
</html>