> ## Documentation Index
> Fetch the complete documentation index at: https://docs.waniwani.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Installation

> Install @waniwani/sdk in a new or existing project.

## Choose your approach

> [card] [Start from template](#option-a-start-from-template)
>
> Clone a ready-to-go MCP app (server + widgets + UI) with the SDK pre-configured. Best for **new projects**.

> [card] [Add to existing project](#option-b-add-to-existing-project)
>
> Install the SDK package into your existing MCP server or app. Best for **existing projects**.

The SDK is open source and works without an API key. Set `WANIWANI_API_KEY` to connect the [Waniwani Platform](https://docs.waniwani.ai/sdk/platform/overview.md) and unlock hosted state, tracking, knowledge base, and the chat widget on top.

***

## Option A: Start from template

The [MCP Distribution Template](https://github.com/WaniWani-AI/mcp-distribution-template) gives you a production-ready **MCP app** (server + widgets + UI) with the SDK, event tracking, flows, and widgets already wired up.

> [!NOTE]
>
> The template is an MCP app. If you only need a bare MCP server (no widgets or embedded UI), skip to [Option B](#option-b-add-to-existing-project) or follow the [Quickstart](https://docs.waniwani.ai/sdk/quickstart.md). The SDK works for both.

**1. Create your repo**

Click **"Use this template"** on [GitHub](https://github.com/WaniWani-AI/mcp-distribution-template) to create a new repository from the template, or clone it directly:

```bash theme={null}
git clone https://github.com/WaniWani-AI/mcp-distribution-template.git my-mcp-server
cd my-mcp-server
```

**2. Install and run**

```bash theme={null}
bun install
cp .env.example .env
bun dev
```

The template runs without an API key. Set `WANIWANI_API_KEY` in `.env` to connect the [Platform](https://docs.waniwani.ai/sdk/platform/overview.md) and enable hosted state, tracking, and the dashboard.

**3. Customize**

The template includes a reference flow (Alpine ski lessons) you can replace with your own. If you have the [agent skill](https://docs.waniwani.ai/sdk/guides/skills.md) installed, run `/waniwani-sdk initialize` to interactively scaffold a flow tailored to your business.

***

## Option B: Add to existing project

### Requirements

* Node.js 18.17 or newer (Node 20+ recommended)
* An MCP server runtime. The SDK is tested against:
  * [`@modelcontextprotocol/sdk`](https://www.npmjs.com/package/@modelcontextprotocol/sdk)
  * [Skybridge](https://github.com/alpic-ai/skybridge) (Alpic)
  * [`@vercel/mcp-handler`](https://www.npmjs.com/package/@vercel/mcp-handler)
* A Waniwani account at [app.waniwani.ai](https://app.waniwani.ai) — optional, only needed for [Platform](https://docs.waniwani.ai/sdk/platform/overview.md) features.

`@modelcontextprotocol/sdk` and `zod` are peer dependencies. Install them in your project if you do not already have them.

### Install

```bash bun theme={null}
bun add @waniwani/sdk
```

```bash pnpm theme={null}
pnpm add @waniwani/sdk
```

```bash npm theme={null}
npm install @waniwani/sdk
```

```bash yarn theme={null}
yarn add @waniwani/sdk
```

> [!NOTE]
>
> The SDK has zero runtime dependencies and targets a sub-5KB core bundle. It runs in long-lived Node processes, serverless functions, and edge runtimes.

### Pick what you need

Three things you can add independently to an existing MCP server. Any subset works.

| Add | What it gives you | API key |
| - | - | - |
| `createFlow` | Multi-step typed flows registered as a single MCP tool. The primary entry point. | Not required. Required only if you want hosted state instead of bringing a `KvStore`. |
| `withWaniwani(server)` | Wraps existing `registerTool` handlers to emit tracking events and bridge session IDs. No handler changes. | Required for events. Bridging works without. |
| `waniwani()` client | Direct calls to Platform APIs (custom events, `identify()`, KB search). | Required. |

### Add a flow to an existing server

Assume you already have an `McpServer` instance with some tools registered on it. Define a flow and `.register()` it alongside them:

```ts theme={null}
import { createFlow, END, MemoryKvStore, START } from "@waniwani/sdk/mcp";
import { z } from "zod";
import { server } from "./mcp-server"; // your existing McpServer

const onboardingFlow = createFlow({
  id: "onboarding",
  title: "User Onboarding",
  description: "Use when a new user wants to get started.",
  state: { name: z.string().describe("Their name") },
})
  .addNode({
    id: "ask_name",
    run: ({ interrupt }) =>
      interrupt({ name: { question: "What is your name?" } }),
  })
  .addEdge(START, "ask_name")
  .addEdge("ask_name", END)
  .compile({ store: new MemoryKvStore() });

await onboardingFlow.register(server);
```

`MemoryKvStore` is fine for development and tests. For production, plug in a real backend (Redis, Upstash, Cloudflare KV, DynamoDB) or set `WANIWANI_API_KEY` to use hosted state. See [KV store adapters](https://docs.waniwani.ai/sdk/flows/kv-store.md).

### Wrap an existing server for Platform tracking

If your server already has plain `registerTool` handlers and you just want them tracked, wrap it once. No changes to the handlers themselves:

```ts theme={null}
import { withWaniwani } from "@waniwani/sdk/mcp";
import { server } from "./mcp-server"; // your existing McpServer

withWaniwani(server);
```

```bash .env theme={null}
WANIWANI_API_KEY=wwk_...
```

Every wrapped tool call now emits a `tool.called` event to your [Waniwani dashboard](https://app.waniwani.ai). Without an API key, the wrapper still bridges session IDs and forwards widget metadata but emits no events. Safe to wrap unconditionally. See [Wrap your server](https://docs.waniwani.ai/sdk/configuration/wrap-server.md) for all options.

## Create a Platform client

The `waniwani()` client is needed only when you call Platform APIs directly (custom event tracking via `track()`, `identify()`, KB search). It is **not** required for `createFlow` or `withWaniwani`.

```ts theme={null}
import { waniwani } from "@waniwani/sdk";

const wani = waniwani();
```

With no arguments, the client reads `WANIWANI_API_KEY` from the environment and uses `https://app.waniwani.ai` as the base URL.

See [API keys](https://docs.waniwani.ai/sdk/configuration/api-key.md) for how to obtain and configure the key, and [Client & KV store reference](https://docs.waniwani.ai/sdk/reference/kv-store-api.md) for the full client config.

## Also supported

* **Project config file.** Drop a `waniwani.json` at the project root with `$schema` set to `https://docs.waniwani.ai/waniwani.json`. The SDK auto-loads it on the first `waniwani()` call; the CLI uses the same file for `connect`/`dev`/evals. No explicit import required.

Next: [get your API key](https://docs.waniwani.ai/sdk/configuration/api-key.md) (if you want Platform features), then [wrap your MCP server](https://docs.waniwani.ai/sdk/configuration/wrap-server.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.