@whook/mcp 26.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -0
- package/README.md +93 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/index.test.d.ts +1 -0
- package/dist/index.test.js +550 -0
- package/dist/index.test.js.map +1 -0
- package/dist/services/httpRouter.d.ts +20 -0
- package/dist/services/httpRouter.js +5 -0
- package/dist/services/httpRouter.js.map +1 -0
- package/dist/services/mcpHandler.d.ts +60 -0
- package/dist/services/mcpHandler.js +334 -0
- package/dist/services/mcpHandler.js.map +1 -0
- package/dist/wrappers/wrapHTTPRouterWithMCPServer.d.ts +17 -0
- package/dist/wrappers/wrapHTTPRouterWithMCPServer.js +72 -0
- package/dist/wrappers/wrapHTTPRouterWithMCPServer.js.map +1 -0
- package/package.json +162 -0
- package/src/index.test.ts +598 -0
- package/src/index.ts +9 -0
- package/src/services/httpRouter.ts +8 -0
- package/src/services/mcpHandler.ts +551 -0
- package/src/types.d.ts +7 -0
- package/src/wrappers/wrapHTTPRouterWithMCPServer.ts +137 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
declare const _default: import("knifecycle").ProviderInitializer<import("@whook/whook").WhookHTTPRouterConfig & {
|
|
2
|
+
ENV: import("application-services").AppEnvVars;
|
|
3
|
+
ROUTES_HANDLERS: import("@whook/whook").WhookHandlersService;
|
|
4
|
+
API: import("@whook/whook").WhookOpenAPI;
|
|
5
|
+
DEFINITIONS: import("@whook/whook").WhookDefinitions;
|
|
6
|
+
PARSERS?: import("@whook/whook").WhookParsers;
|
|
7
|
+
STRINGIFIERS?: import("@whook/whook").WhookStringifiers;
|
|
8
|
+
DECODERS?: import("@whook/whook").WhookEncoders;
|
|
9
|
+
ENCODERS?: import("@whook/whook").WhookDecoders;
|
|
10
|
+
queryParserBuilder: import("@whook/whook").WhookQueryParserBuilderService;
|
|
11
|
+
log?: import("common-services").LogService;
|
|
12
|
+
schemaValidators: import("@whook/whook").WhookSchemaValidatorsService;
|
|
13
|
+
httpTransaction: import("@whook/whook").WhookHTTPTransactionService;
|
|
14
|
+
errorHandler: import("@whook/whook").WhookErrorHandler;
|
|
15
|
+
} & import("../wrappers/wrapHTTPRouterWithMCPServer.js").WhookMCPServerConfig & {
|
|
16
|
+
mcpHandler: import("./mcpHandler.js").WhookMCPHandlerService;
|
|
17
|
+
authentication: import("@whook/authorization").WhookAuthenticationService<import("../wrappers/wrapHTTPRouterWithMCPServer.js").BearerPayload>;
|
|
18
|
+
log?: import("common-services").LogService;
|
|
19
|
+
}, import("@whook/whook").WhookHTTPRouterService>;
|
|
20
|
+
export default _default;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { initHTTPRouter } from '@whook/whook';
|
|
2
|
+
import { location } from 'knifecycle';
|
|
3
|
+
import wrapHTTPRouterWithMCPServer from '../wrappers/wrapHTTPRouterWithMCPServer.js';
|
|
4
|
+
export default location(wrapHTTPRouterWithMCPServer(initHTTPRouter), import.meta.url);
|
|
5
|
+
//# sourceMappingURL=httpRouter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"httpRouter.js","sourceRoot":"","sources":["../../src/services/httpRouter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,OAAO,2BAA2B,MAAM,4CAA4C,CAAC;AAErF,eAAe,QAAQ,CACrB,2BAA2B,CAAC,cAAc,CAAC,EAC3C,MAAM,CAAC,IAAI,CAAC,GAAG,CAChB,CAAC"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { type WhookDefinitions, type WhookOpenAPI, type WhookRoutesHandlersService, type WhookSchemaValidatorsService } from '@whook/whook';
|
|
2
|
+
import { createMcpHandler } from '@modelcontextprotocol/server';
|
|
3
|
+
import { type LogService } from 'common-services';
|
|
4
|
+
export type WhookMCPHandlerService = ReturnType<typeof createMcpHandler>;
|
|
5
|
+
export interface WhookMCPRouteConfig {
|
|
6
|
+
allowMCP?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export interface WhookMCPOptions {
|
|
9
|
+
/** MCP server advertised metadata */
|
|
10
|
+
metadata: {
|
|
11
|
+
name: string;
|
|
12
|
+
version: `${string}.${string}.${string}`;
|
|
13
|
+
websiteUrl?: string;
|
|
14
|
+
title?: string;
|
|
15
|
+
description?: string;
|
|
16
|
+
icons?: {
|
|
17
|
+
src: string;
|
|
18
|
+
mimeType?: string;
|
|
19
|
+
sizes?: string[];
|
|
20
|
+
theme?: 'light' | 'dark';
|
|
21
|
+
}[];
|
|
22
|
+
};
|
|
23
|
+
/** The MCP server path */
|
|
24
|
+
path: `/${string}`;
|
|
25
|
+
/** Allows to prebuild definitions at startup (default: false) */
|
|
26
|
+
prebuildTools: boolean;
|
|
27
|
+
/** Security requirements keys to check
|
|
28
|
+
* (leave the array empty to provide anonymous routes only)
|
|
29
|
+
*/
|
|
30
|
+
securityKeys: string[];
|
|
31
|
+
/** Rules to choose operation to provide
|
|
32
|
+
* (all routes are provided per default)
|
|
33
|
+
*/
|
|
34
|
+
rules?: {
|
|
35
|
+
/** Use a static list of operations to allow/block */
|
|
36
|
+
mode: 'allowlist' | 'blocklist';
|
|
37
|
+
operationsIds: string[];
|
|
38
|
+
}
|
|
39
|
+
/** Use routes configuration */
|
|
40
|
+
| {
|
|
41
|
+
mode: 'config';
|
|
42
|
+
default: 'allow' | 'block';
|
|
43
|
+
} | {
|
|
44
|
+
/** Use a function to filter operations to allow */
|
|
45
|
+
mode: 'filter';
|
|
46
|
+
operationIsAllowed: (operationId: string) => boolean;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
export interface WhookMCPHandlerConfig {
|
|
50
|
+
MCP_OPTIONS: WhookMCPOptions;
|
|
51
|
+
}
|
|
52
|
+
export type WhookMCPHandlerDependencies = WhookMCPHandlerConfig & {
|
|
53
|
+
API: WhookOpenAPI;
|
|
54
|
+
ROUTES_HANDLERS: WhookRoutesHandlersService;
|
|
55
|
+
DEFINITIONS: WhookDefinitions;
|
|
56
|
+
schemaValidators: WhookSchemaValidatorsService;
|
|
57
|
+
log: LogService;
|
|
58
|
+
};
|
|
59
|
+
declare const _default: import("knifecycle").ProviderInitializer<WhookMCPHandlerDependencies, import("@modelcontextprotocol/server").McpHttpHandler>;
|
|
60
|
+
export default _default;
|
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
import { Server, createMcpHandler, } from '@modelcontextprotocol/server';
|
|
2
|
+
import { autoProvider, name } from 'knifecycle';
|
|
3
|
+
import { Readable } from 'node:stream';
|
|
4
|
+
import { ensureResolvedObject, } from 'ya-open-api-types';
|
|
5
|
+
import { printStackTrace } from 'yerror';
|
|
6
|
+
const PARAMETERS_LOCATION = {
|
|
7
|
+
cookie: 'cookies',
|
|
8
|
+
header: 'headers',
|
|
9
|
+
path: 'path',
|
|
10
|
+
query: 'query',
|
|
11
|
+
querystring: 'query',
|
|
12
|
+
};
|
|
13
|
+
async function initWhookMCPHandler({ API, ROUTES_HANDLERS, DEFINITIONS, schemaValidators, MCP_OPTIONS, log, }) {
|
|
14
|
+
log('warning', `💻 - Starting the MCPServer service.`);
|
|
15
|
+
const toolsCache = {};
|
|
16
|
+
if (MCP_OPTIONS.prebuildTools) {
|
|
17
|
+
log('debug', `🔧 - Pre-building tools!`);
|
|
18
|
+
for (const [operationId, config] of Object.entries(DEFINITIONS.configs)) {
|
|
19
|
+
if (config.type !== 'route' || config.method === 'options') {
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
const operation = config.operation;
|
|
23
|
+
if (!operation) {
|
|
24
|
+
log('debug', `🤷 - Filtering undefined "${operationId}" operation!`);
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
await buildTool(operation, config.method, config.config);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
async function buildTool(operation, method, config) {
|
|
31
|
+
const operationId = operation.operationId;
|
|
32
|
+
if (toolsCache[operationId]) {
|
|
33
|
+
return toolsCache[operationId];
|
|
34
|
+
}
|
|
35
|
+
log('debug', `🔧 - Building tool for ${operationId}!`);
|
|
36
|
+
if (MCP_OPTIONS.rules &&
|
|
37
|
+
((MCP_OPTIONS.rules.mode === 'allowlist' &&
|
|
38
|
+
!MCP_OPTIONS.rules.operationsIds.includes(operationId)) ||
|
|
39
|
+
(MCP_OPTIONS.rules.mode === 'blocklist' &&
|
|
40
|
+
MCP_OPTIONS.rules.operationsIds.includes(operationId)) ||
|
|
41
|
+
(MCP_OPTIONS.rules.mode === 'config' &&
|
|
42
|
+
(config?.allowMCP === false ||
|
|
43
|
+
(config?.allowMCP !== true &&
|
|
44
|
+
MCP_OPTIONS.rules.default === 'block'))) ||
|
|
45
|
+
(MCP_OPTIONS.rules.mode === 'filter' &&
|
|
46
|
+
!MCP_OPTIONS.rules.operationIsAllowed(operationId)))) {
|
|
47
|
+
log('debug', `🤷 - Filtering "${operationId}" operation (mode: "${MCP_OPTIONS.rules?.mode}")!`);
|
|
48
|
+
toolsCache[operationId] = 'skipped';
|
|
49
|
+
return toolsCache[operationId];
|
|
50
|
+
}
|
|
51
|
+
const responsesSchemas = [];
|
|
52
|
+
for (const statusCode of Object.keys(operation.responses || {})) {
|
|
53
|
+
const maybeResponse = operation.responses?.[statusCode];
|
|
54
|
+
const response = await ensureResolvedObject(API, maybeResponse);
|
|
55
|
+
if (!response || !('content' in response) || !response.content) {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
const schema = response.content?.['application/json']?.schema || {
|
|
59
|
+
type: 'string',
|
|
60
|
+
format: 'binary',
|
|
61
|
+
};
|
|
62
|
+
responsesSchemas.push({
|
|
63
|
+
type: 'object',
|
|
64
|
+
required: ['status', 'body'],
|
|
65
|
+
properties: {
|
|
66
|
+
status: statusCode === 'default'
|
|
67
|
+
? { type: 'number' }
|
|
68
|
+
: { const: parseInt(statusCode, 10) },
|
|
69
|
+
body: schema,
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
const parameters = await Promise.all((operation.parameters || []).map((parameter) => ensureResolvedObject(API, parameter)));
|
|
74
|
+
const requestBody = operation.requestBody
|
|
75
|
+
? await ensureResolvedObject(API, operation.requestBody)
|
|
76
|
+
: undefined;
|
|
77
|
+
const bodySchema = requestBody
|
|
78
|
+
? requestBody.content?.['application/json']?.schema || {
|
|
79
|
+
type: 'string',
|
|
80
|
+
format: 'binary',
|
|
81
|
+
}
|
|
82
|
+
: undefined;
|
|
83
|
+
const tool = {
|
|
84
|
+
name: operationId,
|
|
85
|
+
description: operation.summary || '',
|
|
86
|
+
annotations: {
|
|
87
|
+
readOnlyHint: !['put', 'post', 'patch', 'delete'].includes(method),
|
|
88
|
+
idempotentHint: [
|
|
89
|
+
'options',
|
|
90
|
+
'head',
|
|
91
|
+
'get',
|
|
92
|
+
'query',
|
|
93
|
+
'put',
|
|
94
|
+
'delete',
|
|
95
|
+
].includes(method),
|
|
96
|
+
destructiveHint: ['put', 'post', 'patch', 'delete'].includes(method),
|
|
97
|
+
openWorldHint: true,
|
|
98
|
+
},
|
|
99
|
+
inputSchema: {
|
|
100
|
+
type: 'object',
|
|
101
|
+
required: [
|
|
102
|
+
...new Set(parameters
|
|
103
|
+
.filter((parameter) => parameter.required)
|
|
104
|
+
.map((parameter) => PARAMETERS_LOCATION[parameter.in])),
|
|
105
|
+
...(requestBody?.required ? ['body'] : []),
|
|
106
|
+
],
|
|
107
|
+
properties: parameters.reduce((properties, parameter) => ({
|
|
108
|
+
...properties,
|
|
109
|
+
[PARAMETERS_LOCATION[parameter.in]]: parameter.in === 'querystring'
|
|
110
|
+
? 'schema' in parameter
|
|
111
|
+
? parameter.schema
|
|
112
|
+
: true
|
|
113
|
+
: {
|
|
114
|
+
type: 'object',
|
|
115
|
+
required: [
|
|
116
|
+
...new Set((properties[parameter.in]
|
|
117
|
+
?.required || []).concat(parameter.required ? [parameter.name] : [])),
|
|
118
|
+
],
|
|
119
|
+
properties: {
|
|
120
|
+
...(properties[parameter.in]
|
|
121
|
+
?.properties || {}),
|
|
122
|
+
[parameter.name]: 'schema' in parameter
|
|
123
|
+
? {
|
|
124
|
+
description: parameter.description,
|
|
125
|
+
...parameter.schema,
|
|
126
|
+
}
|
|
127
|
+
: true,
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
}), (bodySchema
|
|
131
|
+
? {
|
|
132
|
+
body: bodySchema,
|
|
133
|
+
}
|
|
134
|
+
: {})),
|
|
135
|
+
},
|
|
136
|
+
outputSchema: responsesSchemas.length === 1
|
|
137
|
+
? responsesSchemas[0]
|
|
138
|
+
: responsesSchemas.length
|
|
139
|
+
? { oneOf: responsesSchemas }
|
|
140
|
+
: undefined,
|
|
141
|
+
};
|
|
142
|
+
toolsCache[operation.operationId] = tool;
|
|
143
|
+
return tool;
|
|
144
|
+
}
|
|
145
|
+
function checkSecurityScopes(scopes, security) {
|
|
146
|
+
if (!security || Object.keys(security).length === 0) {
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
for (const securityKey of MCP_OPTIONS.securityKeys || []) {
|
|
150
|
+
for (const requirement of security) {
|
|
151
|
+
if (Object.keys(requirement).length === 1 &&
|
|
152
|
+
requirement[securityKey] &&
|
|
153
|
+
requirement[securityKey].every((scope) => scopes.includes(scope))) {
|
|
154
|
+
return true;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
const handler = createMcpHandler(({ requestInfo, authInfo }) => {
|
|
161
|
+
const server = new Server(MCP_OPTIONS.metadata, {
|
|
162
|
+
capabilities: { tools: {} },
|
|
163
|
+
});
|
|
164
|
+
const authenticationData = authInfo;
|
|
165
|
+
log('debug', `🙋 - MCP authentication: `, JSON.stringify(authenticationData) || 'anonymous');
|
|
166
|
+
server.setRequestHandler('tools/list', async () => {
|
|
167
|
+
try {
|
|
168
|
+
log('debug', '➕ - Listing tools!');
|
|
169
|
+
const scopes = authenticationData?.scopes || [];
|
|
170
|
+
const tools = [];
|
|
171
|
+
for (const [operationId, config] of Object.entries(DEFINITIONS.configs)) {
|
|
172
|
+
if (config.type !== 'route' || config.method === 'options') {
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
const operation = config.operation;
|
|
176
|
+
if (!operation) {
|
|
177
|
+
log('debug', `🤷 - Filtering undefined "${operationId}" operation!`);
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
if (!checkSecurityScopes(scopes, operation.security ?? API.security)) {
|
|
181
|
+
log('debug', `🤷 - Not supposed to run "${operationId}" with scopes "${scopes.join(' ')}"!`);
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
const tool = await buildTool(operation, config.method, config.config);
|
|
185
|
+
if (tool !== 'skipped') {
|
|
186
|
+
tools.push(tool);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
log('debug', `➕ - Found ${tools.length} tools!`);
|
|
190
|
+
return {
|
|
191
|
+
tools,
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
catch (err) {
|
|
195
|
+
log('error', `❌ - Failed to list MCP tools!`);
|
|
196
|
+
log('error-stack', printStackTrace(err));
|
|
197
|
+
return {
|
|
198
|
+
tools: [],
|
|
199
|
+
isError: true,
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
server.setRequestHandler('tools/call', async (request) => {
|
|
204
|
+
log('debug', `➕ - Running call ${request.params.name}!`);
|
|
205
|
+
const handler = ROUTES_HANDLERS[request.params.name];
|
|
206
|
+
const definition = DEFINITIONS.configs[request.params.name];
|
|
207
|
+
const tool = definition?.type === 'route' && definition.operation
|
|
208
|
+
? await buildTool(definition.operation, definition.method, definition.config)
|
|
209
|
+
: undefined;
|
|
210
|
+
if (!handler ||
|
|
211
|
+
!definition ||
|
|
212
|
+
definition.type !== 'route' ||
|
|
213
|
+
!tool ||
|
|
214
|
+
tool === 'skipped') {
|
|
215
|
+
return {
|
|
216
|
+
content: [
|
|
217
|
+
{ type: 'text', text: `Unknown tool: ${request.params.name}` },
|
|
218
|
+
],
|
|
219
|
+
isError: true,
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
if (!checkSecurityScopes(authInfo?.scopes || [], definition?.operation?.security ?? API.security)) {
|
|
223
|
+
return {
|
|
224
|
+
content: [
|
|
225
|
+
{ type: 'text', text: `Unauthorized tool: ${request.params.name}` },
|
|
226
|
+
],
|
|
227
|
+
isError: true,
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
const parameters = {
|
|
231
|
+
path: {},
|
|
232
|
+
query: {},
|
|
233
|
+
cookies: {},
|
|
234
|
+
headers: {},
|
|
235
|
+
...request.params.arguments,
|
|
236
|
+
};
|
|
237
|
+
log('debug', `➕ - Parameters "${JSON.stringify(parameters)}"`);
|
|
238
|
+
const validate = schemaValidators(tool.inputSchema);
|
|
239
|
+
validate(parameters);
|
|
240
|
+
if (validate.errors) {
|
|
241
|
+
return {
|
|
242
|
+
content: [
|
|
243
|
+
{
|
|
244
|
+
type: 'text',
|
|
245
|
+
text: `Bad parameters: ${JSON.stringify(validate.errors)}`,
|
|
246
|
+
structuredContent: validate.errors,
|
|
247
|
+
},
|
|
248
|
+
],
|
|
249
|
+
isError: true,
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
try {
|
|
253
|
+
const response = await handler({
|
|
254
|
+
...parameters,
|
|
255
|
+
headers: {
|
|
256
|
+
authorization: requestInfo?.headers.get('authorization'),
|
|
257
|
+
...parameters.headers,
|
|
258
|
+
},
|
|
259
|
+
}, definition);
|
|
260
|
+
log('debug', `➕ - Response "${JSON.stringify(response)}"!`);
|
|
261
|
+
if (response.body instanceof Readable ||
|
|
262
|
+
response.body instanceof Buffer) {
|
|
263
|
+
if (typeof response.headers?.['content-type'] === 'string') {
|
|
264
|
+
const isImage = response.headers['content-type'].startsWith('image/');
|
|
265
|
+
const isAudio = response.headers['content-type'].startsWith('audio/');
|
|
266
|
+
return {
|
|
267
|
+
content: [
|
|
268
|
+
isImage || isAudio
|
|
269
|
+
? {
|
|
270
|
+
type: isImage ? 'image' : 'audio',
|
|
271
|
+
mimeType: response.headers['content-type'],
|
|
272
|
+
data: Buffer.from(response.body instanceof Readable
|
|
273
|
+
? await response.body.toArray()
|
|
274
|
+
: response.body).toString('base64'),
|
|
275
|
+
}
|
|
276
|
+
: {
|
|
277
|
+
type: 'resource',
|
|
278
|
+
resource: {
|
|
279
|
+
uri: requestInfo?.url,
|
|
280
|
+
mimeType: response.headers['content-type'],
|
|
281
|
+
blob: Buffer.from(response.body instanceof Readable
|
|
282
|
+
? await response.body.toArray()
|
|
283
|
+
: response.body).toString('base64url'),
|
|
284
|
+
},
|
|
285
|
+
},
|
|
286
|
+
],
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
else {
|
|
290
|
+
return {
|
|
291
|
+
content: [
|
|
292
|
+
{
|
|
293
|
+
type: 'text',
|
|
294
|
+
text: 'Cannot encode resource.',
|
|
295
|
+
},
|
|
296
|
+
],
|
|
297
|
+
isError: true,
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
return {
|
|
302
|
+
content: [
|
|
303
|
+
{
|
|
304
|
+
type: 'text',
|
|
305
|
+
text: JSON.stringify(response),
|
|
306
|
+
structuredContent: response,
|
|
307
|
+
},
|
|
308
|
+
],
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
catch (err) {
|
|
312
|
+
log('error-stack', printStackTrace(err));
|
|
313
|
+
return {
|
|
314
|
+
content: [
|
|
315
|
+
{
|
|
316
|
+
type: 'text',
|
|
317
|
+
text: `Unexpected error: ${err.message}`,
|
|
318
|
+
},
|
|
319
|
+
],
|
|
320
|
+
isError: true,
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
});
|
|
324
|
+
return server;
|
|
325
|
+
});
|
|
326
|
+
return {
|
|
327
|
+
service: handler,
|
|
328
|
+
dispose: async () => {
|
|
329
|
+
await handler.close();
|
|
330
|
+
},
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
export default name('mcpHandler', autoProvider(initWhookMCPHandler));
|
|
334
|
+
//# sourceMappingURL=mcpHandler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mcpHandler.js","sourceRoot":"","sources":["../../src/services/mcpHandler.ts"],"names":[],"mappings":"AAcA,OAAO,EACL,MAAM,EAEN,gBAAgB,GACjB,MAAM,8BAA8B,CAAC;AAEtC,OAAO,EAAiB,YAAY,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAC/D,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAMvC,OAAO,EAKL,oBAAoB,GACrB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAU,eAAe,EAAE,MAAM,QAAQ,CAAC;AAEjD,MAAM,mBAAmB,GAGrB;IACF,MAAM,EAAE,SAAS;IACjB,MAAM,EAAE,SAAS;IACjB,IAAI,EAAE,MAAM;IACZ,KAAK,EAAE,OAAO;IACd,WAAW,EAAE,OAAO;CACrB,CAAC;AA6DF,KAAK,UAAU,mBAAmB,CAAC,EACjC,GAAG,EACH,eAAe,EACf,WAAW,EACX,gBAAgB,EAChB,WAAW,EACX,GAAG,GACyB;IAC5B,GAAG,CAAC,SAAS,EAAE,sCAAsC,CAAC,CAAC;IAEvD,MAAM,UAAU,GAAqC,EAAE,CAAC;IAExD,IAAI,WAAW,CAAC,aAAa,EAAE,CAAC;QAC9B,GAAG,CAAC,OAAO,EAAE,0BAA0B,CAAC,CAAC;QACzC,KAAK,MAAM,CAAC,WAAW,EAAE,MAAM,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,WAAW,CAAC,OAAO,CAAC,EAAE,CAAC;YACxE,IAAI,MAAM,CAAC,IAAI,KAAK,OAAO,IAAI,MAAM,CAAC,MA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CAAC;yBAAM,CAAC;wBACN,OAAO;4BACL,OAAO,EAAE;gCACP;oCACE,IAAI,EAAE,MAAM;oCACZ,IAAI,EAAE,yBAAyB;iCAChC;6BACF;4BACD,OAAO,EAAE,IAAI;yBACd,CAAC;oBACJ,CAAC;gBACH,CAAC;gBAED,OAAO;oBACL,OAAO,EAAE;wBACP;4BACE,IAAI,EAAE,MAAM;4BACZ,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC;4BAC9B,iBAAiB,EAAE,QAAQ;yBAC5B;qBACF;iBACF,CAAC;YACJ,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACb,GAAG,CAAC,aAAa,EAAE,eAAe,CAAC,GAAG,CAAC,CAAC,CAAC;gBACzC,OAAO;oBACL,OAAO,EAAE;wBACP;4BACE,IAAI,EAAE,MAAM;4BACZ,IAAI,EAAE,qBAAsB,GAAc,CAAC,OAAO,EAAE;yBACrD;qBACF;oBACD,OAAO,EAAE,IAAI;iBACd,CAAC;YACJ,CAAC;QACH,CAAC,CAAC,CAAC;QAEH,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC,CAAC;IAEH,OAAO;QACL,OAAO,EAAE,OAAO;QAChB,OAAO,EAAE,KAAK,IAAI,EAAE;YAClB,MAAM,OAAO,CAAC,KAAK,EAAE,CAAC;QACxB,CAAC;KACF,CAAC;AACJ,CAAC;AAED,eAAe,IAAI,CAAC,YAAY,EAAE,YAAY,CAAC,mBAAmB,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type WhookAuthenticationService } from '@whook/authorization';
|
|
2
|
+
import { type WhookHTTPRouterService } from '@whook/whook';
|
|
3
|
+
import { type LogService } from 'common-services';
|
|
4
|
+
import { type Dependencies, type ProviderInitializer } from 'knifecycle';
|
|
5
|
+
import { type WhookMCPOptions, type WhookMCPHandlerService } from '../services/mcpHandler.js';
|
|
6
|
+
export interface BearerPayload {
|
|
7
|
+
hash: string;
|
|
8
|
+
}
|
|
9
|
+
export interface WhookMCPServerConfig {
|
|
10
|
+
MCP_OPTIONS: WhookMCPOptions;
|
|
11
|
+
}
|
|
12
|
+
export type WhookMCPServerDependencies = WhookMCPServerConfig & {
|
|
13
|
+
mcpHandler: WhookMCPHandlerService;
|
|
14
|
+
authentication: WhookAuthenticationService<BearerPayload>;
|
|
15
|
+
log?: LogService;
|
|
16
|
+
};
|
|
17
|
+
export default function wrapHTTPRouterWithMCPServer<D extends Dependencies>(initHTTPRouter: ProviderInitializer<D, WhookHTTPRouterService>): ProviderInitializer<D & WhookMCPServerDependencies, WhookHTTPRouterService>;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { castWhookHeaders, pickFirstHeaderValue, } from '@whook/whook';
|
|
2
|
+
import { toNodeHandler } from '@modelcontextprotocol/node';
|
|
3
|
+
import { noop } from 'common-services';
|
|
4
|
+
import { alsoInject, wrapInitializer } from 'knifecycle';
|
|
5
|
+
import { hasYErrorCode, printStackTrace } from 'yerror';
|
|
6
|
+
import { YHTTPError } from 'yhttperror';
|
|
7
|
+
export default function wrapHTTPRouterWithMCPServer(initHTTPRouter) {
|
|
8
|
+
const augmentedInitializer = alsoInject(['MCP_OPTIONS', 'mcpHandler', 'authentication', '?log'], initHTTPRouter);
|
|
9
|
+
return wrapInitializer(async ({ MCP_OPTIONS, mcpHandler, authentication, log = noop, }, httpRouter) => {
|
|
10
|
+
log('warning', `💁 - Serving API through MCP (path: ${MCP_OPTIONS.path})`);
|
|
11
|
+
const nodeHandler = toNodeHandler(mcpHandler);
|
|
12
|
+
const checkAuthentication = async (req) => {
|
|
13
|
+
const authorization = pickFirstHeaderValue('authorization', castWhookHeaders(req.headers));
|
|
14
|
+
if (!authorization) {
|
|
15
|
+
throw new YHTTPError(401, 'E_UNAUTHORIZED');
|
|
16
|
+
}
|
|
17
|
+
const [type, data] = (authorization || '').split(/\s+/, 2);
|
|
18
|
+
if (type.toLowerCase() !== 'bearer' || !data) {
|
|
19
|
+
throw new YHTTPError(401, 'E_UNAUTHORIZED', [type]);
|
|
20
|
+
}
|
|
21
|
+
const authenticationData = await authentication.check('bearer', {
|
|
22
|
+
hash: data,
|
|
23
|
+
});
|
|
24
|
+
log('debug', '➕ - MCP request authenticated');
|
|
25
|
+
return authenticationData;
|
|
26
|
+
};
|
|
27
|
+
async function customHTTPRouter(req, res) {
|
|
28
|
+
const url = req.url?.split('?', 1)[0];
|
|
29
|
+
if (url &&
|
|
30
|
+
(url === MCP_OPTIONS.path || url.startsWith(MCP_OPTIONS.path + '/'))) {
|
|
31
|
+
try {
|
|
32
|
+
if (MCP_OPTIONS.securityKeys.length) {
|
|
33
|
+
req.auth =
|
|
34
|
+
await checkAuthentication(req);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
log('warning', '⚠️ - MCP server is publicly available.');
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
catch (err) {
|
|
41
|
+
const castedErr = hasYErrorCode(err, 'E_UNAUTHORIZED');
|
|
42
|
+
if (castedErr) {
|
|
43
|
+
// TODO: Eventually allow the MCP server for
|
|
44
|
+
// anonymous requests later. The main problem
|
|
45
|
+
// is that the MCP protocol errors seems to
|
|
46
|
+
// not allow to generate a 401/403 inside the
|
|
47
|
+
// MCP handler. A MCP server is then either
|
|
48
|
+
// protected or anonymous AFAIK
|
|
49
|
+
log('debug', '➕ - Authentication required');
|
|
50
|
+
log('debug-stack', printStackTrace(err));
|
|
51
|
+
res.statusCode = 401;
|
|
52
|
+
res.setHeader('WWW-Authenticate', 'Bearer');
|
|
53
|
+
res.end();
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
log('error', '💥 - Unexpected MCP error!');
|
|
57
|
+
log('error-stack', printStackTrace(err));
|
|
58
|
+
res.statusCode = 500;
|
|
59
|
+
res.end();
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
return nodeHandler(req, res);
|
|
63
|
+
}
|
|
64
|
+
return httpRouter.service(req, res);
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
...httpRouter,
|
|
68
|
+
service: customHTTPRouter,
|
|
69
|
+
};
|
|
70
|
+
}, augmentedInitializer);
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=wrapHTTPRouterWithMCPServer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wrapHTTPRouterWithMCPServer.js","sourceRoot":"","sources":["../../src/wrappers/wrapHTTPRouterWithMCPServer.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,gBAAgB,EAChB,oBAAoB,GACrB,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAC3D,OAAO,EAAmB,IAAI,EAAE,MAAM,iBAAiB,CAAC;AACxD,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAGzD,OAAO,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,QAAQ,CAAC;AACxD,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAmBxC,MAAM,CAAC,OAAO,UAAU,2BAA2B,CACjD,cAA8D;IAE9D,MAAM,oBAAoB,GAAG,UAAU,CAIrC,CAAC,aAAa,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,CAAC,EAAE,cAAc,CAAC,CAAC;IAE3E,OAAO,eAAe,CACpB,KAAK,EACH,EACE,WAAW,EACX,UAAU,EACV,cAAc,EACd,GAAG,GAAG,IAAI,GACiB,EAC7B,UAAmC,EACnC,EAAE;QACF,GAAG,CACD,SAAS,EACT,uCAAuC,WAAW,CAAC,IAAI,GAAG,CAC3D,CAAC;QACF,MAAM,WAAW,GAAG,aAAa,CAAC,UAAU,CAAC,CAAC;QAE9C,MAAM,mBAAmB,GAAG,KAAK,EAAE,GAAoB,EAAE,EAAE;YACzD,MAAM,aAAa,GAAG,oBAAoB,CACxC,eAAe,EACf,gBAAgB,CAAC,GAAG,CAAC,OAAO,CAAC,CAC9B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