DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • The Agent Protocol Stack: MCP vs. A2A vs. AG-UI
  • Understanding the Shifting Protocols That Secure AI Agents
  • Model Context Protocol Vs Agent2Agent: Practical Integration with Enterprise Data
  • Agentic Commerce: A Developer's Guide to Google's Universal Commerce Protocol (UCP)

Trending

  • A Deep Dive into Tracing Agentic Workflows (Part 1)
  • AI Agents in Java: Architecting Intelligent Health Data Systems
  • S3 Vectors: How to Build a RAG Without a Vector Database
  • Solving the Mystery: Why Java RSS Grows in Docker on M1 Macs
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. AI Agents in PHP With Model Context Protocol

AI Agents in PHP With Model Context Protocol

Supercharge your PHP applications with AI Agents using MCP (Model Context Protocol). Connect powerful tools to LLMs with minimal coding.

By 
Valerio Barbera user avatar
Valerio Barbera
·
Jun. 16, 25 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
1.3K Views

Join the DZone community and get the full member experience.

Join For Free

If you are building AI agents you’ve probably heard about MCP (Model Context Protocol). Actually, really everyone is talking about MCP right now. From what I’ve read online, it seems many people don’t seem to have the slightest idea what we’re talking about, and the new product development opportunities associated with that.

I want to break down a couple of key concepts to help establish the foundational understanding you might need as a software developer exploring new ideas for agent implementations. This will also help clarify the role of MCP servers when directly connected to an AI agent, such as a Neuron AI Agent, as one example.

Introduction to LLM Tools

One of the things we as engineers love are standards. And the reason standards are important is they allow for us as engineers and developers, to build systems that communicate with each other.

Imagine the REST APIs. The idea to have a standard process to authenticate and use a third party service, created a big wave of innovation for years.  The idea behind MCP is to allow developers to implement a standard protocol to expose their application services to LLMs.

At this point, we have to remember that LLM by themselves are incapable of doing anything. They are just “token tumblers”. If you open any LLM chat and you ask to send an email it won’t know how to do that.

It will just tell you “I don’t know how to send an email, I can eventually write the email content if you want.” At its core an LLM can just manage text.

LLM can just manage text


The next evolution of these platforms was when developers figured out how to combine the LLMs capabilities with a mechanism to make functions (or callbacks) available to LLMs. 

2nd stage, LLM tools


Take the ability of the recent chat interface where you can paste a web url into the message, and the LLM is able to fetch its content to give you the final response.

Imagine this prompt: “Can you give me advice on how can I improve SEO performance of this article: https://exmaple.com/blog/article”

The LLM itself is not capable of doing this task. But what developers have done is to construct a textual protocol to make LLMs able to ask the program that is executing the LLM for a function in charge of providing the content of the web page to continue to formulate the final response.

Using this mechanism developers were able to implement and provide any sort of functions (tools) to LLMs in order to perform actions and resolve user questions with information that are not in their training dataset. 

If you do not provide an LLM with a tool to get the content of a web page, they basically are not able to complete this task.

You can now make functions available to LLMs also to perform queries to your database, or gather information from external APIs, or any other task you need for your specific usa case.

Before MCP (Model Context Protocol)

LLMs started to become more powerful when we connect tools to them, because we can join the reasoning power of LLM with the ability to perform actions to the external world.

The problem here is that it could be really frustrating if you need to build an assistant that does multiple tasks. Imagine reading your email, searching on the internet, gathering information from your database, connecting to external services like Google Drive to read documents, GitHub for code, knowledge base, and any other sort of resources.

You can imagine how the implementation of the Agent could become really cumbersome. It could also be really complicated to stack all these tools together and make them able to work coherently in the context of the LLM.

Another level of complexity is that each service we want to connect with has its own APIs, with different technical requirements, and they should be implemented from scratch by every developer that wants to talk with these external services.

Some companies can do it, for many others it would simply be impossible.

This is where we are right now.

Introducing MCP (Model Context Protocol)

MCP is a layer between your LLM and the tools you want to connect.

3rd stage, LLM MCP servers


Now companies can implement an MCP server that is basically a new way to expose their APIs, but in a way that is ready to use by LLMs.

Think about the Stripe APIs. They provide features to access any kind of information about subscriptions, invoices, transactions, and so on. Using the Stripe MCP server (built by Stripe) you can basically expose the entire Stripe APIs to your LLM to make it able to gather information and answer questions about the status of your finance, or customer questions about their subscriptions and invoices. The Agent can even perform actions like cancel subscriptions or activate a new one for a customer.

You just have to install the MCP server, connect your agent to the resources exposed by the server (we are going to see how to do it in a moment), and you instantly have an Agent with powerful skills without all the effort to implement the Stripe API calls. Furthermore you no longer have to worry in case Stripe changes its APIs. Even highly interconnected systems, made up of multiple steps dependent on each other, can be developed more easily and be more reliable.

Using simple tools to implement all actions one at a time would be impossible to overcome certain levels of complexity.

How MCP Works

Let’s get into a practical example of how you can host an MCP server to be used by your Agents.

At its core MCP needs three components to work: A Host, an MCP server, and an MCP client.

Don’t let the word “server” fool you. At this stage of the protocol implementations the MCP server runs in the same machine of your Agent. They communicate via the standard input/output local interface (stdio).

Host machine

Probably in the future it will be possible to host MCP servers remotely, but now it works on board the same machine. So you have to install the MCP server on your computer first during development, and in your cloud machine if you want to deploy the implementation in the production environment.

I will go deeper into MCP server installation in another dedicated article, for now you can access the installation instructions on the MCP servers repository.

