#!/usr/bin/env node "use strict"; const http = require("node:http"); const GITEA_URL = process.env.GITEA_URL || "http://YOUR_SERVER_IP:30005/api/v1"; function request(pathStr) { return new Promise((resolve) => { const url = GITEA_URL + pathStr; const req = http.get(url, { timeout: 8000 }, (res) => { let body = ""; res.on("data", (chunk) => { body += chunk; }); res.on("end", () => { try { resolve({ status: res.statusCode, data: JSON.parse(body) }); } catch { resolve({ status: res.statusCode, raw: body }); } }); }); req.on("error", (err) => resolve({ status: 0, error: err.message })); req.on("timeout", () => { req.destroy(); resolve({ status: 0, error: "Timeout" }); }); }); } function result(text, error = false) { return { text: typeof text === "string" ? text : JSON.stringify(text, null, 2), error }; } async function giteaVersion() { const res = await request("/version"); if (res.status === 200 && res.data?.version) { return result(`Gitea Version: ${res.data.version} (Status 200 OK)`); } return result(`Gitea Status: ${res.status} (${res.error || res.raw || "Fehler"})`, true); } async function giteaListRepos() { const res = await request("/repos/search?limit=20"); if (res.status === 200 && Array.isArray(res.data?.data)) { const repos = res.data.data.map(r => ({ name: r.full_name, stars: r.stars_count, forks: r.forks_count, private: r.private, url: r.html_url, default_branch: r.default_branch })); return result(repos); } return result(`Fehler beim Abrufen der Repositories: ${res.error || res.raw || res.status}`, true); } async function giteaSearchRepos(args) { const q = encodeURIComponent(String(args.query || "").trim()); const res = await request(`/repos/search?q=${q}&limit=10`); if (res.status === 200 && Array.isArray(res.data?.data)) { const repos = res.data.data.map(r => ({ name: r.full_name, description: r.description, url: r.html_url, updated_at: r.updated_at })); return result(repos); } return result(`Suche fehlgeschlagen: ${res.error || res.status}`, true); } async function giteaListIssues(args) { const owner = encodeURIComponent(String(args.owner || "").trim()); const repo = encodeURIComponent(String(args.repo || "").trim()); if (!owner || !repo) return result("owner und repo sind erforderlich.", true); const res = await request(`/repos/${owner}/${repo}/issues?state=all&limit=20`); if (res.status === 200 && Array.isArray(res.data)) { const issues = res.data.map(i => ({ number: i.number, title: i.title, state: i.state, author: i.user?.username, created_at: i.created_at })); return result(issues); } return result(`Issues konnten nicht gelesen werden (${res.status}): ${res.error || res.raw}`, true); } async function giteaListCommits(args) { const owner = encodeURIComponent(String(args.owner || "").trim()); const repo = encodeURIComponent(String(args.repo || "").trim()); if (!owner || !repo) return result("owner und repo sind erforderlich.", true); const res = await request(`/repos/${owner}/${repo}/commits?limit=10`); if (res.status === 200 && Array.isArray(res.data)) { const commits = res.data.map(c => ({ sha: c.sha?.slice(0, 7), author: c.commit?.author?.name, message: c.commit?.message?.split("\n")[0], date: c.commit?.author?.date })); return result(commits); } return result(`Commits konnten nicht gelesen werden (${res.status}): ${res.error || res.raw}`, true); } const TOOLS = [ { name: "gitea_version", description: "Prueft Gitea-Version und API-Verfuegbarkeit. Lesend.", inputSchema: { type: "object", properties: {}, additionalProperties: false } }, { name: "gitea_list_repos", description: "Listet oeffentliche Repositories auf Gitea. Lesend.", inputSchema: { type: "object", properties: {}, additionalProperties: false } }, { name: "gitea_search_repos", description: "Sucht nach Repositories in Gitea. Lesend.", inputSchema: { type: "object", properties: { query: { type: "string" } }, required: ["query"], additionalProperties: false } }, { name: "gitea_list_issues", description: "Listet Issues eines Repositories auf Gitea. Lesend.", inputSchema: { type: "object", properties: { owner: { type: "string" }, repo: { type: "string" } }, required: ["owner", "repo"], additionalProperties: false } }, { name: "gitea_list_commits", description: "Listet letzte Commits eines Repositories. Lesend.", inputSchema: { type: "object", properties: { owner: { type: "string" }, repo: { type: "string" } }, required: ["owner", "repo"], additionalProperties: false } } ]; async function call(name, args) { try { if (name === "gitea_version") return await giteaVersion(); if (name === "gitea_list_repos") return await giteaListRepos(); if (name === "gitea_search_repos") return await giteaSearchRepos(args); if (name === "gitea_list_issues") return await giteaListIssues(args); if (name === "gitea_list_commits") return await giteaListCommits(args); return result("Unbekanntes Werkzeug.", true); } catch (err) { return result(String(err.message || err), true); } } function send(id, value) { process.stdout.write(JSON.stringify({ jsonrpc: "2.0", id, result: value }) + "\n"); } function reply(id, value) { send(id, { content: [{ type: "text", text: value.text }], isError: value.error }); } let buffer = ""; process.stdin.setEncoding("utf8"); process.stdin.on("data", (chunk) => { buffer += chunk; let end; while ((end = buffer.indexOf("\n")) >= 0) { const line = buffer.slice(0, end).trim(); buffer = buffer.slice(end + 1); if (!line) continue; let message; try { message = JSON.parse(line); } catch { continue; } if (message.method === "initialize") send(message.id, { protocolVersion: message.params?.protocolVersion || "2024-11-05", capabilities: { tools: {} }, serverInfo: { name: "gitea-workflow", version: "1.0.0" } }); else if (message.method === "tools/list") send(message.id, { tools: TOOLS }); else if (message.method === "tools/call") call(message.params?.name, message.params?.arguments || {}).then(res => reply(message.id, res)); } });