diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e840040..b7dac6a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -31,5 +31,6 @@ jobs: bun add --ignore-scripts "$artifact" bun -e 'import { providers } from "@checkfu/integrations/providers"; if (!providers.openai) process.exit(1)' bun -e 'import { connectors } from "@checkfu/integrations/connectors"; if (!connectors.linear) process.exit(1)' + node --input-type=module -e 'import { searchToolCatalog } from "@checkfu/integrations/tool-catalog"; if (searchToolCatalog([{ name: "listIssues" }], "list issues").items[0]?.name !== "listIssues") process.exit(1)' node --input-type=module -e 'import { catalogByKind } from "@checkfu/integrations/catalog"; if (!catalogByKind.channel.some(entry => entry.id === "discord")) process.exit(1)' node --input-type=module -e 'import { discordReplyRequest } from "@checkfu/integrations/channels"; if (discordReplyRequest("123", "fixture", "reply").redirect !== "manual") process.exit(1)' diff --git a/README.md b/README.md index 20f464b..c17ebe7 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,25 @@ const { url, transport, auth } = connectors.linear.mcp Use the URL with a Streamable HTTP MCP client. The catalog lists the supported authentication methods and suggested OAuth scopes, where available. Your application handles OAuth discovery or attaches a separately stored bearer token to requests to the connector URL. See the [Linear MCP documentation](https://linear.app/docs/mcp) for supported credentials and scopes. +## Tool catalogs + +Use `@checkfu/integrations/tool-catalog` to present the tools your application has discovered. Supply definitions from an MCP client's `tools/list` response or any source with tool names. Each result keeps the original definition and exact callable name. + +```ts +import { searchToolCatalog } from "@checkfu/integrations/tool-catalog" + +const tools = [ + { name: "listIssues", description: "Show issues in the repository." }, + { name: "createIssue", description: "Open a new issue." }, +] +const page = searchToolCatalog(tools, "list issues", { limit: 20 }) +const next = page.nextOffset === null + ? null + : searchToolCatalog(tools, "list issues", { limit: 20, offset: page.nextOffset }) +``` + +Search prefers a name match over a title or description match and accepts camel case, underscores, hyphens, and spaces. An empty query lists the available tools in input order. Queries have a 256-character limit and pages have a 100-entry limit. Keep the input catalog stable when paging. The results contain only tools the caller supplied. The host supplies the connection and decides what each user or agent may access. + ## Incoming events and channels The [generic signed webhook](catalog/triggers/webhook.yaml) uses Standard Webhooks. The verifier returns the exact authenticated delivery ID. IDs are opaque, nonempty strings that exclude the signing delimiter (`.`). Apply the host's transport and storage limits when using them as idempotency keys. The `@checkfu/integrations/webhooks` export verifies native GitHub, Linear, and Notion deliveries and returns a verified event, account, and deduplication ID. The [Discord channel](catalog/services/discord.yaml) uses signed interactions. Its reply builder returns a standard Fetch request with redirects disabled through manual handling. The host sends the request and handles the response. diff --git a/dist/tool-catalog.d.ts b/dist/tool-catalog.d.ts new file mode 100644 index 0000000..564e149 --- /dev/null +++ b/dist/tool-catalog.d.ts @@ -0,0 +1,19 @@ +export interface ToolCatalogEntry { + readonly name: string; + readonly title?: string; + readonly description?: string; +} +export interface ToolCatalogPageOptions { + readonly offset?: number; + readonly limit?: number; +} +export interface ToolCatalogPage { + readonly items: readonly T[]; + readonly total: number; + readonly nextOffset: number | null; +} +/** + * Searches entries supplied by the caller and returns one page of the original entries. + * An empty query lists the catalog in input order. Ties retain that same order. + */ +export declare function searchToolCatalog(tools: readonly T[], query: string, { offset, limit }?: ToolCatalogPageOptions): ToolCatalogPage; diff --git a/dist/tool-catalog.js b/dist/tool-catalog.js new file mode 100644 index 0000000..649c2ca --- /dev/null +++ b/dist/tool-catalog.js @@ -0,0 +1,50 @@ +const maxQueryChars = 256; +const maxSearchableChars = 4096; +const maxPageSize = 100; +function words(text) { + return text.slice(0, maxSearchableChars) + .normalize("NFKC") + .replace(/(\p{Lu})(\p{Lu}\p{Ll})/gu, "$1 $2") + .replace(/([\p{Ll}\p{N}])(\p{Lu})/gu, "$1 $2") + .toLowerCase() + .match(/[\p{L}\p{N}]+/gu) ?? []; +} +function matches(query, available) { + return query.every(term => available.some(word => word.startsWith(term))); +} +function rank(tool, exactName, query) { + if (tool.name.toLowerCase() === exactName) + return 4; + if (query.length === 0) + return 0; + const name = words(tool.name); + if (matches(query, name)) + return 3; + const headings = [...name, ...words(tool.title ?? "")]; + if (matches(query, headings)) + return 2; + return matches(query, [...headings, ...words(tool.description ?? "")]) ? 1 : 0; +} +/** + * Searches entries supplied by the caller and returns one page of the original entries. + * An empty query lists the catalog in input order. Ties retain that same order. + */ +export function searchToolCatalog(tools, query, { offset = 0, limit = 20 } = {}) { + if (query.length > maxQueryChars) + throw new RangeError("Tool search accepts up to 256 characters."); + if (!Number.isSafeInteger(offset) || offset < 0) + throw new RangeError("Tool search offset must be a nonnegative safe integer."); + if (!Number.isSafeInteger(limit) || limit < 1 || limit > maxPageSize) { + throw new RangeError("Tool search limit must be an integer from 1 to 100."); + } + const exactName = query.trim().toLowerCase(); + const terms = words(query); + const found = exactName === "" ? tools : tools + .map(tool => ({ tool, rank: rank(tool, exactName, terms) })) + .filter(entry => entry.rank > 0) + .sort((left, right) => right.rank - left.rank) + .map(entry => entry.tool); + const items = found.slice(offset, offset + limit); + const next = offset + items.length; + return { items, total: found.length, nextOffset: next < found.length ? next : null }; +} diff --git a/package.json b/package.json index 9d70fdf..10a9189 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "./channels": { "types": "./dist/channels.d.ts", "default": "./dist/channels.js" }, "./connectors": { "types": "./dist/connectors.d.ts", "default": "./dist/connectors.js" }, "./providers": { "types": "./dist/providers.d.ts", "default": "./dist/providers.js" }, + "./tool-catalog": { "types": "./dist/tool-catalog.d.ts", "default": "./dist/tool-catalog.js" }, "./triggers": { "types": "./dist/triggers.d.ts", "default": "./dist/triggers.js" }, "./webhooks": { "types": "./dist/webhooks.d.ts", "default": "./dist/webhooks.js" } }, diff --git a/src/tool-catalog.ts b/src/tool-catalog.ts new file mode 100644 index 0000000..a82ea6a --- /dev/null +++ b/src/tool-catalog.ts @@ -0,0 +1,70 @@ +export interface ToolCatalogEntry { + readonly name: string + readonly title?: string + readonly description?: string +} + +export interface ToolCatalogPageOptions { + readonly offset?: number + readonly limit?: number +} + +export interface ToolCatalogPage { + readonly items: readonly T[] + readonly total: number + readonly nextOffset: number | null +} + +const maxQueryChars = 256 +const maxSearchableChars = 4096 +const maxPageSize = 100 + +function words(text: string): readonly string[] { + return text.slice(0, maxSearchableChars) + .normalize("NFKC") + .replace(/(\p{Lu})(\p{Lu}\p{Ll})/gu, "$1 $2") + .replace(/([\p{Ll}\p{N}])(\p{Lu})/gu, "$1 $2") + .toLowerCase() + .match(/[\p{L}\p{N}]+/gu) ?? [] +} + +function matches(query: readonly string[], available: readonly string[]): boolean { + return query.every(term => available.some(word => word.startsWith(term))) +} + +function rank(tool: ToolCatalogEntry, exactName: string, query: readonly string[]): number { + if (tool.name.toLowerCase() === exactName) return 4 + if (query.length === 0) return 0 + const name = words(tool.name) + if (matches(query, name)) return 3 + const headings = [...name, ...words(tool.title ?? "")] + if (matches(query, headings)) return 2 + return matches(query, [...headings, ...words(tool.description ?? "")]) ? 1 : 0 +} + +/** + * Searches entries supplied by the caller and returns one page of the original entries. + * An empty query lists the catalog in input order. Ties retain that same order. + */ +export function searchToolCatalog( + tools: readonly T[], + query: string, + { offset = 0, limit = 20 }: ToolCatalogPageOptions = {}, +): ToolCatalogPage { + if (query.length > maxQueryChars) throw new RangeError("Tool search accepts up to 256 characters.") + if (!Number.isSafeInteger(offset) || offset < 0) throw new RangeError("Tool search offset must be a nonnegative safe integer.") + if (!Number.isSafeInteger(limit) || limit < 1 || limit > maxPageSize) { + throw new RangeError("Tool search limit must be an integer from 1 to 100.") + } + + const exactName = query.trim().toLowerCase() + const terms = words(query) + const found = exactName === "" ? tools : tools + .map(tool => ({ tool, rank: rank(tool, exactName, terms) })) + .filter(entry => entry.rank > 0) + .sort((left, right) => right.rank - left.rank) + .map(entry => entry.tool) + const items = found.slice(offset, offset + limit) + const next = offset + items.length + return { items, total: found.length, nextOffset: next < found.length ? next : null } +} diff --git a/test/tool-catalog.test.mjs b/test/tool-catalog.test.mjs new file mode 100644 index 0000000..bb3a02a --- /dev/null +++ b/test/tool-catalog.test.mjs @@ -0,0 +1,56 @@ +import assert from "node:assert/strict" +import { test } from "node:test" +import { searchToolCatalog } from "@checkfu/integrations/tool-catalog" + +const entries = [ + { name: "get_item", description: "Look up an issue in the backlog." }, + { name: "find", title: "Look up an issue" }, + { name: "lookupIssue", inputSchema: { type: "object" } }, + { name: "mcp__tracker__lookup_issue" }, + { name: "list_issue" }, +] + +test("keeps exact callable names first and ranks names above titles and descriptions", () => { + const names = query => searchToolCatalog(entries, query).items.map(({ name }) => name) + assert.deepEqual(names("lookupIssue"), ["lookupIssue", "mcp__tracker__lookup_issue"]) + assert.deepEqual(names("lookup_issue"), names("lookup-issue")) + assert.deepEqual(names("look issue"), [ + "lookupIssue", "mcp__tracker__lookup_issue", "find", "get_item", + ]) + assert.equal(searchToolCatalog(entries, "lookupIssue").items[0], entries[2]) +}) + +test("returns stable pages and includes every match once", () => { + assert.deepEqual(searchToolCatalog(entries, "issue", { limit: 2 }), { + items: entries.slice(2, 4), total: 5, nextOffset: 2, + }) + assert.deepEqual(searchToolCatalog(entries, "issue", { limit: 2, offset: 2 }), { + items: [entries[4], entries[1]], total: 5, nextOffset: 4, + }) + assert.deepEqual(searchToolCatalog(entries, "issue", { limit: 2, offset: 4 }), { + items: [entries[0]], total: 5, nextOffset: null, + }) + assert.deepEqual(searchToolCatalog(entries, "issue", { offset: 5 }), { + items: [], total: 5, nextOffset: null, + }) +}) + +test("lists in input order for empty queries and reports empty results explicitly", () => { + assert.deepEqual(searchToolCatalog(entries, " ", { limit: 2 }), { + items: entries.slice(0, 2), total: 5, nextOffset: 2, + }) + const empty = { items: [], total: 0, nextOffset: null } + assert.deepEqual(searchToolCatalog([], ""), empty) + assert.deepEqual(searchToolCatalog(entries, "nothing matches"), empty) + assert.deepEqual(searchToolCatalog(entries, "???"), empty) +}) + +test("bounds page inputs and searchable fields", () => { + assert.throws(() => searchToolCatalog(entries, "x".repeat(257)), RangeError) + assert.throws(() => searchToolCatalog(entries, "issue", { offset: -1 }), RangeError) + assert.throws(() => searchToolCatalog(entries, "issue", { limit: 2.5 }), RangeError) + assert.throws(() => searchToolCatalog(entries, "issue", { limit: 101 }), RangeError) + const entry = { name: "read", description: "x".repeat(4096) + " excluded" } + assert.deepEqual(searchToolCatalog([entry], "excluded").items, []) + assert.deepEqual(searchToolCatalog([entry], "read").items, [entry]) +})