For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://modelgates.ai/docs/_mcp/server.

API Reference

callModel

typescript
function callModel(request: CallModelInput, options?: RequestOptions): ModelResult

Creates a response using the OpenResponses API with multiple consumption patterns.

CallModelInput

ParameterTypeRequiredDescription
modelstring | ((ctx: TurnContext) => string)Yes*Model ID (e.g., "openai/gpt-5-nano")
modelsstring[]Yes*Model fallback array
inputOpenResponsesInputYesInput messages or string
instructionsstring | ((ctx: TurnContext) => string)NoSystem instructions
toolsTool[]NoTools available to the model
maxToolRoundsMaxToolRoundsNoTool execution limit (deprecated)
stopWhenStopWhenNoStop conditions
temperaturenumber | ((ctx: TurnContext) => number)NoSampling temperature (0-2)
maxOutputTokensnumber | ((ctx: TurnContext) => number)NoMaximum tokens to generate
topPnumberNoTop-p sampling
textResponseTextConfigNoText format configuration
providerProviderPreferencesNoProvider routing and configuration
topKnumberNoTop-k sampling
metadataRecord<string, string>NoRequest metadata
toolChoiceToolChoiceNoTool choice configuration
parallelToolCallsbooleanNoEnable parallel tool calling
reasoningReasoningConfigNoReasoning configuration
promptCacheKeystringNoCache key for prompt caching
previousResponseIdstringNoContext from previous response
includestring[]NoInclude extra fields in response
backgroundbooleanNoRun request in background
safetyIdentifierstringNoUser safety identifier
serviceTierstringNoService tier preference
truncationstringNoTruncation mode
pluginsPlugin[]NoEnabled plugins
userstringNoEnd-user identifier
sessionIdstringNoSession identifier
storebooleanNoStore request data
contextContextInput<ToolContextMap>NoTool context keyed by tool name

*Either model or models is required.

ProviderPreferences

Configuration for routing and provider selection.

ParameterTypeDescription
allowFallbacksbooleanAllow backup providers when primary is unavailable (default: true)
requireParametersbooleanOnly use providers that support all requested parameters
dataCollection"allow" | "deny"Data collection policy (allow/deny)
orderstring[]Custom provider routing order
onlystring[]Restrict to specific providers
ignorestring[]Exclude specific providers
quantizationsstring[]Filter by quantization levels
sortstringLoad balancing strategy (e.g., "throughput")
maxPriceobjectMaximum price limits
preferredMinThroughputnumberMinimum tokens per second preference
preferredMaxLatencynumberMaximum latency preference

RequestOptions

ParameterTypeDescription
timeoutnumberRequest timeout in milliseconds
signalAbortSignalAbort signal for cancellation

ModelResult

Wrapper providing multiple consumption patterns for a response.

Methods

getText()

typescript
getText(): Promise<string>

Get text content after tool execution completes.

getResponse()

typescript
getResponse(): Promise<OpenResponsesNonStreamingResponse>

Get full response with usage data (inputTokens, outputTokens, cachedTokens).

getTextStream()

typescript
getTextStream(): AsyncIterableIterator<string>

Stream text deltas.

getReasoningStream()

typescript
getReasoningStream(): AsyncIterableIterator<string>

Stream reasoning deltas (for reasoning models).

getNewMessagesStream()

typescript
getNewMessagesStream(): AsyncIterableIterator<ResponsesOutputMessage | OpenResponsesFunctionCallOutput>

Stream cumulative message snapshots in OpenResponses format.

getFullResponsesStream()

typescript
getFullResponsesStream(): AsyncIterableIterator<EnhancedResponseStreamEvent>

Stream all events including tool preliminary results.

getToolCalls()

typescript
getToolCalls(): Promise<ParsedToolCall[]>

Get all tool calls from initial response.

getToolCallsStream()

typescript
getToolCallsStream(): AsyncIterableIterator<ParsedToolCall>

Stream tool calls as they complete.

getToolStream()

typescript
getToolStream(): AsyncIterableIterator<ToolStreamEvent>

Stream tool deltas and preliminary results.

getContextUpdates()

typescript
getContextUpdates(): AsyncGenerator<ToolContextMap<TTools>>

Stream context snapshots whenever a tool calls setContext(). Completes when tool execution finishes.

cancel()

typescript
cancel(): Promise<void>

Cancel the stream and all consumers.


Tool Types

tool()

typescript
function tool<TInput, TOutput>(config: ToolConfig): Tool

Create a typed tool with Zod schema validation.

ToolConfig

