|
4 | 4 |
|
5 | 5 | use Illuminate\Http\JsonResponse; |
6 | 6 | use Illuminate\Http\Request; |
| 7 | +use Illuminate\Http\Response; |
7 | 8 | use Illuminate\Routing\Controller; |
8 | 9 | use Kirschbaum\Loop\McpHandler; |
| 10 | +use Kirschbaum\Loop\Services\SseService; |
| 11 | +use Symfony\Component\HttpFoundation\StreamedResponse; |
9 | 12 |
|
10 | 13 | class McpController extends Controller |
11 | 14 | { |
12 | | - public function __invoke(Request $request, McpHandler $mcpHandler): JsonResponse |
| 15 | + public function __construct(protected McpHandler $mcpHandler, protected SseService $sseService) {} |
| 16 | + |
| 17 | + /** |
| 18 | + * Handle MCP requests. |
| 19 | + */ |
| 20 | + public function __invoke(Request $request): JsonResponse|StreamedResponse|Response |
| 21 | + { |
| 22 | + $acceptsEventStream = strpos($request->header('Accept', ''), 'text/event-stream') !== false; |
| 23 | + $acceptsJson = strpos($request->header('Accept', ''), 'application/json') !== false; |
| 24 | + |
| 25 | + if (! $acceptsEventStream && ! $acceptsJson) { |
| 26 | + return response()->json([ |
| 27 | + 'jsonrpc' => '2.0', |
| 28 | + 'id' => null, |
| 29 | + 'error' => [ |
| 30 | + 'message' => 'Not Acceptable: Client must accept text/event-stream or application/json', |
| 31 | + ], |
| 32 | + ], 406); |
| 33 | + } |
| 34 | + |
| 35 | + $requestData = $request->all(); |
| 36 | + $containsRequests = $this->containsJsonRpcRequests($requestData); |
| 37 | + |
| 38 | + if (! $containsRequests) { |
| 39 | + $this->mcpHandler->handle($requestData); |
| 40 | + |
| 41 | + return response('', 202); |
| 42 | + } |
| 43 | + |
| 44 | + if ($this->clientPrefersJson($acceptsJson, $acceptsEventStream)) { |
| 45 | + return $this->handlePostJsonResponse($requestData); |
| 46 | + } |
| 47 | + |
| 48 | + return $this->handlePostSseResponse($requestData); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Handle SSE response for POST requests. |
| 53 | + * |
| 54 | + * @param array $messages JSON-RPC messages from the client |
| 55 | + */ |
| 56 | + protected function handlePostSseResponse( |
| 57 | + array $messages |
| 58 | + ): StreamedResponse { |
| 59 | + $response = $this->sseService->createPostSseResponse( |
| 60 | + $messages, |
| 61 | + fn ($message) => $this->mcpHandler->handle($message), |
| 62 | + ); |
| 63 | + |
| 64 | + return $response; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Handle JSON response for POST requests. |
| 69 | + */ |
| 70 | + protected function handlePostJsonResponse(array $messages): JsonResponse |
13 | 71 | { |
14 | | - $message = $request->all(); |
| 72 | + $response = $this->mcpHandler->handle($messages); |
15 | 73 |
|
16 | | - return response()->json($mcpHandler->handle($message)); |
| 74 | + return response()->json($response); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Handle GET requests (server-initiated messages) according to MCP specification. |
| 79 | + */ |
| 80 | + protected function handleGetRequest(Request $request) |
| 81 | + { |
| 82 | + // TODO |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Check if the input contains any JSON-RPC requests. |
| 87 | + * |
| 88 | + * @param mixed $input |
| 89 | + */ |
| 90 | + protected function containsJsonRpcRequests($input): bool |
| 91 | + { |
| 92 | + // If input is an array with numeric keys, it's a batch |
| 93 | + if (is_array($input) && array_keys($input) === range(0, count($input) - 1)) { |
| 94 | + foreach ($input as $item) { |
| 95 | + if ($this->isJsonRpcRequest($item)) { |
| 96 | + return true; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + return $this->isJsonRpcRequest($input); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Check if the client prefers JSON over SSE. |
| 108 | + */ |
| 109 | + protected function clientPrefersJson($acceptsJson, $acceptsEventStream): bool |
| 110 | + { |
| 111 | + return $acceptsJson && $acceptsEventStream === false; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Check if an item is a JSON-RPC request. |
| 116 | + */ |
| 117 | + protected function isJsonRpcRequest($item): bool |
| 118 | + { |
| 119 | + return is_array($item) && |
| 120 | + isset($item['jsonrpc']) && |
| 121 | + $item['jsonrpc'] === '2.0' && |
| 122 | + isset($item['method']) && |
| 123 | + isset($item['id']); |
17 | 124 | } |
18 | 125 | } |
0 commit comments