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 dayong@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
Resources
0
Prompts
0

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!