routes.ts•4.85 kB
import type { Express } from "express";
import { createServer, type Server } from "http";
import { storage } from "./storage";
import { mcpService } from "./services/mcpService";
import { validateTokenSchema, executeToolSchema } from "@shared/schema";
import { z } from "zod";
export async function registerRoutes(app: Express): Promise<Server> {
// Validate and store GitHub token
app.post("/api/token/validate", async (req, res) => {
try {
const { token } = validateTokenSchema.parse(req.body);
// Test token by initializing GitHub session
await mcpService.initializeGitHubSession(token);
// Store token in storage
const githubToken = await storage.createGithubToken({ token });
res.json({
success: true,
message: "Token validated and stored successfully",
tokenId: githubToken.id
});
} catch (error) {
console.error("Token validation error:", error);
res.status(400).json({
success: false,
message: error instanceof Error ? error.message : "Token validation failed"
});
}
});
// Update GitHub token
app.put("/api/token/update", async (req, res) => {
try {
const { token } = validateTokenSchema.parse(req.body);
// Test new token
await mcpService.initializeGitHubSession(token);
// Create new active token (deactivates old ones)
const githubToken = await storage.createGithubToken({ token });
res.json({
success: true,
message: "Token updated successfully",
tokenId: githubToken.id
});
} catch (error) {
console.error("Token update error:", error);
res.status(400).json({
success: false,
message: error instanceof Error ? error.message : "Token update failed"
});
}
});
// Get current token status
app.get("/api/token/status", async (req, res) => {
try {
const activeToken = await storage.getActiveGithubToken();
if (!activeToken) {
return res.json({
hasToken: false,
message: "No active token found"
});
}
res.json({
hasToken: true,
tokenId: activeToken.id,
createdAt: activeToken.createdAt,
updatedAt: activeToken.updatedAt,
message: "Token is active"
});
} catch (error) {
console.error("Token status error:", error);
res.status(500).json({
success: false,
message: "Failed to get token status"
});
}
});
// Get available tools
app.get("/api/tools", async (req, res) => {
try {
const tools = await mcpService.getToolSpecs();
res.json({ tools });
} catch (error) {
console.error("Error fetching tools:", error);
res.status(500).json({
success: false,
message: "Failed to fetch tools"
});
}
});
// Execute a tool
app.post("/api/tools/execute", async (req, res) => {
try {
const { toolName, parameters } = executeToolSchema.parse(req.body);
// Get active token
const activeToken = await storage.getActiveGithubToken();
if (!activeToken) {
return res.status(401).json({
success: false,
message: "No active GitHub token found"
});
}
// Create execution record
const execution = await storage.createToolExecution({
toolName,
parameters,
});
try {
// Execute the tool
const result = await mcpService.executeTool(toolName, parameters || {}, activeToken.token);
// Update execution with result
await storage.updateToolExecutionResult(execution.id, result, "success");
res.json({
success: true,
executionId: execution.id,
result
});
} catch (executionError) {
// Update execution with error
await storage.updateToolExecutionResult(execution.id, { error: executionError instanceof Error ? executionError.message : "Unknown error" }, "failed");
throw executionError;
}
} catch (error) {
console.error("Tool execution error:", error);
res.status(500).json({
success: false,
message: error instanceof Error ? error.message : "Tool execution failed"
});
}
});
// Get tool executions history
app.get("/api/executions", async (req, res) => {
try {
const executions = await storage.getToolExecutions();
res.json({ executions });
} catch (error) {
console.error("Error fetching executions:", error);
res.status(500).json({
success: false,
message: "Failed to fetch executions"
});
}
});
const httpServer = createServer(app);
return httpServer;
}