@tsed/platform-mcp 8.38.4 → 8.38.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsed/platform-mcp",
3
- "version": "8.38.4",
3
+ "version": "8.38.5",
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.4",
53
- "@tsed/core": "8.38.4",
54
- "@tsed/di": "8.38.4",
55
- "@tsed/json-mapper": "8.38.4",
56
- "@tsed/platform-http": "8.38.4",
57
- "@tsed/schema": "8.38.4",
58
- "@tsed/typescript": "8.38.4",
52
+ "@tsed/barrels": "8.38.5",
53
+ "@tsed/core": "8.38.5",
54
+ "@tsed/di": "8.38.5",
55
+ "@tsed/json-mapper": "8.38.5",
56
+ "@tsed/platform-http": "8.38.5",
57
+ "@tsed/schema": "8.38.5",
58
+ "@tsed/typescript": "8.38.5",
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, logger, type TokenProvider} from "@tsed/di";
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
- logger().error({
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, logger, type TokenProvider} from "@tsed/di";
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
- const result = await handler(...args);
111
+ context()?.set("mcp", opts);
112
+ context()?.set("mcp_args", args);
112
113
 
113
- logger().info({
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
- logger().error({
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
- const result = await handler(deserializeInput(args, inputSchema, inputStore), ctx);
166
+ context()?.set("mcp", opts);
167
+ context()?.set("mcp_args", args);
167
168
 
168
- logger().info({
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) {