import { z } from "zod";
/**
* Coordination Tools - "The Linking Glue"
* Helps orchestrate other tools and suggests workflows.
*/
// ============================================
// Suggest Tool Chain
// ============================================
export const suggestToolChainSchema = {
name: "suggest_tool_chain",
description:
"Suggests a sequence of Code-MCP tools to achieve a specific complex goal",
inputSchema: z.object({
goal: z.string().describe("The user's high-level goal"),
context: z.string().optional().describe("Current project state"),
}),
};
type ToolStep = {
tool: string;
reason: string;
phase: "Plan" | "Build" | "Verify" | "Ops";
};
// Tool Knowledge Base
const TOOL_REGISTRY = {
// PLANNING & THINKING
sequential_thinking: {
tags: ["complex", "hard", "plan", "think"],
phase: "Plan",
},
plan_task: {
tags: ["feature", "task", "create", "build", "implement"],
phase: "Plan",
},
act_as_architect: {
tags: ["design", "system", "structure", "new"],
phase: "Plan",
},
search_web: { tags: ["research", "learn", "find", "unknown"], phase: "Plan" },
// ANALYSIS
derive_patterns: {
tags: ["refactor", "legacy", "clean", "audit"],
phase: "Plan",
},
visualize_system: {
tags: ["understand", "complex", "diagram", "map"],
phase: "Plan",
},
security_scan: {
tags: ["security", "audit", "check", "verify"],
phase: "Verify",
},
analyze_complexity: {
tags: ["performance", "optimize", "refactor"],
phase: "Verify",
},
// IMPLEMENTATION
full_stack_scaffold: {
tags: ["new", "project", "start", "create"],
phase: "Build",
},
generate_snippet: {
tags: ["code", "create", "implement", "feature"],
phase: "Build",
},
sql_helper: {
tags: ["db", "database", "sql", "migration", "query"],
phase: "Build",
},
generate_api_client: {
tags: ["api", "sdk", "client", "integration"],
phase: "Build",
},
// LANGUAGE SPECIFIC
analyze_python_ast: { tags: ["python"], phase: "Verify" },
analyze_go_mod: { tags: ["go", "golang"], phase: "Verify" },
typescript_helper: {
tags: ["ts", "typescript", "interface", "type"],
phase: "Build",
},
// VERIFICATION
generate_tests: {
tags: ["test", "unit", "spec", "verify", "debug"],
phase: "Verify",
},
lint_code: { tags: ["lint", "style", "check", "fix"], phase: "Verify" },
debug_problem: {
tags: ["bug", "fix", "error", "fail", "broken"],
phase: "Verify",
},
explain_code: { tags: ["understand", "read", "legacy"], phase: "Verify" },
// OPS
generate_dockerfile: {
tags: ["docker", "container", "deploy"],
phase: "Ops",
},
generate_github_actions: {
tags: ["ci", "cd", "pipeline", "automation"],
phase: "Ops",
},
generate_terraform_config: {
tags: ["infra", "cloud", "aws", "deploy"],
phase: "Ops",
},
};
export function suggestToolChainHandler(args: {
goal: string;
context?: string;
}) {
const { goal, context = "" } = args;
const combinedInput = `${goal} ${context}`.toLowerCase();
// 1. Detect Intent with Enhanced Regex
const intents = {
start: /create|new|start|scaffold|init/.test(combinedInput),
refactor: /refactor|clean|legacy|improve|rewrite/.test(combinedInput),
debug: /fix|bug|error|issue|broken|debug|fail/.test(combinedInput),
deploy: /deploy|ci|cd|release|ops|pipeline|github action/.test(
combinedInput,
),
docs: /document|readme|explain|write|guide/.test(combinedInput),
test: /test|spec|unit|coverage|verify|validate/.test(combinedInput),
};
let chain: ToolStep[] = [];
let name = "";
// 2. Build Chain based on robust logic
if (intents.refactor) {
name = "Refactoring Workflow";
chain.push(
{
tool: "project_profiler",
reason: "Analyze project state",
phase: "Plan",
},
{
tool: "derive_patterns",
reason: "Identify patterns to standardize",
phase: "Plan",
},
{
tool: "plan_task",
reason: "Create safe refactoring plan",
phase: "Plan",
},
{
tool: "generate_tests",
reason: "Ensure coverage before changes",
phase: "Verify",
},
{
tool: "reflect_on_code",
reason: "Critique implementation",
phase: "Build",
},
{ tool: "lint_code", reason: "Enforce standards", phase: "Verify" },
);
} else if (intents.debug) {
name = "Debugging Workflow";
chain.push(
{
tool: "act_as_debugger",
reason: "Adopt debugging mindset",
phase: "Plan",
},
{
tool: "debug_problem",
reason: "Systematically analyze the bug",
phase: "Plan",
},
{ tool: "read_file_snippet", reason: "Inspect code/logs", phase: "Plan" },
{
tool: "generate_tests",
reason: "Create reproduction test case",
phase: "Verify",
},
{ tool: "generate_snippet", reason: "Implement fix", phase: "Build" },
{ tool: "validate_code", reason: "Verify fix syntax", phase: "Verify" },
);
} else if (intents.deploy) {
name = "DevOps Workflow";
chain.push(
{
tool: "check_dependencies",
reason: "Audit security before deploy",
phase: "Verify",
},
{
tool: "generate_dockerfile",
reason: "Containerize application",
phase: "Ops",
},
{
tool: "generate_github_actions",
reason: "Setup CI/CD pipeline",
phase: "Ops",
},
{
tool: "generate_env_template",
reason: "Verify config requirements",
phase: "Ops",
},
);
if (
combinedInput.includes("cloud") ||
combinedInput.includes("aws") ||
combinedInput.includes("terraform")
) {
chain.push({
tool: "generate_terraform_config",
reason: "Provision Infrastructure as Code",
phase: "Ops",
});
}
} else if (intents.start) {
name = "New Project Workflow";
chain.push(
{
tool: "act_as_architect",
reason: "Plan system architecture",
phase: "Plan",
},
{
tool: "track_project",
reason: "Initialize project tracking",
phase: "Plan",
},
{
tool: "full_stack_scaffold",
reason: "Generate project structure",
phase: "Build",
},
{
tool: "generate_readme",
reason: "Create initial documentation",
phase: "Ops",
},
{
tool: "generate_github_actions",
reason: "Setup CI automation",
phase: "Ops",
},
);
} else if (intents.docs) {
name = "Documentation Workflow";
chain.push(
{
tool: "summarize_files",
reason: "Understand code context",
phase: "Plan",
},
{ tool: "generate_readme", reason: "Update/Create README", phase: "Ops" },
{
tool: "explain_code",
reason: "Generate detailed explanations",
phase: "Ops",
},
);
} else if (intents.test) {
name = "Testing Workflow";
chain.push(
{
tool: "generate_tests",
reason: "Generate test suite",
phase: "Verify",
},
{
tool: "generate_snippet",
reason: "Generate test fixtures/mocks",
phase: "Build",
},
{ tool: "validate_code", reason: "Check test syntax", phase: "Verify" },
);
} else {
// 3. Fallback: Dynamic Construction
name = "Dynamic Workflow";
chain.push({
tool: "sequential_thinking",
reason: "Deconstruct complex request",
phase: "Plan",
});
if (combinedInput.includes("api") || combinedInput.includes("backend")) {
chain.push({
tool: "act_as_backend_dev",
reason: "Load backend context",
phase: "Plan",
});
}
chain.push({
tool: "plan_task",
reason: "Create implementation checklist",
phase: "Plan",
});
chain.push({
tool: "generate_snippet",
reason: "Generate code",
phase: "Build",
});
chain.push({
tool: "reflect_on_code",
reason: "Review generated code",
phase: "Verify",
});
}
return {
content: [
{
type: "text",
text: `# Recommended Tool Chain: ${name}\n\nTo achieve "${goal}", try using these tools in order:\n\n${chain.map((s, i) => `${i + 1}. **\`${s.tool}\`** (${s.phase})\n - π‘ ${s.reason}`).join("\n")}\n\nThis chain ensures you follow best practices.`,
},
],
};
}
// ============================================
// Project Profiler
// ============================================
export const projectProfilerSchema = {
name: "project_profiler",
description:
"Analyzes the project state and suggests immediate 'Quick Wins' using Code-MCP tools",
inputSchema: z.object({
files: z.array(z.string()).describe("List of file paths in project"),
hasTests: z.boolean().describe("Does project have tests?"),
hasDocs: z.boolean().describe("Does project have README/CONTRIBUTING?"),
hasCI: z.boolean().describe("Does project have CI config?"),
}),
};
export function projectProfilerHandler(args: {
files: string[];
hasTests: boolean;
hasDocs: boolean;
hasCI: boolean;
}) {
const { files, hasTests, hasDocs, hasCI } = args;
const suggestions: string[] = [];
const missing: string[] = [];
// Check docs
if (!hasDocs) {
missing.push("Documentation");
suggestions.push(
"- Run `generate_readme` to create a professional README.",
);
suggestions.push("- Run `generate_license` to add a LICENSE file.");
suggestions.push(
"- Run `developer_rules` to generate a CONTRIBUTING guide.",
);
} else {
// Check for specific docs
if (!files.some((f) => f.includes("CONTRIBUTING")))
suggestions.push("- Consider adding a `CONTRIBUTING.md` guide.");
if (!files.some((f) => f.includes("CHANGELOG")))
suggestions.push("- Consider keeping a `CHANGELOG.md`.");
}
// Check tests
if (!hasTests) {
missing.push("Tests");
suggestions.push(
"- Run `generate_snippet(test-suite)` to add test scaffolding.",
);
suggestions.push("- Run `generate_tests` on core features.");
}
// Check CI
if (!hasCI) {
missing.push("CI/CD");
suggestions.push(
"- Run `generate_github_actions` to setup automated builds.",
);
}
// Check structure & Modern Configs
const hasDocker = files.some((f) => f.includes("Dockerfile"));
if (!hasDocker) {
suggestions.push("- Run `generate_dockerfile` to containerize the app.");
}
const hasEnv = files.some((f) => f.includes(".env"));
if (!hasEnv) {
suggestions.push(
"- Run `generate_env_template` to create a safe .env.example.",
);
}
const hasGemini = files.some((f) => f.includes("GEMINI.md"));
if (!hasGemini) {
suggestions.push(
"- **Smart**: Create `GEMINI.md` to document customized agent rules for this project.",
);
}
const hasCursorRules = files.some((f) => f.includes(".cursorrules"));
if (!hasCursorRules) {
suggestions.push(
"- Run `generate_cursor_rules` to enhance IDE AI behavior.",
);
}
// Framework specific checks
const isTypscript = files.some((f) => f.includes("tsconfig.json"));
if (isTypscript) {
suggestions.push("- Run `lint_code` to check for strict type compliance.");
}
const isPython = files.some((f) => f.endsWith(".py"));
if (
isPython &&
!files.some(
(f) => f.includes("requirements.txt") || f.includes("pyproject.toml"),
)
) {
suggestions.push(
"- **Critical**: Python project detected but no dependency file found.",
);
}
return {
content: [
{
type: "text",
text: `# Project Profiler Report\n\n**Missing Critical Areas**: ${missing.length ? missing.join(", ") : "None! π"}\n\n## π Quick Wins (Recommended Actions)\n\n${suggestions.length ? suggestions.join("\n") : "Project looks great! Analyzed " + files.length + " files."}`,
},
],
};
}
// Export all
export const coordinationTools = {
suggestToolChainSchema,
suggestToolChainHandler,
projectProfilerSchema,
projectProfilerHandler,
};