ParameterTypeRequiredDescription
namestringYesTool name
descriptionstringNoTool description
inputSchemaZodObjectYesInput parameter schema
outputSchemaZodTypeNoOutput schema
eventSchemaZodTypeNoEvent schema (triggers generator mode)
contextSchemaZodObjectNoContext data this tool needs
executefunction | falseYes*Execute function, or false for manual
onToolCalledfunctionYes*HITL hook — return value to auto-respond, null to pause
onResponseReceivedfunctionNoHITL hook — post-process caller-supplied result (HITL only)
nextTurnParamsNextTurnParamsFunctionsNoParameters to modify next turn

* Provide exactly one of execute or onToolCalled. Omitting both (with execute: false) makes the tool a manual tool.

Tool

Union type of all tool types:

typescript
type Tool =  | ToolWithExecute<ZodObject, ZodType>  | ToolWithGenerator<ZodObject, ZodType, ZodType>  | ManualTool<ZodObject, ZodType>  | HITLTool<ZodObject, ZodType>;

ToolWithExecute

Regular tool with execute function:

typescript
interface ToolWithExecute<  TInput, TOutput, TContext, TName> {  type: ToolType.Function;  function: {    name: TName;    description?: string;    inputSchema: TInput;    outputSchema?: TOutput;    contextSchema?: ZodObject;    execute: (      params: z.infer<TInput>,      context: ToolExecuteContext<TName, TContext>,    ) => Promise<z.infer<TOutput>>;  };}

ToolWithGenerator

Generator tool with eventSchema:

typescript
interface ToolWithGenerator<  TInput, TEvent, TOutput, TContext, TName> {  type: ToolType.Function;  function: {    name: TName;    description?: string;    inputSchema: TInput;    eventSchema: TEvent;    outputSchema: TOutput;    contextSchema?: ZodObject;    execute: (      params: z.infer<TInput>,      context: ToolExecuteContext<TName, TContext>,    ) => AsyncGenerator<z.infer<TEvent>>;  };}

ManualTool

Tool without execute function:

typescript
interface ManualTool<TInput, TOutput> {  type: ToolType.Function;  function: {    name: string;    description?: string;    inputSchema: TInput;    outputSchema?: TOutput;  };}

HITLTool

Human-in-the-loop tool with onToolCalled and optional onResponseReceived hooks. outputSchema is required — it validates both the hook's non-null return value and the caller-supplied response delivered via function_call_output.

typescript
interface HITLToolFunction<  TInput, TOutput, TContext, TName> {  name: TName;  description?: string;  inputSchema: TInput;  outputSchema: TOutput;  contextSchema?: ZodObject;  onToolCalled: (    params: z.infer<TInput>,    context?: ToolExecuteContext<TName, TContext>,  ) => Promise<z.infer<TOutput> | null> | z.infer<TOutput> | null;  onResponseReceived?: (    rawResult: unknown,    context?: ToolExecuteContext<TName, TContext>,  ) => Promise<z.infer<TOutput>> | z.infer<TOutput>;  toModelOutput?: ToModelOutputFunction<    z.infer<TInput>,    z.infer<TOutput>  >;} type HITLTool<TInput, TOutput, TContext> = {  type: ToolType.Function;  function: HITLToolFunction<TInput, TOutput, TContext>;};

Returning null from onToolCalled pauses the loop and sets the conversation status to 'awaiting_hitl'. Throwing from onToolCalled is surfaced as a tool error of the form { error: ... }. Throwing from onResponseReceived is surfaced as an error payload that includes the caller's original output of the form { error: ..., originalOutput: ... }.


Tool Type Guards

typescript
function isManualTool(tool: Tool): tool is ManualTool;function isHITLTool(tool: Tool): tool is HITLTool;function isAutoResolvableTool(  tool: Tool,): tool is ToolWithExecute | ToolWithGenerator | HITLTool;
  • isManualTool — no execute and no onToolCalled. Always pauses the loop.
  • isHITLTool — has an onToolCalled function.
  • isAutoResolvableTool — either has an execute function (regular/generator) or is a HITL tool. Returns false for manual and server tools.

Context Types

TurnContext

typescript
interface TurnContext {  toolCall?: OpenResponsesFunctionToolCall;  numberOfTurns: number;  turnRequest?: OpenResponsesRequest;}

ToolExecuteContext

Flat context passed to tool execute functions. Merges TurnContext fields with tool-specific context:

typescript
type ToolExecuteContext<TName, TContext> =  TurnContext & {    tools: {      readonly [K in TName]: Readonly<TContext>;    };    setContext(partial: Partial<TContext>): void;  };

ToolContextMap

Context map for callModel's context option, keyed by tool name:

typescript
type ToolContextMap<T extends readonly Tool[]> = {  [K in T[number] as K['function']['name']]:    InferToolContext<K>;};

ContextInput

