| 1 |
#!/usr/bin/env node |
| 2 |
|
| 3 |
/** |
| 4 |
* MCP HTTP Bridge for MetaSync WordPress Plugin |
| 5 |
* |
| 6 |
* This bridge allows Claude Desktop to communicate with the MetaSync MCP server |
| 7 |
* running on WordPress via HTTP/REST API. |
| 8 |
*/ |
| 9 |
|
| 10 |
import { Server } from '@modelcontextprotocol/sdk/server/index.js'; |
| 11 |
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'; |
| 12 |
import { |
| 13 |
ListToolsRequestSchema, |
| 14 |
CallToolRequestSchema, |
| 15 |
} from '@modelcontextprotocol/sdk/types.js'; |
| 16 |
|
| 17 |
// Configuration from environment variables |
| 18 |
const WORDPRESS_URL = process.env.WORDPRESS_URL || 'http://localhost'; |
| 19 |
const WORDPRESS_API_KEY = process.env.WORDPRESS_API_KEY; |
| 20 |
|
| 21 |
if (!WORDPRESS_API_KEY) { |
| 22 |
console.error('Error: WORDPRESS_API_KEY environment variable is required'); |
| 23 |
process.exit(1); |
| 24 |
} |
| 25 |
|
| 26 |
// MCP endpoint (single endpoint with JSON-RPC) |
| 27 |
const MCP_ENDPOINT = `${WORDPRESS_URL}/wp-json/metasync/v1/mcp`; |
| 28 |
|
| 29 |
// Request ID counter for JSON-RPC |
| 30 |
let requestId = 1; |
| 31 |
|
| 32 |
/** |
| 33 |
* Make JSON-RPC request to WordPress MCP endpoint |
| 34 |
*/ |
| 35 |
async function callWordPressMCP(method, params = {}) { |
| 36 |
const payload = { |
| 37 |
jsonrpc: '2.0', |
| 38 |
id: requestId++, |
| 39 |
method: method, |
| 40 |
params: params |
| 41 |
}; |
| 42 |
|
| 43 |
const options = { |
| 44 |
method: 'POST', |
| 45 |
headers: { |
| 46 |
'Content-Type': 'application/json', |
| 47 |
'X-API-Key': WORDPRESS_API_KEY, |
| 48 |
}, |
| 49 |
body: JSON.stringify(payload) |
| 50 |
}; |
| 51 |
|
| 52 |
try { |
| 53 |
const response = await fetch(MCP_ENDPOINT, options); |
| 54 |
|
| 55 |
if (!response.ok) { |
| 56 |
const errorText = await response.text(); |
| 57 |
throw new Error(`HTTP ${response.status}: ${errorText}`); |
| 58 |
} |
| 59 |
|
| 60 |
const jsonRpcResponse = await response.json(); |
| 61 |
|
| 62 |
// Handle JSON-RPC error |
| 63 |
if (jsonRpcResponse.error) { |
| 64 |
throw new Error(jsonRpcResponse.error.message || 'Unknown JSON-RPC error'); |
| 65 |
} |
| 66 |
|
| 67 |
// Return the result field from JSON-RPC response |
| 68 |
return jsonRpcResponse.result; |
| 69 |
} catch (error) { |
| 70 |
console.error(`[MCP Bridge] WordPress MCP Error: ${error.message}`); |
| 71 |
throw error; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Initialize MCP Server |
| 77 |
*/ |
| 78 |
async function main() { |
| 79 |
console.error('[MCP Bridge] Initializing...'); |
| 80 |
console.error(`[MCP Bridge] WordPress URL: ${WORDPRESS_URL}`); |
| 81 |
console.error(`[MCP Bridge] API Key: ${WORDPRESS_API_KEY.substring(0, 8)}...`); |
| 82 |
|
| 83 |
const server = new Server( |
| 84 |
{ |
| 85 |
name: 'wordpress-metasync', |
| 86 |
version: '1.0.0', |
| 87 |
}, |
| 88 |
{ |
| 89 |
capabilities: { |
| 90 |
tools: {}, |
| 91 |
}, |
| 92 |
} |
| 93 |
); |
| 94 |
|
| 95 |
// Handle tools/list requests |
| 96 |
server.setRequestHandler(ListToolsRequestSchema, async () => { |
| 97 |
try { |
| 98 |
const result = await callWordPressMCP('tools/list'); |
| 99 |
return result; |
| 100 |
} catch (error) { |
| 101 |
console.error('Error listing tools:', error); |
| 102 |
return { tools: [] }; |
| 103 |
} |
| 104 |
}); |
| 105 |
|
| 106 |
// Handle tools/call requests |
| 107 |
server.setRequestHandler(CallToolRequestSchema, async (request) => { |
| 108 |
try { |
| 109 |
console.error(`[MCP Bridge] Calling tool: ${request.params.name}`); |
| 110 |
const result = await callWordPressMCP('tools/call', { |
| 111 |
name: request.params.name, |
| 112 |
arguments: request.params.arguments || {} |
| 113 |
}); |
| 114 |
return result; |
| 115 |
} catch (error) { |
| 116 |
console.error(`[MCP Bridge] Error calling tool:`, error); |
| 117 |
return { |
| 118 |
content: [ |
| 119 |
{ |
| 120 |
type: 'text', |
| 121 |
text: `Error: ${error.message}`, |
| 122 |
}, |
| 123 |
], |
| 124 |
isError: true, |
| 125 |
}; |
| 126 |
} |
| 127 |
}); |
| 128 |
|
| 129 |
// Connect to stdio transport |
| 130 |
const transport = new StdioServerTransport(); |
| 131 |
await server.connect(transport); |
| 132 |
|
| 133 |
console.error('[MCP Bridge] ✓ Connected to Claude Desktop'); |
| 134 |
console.error('[MCP Bridge] ✓ Ready to serve WordPress MCP tools'); |
| 135 |
} |
| 136 |
|
| 137 |
main().catch((error) => { |
| 138 |
console.error('[MCP Bridge] Fatal error:', error); |
| 139 |
process.exit(1); |
| 140 |
}); |
| 141 |
|