Learn how to build a CakePHP MCP server (local) for AI integration.
Intro
Unless your crew left you stranded on a desert island earlier this year, I'm sure you've heard about every big name in the industry integrating their applications and exposing their data to "agents". Model Context Protocol https://modelcontextprotocol.io/docs/getting-started/intro was created to define how the your application could interact and provide features to Agents. These features could be readonly, but also methods (or tools) to allow the Agent operate with your application, for example, creating orders, updating post titles, reordering invoices, or creating reports for your bookings.
As a developer, this is a quick win! Providing access, even readonly, could expand the quality of the interaction between your users and your application. In my opinion, the benefits are: Agents deal with context very well, they can use the conversation history, and also extract required data to use the available tools. Agents can transform the data, providing "features" to your users that you didn't implement. For example building charts on the fly, or creating scripts to transform the data for another tool.
Quickly after the publication of the MCP protocol, the PHP community started working on a standarized SDK to help with the implementation of MCP servers. Even if the SDK is in active development right now, we are going to explore it and build a local MCP server, connecting Claude Desktop to it.
The idea behind the example is to open your application to Claude Desktop, so the Agent (Claude) can connect directly to your code using the specified tools. For production environments, there are many other considerations we should be handling, like authorization, rate limiting, data exchange and privacy, etc. We'll leave all these production grade issues for another day and jump into an example you can implement "today" in your CakePHP application.
Development vs. Production: This tutorial focuses on a local development setup for your CakePHP MCP server. Production environments require additional considerations including:
- Authentication and authorization
- Rate limiting
- Data privacy and security
- Audit logging
- Input validation and sanitization
- Error handling and monitoring
What is a CakePHP MCP Server?
A CakePHP MCP server is a specialized implementation that allows AI agents like Claude to interact with your CakePHP application through the Model Context Protocol. This CakePHP AI integration creates a bridge between your application logic and AI capabilities, enabling:
- Natural language interfaces for complex queries
- Automated content generation and management
- Real-time data analysis and reporting
Prerequisites
Before starting, ensure you have:
- PHP 8.1 or higher
- Composer
- SQLite or MySQL
- Claude Desktop (free tier available)
Step 1: Set Up the CakePHP CMS Application
We'll use the official CakePHP CMS tutorial.
# Clone the repository
git clone https://github.com/cakephp/cms-tutorial
cd cms-tutorial
# Install dependencies
composer install
# Run database migrations
bin/cake migrations migrate
# Start the development server
bin/cake server
Create a Test User
- Navigate to http://localhost:8765/users/add
- Create a new user with your preferred email and password
- Log in at http://localhost:8765/users/login
- Verify you can create an article via http://localhost:8765/articles/add
Step 2: Install Claude Desktop
Download and install Claude Desktop from https://claude.com/download
Step 3: Install the CakePHP MCP Plugin
To build your CakePHP MCP server, install the MCP utility plugin and SDK in your CakePHP project:
composer require cakedc/cakephp-mcp:dev-2.next-cake5 mcp/sdk:'dev-main#4b91567'
Note: These packages are in active development.
Step 4: Create the CakePHP MCP Server Script
Create a new file bin/mcp to initialize your CakePHP MCP server:
#!/usr/bin/env sh
cd /absolute/path/to/your/cms-tutorial && php vendor/cakedc/cakephp-mcp/bin/mcp-server
Important: Replace /absolute/path/to/your/cms-tutorial with your actual project path. For example: /home/user/cms-tutorial or C:\Users\YourName\cms-tutorial
Make the script executable:
chmod +x bin/mcp
Step 5: Create Your First CakePHP MCP Tool
Create the file: src/Mcp/Articles.php
<?php
namespace App\Mcp;
use App\Model\Entity\Article;
use Cake\ORM\Locator\LocatorAwareTrait;
use Mcp\Capability\Attribute\McpTool;
class Articles
{
use LocatorAwareTrait;
#[McpTool(name: 'createArticle')]
public function createArticle(string $title, string $body): array
{
try {
$article = new Article([
'title' => $title,
'body' => $body,
'user_id' => $this->fetchTable('Users')->find()->firstOrFail()->id, // a default user ID for simplicity
]);
if (!$this->fetchTable('Articles')->save($article)) {
return [
'success' => false,
'message' => 'Failed to create article: ' . json_encode($article->getErrors()),
];
}
return [
'success' => true,
'message' => 'Article created successfully',
];
} catch (\Throwable $e) {
return [
'success' => false,
'message' => 'Exception to create article: ' . $e->getMessage(),
];
}
}
}
The #[McpTool] Attribute: This PHP 8 attribute registers the method as an MCP tool that Claude can discover and use in your CakePHP AI integration. The name parameter defines how Claude will reference this tool.
Simplified User Assignment: For demonstration purposes, we're using the first available user. In production CakePHP AI integrations, you'd implement proper user authentication and context.
Step 6: Configure Claude Desktop for CakePHP MCP Integration
Add your CakePHP MCP server to Claude Desktop's configuration:
- Open Claude Desktop
- Go to Settings → Developer → Edit Config
- Add your MCP server configuration:
{
"mcpServers": {
"cakephp-cms": {
"command": "/absolute/path/to/your/cms-tutorial/bin/mcp"
}
}
}
- Save the configuration and restart Claude Desktop
Step 7: Test Your CakePHP AI Integration
Once Claude Desktop restarts, you should see your CakePHP MCP server connected:
- MCP Server Connected: Look for the server indicator in Claude Desktop showing your CakePHP MCP integration is active
- Available Tools: You can view available CakePHP MCP tools by clicking the tools icon
- The createArticle tool: Should appear in the list of available tools
- Now you can use the Claude Desktop prompt to generate articles, that will be saved directly into your CakePHP application!
Wrapping up
You've successfully built a CakePHP MCP server and implemented CakePHP AI integration with Claude! This Model Context Protocol CakePHP implementation opens up powerful possibilities for AI-enhanced user experiences and automation in your web applications.