XAI

Connect Integrations to
XAI

Build advanced AI agents with XAI. Connect 600+ integrations, automate workflows, and deploy with ease using Metorial.

Back to XAI overview

Handling Tool Calls with XAI and Metorial

When Grok decides to use one of your Metorial integrations, it returns tool calls that you need to execute and send back.

Understanding Tool Calls

Tool calls are Grok's way of requesting to use an integration. For example, if you ask "What's on my calendar today?", Grok might request to call a calendar tool.

Basic Tool Call Handling

Here's how to handle a single tool call:

await metorial.withProviderSession(
  metorialXai,
  { serverDeployments: ['your-server-deployment-id'] },
  async session => {
    let messages = [
      {
        role: 'user',
        content: 'What emails did I receive today?'
      }
    ];

    let response = await xai.chat.completions.create({
      model: 'grok-beta',
      messages,
      tools: session.tools
    });

    let choice = response.choices[0];
    let toolCalls = choice.message.tool_calls;

    // Check if Grok wants to use any tools
    if (toolCalls && toolCalls.length > 0) {
      // Execute the tool calls via Metorial
      let toolResponses = await session.callTools(toolCalls);
      
      // Add both the assistant's tool calls and the responses to history
      messages.push(
        { role: 'assistant', tool_calls: toolCalls },
        ...toolResponses
      );

      // Make another request with the tool results
      let finalResponse = await xai.chat.completions.create({
        model: 'grok-beta',
        messages,
        tools: session.tools
      });

      console.log(finalResponse.choices[0].message.content);
    } else {
      // No tools needed, just print the response
      console.log(choice.message.content);
    }
  }
);

Key Points

  • Always check for tool calls before trying to execute them
  • Use session.callTools() to execute tools through Metorial
  • Append both tool calls and responses to your message history
  • Make a follow-up request with the tool results so Grok can formulate a final answer

Next Steps

For handling multiple rounds of tool calls, see our "Building a Conversation Loop" guide.

XAI on Metorial

Integrate XAI's powerful language models with over 600 tools and services using Metorial's streamlined, MCP-powered platform. Our open-source Python and TypeScript SDKs make it simple to connect Grok and other XAI models to the integrations your AI agents need—from productivity suites and CRMs to databases and communication platforms. With Metorial, adding sophisticated integration capabilities to your XAI applications takes just a couple of lines of code, dramatically reducing time to market and eliminating the maintenance burden of custom integration code. Whether you're building conversational AI, data analysis tools, or intelligent automation with XAI's cutting-edge models, Metorial provides the infrastructure to connect seamlessly with real-world tools and workflows. Our platform handles all the complexity of authentication, API calls, rate limiting, and error handling automatically, so you can focus on crafting unique AI experiences rather than wrestling with integration challenges. Join developers who are shipping faster by leveraging Metorial's battle-tested integration layer—let us manage the connections while you concentrate on innovation and creating value for your users with XAI's advanced language models.

Connect anything. Anywhere.

Ready to build with Metorial?

Let's take your AI-powered applications to the next level, together.

About Metorial

Metorial provides developers with instant access to 600+ MCP servers for building AI agents that can interact with real-world tools and services. Built on MCP, Metorial simplifies agent tool integration by offering pre-configured connections to popular platforms like Google Drive, Slack, GitHub, Notion, and hundreds of other APIs. Our platform supports all major AI agent frameworks—including LangChain, AutoGen, CrewAI, and LangGraph—enabling developers to add tool calling capabilities to their agents in just a few lines of code. By eliminating the need for custom integration code, Metorial helps AI developers move from prototype to production faster while maintaining security and reliability. Whether you're building autonomous research agents, customer service bots, or workflow automation tools, Metorial's MCP server library provides the integrations you need to connect your agents to the real world.

Star us on GitHub