What you’ll learn:
- How to install the Metorial SDK
- Available AI providers and integrations
- How to create sessions and use tools
- OAuth flow for authenticated services
- Streaming and session management
Prerequisites
- Metorial API key: Get one from app.metorial.com
- Server deployment ID: Deploy a server (e.g., Exa for search)
- AI provider API key: OpenAI, Anthropic, Google, etc.
Installation
The Metorial SDK consists of two parts: the core SDK (which handles sessions and tool management) and a provider package (which integrates with your chosen AI model). Install both to get started.Your First AI Agent
This example shows how to create an AI agent that can use tools from your deployed MCP servers. The agent will have access to any tools provided by your server deployment (like search, file operations, or API calls). What this code does:- Initializes the Metorial SDK with your API key
- Creates a session connected to your server deployment
- Passes the available tools to your AI model
- Streams the AI’s response back in real-time
- Automatically closes the session when complete
OAuth Flow
For services requiring user authentication (like Slack, GitHub, or Google Calendar), you need to use OAuth sessions. This allows your users to authorize access to their accounts. The OAuth flow works in 4 steps:- Create OAuth sessions for each service that needs authentication
- Send the authorization URLs to your users (they’ll approve access in their browser)
- Wait for the user to complete the OAuth flow
- Use the authenticated sessions to create tool-enabled AI agents
Session Options
Streaming Mode
When your AI model streams responses back to users in real-time, you need to enable streaming mode in Metorial. This ensures that tool calls work correctly with streamed responses.Closing Sessions
Sessions maintain an active connection to your MCP servers. Always close your session when you’re done to free up resources and ensure clean disconnection from your deployments.Error Handling
The Metorial SDK provides specific error types to help you handle different failure scenarios. CatchMetorialAPIError for API-related issues like authentication failures, rate limits, or invalid deployment IDs.