UNPKG

claudeus-wp-mcp

Version:

The most comprehensive WordPress MCP server - 145 production-ready tools for complete WordPress management with AI

82 lines 2.85 kB
import { Server } from '@modelcontextprotocol/sdk/server/index.js'; import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'; import { SSEServerTransport } from '@modelcontextprotocol/sdk/server/sse.js'; import express from 'express'; import cors from 'cors'; export class McpServer { server; app; connections = new Map(); nextConnectionId = 1; capabilities = { prompts: { listChanged: true }, tools: { listChanged: true }, resources: { listChanged: true } }; constructor(name, version) { // Create server with proper initialization this.server = new Server({ name, version }, { capabilities: this.capabilities }); // Initialize express app for SSE this.app = express(); this.app.use(cors()); this.app.use(express.json()); // Note: The MCP SDK handles initialize/shutdown protocol methods automatically // No need to register custom handlers for these } trackConnection(transport) { const id = `conn_${this.nextConnectionId++}`; this.connections.set(id, { id, transport, initialized: false }); console.error(`🔌 New connection established: ${id}`); } untrackConnection(transport) { for (const [id, conn] of this.connections.entries()) { if (conn.transport === transport) { this.connections.delete(id); console.error(`🔌 Connection closed: ${id}`); break; } } } getServer() { return this.server; } getApp() { return this.app; } getActiveConnections() { return this.connections.size; } async connectStdio() { const transport = new StdioServerTransport(); this.trackConnection(transport); try { await this.server.connect(transport); } catch (error) { this.untrackConnection(transport); throw error; } } async connectSSE(port = 4000, path = '/') { this.app.get(path, (_, res) => { const transport = new SSEServerTransport(path, res); this.trackConnection(transport); this.server.connect(transport).catch(error => { this.untrackConnection(transport); console.error('Failed to connect transport:', error); res.status(500).end(); }); // Handle client disconnect res.on('close', () => { this.untrackConnection(transport); }); }); await new Promise((resolve) => { this.app.listen(port, () => { console.info(`Server listening on port ${port}`); resolve(); }); }); } } //# sourceMappingURL=server.js.map