create_session
Create a new WhatsApp session by providing a unique name. The session enables independent message management.
Instructions
Create a new WhatsApp session with the given name
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| name | Yes | Unique name for the new session |
Implementation Reference
- src/tools/sessions.ts:26-30 (handler)The handler function that executes the create_session tool logic - it destructures `name` from the input, calls openwaClient with POST /sessions, and returns the result.
async ({ name }) => { const data = await openwaClient({ method: "POST", path: "/sessions", body: { name } }); return { content: [{ type: "text" as const, text: JSON.stringify(data, null, 2) }] }; } ); - src/tools/sessions.ts:22-24 (schema)The input schema for create_session - expects a single `name` parameter (zod string).
inputSchema: { name: z.string().describe("Unique name for the new session"), }, - src/tools/sessions.ts:18-30 (registration)Registration of the create_session tool via server.registerTool with name, schema, and handler.
server.registerTool( "create_session", { description: "Create a new WhatsApp session with the given name", inputSchema: { name: z.string().describe("Unique name for the new session"), }, }, async ({ name }) => { const data = await openwaClient({ method: "POST", path: "/sessions", body: { name } }); return { content: [{ type: "text" as const, text: JSON.stringify(data, null, 2) }] }; } ); - src/index.ts:4-16 (registration)Import of the registerSessionTools registration function.
import { registerSessionTools } from "./tools/sessions.js"; import { registerMessageTools } from "./tools/messages.js"; import { registerBulkTools } from "./tools/bulk.js"; import { registerGroupTools } from "./tools/groups.js"; import { registerContactTools } from "./tools/contacts.js"; import { registerWebhookTools } from "./tools/webhooks.js"; import { registerLabelTools } from "./tools/labels.js"; import { registerMediaTools } from "./tools/media.js"; const server = new McpServer({ name: "openwa-mcp", version: "1.0.0" }); registerSessionTools(server); registerMessageTools(server); - src/client.ts:10-35 (helper)The openwaClient helper used by create_session to make HTTP requests to the OpenWA API.
export async function openwaClient<T = unknown>(opts: RequestOptions): Promise<T> { const url = `${BASE_URL}${opts.path}`; const headers: Record<string, string> = { "Content-Type": "application/json", "X-API-Key": API_KEY, }; const res = await fetch(url, { method: opts.method, headers, body: opts.body ? JSON.stringify(opts.body) : undefined, }); const text = await res.text(); if (!res.ok) { throw new Error(`OpenWA API ${res.status}: ${text}`); } try { return JSON.parse(text) as T; } catch { return text as T; } }