@coretext-ai/qa-gusto-c0d1e2f3-a4b5-4678-b901-012345678901
Version:
MCP server with gusto integration
106 lines • 4.21 kB
JavaScript
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { CallToolRequestSchema, ErrorCode, ListToolsRequestSchema, McpError, } from '@modelcontextprotocol/sdk/types.js';
import { config } from 'dotenv';
import { loadConfig, validateConfig } from './config.js';
import { LogShipper } from './services/log-shipper.js';
import { Logger } from './services/logger.js';
// Load environment variables
config();
// Import tools from each template
import { GustoTools } from './tools/gusto-tools.js';
import { GustoClient } from './clients/gusto-client.js';
class GustoMcpServerServer {
constructor() {
// Initialize logging first
this.initializeLogging();
this.server = new Server({
name: 'gusto-mcp-server',
version: '1.0.0',
}, {
capabilities: {
tools: {},
},
});
// Initialize template clients and tools
// Regular client - pass configuration object
this.gustoClient = new GustoClient({
authToken: process.env.GUSTO_API_TOKEN,
gUSTOAPITOKEN: process.env.GUSTO_API_TOKEN,
logger: this.logger
});
this.gustoTools = new GustoTools(this.gustoClient);
this.setupHandlers();
}
initializeLogging() {
const config = loadConfig();
const validation = validateConfig(config);
if (!validation.isValid) {
console.error('Configuration validation failed:', validation.errors);
process.exit(1);
}
this.logShipper = new LogShipper(config.logShipping);
this.logger = new Logger({
logLevel: config.logShipping.logLevel,
component: 'server',
enableConsole: true,
enableShipping: config.logShipping.enabled,
serverName: 'gusto-mcp-server',
logShipper: this.logShipper
});
this.logger.info('SERVER_INIT', 'MCP server initializing', {
serverName: 'gusto-mcp-server',
logShippingEnabled: config.logShipping.enabled,
logLevel: config.logShipping.logLevel
});
}
setupHandlers() {
this.server.setRequestHandler(ListToolsRequestSchema, async () => {
const tools = [];
// Add gusto tools
tools.push(...this.gustoTools.getToolDefinitions());
return { tools };
});
this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
const { name, arguments: args } = request.params;
try {
// Handle gusto tools
if (this.gustoTools.canHandle(name)) {
return await this.gustoTools.executeTool(name, args);
}
throw new McpError(ErrorCode.MethodNotFound, `Unknown tool: ${name}`);
}
catch (error) {
if (error instanceof McpError) {
throw error;
}
throw new McpError(ErrorCode.InternalError, `Tool execution failed: ${error instanceof Error ? error.message : String(error)}`);
}
});
}
async run() {
const transport = new StdioServerTransport();
await this.server.connect(transport);
this.logger.info('SERVER_START', 'MCP server started successfully', {
serverName: 'gusto-mcp-server',
transport: 'stdio'
});
console.error('gusto-mcp-server MCP server running on stdio');
// Handle graceful shutdown for log shipping
const shutdown = async () => {
this.logger.info('SERVER_SHUTDOWN', 'MCP server shutting down', {
serverName: 'gusto-mcp-server'
});
if (this.logShipper) {
await this.logShipper.shutdown();
}
process.exit(0);
};
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
}
}
const server = new GustoMcpServerServer();
server.run().catch(console.error);
//# sourceMappingURL=index.js.map