wacrm/mcp-server/src/index.ts
Arnas Donauskas 78f2f79d3b Add MCP server for the public API
Introduce mcp-server/ — a stdio Model Context Protocol server that
wraps the /api/v1 public API so MCP clients (Claude, Cursor, etc.) can
drive a self-hosted wacrm instance in natural language.

- Thin wrapper over the existing REST API; all auth, scoping, and rate
  limiting stay server-side. No new backend surface.
- Read-only by default: write and broadcast tools are only registered
  when WACRM_ENABLE_WRITES / WACRM_ENABLE_BROADCASTS are set, layered
  on top of the API key's own scopes. send_broadcast also requires an
  explicit confirm and is marked destructive.
- Registry-ready: server.json manifest, package.json mcpName linked to
  it (io.github.arnasdon/wacrm-mcp), and glama.json for discovery.
- Docs: docs/mcp.md + README bullet + mcp-server/README.md.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-08 17:30:24 +03:00

46 lines
1.7 KiB
JavaScript

#!/usr/bin/env node
// ============================================================
// wacrm MCP server — entry point.
//
// A stdio Model Context Protocol server that exposes the wacrm
// public API (`/api/v1`) as MCP tools, so an MCP client (Claude
// Desktop, Cursor, etc.) can drive a self-hosted WhatsApp CRM in
// natural language.
//
// Transport is stdio: logs MUST go to stderr, never stdout (stdout
// is the protocol channel). Configuration comes from the environment
// — see .env.example / README.md.
// ============================================================
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { loadConfig } from './config.js';
import { WacrmClient } from './client.js';
import { registerTools } from './tools/index.js';
// package.json version, kept in sync manually with the manifest.
const VERSION = '0.1.0';
async function main(): Promise<void> {
const config = loadConfig();
const client = new WacrmClient(config);
const server = new McpServer({ name: 'wacrm-mcp', version: VERSION });
const groups = registerTools(server, client, config);
const transport = new StdioServerTransport();
await server.connect(transport);
// Stderr only — stdout is reserved for the MCP protocol.
console.error(
`wacrm MCP server v${VERSION} ready — instance ${config.baseUrl}, ` +
`tool groups: ${groups.join(', ')}` +
(config.enableWrites ? '' : ' (read-only; set WACRM_ENABLE_WRITES to allow changes)'),
);
}
main().catch((err) => {
console.error(`Failed to start wacrm MCP server: ${(err as Error).message}`);
process.exit(1);
});