Here are some websites where you can explore a list of available MCP servers: 

  • https://github.com/modelcontextprotocol/servers
  • https://mcp-get.com/

At this stage it’s not all sunshine and rainbows, there are some technical things to configure. You have to set up the server, configure some files, but once you figure it out, your Agents can become very powerful and capable of completing any sort of tasks autonomously.

Connect Your AI Agent to MCP servers in PHP

To get started with AI agent development in PHP, you can install the Neuron AI framework. Neuron is an open source project designed to support PHP developers in building agent-based applications without switching to another language.

It includes tools for creating agents, working with retrieval-augmented generation (RAG) systems, using vector stores, generating embeddings, and monitoring system behavior.

Installation is available via Composer using the following command. You can find more technical details in the documentation.

You can install Neuron via composer with the command below:

Shell
 
composer require inspector-apm/neuron-ai


Create your custom agent extending the NauronAI\Agent class: 

PHP
 
use NeuronAI\Agent;
use NeuronAI\Providers\AIProviderInterface;
use NeuronAI\Providers\Anthropic\Anthropic;

class MyAgent extends Agent
{
    public function provider(): AIProviderInterface
    {
        // return an AI provider (Anthropic, OpenAI, Mistral, etc.)
        return new Anthropic(
            key: 'ANTHROPIC_API_KEY',
            model: 'ANTHROPIC_MODEL',
        );
    }
	
	
	public function instructions()
	{
		return "LLM system instructions.";
	}
}


Now you need to attach tools to the Agent so it can perform tasks in the application context and resolve questions sent by you or your users. If you need to implement a specific action related to your specific environment you can attach a Tool and create your own implementation: 

PHP
 
use NeuronAI\Agent;
use NeuronAI\Providers\AIProviderInterface;
use NeuronAI\Providers\Anthropic\Anthropic;
use NeuronAI\Tools\Tool;
use NeuronAI\Tools\ToolProperty;

class MyAgent extends Agent
{
    public function provider(): AIProviderInterface
    {
        // return an AI provider (Anthropic, OpenAI, Mistral, etc.)
        return new Anthropic(
            key: 'ANTHROPIC_API_KEY',
            model: 'ANTHROPIC_MODEL',
        );
    }
	
	
	public function instructions()
	{
		return "LLM system instructions.";
	}
	
	public function tools(): array
    {
        return [
            Tool::make(
                "get_article_content", 
                "Use the ID of the article to get its content."
            )->addProperty(
                new ToolProperty(
                    name: 'article_id',
                    type: 'integer',
                    description: 'The ID  of the article you want to analyze.',
                    required: true
                )
            )->setCallable(function (string $article_id) {
                // You should use your DB layer here...
                $stm = $pdo->prepare("SELECT * FROM articles WHERE id=? LIMIT 1");
                $stm->execute([$article_id]);
                return json_encode(
                    $stmt->fetch(PDO::FETCH_ASSOC)
                );
            })
        ];
    }
}


For other tools you can search for a ready to use MCP servers and attach the exposed tools to your agent. Neuron provides you with the McpConnector components to automatically gather available tools from the server and attach them to your Agent. 

PHP
 
use NeuronAI\Agent;
use NeuronAI\Providers\AIProviderInterface;
use NeuronAI\Providers\Anthropic\Anthropic;
use NeuronAI\Tools\Tool;
use NeuronAI\Tools\ToolProperty;

class MyAgent extends Agent
{
    public function provider(): AIProviderInterface
    {
        // return an AI provider (Anthropic, OpenAI, Mistral, etc.)
        return new Anthropic(
            key: 'ANTHROPIC_API_KEY',
            model: 'ANTHROPIC_MODEL',
        );
    }
	
	
	public function instructions()
	{
		return "LLM system instructions.";
	}
	
	public function tools(): array
    {
        return [
			// Load tools from an MCP server
			...McpConnector::make([
                'command' => 'npx',
                'args' => ['-y', '@modelcontextprotocol/server-everything'],
            ])->tools(),
		
			// Your custom tools
            Tool::make(
                "get_article_content", 
                "Use the ID of the article to get its content."
            )->addProperty(
                new ToolProperty(
                    name: 'article_id',
                    type: 'integer',
                    description: 'The ID  of the article you want to analyze.',
                    required: true
                )
            )->setCallable(function (string $article_id) {
                // You should use your DB layer here...
                $stm = $pdo->prepare("SELECT * FROM articles WHERE id=? LIMIT 1");
                $stm->execute([$article_id]);
                return json_encode(
                    $stmt->fetch(PDO::FETCH_ASSOC)
                );
            })
        ];
    }
}


Neuron automatically discovers the tools exposed by the server and connects them to your agent.

When the agent decides to run a tool, Neuron will generate the appropriate request to call the tool on the MCP servers and return the result to the LLM to continue the task.  It feels exactly like with your own defined tools, but you can access a huge archive of predefined actions your agent can perform with just one line of code.

You can also check out the MCP server connection example in the documentation.

Let Us Know What You Are Building

It was clear to me that MCP is a big deal in the sense that now Agents can become capable of doing an incredible amount of tasks with so little effort in developing these features.

With all these tools available also the product development opportunities will grow exponentially. And I can’t wait to see what you are going to build.

AI PHP Protocol (object-oriented programming)

Published at DZone with permission of Valerio Barbera. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • The Agent Protocol Stack: MCP vs. A2A vs. AG-UI
  • Understanding the Shifting Protocols That Secure AI Agents
  • Model Context Protocol Vs Agent2Agent: Practical Integration with Enterprise Data
  • Agentic Commerce: A Developer's Guide to Google's Universal Commerce Protocol (UCP)

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook