@tsed/platform-mcp 8.38.4 → 8.38.6
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/common/fn/definePrompt.js +4 -2
- package/lib/esm/common/fn/defineResource.js +4 -6
- package/lib/esm/common/fn/defineTool.js +2 -4
- package/lib/tsconfig.esm.tsbuildinfo +1 -1
- package/package.json +8 -8
- package/src/common/fn/definePrompt.spec.ts +19 -1
- package/src/common/fn/definePrompt.ts +5 -2
- package/src/common/fn/defineResource.spec.ts +20 -1
- package/src/common/fn/defineResource.ts +5 -7
- package/src/common/fn/defineTool.spec.ts +18 -1
- package/src/common/fn/defineTool.ts +3 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsed/platform-mcp",
|
|
3
|
-
"version": "8.38.
|
|
3
|
+
"version": "8.38.6",
|
|
4
4
|
"description": "Model Context Protocol integration for Ts.ED HTTP adapters.",
|
|
5
5
|
"source": "./src/http/index.ts",
|
|
6
6
|
"type": "module",
|
|
@@ -49,13 +49,13 @@
|
|
|
49
49
|
"uuid": ">=11.0.2"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
|
-
"@tsed/barrels": "8.38.
|
|
53
|
-
"@tsed/core": "8.38.
|
|
54
|
-
"@tsed/di": "8.38.
|
|
55
|
-
"@tsed/json-mapper": "8.38.
|
|
56
|
-
"@tsed/platform-http": "8.38.
|
|
57
|
-
"@tsed/schema": "8.38.
|
|
58
|
-
"@tsed/typescript": "8.38.
|
|
52
|
+
"@tsed/barrels": "8.38.6",
|
|
53
|
+
"@tsed/core": "8.38.6",
|
|
54
|
+
"@tsed/di": "8.38.6",
|
|
55
|
+
"@tsed/json-mapper": "8.38.6",
|
|
56
|
+
"@tsed/platform-http": "8.38.6",
|
|
57
|
+
"@tsed/schema": "8.38.6",
|
|
58
|
+
"@tsed/typescript": "8.38.6",
|
|
59
59
|
"typescript": "6.0.2",
|
|
60
60
|
"vitest": "4.1.4"
|
|
61
61
|
},
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {afterEach, beforeEach, describe, expect, it} from "vitest";
|
|
2
2
|
import {PlatformTest} from "@tsed/platform-http/testing";
|
|
3
3
|
import {definePrompt} from "./definePrompt.js";
|
|
4
|
-
import {inject} from "@tsed/di";
|
|
4
|
+
import {context, inject, runInContext} from "@tsed/di";
|
|
5
5
|
|
|
6
6
|
describe("definePrompt", () => {
|
|
7
7
|
beforeEach(() => PlatformTest.create());
|
|
@@ -57,4 +57,22 @@ describe("definePrompt", () => {
|
|
|
57
57
|
}
|
|
58
58
|
});
|
|
59
59
|
});
|
|
60
|
+
|
|
61
|
+
it("should expose its definition and invocation arguments in the execution context", async () => {
|
|
62
|
+
const args = {city: "Paris"};
|
|
63
|
+
const serverContext = {} as any;
|
|
64
|
+
const token = definePrompt({
|
|
65
|
+
name: "contextual-prompt",
|
|
66
|
+
handler() {
|
|
67
|
+
expect(context().get("mcp")).toMatchObject({name: "contextual-prompt"});
|
|
68
|
+
expect(context().get("mcp_args")).toEqual([args, serverContext]);
|
|
69
|
+
|
|
70
|
+
return {messages: []};
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const definition = inject<any>(token);
|
|
75
|
+
|
|
76
|
+
await runInContext(PlatformTest.createRequestContext(), () => definition.handler(args, serverContext));
|
|
77
|
+
});
|
|
60
78
|
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {type AbstractType, isArrowFn, type Type} from "@tsed/core";
|
|
2
2
|
import type {GetPromptResult, ServerContext} from "@modelcontextprotocol/server";
|
|
3
3
|
import {JsonSchema, s} from "@tsed/schema";
|
|
4
|
-
import {context, inject, injectable,
|
|
4
|
+
import {context, inject, injectable, type TokenProvider} from "@tsed/di";
|
|
5
5
|
import {MCP_PROVIDER_TYPES} from "../constants/constants.js";
|
|
6
6
|
import {constantCase} from "change-case";
|
|
7
7
|
import {fromJsonSchema} from "../utils/fromJsonSchema.js";
|
|
@@ -109,10 +109,13 @@ export function definePrompt<Args = any>(options: PromptProps<Args>) {
|
|
|
109
109
|
...opts,
|
|
110
110
|
async handler(...args: any[]) {
|
|
111
111
|
try {
|
|
112
|
+
context()?.set("mcp", opts);
|
|
113
|
+
context()?.set("mcp_args", args);
|
|
114
|
+
|
|
112
115
|
return await (handler as any)(...args);
|
|
113
116
|
} catch (er: any) {
|
|
114
117
|
const code = er.name && er.status ? `E_MCP_PROMPT_${constantCase(er.name)}` : "E_MCP_PROMPT_ERROR";
|
|
115
|
-
|
|
118
|
+
context().logger.error({
|
|
116
119
|
event: "MCP_PROMPT_ERROR",
|
|
117
120
|
status_code: er.status,
|
|
118
121
|
code,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {afterEach, beforeEach, describe, expect, it} from "vitest";
|
|
2
2
|
import {PlatformTest} from "@tsed/platform-http/testing";
|
|
3
3
|
import {defineResource} from "./defineResource.js";
|
|
4
|
-
import {inject} from "@tsed/di";
|
|
4
|
+
import {context, inject, runInContext} from "@tsed/di";
|
|
5
5
|
|
|
6
6
|
describe("defineResource", () => {
|
|
7
7
|
beforeEach(() => PlatformTest.create());
|
|
@@ -109,4 +109,23 @@ describe("defineResource", () => {
|
|
|
109
109
|
]
|
|
110
110
|
});
|
|
111
111
|
});
|
|
112
|
+
|
|
113
|
+
it("should expose its definition and invocation arguments in the execution context", async () => {
|
|
114
|
+
const uri = new URL("tsed://contextual-resource");
|
|
115
|
+
const extra = {} as any;
|
|
116
|
+
const token = defineResource({
|
|
117
|
+
name: "contextual-resource",
|
|
118
|
+
uri: "tsed://contextual-resource",
|
|
119
|
+
handler() {
|
|
120
|
+
expect(context().get("mcp")).toMatchObject({name: "contextual-resource"});
|
|
121
|
+
expect(context().get("mcp_args")).toEqual([uri, extra]);
|
|
122
|
+
|
|
123
|
+
return {id: "contextual-resource"} as any;
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
const definition = inject<any>(token);
|
|
128
|
+
|
|
129
|
+
await runInContext(PlatformTest.createRequestContext(), () => definition.handler(uri, extra));
|
|
130
|
+
});
|
|
112
131
|
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type {ReadResourceCallback, ReadResourceResult, ResourceMetadata, ResourceTemplate} from "@modelcontextprotocol/server";
|
|
2
|
-
import {context, inject, injectable,
|
|
2
|
+
import {context, inject, injectable, type TokenProvider} from "@tsed/di";
|
|
3
3
|
import {MCP_PROVIDER_TYPES} from "../constants/constants.js";
|
|
4
4
|
import {constantCase} from "change-case";
|
|
5
5
|
import {s} from "@tsed/schema";
|
|
@@ -108,12 +108,10 @@ export function defineResource(options: ResourceProps): TokenProvider {
|
|
|
108
108
|
...opts,
|
|
109
109
|
async handler(...args: Parameters<ReadResourceCallback>) {
|
|
110
110
|
try {
|
|
111
|
-
|
|
111
|
+
context()?.set("mcp", opts);
|
|
112
|
+
context()?.set("mcp_args", args);
|
|
112
113
|
|
|
113
|
-
|
|
114
|
-
event: "MCP_TOOL_END",
|
|
115
|
-
tool: opts.name
|
|
116
|
-
});
|
|
114
|
+
const result = await handler(...args);
|
|
117
115
|
|
|
118
116
|
return asResourceResponse(args[0]?.toString(), result);
|
|
119
117
|
} catch (er: any) {
|
|
@@ -127,7 +125,7 @@ export function defineResource(options: ResourceProps): TokenProvider {
|
|
|
127
125
|
};
|
|
128
126
|
const code = safeErr.name && safeErr.status ? `E_MCP_RESOURCE_${constantCase(safeErr.name)}` : "E_MCP_RESOURCE_ERROR";
|
|
129
127
|
|
|
130
|
-
|
|
128
|
+
context().logger.error({
|
|
131
129
|
event: "MCP_RESOURCE_ERROR",
|
|
132
130
|
status_code: safeErr.status,
|
|
133
131
|
code,
|
|
@@ -2,7 +2,7 @@ import {Default, from, JsonSchema, Name, Property, s, string} from "@tsed/schema
|
|
|
2
2
|
import {afterEach, beforeEach, describe, expect, it} from "vitest";
|
|
3
3
|
import {PlatformTest} from "@tsed/platform-http/testing";
|
|
4
4
|
import {defineTool} from "./defineTool.js";
|
|
5
|
-
import {inject} from "@tsed/di";
|
|
5
|
+
import {context, inject, runInContext} from "@tsed/di";
|
|
6
6
|
|
|
7
7
|
class KnowledgeSearchRequest {
|
|
8
8
|
@Property()
|
|
@@ -169,4 +169,21 @@ describe("defineTool", () => {
|
|
|
169
169
|
structuredContent: {id: "tool-id"}
|
|
170
170
|
});
|
|
171
171
|
});
|
|
172
|
+
|
|
173
|
+
it("should expose its definition and input in the execution context", async () => {
|
|
174
|
+
const args = {query: "Ts.ED"};
|
|
175
|
+
const token = defineTool({
|
|
176
|
+
name: "contextual-tool",
|
|
177
|
+
handler() {
|
|
178
|
+
expect(context().get("mcp")).toMatchObject({name: "contextual-tool"});
|
|
179
|
+
expect(context().get("mcp_args")).toEqual(args);
|
|
180
|
+
|
|
181
|
+
return {content: []};
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
const definition = inject<any>(token);
|
|
186
|
+
|
|
187
|
+
await runInContext(PlatformTest.createRequestContext(), () => definition.handler(args, {} as any));
|
|
188
|
+
});
|
|
172
189
|
});
|
|
@@ -163,12 +163,10 @@ export function defineTool<Input, Output = unknown>(options: ToolProps<Input, Ou
|
|
|
163
163
|
}),
|
|
164
164
|
async handler(args: Input, ctx: ServerContext) {
|
|
165
165
|
try {
|
|
166
|
-
|
|
166
|
+
context()?.set("mcp", opts);
|
|
167
|
+
context()?.set("mcp_args", args);
|
|
167
168
|
|
|
168
|
-
|
|
169
|
-
event: "MCP_TOOL_END",
|
|
170
|
-
tool: opts.name
|
|
171
|
-
});
|
|
169
|
+
const result = await handler(deserializeInput(args, inputSchema, inputStore), ctx);
|
|
172
170
|
|
|
173
171
|
return asToolResponse(result as unknown as Record<string, unknown>);
|
|
174
172
|
} catch (er: any) {
|