-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathopenai_tools.ts
More file actions
39 lines (34 loc) · 1.05 KB
/
openai_tools.ts
File metadata and controls
39 lines (34 loc) · 1.05 KB
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
/**
* Convert tool definitions to OpenAI format
* Unified function for both MCP tools and internal ToolBase types
*/
import type OpenAI from 'openai';
import type { McpTool } from './types.js';
/**
* Generic tool interface that matches both ToolBase and McpTool
*/
type GenericTool = {
name: string;
description?: string;
inputSchema: Record<string, unknown>;
}
/**
* Convert tools to OpenAI Chat Completion format
* Works with both MCP tools and ToolBase from the server
*/
export function transformToolsToOpenAIFormat(tools: GenericTool[]): OpenAI.Chat.Completions.ChatCompletionTool[] {
return tools.map((tool) => ({
type: 'function' as const,
function: {
name: tool.name,
description: tool.description || '',
parameters: tool.inputSchema,
},
}));
}
/**
* Alias for MCP-specific usage (keeps backwards compatibility)
*/
export function mcpToolsToOpenAiTools(mcpTools: McpTool[]): OpenAI.Chat.Completions.ChatCompletionTool[] {
return transformToolsToOpenAIFormat(mcpTools);
}