server.js•1.32 kB
// src/core/server.js
require('dotenv').config(); // <-- ADD THIS LINE AT THE TOP
const express = require('express');
const bodyParser = require('body-parser');
const config = require('config');
const { loadTools, executeTool } = require('./toolRegistry');
const app = express();
app.use(bodyParser.json());
const PORT = config.get('server.port');
const MCP_ENDPOINT = config.get('server.mcp_endpoint');
// Endpoint to list all available tools for the AI agent
app.get('/tools', (req, res) => {
res.json(listTools());
});
// Main endpoint for executing tools
app.post(MCP_ENDPOINT, async (req, res) => {
const { method, params, id } = req.body;
const result = await executeTool(method, params);
const response = {
jsonrpc: '2.0',
id: id,
result: result.error ? undefined : result.data,
error: result.error ? result.error : undefined
};
res.json(response);
});
// Start the server after initializing services
async function startServer() {
await loadTools(); // Load tools from enabled connectors
app.listen(PORT, () => {
console.log(`✅ Core MCP Server running on http://localhost:${PORT}`);
console.log(` - Tool Execution Endpoint: ${MCP_ENDPOINT}`);
console.log(` - Available Tools: /tools`);
});
}
startServer();