#!/usr/bin/env node
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import {
CallToolRequestSchema,
ListToolsRequestSchema,
} from '@modelcontextprotocol/sdk/types.js';
import { tools } from './tools/index';
class ContentManagerMCPServer {
private server: Server;
constructor() {
this.server = new Server(
{
name: 'content-manager-mcp',
version: '1.0.0'
}
);
this.setupToolHandlers();
this.setupErrorHandling();
}
private setupToolHandlers(): void {
this.server.setRequestHandler(ListToolsRequestSchema, async () => {
return {
tools: Object.entries(tools).map(([name, tool]) => ({
name,
description: tool.description,
inputSchema: tool.inputSchema,
})),
};
});
this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
const { name, arguments: args } = request.params;
if (!name || !(name in tools)) {
throw new Error(`Unknown tool: ${name}`);
}
try {
const tool = tools[name as keyof typeof tools];
const result = await tool.handler(args ?? {});
return result;
} catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
return {
content: [
{
type: 'text',
text: `Error executing tool "${name}": ${errorMessage}`
}
],
isError: true
};
}
});
}
private setupErrorHandling(): void {
this.server.onerror = (error) => {
console.error('[MCP Error]', error);
};
process.on('SIGINT', async () => {
await this.server.close();
process.exit(0);
});
process.on('SIGTERM', async () => {
await this.server.close();
process.exit(0);
});
}
async run(): Promise<void> {
const transport = new StdioServerTransport();
await this.server.connect(transport);
// Log server info to stderr (won't interfere with MCP protocol on stdout)
console.error('Content Manager MCP Server running on stdio');
console.error('Available tools:', Object.keys(tools).join(', '));
}
}
// Main execution
async function main(): Promise<void> {
const server = new ContentManagerMCPServer();
await server.run();
}
// Handle uncaught errors
process.on('uncaughtException', (error) => {
console.error('Uncaught Exception:', error);
process.exit(1);
});
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
process.exit(1);
});
if (import.meta.url === `file://${process.argv[1]}`) {
main().catch((error: unknown) => {
console.error('Failed to start server:', error);
process.exit(1);
});
}