Skip to main content
Glama

function_calling

Call functions using Grok AI based on user input by processing message arrays and tool definitions to execute specific actions.

Instructions

Use Grok AI to call functions based on user input

Input Schema

NameRequiredDescriptionDefault
messagesYesArray of message objects with role and content
modelNoGrok model to use (e.g., grok-2-latest, grok-3, grok-3-reasoner, grok-3-deepsearch, grok-3-mini-beta)grok-3-mini-beta
tool_choiceNoTool choice mode (auto, required, none)auto
toolsYesArray of tool objects with type, function name, description, and parameters

Input Schema (JSON Schema)

{ "properties": { "messages": { "description": "Array of message objects with role and content", "items": { "properties": { "content": { "description": "Content of the message", "type": "string" }, "role": { "description": "Role of the message sender (system, user, assistant, tool)", "enum": [ "system", "user", "assistant", "tool" ], "type": "string" }, "tool_call_id": { "description": "ID of the tool call (for tool messages)", "type": "string" } }, "required": [ "role", "content" ], "type": "object" }, "type": "array" }, "model": { "default": "grok-3-mini-beta", "description": "Grok model to use (e.g., grok-2-latest, grok-3, grok-3-reasoner, grok-3-deepsearch, grok-3-mini-beta)", "type": "string" }, "tool_choice": { "default": "auto", "description": "Tool choice mode (auto, required, none)", "enum": [ "auto", "required", "none" ], "type": "string" }, "tools": { "description": "Array of tool objects with type, function name, description, and parameters", "items": { "type": "object" }, "type": "array" } }, "required": [ "messages", "tools" ], "type": "object" }

Implementation Reference

  • The primary handler for the 'function_calling' tool. Validates arguments, calls the Grok API client for function calling, and formats the response to include tool calls or content.
    private async handleFunctionCalling(args: any) { console.error('[Tool] Handling function_calling tool call'); const { messages, tools, tool_choice, model, ...otherOptions } = args; // Validate inputs if (!Array.isArray(messages) || messages.length === 0) { throw new Error('Messages must be a non-empty array'); } if (!Array.isArray(tools) || tools.length === 0) { throw new Error('Tools must be a non-empty array'); } // Create options object const options = { model: model || 'grok-2-latest', tool_choice: tool_choice || 'auto', ...otherOptions }; // Call Grok API const response = await this.grokClient.createFunctionCall(messages, tools, options); // Check if there are tool calls in the response if (response.choices[0].message.tool_calls) { return { content: [ { type: 'text', text: JSON.stringify({ message: response.choices[0].message, usage: response.usage }, null, 2), }, ], }; } else { return { content: [ { type: 'text', text: response.choices[0].message.content, }, ], }; } }
  • src/index.ts:134-184 (registration)
    Tool registration in the ListToolsRequestSchema handler, defining the name, description, and inputSchema for 'function_calling'.
    { name: 'function_calling', description: 'Use Grok AI to call functions based on user input', inputSchema: { type: 'object', properties: { messages: { type: 'array', description: 'Array of message objects with role and content', items: { type: 'object', properties: { role: { type: 'string', description: 'Role of the message sender (system, user, assistant, tool)', enum: ['system', 'user', 'assistant', 'tool'] }, content: { type: 'string', description: 'Content of the message' }, tool_call_id: { type: 'string', description: 'ID of the tool call (for tool messages)' } }, required: ['role', 'content'] } }, tools: { type: 'array', description: 'Array of tool objects with type, function name, description, and parameters', items: { type: 'object' } }, tool_choice: { type: 'string', description: 'Tool choice mode (auto, required, none)', enum: ['auto', 'required', 'none'], default: 'auto' }, model: { type: 'string', description: 'Grok model to use (e.g., grok-2-latest, grok-3, grok-3-reasoner, grok-3-deepsearch, grok-3-mini-beta)', default: 'grok-3-mini-beta' } }, required: ['messages', 'tools'] } }
  • Input schema definition for the 'function_calling' tool, specifying properties like messages, tools, tool_choice, and model.
    inputSchema: { type: 'object', properties: { messages: { type: 'array', description: 'Array of message objects with role and content', items: { type: 'object', properties: { role: { type: 'string', description: 'Role of the message sender (system, user, assistant, tool)', enum: ['system', 'user', 'assistant', 'tool'] }, content: { type: 'string', description: 'Content of the message' }, tool_call_id: { type: 'string', description: 'ID of the tool call (for tool messages)' } }, required: ['role', 'content'] } }, tools: { type: 'array', description: 'Array of tool objects with type, function name, description, and parameters', items: { type: 'object' } }, tool_choice: { type: 'string', description: 'Tool choice mode (auto, required, none)', enum: ['auto', 'required', 'none'], default: 'auto' }, model: { type: 'string', description: 'Grok model to use (e.g., grok-2-latest, grok-3, grok-3-reasoner, grok-3-deepsearch, grok-3-mini-beta)', default: 'grok-3-mini-beta' } }, required: ['messages', 'tools'] }
  • Helper method in GrokApiClient that performs the HTTP POST to /chat/completions with tools and tool_choice parameters for function calling.
    async createFunctionCall(messages: any[], tools: any[], options: any = {}): Promise<any> { try { console.error('[API] Creating function call request...'); const requestBody = { messages, model: options.model || 'grok-3-mini-beta', tools, tool_choice: options.tool_choice || 'auto', ...options }; const response = await this.axiosInstance.post('/chat/completions', requestBody); return response.data; } catch (error) { console.error('[Error] Failed to create function call request:', error); throw error; } }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/Bob-lance/grok-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server