All files / src tool.ts

82.85% Statements 87/105
82.45% Branches 47/57
75.55% Functions 34/45
83.83% Lines 83/99

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565                                                                                    280x       13x 13x   11x         7x         1x             1x                     13x                         280x 280x 280x 280x 280x 280x 158x               3x 3x 2x   3x             30x 19x 19x 19x   11x                   170x             118x 118x             11x 11x   1x         10x         1x               9x     11x   4x 4x                         48x                     11x                             3x                       7x 7x                                                   4x 4x   4x         1x 1x                             21x           21x       21x     21x           21x             2x 2x   1x               21x                                                                 151x             23x             13x                                                                                       6x               2x               5x                     2x 2x 6x   2x                                                         2x     2x               2x   3x   3x                   2x                                                           13x 13x 61x 61x     13x             1x 1x   1x   3x 2x   1x                 126x                                  
import { type JSONSchema7 as AISDKJSONSchema, jsonSchema } from 'ai';
import type { Tool as AnthropicTool } from '@anthropic-ai/sdk/resources';
import type { McpSdkServerConfigWithInstance } from '@anthropic-ai/claude-agent-sdk';
import type { ChatCompletionFunctionTool } from 'openai/resources/chat/completions';
import type { FunctionTool as OpenAIResponsesFunctionTool } from 'openai/resources/responses/responses';
import type { OverrideProperties } from 'type-fest';
import { peerDependencies } from '../package.json';
import { RequestBuilder } from './requestBuilder';
import type {
	AISDKToolDefinition,
	AISDKToolResult,
	ClaudeAgentSdkOptions,
	ExecuteConfig,
	ExecuteOptions,
	HttpExecuteConfig,
	JsonObject,
	JSONSchema,
	LocalExecuteConfig,
	RpcExecuteConfig,
	ToolExecution,
	ToolParameters,
} from './types';
 
import { StackOneError } from './utils/error-stackone';
import { tryImport } from './utils/try-import';
 
/**
 * JSON Schema with type narrowed to 'object'
 * Used for tool parameter schemas which are always objects
 */
type ObjectJSONSchema = OverrideProperties<JSONSchema, { type: 'object' }>;
 
/**
 * Base class for all tools. Provides common functionality for executing API calls
 * and converting to various formats (OpenAI, AI SDK)
 */
export class BaseTool {
	name: string;
	description: string;
	parameters: ToolParameters;
	executeConfig: ExecuteConfig;
	protected requestBuilder?: RequestBuilder;
	#exposeExecutionMetadata = true;
	#headers: Record<string, string>;
 
	private createExecutionMetadata(): ToolExecution {
		const config = (() => {
			switch (this.executeConfig.kind) {
				case 'http':
					return {
						kind: 'http',
						method: this.executeConfig.method,
						url: this.executeConfig.url,
						bodyType: this.executeConfig.bodyType,
						params: this.executeConfig.params.map((param) => ({
							...param,
						})),
					} satisfies HttpExecuteConfig;
				case 'rpc':
					return {
						kind: 'rpc',
						method: this.executeConfig.method,
						url: this.executeConfig.url,
						payloadKeys: { ...this.executeConfig.payloadKeys },
					} satisfies RpcExecuteConfig;
				case 'local':
					return {
						kind: 'local',
						identifier: this.executeConfig.identifier,
						description: this.executeConfig.description,
					} satisfies LocalExecuteConfig;
				default:
					this.executeConfig satisfies never;
					throw new StackOneError('Unsupported executeConfig kind');
			}
		})();
 
		return {
			config,
			headers: this.getHeaders(),
		};
	}
 
