Skip to content
This repository was archived by the owner on Jun 3, 2026. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ export type { InvokableTool, ToolContext, ToolStreamEventData, ToolStreamGenerat
export { Tool, ToolStreamEvent } from './tools/tool.js'

// FunctionTool implementation
export { FunctionTool } from './tools/function-tool.js'
export type { FunctionToolConfig, FunctionToolCallback } from './tools/function-tool.js'
export { FunctionTool, fromJsonSchema } from './tools/function-tool.js'
export type { FunctionToolConfig, FunctionToolCallback, FromJsonSchemaConfig } from './tools/function-tool.js'

// ZodTool implementation
export { ZodTool } from './tools/zod-tool.js'
Expand Down
27 changes: 26 additions & 1 deletion src/tools/__tests__/tool.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { FunctionTool } from '../function-tool.js'
import { FunctionTool, fromJsonSchema } from '../function-tool.js'
import { Tool, ToolStreamEvent } from '../tool.js'
import type { ToolContext } from '../tool.js'
import type { JSONValue } from '../../types/json.js'
Expand All @@ -8,6 +8,31 @@ import { createMockContext } from '../../__fixtures__/tool-helpers.js'
import { collectGenerator } from '../../__fixtures__/model-test-helpers.js'

describe('FunctionTool', () => {
describe('fromJsonSchema', () => {
it('matches constructor with inputSchema', () => {
const schema = {
type: 'object' as const,
properties: { id: { type: 'string' as const } },
required: ['id'],
}
const viaHelper = fromJsonSchema({
name: 'lookup',
description: 'Looks up by id',
schema,
callback: (input) => (input as { id: string }).id,
})
const viaCtor = new FunctionTool({
name: 'lookup',
description: 'Looks up by id',
inputSchema: schema,
callback: (input) => (input as { id: string }).id,
})
expect(viaHelper.name).toBe(viaCtor.name)
expect(viaHelper.description).toBe(viaCtor.description)
expect(viaHelper.toolSpec).toEqual(viaCtor.toolSpec)
})
})

describe('properties', () => {
it('has a non-empty toolName', () => {
const tool = new FunctionTool({
Expand Down
28 changes: 28 additions & 0 deletions src/tools/function-tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,31 @@ export class FunctionTool extends Tool implements InvokableTool<unknown, JSONVal
}
}
}

/**
* Same shape as {@link FunctionToolConfig} but uses `schema` instead of `inputSchema`
* for call sites that already model tools as JSON Schema objects.
*/
export interface FromJsonSchemaConfig {
/** The unique name of the tool */
name: string
/** Human-readable description of the tool's purpose */
description: string
/** JSON Schema defining the expected input structure */
schema: JSONSchema
/** Function that implements the tool logic */
callback: FunctionToolCallback
}

/**
* Builds a {@link FunctionTool} from a JSON Schema definition (no Zod).
* Prefer this when tool definitions already use a `schema` field.
*/
export function fromJsonSchema(config: FromJsonSchemaConfig): FunctionTool {
return new FunctionTool({
name: config.name,
description: config.description,
inputSchema: config.schema,
callback: config.callback,
})
}
Loading