Skip to main content
Now that you’ve deployed your first MCP server and confirmed it’s working, you can connect it to an LLM provider like OpenAI. In this guide, you’ll learn how to build a chat-enabled app that automatically handles tool calls from your Metorial-powered MCP server.
What you’ll learn:
  • How to use a Metorial MCP server
  • How to use the Metorial SDKs
Before you start:
1

1. Install the SDKs

Run the installer for your language of choice:
npm install metorial @metorial/openai openai
2

2. Configure Clients

Instantiate both clients with your API keys and your MCP server ID.
import Metorial from 'metorial';
import OpenAI from 'openai';

const metorial = new Metorial({
  apiKey: '$$SECRET_TOKEN$$'
});
const openai = new OpenAI({
  apiKey: '...your-openai-api-key...'
});
3

3. Fetch Your Server Tools

Retrieve the session object that exposes your deployed MCP tools.
const session = await metorial.withProviderSession(
  metorialOpenAI.chatCompletions,
  { serverDeployments: ['...server-deployment-id...'] }
);
4

4. Send Your First Prompt

Kick off the loop by sending an initial message.
let messages = [
  { role: "user", content: "Summarize the README.md file of the metorial/websocket-explorer repository on GitHub." }
];
5

5. Loop & Handle Tool Calls

  1. Send messages to OpenAI, passing tools: session.tools (TS) or tools=session.tools (Py).
  2. If the assistant response contains tool_calls, invoke it:
const response = await openai.chat.completions.create({
  model: 'gpt-4o',
  messages,
  tools: session.tools
});
const choice = response.choices[0]!;
const toolCalls = choice.message.tool_calls;
const toolResults = await session.callTools(toolCalls);
  1. Append both the tool call requests and their results to messages.
  2. Repeat until the assistant’s response has no more tool_calls.
6

6. Display the Final Output

Once there are no more tool calls, your assistant’s final reply is in:
console.log(choice.message.content);

What’s Next?

You are all set on having a production-ready MCP server to use in your AI apps. Next, you will learn about all the dev tooling available.

Next Up: How to monitor your server and tool calls

Learn how to use the observability & logging features.