Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
TypeScript agentic-aiagentsaiai-agentsaiagentsdeveloper-toolsfunction-callinggpt-4javascriptjsllmllmopsmcppythonremote-mcp-serverssetypescript
/**
* OpenAI Agents Provider
* To be used with the @openai/agents package
*
* Author: Musthaq Ahamad <[email protected]>
* Reference: https://openai.github.io/openai-agents-js/
*
* This provider provides a set of tools for interacting with OpenAI's Agents API.
*
* @packageDocumentation
* @module providers/openai-agents
*/
import {
BaseAgenticProvider,
Tool as ComposioTool,
ExecuteToolFn,
McpUrlResponse,
McpServerGetResponse,
normalizeToolArguments,
deduplicateJsonSchemaRequiredArrays,
dereferenceJsonSchema,
logger,
omitNullToolArguments,
toStrictJsonSchema,
} from '@composio/core';
import type { Tool as OpenAIAgentTool } from '@openai/agents';
import { tool as createOpenAIAgentTool } from '@openai/agents';
type OpenAIAgentsToolCollection = Array<OpenAIAgentTool>;
/** The strict JSON Schema parameter shape the Agents SDK accepts. */
type StrictToolParameters = Extract<
Parameters<typeof createOpenAIAgentTool>[0]['parameters'],
{ additionalProperties: false }
>;
/** Parameters registered for a tool without input parameters under strict mode. */
const EMPTY_OBJECT_SCHEMA = {
type: 'object',
properties: {},
required: [],
additionalProperties: false,
} as const;
export class OpenAIAgentsProvider extends BaseAgenticProvider<
OpenAIAgentsToolCollection,
OpenAIAgentTool,
McpServerGetResponse
> {
readonly name = 'openai-agents';
private strict: boolean | null;
/**
* Creates a new instance of the OpenAIAgentsProvider.
*
* This provider enables integration with the @openai/agents package,
* allowing Composio tools to be used with OpenAI Agents.
*
* @example
* ```typescript
* // Initialize the OpenAIAgentsProvider
* const provider = new OpenAIAgentsProvider();
*
* // Use with Composio
* const composio = new Composio({
* apiKey: 'your-api-key',
* provider: new OpenAIAgentsProvider()
* });
*
* // Use the provider to wrap tools for @openai/agents
* const agentTools = provider.wrapTools(composioTools, composio.tools.execute);
* ```
*/
constructor(options?: { strict?: boolean }) {
super();
this.strict = options?.strict ?? false;
}
/**
* Transform MCP URL response into Anthropic-specific format.
* By default, Anthropic uses the standard format (same as default),
* but this method is here to show providers can customize if needed.
*
* @param data - The MCP URL response data
* @returns Standard MCP server response format
*/
wrapMcpServerResponse(data: McpUrlResponse): McpServerGetResponse {
// Anthropic uses the standard format
return data.map(item => ({
url: new URL(item.url),
name: item.name,
})) as McpServerGetResponse;
}
/**
* Wraps a Composio tool in a OpenAI Agents tool format.
*
* This method transforms a Composio tool definition into the format
* expected by @openai/agents for function calling.
*
* @param {ComposioTool} composioTool - The Composio tool to wrap
* @param {ExecuteToolFn} executeTool - Function to execute the tool
* @returns {OpenAIAgentsTool} The wrapped OpenAI Agents tool
*
* @example
* ```typescript
* // Wrap a single tool for use with Vercel AI SDK
* const composioTool = {
* slug: 'SEARCH_TOOL',
* description: 'Search for information',
* inputParameters: {
* type: 'object',
* properties: {
* query: { type: 'string' }
* },
* required: ['query']
* }
* };
*
* // Create a Vercel tool using the provider
* const vercelTool = provider.wrapTool(
* composioTool,
* composio.tools.execute
* );
*
* // Use with Vercel AI SDK
* import { StreamingTextResponse, Message } from 'ai';
* import { OpenAI } from 'openai';
*
* export async function POST(req: Request) {
* const { messages } = await req.json();
* const openai = new OpenAI();
*
* const response = await openai.chat.completions.create({
* model: 'gpt-4',
* messages,
* tools: [vercelTool]
* });
*
* return new StreamingTextResponse(response.choices[0].message);
* }
* ```
*/
wrapTool(composioTool: ComposioTool, executeTool: ExecuteToolFn): OpenAIAgentTool {
const inputParameters = deduplicateJsonSchemaRequiredArrays(
dereferenceJsonSchema(composioTool.inputParameters ?? {}, { onUnresolved: 'sentinel' })
);
// Strict mode applies OpenAI's structured-output contract at every
// depth: all properties required, closed objects, optional properties
// widened to accept null instead of being dropped. Tools whose schema
// strict mode cannot express are registered without strict mode.
const execute = async (params: unknown, strictSource?: Record<string, unknown>) => {
// Models occasionally emit tool input as a JSON string rather than an object (issue #2406).
const normalized = normalizeToolArguments(params, composioTool.slug);
// Under strict mode optional parameters are emitted as required-nullable,
// so a null the tool's own schema does not accept means "omitted".
return await executeTool(
composioTool.slug,
strictSource ? omitNullToolArguments(normalized, strictSource) : normalized
);
};
if (this.strict) {
const strict = toStrictJsonSchema(composioTool.inputParameters ?? EMPTY_OBJECT_SCHEMA);
if (strict.unsupported.length === 0) {
return createOpenAIAgentTool({
name: composioTool.slug,
description: composioTool.description ?? '',
parameters: strict.schema as StrictToolParameters,
strict: true,
execute: params => execute(params, strict.source),
});
}
const reasons = strict.unsupported
.map(entry => `${entry.path || '<root>'}: ${entry.keyword} (${entry.detail})`)
.join('; ');
logger.warn(
`OpenAIAgentsProvider: tool "${composioTool.slug}" is registered without strict mode because its schema cannot be expressed as strict structured outputs: ${reasons}`
);
}
return createOpenAIAgentTool({
name: composioTool.slug,
description: composioTool.description ?? '',
parameters: {
type: 'object',
properties: inputParameters?.properties || {},
required: inputParameters?.required || [],
additionalProperties: true,
},
strict: false,
execute: params => execute(params),
});
}
/**
* Wraps a list of Composio tools as a OpenAI Agents tool collection.
*
* This method transforms multiple Composio tool definitions into the format
* expected by OpenAI Agents for function calling, organizing them
* into a dictionary keyed by tool slug.
*
* @param {ComposioTool[]} tools - Array of Composio tools to wrap
* @param {ExecuteToolFn} executeTool - Function to execute the tools
* @returns {OpenAIAgentsToolCollection} Dictionary of wrapped tools in OpenAI Agents format
*
* @example
* ```typescript
* // Get OpenAI Agents tools from Composio
* const composio = new Composio({
* apiKey: 'your-api-key',
* provider: new OpenAIAgentsProvider()
* });
*
* const tools = await composio.tools.get('default', {
* toolkits: ['github'],
* });
*
* import { OpenAI } from '@openai/agents';
*
* export async function POST(req: Request) {
* const { messages } = await req.json();
* const openai = new OpenAI();
*
* const response = await openai.chat.completions.create({
* model: 'gpt-4',
* messages,
* tools: openaiAgentsTools
* });
*
* return new StreamingTextResponse(response.choices[0].message);
* }
* ```
*/
wrapTools(tools: ComposioTool[], executeTool: ExecuteToolFn): OpenAIAgentsToolCollection {
return tools.map(tool => {
return this.wrapTool(tool, executeTool);
});
}
}