sderhy/hello-world-mcp-server
If you are the rightful owner of hello-world-mcp-server 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.
A minimal Model Context Protocol (MCP) server implementation in pure PHP with no dependencies.
Hello World MCP Server - PHP Implementation
A minimal Model Context Protocol (MCP) server implementation in pure PHP with no dependencies. This demonstrates how MCP works at a fundamental level.
How MCP Works
The Model Context Protocol (MCP) enables AI assistants like Claude to interact with external tools and data sources. Here's the architecture:
┌─────────────┐ JSON-RPC over stdio ┌─────────────┐
│ │ ────────────────────────────────> │ │
│ MCP Client │ │ MCP Server │
│ (Claude) │ <──────────────────────────────── │ (PHP) │
└─────────────┘ └─────────────┘
Key Concepts
- Transport: Communication happens via stdio (standard input/output)
- Protocol: JSON-RPC 2.0 format for all messages
- Tools: Functions the server exposes that the client can call
- Schema: Each tool has an input schema describing its parameters
Message Flow
- Initialize: Client sends initialize request with capabilities
- List Tools: Client requests available tools
- Call Tool: Client invokes a tool with arguments
- Response: Server returns results
Files
mcp-server.php- The MCP server implementationmcp-config.json- Configuration for Claude Codetest-mcp.sh- Test script to verify the server worksREADME.md- This file
Prerequisites
You need PHP installed on your system (PHP 7.4 or higher recommended).
Install PHP on macOS
# Using Homebrew
brew install php
# Verify installation
php --version
Install PHP on Linux
# Ubuntu/Debian
sudo apt-get update
sudo apt-get install php-cli
# Fedora/CentOS
sudo dnf install php-cli
# Verify installation
php --version
Testing the Server
Run the test script to verify the server works correctly:
./test-mcp.sh
You should see JSON responses for:
- Server initialization
- List of available tools
- Tool execution results
Manual Testing
You can also test manually by piping JSON-RPC requests:
# Initialize the server
echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test","version":"1.0.0"}}}' | php mcp-server.php
# List available tools
echo '{"jsonrpc":"2.0","id":1,"method":"tools/list","params":{}}' | php mcp-server.php
# Call the helloWorld tool
echo '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"helloWorld","arguments":{"name":"PHP"}}}' | php mcp-server.php
Using with Claude Code
To use this MCP server with Claude Code:
-
Option A: Copy the configuration to Claude Code's global config
# Location of Claude Code config (adjust path if needed) # macOS/Linux: ~/.claude-code/mcp.json # Windows: %APPDATA%\claude-code\mcp.jsonAdd this to the
mcpServerssection:{ "mcpServers": { "hello-world-php": { "command": "php", "args": ["mcp-server.php"], "cwd": "/Users/sergederhy/work/hello-world-mcp/test", "description": "Hello World MCP server in PHP" } } } -
Option B: Use the local config file
The
mcp-config.jsonfile in this directory can be used as a reference. -
Restart Claude Code to load the new MCP server
-
Verify: Claude Code should now have access to the
helloWorldtool
Understanding the Code
Server Structure
class MCPServer {
// Server metadata
private $serverInfo = ['name' => '...', 'version' => '...'];
// Registered tools
private $tools = [];
// Register a tool
public function registerTool($name, $schema, $handler) { ... }
// Handle incoming requests
public function handleRequest($request) { ... }
// Start listening on stdio
public function start() { ... }
}
Request Handling
The server handles these JSON-RPC methods:
- initialize - Handshake and capability negotiation
- tools/list - Returns available tools and their schemas
- tools/call - Executes a tool with given arguments
Adding New Tools
To add a new tool, register it in the constructor:
$this->registerTool('myTool', [
'description' => 'Description of what this tool does',
'inputSchema' => [
'type' => 'object',
'properties' => [
'param1' => [
'type' => 'string',
'description' => 'Parameter description'
]
]
]
], function($params) {
// Your tool logic here
return [
'content' => [
['type' => 'text', 'text' => 'Result']
]
];
});
Architecture Decisions
This implementation is intentionally minimal to demonstrate MCP concepts:
- No dependencies: Pure PHP, no composer packages
- Single file: All code in one file for easy understanding
- Synchronous: Simple blocking I/O for clarity
- Basic validation: Minimal error handling to keep code readable
For production use, consider:
- Adding input validation
- Better error handling
- Logging to files instead of stderr
- Using async I/O for better performance
- Adding more sophisticated tool schemas
Common Issues
PHP not found
# Install PHP first (see Prerequisites section)
brew install php # macOS
Permission denied
chmod +x mcp-server.php
chmod +x test-mcp.sh
Server not responding
Check stderr output for debug messages:
echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}' | php mcp-server.php 2>&1
Next Steps
- Experiment: Modify the helloWorld tool to do something different
- Add Tools: Create new tools (calculator, file reader, etc.)
- Connect to Claude: Configure Claude Code to use this server
- Learn More: Read the MCP documentation
License
ISC