	constructor(
		name: string,
		description: string,
		parameters: ToolParameters,
		executeConfig: ExecuteConfig,
		headers?: Record<string, string>,
	) {
		this.name = name;
		this.description = description;
		this.parameters = parameters;
		this.executeConfig = executeConfig;
		this.#headers = { ...headers };
		if (executeConfig.kind === 'http') {
			this.requestBuilder = new RequestBuilder(executeConfig, this.#headers);
		}
	}
 
	/**
	 * Set headers for this tool
	 */
	setHeaders(headers: Record<string, string>): BaseTool {
		this.#headers = { ...this.#headers, ...headers };
		if (this.requestBuilder) {
			this.requestBuilder.setHeaders(headers);
		}
		return this;
	}
 
	/**
	 * Get the current headers
	 */
	getHeaders(): Record<string, string> {
		if (this.requestBuilder) {
			const currentHeaders = this.requestBuilder.getHeaders();
			this.#headers = { ...currentHeaders };
			return currentHeaders;
		}
		return { ...this.#headers };
	}
 
	/**
	 * Extract connector/provider prefix from the tool name.
	 *
	 * Tool names follow the format `{connector}_{action}_{entity}`,
	 * e.g. `"bamboohr_list_employees"` → `"bamboohr"`.
	 */
	get connector(): string {
		return this.name.split('_')[0]?.toLowerCase() ?? '';
	}
 
	/**
	 * Control whether execution metadata should be exposed in AI SDK conversions.
	 */
	setExposeExecutionMetadata(expose: boolean): this {
		this.#exposeExecutionMetadata = expose;
		return this;
	}
 
	/**
	 * Execute the tool with the provided parameters
	 */
	async execute(inputParams?: JsonObject | string, options?: ExecuteOptions): Promise<JsonObject> {
		try {
			if (!this.requestBuilder || this.executeConfig.kind !== 'http') {
				// Non-HTTP tools provide their own execute override (e.g. RPC, local meta tools).
				throw new StackOneError(
					'BaseTool.execute is only available for HTTP-backed tools. Provide a custom execute implementation for non-HTTP tools.',
				);
			}
			// Validate params is either undefined, string, or object
			if (
				inputParams !== undefined &&
				typeof inputParams !== 'string' &&
				typeof inputParams !== 'object'
			) {
				throw new StackOneError(
					`Invalid parameters type. Expected object or string, got ${typeof inputParams}. Parameters: ${JSON.stringify(
						inputParams,
					)}`,
				);
			}
 
			// Convert string params to object
			const params = typeof inputParams === 'string' ? JSON.parse(inputParams) : inputParams || {};
 
			// Execute the request directly with parameters
			return await this.requestBuilder.execute(params, options);
		} catch (error) {
			Eif (error instanceof StackOneError) {
				throw error;
			}
			throw new StackOneError(
				`Error executing tool: ${error instanceof Error ? error.message : String(error)}`,
			);
		}
	}
 
	/**
	 * Convert the tool parameters to a pure JSON Schema format
	 * This is framework-agnostic and can be used with any LLM that accepts JSON Schema
	 */
	toJsonSchema(): ObjectJSONSchema {
		return {
			type: 'object',
			properties: this.parameters.properties,
			required: this.parameters.required,
		};
	}
 
	/**
	 * Convert the tool to OpenAI Chat Completions API format
	 */
	toOpenAI(): ChatCompletionFunctionTool {
		return {
			type: 'function',
			function: {
				name: this.name,
				description: this.description,
				parameters: this.toJsonSchema(),
			},
		};
	}
 
	/**
	 * Convert the tool to Anthropic format
	 * @see https://docs.anthropic.com/en/docs/build-with-claude/tool-use
	 */
	toAnthropic(): AnthropicTool {
		return {
			name: this.name,
			description: this.description,
			input_schema: this.toJsonSchema(),
		};
	}
 
	/**
	 * Convert the tool to OpenAI Responses API format
	 * @see https://platform.openai.com/docs/api-reference/responses
	 */
	toOpenAIResponses(options: { strict?: boolean } = {}): OpenAIResponsesFunctionTool {
		const { strict = true } = options;
		return {
			type: 'function',
			name: this.name,
			description: this.description,
			strict,
			parameters: {
				...this.toJsonSchema(),
				...(strict ? { additionalProperties: false } : {}),
			},
		};
	}
 
	/**
	 * Convert the tool to Claude Agent SDK format.
	 * Returns a tool definition compatible with the Claude Agent SDK's tool() function.
	 *
	 * @see https://docs.anthropic.com/en/docs/agents-and-tools/claude-agent-sdk
	 */
	async toClaudeAgentSdkTool(): Promise<{
		name: string;
		description: string;
		inputSchema: Record<string, unknown>;
		handler: (
			args: Record<string, unknown>,
		) => Promise<{ content: Array<{ type: 'text'; text: string }> }>;
	}> {
		const inputSchema = jsonSchema(this.toJsonSchema());
		const execute = this.execute.bind(this);
 
		return {
			name: this.name,
			description: this.description,
			inputSchema,
			handler: async (args: Record<string, unknown>) => {
				const result = await execute(args as JsonObject);
				return {
					content: [{ type: 'text' as const, text: JSON.stringify(result) }],
				};
			},
		};
	}
 
	/**
	 * Convert the tool to AI SDK format
	 */
	async toAISDK(
		options: { executable?: boolean; execution?: ToolExecution | false } = {
			executable: true,
		},
	): Promise<AISDKToolResult> {
		const schema = {
			...this.toJsonSchema(),
			additionalProperties: false,
		} satisfies AISDKJSONSchema;
 
		/** AI SDK is optional dependency, import only when needed */
		const ai = await tryImport<typeof import('ai')>(
			'ai',
			`npm install ai (requires ${peerDependencies.ai})`,
		);
		const schemaObject = ai.jsonSchema(schema);
 
		const executionOption =
			options.execution !== undefined
				? options.execution
				: this.#exposeExecutionMetadata
					? this.createExecutionMetadata()
					: false;
 
		const toolDefinition = {
			inputSchema: schemaObject,
			description: this.description,
			execution: executionOption !== false ? executionOption : undefined,
			execute:
				(options.executable ?? true)
					? async (args: Record<string, unknown>) => {
							try {
								return await this.execute(args as JsonObject);
							} catch (error) {
								return `Error executing tool: ${
									error instanceof Error ? error.message : String(error)
								}`;
							}
						}
					: undefined,
		} satisfies AISDKToolDefinition;
 
		return {
			[this.name]: toolDefinition,
		} satisfies AISDKToolResult;
	}
}
 
/**
 * StackOne-specific tool class with additional functionality
 */
export class StackOneTool extends BaseTool {
	/**
	 * Get the current account ID
	 */
	getAccountId(): string | undefined {
		return this.getHeaders()['x-account-id'];
	}
 
	/**
	 * Set the account ID for this tool
	 */
	setAccountId(accountId: string): StackOneTool {
		this.setHeaders({ 'x-account-id': accountId });
		return this;
	}
}
 
/**
 * Collection of tools with utility methods
 */
export class Tools implements Iterable<BaseTool> {
	private tools: BaseTool[];
 
	constructor(tools: BaseTool[]) {
		this.tools = tools;
	}
 
	/**
	 * Get the number of tools in the collection
	 */
	get length(): number {
		return this.tools.length;
	}
 
	/**
	 * Get a tool by name
	 */
	getTool(name: string): BaseTool | undefined {
		return this.tools.find((tool) => tool.name === name);
	}
 
	/**
	 * Get a StackOne tool by name
	 */
	getStackOneTool(name: string): StackOneTool {
		const tool = this.getTool(name);
		if (tool instanceof StackOneTool) {
			return tool;
		}
		throw new StackOneError(`Tool ${name} is not a StackOne tool`);
	}
 
	/**
	 * Check if a tool is a StackOne tool
	 */
	isStackOneTool(tool: BaseTool): tool is StackOneTool {
		return tool instanceof StackOneTool;
	}
 
	/**
	 * Get all StackOne tools in the collection
	 */
	getStackOneTools(): StackOneTool[] {
		return this.tools.filter((tool): tool is StackOneTool => tool instanceof StackOneTool);
	}
 
	/**
	 * Convert all tools to pure JSON Schema format
	 * Returns an array of objects with name, description, and schema
	 */
	toJsonSchema(): Array<{ name: string; description: string; parameters: JSONSchema }> {
		return this.tools.map((tool) => ({
			name: tool.name,
			description: tool.description,
			parameters: tool.toJsonSchema(),
		}));
	}
 
	/**
	 * Convert all tools to OpenAI Chat Completions API format
	 */
	toOpenAI(): ChatCompletionFunctionTool[] {
		return this.tools.map((tool) => tool.toOpenAI());
	}
 
	/**
	 * Convert all tools to Anthropic format
	 * @see https://docs.anthropic.com/en/docs/build-with-claude/tool-use
	 */
	toAnthropic(): AnthropicTool[] {
		return this.tools.map((tool) => tool.toAnthropic());
	}
 
	/**
	 * Convert all tools to OpenAI Responses API format
	 * @see https://platform.openai.com/docs/api-reference/responses
	 */
	toOpenAIResponses(options: { strict?: boolean } = {}): OpenAIResponsesFunctionTool[] {
		return this.tools.map((tool) => tool.toOpenAIResponses(options));
	}
 
	/**
	 * Convert all tools to AI SDK format
	 */
	async toAISDK(
		options: { executable?: boolean; execution?: ToolExecution | false } = {
			executable: true,
		},
	): Promise<AISDKToolResult> {
		const result: AISDKToolResult = {};
		for (const tool of this.tools) {
			Object.assign(result, await tool.toAISDK(options));
		}
		return result;
	}
 
	/**
	 * Convert all tools to Claude Agent SDK format.
	 * Returns an MCP server configuration that can be passed to the
	 * Claude Agent SDK query() function's mcpServers option.
	 *
	 * @example
	 * ```typescript
	 * const tools = await toolset.fetchTools();
	 * const mcpServer = await tools.toClaudeAgentSdk();
	 *
	 * const result = query({
	 *   prompt: 'Get employee info',
	 *   options: {
	 *     model: 'claude-sonnet-4-5-20250929',
	 *     mcpServers: {
	 *       'stackone-tools': mcpServer,
	 *     },
	 *   },
	 * });
	 * ```
	 *
	 * @see https://docs.anthropic.com/en/docs/agents-and-tools/claude-agent-sdk
	 */
	async toClaudeAgentSdk(
		options: ClaudeAgentSdkOptions = {},
	): Promise<McpSdkServerConfigWithInstance> {
		const { serverName = 'stackone-tools', serverVersion = '1.0.0' } = options;
 
		// Import the Claude Agent SDK dynamically
		const claudeAgentSdk = await tryImport<typeof import('@anthropic-ai/claude-agent-sdk')>(
			'@anthropic-ai/claude-agent-sdk',
			`npm install @anthropic-ai/claude-agent-sdk (requires ${peerDependencies['@anthropic-ai/claude-agent-sdk']})`,
		);
 
		// Convert all tools to Claude Agent SDK format
		// We use type assertions here because the Zod types from our dynamic import
		// don't perfectly match the Claude Agent SDK's expected types at compile time
		const sdkTools = await Promise.all(
			this.tools.map(async (baseTool) => {
				const toolDef = await baseTool.toClaudeAgentSdkTool();
				// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Dynamic Zod schema types
				return claudeAgentSdk.tool(
					toolDef.name,
					toolDef.description,
					toolDef.inputSchema as any,
					toolDef.handler as any,
				);
			}),
		);
 
		// Create and return the MCP server
		return claudeAgentSdk.createSdkMcpServer({
			name: serverName,
			version: serverVersion,
			tools: sdkTools,
		});
	}
 
	/**
	 * Filter tools by a predicate function
	 */
	filter(predicate: (tool: BaseTool) => boolean): Tools {
		return new Tools(this.tools.filter(predicate));
	}
 
	/**
	 * Get unique connector names from all tools.
	 *
	 * Extracts the connector/provider prefix from each tool name
	 * (the first segment before `_`).
	 *
	 * @returns Set of connector names (lowercase)
	 *
	 * @example
	 * ```typescript
	 * const tools = await toolset.fetchTools();
	 * const connectors = tools.getConnectors();
	 * // Set { 'bamboohr', 'hibob', 'slack', ... }
	 * ```
	 */
	getConnectors(): Set<string> {
		const connectors = new Set<string>();
		for (const tool of this.tools) {
			Eif (tool.connector) {
				connectors.add(tool.connector);
			}
		}
		return connectors;
	}
 
	/**
	 * Iterator implementation
	 */
	[Symbol.iterator](): Iterator<BaseTool> {
		let index = 0;
		const tools = this.tools;
 
		return {
			next(): IteratorResult<BaseTool> {
				if (index < tools.length) {
					return { value: tools[index++], done: false };
				}
				return { value: undefined as unknown as BaseTool, done: true };
			},
		};
	}
 
	/**
	 * Convert to array
	 */
	toArray(): BaseTool[] {
		return [...this.tools];
	}
 
	/**
	 * Map tools to a new array
	 */
	map<T>(mapper: (tool: BaseTool) => T): T[] {
		return this.tools.map(mapper);
	}
 
	/**
	 * Execute a function for each tool
	 */
	forEach(callback: (tool: BaseTool) => void): void {
		this.tools.forEach(callback);
	}
}