@backstage/plugin-mcp-actions-backend 0.1.9 → 0.1.10-next.1
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/CHANGELOG.md +28 -0
- package/README.md +8 -1
- package/dist/metrics.cjs.js +21 -0
- package/dist/metrics.cjs.js.map +1 -0
- package/dist/plugin.cjs.js +8 -4
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/routers/createStreamableRouter.cjs.js +27 -1
- package/dist/routers/createStreamableRouter.cjs.js.map +1 -1
- package/dist/services/McpService.cjs.js +93 -44
- package/dist/services/McpService.cjs.js.map +1 -1
- package/dist/services/handleErrors.cjs.js +1 -1
- package/dist/services/handleErrors.cjs.js.map +1 -1
- package/package.json +9 -9
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,33 @@
|
|
|
1
1
|
# @backstage/plugin-mcp-actions-backend
|
|
2
2
|
|
|
3
|
+
## 0.1.10-next.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 62f0a53: Fixed error forwarding in the actions registry so that known errors like `InputError` and `NotFoundError` thrown by actions preserve their original status codes and messages instead of being wrapped in `ForwardedError` and coerced to 500.
|
|
8
|
+
- Updated dependencies
|
|
9
|
+
- @backstage/catalog-client@1.14.0-next.1
|
|
10
|
+
- @backstage/plugin-catalog-node@2.1.0-next.1
|
|
11
|
+
- @backstage/backend-plugin-api@1.7.1-next.0
|
|
12
|
+
- @backstage/errors@1.2.7
|
|
13
|
+
- @backstage/types@1.2.2
|
|
14
|
+
|
|
15
|
+
## 0.1.10-next.0
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- dc81af1: Adds two new metrics to track MCP server operations and sessions.
|
|
20
|
+
|
|
21
|
+
- `mcp.server.operation.duration`: The duration taken to process an individual MCP operation
|
|
22
|
+
- `mcp.server.session.duration`: The duration of the MCP session from the perspective of the server
|
|
23
|
+
|
|
24
|
+
- Updated dependencies
|
|
25
|
+
- @backstage/plugin-catalog-node@2.1.0-next.0
|
|
26
|
+
- @backstage/backend-plugin-api@1.7.1-next.0
|
|
27
|
+
- @backstage/catalog-client@1.13.1-next.0
|
|
28
|
+
- @backstage/errors@1.2.7
|
|
29
|
+
- @backstage/types@1.2.2
|
|
30
|
+
|
|
3
31
|
## 0.1.9
|
|
4
32
|
|
|
5
33
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -75,7 +75,7 @@ export const myPlugin = createBackendPlugin({
|
|
|
75
75
|
|
|
76
76
|
When errors are thrown from MCP actions, the backend will handle and surface error message for any error from `@backstage/errors`. Unknown errors will be handled by `@modelcontextprotocol/sdk`'s default error handling, which may result in a generic `500 Server Error` being returned. As a result, we recommend using errors from `@backstage/errors` when applicable.
|
|
77
77
|
|
|
78
|
-
See https://backstage.io/
|
|
78
|
+
See [Backstage Errors](https://backstage.io/docs/reference/errors/) for a full list of supported errors.
|
|
79
79
|
|
|
80
80
|
When writing MCP tools, use the appropriate error from `@backstage/errors` when applicable:
|
|
81
81
|
|
|
@@ -178,6 +178,13 @@ There's a few different ways to configure MCP tools, but here's a snippet of the
|
|
|
178
178
|
}
|
|
179
179
|
```
|
|
180
180
|
|
|
181
|
+
## Metrics
|
|
182
|
+
|
|
183
|
+
The MCP Actions Backend emits metrics for the following operations:
|
|
184
|
+
|
|
185
|
+
- `mcp.server.operation.duration`: The duration taken to process an individual MCP operation
|
|
186
|
+
- `mcp.server.session.duration`: The duration of the MCP session from the perspective of the server
|
|
187
|
+
|
|
181
188
|
## Development
|
|
182
189
|
|
|
183
190
|
This plugin backend can be started in a standalone mode from directly in this package with `yarn start`. It is a limited setup that is most convenient when developing the plugin backend itself.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const bucketBoundaries = [
|
|
4
|
+
0.01,
|
|
5
|
+
0.02,
|
|
6
|
+
0.05,
|
|
7
|
+
0.1,
|
|
8
|
+
0.2,
|
|
9
|
+
0.5,
|
|
10
|
+
1,
|
|
11
|
+
2,
|
|
12
|
+
5,
|
|
13
|
+
10,
|
|
14
|
+
30,
|
|
15
|
+
60,
|
|
16
|
+
120,
|
|
17
|
+
300
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
exports.bucketBoundaries = bucketBoundaries;
|
|
21
|
+
//# sourceMappingURL=metrics.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"metrics.cjs.js","sources":["../src/metrics.ts"],"sourcesContent":["/*\n * Copyright 2026 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { MetricAttributes } from '@backstage/backend-plugin-api/alpha';\n\n/**\n * Attributes for mcp.server.operation.duration\n * Following OTel requirement levels from the spec\n *\n * @see https://opentelemetry.io/docs/specs/semconv/gen-ai/mcp/#metric-mcpserveroperationduration\n */\nexport interface McpServerOperationAttributes extends MetricAttributes {\n // Required\n 'mcp.method.name': string;\n\n // Conditionally Required\n 'error.type'?: string;\n 'gen_ai.tool.name'?: string;\n 'gen_ai.prompt.name'?: string;\n 'mcp.resource.uri'?: string;\n 'rpc.response.status_code'?: string;\n\n // Recommended\n 'gen_ai.operation.name'?: 'execute_tool';\n 'mcp.protocol.version'?: string;\n 'mcp.session.id'?: string;\n 'network.transport'?: 'tcp' | 'quic' | 'pipe' | 'unix';\n 'network.protocol.name'?: string;\n 'network.protocol.version'?: string;\n}\n\n/**\n * Attributes for mcp.server.session.duration\n * Following OTel requirement levels from the spec\n *\n * @see https://opentelemetry.io/docs/specs/semconv/gen-ai/mcp/#metric-mcpserversessionduration\n */\nexport interface McpServerSessionAttributes extends MetricAttributes {\n // Conditionally Required\n 'error.type'?: string;\n\n // Recommended\n 'mcp.protocol.version'?: string;\n 'network.transport'?: 'tcp' | 'quic' | 'pipe' | 'unix';\n 'network.protocol.name'?: string;\n 'network.protocol.version'?: string;\n}\n\n/**\n * OTel recommended bucket boundaries for MCP metrics\n *\n * @remarks\n *\n * Based on the MCP metrics defined in the OTel semantic conventions v1.39.0\n * @see https://opentelemetry.io/docs/specs/semconv/gen-ai/mcp/\n *\n */\nexport const bucketBoundaries = [\n 0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2, 5, 10, 30, 60, 120, 300,\n];\n"],"names":[],"mappings":";;AAsEO,MAAM,gBAAA,GAAmB;AAAA,EAC9B,IAAA;AAAA,EAAM,IAAA;AAAA,EAAM,IAAA;AAAA,EAAM,GAAA;AAAA,EAAK,GAAA;AAAA,EAAK,GAAA;AAAA,EAAK,CAAA;AAAA,EAAG,CAAA;AAAA,EAAG,CAAA;AAAA,EAAG,EAAA;AAAA,EAAI,EAAA;AAAA,EAAI,EAAA;AAAA,EAAI,GAAA;AAAA,EAAK;AAC7D;;;;"}
|
package/dist/plugin.cjs.js
CHANGED
|
@@ -25,7 +25,8 @@ const mcpPlugin = backendPluginApi.createBackendPlugin({
|
|
|
25
25
|
registry: alpha.actionsRegistryServiceRef,
|
|
26
26
|
rootRouter: backendPluginApi.coreServices.rootHttpRouter,
|
|
27
27
|
discovery: backendPluginApi.coreServices.discovery,
|
|
28
|
-
config: backendPluginApi.coreServices.rootConfig
|
|
28
|
+
config: backendPluginApi.coreServices.rootConfig,
|
|
29
|
+
metrics: alpha.metricsServiceRef
|
|
29
30
|
},
|
|
30
31
|
async init({
|
|
31
32
|
actions,
|
|
@@ -34,10 +35,12 @@ const mcpPlugin = backendPluginApi.createBackendPlugin({
|
|
|
34
35
|
httpAuth,
|
|
35
36
|
rootRouter,
|
|
36
37
|
discovery,
|
|
37
|
-
config
|
|
38
|
+
config,
|
|
39
|
+
metrics
|
|
38
40
|
}) {
|
|
39
41
|
const mcpService = await McpService.McpService.create({
|
|
40
|
-
actions
|
|
42
|
+
actions,
|
|
43
|
+
metrics
|
|
41
44
|
});
|
|
42
45
|
const sseRouter = createSseRouter.createSseRouter({
|
|
43
46
|
mcpService,
|
|
@@ -46,7 +49,8 @@ const mcpPlugin = backendPluginApi.createBackendPlugin({
|
|
|
46
49
|
const streamableRouter = createStreamableRouter.createStreamableRouter({
|
|
47
50
|
mcpService,
|
|
48
51
|
httpAuth,
|
|
49
|
-
logger
|
|
52
|
+
logger,
|
|
53
|
+
metrics
|
|
50
54
|
});
|
|
51
55
|
const router = PromiseRouter__default.default();
|
|
52
56
|
router.use(express.json());
|
package/dist/plugin.cjs.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.cjs.js","sources":["../src/plugin.ts"],"sourcesContent":["/*\n * Copyright 2025 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport {\n coreServices,\n createBackendPlugin,\n} from '@backstage/backend-plugin-api';\nimport { json } from 'express';\nimport Router from 'express-promise-router';\nimport { McpService } from './services/McpService';\nimport { createStreamableRouter } from './routers/createStreamableRouter';\nimport { createSseRouter } from './routers/createSseRouter';\nimport {\n actionsRegistryServiceRef,\n actionsServiceRef,\n} from '@backstage/backend-plugin-api/alpha';\n\n/**\n * mcpPlugin backend plugin\n *\n * @public\n */\nexport const mcpPlugin = createBackendPlugin({\n pluginId: 'mcp-actions',\n register(env) {\n env.registerInit({\n deps: {\n logger: coreServices.logger,\n auth: coreServices.auth,\n httpAuth: coreServices.httpAuth,\n httpRouter: coreServices.httpRouter,\n actions: actionsServiceRef,\n registry: actionsRegistryServiceRef,\n rootRouter: coreServices.rootHttpRouter,\n discovery: coreServices.discovery,\n config: coreServices.rootConfig,\n },\n async init({\n actions,\n logger,\n httpRouter,\n httpAuth,\n rootRouter,\n discovery,\n config,\n }) {\n const mcpService = await McpService.create({\n actions,\n });\n\n const sseRouter = createSseRouter({\n mcpService,\n httpAuth,\n });\n\n const streamableRouter = createStreamableRouter({\n mcpService,\n httpAuth,\n logger,\n });\n\n const router = Router();\n router.use(json());\n\n router.use('/v1/sse', sseRouter);\n router.use('/v1', streamableRouter);\n\n httpRouter.use(router);\n\n const oauthEnabled =\n config.getOptionalBoolean(\n 'auth.experimentalDynamicClientRegistration.enabled',\n ) ||\n config.getOptionalBoolean(\n 'auth.experimentalClientIdMetadataDocuments.enabled',\n );\n\n if (oauthEnabled) {\n // OAuth Authorization Server Metadata (RFC 8414)\n // This should be replaced with throwing a WWW-Authenticate header, but that doesn't seem to be supported by\n // many of the MCP clients as of yet. So this seems to be the oldest version of the spec that's implemented.\n rootRouter.use(\n '/.well-known/oauth-authorization-server',\n async (_, res) => {\n const authBaseUrl = await discovery.getBaseUrl('auth');\n const oidcResponse = await fetch(\n `${authBaseUrl}/.well-known/openid-configuration`,\n );\n res.json(await oidcResponse.json());\n },\n );\n\n // Protected Resource Metadata (RFC 9728)\n // https://datatracker.ietf.org/doc/html/rfc9728\n // This allows MCP clients to discover the authorization server for this resource\n rootRouter.use(\n '/.well-known/oauth-protected-resource',\n async (_, res) => {\n const [authBaseUrl, mcpBaseUrl] = await Promise.all([\n discovery.getBaseUrl('auth'),\n discovery.getBaseUrl('mcp-actions'),\n ]);\n res.json({\n resource: mcpBaseUrl,\n authorization_servers: [authBaseUrl],\n });\n },\n );\n }\n },\n });\n },\n});\n"],"names":["createBackendPlugin","coreServices","actionsServiceRef","actionsRegistryServiceRef","McpService","createSseRouter","createStreamableRouter","Router","json"],"mappings":";;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"plugin.cjs.js","sources":["../src/plugin.ts"],"sourcesContent":["/*\n * Copyright 2025 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport {\n coreServices,\n createBackendPlugin,\n} from '@backstage/backend-plugin-api';\nimport { json } from 'express';\nimport Router from 'express-promise-router';\nimport { McpService } from './services/McpService';\nimport { createStreamableRouter } from './routers/createStreamableRouter';\nimport { createSseRouter } from './routers/createSseRouter';\nimport {\n actionsRegistryServiceRef,\n actionsServiceRef,\n metricsServiceRef,\n} from '@backstage/backend-plugin-api/alpha';\n\n/**\n * mcpPlugin backend plugin\n *\n * @public\n */\nexport const mcpPlugin = createBackendPlugin({\n pluginId: 'mcp-actions',\n register(env) {\n env.registerInit({\n deps: {\n logger: coreServices.logger,\n auth: coreServices.auth,\n httpAuth: coreServices.httpAuth,\n httpRouter: coreServices.httpRouter,\n actions: actionsServiceRef,\n registry: actionsRegistryServiceRef,\n rootRouter: coreServices.rootHttpRouter,\n discovery: coreServices.discovery,\n config: coreServices.rootConfig,\n metrics: metricsServiceRef,\n },\n async init({\n actions,\n logger,\n httpRouter,\n httpAuth,\n rootRouter,\n discovery,\n config,\n metrics,\n }) {\n const mcpService = await McpService.create({\n actions,\n metrics,\n });\n\n const sseRouter = createSseRouter({\n mcpService,\n httpAuth,\n });\n\n const streamableRouter = createStreamableRouter({\n mcpService,\n httpAuth,\n logger,\n metrics,\n });\n\n const router = Router();\n router.use(json());\n\n router.use('/v1/sse', sseRouter);\n router.use('/v1', streamableRouter);\n\n httpRouter.use(router);\n\n const oauthEnabled =\n config.getOptionalBoolean(\n 'auth.experimentalDynamicClientRegistration.enabled',\n ) ||\n config.getOptionalBoolean(\n 'auth.experimentalClientIdMetadataDocuments.enabled',\n );\n\n if (oauthEnabled) {\n // OAuth Authorization Server Metadata (RFC 8414)\n // This should be replaced with throwing a WWW-Authenticate header, but that doesn't seem to be supported by\n // many of the MCP clients as of yet. So this seems to be the oldest version of the spec that's implemented.\n rootRouter.use(\n '/.well-known/oauth-authorization-server',\n async (_, res) => {\n const authBaseUrl = await discovery.getBaseUrl('auth');\n const oidcResponse = await fetch(\n `${authBaseUrl}/.well-known/openid-configuration`,\n );\n res.json(await oidcResponse.json());\n },\n );\n\n // Protected Resource Metadata (RFC 9728)\n // https://datatracker.ietf.org/doc/html/rfc9728\n // This allows MCP clients to discover the authorization server for this resource\n rootRouter.use(\n '/.well-known/oauth-protected-resource',\n async (_, res) => {\n const [authBaseUrl, mcpBaseUrl] = await Promise.all([\n discovery.getBaseUrl('auth'),\n discovery.getBaseUrl('mcp-actions'),\n ]);\n res.json({\n resource: mcpBaseUrl,\n authorization_servers: [authBaseUrl],\n });\n },\n );\n }\n },\n });\n },\n});\n"],"names":["createBackendPlugin","coreServices","actionsServiceRef","actionsRegistryServiceRef","metricsServiceRef","McpService","createSseRouter","createStreamableRouter","Router","json"],"mappings":";;;;;;;;;;;;;;AAmCO,MAAM,YAAYA,oCAAA,CAAoB;AAAA,EAC3C,QAAA,EAAU,aAAA;AAAA,EACV,SAAS,GAAA,EAAK;AACZ,IAAA,GAAA,CAAI,YAAA,CAAa;AAAA,MACf,IAAA,EAAM;AAAA,QACJ,QAAQC,6BAAA,CAAa,MAAA;AAAA,QACrB,MAAMA,6BAAA,CAAa,IAAA;AAAA,QACnB,UAAUA,6BAAA,CAAa,QAAA;AAAA,QACvB,YAAYA,6BAAA,CAAa,UAAA;AAAA,QACzB,OAAA,EAASC,uBAAA;AAAA,QACT,QAAA,EAAUC,+BAAA;AAAA,QACV,YAAYF,6BAAA,CAAa,cAAA;AAA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A,CAAI,IAAA,CAAK,MAAM,YAAA,CAAa,IAAA,EAAM,CAAA;AAAA,YACpC;AAAA,WACF;AAKA,UAAA,UAAA,CAAW,GAAA;AAAA,YACT,uCAAA;AAAA,YACA,OAAO,GAAG,GAAA,KAAQ;AAChB,cAAA,MAAM,CAAC,WAAA,EAAa,UAAU,CAAA,GAAI,MAAM,QAAQ,GAAA,CAAI;AAAA,gBAClD,SAAA,CAAU,WAAW,MAAM,CAAA;AAAA,gBAC3B,SAAA,CAAU,WAAW,aAAa;AAAA,eACnC,CAAA;AACD,cAAA,GAAA,CAAI,IAAA,CAAK;AAAA,gBACP,QAAA,EAAU,UAAA;AAAA,gBACV,qBAAA,EAAuB,CAAC,WAAW;AAAA,eACpC,CAAA;AAAA,YACH;AAAA,WACF;AAAA,QACF;AAAA,MACF;AAAA,KACD,CAAA;AAAA,EACH;AACF,CAAC;;;;"}
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var PromiseRouter = require('express-promise-router');
|
|
4
|
+
var node_perf_hooks = require('node:perf_hooks');
|
|
4
5
|
var streamableHttp_js = require('@modelcontextprotocol/sdk/server/streamableHttp.js');
|
|
6
|
+
var types_js = require('@modelcontextprotocol/sdk/types.js');
|
|
5
7
|
var errors = require('@backstage/errors');
|
|
8
|
+
var metrics = require('../metrics.cjs.js');
|
|
6
9
|
|
|
7
10
|
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }
|
|
8
11
|
|
|
@@ -11,10 +14,25 @@ var PromiseRouter__default = /*#__PURE__*/_interopDefaultCompat(PromiseRouter);
|
|
|
11
14
|
const createStreamableRouter = ({
|
|
12
15
|
mcpService,
|
|
13
16
|
httpAuth,
|
|
14
|
-
logger
|
|
17
|
+
logger,
|
|
18
|
+
metrics: metrics$1
|
|
15
19
|
}) => {
|
|
16
20
|
const router = PromiseRouter__default.default();
|
|
21
|
+
const sessionDuration = metrics$1.createHistogram(
|
|
22
|
+
"mcp.server.session.duration",
|
|
23
|
+
{
|
|
24
|
+
description: "The duration of the MCP session as observed on the MCP server",
|
|
25
|
+
unit: "s",
|
|
26
|
+
advice: { explicitBucketBoundaries: metrics.bucketBoundaries }
|
|
27
|
+
}
|
|
28
|
+
);
|
|
17
29
|
router.post("/", async (req, res) => {
|
|
30
|
+
const sessionStart = node_perf_hooks.performance.now();
|
|
31
|
+
const baseAttributes = {
|
|
32
|
+
"mcp.protocol.version": types_js.LATEST_PROTOCOL_VERSION,
|
|
33
|
+
"network.transport": "tcp",
|
|
34
|
+
"network.protocol.name": "http"
|
|
35
|
+
};
|
|
18
36
|
try {
|
|
19
37
|
const server = mcpService.getServer({
|
|
20
38
|
credentials: await httpAuth.credentials(req)
|
|
@@ -29,8 +47,11 @@ const createStreamableRouter = ({
|
|
|
29
47
|
res.on("close", () => {
|
|
30
48
|
transport.close();
|
|
31
49
|
server.close();
|
|
50
|
+
const durationSeconds = (node_perf_hooks.performance.now() - sessionStart) / 1e3;
|
|
51
|
+
sessionDuration.record(durationSeconds, baseAttributes);
|
|
32
52
|
});
|
|
33
53
|
} catch (error) {
|
|
54
|
+
const errorType = errors.isError(error) ? error.name : "Error";
|
|
34
55
|
if (errors.isError(error)) {
|
|
35
56
|
logger.error(error.message);
|
|
36
57
|
}
|
|
@@ -44,6 +65,11 @@ const createStreamableRouter = ({
|
|
|
44
65
|
id: null
|
|
45
66
|
});
|
|
46
67
|
}
|
|
68
|
+
const durationSeconds = (node_perf_hooks.performance.now() - sessionStart) / 1e3;
|
|
69
|
+
sessionDuration.record(durationSeconds, {
|
|
70
|
+
...baseAttributes,
|
|
71
|
+
"error.type": errorType
|
|
72
|
+
});
|
|
47
73
|
}
|
|
48
74
|
});
|
|
49
75
|
router.get("/", async (_, res) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createStreamableRouter.cjs.js","sources":["../../src/routers/createStreamableRouter.ts"],"sourcesContent":["/*\n * Copyright 2025 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport PromiseRouter from 'express-promise-router';\nimport { Router } from 'express';\nimport { McpService } from '../services/McpService';\nimport { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';\nimport { HttpAuthService, LoggerService } from '@backstage/backend-plugin-api';\nimport { isError } from '@backstage/errors';\n\nexport const createStreamableRouter = ({\n mcpService,\n httpAuth,\n logger,\n}: {\n mcpService: McpService;\n logger: LoggerService;\n httpAuth: HttpAuthService;\n}): Router => {\n const router = PromiseRouter();\n\n router.post('/', async (req, res) => {\n try {\n const server = mcpService.getServer({\n credentials: await httpAuth.credentials(req),\n });\n\n const transport = new StreamableHTTPServerTransport({\n // stateless implementation for now, so that we can support multiple\n // instances of the server backend, and avoid sticky sessions.\n sessionIdGenerator: undefined,\n });\n\n await server.connect(transport);\n await transport.handleRequest(req, res, req.body);\n\n res.on('close', () => {\n transport.close();\n server.close();\n });\n } catch (error) {\n if (isError(error)) {\n logger.error(error.message);\n }\n\n if (!res.headersSent) {\n res.status(500).json({\n jsonrpc: '2.0',\n error: {\n code: -32603,\n message: 'Internal server error',\n },\n id: null,\n });\n }\n }\n });\n\n router.get('/', async (_, res) => {\n // We only support POST requests, so we return a 405 error for all other methods.\n res.writeHead(405).end(\n JSON.stringify({\n jsonrpc: '2.0',\n error: {\n code: -32000,\n message: 'Method not allowed.',\n },\n id: null,\n }),\n );\n });\n\n router.delete('/', async (_, res) => {\n // We only support POST requests, so we return a 405 error for all other methods.\n res.writeHead(405).end(\n JSON.stringify({\n jsonrpc: '2.0',\n error: {\n code: -32000,\n message: 'Method not allowed.',\n },\n id: null,\n }),\n );\n });\n\n return router;\n};\n"],"names":["PromiseRouter","StreamableHTTPServerTransport","isError"],"mappings":"
|
|
1
|
+
{"version":3,"file":"createStreamableRouter.cjs.js","sources":["../../src/routers/createStreamableRouter.ts"],"sourcesContent":["/*\n * Copyright 2025 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport PromiseRouter from 'express-promise-router';\nimport { Router } from 'express';\nimport { performance } from 'node:perf_hooks';\nimport { McpService } from '../services/McpService';\nimport { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';\nimport { LATEST_PROTOCOL_VERSION } from '@modelcontextprotocol/sdk/types.js';\nimport { HttpAuthService, LoggerService } from '@backstage/backend-plugin-api';\nimport { isError } from '@backstage/errors';\nimport { MetricsService } from '@backstage/backend-plugin-api/alpha';\nimport { bucketBoundaries, McpServerSessionAttributes } from '../metrics';\n\nexport const createStreamableRouter = ({\n mcpService,\n httpAuth,\n logger,\n metrics,\n}: {\n mcpService: McpService;\n logger: LoggerService;\n httpAuth: HttpAuthService;\n metrics: MetricsService;\n}): Router => {\n const router = PromiseRouter();\n\n const sessionDuration = metrics.createHistogram<McpServerSessionAttributes>(\n 'mcp.server.session.duration',\n {\n description:\n 'The duration of the MCP session as observed on the MCP server',\n unit: 's',\n advice: { explicitBucketBoundaries: bucketBoundaries },\n },\n );\n\n router.post('/', async (req, res) => {\n const sessionStart = performance.now();\n\n const baseAttributes: McpServerSessionAttributes = {\n 'mcp.protocol.version': LATEST_PROTOCOL_VERSION,\n 'network.transport': 'tcp',\n 'network.protocol.name': 'http',\n };\n\n try {\n const server = mcpService.getServer({\n credentials: await httpAuth.credentials(req),\n });\n\n const transport = new StreamableHTTPServerTransport({\n // stateless implementation for now, so that we can support multiple\n // instances of the server backend, and avoid sticky sessions.\n sessionIdGenerator: undefined,\n });\n\n await server.connect(transport);\n await transport.handleRequest(req, res, req.body);\n\n res.on('close', () => {\n transport.close();\n server.close();\n\n const durationSeconds = (performance.now() - sessionStart) / 1000;\n\n sessionDuration.record(durationSeconds, baseAttributes);\n });\n } catch (error) {\n const errorType = isError(error) ? error.name : 'Error';\n\n if (isError(error)) {\n logger.error(error.message);\n }\n\n if (!res.headersSent) {\n res.status(500).json({\n jsonrpc: '2.0',\n error: {\n code: -32603,\n message: 'Internal server error',\n },\n id: null,\n });\n }\n\n const durationSeconds = (performance.now() - sessionStart) / 1000;\n\n sessionDuration.record(durationSeconds, {\n ...baseAttributes,\n 'error.type': errorType,\n });\n }\n });\n\n router.get('/', async (_, res) => {\n // We only support POST requests, so we return a 405 error for all other methods.\n res.writeHead(405).end(\n JSON.stringify({\n jsonrpc: '2.0',\n error: {\n code: -32000,\n message: 'Method not allowed.',\n },\n id: null,\n }),\n );\n });\n\n router.delete('/', async (_, res) => {\n // We only support POST requests, so we return a 405 error for all other methods.\n res.writeHead(405).end(\n JSON.stringify({\n jsonrpc: '2.0',\n error: {\n code: -32000,\n message: 'Method not allowed.',\n },\n id: null,\n }),\n );\n });\n\n return router;\n};\n"],"names":["metrics","PromiseRouter","bucketBoundaries","performance","LATEST_PROTOCOL_VERSION","StreamableHTTPServerTransport","isError"],"mappings":";;;;;;;;;;;;;AA0BO,MAAM,yBAAyB,CAAC;AAAA,EACrC,UAAA;AAAA,EACA,QAAA;AAAA,EACA,MAAA;AAAA,WACAA;AACF,CAAA,KAKc;AACZ,EAAA,MAAM,SAASC,8BAAA,EAAc;AAE7B,EAAA,MAAM,kBAAkBD,SAAA,CAAQ,eAAA;AAAA,IAC9B,6BAAA;AAAA,IACA;AAAA,MACE,WAAA,EACE,+D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ACL;AAAA,KACH;AAAA,EACF,CAAC,CAAA;AAED,EAAA,OAAO,MAAA;AACT;;;;"}
|
|
@@ -4,15 +4,29 @@ var index_js = require('@modelcontextprotocol/sdk/server/index.js');
|
|
|
4
4
|
var types_js = require('@modelcontextprotocol/sdk/types.js');
|
|
5
5
|
var package_json = require('@backstage/plugin-mcp-actions-backend/package.json');
|
|
6
6
|
var errors = require('@backstage/errors');
|
|
7
|
+
var node_perf_hooks = require('node:perf_hooks');
|
|
7
8
|
var handleErrors = require('./handleErrors.cjs.js');
|
|
9
|
+
var metrics = require('../metrics.cjs.js');
|
|
8
10
|
|
|
9
11
|
class McpService {
|
|
10
12
|
actions;
|
|
11
|
-
|
|
13
|
+
operationDuration;
|
|
14
|
+
constructor(actions, metrics$1) {
|
|
12
15
|
this.actions = actions;
|
|
16
|
+
this.operationDuration = metrics$1.createHistogram(
|
|
17
|
+
"mcp.server.operation.duration",
|
|
18
|
+
{
|
|
19
|
+
description: "MCP request duration as observed on the receiver",
|
|
20
|
+
unit: "s",
|
|
21
|
+
advice: { explicitBucketBoundaries: metrics.bucketBoundaries }
|
|
22
|
+
}
|
|
23
|
+
);
|
|
13
24
|
}
|
|
14
|
-
static async create({
|
|
15
|
-
|
|
25
|
+
static async create({
|
|
26
|
+
actions,
|
|
27
|
+
metrics
|
|
28
|
+
}) {
|
|
29
|
+
return new McpService(actions, metrics);
|
|
16
30
|
}
|
|
17
31
|
getServer({ credentials }) {
|
|
18
32
|
const server = new index_js.Server(
|
|
@@ -24,51 +38,86 @@ class McpService {
|
|
|
24
38
|
{ capabilities: { tools: {} } }
|
|
25
39
|
);
|
|
26
40
|
server.setRequestHandler(types_js.ListToolsRequestSchema, async () => {
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
inputSchema: action.schema.input,
|
|
31
|
-
// todo(blam): this is unfortunately not supported by most clients yet.
|
|
32
|
-
// When this is provided you need to provide structuredContent instead.
|
|
33
|
-
// outputSchema: action.schema.output,
|
|
34
|
-
name: action.name,
|
|
35
|
-
description: action.description,
|
|
36
|
-
annotations: {
|
|
37
|
-
title: action.title,
|
|
38
|
-
destructiveHint: action.attributes.destructive,
|
|
39
|
-
idempotentHint: action.attributes.idempotent,
|
|
40
|
-
readOnlyHint: action.attributes.readOnly,
|
|
41
|
-
openWorldHint: false
|
|
42
|
-
}
|
|
43
|
-
}))
|
|
44
|
-
};
|
|
45
|
-
});
|
|
46
|
-
server.setRequestHandler(types_js.CallToolRequestSchema, async ({ params }) => {
|
|
47
|
-
return handleErrors.handleErrors(async () => {
|
|
41
|
+
const startTime = node_perf_hooks.performance.now();
|
|
42
|
+
let errorType;
|
|
43
|
+
try {
|
|
48
44
|
const { actions } = await this.actions.list({ credentials });
|
|
49
|
-
const action = actions.find((a) => a.name === params.name);
|
|
50
|
-
if (!action) {
|
|
51
|
-
throw new errors.NotFoundError(`Action "${params.name}" not found`);
|
|
52
|
-
}
|
|
53
|
-
const { output } = await this.actions.invoke({
|
|
54
|
-
id: action.id,
|
|
55
|
-
input: params.arguments,
|
|
56
|
-
credentials
|
|
57
|
-
});
|
|
58
45
|
return {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
46
|
+
tools: actions.map((action) => ({
|
|
47
|
+
inputSchema: action.schema.input,
|
|
48
|
+
// todo(blam): this is unfortunately not supported by most clients yet.
|
|
49
|
+
// When this is provided you need to provide structuredContent instead.
|
|
50
|
+
// outputSchema: action.schema.output,
|
|
51
|
+
name: action.name,
|
|
52
|
+
description: action.description,
|
|
53
|
+
annotations: {
|
|
54
|
+
title: action.title,
|
|
55
|
+
destructiveHint: action.attributes.destructive,
|
|
56
|
+
idempotentHint: action.attributes.idempotent,
|
|
57
|
+
readOnlyHint: action.attributes.readOnly,
|
|
58
|
+
openWorldHint: false
|
|
68
59
|
}
|
|
69
|
-
|
|
60
|
+
}))
|
|
70
61
|
};
|
|
71
|
-
})
|
|
62
|
+
} catch (err) {
|
|
63
|
+
errorType = err instanceof Error ? err.name : "Error";
|
|
64
|
+
throw err;
|
|
65
|
+
} finally {
|
|
66
|
+
const durationSeconds = (node_perf_hooks.performance.now() - startTime) / 1e3;
|
|
67
|
+
this.operationDuration.record(durationSeconds, {
|
|
68
|
+
"mcp.method.name": "tools/list",
|
|
69
|
+
...errorType && { "error.type": errorType }
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
server.setRequestHandler(types_js.CallToolRequestSchema, async ({ params }) => {
|
|
74
|
+
const startTime = node_perf_hooks.performance.now();
|
|
75
|
+
let errorType;
|
|
76
|
+
let isError = false;
|
|
77
|
+
try {
|
|
78
|
+
const result = await handleErrors.handleErrors(async () => {
|
|
79
|
+
const { actions } = await this.actions.list({ credentials });
|
|
80
|
+
const action = actions.find((a) => a.name === params.name);
|
|
81
|
+
if (!action) {
|
|
82
|
+
throw new errors.NotFoundError(`Action "${params.name}" not found`);
|
|
83
|
+
}
|
|
84
|
+
const { output } = await this.actions.invoke({
|
|
85
|
+
id: action.id,
|
|
86
|
+
input: params.arguments,
|
|
87
|
+
credentials
|
|
88
|
+
});
|
|
89
|
+
return {
|
|
90
|
+
// todo(blam): unfortunately structuredContent is not supported by most clients yet.
|
|
91
|
+
// so the validation for the output happens in the default actions registry
|
|
92
|
+
// and we return it as json text instead for now.
|
|
93
|
+
content: [
|
|
94
|
+
{
|
|
95
|
+
type: "text",
|
|
96
|
+
text: ["```json", JSON.stringify(output, null, 2), "```"].join(
|
|
97
|
+
"\n"
|
|
98
|
+
)
|
|
99
|
+
}
|
|
100
|
+
]
|
|
101
|
+
};
|
|
102
|
+
});
|
|
103
|
+
isError = !!result?.isError;
|
|
104
|
+
return result;
|
|
105
|
+
} catch (err) {
|
|
106
|
+
errorType = err instanceof Error ? err.name : "Error";
|
|
107
|
+
throw err;
|
|
108
|
+
} finally {
|
|
109
|
+
const durationSeconds = (node_perf_hooks.performance.now() - startTime) / 1e3;
|
|
110
|
+
let errorAttribute = errorType;
|
|
111
|
+
if (!errorAttribute && isError) {
|
|
112
|
+
errorAttribute = "tool_error";
|
|
113
|
+
}
|
|
114
|
+
this.operationDuration.record(durationSeconds, {
|
|
115
|
+
"mcp.method.name": "tools/call",
|
|
116
|
+
"gen_ai.tool.name": params.name,
|
|
117
|
+
"gen_ai.operation.name": "execute_tool",
|
|
118
|
+
...errorAttribute && { "error.type": errorAttribute }
|
|
119
|
+
});
|
|
120
|
+
}
|
|
72
121
|
});
|
|
73
122
|
return server;
|
|
74
123
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"McpService.cjs.js","sources":["../../src/services/McpService.ts"],"sourcesContent":["/*\n * Copyright 2025 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { BackstageCredentials } from '@backstage/backend-plugin-api';\nimport { Server as McpServer } from '@modelcontextprotocol/sdk/server/index.js';\nimport {\n ListToolsRequestSchema,\n CallToolRequestSchema,\n} from '@modelcontextprotocol/sdk/types.js';\nimport { JsonObject } from '@backstage/types';\nimport {
|
|
1
|
+
{"version":3,"file":"McpService.cjs.js","sources":["../../src/services/McpService.ts"],"sourcesContent":["/*\n * Copyright 2025 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { BackstageCredentials } from '@backstage/backend-plugin-api';\nimport { Server as McpServer } from '@modelcontextprotocol/sdk/server/index.js';\nimport {\n ListToolsRequestSchema,\n CallToolRequestSchema,\n} from '@modelcontextprotocol/sdk/types.js';\nimport { JsonObject } from '@backstage/types';\nimport {\n ActionsService,\n MetricsServiceHistogram,\n MetricsService,\n} from '@backstage/backend-plugin-api/alpha';\nimport { version } from '@backstage/plugin-mcp-actions-backend/package.json';\nimport { NotFoundError } from '@backstage/errors';\nimport { performance } from 'node:perf_hooks';\n\nimport { handleErrors } from './handleErrors';\nimport { bucketBoundaries, McpServerOperationAttributes } from '../metrics';\n\nexport class McpService {\n private readonly actions: ActionsService;\n private readonly operationDuration: MetricsServiceHistogram<McpServerOperationAttributes>;\n\n constructor(actions: ActionsService, metrics: MetricsService) {\n this.actions = actions;\n this.operationDuration =\n metrics.createHistogram<McpServerOperationAttributes>(\n 'mcp.server.operation.duration',\n {\n description: 'MCP request duration as observed on the receiver',\n unit: 's',\n advice: { explicitBucketBoundaries: bucketBoundaries },\n },\n );\n }\n\n static async create({\n actions,\n metrics,\n }: {\n actions: ActionsService;\n metrics: MetricsService;\n }) {\n return new McpService(actions, metrics);\n }\n\n getServer({ credentials }: { credentials: BackstageCredentials }) {\n const server = new McpServer(\n {\n name: 'backstage',\n // TODO: this version will most likely change in the future.\n version,\n },\n { capabilities: { tools: {} } },\n );\n\n server.setRequestHandler(ListToolsRequestSchema, async () => {\n const startTime = performance.now();\n let errorType: string | undefined;\n\n try {\n // TODO: switch this to be configuration based later\n const { actions } = await this.actions.list({ credentials });\n\n return {\n tools: actions.map(action => ({\n inputSchema: action.schema.input,\n // todo(blam): this is unfortunately not supported by most clients yet.\n // When this is provided you need to provide structuredContent instead.\n // outputSchema: action.schema.output,\n name: action.name,\n description: action.description,\n annotations: {\n title: action.title,\n destructiveHint: action.attributes.destructive,\n idempotentHint: action.attributes.idempotent,\n readOnlyHint: action.attributes.readOnly,\n openWorldHint: false,\n },\n })),\n };\n } catch (err) {\n errorType = err instanceof Error ? err.name : 'Error';\n throw err;\n } finally {\n const durationSeconds = (performance.now() - startTime) / 1000;\n\n this.operationDuration.record(durationSeconds, {\n 'mcp.method.name': 'tools/list',\n ...(errorType && { 'error.type': errorType }),\n });\n }\n });\n\n server.setRequestHandler(CallToolRequestSchema, async ({ params }) => {\n const startTime = performance.now();\n let errorType: string | undefined;\n let isError = false;\n\n try {\n const result = await handleErrors(async () => {\n const { actions } = await this.actions.list({ credentials });\n const action = actions.find(a => a.name === params.name);\n\n if (!action) {\n throw new NotFoundError(`Action \"${params.name}\" not found`);\n }\n\n const { output } = await this.actions.invoke({\n id: action.id,\n input: params.arguments as JsonObject,\n credentials,\n });\n\n return {\n // todo(blam): unfortunately structuredContent is not supported by most clients yet.\n // so the validation for the output happens in the default actions registry\n // and we return it as json text instead for now.\n content: [\n {\n type: 'text',\n text: ['```json', JSON.stringify(output, null, 2), '```'].join(\n '\\n',\n ),\n },\n ],\n };\n });\n\n isError = !!(result as { isError?: boolean })?.isError;\n return result;\n } catch (err) {\n errorType = err instanceof Error ? err.name : 'Error';\n throw err;\n } finally {\n const durationSeconds = (performance.now() - startTime) / 1000;\n\n // Determine error.type per OTel MCP spec:\n // - Thrown exceptions use the error name\n // - CallToolResult with isError=true uses 'tool_error'\n let errorAttribute: string | undefined = errorType;\n if (!errorAttribute && isError) {\n errorAttribute = 'tool_error';\n }\n\n this.operationDuration.record(durationSeconds, {\n 'mcp.method.name': 'tools/call',\n 'gen_ai.tool.name': params.name,\n 'gen_ai.operation.name': 'execute_tool',\n ...(errorAttribute && { 'error.type': errorAttribute }),\n });\n }\n });\n\n return server;\n }\n}\n"],"names":["metrics","bucketBoundaries","McpServer","version","ListToolsRequestSchema","performance","CallToolRequestSchema","handleErrors","NotFoundError"],"mappings":";;;;;;;;;;AAkCO,MAAM,UAAA,CAAW;AAAA,EACL,OAAA;AAAA,EACA,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AACF;;;;"}
|
|
@@ -15,7 +15,7 @@ const knownErrors = /* @__PURE__ */ new Set([
|
|
|
15
15
|
]);
|
|
16
16
|
function extractCause(err) {
|
|
17
17
|
if ((err.name === "ResponseError" || err instanceof errors.ForwardedError) && errors.isError(err.cause)) {
|
|
18
|
-
return err.cause;
|
|
18
|
+
return extractCause(err.cause);
|
|
19
19
|
}
|
|
20
20
|
return err;
|
|
21
21
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handleErrors.cjs.js","sources":["../../src/services/handleErrors.ts"],"sourcesContent":["/*\n * Copyright 2025 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n ErrorLike,\n ForwardedError,\n isError,\n serializeError,\n} from '@backstage/errors';\n\nimport { Server as McpServer } from '@modelcontextprotocol/sdk/server/index.js';\n\nconst knownErrors = new Set([\n 'InputError',\n 'AuthenticationError',\n 'NotAllowedError',\n 'NotFoundError',\n 'ConflictError',\n 'NotModifiedError',\n 'NotImplementedError',\n 'ResponseError',\n 'ServiceUnavailableError',\n]);\n\n//
|
|
1
|
+
{"version":3,"file":"handleErrors.cjs.js","sources":["../../src/services/handleErrors.ts"],"sourcesContent":["/*\n * Copyright 2025 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n ErrorLike,\n ForwardedError,\n isError,\n serializeError,\n} from '@backstage/errors';\n\nimport { Server as McpServer } from '@modelcontextprotocol/sdk/server/index.js';\n\nconst knownErrors = new Set([\n 'InputError',\n 'AuthenticationError',\n 'NotAllowedError',\n 'NotFoundError',\n 'ConflictError',\n 'NotModifiedError',\n 'NotImplementedError',\n 'ResponseError',\n 'ServiceUnavailableError',\n]);\n\n// Recursively extracts the innermost cause from ResponseError or\n// ForwardedError wrappers to surface the original error.\nfunction extractCause(err: ErrorLike): ErrorLike {\n if (\n (err.name === 'ResponseError' || err instanceof ForwardedError) &&\n isError(err.cause)\n ) {\n return extractCause(err.cause);\n }\n return err;\n}\n\n/**\n * Takes a value expected to be an object, and returns a description of the\n * error to return to the MCP client, if the error is a known Backstage error.\n *\n * Re-throws the original error otherwise\n */\nfunction describeError(err: unknown): string {\n if (err instanceof Error) {\n const serialized = serializeError(err);\n\n const { name, message } = extractCause(serialized);\n\n if (knownErrors.has(name)) {\n return `${name}: ${message}`;\n }\n }\n\n throw err;\n}\n\ntype RequestResultType = ReturnType<\n Parameters<McpServer['setRequestHandler']>[1]\n>;\n/**\n * Wraps a request function with an error handler that turns known Backstage\n * errors into user-friendly messages, instead of failing the request\n * generically with a 500.\n */\nexport async function handleErrors(\n fn: () => RequestResultType | Promise<RequestResultType>,\n): Promise<RequestResultType> {\n try {\n return await fn();\n } catch (err) {\n // This will rethrow if the error is not a known Backstage error\n const description = describeError(err);\n return {\n content: [{ type: 'text', text: description }],\n isError: true,\n };\n }\n}\n"],"names":["ForwardedError","isError","serializeError"],"mappings":";;;;AAyBA,MAAM,WAAA,uBAAkB,GAAA,CAAI;AAAA,EAC1B,YAAA;AAAA,EACA,qBAAA;AAAA,EACA,iBAAA;AAAA,EACA,eAAA;AAAA,EACA,eAAA;AAAA,EACA,kBAAA;AAAA,EACA,qBAAA;AAAA,EACA,eAAA;AAAA,EACA;AACF,CAAC,CAAA;AAID,SAAS,aAAa,GAAA,EAA2B;AAC/C,EAAA,IAAA,CACG,GAAA,CAAI,SAAS,eAAA,IAAmB,GAAA,YAAeA,0BAChDC,cAAA,CAAQ,GAAA,CAAI,KAAK,CAAA,EACjB;AACA,IAAA,OAAO,YAAA,CAAa,IAAI,KAAK,CAAA;AAAA,EAC/B;AACA,EAAA,OAAO,GAAA;AACT;AAQA,SAAS,cAAc,GAAA,EAAsB;AAC3C,EAAA,IAAI,eAAe,KAAA,EAAO;AACxB,IAAA,MAAM,UAAA,GAAaC,sBAAe,GAAG,CAAA;AAErC,IAAA,MAAM,EAAE,IAAA,EAAM,OAAA,EAAQ,GAAI,aAAa,UAAU,CAAA;AAEjD,IAAA,IAAI,WAAA,CAAY,GAAA,CAAI,IAAI,CAAA,EAAG;AACzB,MAAA,OAAO,CAAA,EAAG,IAAI,CAAA,EAAA,EAAK,OAAO,CAAA,CAAA;AAAA,IAC5B;AAAA,EACF;AAEA,EAAA,MAAM,GAAA;AACR;AAUA,eAAsB,aACpB,EAAA,EAC4B;AAC5B,EAAA,IAAI;AACF,IAAA,OAAO,MAAM,EAAA,EAAG;AAAA,EAClB,SAAS,GAAA,EAAK;AAEZ,IAAA,MAAM,WAAA,GAAc,cAAc,GAAG,CAAA;AACrC,IAAA,OAAO;AAAA,MACL,SAAS,CAAC,EAAE,MAAM,MAAA,EAAQ,IAAA,EAAM,aAAa,CAAA;AAAA,MAC7C,OAAA,EAAS;AAAA,KACX;AAAA,EACF;AACF;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@backstage/plugin-mcp-actions-backend",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.10-next.1",
|
|
4
4
|
"backstage": {
|
|
5
5
|
"role": "backend-plugin",
|
|
6
6
|
"pluginId": "mcp-actions",
|
|
@@ -37,11 +37,11 @@
|
|
|
37
37
|
"test": "backstage-cli package test"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@backstage/backend-plugin-api": "
|
|
41
|
-
"@backstage/catalog-client": "
|
|
42
|
-
"@backstage/errors": "
|
|
43
|
-
"@backstage/plugin-catalog-node": "
|
|
44
|
-
"@backstage/types": "
|
|
40
|
+
"@backstage/backend-plugin-api": "1.7.1-next.0",
|
|
41
|
+
"@backstage/catalog-client": "1.14.0-next.1",
|
|
42
|
+
"@backstage/errors": "1.2.7",
|
|
43
|
+
"@backstage/plugin-catalog-node": "2.1.0-next.1",
|
|
44
|
+
"@backstage/types": "1.2.2",
|
|
45
45
|
"@cfworker/json-schema": "^4.1.1",
|
|
46
46
|
"@modelcontextprotocol/sdk": "^1.25.2",
|
|
47
47
|
"express": "^4.22.0",
|
|
@@ -49,9 +49,9 @@
|
|
|
49
49
|
"zod": "^3.25.76"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
|
-
"@backstage/backend-defaults": "
|
|
53
|
-
"@backstage/backend-test-utils": "
|
|
54
|
-
"@backstage/cli": "
|
|
52
|
+
"@backstage/backend-defaults": "0.16.0-next.1",
|
|
53
|
+
"@backstage/backend-test-utils": "1.11.1-next.1",
|
|
54
|
+
"@backstage/cli": "0.36.0-next.1",
|
|
55
55
|
"@types/express": "^4.17.6",
|
|
56
56
|
"@types/supertest": "^2.0.8",
|
|
57
57
|
"supertest": "^7.0.0"
|