@softr/sdk
Version:
The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.
52 lines (50 loc) • 1.85 kB
JavaScript
import { assert, panic } from "./assertions.js";
//#region src/tool.ts
const TOOL = Symbol.for("@softr/sdk/tool");
/** What the MCP spec allows in a tool name. */
const TOOL_NAME = /^[A-Za-z0-9_.-]{1,128}$/;
/**
* 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),
* })
*/
function defineTool(definition) {
const { name, title, description, input, readOnly, destructive, handler } = definition;
assert(TOOL_NAME.test(name), () => `defineTool "${name}": a name is 1 to 128 letters, digits, "_", "-" or ".".`);
assert(description.trim() !== "", () => `defineTool "${name}": the description must not be empty.`);
assert(!(readOnly && destructive === true), () => `defineTool "${name}": a read-only tool cannot be destructive.`);
const strict = input.strict();
let inputSchema;
try {
inputSchema = strict["~standard"].jsonSchema.input({ target: "draft-07" });
} catch (error) {
panic(`defineTool "${name}": the input cannot be described to an assistant. ${error instanceof Error ? error.message : String(error)}`);
}
const value = {
name,
title,
description,
input: strict,
inputSchema,
readOnly,
destructive,
handler
};
return Object.assign(value, { [TOOL]: true });
}
function isTool(value) {
return typeof value === "object" && value !== null && TOOL in value;
}
//#endregion
export { defineTool, isTool };