AI Integration

Prisma PHP is built for the AI-native era. Whether you are generating text with OpenAI or exposing your database as tools via MCP, the framework gets out of your way and lets you use the best libraries available.

Consuming AI

Use PHP to send prompts to OpenAI, Anthropic, or Groq. We recommend the openai-php/client package.

Serving AI Tools (MCP)

Native support for the Model Context Protocol. Expose your PHP functions as tools that Agents can call.

1. Install the Client

Prisma PHP is unopinionated. You can install any AI SDK, but for the best experience with ChatGPT and compatible providers, we use the standard OpenAI PHP client.

Terminal
composer require openai-php/client

2. Configure Secrets

Add your API key to the .env file in your project root.

.env
OPENAI_API_KEY="sk-..."

3. Make a Request

In Prisma PHP, API calls and logic are typically handled in route.php. Here is a simple example of creating a chat completion route.

Prisma API Convention

For detailed information on how routing works, refer to the route.php documentation.

route.php
// Initialize the client
$client = OpenAI::client(env('OPENAI_API_KEY'));

// Define an API endpoint
if ($this->resource('api/chat', 'POST')) {
    
    $input = json_decode(file_get_contents('php://input'), true);
    
    $result = $client->chat()->create([
        'model' => 'gpt-4o',
        'messages' => [
            ['role' => 'user', 'content' => $input['message']],
        ],
    ]);

    echo json_encode([
        'response' => $result->choices[0]->message->content
    ]);
    exit;
}

Native MCP Integration

Prisma PHP isn't just for calling AI—it's for powering AI. During installation or update, you can enable the MCP Environment. This configures a Model Context Protocol server automatically, allowing you to define tools (via PHP Attributes) that AI agents can use to query your database or execute logic.