Created
June 10, 2025 10:01
-
-
Save woganmay/b70d13c60e0cddd34794f94e7277bb39 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// index.php - Simplified MCP Server entry point | |
// For demonstration, we'll use a simple routing mechanism. | |
// In a real application, you'd use a framework like Laravel, Symfony, etc. | |
$requestMethod = $_SERVER['REQUEST_METHOD']; | |
$requestUri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); | |
$input = file_get_contents('php://input'); | |
$data = json_decode($input, true); // Decode the incoming JSON payload | |
header('Content-Type: application/json'); | |
// --- Simulate Basic Routing --- | |
if ($requestMethod === 'POST' && $requestUri === '/api/mcp/toolUse') { | |
// This is where an AI client would send a tool call | |
handleToolUse($data); | |
} elseif ($requestMethod === 'GET' && strpos($requestUri, '/api/mcp/resource/') === 0) { | |
// This is where an AI client would request a resource | |
handleResourceRequest($requestUri); | |
} else { | |
http_response_code(404); | |
echo json_encode(['error' => 'Not Found']); | |
} | |
// --- Function to handle tool calls --- | |
function handleToolUse($payload) { | |
// In a real MCP server, you'd validate the payload structure based on the MCP spec. | |
// For simplicity, we assume a basic 'toolName' and 'parameters'. | |
$toolName = $payload['toolName'] ?? null; | |
$parameters = $payload['parameters'] ?? []; | |
if ($toolName === 'get_current_weather') { | |
$city = $parameters['city'] ?? 'unknown'; | |
// Simulate calling an external weather API | |
$weatherData = simulateWeatherApiCall($city); | |
echo json_encode([ | |
'tool_result' => [ | |
'type' => 'tool_output', // As per MCP tool output message | |
'tool_name' => $toolName, | |
'content' => json_encode($weatherData) // Output usually as JSON string | |
] | |
]); | |
http_response_code(200); | |
} else { | |
http_response_code(400); | |
echo json_encode(['error' => 'Unknown tool', 'toolName' => $toolName]); | |
} | |
} | |
// --- Function to handle resource requests --- | |
function handleResourceRequest($uri) { | |
// Extract resource ID from URI, e.g., /api/mcp/resource/my_document_id | |
$parts = explode('/', $uri); | |
$resourceId = end($parts); | |
if ($resourceId === 'my_document_id') { | |
$documentContent = "This is the content of my important document.\nIt contains key information for the AI."; | |
echo json_encode([ | |
'resource_content' => [ | |
'type' => 'resource_content', // As per MCP resource content message | |
'resource_id' => $resourceId, | |
'content_type' => 'text/plain', | |
'content' => base64_encode($documentContent) // Base64 encode textual content | |
] | |
]); | |
http_response_code(200); | |
} else { | |
http_response_code(404); | |
echo json_encode(['error' => 'Resource not found', 'resourceId' => $resourceId]); | |
} | |
} | |
// --- Simulate External API Calls --- | |
function simulateWeatherApiCall($city) { | |
// In a real application, you'd use Guzzle or curl to call a real weather API (e.g., OpenWeatherMap) | |
// For demonstration, we return mock data. | |
$mockWeatherData = [ | |
'Cape Town' => ['temperature' => '18°C', 'conditions' => 'Partly cloudy', 'humidity' => '70%'], | |
'London' => ['temperature' => '12°C', 'conditions' => 'Rainy', 'humidity' => '85%'], | |
'New York' => ['temperature' => '25°C', 'conditions' => 'Sunny', 'humidity' => '60%'], | |
]; | |
return $mockWeatherData[$city] ?? ['temperature' => 'N/A', 'conditions' => 'unknown', 'humidity' => 'N/A']; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment