MCP ExplorerExplorer

Mcp Server Nodejs And Php Client Example

@YusufBesimon 15 days ago
1 MIT
FreeCommunity
AI Systems
MCP Server with Node.js and PHP Client Integration for OpenAI

Overview

What is Mcp Server Nodejs And Php Client Example

mcp-server-nodejs-and-php-client-example is a project that demonstrates the integration of a Model Customization Protocol (MCP) server built with Node.js and a PHP client that interacts with OpenAI’s API.

Use cases

Use cases include building chat applications that utilize AI tools, creating automated workflows that leverage AI capabilities, and developing custom integrations for various AI services.

How to use

To use this project, set up the Node.js MCP server by installing the required packages and running the server script. Then, configure the PHP client with your OpenAI API key and the MCP server URL, and execute the PHP script to interact with the server.

Key features

Key features include dynamic tool fetching from the MCP server, integration with OpenAI’s API, and support for multiple AI platforms such as OpenAI, Claude.ai, and Cursor.

Where to use

This project can be used in applications that require AI-driven functionalities, such as chatbots, automated tools, and any system that benefits from custom AI model interactions.

Content

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!

Tools

No tools

Comments