mcp-server-nodejs-and-php-client-example

YusufBesim/mcp-server-nodejs-and-php-client-example

3.2

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.

Tools
  1. 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 endpoint POST /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:

  1. Fetch tools from the MCP server via a tools/list call.
  2. Map them to OpenAI's expected function format.
  3. 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

๐Ÿ“„ License

MIT โ€“ use freely and customize as needed.

Ask if you need help!