YusufBesim/mcp-server-nodejs-and-php-client-example
If you are the rightful owner of mcp-server-nodejs-and-php-client-example and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.
This project demonstrates how to build a minimal Model Customization Protocol (MCP) server using Node.js and interact with it via a PHP client using openai-php/client.
increment
Increments the counter by one.
MCP Server and Usage on PHP with OpenAI
This project demonstrates how to build a minimal MCP (Model Customization Protocol) server using Node.js and how to interact with it via a PHP client using openai-php/client. The PHP client fetches available tools from the MCP server dynamically and uses them in a function calling context with OpenAI's API.
๐ Folder Structure
mcp-server-and-usage-on-php-with-openai/
โโโ server/
โ โโโ server.js
โ โโโ package.json
โโโ client/
โโโ client.php
โโโ composer.json (installed via Composer)
๐ Server (Node.js MCP Server)
This server supports OpenAI, Claude.ai, Cursor, Windsurf etc.
๐ฆ Requirements
- Node.js v16+ recommended
- npm (Node Package Manager)
๐ฅ Install
cd server
npm install
โถ๏ธ Run
node server.js
By default, the server listens on port
3000
at the endpointPOST /mcp
.
๐ง Client (PHP + OpenAI)
๐ฆ Requirements
- PHP 8.0+
- Composer
๐ฅ Install
cd client
composer install
This will install
openai-php/client
and its dependencies.
โ๏ธ Configuration
Edit client.php
and set your OpenAI API key:
$this->openaiClient = OpenAI::client("your-openai-api-key");
Also make sure the $mcpServerUrl
variable is correctly pointing to your MCP server (default: http://localhost:3000/mcp
or your public server IP).
โถ๏ธ Run
php client.php
The PHP script will:
- Fetch tools from the MCP server via a
tools/list
call. - Map them to OpenAI's expected function format.
- Call the OpenAI chat completion endpoint using these tools.
๐งช Example Response
MCP server returns tools like:
{
"jsonrpc": "2.0",
"result": {
"tools": [
{
"name": "increment",
"description": "Increments the counter by one.",
"inputSchema": {
"type": "object",
"properties": {},
"required": []
}
}
]
},
"id": "test"
}
The client converts this into the OpenAI-compatible format automatically.
๐ ๏ธ Stack
- Node.js (Express)
- PHP
- Composer
- openai-php/client
- JSON-RPC 2.0
๐ License
MIT โ use freely and customize as needed.
Ask if you need help!