@softr/sdk
Version:
The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.
69 lines (68 loc) • 2.65 kB
TypeScript
import { User } from "./auth/user.js";
import { z } from "zod";
//#region src/tool.d.ts
declare const TOOL: unique symbol;
/** A tool from {@link defineTool}, ready for `softrMcpMiddleware({ tools })`. */
type Tool = {
readonly name: string;
readonly [TOOL]: true;
};
type ToolDefinition<TInput extends z.ZodObject, TOutput> = {
/** What the assistant calls it, e.g. `create_issue`. Letters, digits, `_`, `-` and `.`. */
name: string;
/** A short name for people, e.g. "Create issue". */
title?: string;
/** What the tool does and when to use it. The assistant picks tools by this. */
description: string;
/**
* The arguments. Describe each field with `.describe()`: the assistant reads it.
* Top-level keys the schema does not name are refused, not dropped. A nested
* object drops them, unless it is a `z.strictObject`.
*/
input: TInput;
/**
* `true` when the tool only reads. Required, so every tool says which it is.
*
* The SDK calls that change data or send something fail inside a read-only
* tool. It is the only kind an assistant the user approved to read only may call.
*/
readOnly: boolean;
/**
* `true` when the tool deletes or overwrites data, `false` when it only adds.
* Only for a tool that is not read-only.
*
* Left out, the assistant is told neither, and treats the tool as one that
* may delete or overwrite.
*/
destructive?: boolean;
/**
* Runs the tool as the signed-in user. The `ds` calls it reaches act as that
* user, with their groups, as a click in the app would. Return JSON.
*
* Never call `ds`, or anything else from `@softr/sdk/server`, in the handler
* itself: the production build fails. Call a helper wrapped in
* `createServerOnlyFn` from `@tanstack/react-start`.
*/
handler: (input: z.output<TInput>, context: {
user: User;
}) => Promise<TOutput>;
};
/**
* An MCP tool that runs the app's own logic, for `softrMcpMiddleware({ tools })`.
*
* @example
* // createIssueFor is wrapped in createServerOnlyFn, and calls `ds`.
* export const createIssue = defineTool({
* name: 'create_issue',
* description: 'Create an issue in a project. Returns the new issue with its key.',
* input: z.object({
* projectId: z.string().describe('The project record id.'),
* title: z.string().describe('A one-line summary.'),
* }),
* readOnly: false,
* handler: async (input, { user }) => createIssueFor(user, input),
* })
*/
declare function defineTool<TInput extends z.ZodObject, TOutput>(definition: ToolDefinition<TInput, TOutput>): Tool;
//#endregion
export { Tool, ToolDefinition, defineTool };