Context can be static, a sync function, or an async function:

typescript
type ContextInput<T> =  | T  | ((turn: TurnContext) => T)  | ((turn: TurnContext) => Promise<T>);

NextTurnParamsContext

typescript
interface NextTurnParamsContext {  input: OpenResponsesInput;  model: string;  models: string[];  temperature: number | null;  maxOutputTokens: number | null;  topP: number | null;  topK?: number | undefined;  instructions: string | null;}

Stream Event Types

EnhancedResponseStreamEvent

typescript
type EnhancedResponseStreamEvent =  | OpenResponsesStreamEvent  | ToolPreliminaryResultEvent;

ToolStreamEvent

typescript
type ToolStreamEvent =  | { type: 'delta'; content: string }  | { type: 'preliminary_result'; toolCallId: string; result: unknown };

ParsedToolCall

typescript
interface ParsedToolCall {  id: string;  name: string;  arguments: unknown;}

ToolExecutionResult

typescript
interface ToolExecutionResult {  toolCallId: string;  toolName: string;  result: unknown;  preliminaryResults?: unknown[];  error?: Error;}

Stop Conditions

StopWhen

typescript
type StopWhen =  | StopCondition  | StopCondition[];

StopCondition

typescript
type StopCondition = (context: StopConditionContext) => boolean | Promise<boolean>;

StopConditionContext

typescript
interface StopConditionContext {  steps: StepResult[];}

StepResult

typescript
interface StepResult {  stepType: 'initial' | 'continue';  text: string;  toolCalls: TypedToolCallUnion[];  toolResults: ToolExecutionResultUnion[];  response: OpenResponsesNonStreamingResponse;  usage?: OpenResponsesUsage;  finishReason?: string;  warnings?: Warning[];  experimental_providerMetadata?: Record<string, unknown>;}

Warning

typescript
interface Warning {  type: string;  message: string;}

Built-in Helpers

FunctionSignatureDescription
stepCountIs(n: number) => StopConditionStop after n steps
hasToolCall(name: string) => StopConditionStop when tool is called
maxTokensUsed(n: number) => StopConditionStop after n tokens
maxCost(amount: number) => StopConditionStop after cost limit
finishReasonIs(reason: string) => StopConditionStop on finish reason

Format Helpers

fromChatMessages

typescript
function fromChatMessages(messages: Message[]): OpenResponsesInput

Convert OpenAI chat format to OpenResponses input.

toChatMessage

typescript
function toChatMessage(response: OpenResponsesNonStreamingResponse): AssistantMessage

Convert response to chat message format.

fromClaudeMessages

typescript
function fromClaudeMessages(messages: ClaudeMessageParam[]): OpenResponsesInput

Convert Anthropic Claude format to OpenResponses input.

toClaudeMessage

typescript
function toClaudeMessage(response: OpenResponsesNonStreamingResponse): ClaudeMessage

Convert response to Claude message format.


Type Utilities

InferToolInput

typescript
type InferToolInput<T> = T extends { function: { inputSchema: infer S } }  ? S extends ZodType ? z.infer<S> : unknown  : unknown;

InferToolOutput

typescript
type InferToolOutput<T> = T extends { function: { outputSchema: infer S } }  ? S extends ZodType ? z.infer<S> : unknown  : unknown;

InferToolEvent

typescript
type InferToolEvent<T> = T extends { function: { eventSchema: infer S } }  ? S extends ZodType ? z.infer<S> : never  : never;

TypedToolCall

typescript
type TypedToolCall<T extends Tool> = {  id: string;  name: T extends { function: { name: infer N } } ? N : string;  arguments: InferToolInput<T>;};

Exports

typescript
// Agent clientexport { ModelGates } from '@modelgates/agent'; // Tool helpersexport {  tool,  ToolType,  isManualTool,  isHITLTool,  isAutoResolvableTool,} from '@modelgates/agent'; // Format helpersexport { fromChatMessages, toChatMessage, fromClaudeMessages, toClaudeMessage } from '@modelgates/agent'; // Stop condition helpersexport { stepCountIs, hasToolCall, maxTokensUsed, maxCost, finishReasonIs } from '@modelgates/agent'; // Context helpersexport {  buildToolExecuteContext,  ToolContextStore,} from '@modelgates/agent'; // Typesexport type {  CallModelInput,  ContextInput,  Tool,  ToolWithExecute,  ToolWithGenerator,  ManualTool,  HITLTool,  HITLToolFunction,  ToolExecuteContext,  ToolContextMap,  TurnContext,  ParsedToolCall,  ToolExecutionResult,  StopCondition,  StopWhen,  InferToolInput,  InferToolOutput,  InferToolEvent,} from '@modelgates/agent';