tmcp 1.6.0 → 1.6.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +20 -12
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tmcp",
3
- "version": "1.6.0",
3
+ "version": "1.6.2",
4
4
  "description": "The main tmcp library",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
package/src/index.js CHANGED
@@ -17,6 +17,7 @@ import {
17
17
  CreateMessageResultSchema,
18
18
  GetPromptResultSchema,
19
19
  InitializeRequestParamsSchema,
20
+ JSONRPCNotificationSchema,
20
21
  JSONRPCRequestSchema,
21
22
  JSONRPCResponseSchema,
22
23
  McpError,
@@ -316,7 +317,7 @@ export class McpServer {
316
317
  async ({ name, arguments: args }) => {
317
318
  const tool = this.#tools.get(name);
318
319
  if (!tool) {
319
- throw new Error(`Tool ${name} not found`);
320
+ throw new McpError(-32601, `Tool ${name} not found`);
320
321
  }
321
322
  if (!tool.schema) {
322
323
  return v.parse(CallToolResultSchema, await tool.execute());
@@ -325,7 +326,8 @@ export class McpServer {
325
326
  if (validated_args instanceof Promise)
326
327
  validated_args = await validated_args;
327
328
  if (validated_args.issues) {
328
- throw new Error(
329
+ throw new McpError(
330
+ -32602,
329
331
  `Invalid arguments for tool ${name}: ${JSON.stringify(validated_args.issues)}`,
330
332
  );
331
333
  }
@@ -402,7 +404,7 @@ export class McpServer {
402
404
  async ({ name, arguments: args }) => {
403
405
  const prompt = this.#prompts.get(name);
404
406
  if (!prompt) {
405
- throw new Error(`Tool ${name} not found`);
407
+ throw new McpError(-32601, `Prompt ${name} not found`);
406
408
  }
407
409
  if (!prompt.schema) {
408
410
  return v.parse(
@@ -414,8 +416,9 @@ export class McpServer {
414
416
  if (validated_args instanceof Promise)
415
417
  validated_args = await validated_args;
416
418
  if (validated_args.issues) {
417
- throw new Error(
418
- `Invalid arguments for tool ${name}: ${JSON.stringify(validated_args.issues)}`,
419
+ throw new McpError(
420
+ -32602,
421
+ `Invalid arguments for prompt ${name}: ${JSON.stringify(validated_args.issues)}`,
419
422
  );
420
423
  }
421
424
  return v.parse(
@@ -511,12 +514,12 @@ export class McpServer {
511
514
  params = match.params;
512
515
  }
513
516
  if (!resource) {
514
- throw new Error(`Resource ${uri} not found`);
517
+ throw new McpError(-32601, `Resource ${uri} not found`);
515
518
  }
516
519
  }
517
520
  if (resource.template) {
518
521
  if (!params)
519
- throw new Error('Missing parameters for template resource');
522
+ throw new McpError(-32602, 'Missing parameters for template resource');
520
523
  return v.parse(
521
524
  ReadResourceResultSchema,
522
525
  await resource.execute(uri, params),
@@ -682,7 +685,10 @@ export class McpServer {
682
685
  */
683
686
  receive(message, session_id) {
684
687
  // Validate the message first
685
- const validated_message = v.safeParse(JSONRPCRequestSchema, message);
688
+ const validated_message = v.safeParse(
689
+ v.union([JSONRPCRequestSchema, JSONRPCNotificationSchema]),
690
+ message,
691
+ );
686
692
 
687
693
  // Check if it's a request or response
688
694
  if (validated_message.success) {
@@ -749,7 +755,7 @@ export class McpServer {
749
755
  */
750
756
  async elicitation(schema) {
751
757
  if (!this.#client_capabilities?.elicitation)
752
- throw new Error("Client doesn't support elicitation");
758
+ throw new McpError(-32601, "Client doesn't support elicitation");
753
759
 
754
760
  this.#lazyily_create_client();
755
761
  let validated_result = schema['~standard'].validate(
@@ -766,7 +772,8 @@ export class McpServer {
766
772
  if (validated_result instanceof Promise)
767
773
  validated_result = await validated_result;
768
774
  if (validated_result.issues) {
769
- throw new Error(
775
+ throw new McpError(
776
+ -32603,
770
777
  `Invalid elicitation result: ${JSON.stringify(validated_result.issues)}`,
771
778
  );
772
779
  }
@@ -780,7 +787,7 @@ export class McpServer {
780
787
  */
781
788
  async message(request) {
782
789
  if (!this.#client_capabilities?.sampling)
783
- throw new Error("Client doesn't support sampling");
790
+ throw new McpError(-32601, "Client doesn't support sampling");
784
791
 
785
792
  this.#lazyily_create_client();
786
793
 
@@ -811,7 +818,8 @@ export class McpServer {
811
818
  */
812
819
  log(level, data, logger) {
813
820
  if (!this.#options.capabilities?.logging) {
814
- throw new Error(
821
+ throw new McpError(
822
+ -32601,
815
823
  "The server doesn't support logging, please enable it in capabilities",
816
824
  );
817
825
  }