Spaces:
Running
Running
File size: 10,134 Bytes
03687eb 6cca3b1 03687eb 6cca3b1 03687eb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
import { NextRequest, NextResponse } from "next/server";
import { RepoDesignation, createRepo, uploadFiles, spaceInfo, listCommits } from "@huggingface/hub";
import { COLORS } from "@/lib/utils";
import { injectDeepSiteBadge, isIndexPage } from "@/lib/inject-badge";
import { Commit, Page } from "@/types";
interface MCPRequest {
jsonrpc: "2.0";
id: number | string;
method: string;
params?: any;
}
interface MCPResponse {
jsonrpc: "2.0";
id: number | string;
result?: any;
error?: {
code: number;
message: string;
data?: any;
};
}
interface CreateProjectParams {
title?: string;
pages: Page[];
prompt?: string;
hf_token?: string; // Optional - can come from header instead
}
// MCP Server over HTTP
export async function POST(req: NextRequest) {
try {
const body: MCPRequest = await req.json();
const { jsonrpc, id, method, params } = body;
// Validate JSON-RPC 2.0 format
if (jsonrpc !== "2.0") {
return NextResponse.json({
jsonrpc: "2.0",
id: id || null,
error: {
code: -32600,
message: "Invalid Request: jsonrpc must be '2.0'",
},
});
}
let response: MCPResponse;
switch (method) {
case "initialize":
response = {
jsonrpc: "2.0",
id,
result: {
protocolVersion: "2024-11-05",
capabilities: {
tools: {},
},
serverInfo: {
name: "deepsite-mcp-server",
version: "1.0.0",
},
},
};
break;
case "tools/list":
response = {
jsonrpc: "2.0",
id,
result: {
tools: [
{
name: "create_project",
description: `Create a new DeepSite project. This will create a new Hugging Face Space with your HTML/CSS/JS files.
Example usage:
- Create a simple website with HTML, CSS, and JavaScript files
- Each page needs a 'path' (filename like "index.html", "styles.css", "script.js") and 'html' (the actual content)
- The title will be formatted to a valid repository name
- Returns the project URL and metadata`,
inputSchema: {
type: "object",
properties: {
title: {
type: "string",
description: "Project title (optional, defaults to 'DeepSite Project'). Will be formatted to a valid repo name.",
},
pages: {
type: "array",
description: "Array of files to include in the project",
items: {
type: "object",
properties: {
path: {
type: "string",
description: "File path (e.g., 'index.html', 'styles.css', 'script.js')",
},
html: {
type: "string",
description: "File content",
},
},
required: ["path", "html"],
},
},
prompt: {
type: "string",
description: "Optional prompt/description for the commit message",
},
hf_token: {
type: "string",
description: "Hugging Face API token (optional if provided via Authorization header)",
},
},
required: ["pages"],
},
},
],
},
};
break;
case "tools/call":
const { name, arguments: toolArgs } = params;
if (name === "create_project") {
try {
// Extract token from Authorization header if present
const authHeader = req.headers.get("authorization");
let hf_token = toolArgs.hf_token;
if (authHeader && authHeader.startsWith("Bearer ")) {
hf_token = authHeader.substring(7); // Remove "Bearer " prefix
}
const result = await handleCreateProject({
...toolArgs,
hf_token,
} as CreateProjectParams);
response = {
jsonrpc: "2.0",
id,
result,
};
} catch (error: any) {
response = {
jsonrpc: "2.0",
id,
error: {
code: -32000,
message: error.message || "Failed to create project",
data: error.data,
},
};
}
} else {
response = {
jsonrpc: "2.0",
id,
error: {
code: -32601,
message: `Unknown tool: ${name}`,
},
};
}
break;
default:
response = {
jsonrpc: "2.0",
id,
error: {
code: -32601,
message: `Method not found: ${method}`,
},
};
}
return NextResponse.json(response);
} catch (error: any) {
return NextResponse.json({
jsonrpc: "2.0",
id: null,
error: {
code: -32700,
message: "Parse error",
data: error.message,
},
});
}
}
// Handle OPTIONS for CORS
export async function OPTIONS() {
return new NextResponse(null, {
status: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type",
},
});
}
async function handleCreateProject(params: CreateProjectParams) {
const { title: titleFromRequest, pages, prompt, hf_token } = params;
// Validate required parameters
if (!hf_token || typeof hf_token !== "string") {
throw new Error("hf_token is required and must be a string");
}
if (!pages || !Array.isArray(pages) || pages.length === 0) {
throw new Error("At least one page is required");
}
// Validate that each page has required fields
for (const page of pages) {
if (!page.path || !page.html) {
throw new Error("Each page must have 'path' and 'html' properties");
}
}
// Get user info from HF token
let username: string;
try {
const userResponse = await fetch("https://huggingface.co/api/whoami-v2", {
headers: {
Authorization: `Bearer ${hf_token}`,
},
});
if (!userResponse.ok) {
throw new Error("Invalid Hugging Face token");
}
const userData = await userResponse.json();
username = userData.name;
} catch (error: any) {
throw new Error(`Authentication failed: ${error.message}`);
}
const title = titleFromRequest ?? "DeepSite Project";
const formattedTitle = title
.toLowerCase()
.replace(/[^a-z0-9]+/g, "-")
.split("-")
.filter(Boolean)
.join("-")
.slice(0, 96);
const repo: RepoDesignation = {
type: "space",
name: `${username}/${formattedTitle}`,
};
const colorFrom = COLORS[Math.floor(Math.random() * COLORS.length)];
const colorTo = COLORS[Math.floor(Math.random() * COLORS.length)];
const README = `---
title: ${title}
colorFrom: ${colorFrom}
colorTo: ${colorTo}
emoji: 🐳
sdk: static
pinned: false
tags:
- deepsite-v3
---
# Welcome to your new DeepSite project!
This project was created with [DeepSite](https://huggingface.co/deepsite).
`;
const files: File[] = [];
const readmeFile = new File([README], "README.md", { type: "text/markdown" });
files.push(readmeFile);
pages.forEach((page: Page) => {
// Determine MIME type based on file extension
let mimeType = "text/html";
if (page.path.endsWith(".css")) {
mimeType = "text/css";
} else if (page.path.endsWith(".js")) {
mimeType = "text/javascript";
} else if (page.path.endsWith(".json")) {
mimeType = "application/json";
}
// Inject the DeepSite badge script into index pages only
const content = mimeType === "text/html" && isIndexPage(page.path)
? injectDeepSiteBadge(page.html)
: page.html;
const file = new File([content], page.path, { type: mimeType });
files.push(file);
});
try {
const { repoUrl } = await createRepo({
repo,
accessToken: hf_token,
});
const commitTitle = !prompt || prompt.trim() === "" ? "Initial project creation via MCP" : prompt;
await uploadFiles({
repo,
files,
accessToken: hf_token,
commitTitle,
});
const path = repoUrl.split("/").slice(-2).join("/");
const commits: Commit[] = [];
for await (const commit of listCommits({ repo, accessToken: hf_token })) {
if (commit.title.includes("initial commit") || commit.title.includes("image(s)") || commit.title.includes("Promote version")) {
continue;
}
commits.push({
title: commit.title,
oid: commit.oid,
date: commit.date,
});
}
const space = await spaceInfo({
name: repo.name,
accessToken: hf_token,
});
const projectUrl = `https://huggingface.co/deepsite/${path}`;
const spaceUrl = `https://huggingface.co/spaces/${path}`;
const liveUrl = `https://${username}-${formattedTitle}.hf.space`;
return {
content: [
{
type: "text",
text: JSON.stringify(
{
success: true,
message: "Project created successfully!",
projectUrl,
spaceUrl,
liveUrl,
spaceId: space.name,
projectId: space.id,
files: pages.map((p) => p.path),
updatedAt: space.updatedAt,
},
null,
2
),
},
],
};
} catch (err: any) {
throw new Error(err.message || "Failed to create project");
}
}
|