@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
package/src/decorators/tool.ts
CHANGED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import {inject} from "@tsed/di";
|
|
2
|
+
import {PlatformTest} from "@tsed/platform-http/testing";
|
|
3
|
+
import {afterEach, beforeEach, describe, expect, it} from "vitest";
|
|
4
|
+
|
|
5
|
+
import {definePrompt} from "./definePrompt.js";
|
|
6
|
+
|
|
7
|
+
describe("definePrompt", () => {
|
|
8
|
+
beforeEach(() => PlatformTest.create());
|
|
9
|
+
afterEach(() => PlatformTest.reset());
|
|
10
|
+
|
|
11
|
+
it("should wrap handler errors with structured MCP payloads", async () => {
|
|
12
|
+
const token = definePrompt({
|
|
13
|
+
name: "failing-prompt",
|
|
14
|
+
handler() {
|
|
15
|
+
throw new Error("boom");
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
const definition = inject<any>(token);
|
|
20
|
+
const result = await definition.handler({} as any);
|
|
21
|
+
|
|
22
|
+
expect(result).toEqual({
|
|
23
|
+
description: "boom",
|
|
24
|
+
messages: [],
|
|
25
|
+
_meta: {
|
|
26
|
+
status_code: undefined,
|
|
27
|
+
code: "E_MCP_PROMPT_ERROR",
|
|
28
|
+
message: "boom",
|
|
29
|
+
request_id: expect.any(String),
|
|
30
|
+
prompt: "failing-prompt"
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("should derive error code from error name and status when available", async () => {
|
|
36
|
+
const token = definePrompt({
|
|
37
|
+
name: "http-prompt",
|
|
38
|
+
handler() {
|
|
39
|
+
const er = new Error("Bad request") as Error & {status?: number; name: string};
|
|
40
|
+
er.name = "BadRequest";
|
|
41
|
+
er.status = 400;
|
|
42
|
+
throw er;
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const definition = inject<any>(token);
|
|
47
|
+
const result = await definition.handler({} as any);
|
|
48
|
+
|
|
49
|
+
expect(result).toEqual({
|
|
50
|
+
description: "Bad request",
|
|
51
|
+
messages: [],
|
|
52
|
+
_meta: {
|
|
53
|
+
status_code: 400,
|
|
54
|
+
code: "E_MCP_PROMPT_BAD_REQUEST",
|
|
55
|
+
message: "Bad request",
|
|
56
|
+
request_id: expect.any(String),
|
|
57
|
+
prompt: "http-prompt"
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
});
|
package/src/fn/definePrompt.ts
CHANGED
|
@@ -1,21 +1,33 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {RequestHandlerExtra} from "@modelcontextprotocol/sdk/shared/protocol.js";
|
|
2
|
+
import type {GetPromptResult, ServerNotification, ServerRequest} from "@modelcontextprotocol/sdk/types.js";
|
|
2
3
|
import {type AbstractType, isArrowFn, type Type} from "@tsed/core";
|
|
3
|
-
import {inject, injectable} from "@tsed/di";
|
|
4
|
+
import {context, inject, injectable, logger, type TokenProvider} from "@tsed/di";
|
|
4
5
|
import {JsonEntityStore, JsonSchema} from "@tsed/schema";
|
|
6
|
+
import {constantCase} from "change-case";
|
|
5
7
|
|
|
6
8
|
import {MCP_PROVIDER_TYPES} from "../constants/constants.js";
|
|
7
9
|
import {toZod} from "../utils/toZod.js";
|
|
8
10
|
|
|
9
|
-
type
|
|
11
|
+
type BasePromptConfig = {
|
|
12
|
+
title?: string;
|
|
13
|
+
description?: string;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
type BasePromptProps<Args> = BasePromptConfig & {
|
|
10
17
|
name: string;
|
|
11
|
-
argsSchema?:
|
|
18
|
+
argsSchema?: JsonSchema<Args> | (() => JsonSchema<Args>) | Record<string, unknown>;
|
|
12
19
|
};
|
|
13
20
|
|
|
14
|
-
type
|
|
15
|
-
|
|
21
|
+
export type PromptHandler<Args = undefined> = Args extends undefined
|
|
22
|
+
? (extra: RequestHandlerExtra<ServerRequest, ServerNotification>) => GetPromptResult | Promise<GetPromptResult>
|
|
23
|
+
: (args: Args, extra: RequestHandlerExtra<ServerRequest, ServerNotification>) => GetPromptResult | Promise<GetPromptResult>;
|
|
24
|
+
|
|
25
|
+
export type FnPromptProps<Args = any> = BasePromptProps<Args> & {
|
|
26
|
+
handler: PromptHandler<Args>;
|
|
16
27
|
};
|
|
17
28
|
|
|
18
|
-
type ClassPromptProps<Args
|
|
29
|
+
export type ClassPromptProps<Args = any> = Omit<BasePromptProps<Args>, "name"> & {
|
|
30
|
+
name?: string;
|
|
19
31
|
token: Type | AbstractType<any>;
|
|
20
32
|
propertyKey: string | symbol;
|
|
21
33
|
};
|
|
@@ -27,10 +39,11 @@ type ClassPromptProps<Args extends undefined = any> = BasePromptProps<Args> & {
|
|
|
27
39
|
* @module platform/mcp
|
|
28
40
|
* @since 8.17.0
|
|
29
41
|
*/
|
|
30
|
-
export type PromptProps<Args
|
|
42
|
+
export type PromptProps<Args = any> = FnPromptProps<Args> | ClassPromptProps<Args>;
|
|
31
43
|
|
|
32
|
-
function mapOptions<Args
|
|
33
|
-
let handler:
|
|
44
|
+
function mapOptions<Args = any>(options: PromptProps<Args>) {
|
|
45
|
+
let handler: PromptHandler<Args> = undefined as any;
|
|
46
|
+
const name = options.name || ("propertyKey" in options ? String(options.propertyKey) : undefined);
|
|
34
47
|
|
|
35
48
|
if ("handler" in options) {
|
|
36
49
|
handler = options.handler;
|
|
@@ -42,7 +55,7 @@ function mapOptions<Args extends undefined = any>(options: PromptProps<Args>) {
|
|
|
42
55
|
handler = ((...args: any[]) => {
|
|
43
56
|
const instance = inject(options.token) as any;
|
|
44
57
|
return instance[propertyKey](...args);
|
|
45
|
-
}) as unknown as
|
|
58
|
+
}) as unknown as PromptHandler<Args>;
|
|
46
59
|
|
|
47
60
|
const methodStore = JsonEntityStore.fromMethod(token, propertyKey);
|
|
48
61
|
options.description = options.description || methodStore.operation.get("description");
|
|
@@ -51,6 +64,7 @@ function mapOptions<Args extends undefined = any>(options: PromptProps<Args>) {
|
|
|
51
64
|
|
|
52
65
|
return {
|
|
53
66
|
...options,
|
|
67
|
+
name,
|
|
54
68
|
argsSchema: toZod(isArrowFn(options.argsSchema) ? options.argsSchema() : options.argsSchema),
|
|
55
69
|
handler: handler
|
|
56
70
|
};
|
|
@@ -82,11 +96,45 @@ export type PromptsSettings = ReturnType<typeof mapOptions>;
|
|
|
82
96
|
* });
|
|
83
97
|
* ```
|
|
84
98
|
*/
|
|
85
|
-
export function definePrompt<Args
|
|
99
|
+
export function definePrompt<Args = any>(options: FnPromptProps<Args>): TokenProvider;
|
|
100
|
+
export function definePrompt<Args = any>(options: ClassPromptProps<Args>): TokenProvider;
|
|
101
|
+
export function definePrompt<Args = any>(options: PromptProps<Args>) {
|
|
86
102
|
const provider = injectable(Symbol.for(`MCP:PROMPT:${options.name}`))
|
|
87
103
|
.type(MCP_PROVIDER_TYPES.PROMPT)
|
|
88
104
|
.factory(() => {
|
|
89
|
-
|
|
105
|
+
const {handler, ...opts} = mapOptions<Args>(options);
|
|
106
|
+
|
|
107
|
+
return {
|
|
108
|
+
...opts,
|
|
109
|
+
async handler(...args: any[]) {
|
|
110
|
+
try {
|
|
111
|
+
return await (handler as any)(...args);
|
|
112
|
+
} catch (er: any) {
|
|
113
|
+
const code = er.name && er.status ? `E_MCP_PROMPT_${constantCase(er.name)}` : "E_MCP_PROMPT_ERROR";
|
|
114
|
+
logger().error({
|
|
115
|
+
event: "MCP_PROMPT_ERROR",
|
|
116
|
+
status_code: er.status,
|
|
117
|
+
code,
|
|
118
|
+
error_name: er.name,
|
|
119
|
+
message: er.message,
|
|
120
|
+
request_id: context().id,
|
|
121
|
+
prompt: opts.name
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
return {
|
|
125
|
+
description: er.message,
|
|
126
|
+
messages: [],
|
|
127
|
+
_meta: {
|
|
128
|
+
status_code: er.status,
|
|
129
|
+
code,
|
|
130
|
+
message: er.message,
|
|
131
|
+
request_id: context().id,
|
|
132
|
+
prompt: opts.name
|
|
133
|
+
}
|
|
134
|
+
} satisfies GetPromptResult;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
};
|
|
90
138
|
});
|
|
91
139
|
|
|
92
140
|
return provider.token();
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import {inject} from "@tsed/di";
|
|
2
|
+
import {PlatformTest} from "@tsed/platform-http/testing";
|
|
3
|
+
import {afterEach, beforeEach, describe, expect, it} from "vitest";
|
|
4
|
+
|
|
5
|
+
import {defineResource} from "./defineResource.js";
|
|
6
|
+
|
|
7
|
+
describe("defineResource", () => {
|
|
8
|
+
beforeEach(() => PlatformTest.create());
|
|
9
|
+
afterEach(() => PlatformTest.reset());
|
|
10
|
+
|
|
11
|
+
it("should wrap handler errors with structured MCP payloads", async () => {
|
|
12
|
+
const token = defineResource({
|
|
13
|
+
name: "failing-resource",
|
|
14
|
+
uri: "tsed://resource",
|
|
15
|
+
handler() {
|
|
16
|
+
throw new Error("boom");
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const definition = inject<any>(token);
|
|
21
|
+
const result = await definition.handler(new URL("tsed://resource"), {} as any);
|
|
22
|
+
|
|
23
|
+
expect(result).toEqual({
|
|
24
|
+
contents: [],
|
|
25
|
+
_meta: {
|
|
26
|
+
status_code: undefined,
|
|
27
|
+
code: "E_MCP_RESOURCE_ERROR",
|
|
28
|
+
error_name: "Error",
|
|
29
|
+
message: "boom",
|
|
30
|
+
request_id: expect.any(String),
|
|
31
|
+
resource: "failing-resource"
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("should derive error code from error name and status when available", async () => {
|
|
37
|
+
const token = defineResource({
|
|
38
|
+
name: "http-resource",
|
|
39
|
+
uri: "tsed://resource",
|
|
40
|
+
handler() {
|
|
41
|
+
const er = new Error("Not found") as Error & {status?: number; name: string};
|
|
42
|
+
er.name = "NotFound";
|
|
43
|
+
er.status = 404;
|
|
44
|
+
throw er;
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const definition = inject<any>(token);
|
|
49
|
+
const result = await definition.handler(new URL("tsed://resource"), {} as any);
|
|
50
|
+
|
|
51
|
+
expect(result).toEqual({
|
|
52
|
+
contents: [],
|
|
53
|
+
_meta: {
|
|
54
|
+
status_code: 404,
|
|
55
|
+
code: "E_MCP_RESOURCE_NOT_FOUND",
|
|
56
|
+
error_name: "NotFound",
|
|
57
|
+
message: "Not found",
|
|
58
|
+
request_id: expect.any(String),
|
|
59
|
+
resource: "http-resource"
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("should handle primitive throws safely", async () => {
|
|
65
|
+
const token = defineResource({
|
|
66
|
+
name: "primitive-error-resource",
|
|
67
|
+
uri: "tsed://resource",
|
|
68
|
+
handler() {
|
|
69
|
+
throw "boom";
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
const definition = inject<any>(token);
|
|
74
|
+
const result = await definition.handler(new URL("tsed://resource"), {} as any);
|
|
75
|
+
|
|
76
|
+
expect(result).toEqual({
|
|
77
|
+
contents: [],
|
|
78
|
+
_meta: {
|
|
79
|
+
status_code: undefined,
|
|
80
|
+
code: "E_MCP_RESOURCE_ERROR",
|
|
81
|
+
error_name: undefined,
|
|
82
|
+
message: "boom",
|
|
83
|
+
request_id: expect.any(String),
|
|
84
|
+
resource: "primitive-error-resource"
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
});
|
package/src/fn/defineResource.ts
CHANGED
|
@@ -1,21 +1,35 @@
|
|
|
1
1
|
import type {ReadResourceCallback, ResourceMetadata, ResourceTemplate} from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
-
import {inject, injectable, type TokenProvider} from "@tsed/di";
|
|
2
|
+
import {context, inject, injectable, logger, type TokenProvider} from "@tsed/di";
|
|
3
3
|
import {JsonEntityStore} from "@tsed/schema";
|
|
4
|
+
import {constantCase} from "change-case";
|
|
4
5
|
|
|
5
6
|
import {MCP_PROVIDER_TYPES} from "../constants/constants.js";
|
|
6
7
|
|
|
7
|
-
type
|
|
8
|
+
type ResourceMetadataProps = ResourceMetadata & {
|
|
8
9
|
name: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type FnResourceReadProps = ResourceMetadataProps & {
|
|
13
|
+
uri: string;
|
|
14
|
+
handler: ReadResourceCallback;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type FnResourceTemplateProps = ResourceMetadataProps & {
|
|
18
|
+
template: ResourceTemplate;
|
|
9
19
|
handler: ReadResourceCallback;
|
|
10
|
-
token?: TokenProvider;
|
|
11
|
-
propertyKey?: string | symbol;
|
|
12
20
|
};
|
|
13
21
|
|
|
14
|
-
type
|
|
22
|
+
type ClassResourceBaseProps = Omit<ResourceMetadataProps, "name"> & {
|
|
23
|
+
name?: string;
|
|
24
|
+
token: TokenProvider;
|
|
25
|
+
propertyKey: string | symbol;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export type ClassResourceReadProps = ClassResourceBaseProps & {
|
|
15
29
|
uri: string;
|
|
16
30
|
};
|
|
17
31
|
|
|
18
|
-
type
|
|
32
|
+
export type ClassResourceTemplateProps = ClassResourceBaseProps & {
|
|
19
33
|
template: ResourceTemplate;
|
|
20
34
|
};
|
|
21
35
|
|
|
@@ -25,12 +39,17 @@ type ResourceTemplateProps = ResourceBaseProps & {
|
|
|
25
39
|
* @module platform/mcp
|
|
26
40
|
* @since 8.17.0
|
|
27
41
|
*/
|
|
28
|
-
export type ResourceProps =
|
|
42
|
+
export type ResourceProps = FnResourceReadProps | FnResourceTemplateProps | ClassResourceReadProps | ClassResourceTemplateProps;
|
|
43
|
+
|
|
44
|
+
function isClassResourceProps(options: ResourceProps): options is ClassResourceReadProps | ClassResourceTemplateProps {
|
|
45
|
+
return "token" in options && "propertyKey" in options;
|
|
46
|
+
}
|
|
29
47
|
|
|
30
48
|
function mapOptions(options: ResourceProps) {
|
|
31
49
|
let handler: ReadResourceCallback;
|
|
50
|
+
const name = options.name || ("propertyKey" in options ? String(options.propertyKey) : undefined);
|
|
32
51
|
|
|
33
|
-
if (
|
|
52
|
+
if (isClassResourceProps(options)) {
|
|
34
53
|
const {token, propertyKey} = options;
|
|
35
54
|
|
|
36
55
|
handler = (...args: any[]) => {
|
|
@@ -42,15 +61,18 @@ function mapOptions(options: ResourceProps) {
|
|
|
42
61
|
options.description = options.description || methodStore.operation.get("description");
|
|
43
62
|
options.title = options.title || methodStore.schema.get("title");
|
|
44
63
|
} else {
|
|
45
|
-
handler = options.handler
|
|
64
|
+
handler = options.handler;
|
|
46
65
|
}
|
|
47
66
|
|
|
48
67
|
return {
|
|
49
68
|
...options,
|
|
69
|
+
name,
|
|
50
70
|
handler
|
|
51
71
|
};
|
|
52
72
|
}
|
|
53
73
|
|
|
74
|
+
export type ResourceSettings = ReturnType<typeof mapOptions>;
|
|
75
|
+
|
|
54
76
|
/**
|
|
55
77
|
* Registers an MCP resource provider with the Ts.ED injector and returns its token.
|
|
56
78
|
*
|
|
@@ -68,13 +90,55 @@ function mapOptions(options: ResourceProps) {
|
|
|
68
90
|
* });
|
|
69
91
|
* ```
|
|
70
92
|
*/
|
|
71
|
-
export function defineResource(options:
|
|
72
|
-
export function defineResource(options:
|
|
93
|
+
export function defineResource(options: FnResourceReadProps): TokenProvider;
|
|
94
|
+
export function defineResource(options: FnResourceTemplateProps): TokenProvider;
|
|
95
|
+
export function defineResource(options: ClassResourceReadProps): TokenProvider;
|
|
96
|
+
export function defineResource(options: ClassResourceTemplateProps): TokenProvider;
|
|
73
97
|
export function defineResource(options: ResourceProps): TokenProvider {
|
|
74
98
|
const provider = injectable(Symbol.for(`MCP:RESOURCE:${options.name}`))
|
|
75
99
|
.type(MCP_PROVIDER_TYPES.RESOURCE)
|
|
76
100
|
.factory(() => {
|
|
77
|
-
|
|
101
|
+
const {handler, ...opts} = mapOptions(options);
|
|
102
|
+
|
|
103
|
+
return {
|
|
104
|
+
...opts,
|
|
105
|
+
async handler(...args: Parameters<ReadResourceCallback>) {
|
|
106
|
+
try {
|
|
107
|
+
return await handler(...args);
|
|
108
|
+
} catch (er: any) {
|
|
109
|
+
const safeErr =
|
|
110
|
+
er && typeof er === "object"
|
|
111
|
+
? er
|
|
112
|
+
: {
|
|
113
|
+
message: String(er),
|
|
114
|
+
name: undefined,
|
|
115
|
+
status: undefined
|
|
116
|
+
};
|
|
117
|
+
const code = safeErr.name && safeErr.status ? `E_MCP_RESOURCE_${constantCase(safeErr.name)}` : "E_MCP_RESOURCE_ERROR";
|
|
118
|
+
logger().error({
|
|
119
|
+
event: "MCP_RESOURCE_ERROR",
|
|
120
|
+
status_code: safeErr.status,
|
|
121
|
+
code,
|
|
122
|
+
error_name: safeErr.name,
|
|
123
|
+
message: safeErr.message,
|
|
124
|
+
request_id: context().id,
|
|
125
|
+
resource: opts.name
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
return {
|
|
129
|
+
contents: [],
|
|
130
|
+
_meta: {
|
|
131
|
+
status_code: safeErr.status,
|
|
132
|
+
code,
|
|
133
|
+
error_name: safeErr.name,
|
|
134
|
+
message: safeErr.message,
|
|
135
|
+
request_id: context().id,
|
|
136
|
+
resource: opts.name
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
};
|
|
78
142
|
});
|
|
79
143
|
|
|
80
144
|
return provider.token();
|
|
@@ -15,15 +15,41 @@ describe("defineTool", () => {
|
|
|
15
15
|
handler() {
|
|
16
16
|
throw new Error("boom");
|
|
17
17
|
}
|
|
18
|
-
}
|
|
18
|
+
});
|
|
19
19
|
|
|
20
20
|
const definition = inject<any>(token);
|
|
21
21
|
|
|
22
22
|
const result = await definition.handler({}, {} as any);
|
|
23
23
|
|
|
24
24
|
expect(result.structuredContent).toEqual({
|
|
25
|
+
status_code: undefined,
|
|
25
26
|
code: "E_MCP_TOOL_ERROR",
|
|
26
|
-
message: "boom"
|
|
27
|
+
message: "boom",
|
|
28
|
+
request_id: expect.any(String),
|
|
29
|
+
tool: "failing-tool"
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("should derive error code from error name and status when available", async () => {
|
|
34
|
+
const token = defineTool<any>({
|
|
35
|
+
name: "http-tool",
|
|
36
|
+
handler() {
|
|
37
|
+
const er = new Error("Not found") as Error & {status?: number; name: string};
|
|
38
|
+
er.name = "NotFound";
|
|
39
|
+
er.status = 404;
|
|
40
|
+
throw er;
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const definition = inject<any>(token);
|
|
45
|
+
const result = await definition.handler({}, {} as any);
|
|
46
|
+
|
|
47
|
+
expect(result.structuredContent).toEqual({
|
|
48
|
+
status_code: 404,
|
|
49
|
+
code: "E_MCP_TOOL_NOT_FOUND",
|
|
50
|
+
message: "Not found",
|
|
51
|
+
request_id: expect.any(String),
|
|
52
|
+
tool: "http-tool"
|
|
27
53
|
});
|
|
28
54
|
});
|
|
29
55
|
});
|
package/src/fn/defineTool.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type {McpServer} from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
1
|
import type {RequestHandlerExtra} from "@modelcontextprotocol/sdk/shared/protocol.js";
|
|
3
|
-
import type {CallToolResult, ServerNotification, ServerRequest, Tool} from "@modelcontextprotocol/sdk/types.js";
|
|
2
|
+
import type {CallToolResult, ServerNotification, ServerRequest, Tool, ToolAnnotations} from "@modelcontextprotocol/sdk/types.js";
|
|
4
3
|
import {type AbstractType, isArrowFn, type Type} from "@tsed/core";
|
|
5
|
-
import {inject, injectable, logger} from "@tsed/di";
|
|
4
|
+
import {context, inject, injectable, logger, type TokenProvider} from "@tsed/di";
|
|
6
5
|
import {JsonEntityStore, JsonMethodStore, JsonSchema} from "@tsed/schema";
|
|
6
|
+
import {constantCase} from "change-case";
|
|
7
7
|
|
|
8
8
|
import {MCP_PROVIDER_TYPES} from "../constants/constants.js";
|
|
9
9
|
import {toZod} from "../utils/toZod.js";
|
|
@@ -20,13 +20,19 @@ export type ToolCallback<Args = undefined> = (
|
|
|
20
20
|
extra: RequestHandlerExtra<ServerRequest, ServerNotification>
|
|
21
21
|
) => CallToolResult | Promise<CallToolResult>;
|
|
22
22
|
|
|
23
|
-
type
|
|
24
|
-
|
|
23
|
+
type BaseToolConfig = {
|
|
24
|
+
title?: string;
|
|
25
|
+
description?: string;
|
|
26
|
+
annotations?: ToolAnnotations;
|
|
27
|
+
_meta?: Record<string, unknown>;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
type BaseToolProps<Input, Output = undefined> = BaseToolConfig & {
|
|
25
31
|
inputSchema?: JsonSchema<Input> | (() => JsonSchema<Input>) | Tool["inputSchema"];
|
|
26
32
|
outputSchema?: JsonSchema<Output> | Tool["outputSchema"];
|
|
27
33
|
};
|
|
28
34
|
|
|
29
|
-
type FnToolProps<Input, Output = undefined> = BaseToolProps<Input, Output> & {
|
|
35
|
+
export type FnToolProps<Input, Output = undefined> = BaseToolProps<Input, Output> & {
|
|
30
36
|
name: string;
|
|
31
37
|
handler: ToolCallback<Input>;
|
|
32
38
|
};
|
|
@@ -103,6 +109,8 @@ function mapOptions<Input, Output = undefined>(options: ToolProps<Input, Output>
|
|
|
103
109
|
* });
|
|
104
110
|
* ```
|
|
105
111
|
*/
|
|
112
|
+
export function defineTool<Input, Output = undefined>(options: FnToolProps<Input, Output>): TokenProvider;
|
|
113
|
+
export function defineTool<Input, Output = undefined>(options: ClassToolProps<Input, Output>): TokenProvider;
|
|
106
114
|
export function defineTool<Input, Output = undefined>(options: ToolProps<Input, Output>) {
|
|
107
115
|
const provider = injectable(Symbol.for(`MCP:TOOL:${options.name}`))
|
|
108
116
|
.type(MCP_PROVIDER_TYPES.TOOL)
|
|
@@ -118,18 +126,25 @@ export function defineTool<Input, Output = undefined>(options: ToolProps<Input,
|
|
|
118
126
|
try {
|
|
119
127
|
return await handler(args as Input, extra);
|
|
120
128
|
} catch (er: any) {
|
|
129
|
+
const code = er.name && er.status ? `E_MCP_TOOL_${constantCase(er.name)}` : "E_MCP_TOOL_ERROR";
|
|
121
130
|
logger().error({
|
|
122
131
|
event: "MCP_TOOL_ERROR",
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
132
|
+
status_code: er.status,
|
|
133
|
+
code,
|
|
134
|
+
error_name: er.name,
|
|
135
|
+
message: er.message,
|
|
136
|
+
request_id: context().id,
|
|
137
|
+
tool: opts.name
|
|
126
138
|
});
|
|
127
139
|
|
|
128
140
|
return {
|
|
129
141
|
content: [],
|
|
130
142
|
structuredContent: {
|
|
131
|
-
|
|
132
|
-
|
|
143
|
+
status_code: er.status,
|
|
144
|
+
code,
|
|
145
|
+
message: er.message,
|
|
146
|
+
request_id: context().id,
|
|
147
|
+
tool: opts.name
|
|
133
148
|
}
|
|
134
149
|
} satisfies CallToolResult;
|
|
135
150
|
}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import type {ServerOptions} from "@modelcontextprotocol/sdk/server/index.js";
|
|
1
2
|
import type {StreamableHTTPServerTransportOptions} from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
3
|
+
import type {Implementation} from "@modelcontextprotocol/sdk/types.js";
|
|
2
4
|
import type {TokenProvider} from "@tsed/di";
|
|
3
5
|
|
|
4
6
|
/**
|
|
@@ -24,12 +26,32 @@ export interface PlatformMcpSettings {
|
|
|
24
26
|
* Optional version reported to MCP clients. Falls back to the Ts.ED app version.
|
|
25
27
|
*/
|
|
26
28
|
version?: string;
|
|
29
|
+
/**
|
|
30
|
+
* Optional human-readable server title exposed to MCP clients.
|
|
31
|
+
*/
|
|
32
|
+
title?: Implementation["title"];
|
|
33
|
+
/**
|
|
34
|
+
* Optional server description exposed to MCP clients.
|
|
35
|
+
*/
|
|
36
|
+
description?: Implementation["description"];
|
|
37
|
+
/**
|
|
38
|
+
* Optional canonical website URL exposed to MCP clients.
|
|
39
|
+
*/
|
|
40
|
+
websiteUrl?: Implementation["websiteUrl"];
|
|
41
|
+
/**
|
|
42
|
+
* Optional icon descriptors exposed to MCP clients.
|
|
43
|
+
*/
|
|
44
|
+
icons?: Implementation["icons"];
|
|
27
45
|
/**
|
|
28
46
|
* Optional tokens registered through configuration.
|
|
29
47
|
*/
|
|
30
48
|
tools?: TokenProvider[];
|
|
31
49
|
resources?: TokenProvider[];
|
|
32
50
|
prompts?: TokenProvider[];
|
|
51
|
+
/**
|
|
52
|
+
* Optional low-level MCP server options forwarded to `new McpServer(serverInfo, options)`.
|
|
53
|
+
*/
|
|
54
|
+
serverOptions?: ServerOptions;
|
|
33
55
|
/**
|
|
34
56
|
* Optional transport options for the MCP server.
|
|
35
57
|
*/
|
|
@@ -4,7 +4,7 @@ import {constant, inject, injectable, injector, type TokenProvider} from "@tsed/
|
|
|
4
4
|
|
|
5
5
|
import {MCP_PROVIDER_TYPES} from "../constants/constants.js";
|
|
6
6
|
import type {PromptsSettings} from "../fn/definePrompt.js";
|
|
7
|
-
import type {
|
|
7
|
+
import type {ResourceSettings} from "../fn/defineResource.js";
|
|
8
8
|
import type {ToolProps} from "../fn/defineTool.js";
|
|
9
9
|
import type {PlatformMcpSettings} from "../interfaces/PlatformMcpSettings.js";
|
|
10
10
|
|
|
@@ -29,11 +29,19 @@ export const MCP_SERVER = injectable(McpServer)
|
|
|
29
29
|
const settings = constant<PlatformMcpSettings>("mcp", {}) || {};
|
|
30
30
|
const name = settings.name || constant<string>("name") || "tsed-mcp";
|
|
31
31
|
const version = settings.version || constant<string>("version") || "0.0.0";
|
|
32
|
+
const {websiteUrl, description, title, icons} = settings;
|
|
32
33
|
|
|
33
|
-
const server = new McpServer(
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
const server = new McpServer(
|
|
35
|
+
{
|
|
36
|
+
websiteUrl,
|
|
37
|
+
description,
|
|
38
|
+
icons,
|
|
39
|
+
title,
|
|
40
|
+
name,
|
|
41
|
+
version
|
|
42
|
+
},
|
|
43
|
+
settings?.serverOptions
|
|
44
|
+
);
|
|
37
45
|
|
|
38
46
|
const toolTokens = collectTokens(MCP_PROVIDER_TYPES.TOOL, settings.tools);
|
|
39
47
|
toolTokens.forEach((token) => {
|
|
@@ -44,16 +52,22 @@ export const MCP_SERVER = injectable(McpServer)
|
|
|
44
52
|
|
|
45
53
|
const resourceTokens = collectTokens(MCP_PROVIDER_TYPES.RESOURCE, settings.resources);
|
|
46
54
|
resourceTokens.forEach((token) => {
|
|
47
|
-
const definition = inject<
|
|
55
|
+
const definition = inject<ResourceSettings & {uri?: string; template?: ResourceTemplate}>(token);
|
|
48
56
|
const {name, handler, uri, template, ...opts} = definition;
|
|
49
|
-
|
|
57
|
+
const resourceName = name || String(token);
|
|
58
|
+
|
|
59
|
+
if (uri) {
|
|
60
|
+
server.registerResource(resourceName, uri, opts, handler as any);
|
|
61
|
+
} else {
|
|
62
|
+
server.registerResource(resourceName, template as ResourceTemplate, opts, handler as any);
|
|
63
|
+
}
|
|
50
64
|
});
|
|
51
65
|
|
|
52
66
|
const promptTokens = collectTokens(MCP_PROVIDER_TYPES.PROMPT, settings.prompts);
|
|
53
67
|
promptTokens.forEach((token) => {
|
|
54
68
|
const definition = inject<PromptsSettings>(token);
|
|
55
69
|
const {name, handler, ...opts} = definition;
|
|
56
|
-
server.registerPrompt(name, opts, handler as any);
|
|
70
|
+
server.registerPrompt(name || String(token), opts as any, handler as any);
|
|
57
71
|
});
|
|
58
72
|
|
|
59
73
|
return server;
|