Align MCP JSON-RPC error responses
This commit is contained in:
1 parent
ebb71b4769
commit
ea434033f0
1 file changed
+32
-3
@@ -4,6 +4,19 @@ declare(strict_types=1);
|
|||||||
require_once __DIR__ . '/../../modules/shared/db.php';
|
require_once __DIR__ . '/../../modules/shared/db.php';
|
||||||
require_once __DIR__ . '/../../modules/market_management/mcp.php';
|
require_once __DIR__ . '/../../modules/market_management/mcp.php';
|
||||||
|
|
||||||
|
final class MarketManagementMcpJsonRpcException extends RuntimeException
|
||||||
|
{
|
||||||
|
public function __construct(private int $rpcCode, string $message, ?Throwable $previous = null)
|
||||||
|
{
|
||||||
|
parent::__construct($message, 0, $previous);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function rpcCode(): int
|
||||||
|
{
|
||||||
|
return $this->rpcCode;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
header('Content-Type: application/json; charset=utf-8');
|
header('Content-Type: application/json; charset=utf-8');
|
||||||
header('Cache-Control: no-store');
|
header('Cache-Control: no-store');
|
||||||
|
|
||||||
@@ -18,6 +31,10 @@ try {
|
|||||||
throw new InvalidArgumentException('Ungültige JSON-RPC-Anfrage.');
|
throw new InvalidArgumentException('Ungültige JSON-RPC-Anfrage.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (array_key_exists('params', $request) && !is_array($request['params'])) {
|
||||||
|
throw new MarketManagementMcpJsonRpcException(-32602, 'Parameter müssen als Objekt oder Array übergeben werden.');
|
||||||
|
}
|
||||||
|
|
||||||
$id = $request['id'] ?? null;
|
$id = $request['id'] ?? null;
|
||||||
$method = (string) ($request['method'] ?? '');
|
$method = (string) ($request['method'] ?? '');
|
||||||
$params = is_array($request['params'] ?? null) ? $request['params'] : [];
|
$params = is_array($request['params'] ?? null) ? $request['params'] : [];
|
||||||
@@ -36,6 +53,10 @@ try {
|
|||||||
} elseif ($method === 'tools/call') {
|
} elseif ($method === 'tools/call') {
|
||||||
$tool = (string) ($params['name'] ?? '');
|
$tool = (string) ($params['name'] ?? '');
|
||||||
$arguments = is_array($params['arguments'] ?? null) ? $params['arguments'] : [];
|
$arguments = is_array($params['arguments'] ?? null) ? $params['arguments'] : [];
|
||||||
|
if (array_key_exists('arguments', $params) && !is_array($params['arguments'])) {
|
||||||
|
throw new MarketManagementMcpJsonRpcException(-32602, 'Tool-Argumente müssen als Objekt übergeben werden.');
|
||||||
|
}
|
||||||
|
try {
|
||||||
$result = [
|
$result = [
|
||||||
'content' => [[
|
'content' => [[
|
||||||
'type' => 'text',
|
'type' => 'text',
|
||||||
@@ -43,16 +64,24 @@ try {
|
|||||||
]],
|
]],
|
||||||
'isError' => false,
|
'isError' => false,
|
||||||
];
|
];
|
||||||
|
} catch (InvalidArgumentException $e) {
|
||||||
|
throw new MarketManagementMcpJsonRpcException(-32602, $e->getMessage(), $e);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new InvalidArgumentException('Unbekannte JSON-RPC-Methode: ' . $method);
|
throw new MarketManagementMcpJsonRpcException(-32601, 'Unbekannte JSON-RPC-Methode: ' . $method);
|
||||||
}
|
}
|
||||||
|
|
||||||
echo json_encode(['jsonrpc' => '2.0', 'id' => $id, 'result' => $result], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR);
|
echo json_encode(['jsonrpc' => '2.0', 'id' => $id, 'result' => $result], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR);
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
http_response_code($e instanceof RuntimeException && str_contains($e->getMessage(), 'Token') ? 401 : 400);
|
$isAuthError = $e instanceof RuntimeException && str_contains($e->getMessage(), 'Token');
|
||||||
|
$isJsonRpcError = $e instanceof MarketManagementMcpJsonRpcException;
|
||||||
|
http_response_code($isJsonRpcError ? 200 : ($isAuthError ? 401 : 400));
|
||||||
echo json_encode([
|
echo json_encode([
|
||||||
'jsonrpc' => '2.0',
|
'jsonrpc' => '2.0',
|
||||||
'id' => $request['id'] ?? null,
|
'id' => $request['id'] ?? null,
|
||||||
'error' => ['code' => -32600, 'message' => $e->getMessage()],
|
'error' => [
|
||||||
|
'code' => $isJsonRpcError ? $e->rpcCode() : -32600,
|
||||||
|
'message' => $e->getMessage(),
|
||||||
|
],
|
||||||
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user