-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPI.php
More file actions
178 lines (151 loc) · 5.49 KB
/
API.php
File metadata and controls
178 lines (151 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
declare(strict_types=1);
namespace Piwik\Plugins\McpServer;
use Piwik\API\Request as ApiRequest;
use Matomo\Dependencies\McpServer\Http\Discovery\Psr17Factory;
use Matomo\Dependencies\McpServer\Mcp\Schema\JsonRpc\Error as JsonRpcError;
use Matomo\Dependencies\McpServer\Mcp\Server\Transport\StreamableHttpTransport;
use Matomo\Dependencies\McpServer\Psr\Http\Message\ResponseInterface;
use Matomo\Dependencies\McpServer\Psr\Http\Message\ServerRequestInterface;
use Piwik\NoAccessException;
use Piwik\Piwik;
use Piwik\Http\BadRequestException;
use Piwik\Plugins\McpServer\Support\Api\JsonRpcErrorResponseFactory;
use Piwik\Plugins\McpServer\Support\Api\JsonRpcRequestIdExtractor;
use Piwik\Plugins\McpServer\Support\Api\McpEndpointGuard;
use Piwik\Plugins\McpServer\Support\Api\McpEndpointSpec;
use Piwik\Request;
class API extends \Piwik\Plugin\API
{
public function __construct(
private McpServerFactory $factory,
private McpEndpointGuard $endpointGuard,
private JsonRpcErrorResponseFactory $jsonRpcErrorResponseFactory,
private JsonRpcRequestIdExtractor $jsonRpcRequestIdExtractor,
private SystemSettings $systemSettings
) {
}
/**
* @internal
*/
public function mcp(): ResponseInterface
{
$requestParams = Request::fromRequest();
$format = strtolower($requestParams->getStringParameter('format', ''));
$errorFactory = $this->jsonRpcErrorResponseFactory;
// If format!=mcp, Matomo will use a non-MCP renderer that cannot serialize PSR-7 responses.
if ($format !== McpEndpointSpec::FORMAT) {
throw new BadRequestException(
'MCP endpoint requires format=mcp. Use module=API&method=McpServer.mcp&format=mcp.'
);
}
try {
$request = $this->createRequestFromGlobals();
} catch (\Throwable $e) {
return $errorFactory->create(
500,
JsonRpcError::INTERNAL_ERROR,
McpEndpointSpec::INTERNAL_ERROR
);
}
$requestMetadata = $this->jsonRpcRequestIdExtractor->extractRequestMetadata($request);
$requestId = $requestMetadata['requestId'];
$guardError = $this->endpointGuard->validate(
$format,
$requestParams->getStringParameter('module', ''),
$requestParams->getStringParameter('method', ''),
$this->isCurrentApiRequestRoot(),
$this->getRootApiRequestMethod()
);
if ($guardError !== null) {
return $errorFactory->create(400, JsonRpcError::INVALID_REQUEST, $guardError, $requestId);
}
try {
$this->checkUserHasSomeViewAccess();
} catch (\Throwable $e) {
if ($this->isUnauthorizedLike($e)) {
return $this->createUnauthorizedResponse($requestId);
}
return $errorFactory->create(
500,
JsonRpcError::INTERNAL_ERROR,
McpEndpointSpec::INTERNAL_ERROR,
$requestId
);
}
if (!$this->isMcpEnabled()) {
return $this->createDisabledResponse($requestMetadata['topLevelRequestId']);
}
try {
$server = $this->factory->createServer();
$transport = new StreamableHttpTransport($request);
return $server->run($transport);
} catch (\Throwable $e) {
return $errorFactory->create(
500,
JsonRpcError::INTERNAL_ERROR,
McpEndpointSpec::INTERNAL_ERROR,
$requestId
);
}
}
protected function createRequestFromGlobals(): ServerRequestInterface
{
return (new Psr17Factory())->createServerRequestFromGlobals();
}
protected function checkUserHasSomeViewAccess(): void
{
Piwik::checkUserHasSomeViewAccess();
}
protected function createUnauthorizedResponse(string|int $requestId = ''): ResponseInterface
{
return $this->jsonRpcErrorResponseFactory->create(
401,
JsonRpcError::INVALID_REQUEST,
McpEndpointSpec::UNAUTHORIZED_ERROR,
$requestId,
['WWW-Authenticate' => 'Bearer realm="mcp"']
);
}
protected function isMcpEnabled(): bool
{
return $this->systemSettings->isMcpEnabled();
}
protected function isCurrentApiRequestRoot(): bool
{
return ApiRequest::isCurrentApiRequestTheRootApiRequest();
}
protected function getRootApiRequestMethod(): string
{
return (string) ApiRequest::getRootApiRequestMethod();
}
private function isUnauthorizedLike(\Throwable $e): bool
{
$current = $e;
do {
if ($current instanceof NoAccessException) {
return true;
}
$current = $current->getPrevious();
} while ($current !== null);
return false;
}
protected function createDisabledResponse(string|int|null $topLevelRequestId): ResponseInterface
{
if ($topLevelRequestId === null) {
return (new Psr17Factory())->createResponse(403);
}
return $this->jsonRpcErrorResponseFactory->create(
403,
JsonRpcError::INVALID_REQUEST,
McpEndpointSpec::DISABLED_ERROR,
$topLevelRequestId
);
}
}