@tsed/platform-mcp 8.28.0 → 8.28.2
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/lib/esm/decorators/resource.js +13 -4
- package/lib/esm/fn/definePrompt.js +36 -20
- package/lib/esm/fn/defineResource.js +47 -3
- package/lib/esm/fn/defineTool.js +14 -24
- package/lib/esm/services/McpServerFactory.js +14 -3
- package/lib/tsconfig.esm.tsbuildinfo +1 -1
- package/lib/types/decorators/prompt.d.ts +3 -3
- package/lib/types/decorators/resource.d.ts +2 -2
- package/lib/types/fn/definePrompt.d.ts +24 -29
- package/lib/types/fn/defineResource.d.ts +114 -8
- package/lib/types/fn/defineTool.d.ts +12 -222
- package/lib/types/interfaces/PlatformMcpSettings.d.ts +22 -0
- package/package.json +9 -7
- package/src/decorators/prompt.ts +5 -5
- package/src/decorators/resource.ts +20 -8
- package/src/decorators/tool.ts +1 -1
- package/src/fn/definePrompt.spec.ts +61 -0
- package/src/fn/definePrompt.ts +61 -13
- package/src/fn/defineResource.spec.ts +88 -0
- package/src/fn/defineResource.ts +76 -12
- package/src/fn/defineTool.spec.ts +28 -2
- package/src/fn/defineTool.ts +26 -11
- package/src/interfaces/PlatformMcpSettings.ts +22 -0
- package/src/services/McpServerFactory.ts +22 -8
|
@@ -21,13 +21,22 @@ import { defineResource } from "../fn/defineResource.js";
|
|
|
21
21
|
*/
|
|
22
22
|
export function Resource(uriOrTemplate, options) {
|
|
23
23
|
return (target, propertyKey, _) => {
|
|
24
|
-
|
|
24
|
+
const base = {
|
|
25
25
|
...options,
|
|
26
26
|
name: options?.name || String(propertyKey),
|
|
27
27
|
token: classOf(target),
|
|
28
|
-
propertyKey
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
propertyKey
|
|
29
|
+
};
|
|
30
|
+
if (isString(uriOrTemplate)) {
|
|
31
|
+
defineResource({
|
|
32
|
+
...base,
|
|
33
|
+
uri: uriOrTemplate
|
|
34
|
+
});
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
defineResource({
|
|
38
|
+
...base,
|
|
39
|
+
template: uriOrTemplate
|
|
31
40
|
});
|
|
32
41
|
};
|
|
33
42
|
}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { isArrowFn } from "@tsed/core";
|
|
2
|
-
import { inject, injectable } from "@tsed/di";
|
|
2
|
+
import { context, inject, injectable, logger } from "@tsed/di";
|
|
3
3
|
import { JsonEntityStore } from "@tsed/schema";
|
|
4
|
+
import { constantCase } from "change-case";
|
|
4
5
|
import { MCP_PROVIDER_TYPES } from "../constants/constants.js";
|
|
5
6
|
import { toZod } from "../utils/toZod.js";
|
|
6
7
|
function mapOptions(options) {
|
|
7
8
|
let handler = undefined;
|
|
9
|
+
const name = options.name || ("propertyKey" in options ? String(options.propertyKey) : undefined);
|
|
8
10
|
if ("handler" in options) {
|
|
9
11
|
handler = options.handler;
|
|
10
12
|
}
|
|
@@ -20,33 +22,47 @@ function mapOptions(options) {
|
|
|
20
22
|
}
|
|
21
23
|
return {
|
|
22
24
|
...options,
|
|
25
|
+
name,
|
|
23
26
|
argsSchema: toZod(isArrowFn(options.argsSchema) ? options.argsSchema() : options.argsSchema),
|
|
24
27
|
handler: handler
|
|
25
28
|
};
|
|
26
29
|
}
|
|
27
|
-
/**
|
|
28
|
-
* Registers an MCP prompt provider with the Ts.ED injector and returns the associated token.
|
|
29
|
-
*
|
|
30
|
-
* @typeParam Args JSON payload expected by the prompt.
|
|
31
|
-
* @param options Functional or class-based prompt configuration.
|
|
32
|
-
* @returns The DI token representing the registered prompt provider.
|
|
33
|
-
* @module platform/mcp
|
|
34
|
-
* @since 8.17.0
|
|
35
|
-
*
|
|
36
|
-
* ### Usage
|
|
37
|
-
* ```ts
|
|
38
|
-
* definePrompt({
|
|
39
|
-
* name: "weather",
|
|
40
|
-
* description: "Summaries today's forecast",
|
|
41
|
-
* handler: async () => ({content: [{type: "text", text: "Sunny"}]})
|
|
42
|
-
* });
|
|
43
|
-
* ```
|
|
44
|
-
*/
|
|
45
30
|
export function definePrompt(options) {
|
|
46
31
|
const provider = injectable(Symbol.for(`MCP:PROMPT:${options.name}`))
|
|
47
32
|
.type(MCP_PROVIDER_TYPES.PROMPT)
|
|
48
33
|
.factory(() => {
|
|
49
|
-
|
|
34
|
+
const { handler, ...opts } = mapOptions(options);
|
|
35
|
+
return {
|
|
36
|
+
...opts,
|
|
37
|
+
async handler(...args) {
|
|
38
|
+
try {
|
|
39
|
+
return await handler(...args);
|
|
40
|
+
}
|
|
41
|
+
catch (er) {
|
|
42
|
+
const code = er.name && er.status ? `E_MCP_PROMPT_${constantCase(er.name)}` : "E_MCP_PROMPT_ERROR";
|
|
43
|
+
logger().error({
|
|
44
|
+
event: "MCP_PROMPT_ERROR",
|
|
45
|
+
status_code: er.status,
|
|
46
|
+
code,
|
|
47
|
+
error_name: er.name,
|
|
48
|
+
message: er.message,
|
|
49
|
+
request_id: context().id,
|
|
50
|
+
prompt: opts.name
|
|
51
|
+
});
|
|
52
|
+
return {
|
|
53
|
+
description: er.message,
|
|
54
|
+
messages: [],
|
|
55
|
+
_meta: {
|
|
56
|
+
status_code: er.status,
|
|
57
|
+
code,
|
|
58
|
+
message: er.message,
|
|
59
|
+
request_id: context().id,
|
|
60
|
+
prompt: opts.name
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
};
|
|
50
66
|
});
|
|
51
67
|
return provider.token();
|
|
52
68
|
}
|
|
@@ -1,9 +1,14 @@
|
|
|
1
|
-
import { inject, injectable } from "@tsed/di";
|
|
1
|
+
import { context, inject, injectable, logger } from "@tsed/di";
|
|
2
2
|
import { JsonEntityStore } from "@tsed/schema";
|
|
3
|
+
import { constantCase } from "change-case";
|
|
3
4
|
import { MCP_PROVIDER_TYPES } from "../constants/constants.js";
|
|
5
|
+
function isClassResourceProps(options) {
|
|
6
|
+
return "token" in options && "propertyKey" in options;
|
|
7
|
+
}
|
|
4
8
|
function mapOptions(options) {
|
|
5
9
|
let handler;
|
|
6
|
-
|
|
10
|
+
const name = options.name || ("propertyKey" in options ? String(options.propertyKey) : undefined);
|
|
11
|
+
if (isClassResourceProps(options)) {
|
|
7
12
|
const { token, propertyKey } = options;
|
|
8
13
|
handler = (...args) => {
|
|
9
14
|
const instance = inject(options.token);
|
|
@@ -18,6 +23,7 @@ function mapOptions(options) {
|
|
|
18
23
|
}
|
|
19
24
|
return {
|
|
20
25
|
...options,
|
|
26
|
+
name,
|
|
21
27
|
handler
|
|
22
28
|
};
|
|
23
29
|
}
|
|
@@ -25,7 +31,45 @@ export function defineResource(options) {
|
|
|
25
31
|
const provider = injectable(Symbol.for(`MCP:RESOURCE:${options.name}`))
|
|
26
32
|
.type(MCP_PROVIDER_TYPES.RESOURCE)
|
|
27
33
|
.factory(() => {
|
|
28
|
-
|
|
34
|
+
const { handler, ...opts } = mapOptions(options);
|
|
35
|
+
return {
|
|
36
|
+
...opts,
|
|
37
|
+
async handler(...args) {
|
|
38
|
+
try {
|
|
39
|
+
return await handler(...args);
|
|
40
|
+
}
|
|
41
|
+
catch (er) {
|
|
42
|
+
const safeErr = er && typeof er === "object"
|
|
43
|
+
? er
|
|
44
|
+
: {
|
|
45
|
+
message: String(er),
|
|
46
|
+
name: undefined,
|
|
47
|
+
status: undefined
|
|
48
|
+
};
|
|
49
|
+
const code = safeErr.name && safeErr.status ? `E_MCP_RESOURCE_${constantCase(safeErr.name)}` : "E_MCP_RESOURCE_ERROR";
|
|
50
|
+
logger().error({
|
|
51
|
+
event: "MCP_RESOURCE_ERROR",
|
|
52
|
+
status_code: safeErr.status,
|
|
53
|
+
code,
|
|
54
|
+
error_name: safeErr.name,
|
|
55
|
+
message: safeErr.message,
|
|
56
|
+
request_id: context().id,
|
|
57
|
+
resource: opts.name
|
|
58
|
+
});
|
|
59
|
+
return {
|
|
60
|
+
contents: [],
|
|
61
|
+
_meta: {
|
|
62
|
+
status_code: safeErr.status,
|
|
63
|
+
code,
|
|
64
|
+
error_name: safeErr.name,
|
|
65
|
+
message: safeErr.message,
|
|
66
|
+
request_id: context().id,
|
|
67
|
+
resource: opts.name
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
};
|
|
29
73
|
});
|
|
30
74
|
return provider.token();
|
|
31
75
|
}
|
package/lib/esm/fn/defineTool.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { isArrowFn } from "@tsed/core";
|
|
2
|
-
import { inject, injectable, logger } from "@tsed/di";
|
|
2
|
+
import { context, inject, injectable, logger } from "@tsed/di";
|
|
3
3
|
import { JsonEntityStore } from "@tsed/schema";
|
|
4
|
+
import { constantCase } from "change-case";
|
|
4
5
|
import { MCP_PROVIDER_TYPES } from "../constants/constants.js";
|
|
5
6
|
import { toZod } from "../utils/toZod.js";
|
|
6
7
|
function getOutputSchema(methodStore) {
|
|
@@ -31,24 +32,6 @@ function mapOptions(options) {
|
|
|
31
32
|
handler
|
|
32
33
|
};
|
|
33
34
|
}
|
|
34
|
-
/**
|
|
35
|
-
* Registers an MCP tool provider and wraps its handler with error logging plus schema conversion.
|
|
36
|
-
*
|
|
37
|
-
* @typeParam Input JSON payload expected by the tool.
|
|
38
|
-
* @typeParam Output Structured output returned by the handler.
|
|
39
|
-
* @param options Functional or class-based tool configuration.
|
|
40
|
-
* @returns The DI token referencing the registered tool provider.
|
|
41
|
-
* @module platform/mcp
|
|
42
|
-
* @since 8.17.0
|
|
43
|
-
*
|
|
44
|
-
* ### Usage
|
|
45
|
-
* ```ts
|
|
46
|
-
* defineTool({
|
|
47
|
-
* name: "add",
|
|
48
|
-
* handler: async ({a, b}) => ({content: [{type: "text", text: String(a + b)}]})
|
|
49
|
-
* });
|
|
50
|
-
* ```
|
|
51
|
-
*/
|
|
52
35
|
export function defineTool(options) {
|
|
53
36
|
const provider = injectable(Symbol.for(`MCP:TOOL:${options.name}`))
|
|
54
37
|
.type(MCP_PROVIDER_TYPES.TOOL)
|
|
@@ -64,17 +47,24 @@ export function defineTool(options) {
|
|
|
64
47
|
return await handler(args, extra);
|
|
65
48
|
}
|
|
66
49
|
catch (er) {
|
|
50
|
+
const code = er.name && er.status ? `E_MCP_TOOL_${constantCase(er.name)}` : "E_MCP_TOOL_ERROR";
|
|
67
51
|
logger().error({
|
|
68
52
|
event: "MCP_TOOL_ERROR",
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
53
|
+
status_code: er.status,
|
|
54
|
+
code,
|
|
55
|
+
error_name: er.name,
|
|
56
|
+
message: er.message,
|
|
57
|
+
request_id: context().id,
|
|
58
|
+
tool: opts.name
|
|
72
59
|
});
|
|
73
60
|
return {
|
|
74
61
|
content: [],
|
|
75
62
|
structuredContent: {
|
|
76
|
-
|
|
77
|
-
|
|
63
|
+
status_code: er.status,
|
|
64
|
+
code,
|
|
65
|
+
message: er.message,
|
|
66
|
+
request_id: context().id,
|
|
67
|
+
tool: opts.name
|
|
78
68
|
}
|
|
79
69
|
};
|
|
80
70
|
}
|
|
@@ -19,10 +19,15 @@ export const MCP_SERVER = injectable(McpServer)
|
|
|
19
19
|
const settings = constant("mcp", {}) || {};
|
|
20
20
|
const name = settings.name || constant("name") || "tsed-mcp";
|
|
21
21
|
const version = settings.version || constant("version") || "0.0.0";
|
|
22
|
+
const { websiteUrl, description, title, icons } = settings;
|
|
22
23
|
const server = new McpServer({
|
|
24
|
+
websiteUrl,
|
|
25
|
+
description,
|
|
26
|
+
icons,
|
|
27
|
+
title,
|
|
23
28
|
name,
|
|
24
29
|
version
|
|
25
|
-
});
|
|
30
|
+
}, settings?.serverOptions);
|
|
26
31
|
const toolTokens = collectTokens(MCP_PROVIDER_TYPES.TOOL, settings.tools);
|
|
27
32
|
toolTokens.forEach((token) => {
|
|
28
33
|
const definition = inject(token);
|
|
@@ -33,13 +38,19 @@ export const MCP_SERVER = injectable(McpServer)
|
|
|
33
38
|
resourceTokens.forEach((token) => {
|
|
34
39
|
const definition = inject(token);
|
|
35
40
|
const { name, handler, uri, template, ...opts } = definition;
|
|
36
|
-
|
|
41
|
+
const resourceName = name || String(token);
|
|
42
|
+
if (uri) {
|
|
43
|
+
server.registerResource(resourceName, uri, opts, handler);
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
server.registerResource(resourceName, template, opts, handler);
|
|
47
|
+
}
|
|
37
48
|
});
|
|
38
49
|
const promptTokens = collectTokens(MCP_PROVIDER_TYPES.PROMPT, settings.prompts);
|
|
39
50
|
promptTokens.forEach((token) => {
|
|
40
51
|
const definition = inject(token);
|
|
41
52
|
const { name, handler, ...opts } = definition;
|
|
42
|
-
server.registerPrompt(name, opts, handler);
|
|
53
|
+
server.registerPrompt(name || String(token), opts, handler);
|
|
43
54
|
});
|
|
44
55
|
return server;
|
|
45
56
|
})
|