DEV Community

Khaled Hammrouni
Khaled Hammrouni

Posted on

07 - MCP in PHP - Connect an Agent to a Remote Tool Server in 4 Lines

If you build AI tools in more than one language, you know the tax: every integration you write for Python, you re-write for PHP. MCP (Model Context Protocol) kills that tax. It's a standard protocol for exposing tools, so your PHP agent talks to the same tool servers a Python, Node, or Go agent would. You don't re-implement integrations per language - you point at a server and let the agent discover the tools.

This is the shortest path to a genuinely capable agent in PHP.

The four lines

1. Connect to the MCP server

McpClient just points at a URL - no handshake code, no auth setup for a public server like this one.

use NanoAgent\Agent;
use NanoAgent\Mcp\McpClient;

$mcpClient = new McpClient('https://mcp.deepwiki.com/mcp');
Enter fullscreen mode Exit fullscreen mode

2. Build the agent as usual

Nothing MCP-specific here yet - it's a normal Agent with a system prompt describing what it's for.

$agent = new Agent(
    llm: $llmConfig,
    systemPrompt: "You are a research assistant with access to MCP tools for "
                . "exploring GitHub repositories. The user will name a repository "
                . "as 'owner/repo'. Use the available tools to answer their question."
);
Enter fullscreen mode Exit fullscreen mode

3. Register the server, then chat

registerMcpServer() asks the remote server what tools it offers and wires them into the agent - that's the "discovery" step. From here, calling chat() works exactly like it would with local tools.

$discovered = $agent->registerMcpServer($mcpClient);
$agent->enableActivityLogging();

$answer = $agent->chat("Repository: facebook/react\nQuestion: What does this repo do?");
Enter fullscreen mode Exit fullscreen mode

Connect → register → chat. The agent discovers the server's tools at runtime (that's what registerMcpServer() returns) and calls them exactly like local FunctionTools. No tool schema by hand, no new DSL, no sidecar process.

What the agent is talking to here

The example uses DeepWiki's public MCP server - free, no auth - which exposes tools for asking questions about the source and docs of any public GitHub repository. So the agent above can genuinely read and summarize an unfamiliar codebase. Point the same four lines at any other MCP server (search, databases, file systems, your own services) and the agent picks up that server's tools instead.

Local tool vs. MCP tool - the comparison

FunctionTool (local) MCP tool (remote)
Where it runs in your PHP process on a remote server
Who defines it you, in PHP the MCP server
Reuse across languages no yes - one server, any client
Auth / infra none server handles it
Discovery you list them automatic via registerMcpServer()

You use local tools for logic that lives in your app (your DB, your business rules). You use MCP for capabilities that should be shared, versioned, or maintained by someone else.

Why this matters for PHP specifically

PHP shops don't leave PHP to add an AI feature. MCP means they don't have to build the feature either - point at a server that already has the tool. The agent is a thin PHP layer over a standard protocol, which is exactly the shape you want for a language that has no AI framework ecosystem.

Getting started

  1. Find or run an MCP server (DeepWiki is a zero-config one).
  2. new McpClient($url).
  3. $agent->registerMcpServer($client).
  4. Ask it something the server can answer.

That's the whole integration.


Part of the NanoAgent examples series. Landing + demos.

Top comments (0)