> ## Documentation Index
> Fetch the complete documentation index at: https://metorial.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Using with OpenAI

> Use Metorial with OpenAI GPT models

The OpenAI provider integration lets you use Metorial's MCP tools with OpenAI's GPT models, including GPT-4 and GPT-3.5. The integration handles converting Metorial tools into OpenAI's function calling format and processing tool call responses.

You'll need both a Metorial API key and an OpenAI API key to use this integration. The Metorial SDK manages your provider connections, while the OpenAI SDK handles the model interactions.

**What this example does:**

1. Initializes both Metorial and OpenAI clients
2. Creates a Metorial session that provides tools in OpenAI's format
3. Passes those tools to OpenAI's chat completions API
4. Provides a `callTools` function to execute any tool calls the model requests

## Example

<CodeGroup>
  ```typescript TypeScript theme={null}
  import { Metorial } from 'metorial';
  import { metorialOpenAI } from '@metorial/openai';
  import OpenAI from 'openai';

  let metorial = new Metorial({ apiKey: process.env.METORIAL_API_KEY });
  let openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

  let session = await metorial.connect({
    adapter: metorialOpenAI.chatCompletions(),
    providers: [
      { providerDeploymentId: 'your-provider-deployment-id' }
    ]
  });

  let response = await openai.chat.completions.create({
    model: "gpt-4o",
    messages: [{ role: "user", content: "Help me" }],
    tools: session.tools()
  });
  ```

  ```python Python theme={null}
  from metorial import Metorial, metorial_openai
  from openai import AsyncOpenAI
  import asyncio
  import os

  metorial = Metorial(api_key=os.getenv("METORIAL_API_KEY"))
  openai = AsyncOpenAI(api_key=os.getenv("OPENAI_API_KEY"))

  async def main():
      session = await metorial.connect(
          adapter=metorial_openai(),
          providers=[{"provider_deployment_id": "your-provider-deployment-id"}],
      )

      response = await openai.chat.completions.create(
          model="gpt-4o",
          messages=[{"role": "user", "content": "Help me"}],
          tools=session.tools(),
      )

      if response.choices[0].message.tool_calls:
          results = await session.call_tools(response.choices[0].message.tool_calls)
          # Add results to messages and continue conversation...

  asyncio.run(main())
  ```
</CodeGroup>
