Skip to content

Planner hook

A Kit that declares tools can also decide which of its tools answers a question. You add one field to the manifest, and Ketvia asks your endpoint before it asks any model.

"tools": {
"endpoint": "https://kit.acme.example/ketvia/mcp",
"planner": { "endpoint": "https://kit.acme.example/ketvia/plan" },
"items": [ … ]
}
  • tools.planner.endpoint is an https URL. It follows the same verified-host rule as tools.endpoint: for an unlisted Kit the host must be on a verified domain (see Distribution).
  • When a member asks the assistant a question and your Kit’s tools are granted, Ketvia sends the question to your endpoint before any model. If you answer plan, no model is called for planning: your tool is dispatched.
  • If you answer defer, or anything goes wrong, Ketvia plans exactly as it does for a Kit without a hook.

Privacy. The hook receives the text of the question. The consent screen therefore lists “Receives the questions asked to the assistant” for every Kit with a planner.endpoint, and the Directory review checks it. You never receive a tool result through this hook, only the question and the names of the tools on offer.

Ketvia sends an HTTPS POST with a JSON body:

{
"type": "plan",
"installationId": "0b4f…",
"question": "Ayşe bakiye",
"locale": "tr",
"localDate": "2026-10-06",
"timezone": "Europe/Istanbul",
"tools": ["balance", "appointments_summary"],
"audience": "private",
"previous": null
}
Field Meaning
type Always plan.
installationId The installation the question is asked in. Answer for this installation only.
question The question text, 1 to 8000 characters.
locale tr when the member’s language is Turkish, otherwise en.
localDate Today’s date (YYYY-MM-DD) in timezone. Resolve “today” and weekdays against it.
timezone The IANA zone of your tool source (tools.timezone).
tools The tool names granted and offered in this run (audience already applied). Never plan outside this list.
audience private (an assistant DM or a private answer) or channel. Private-only tools are absent from tools in a channel.
previous null for a first question, otherwise the context described below.

Headers:

  • Ketvia-Request-Timestamp: unix seconds.
  • Ketvia-Signature: v1= plus the hex HMAC-SHA256 over v1:<timestamp>:<body>, the same scheme as events. Verify it before you trust the body; during a secret rotation the header carries several values.
  • Ketvia-Installation-Id: the installation id again, for routing.

previous is null for a first question. For a follow-up it contains:

Field Meaning
question The last human question of the thread.
plan The tool call that answered it (tool, arguments, optional emphasis), only if it was your tool; otherwise null.
choice Non-null ({ tool, argument }) when the previous tool declares "selection": { "argument": "query" } and was called with that argument. Otherwise null.

previous comes only from the run’s own follow-up parent and only when the same member asked both questions. Ketvia never builds it from another member’s question or from an unrelated thread, so a Kit cannot learn what someone else asked. It carries the question text and your own plan, never a tool result.

choice exists because a card may have asked “which record?”. Example: the member asks “Ayşe bakiye” and you plan balance { query: "Ayşe" }. The card lists three patients; the member types 4812. You receive previous.choice = { tool: "balance", argument: "query" } and answer plan.tool("balance", { patientId: "4812" }).

The member’s reply to your clarify is an ordinary follow-up: you receive it as question, and previous.question is the question you clarified (with previous.plan: null). Ketvia carries one earlier question and nothing else, and does not add the answers together. If the reply gives only part of what you need (“every 2500”, but no month count), ask again with clarify or defer; the request after that has the partial reply as its previous.question. A reply that stands on its own should therefore contain, or be easy to combine with, what the first question asked for.

Answer with 200 and a JSON body of at most 16 KB, content type JSON, one of:

type Meaning Example
plan Run this tool with these arguments. No model is called for planning. { "type": "plan", "tool": "appointments_summary", "arguments": { "date": "2026-10-06" } }
clarify Show this text as the assistant’s reply (a question back to the member). { "type": "clarify", "text": { "en": "Which patient?", "tr": "Hangi hasta?" } }
unsupported The question is recognized but cannot be answered exactly. { "type": "unsupported" }
defer Let Ketvia’s model planner choose. { "type": "defer" }
  • emphasis (an array of 1 to 4 metric keys) is only valid for tools whose card headline uses {emphasis}.
  • Properties your tool declares with const are filled in by Ketvia. Echoing them is fine; changing them is invalid.
  • clarify.text needs en; add tr for Turkish members.

Ketvia validates a plan before dispatching anything:

  • the tool must be in the request’s tools;
  • the arguments are validated against the manifest input schema (additionalProperties: false, formats and inputConstraints).

An invalid plan fails the run with the code KIT_PLAN_INVALID: the member sees “could not answer” and no tool is called. A body with "type": "plan" that is malformed (a missing or wrongly typed field, an unknown property) also fails the run with KIT_PLAN_INVALID, because you tried to plan. Any other unrecognised body (not JSON, not a JSON content type, over 16 KB, or another type) counts as a fallback (below), not as a failure.

PRIVATE_ONLY tools and write approvals apply as always: a planned write tool still shows its approval card (a batch shows all its items on one card, see Tools and approvals) and changes nothing until a member approves it.

Not every run is planned by a hook or a model. When a member asks for the balance of a record a note is linked to (the manifest’s records[].balance), Ketvia starts a private run that calls the declared balance tool directly: your hook is not called and no model is involved.

Ketvia waits 1.5 seconds for your answer and never retries. On any problem it falls back to its own model planner, as if the Kit had no hook, and logs kit_plan_fallback with the reason:

Reason Cause
timeout No answer within 1.5 seconds.
network Connection failed or was reset.
http_error A non-2xx status.
egress_blocked The host is not allowed (for example it resolves to a private address).
no_signing_secret The installation has no signing secret to sign the request with.
request_too_large The request body would exceed the size limit.
invalid_response Not JSON, over 16 KB, or not one of the four answers (a malformed plan is KIT_PLAN_INVALID instead).

Keep the hook fast and stateless, and answer defer when you are unsure. A slow hook only costs the member up to 1.5 seconds before the usual planner runs.

Each Kit sees only its own tools, in its own request, in parallel. The first plan wins. clarify and unsupported end the run only when every granted tool belongs to a Kit with a hook; otherwise the other Kits’ tools are still planned normally.

import { createServer } from 'node:http';
import { createKitServer, plan, toNodeListener } from '@ketvia/kit';
const kit = createKitServer({
signingSecret: process.env.KETVIA_SIGNING_SECRET!,
onPlan: async (request) => {
if (request.previous?.choice && /^\d+$/.test(request.question.trim()))
return plan.tool(request.previous.choice.tool, { patientId: request.question.trim() });
if (/bugün|today/i.test(request.question))
return plan.tool('appointments_summary', { date: request.localDate });
return plan.defer();
},
});
createServer(toNodeListener(kit)).listen(3000);

createKitServer serves /ketvia/plan by default, verifies the signature and parses the request for you. Without it, verify and parse yourself:

import { verifySignature, parsePlanRequest } from '@ketvia/kit';
const ok = verifySignature({
secret,
timestamp: req.headers['ketvia-request-timestamp'],
signature: req.headers['ketvia-signature'],
rawBody,
});
const request = ok ? parsePlanRequest(rawBody) : null;
  • Send a signed request yourself with signRequest(secret, timestamp, rawBody) from the SDK.
  • Golden-test your planner with a table of question to response, including the previous.choice follow-ups and a defer for everything you do not recognise.
  • Remember what the hook sees: the member’s question. The consent screen tells the installer so (“Receives the questions asked to the assistant”), and your privacy policy should say what you do with them.