import { z } from "zod";
import fs from "fs/promises";
import path from "path";
// --- Tools ---
export const analyzeGoModSchema = {
name: "analyze_go_mod",
description: "Parse go.mod file to understand module dependencies.",
inputSchema: z.object({
filePath: z.string().describe("Path to go.mod file"),
}),
};
export const goStructHelperSchema = {
name: "go_struct_helper",
description: "Generate Go struct definitions.",
inputSchema: z.object({
name: z.string().describe("Struct name"),
fields: z.array(z.object({
name: z.string(),
type: z.string(),
jsonTag: z.string().optional()
})).describe("List of fields"),
}),
};
// --- Handlers ---
export async function analyzeGoModHandler(args: { filePath: string }) {
try {
const content = await fs.readFile(args.filePath, "utf-8");
const moduleMatch = content.match(/^module\s+(.+)$/m);
const goVersionMatch = content.match(/^go\s+(.+)$/m);
const requireBlock = content.match(/require\s+\(([\s\S]*?)\)/);
const requires = requireBlock
? requireBlock[1]
.split("\n")
.map(l => l.trim())
.filter(l => l && !l.startsWith("//"))
.map(l => l.split(" ")[0])
: [];
return {
content: [{
type: "text",
text: JSON.stringify({
module: moduleMatch ? moduleMatch[1] : "unknown",
goVersion: goVersionMatch ? goVersionMatch[1] : "unknown",
dependencies: requires
}, null, 2)
}]
};
} catch (error) {
return {
content: [{ type: "text", text: `Error reading go.mod: ${error instanceof Error ? error.message : String(error)}` }],
isError: true
};
}
}
export async function goStructHelperHandler(args: { name: string, fields: { name: string, type: string, jsonTag?: string }[] }) {
const { name, fields } = args;
let struct = `type ${name} struct {\n`;
for (const field of fields) {
struct += ` ${field.name} ${field.type}`;
if (field.jsonTag) {
struct += ` \`json:"${field.jsonTag}"\``;
}
struct += "\n";
}
struct += "}";
return {
content: [{ type: "text", text: struct }]
};
}